// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
#include
namespace ia::iae
{
INT32 g_tabContainerCount{0};
TabContainer::TabContainer()
: m_containerID(BuildString("TabContainer##", g_tabContainerCount++)),
m_tabBarID(BuildString("TabBar##", g_tabContainerCount++))
{
}
VOID TabContainer::Initialize()
{
}
VOID TabContainer::Terminate()
{
for (const auto &t : m_tabViews)
RemoveTab(t->Key);
}
VOID TabContainer::Draw(IN ImVec2 position, IN ImVec2 size)
{
ImGui::Begin(m_containerID.c_str(), nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoTitleBar);
ImGui::SetWindowPos(position);
ImGui::SetWindowSize(size);
ImGui::BeginTabBar(m_tabBarID.c_str());
for (const auto &v : m_tabViews)
{
ImGuiTabItemFlags flags{};
if (m_pendingActiveTabName && m_pendingActiveTabName == v->Key)
{
flags |= ImGuiTabItemFlags_SetSelected;
m_pendingActiveTabName = nullptr;
}
BOOL* isOpen = v->Value.IsCloseable ? &v->Value.IsOpen : nullptr;
if (ImGui::BeginTabItem(v->Value.View->IconAndName().c_str(), isOpen, flags))
{
v->Value.View->Render();
ImGui::EndTabItem();
m_activeTabName = v->Key.c_str();
}
}
ImGui::EndTabBar();
ImGui::End();
}
VOID TabContainer::Update()
{
for (const auto &t : m_tabViews)
t->Value.View->Update();
}
VOID TabContainer::ProcessEvent(IN SDL_Event *event)
{
for (const auto &t : m_tabViews)
t->Value.View->ProcessEvent(event);
}
VOID TabContainer::AddTab(IN CONST String &name, IN IView *view, IN BOOL isCloseable)
{
RemoveTab(name);
view->Initialize();
m_tabViews[name] = Tab{.View = view, .IsCloseable = isCloseable};
if (!m_activeTabName)
m_activeTabName = name.c_str();
}
VOID TabContainer::RemoveTab(IN CONST String &name)
{
if (!m_tabViews.contains(name))
return;
m_tabViews[name].IsOpen = false;
if (m_tabViews[name].View)
m_tabViews[name].View->Terminate();
delete m_tabViews[name].View;
m_tabViews[name] = {};
}
VOID TabContainer::OpenTab(IN CONST String &name)
{
if (!m_tabViews.contains(name))
return;
m_tabViews[name].IsOpen = true;
}
VOID TabContainer::CloseTab(IN CONST String &name)
{
if (!m_tabViews.contains(name))
return;
m_tabViews[name].IsOpen = false;
}
VOID TabContainer::ChangeActiveTab(IN PCCHAR name)
{
m_pendingActiveTabName = name;
}
IView *TabContainer::GetTab(IN CONST String &name)
{
return m_tabViews[name].View;
}
} // namespace ia::iae