diff --git a/extra/testTheme.txt b/extra/testTheme.oss similarity index 98% rename from extra/testTheme.txt rename to extra/testTheme.oss index 5bea0d7..12c4e0e 100644 --- a/extra/testTheme.txt +++ b/extra/testTheme.oss @@ -11,7 +11,7 @@ $accentColor = rgb(255, 0, 0) (label) { textColor = $accentColor - backgroundColor = #000000FF + backgroundColor = Color(#000000FF) borderColor = $accentColor fontSize = 20 borderRadius = 0 diff --git a/other/TODO.txt b/other/TODO.txt index d3bc900..ac24cdd 100644 --- a/other/TODO.txt +++ b/other/TODO.txt @@ -17,9 +17,9 @@ ***find a way to add fixed update to gui::Window ***Add triangle drawing functions to BasicRenderer2D ***Implement Panel title +***Implement show/hide functionality for widgets Add theme caching Implement global scale (probably query desktop scale and adjust accordingly) -Implement show/hide functionality for widgets Create reliable default stylesheet Implement cursors in Stylesheet diff --git a/src/ogfx/gui/WidgetManager.cpp b/src/ogfx/gui/WidgetManager.cpp index 47d9efc..5744387 100644 --- a/src/ogfx/gui/WidgetManager.cpp +++ b/src/ogfx/gui/WidgetManager.cpp @@ -46,6 +46,7 @@ namespace ogfx { if (w == nullptr) continue; if (w->isInvalid()) continue; + if (!w->isVisible()) continue; if (w->m_focused) { w->m_focused = false; @@ -105,6 +106,7 @@ namespace ogfx { if (w == nullptr) continue; if (w->isInvalid()) continue; + if (!w->isVisible()) continue; gfx.pushClippingRect({ w->getGlobalPosition(), w->getSize() }, true); w->__draw(gfx); gfx.popClippingRect(); @@ -138,6 +140,7 @@ namespace ogfx Widget* w = m_widgetList[i]; if (w == nullptr) continue; if (w->isInvalid()) continue; + if (!w->isVisible()) continue; if (!w->contains(event.mouse->position_x, event.mouse->position_y, true)) continue; w->__onMousePressed(event); @@ -160,6 +163,7 @@ namespace ogfx Widget* w = m_widgetList[i]; if (w == nullptr) continue; if (w->isInvalid()) continue; + if (!w->isVisible()) continue; if (w == m_mousePressedOnWidget) continue; event.mouse->mousePressedOnWidget = m_mousePressedOnWidget; w->__onMouseReleased(event); @@ -178,6 +182,7 @@ namespace ogfx Widget* w = m_widgetList[i]; if (w == nullptr) continue; if (w->isInvalid()) continue; + if (!w->isVisible()) continue; if (!w->contains(event.mouse->position_x, event.mouse->position_y, true)) { if (w->m_mouseInside) @@ -223,6 +228,7 @@ namespace ogfx Widget* w = m_widgetList[i]; if (w == nullptr) continue; if (w->isInvalid()) continue; + if (!w->isVisible()) continue; if (!w->contains(event.mouse->position_x, event.mouse->position_y, true)) continue; w->__onMouseScrolled(event); @@ -238,6 +244,7 @@ namespace ogfx Widget* w = m_widgetList[i]; if (w == nullptr) continue; if (w->isInvalid()) continue; + if (!w->isVisible()) continue; if (!w->contains(event.mouse->position_x, event.mouse->position_y, true)) continue; w->__onMouseDragged(event); @@ -248,13 +255,13 @@ namespace ogfx void WidgetManager::onKeyPressed(const Event& event) { - if (!m_focused) return; + if (!m_focused || !m_focused->isVisible()) return; m_focused->__onKeyPressed(event); } void WidgetManager::onKeyReleased(const Event& event) { - if (!m_focused) return; + if (!m_focused || !m_focused->isVisible()) return; m_focused->__onKeyReleased(event); if (m_tabNavigationEnabled && event.keyboard->keyCode == KeyCode::Tab) focusNext(); @@ -262,7 +269,7 @@ namespace ogfx void WidgetManager::onTextEntered(const Event& event) { - if (!m_focused) return; + if (!m_focused || !m_focused->isVisible()) return; m_focused->__onTextEntered(event); } @@ -317,6 +324,7 @@ namespace ogfx void WidgetManager::processDragAndDrop(Widget* widget, const Event& event) { if (widget == nullptr) return; + if (!widget->isVisible()) return; if (!widget->isDragAndDropEnabled()) return; if (!widget->isMouseInside()) return; if (Widget::s_dragAndDropData != nullptr) diff --git a/src/ogfx/gui/widgets/Widget.cpp b/src/ogfx/gui/widgets/Widget.cpp index 69f8b44..f99b3bc 100644 --- a/src/ogfx/gui/widgets/Widget.cpp +++ b/src/ogfx/gui/widgets/Widget.cpp @@ -128,16 +128,16 @@ namespace ogfx void Widget::setThemeQualifier(const String& qualifier, bool value) { - for (auto& [name, state] : m_qualifierList) - { - if (name == qualifier) - { + for (auto& [name, state] : m_qualifierList) + { + if (name == qualifier) + { if (state == value) return; - state = value; + state = value; reloadTheme(); - return; - } - } + return; + } + } } bool Widget::getThemeQualifier(const String& qualifier) const @@ -166,8 +166,15 @@ namespace ogfx return true; } + void Widget::setVisible(bool v) + { + m_visible = v; + } + void Widget::__draw(ogfx::BasicRenderer2D& gfx) { + if (!isVisible()) + return; if (isDrawBoxEnabled()) gfx.fillRect({ getGlobalPosition(), getSize() }, getDrawBoxColor()); else diff --git a/src/ogfx/gui/widgets/Widget.hpp b/src/ogfx/gui/widgets/Widget.hpp index 18a23a3..0b6ca31 100644 --- a/src/ogfx/gui/widgets/Widget.hpp +++ b/src/ogfx/gui/widgets/Widget.hpp @@ -57,6 +57,7 @@ namespace ogfx bool getThemeQualifier(const String& qualifier) const; bool addThemeID(const String& id); bool removeThemeID(const String& id); + void setVisible(bool v); inline const ostd::Stylesheet::QualifierList& getThemeQualifierList(void) const { return m_qualifierList; } inline virtual void onDraw(ogfx::BasicRenderer2D& gfx) { } @@ -152,6 +153,9 @@ namespace ogfx inline i32 getBorderRadius(void) const { return m_borderRadius; } inline void setBorderWidth(i32 bw) { m_borderWidth = bw; } inline i32 getBorderWidth(void) const { return m_borderWidth; } + inline bool isVisible(void) const { return m_visible; } + inline void show(void) { setVisible(true); } + inline void hide(void) { setVisible(false); } template inline T getThemeValue(const ostd::Stylesheet &theme, const String& key, const T& fallback) @@ -210,6 +214,7 @@ namespace ogfx bool m_stopEvents { true }; bool m_clipContents { true }; bool m_acceptDragAndDrop { false }; + bool m_visible { true }; MouseEventData::eButton m_pressedButton { MouseEventData::eButton::None }; stdvec m_themeIDList; diff --git a/src/test/GuiTest.cpp b/src/test/GuiTest.cpp index db38346..7efb7c4 100644 --- a/src/test/GuiTest.cpp +++ b/src/test/GuiTest.cpp @@ -89,7 +89,7 @@ class Window : public ogfx::gui::Window m_check1.setPosition(30, 30); m_check1.setText("Check this out!"); m_check1.setStateChangedCallback([&](ogfx::gui::widgets::CheckBox& sender, bool state) -> void { - std::cout << STR_BOOL(state) << "\n"; + m_panel1.setVisible(state); }); addWidget(m_check1); @@ -124,13 +124,16 @@ class Window : public ogfx::gui::Window m_label1.addThemeOverride("@:pressed.label.textColor", Colors::Crimson); - m_theme.loadFromFile("./testTheme.txt", true, getDefaultStylesheetVariableList()); + m_theme.loadFromFile("./testTheme.oss", true, getDefaultStylesheetVariableList()); m_label2.addThemeOverride("@:hover.label.showBackground", true); m_label2.addThemeOverride("@:hover.label.backgroundColor", Colors::DarkGreen); m_label3.setPosition(0, 30); m_label3.setText("Bella!"); + m_label3.setMousePressedCallback([&](const ogfx::gui::Event& event) -> void { + std::cout << "BALALLA!\n"; + }); m_label3.addThemeID("label3"); m_panel1.addChild(m_label3);