Skip to content

Commit

Permalink
Add PushTweakedTheme / PopTweakedTheme
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed May 26, 2024
1 parent 3a7f7ba commit d2169ac
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/hello_imgui/imgui_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ namespace ImGuiTheme
{
ImGuiTheme_ Theme = ImGuiTheme_DarculaDarker;
ImGuiThemeTweaks Tweaks = ImGuiThemeTweaks();

ImGuiTweakedTheme(ImGuiTheme_ theme = ImGuiTheme_DarculaDarker, const ImGuiThemeTweaks& tweaks = ImGuiThemeTweaks())
: Theme(theme), Tweaks(tweaks) {}
};

ImGuiStyle TweakedThemeThemeToStyle(const ImGuiTweakedTheme& tweaked_theme);
void ApplyTweakedTheme(const ImGuiTweakedTheme& tweaked_theme);

void PushTweakedTheme(const ImGuiTweakedTheme& tweaked_theme);
void PopTweakedTheme();

// Show the theme selection listbox, the theme tweak widgets, as well as ImGui::ShowStyleEditor. Returns true if modified (Warning, when using ShowStyleEditor, no info about modification is transmitted)
bool ShowThemeTweakGui(ImGuiTweakedTheme *tweaked_theme);

Expand Down
17 changes: 17 additions & 0 deletions src/hello_imgui/impl/imgui_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
#include "hello_imgui/imgui_theme.h"
#include <string>
#include <stack>

namespace ImGuiTheme
{
Expand Down Expand Up @@ -970,6 +971,22 @@ namespace ImGuiTheme
ImGui::GetStyle() = TweakedThemeThemeToStyle(tweaked_theme);
}

std::stack<ImGuiStyle> gPreviousStyles_PushTweakedTheme;

void PushTweakedTheme(const ImGuiTweakedTheme& tweaked_theme)
{
gPreviousStyles_PushTweakedTheme.push(ImGui::GetStyle());
ApplyTweakedTheme(tweaked_theme);
}

void PopTweakedTheme()
{
IM_ASSERT(!gPreviousStyles_PushTweakedTheme.empty());
ImGui::GetStyle() = gPreviousStyles_PushTweakedTheme.top();
gPreviousStyles_PushTweakedTheme.pop();
}


bool _ShowThemeSelector(ImGuiTheme_* theme)
{
bool changed = false;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ struct AppState

MyAppSettings myAppSettings; // This values will be stored in the application settings

HelloImGui::FontDpiResponsive *TitleFont;
HelloImGui::FontDpiResponsive *ColorFont;
HelloImGui::FontDpiResponsive *EmojiFont;
HelloImGui::FontDpiResponsive *LargeIconFont;
HelloImGui::FontDpiResponsive *TitleFont;
HelloImGui::FontDpiResponsive *ColorFont;
HelloImGui::FontDpiResponsive *EmojiFont;
HelloImGui::FontDpiResponsive *LargeIconFont;
};


Expand Down Expand Up @@ -332,6 +332,108 @@ void GuiWindowLayoutCustomization(AppState& appState)
}


void GuiWindowAlternativeTheme(AppState& appState)
{
// Since this window applies a theme, We need to call "ImGui::Begin" ourselves so
// that we can apply the theme before opening the window.
// Note: we did set this option for that:
// alternativeThemeWindow.callBeginEnd = false;

ImGuiTheme::ImGuiTweakedTheme tweakedTheme;
tweakedTheme.Theme = ImGuiTheme::ImGuiTheme_WhiteIsWhite;
tweakedTheme.Tweaks.Rounding = 0.0f;

ImGuiTheme::PushTweakedTheme(tweakedTheme);

bool windowOpened = ImGui::Begin("Alternative Theme");
if (windowOpened)
{
ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Alternative Theme"); ImGui::PopFont();
ImGui::Text("This window uses a different theme");

if (ImGui::CollapsingHeader("Basic Widgets", ImGuiTreeNodeFlags_DefaultOpen))
{
static bool checked = true;
ImGui::Checkbox("Checkbox", &checked);

if (ImGui::Button("Button"))
HelloImGui::Log(HelloImGui::LogLevel::Info, "Button was pressed");
ImGui::SetItemTooltip("This is a button");

static int radio = 0;
ImGui::RadioButton("Radio 1", &radio, 0); ImGui::SameLine();
ImGui::RadioButton("Radio 2", &radio, 1); ImGui::SameLine();
ImGui::RadioButton("Radio 3", &radio, 2);

// Haiku
{
// Display a image of the haiku below with Japanese characters
// with an informative tooltip
float haikuImageHeight = HelloImGui::EmSize(5.f);
HelloImGui::ImageFromAsset("images/haiku.png", ImVec2(0.f, haikuImageHeight));
ImGui::SetItemTooltip(R"(
From wikipedia:
---------------
In early 1686, Bashō composed one of his best-remembered haiku:
furu ike ya / kawazu tobikomu / mizu no oto
an ancient pond / a frog jumps in / the splash of water
This poem became instantly famous.
---------------------------------------------------------------------
This haiku is here rendered as an image, mainly to preserve space,
because adding a Japanese font to the project would enlarge its size.
Handling Japanese font is of course possible within ImGui / Hello ImGui!
)");

// Display the haiku text as an InputTextMultiline
static std::string poem =
" Old Pond\n"
" Frog Leaps In\n"
" Water's Sound\n"
"\n"
" Matsuo Bashō - 1686";
ImGui::InputTextMultiline("##Poem", &poem, HelloImGui::EmToVec2(15.f, 5.5f));
}

// A popup with a modal window
if (ImGui::Button("Open Modal"))
ImGui::OpenPopup("MyModal");
if (ImGui::BeginPopupModal("MyModal", NULL, ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::Text("This is a modal window");
if (ImGui::Button("Close"))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}

static std::string text = "Hello, world!";
ImGui::InputText("Input text", &text);

if (ImGui::TreeNode("Text Display"))
{
ImGui::Text("Hello, world!");
ImGui::TextColored(ImVec4(1.f, 0.5f, 0.5f, 1.f), "Some text");
ImGui::TextDisabled("Disabled text");
ImGui::TextWrapped("This is a long text that will be wrapped in the window");
ImGui::TreePop();
}

}


}

ImGui::End();

ImGuiTheme::PopTweakedTheme();

}

void DemoAssets(AppState& appState)
{
ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Image From Asset"); ImGui::PopFont();
Expand Down Expand Up @@ -448,6 +550,7 @@ void StatusBarGui(AppState& app_state)
}
}


