Skip to content

Commit f2b52c3

Browse files
authored
Removed ImGui brute-force style changes for Push/Pop (#738)
1 parent 7c6b8a0 commit f2b52c3

6 files changed

Lines changed: 39 additions & 119 deletions

File tree

Sources/OvCore/src/OvCore/Helpers/GUIDrawer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <OvUI/Widgets/Layout/Columns.h>
2323
#include <OvUI/Widgets/Selection/CheckBox.h>
2424
#include <OvUI/Widgets/Buttons/Button.h>
25-
#include <OvUI/Widgets/Buttons/ButtonSmall.h>
2625
#include <OvUI/Widgets/Buttons/Toggle.h>
2726
#include <OvUI/Plugins/DDTarget.h>
2827

Sources/OvUI/include/OvUI/Widgets/Buttons/Button.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
#pragma once
88

9+
#include <optional>
910
#include <string>
1011

1112
#include <OvMaths/FVector2.h>
1213

13-
#include "OvUI/Widgets/Buttons/AButton.h"
14-
#include "OvUI/Types/Color.h"
14+
#include <OvUI/Widgets/Buttons/AButton.h>
15+
#include <OvUI/Types/Color.h>
1516

1617
namespace OvUI::Widgets::Buttons
1718
{
@@ -37,10 +38,9 @@ namespace OvUI::Widgets::Buttons
3738
OvMaths::FVector2 size;
3839
bool disabled = false;
3940

40-
Types::Color idleBackgroundColor;
41-
Types::Color hoveredBackgroundColor;
42-
Types::Color clickedBackgroundColor;
43-
44-
Types::Color textColor;
41+
std::optional<Types::Color> idleBackgroundColor;
42+
std::optional<Types::Color> hoveredBackgroundColor;
43+
std::optional<Types::Color> clickedBackgroundColor;
44+
std::optional<Types::Color> textColor;
4545
};
46-
}
46+
}

Sources/OvUI/include/OvUI/Widgets/Buttons/ButtonSmall.h

Lines changed: 0 additions & 40 deletions
This file was deleted.

Sources/OvUI/src/OvUI/Panels/PanelUndecorated.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010

1111
void OvUI::Panels::PanelUndecorated::_Draw_Impl()
1212
{
13-
auto& style = ImGui::GetStyle();
14-
ImVec2 previousPadding = style.WindowPadding;
15-
ImVec2 previousMinSize = style.WindowMinSize;
16-
style.WindowPadding = { 0, 0 };
17-
style.WindowMinSize = { 0, 0 };
13+
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0, 0 });
14+
ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, { 0, 0 });
1815

1916
if (ImGui::Begin(m_panelID.c_str(), nullptr, CollectFlags()))
2017
{
21-
style.WindowPadding = previousPadding;
22-
style.WindowMinSize = previousMinSize;
18+
ImGui::PopStyleVar(2);
2319

2420
Update();
2521

@@ -29,7 +25,7 @@ void OvUI::Panels::PanelUndecorated::_Draw_Impl()
2925
}
3026
else
3127
{
32-
style.WindowPadding = previousPadding;
28+
ImGui::PopStyleVar(2);
3329
}
3430
}
3531

Sources/OvUI/src/OvUI/Widgets/Buttons/Button.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,37 @@
1313
OvUI::Widgets::Buttons::Button::Button(const std::string& p_label, const OvMaths::FVector2& p_size, bool p_disabled) :
1414
label(p_label), size(p_size), disabled(p_disabled)
1515
{
16-
auto& style = ImGui::GetStyle();
17-
18-
idleBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_Button]);
19-
hoveredBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_ButtonHovered]);
20-
clickedBackgroundColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_ButtonActive]);
21-
textColor = Internal::Converter::ToColor(style.Colors[ImGuiCol_Text]);
2216
}
2317

2418
void OvUI::Widgets::Buttons::Button::_Draw_Impl()
2519
{
26-
auto& style = ImGui::GetStyle();
20+
using namespace OvUI::Internal;
2721

28-
auto defaultIdleColor = style.Colors[ImGuiCol_Button];
29-
auto defaultHoveredColor = style.Colors[ImGuiCol_ButtonHovered];
30-
auto defaultClickedColor = style.Colors[ImGuiCol_ButtonActive];
31-
auto defaultTextColor = style.Colors[ImGuiCol_Text];
22+
uint32_t styleOverrides = 0;
3223

33-
style.Colors[ImGuiCol_Button] = OvUI::Internal::Converter::ToImVec4(idleBackgroundColor);
34-
style.Colors[ImGuiCol_ButtonHovered] = OvUI::Internal::Converter::ToImVec4(hoveredBackgroundColor);
35-
style.Colors[ImGuiCol_ButtonActive] = OvUI::Internal::Converter::ToImVec4(clickedBackgroundColor);
36-
style.Colors[ImGuiCol_Text] = OvUI::Internal::Converter::ToImVec4(textColor);
24+
if (idleBackgroundColor.has_value())
25+
{
26+
ImGui::PushStyleColor(ImGuiCol_Button, Converter::ToImVec4(idleBackgroundColor.value()));
27+
++styleOverrides;
28+
}
29+
30+
if (hoveredBackgroundColor.has_value())
31+
{
32+
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, Converter::ToImVec4(hoveredBackgroundColor.value()));
33+
++styleOverrides;
34+
}
35+
36+
if (clickedBackgroundColor.has_value())
37+
{
38+
ImGui::PushStyleColor(ImGuiCol_ButtonActive, Converter::ToImVec4(clickedBackgroundColor.value()));
39+
++styleOverrides;
40+
}
41+
42+
if (textColor.has_value())
43+
{
44+
ImGui::PushStyleColor(ImGuiCol_Text, Converter::ToImVec4(textColor.value()));
45+
++styleOverrides;
46+
}
3747

3848
// Instead of using disabled directly, as its value can change if some
3949
// callback is bound to the ClickedEvent.
@@ -54,8 +64,6 @@ void OvUI::Widgets::Buttons::Button::_Draw_Impl()
5464
ImGui::EndDisabled();
5565
}
5666

57-
style.Colors[ImGuiCol_Button] = defaultIdleColor;
58-
style.Colors[ImGuiCol_ButtonHovered] = defaultHoveredColor;
59-
style.Colors[ImGuiCol_ButtonActive] = defaultClickedColor;
60-
style.Colors[ImGuiCol_Text] = defaultTextColor;
67+
ImGui::PopStyleColor(styleOverrides);
6168
}
69+

Sources/OvUI/src/OvUI/Widgets/Buttons/ButtonSmall.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)