Index: source/gui/CButton.h =================================================================== --- source/gui/CButton.h +++ source/gui/CButton.h @@ -24,7 +24,7 @@ #include "gui/CGUISprite.h" #include "gui/CGUIString.h" -class CButton : public IGUIButtonBehavior, public IGUITextOwner +class CButton : public IGUITextOwner { GUI_OBJECT(CButton) @@ -35,7 +35,7 @@ /** * @see IGUIObject#ResetStates() */ - virtual void ResetStates() { IGUIButtonBehavior::ResetStates(); } + virtual void ResetStates() { m_ButtonBehavior.ResetStates(); } /** * @see IGUIObject#HandleMessage() @@ -59,6 +59,8 @@ */ const CGUIColor& ChooseColor(); + IGUIButtonBehavior m_ButtonBehavior; + /** * Placement of text. */ Index: source/gui/CButton.cpp =================================================================== --- source/gui/CButton.cpp +++ source/gui/CButton.cpp @@ -19,13 +19,14 @@ #include "CButton.h" +#include "gui/CGUI.h" #include "gui/CGUIColor.h" #include "gui/CGUIText.h" CButton::CButton(CGUI& pGUI) : IGUIObject(pGUI), - IGUIButtonBehavior(pGUI), IGUITextOwner(pGUI), + m_ButtonBehavior(*this), m_BufferZone(), m_CellID(), m_Caption(), @@ -74,14 +75,20 @@ void CButton::HandleMessage(SGUIMessage& Message) { // Important - IGUIButtonBehavior::HandleMessage(Message); + m_ButtonBehavior.HandleMessage(Message); IGUITextOwner::HandleMessage(Message); } void CButton::Draw() { const float bz = GetBufferedZ(); - DrawButton(m_CachedActualSize, bz, m_Sprite, m_SpriteOver, m_SpritePressed, m_SpriteDisabled, m_CellID); + + m_pGUI.DrawSprite( + m_ButtonBehavior.GetButtonSprite(m_Sprite, m_SpriteOver, m_SpritePressed, m_SpriteDisabled), + m_CellID, + bz, + m_CachedActualSize); + DrawText(0, ChooseColor(), m_TextPos, bz + 0.1f); } @@ -93,7 +100,7 @@ if (!m_MouseHovering) return m_TextColor; - if (m_Pressed) + if (m_ButtonBehavior.IsPressed()) return m_TextColorPressed || m_TextColor; return m_TextColorOver || m_TextColor; Index: source/gui/CCheckBox.h =================================================================== --- source/gui/CCheckBox.h +++ source/gui/CCheckBox.h @@ -21,7 +21,7 @@ #include "gui/CGUISprite.h" #include "gui/IGUIButtonBehavior.h" -class CCheckBox : public IGUIButtonBehavior +class CCheckBox : public IGUIObject { GUI_OBJECT(CCheckBox) @@ -32,7 +32,7 @@ /** * @see IGUIObject#ResetStates() */ - virtual void ResetStates() { IGUIButtonBehavior::ResetStates(); } + virtual void ResetStates() { m_ButtonBehavior.ResetStates(); } /** * @see IGUIObject#HandleMessage() @@ -45,6 +45,8 @@ virtual void Draw(); protected: + IGUIButtonBehavior m_ButtonBehavior; + // Settings i32 m_CellID; bool m_Checked; Index: source/gui/CCheckBox.cpp =================================================================== --- source/gui/CCheckBox.cpp +++ source/gui/CCheckBox.cpp @@ -19,9 +19,11 @@ #include "CCheckBox.h" +#include "gui/CGUI.h" + CCheckBox::CCheckBox(CGUI& pGUI) : IGUIObject(pGUI), - IGUIButtonBehavior(pGUI), + m_ButtonBehavior(*this), m_CellID(), m_Checked(), m_SpriteUnchecked(), @@ -52,7 +54,7 @@ void CCheckBox::HandleMessage(SGUIMessage& Message) { // Important - IGUIButtonBehavior::HandleMessage(Message); + m_ButtonBehavior.HandleMessage(Message); switch (Message.type) { @@ -70,21 +72,11 @@ void CCheckBox::Draw() { - if (m_Checked) - DrawButton( - m_CachedActualSize, - GetBufferedZ(), - m_SpriteChecked, - m_SpriteCheckedOver, - m_SpriteCheckedPressed, - m_SpriteCheckedDisabled, - m_CellID); - else - DrawButton(m_CachedActualSize, - GetBufferedZ(), - m_SpriteUnchecked, - m_SpriteUncheckedOver, - m_SpriteUncheckedPressed, - m_SpriteUncheckedDisabled, - m_CellID); + m_pGUI.DrawSprite( + m_Checked ? + m_ButtonBehavior.GetButtonSprite(m_SpriteChecked, m_SpriteCheckedOver, m_SpriteCheckedPressed, m_SpriteCheckedDisabled) : + m_ButtonBehavior.GetButtonSprite(m_SpriteUnchecked, m_SpriteUncheckedOver, m_SpriteUncheckedPressed, m_SpriteUncheckedDisabled), + m_CellID, + GetBufferedZ(), + m_CachedActualSize); } Index: source/gui/CRadioButton.cpp =================================================================== --- source/gui/CRadioButton.cpp +++ source/gui/CRadioButton.cpp @@ -20,14 +20,14 @@ #include "CRadioButton.h" CRadioButton::CRadioButton(CGUI& pGUI) - : CCheckBox(pGUI), IGUIObject(pGUI) + : CCheckBox(pGUI) { } void CRadioButton::HandleMessage(SGUIMessage& Message) { // Important - IGUIButtonBehavior::HandleMessage(Message); + m_ButtonBehavior.HandleMessage(Message); switch (Message.type) { Index: source/gui/IGUIButtonBehavior.h =================================================================== --- source/gui/IGUIButtonBehavior.h +++ source/gui/IGUIButtonBehavior.h @@ -35,16 +35,16 @@ * Can be used with multiple inheritance alongside * IGUISettingsObject and such. */ -class IGUIButtonBehavior : virtual public IGUIObject +class IGUIButtonBehavior { public: - IGUIButtonBehavior(CGUI& pGUI); - virtual ~IGUIButtonBehavior(); + IGUIButtonBehavior(IGUIObject& pObject); + ~IGUIButtonBehavior(); /** * @see IGUIObject#HandleMessage() */ - virtual void HandleMessage(SGUIMessage& Message); + void HandleMessage(SGUIMessage& Message); /** * This is a function that lets a button being drawn, @@ -52,31 +52,35 @@ * You input sprite names and area and it'll output * it accordingly. * - * This class is meant to be used manually in Draw() - * - * @param rect Rectangle in which the sprite should be drawn - * @param z Z-value * @param sprite Sprite drawn when not pressed, hovered or disabled * @param sprite_over Sprite drawn when m_MouseHovering is true * @param sprite_pressed Sprite drawn when m_Pressed is true * @param sprite_disabled Sprite drawn when "enabled" is false - * @param cell_id Identifies the icon to be used (if the sprite contains - * cell-using images) */ - void DrawButton(const CRect& rect, const float& z, const CGUISpriteInstance& sprite, const CGUISpriteInstance& sprite_over, const CGUISpriteInstance& sprite_pressed, const CGUISpriteInstance& sprite_disabled, int cell_id); + const CGUISpriteInstance& GetButtonSprite(const CGUISpriteInstance& sprite, const CGUISpriteInstance& sprite_over, const CGUISpriteInstance& sprite_pressed, const CGUISpriteInstance& sprite_disabled) const; -protected: /** * @see IGUIObject#ResetStates() */ - virtual void ResetStates() + void ResetStates() { - // Notify the gui that we aren't hovered anymore - UpdateMouseOver(nullptr); m_Pressed = false; m_PressedRight = false; } + bool IsPressed() + { + return m_Pressed; + } + + bool IsPressedRight() + { + return m_PressedRight; + } + +protected: + IGUIObject& m_pObject; + /** * Everybody knows how a button works, you don't simply press it, * you have to first press the button, and then release it... Index: source/gui/IGUIButtonBehavior.cpp =================================================================== --- source/gui/IGUIButtonBehavior.cpp +++ source/gui/IGUIButtonBehavior.cpp @@ -19,11 +19,10 @@ #include "IGUIButtonBehavior.h" -#include "gui/CGUI.h" #include "gui/CGUISprite.h" -IGUIButtonBehavior::IGUIButtonBehavior(CGUI& pGUI) - : IGUIObject(pGUI), +IGUIButtonBehavior::IGUIButtonBehavior(IGUIObject& pObject) + : m_pObject(pObject), m_Pressed(), m_PressedRight(), m_SoundDisabled(), @@ -32,11 +31,11 @@ m_SoundPressed(), m_SoundReleased() { - RegisterSetting("sound_disabled", m_SoundDisabled); - RegisterSetting("sound_enter", m_SoundEnter); - RegisterSetting("sound_leave", m_SoundLeave); - RegisterSetting("sound_pressed", m_SoundPressed); - RegisterSetting("sound_released", m_SoundReleased); + m_pObject.RegisterSetting("sound_disabled", m_SoundDisabled); + m_pObject.RegisterSetting("sound_enter", m_SoundEnter); + m_pObject.RegisterSetting("sound_leave", m_SoundLeave); + m_pObject.RegisterSetting("sound_pressed", m_SoundPressed); + m_pObject.RegisterSetting("sound_released", m_SoundReleased); } IGUIButtonBehavior::~IGUIButtonBehavior() @@ -49,77 +48,77 @@ switch (Message.type) { case GUIM_MOUSE_ENTER: - if (m_Enabled) - PlaySound(m_SoundEnter); + if (m_pObject.IsEnabled()) + m_pObject.PlaySound(m_SoundEnter); break; case GUIM_MOUSE_LEAVE: - if (m_Enabled) - PlaySound(m_SoundLeave); + if (m_pObject.IsEnabled()) + m_pObject.PlaySound(m_SoundLeave); break; case GUIM_MOUSE_DBLCLICK_LEFT: - if (!m_Enabled) + if (!m_pObject.IsEnabled()) break; // Since GUIM_MOUSE_PRESS_LEFT also gets called twice in a // doubleclick event, we let it handle playing sounds. - SendEvent(GUIM_DOUBLE_PRESSED, "doublepress"); + m_pObject.SendEvent(GUIM_DOUBLE_PRESSED, "doublepress"); break; case GUIM_MOUSE_PRESS_LEFT: - if (!m_Enabled) + if (!m_pObject.IsEnabled()) { - PlaySound(m_SoundDisabled); + m_pObject.PlaySound(m_SoundDisabled); break; } - PlaySound(m_SoundPressed); - SendEvent(GUIM_PRESSED, "press"); + m_pObject.PlaySound(m_SoundPressed); + m_pObject.SendEvent(GUIM_PRESSED, "press"); m_Pressed = true; break; case GUIM_MOUSE_DBLCLICK_RIGHT: - if (!m_Enabled) + if (!m_pObject.IsEnabled()) break; // Since GUIM_MOUSE_PRESS_RIGHT also gets called twice in a // doubleclick event, we let it handle playing sounds. - SendEvent(GUIM_DOUBLE_PRESSED_MOUSE_RIGHT, "doublepressright"); + m_pObject.SendEvent(GUIM_DOUBLE_PRESSED_MOUSE_RIGHT, "doublepressright"); break; case GUIM_MOUSE_PRESS_RIGHT: - if (!m_Enabled) + if (!m_pObject.IsEnabled()) { - PlaySound(m_SoundDisabled); + m_pObject.PlaySound(m_SoundDisabled); break; } // Button was right-clicked - PlaySound(m_SoundPressed); - SendEvent(GUIM_PRESSED_MOUSE_RIGHT, "pressright"); + m_pObject.PlaySound(m_SoundPressed); + m_pObject.SendEvent(GUIM_PRESSED_MOUSE_RIGHT, "pressright"); m_PressedRight = true; break; case GUIM_MOUSE_RELEASE_RIGHT: - if (!m_Enabled) + if (!m_pObject.IsEnabled()) break; if (m_PressedRight) { m_PressedRight = false; - PlaySound(m_SoundReleased); + m_pObject.PlaySound(m_SoundReleased); } break; case GUIM_MOUSE_RELEASE_LEFT: - if (!m_Enabled) + if (!m_pObject.IsEnabled()) break; if (m_Pressed) { m_Pressed = false; - PlaySound(m_SoundReleased); + m_pObject.PlaySound(m_SoundReleased); } break; @@ -128,17 +127,16 @@ } } -void IGUIButtonBehavior::DrawButton(const CRect& rect, const float& z, const CGUISpriteInstance& sprite, const CGUISpriteInstance& sprite_over, const CGUISpriteInstance& sprite_pressed, const CGUISpriteInstance& sprite_disabled, int cell_id) +const CGUISpriteInstance& IGUIButtonBehavior::GetButtonSprite(const CGUISpriteInstance& sprite, const CGUISpriteInstance& sprite_over, const CGUISpriteInstance& sprite_pressed, const CGUISpriteInstance& sprite_disabled) const { - if (!m_Enabled) - m_pGUI.DrawSprite(sprite_disabled || sprite, cell_id, z, rect); - else if (m_MouseHovering) - { - if (m_Pressed) - m_pGUI.DrawSprite(sprite_pressed || sprite, cell_id, z, rect); - else - m_pGUI.DrawSprite(sprite_over || sprite, cell_id, z, rect); - } - else - m_pGUI.DrawSprite(sprite, cell_id, z, rect); + if (!m_pObject.IsEnabled()) + return sprite_disabled || sprite; + + if (!m_pObject.IsMouseOver()) + return sprite; + + if (m_Pressed) + return sprite_pressed || sprite; + + return sprite_over || sprite; } Index: source/gui/IGUIObject.h =================================================================== --- source/gui/IGUIObject.h +++ source/gui/IGUIObject.h @@ -130,6 +130,16 @@ //@{ /** + * Registers the given setting variables with the GUI object. + * Enable XML and JS to modify the given variable. + * + * @param Type Setting type + * @param Name Setting reference name + */ + template + void RegisterSetting(const CStr& Name, T& Value); + + /** * Returns whether there is a setting with the given name registered. * * @param Setting setting name @@ -172,6 +182,11 @@ void SetSetting(const CStr& Setting, const T& Value, const bool SendMessage); /** + * Returns whether this object is set to be hidden or ghost. + */ + bool IsEnabled() const; + + /** * Returns whether this is object is set to be hidden. */ bool IsHidden() const; @@ -213,6 +228,11 @@ */ JSObject* GetJSObject(); + /** + * Retrieves the configured sound filename from the given setting name and plays that once. + */ + void PlaySound(const CStrW& soundPath) const; + //@} protected: //-------------------------------------------------------- @@ -227,17 +247,8 @@ //-------------------------------------------------------- //@{ - /** - * Registers the given setting variables with the GUI object. - * Enable XML and JS to modify the given variable. - * - * @param Type Setting type - * @param Name Setting reference name - */ - template - void RegisterSetting(const CStr& Name, T& Value); - public: + /** * This function is called with different messages * for instance when the mouse enters the object. @@ -323,6 +334,15 @@ */ virtual void* GetTextOwner() { return nullptr; } + /** + * Send event to this GUI object (HandleMessage and ScriptEvent) + * + * @param type Type of GUI message to be handled + * @param EventName String representation of event name + * @return IN_HANDLED if event was handled, or IN_PASS if skipped + */ + InReaction SendEvent(EGUIMessageType type, const CStr& EventName); + protected: /** * Check if object is focused. @@ -367,15 +387,6 @@ CRect m_CachedActualSize; /** - * Send event to this GUI object (HandleMessage and ScriptEvent) - * - * @param type Type of GUI message to be handled - * @param EventName String representation of event name - * @return IN_HANDLED if event was handled, or IN_PASS if skipped - */ - InReaction SendEvent(EGUIMessageType type, const CStr& EventName); - - /** * Execute the script for a particular action. * Does nothing if no script has been registered for that action. * The mouse coordinates will be passed as the first argument. @@ -404,11 +415,6 @@ */ void UpdateMouseOver(IGUIObject* const& pMouseOver); - /** - * Retrieves the configured sound filename from the given setting name and plays that once. - */ - void PlaySound(const CStrW& soundPath) const; - //@} private: //-------------------------------------------------------- Index: source/gui/IGUIObject.cpp =================================================================== --- source/gui/IGUIObject.cpp +++ source/gui/IGUIObject.cpp @@ -467,6 +467,11 @@ return m_JSObject.get(); } +bool IGUIObject::IsEnabled() const +{ + return m_Enabled; +} + bool IGUIObject::IsHidden() const { return m_Hidden;