From 49699ad5210b6d6ff2274c04e150ac74b3312862 Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Fri, 24 May 2019 18:01:29 +0200 Subject: [PATCH] Added focusable property to Widget --- changelog.txt | 1 + include/TGUI/Widget.hpp | 29 +++++++++++++++++++++++++++-- src/TGUI/Widget.cpp | 16 +++++++++++++++- tests/Widget.cpp | 9 +++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/changelog.txt b/changelog.txt index 044de9aa..be2d768e 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,6 +4,7 @@ TGUI 0.8.6 (TBD) - Added SelectionChanged signal to TextBox - Added getSelectionStart and getSelectionEnd functions to TextBox - Added mousePressed and mouseReleased to respond to different mouse buttons in custom widgets +- Added focusable property to widgets TGUI 0.8.5 (6 April 2019) diff --git a/include/TGUI/Widget.hpp b/include/TGUI/Widget.hpp index d8660cec..eb78894f 100644 --- a/include/TGUI/Widget.hpp +++ b/include/TGUI/Widget.hpp @@ -497,8 +497,30 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// @brief Returns whether the widget can gain focus - /// @return Can the widget be focused? + /// @brief Changes whether a widget could be focused + /// + /// @param focusable Should it be possible for the widget to gain focus? + /// + /// By default all widgets are focusable. + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void setFocusable(bool focusable); + + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Returns whether a widget could be focused + /// + /// @return Should it be possible for the widget to gain focus? + /// + /// By default all widgets are focusable. + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + bool isFocusable() const; + + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Returns whether the widget can currently gain focus + /// @return Can the widget be focused right now? + /// + /// A widget has to be visible, enabled and focusable for this function to return true. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// virtual bool canGainFocus() const; @@ -822,6 +844,9 @@ namespace tgui // Is the widget focused? bool m_focused = false; + // Can the widget be focused? + bool m_focusable = true; + // Keep track of the elapsed time. sf::Time m_animationTimeElapsed; diff --git a/src/TGUI/Widget.cpp b/src/TGUI/Widget.cpp index 75514a98..d9d9c1c7 100644 --- a/src/TGUI/Widget.cpp +++ b/src/TGUI/Widget.cpp @@ -716,9 +716,23 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void Widget::setFocusable(bool focusable) + { + m_focusable = focusable; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + bool Widget::isFocusable() const + { + return m_focusable; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + bool Widget::canGainFocus() const { - return m_enabled && m_visible; + return m_enabled && m_visible && m_focusable; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tests/Widget.cpp b/tests/Widget.cpp index adbce3b4..4b09f50b 100644 --- a/tests/Widget.cpp +++ b/tests/Widget.cpp @@ -49,6 +49,15 @@ TEST_CASE("[Widget]") REQUIRE(widget->isEnabled()); } + SECTION("Focusable") + { + REQUIRE(widget->isFocusable()); + widget->setFocusable(false); + REQUIRE(!widget->isFocusable()); + widget->setFocusable(true); + REQUIRE(widget->isFocusable()); + } + SECTION("Parent") { tgui::Panel::Ptr panel1 = tgui::Panel::create();