// The menu gui
void ShowMenuGui(HelloImGui::RunnerParams& runnerParams)
{
Expand Down Expand Up @@ -520,9 +623,9 @@ std::vector<HelloImGui::DockingSplit> CreateDefaultDockingSplits()
// | | |
// | Command| |
// | Space | MainDockSpace |
// | | |
// | | |
// | | |
// |------- | |
// | Command| |
// | Space2 | |
// -------------------------------------------
// | MiscSpace |
// -------------------------------------------
Expand All @@ -543,7 +646,14 @@ std::vector<HelloImGui::DockingSplit> CreateDefaultDockingSplits()
splitMainCommand.direction = ImGuiDir_Left;
splitMainCommand.ratio = 0.25f;

std::vector<HelloImGui::DockingSplit> splits {splitMainMisc, splitMainCommand};
// Then, add a space to the left which occupies a column whose width is 25% of the app width
HelloImGui::DockingSplit splitMainCommand2;
splitMainCommand2.initialDock = "CommandSpace";
splitMainCommand2.newDock = "CommandSpace2";
splitMainCommand2.direction = ImGuiDir_Down;
splitMainCommand2.ratio = 0.4f;

std::vector<HelloImGui::DockingSplit> splits {splitMainMisc, splitMainCommand, splitMainCommand2};
return splits;
}

Expand All @@ -556,10 +666,10 @@ std::vector<HelloImGui::DockingSplit> CreateAlternativeDockingSplits()
// | Space | MainDockSpace |
// | | |
// -------------------------------------------
// | |
// | |
// | CommandSpace |
// | |
// | | |
// | | Command |
// | CommandSpace | Space2 |
// | | |
// -------------------------------------------

HelloImGui::DockingSplit splitMainCommand;
Expand All @@ -568,13 +678,19 @@ std::vector<HelloImGui::DockingSplit> CreateAlternativeDockingSplits()
splitMainCommand.direction = ImGuiDir_Down;
splitMainCommand.ratio = 0.5f;

HelloImGui::DockingSplit splitMainCommand2;
splitMainCommand2.initialDock = "CommandSpace";
splitMainCommand2.newDock = "CommandSpace2";
splitMainCommand2.direction = ImGuiDir_Down;
splitMainCommand2.ratio = 0.4f;

HelloImGui::DockingSplit splitMainMisc;
splitMainMisc.initialDock = "MainDockSpace";
splitMainMisc.newDock = "MiscSpace";
splitMainMisc.direction = ImGuiDir_Left;
splitMainMisc.ratio = 0.5f;

std::vector<HelloImGui::DockingSplit> splits {splitMainCommand, splitMainMisc};
std::vector<HelloImGui::DockingSplit> splits {splitMainCommand, splitMainCommand2, splitMainMisc};
return splits;
}

Expand Down Expand Up @@ -618,12 +734,22 @@ std::vector<HelloImGui::DockableWindow> CreateDockableWindows(AppState& appState
additionalWindow.dockSpaceName = "MiscSpace"; // when shown, it will appear in BottomSpace.
additionalWindow.GuiFunction = [] { ImGui::Text("This is the additional window"); };

// alternativeThemeWindow
HelloImGui::DockableWindow alternativeThemeWindow;
// Since this window applies a theme, We need to call "ImGui::Begin" ourselves so
// that we can apply the theme before opening the window.
alternativeThemeWindow.callBeginEnd = false;
alternativeThemeWindow.label = "Alternative Theme";
alternativeThemeWindow.dockSpaceName = "CommandSpace2";
alternativeThemeWindow.GuiFunction = [&appState]() { GuiWindowAlternativeTheme(appState); };

std::vector<HelloImGui::DockableWindow> dockableWindows {
featuresDemoWindow,
layoutCustomizationWindow,
logsWindow,
dearImGuiDemoWindow,
additionalWindow,
alternativeThemeWindow
};
return dockableWindows;
}
Expand Down

0 comments on commit d2169ac

Please sign in to comment.