From 7bb0f2ea693178749127b01ad78cbadb9e5be453 Mon Sep 17 00:00:00 2001 From: elexis Date: Sun, 22 Sep 2019 14:53:27 +0000 Subject: [PATCH] Defragment IGUIButtonBehavior Settings. Moves the AddSetting sound calls from CButton and CCheckbox to IGUIButtonBehavior, since the latter is the only class to use them. Moves ChooseColor from IGUIButtonBehavior to CButton, since that's the only class to use it and the only class registering the dependent Settings following b1422137e5 (refs d412b2010b / D2314) Initialize m_PressedRight in the constructor to prevent undefined behavior in possible future code following 0d204037b6, refs #1028. Remove unused soundPath variable following 0e26503cc6 / D2209. Differential Revision: https://code.wildfiregames.com/D2318 Tested on: clang 8.0.1, Jenkins This was SVN commit r22969. --- source/gui/CButton.cpp | 21 +++++++++++++++----- source/gui/CButton.h | 5 +++++ source/gui/CCheckBox.cpp | 5 ----- source/gui/CDropDown.h | 2 -- source/gui/IGUIButtonBehavior.cpp | 32 ++++++++++--------------------- source/gui/IGUIButtonBehavior.h | 13 ++----------- 6 files changed, 33 insertions(+), 45 deletions(-) diff --git a/source/gui/CButton.cpp b/source/gui/CButton.cpp index 56de0d1f0f..dd29984c6e 100644 --- a/source/gui/CButton.cpp +++ b/source/gui/CButton.cpp @@ -29,11 +29,6 @@ CButton::CButton(CGUI& pGUI) AddSetting("caption"); AddSetting("cell_id"); AddSetting("font"); - AddSetting("sound_disabled"); - AddSetting("sound_enter"); - AddSetting("sound_leave"); - AddSetting("sound_pressed"); - AddSetting("sound_released"); AddSetting("sprite"); AddSetting("sprite_over"); AddSetting("sprite_pressed"); @@ -97,3 +92,19 @@ void CButton::Draw() DrawText(0, ChooseColor(), m_TextPos, bz + 0.1f); } + +const CGUIColor& CButton::ChooseColor() +{ + const CGUIColor& color = GetSetting("textcolor"); + + if (!GetSetting("enabled")) + return GetSetting("textcolor_disabled") || color; + + if (!m_MouseHovering) + return color; + + if (m_Pressed) + return GetSetting("textcolor_pressed") || color; + + return GetSetting("textcolor_over") || color; +} diff --git a/source/gui/CButton.h b/source/gui/CButton.h index 9354bc84ca..80d6e8a5c4 100644 --- a/source/gui/CButton.h +++ b/source/gui/CButton.h @@ -52,6 +52,11 @@ protected: */ void SetupText(); + /** + * Picks the text color depending on current object settings. + */ + const CGUIColor& ChooseColor(); + /** * Placement of text. */ diff --git a/source/gui/CCheckBox.cpp b/source/gui/CCheckBox.cpp index 9b5a10ef4e..1970a304fa 100644 --- a/source/gui/CCheckBox.cpp +++ b/source/gui/CCheckBox.cpp @@ -24,11 +24,6 @@ CCheckBox::CCheckBox(CGUI& pGUI) { AddSetting("cell_id"); AddSetting("checked"); - AddSetting("sound_disabled"); - AddSetting("sound_enter"); - AddSetting("sound_leave"); - AddSetting("sound_pressed"); - AddSetting("sound_released"); AddSetting("sprite"); AddSetting("sprite_over"); AddSetting("sprite_pressed"); diff --git a/source/gui/CDropDown.h b/source/gui/CDropDown.h index 007e70b469..f17b2cb7d6 100644 --- a/source/gui/CDropDown.h +++ b/source/gui/CDropDown.h @@ -51,8 +51,6 @@ public: CDropDown(CGUI& pGUI); virtual ~CDropDown(); -// virtual void ResetStates() { IGUIButtonBehavior::ResetStates(); } - /** * @see IGUIObject#HandleMessage() */ diff --git a/source/gui/IGUIButtonBehavior.cpp b/source/gui/IGUIButtonBehavior.cpp index d678f8b308..2a2e94eacc 100644 --- a/source/gui/IGUIButtonBehavior.cpp +++ b/source/gui/IGUIButtonBehavior.cpp @@ -17,13 +17,21 @@ #include "precompiled.h" +#include "IGUIButtonBehavior.h" + #include "gui/CGUI.h" #include "gui/CGUISprite.h" -#include "gui/IGUIButtonBehavior.h" IGUIButtonBehavior::IGUIButtonBehavior(CGUI& pGUI) - : IGUIObject(pGUI), m_Pressed(false) + : IGUIObject(pGUI), + m_Pressed(false), + m_PressedRight(false) { + AddSetting("sound_disabled"); + AddSetting("sound_enter"); + AddSetting("sound_leave"); + AddSetting("sound_pressed"); + AddSetting("sound_released"); } IGUIButtonBehavior::~IGUIButtonBehavior() @@ -34,7 +42,6 @@ void IGUIButtonBehavior::HandleMessage(SGUIMessage& Message) { const bool enabled = GetSetting("enabled"); - CStrW soundPath; // TODO Gee: easier access functions switch (Message.type) { @@ -118,25 +125,6 @@ void IGUIButtonBehavior::HandleMessage(SGUIMessage& Message) } } -const CGUIColor& IGUIButtonBehavior::ChooseColor() -{ - // Yes, the object must possess these settings. They are standard - const CGUIColor& color = GetSetting("textcolor"); - - if (!GetSetting("enabled")) - return GetSetting("textcolor_disabled") || color; - - if (m_MouseHovering) - { - if (m_Pressed) - return GetSetting("textcolor_pressed") || color; - else - return GetSetting("textcolor_over") || color; - } - - return color; -} - void IGUIButtonBehavior::DrawButton(const CRect& rect, const float& z, CGUISpriteInstance& sprite, CGUISpriteInstance& sprite_over, CGUISpriteInstance& sprite_pressed, CGUISpriteInstance& sprite_disabled, int cell_id) { if (!GetSetting("enabled")) diff --git a/source/gui/IGUIButtonBehavior.h b/source/gui/IGUIButtonBehavior.h index d3bf269dcc..cbe9c5c149 100644 --- a/source/gui/IGUIButtonBehavior.h +++ b/source/gui/IGUIButtonBehavior.h @@ -65,15 +65,6 @@ public: */ void DrawButton(const CRect& rect, const float& z, CGUISpriteInstance& sprite, CGUISpriteInstance& sprite_over, CGUISpriteInstance& sprite_pressed, CGUISpriteInstance& sprite_disabled, int cell_id); - /** - * Choosing which color of the following according to object enabled/hovered/pressed status: - * textcolor_disabled -- disabled - * textcolor_pressed -- pressed - * textcolor_over -- hovered - */ - const CGUIColor& ChooseColor(); - - protected: /** * @see IGUIObject#ResetStates() @@ -93,8 +84,8 @@ protected: * area, as long as you release it within the button area... Anyway * this lets us know we are done with step one (clicking). */ - bool m_Pressed; - bool m_PressedRight; + bool m_Pressed; + bool m_PressedRight; }; #endif // INCLUDED_IGUIBUTTONBEHAVIOR