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 f5c6f82
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 13 deletions.
23 changes: 23 additions & 0 deletions src/hello_imgui/imgui_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,34 @@ 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);

// PushTweakedTheme() / PopTweakedTheme()
// Push and pop a tweaked theme
//
// Note: If you want the theme to apply globally to a window, you need to apply it
// *before* calling ImGui::Begin
//
// For example, within Hello ImGui, given a dockable window, you should set this option:
// myDockableWindow.callBeginEnd = false;
// And then:
// - call ImGuiTheme::PushTweakedTheme
// - call ImGui::Begin
// - display your content
// - call ImGui::End
// - call ImGuiTheme::PopTweakedTheme
//
// See demo inside src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp:
// look at `GuiWindowAlternativeTheme()`
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
21 changes: 21 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,26 @@ 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);

bool tooManyThemes = gPreviousStyles_PushTweakedTheme.size() > 10;
if (tooManyThemes)
IM_ASSERT(false && "Too many PushTweakedTheme() calls without matching PopTweakedTheme()");
}

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,113 @@ 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.
//
// In order to obtain this, we applied the following option to the window
// that displays this Gui:
// alternativeThemeWindow.callBeginEnd = false;

// Apply the theme before opening the window
ImGuiTheme::ImGuiTweakedTheme tweakedTheme;
tweakedTheme.Theme = ImGuiTheme::ImGuiTheme_WhiteIsWhite;
tweakedTheme.Tweaks.Rounding = 0.0f;
ImGuiTheme::PushTweakedTheme(tweakedTheme);

// Open the window
bool windowOpened = ImGui::Begin("Alternative Theme");
if (windowOpened)
{
// Display some widgets
ImGui::PushFont(appState.TitleFont->font); ImGui::Text("Alternative Theme"); ImGui::PopFont();
ImGui::Text("This window uses a different theme");
ImGui::SetItemTooltip(" ImGuiTheme::ImGuiTweakedTheme tweakedTheme;\n"
" tweakedTheme.Theme = ImGuiTheme::ImGuiTheme_WhiteIsWhite;\n"
" tweakedTheme.Tweaks.Rounding = 0.0f;\n"
" ImGuiTheme::PushTweakedTheme(tweakedTheme);");

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"(
Extract 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();
}
}
}
// Close the window
ImGui::End();

// Restore the theme
ImGuiTheme::PopTweakedTheme();
}

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


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

std::vector<HelloImGui::DockingSplit> splits {splitMainMisc, splitMainCommand};
// Then, add CommandSpace2 below MainDockSpace
HelloImGui::DockingSplit splitMainCommand2;
splitMainCommand2.initialDock = "MainDockSpace";
splitMainCommand2.newDock = "CommandSpace2";
splitMainCommand2.direction = ImGuiDir_Down;
splitMainCommand2.ratio = 0.5f;

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

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

HelloImGui::DockingSplit splitMainCommand;
Expand All @@ -568,13 +683,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_Right;
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 +739,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 f5c6f82

Please sign in to comment.