Skip to content

Commit

Permalink
Wrap a couple of ImGui enums into osc::ui:: (ongoing refactor)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Aug 8, 2024
1 parent de3a1c0 commit a8bcf02
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/OpenSimCreator/UI/MainUIScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,26 +473,26 @@ class osc::MainUIScreen::Impl final :
{
for (size_t i = 0; i < m_Tabs.size(); ++i)
{
ImGuiTabItemFlags flags = ImGuiTabItemFlags_NoReorder;
ui::TabItemFlags flags = ui::TabItemFlag::NoReorder;

if (i == 0)
{
flags |= ImGuiTabItemFlags_NoCloseButton; // splash screen
flags |= ui::TabItemFlag::NoCloseButton; // splash screen
}

if (m_Tabs[i]->is_unsaved())
{
flags |= ImGuiTabItemFlags_UnsavedDocument;
flags |= ui::TabItemFlag::UnsavedDocument;
}

if (m_Tabs[i]->id() == m_RequestedTab)
{
flags |= ImGuiTabItemFlags_SetSelected;
flags |= ui::TabItemFlag::SetSelected;;
}

if (m_Tabs[i]->id() == m_ActiveTabID && m_Tabs[i]->name() != m_ActiveTabNameLastFrame)
{
flags |= ImGuiTabItemFlags_SetSelected;
flags |= ui::TabItemFlag::SetSelected;
m_ActiveTabNameLastFrame = m_Tabs[i]->name();
}

Expand Down
2 changes: 1 addition & 1 deletion src/OpenSimCreator/UI/Shared/NavigatorPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class osc::NavigatorPanel::Impl final : public StandardPanelImpl {

// handle display mode (node vs leaf)
const bool isInternalNode = currentPath.size() < 2 || lookaheadPath.size() > currentPath.size();
const ImGuiTreeNodeFlags nodeFlags = isInternalNode ? ImGuiTreeNodeFlags_OpenOnArrow : (ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet);
const ui::TreeNodeFlags nodeFlags = isInternalNode ? ui::TreeNodeFlag::OpenOnArrow : ui::TreeNodeFlags{ui::TreeNodeFlag::Leaf, ui::TreeNodeFlag::Bullet};

// handle coloring
int styles = 0;
Expand Down
41 changes: 37 additions & 4 deletions src/oscar/UI/oscimgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,39 @@ namespace
default: return ImGuizmo::MODE::WORLD;
}
}

ImGuiTreeNodeFlags to_ImGuiTreeNodeFlags(ui::TreeNodeFlags flags)
{
ImGuiTreeNodeFlags rv = 0;
if (flags & ui::TreeNodeFlag::OpenOnArrow) {
rv |= ImGuiTreeNodeFlags_OpenOnArrow;
}
if (flags & ui::TreeNodeFlag::Leaf) {
rv |= ImGuiTreeNodeFlags_Leaf;
}
if (flags & ui::TreeNodeFlag::Bullet) {
rv |= ImGuiTreeNodeFlags_Bullet;
}
return rv;
}

ImGuiTabItemFlags to_ImGuiTabItemFlags(ui::TabItemFlags flags)
{
ImGuiTabItemFlags rv = 0;
if (flags & ui::TabItemFlag::NoReorder) {
rv |= ImGuiTabItemFlags_NoReorder;
}
if (flags & ui::TabItemFlag::NoCloseButton) {
rv |= ImGuiTabItemFlags_NoCloseButton;
}
if (flags & ui::TabItemFlag::UnsavedDocument) {
rv |= ImGuiTabItemFlags_UnsavedDocument;
}
if (flags & ui::TabItemFlag::SetSelected) {
rv |= ImGuiTabItemFlags_SetSelected;
}
return rv;
}
}

void osc::ui::align_text_to_frame_padding()
Expand Down Expand Up @@ -154,9 +187,9 @@ void osc::ui::draw_text_bullet_pointed(CStringView str)
ImGui::BulletText("%s", str.c_str());
}

bool osc::ui::draw_tree_node_ex(CStringView label, ImGuiTreeNodeFlags flags)
bool osc::ui::draw_tree_node_ex(CStringView label, ui::TreeNodeFlags flags)
{
return ImGui::TreeNodeEx(label.c_str(), flags);
return ImGui::TreeNodeEx(label.c_str(), to_ImGuiTreeNodeFlags(flags));
}

float osc::ui::get_tree_node_to_label_spacing()
Expand Down Expand Up @@ -212,9 +245,9 @@ void osc::ui::end_tab_bar()
ImGui::EndTabBar();
}

bool osc::ui::begin_tab_item(CStringView label, bool* p_open, ImGuiTabItemFlags flags)
bool osc::ui::begin_tab_item(CStringView label, bool* p_open, TabItemFlags flags)
{
return ImGui::BeginTabItem(label.c_str(), p_open, flags);
return ImGui::BeginTabItem(label.c_str(), p_open, to_ImGuiTabItemFlags(flags));
}

void osc::ui::end_tab_item()
Expand Down
21 changes: 19 additions & 2 deletions src/oscar/UI/oscimgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <oscar/Maths/Vec4.h>
#include <oscar/Shims/Cpp23/utility.h>
#include <oscar/Utils/CStringView.h>
#include <oscar/Utils/EnumHelpers.h>
#include <oscar/Utils/Flags.h>
#include <oscar/Utils/UID.h>

Expand Down Expand Up @@ -99,7 +100,15 @@ namespace osc::ui

void draw_text_bullet_pointed(CStringView);

bool draw_tree_node_ex(CStringView, ImGuiTreeNodeFlags = 0);
enum class TreeNodeFlag {
None = 0,
OpenOnArrow = 1<<0, // only open when clicking on the arrow part
Leaf = 1<<1, // no collapsing, no arrow (use as a convenience for leaf nodes)
Bullet = 1<<2, // display a bullet instead of arrow. IMPORTANT: node can still be marked open/close if you don't also set the `Leaf` flag!
};
using TreeNodeFlags = Flags<TreeNodeFlag>;

bool draw_tree_node_ex(CStringView, TreeNodeFlags = {});

float get_tree_node_to_label_spacing();

Expand Down Expand Up @@ -128,8 +137,16 @@ namespace osc::ui
bool begin_tab_bar(CStringView str_id);
void end_tab_bar();

bool begin_tab_item(CStringView label, bool* p_open = nullptr, ImGuiTabItemFlags flags = 0);
enum class TabItemFlag {
None = 0,
NoReorder = 1<<0, // disable reordering this tab or having another tab cross over this tab
NoCloseButton = 1<<1, // track whether `p_open` was set or not (we'll need this info on the next frame to recompute ContentWidth during layout)
UnsavedDocument = 1<<2, // display a dot next to the title + (internally) set `ImGuiTabItemFlags_NoAssumedClosure`
SetSelected = 1<<3, // trigger flag to programmatically make the tab selected when calling `begin_tab_item`
};
using TabItemFlags = Flags<TabItemFlag>;

bool begin_tab_item(CStringView label, bool* p_open = nullptr, TabItemFlags = {});
void end_tab_item();

bool draw_tab_item_button(CStringView label);
Expand Down

0 comments on commit a8bcf02

Please sign in to comment.