Index: ps/trunk/source/gui/CButton.cpp =================================================================== --- ps/trunk/source/gui/CButton.cpp (revision 22968) +++ ps/trunk/source/gui/CButton.cpp (revision 22969) @@ -1,99 +1,110 @@ /* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "CButton.h" #include "gui/CGUIColor.h" #include "lib/ogl.h" CButton::CButton(CGUI& pGUI) : IGUIObject(pGUI), IGUIButtonBehavior(pGUI), IGUITextOwner(pGUI) { AddSetting("buffer_zone"); 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"); AddSetting("sprite_disabled"); AddSetting("text_align"); AddSetting("text_valign"); AddSetting("textcolor"); AddSetting("textcolor_over"); AddSetting("textcolor_pressed"); AddSetting("textcolor_disabled"); AddText(); } CButton::~CButton() { } void CButton::SetupText() { ENSURE(m_GeneratedTexts.size() == 1); m_GeneratedTexts[0] = CGUIText( m_pGUI, GetSetting("caption"), GetSetting("font"), m_CachedActualSize.GetWidth(), GetSetting("buffer_zone"), this); CalculateTextPosition(m_CachedActualSize, m_TextPos, m_GeneratedTexts[0]); } void CButton::HandleMessage(SGUIMessage& Message) { // Important IGUIButtonBehavior::HandleMessage(Message); IGUITextOwner::HandleMessage(Message); } void CButton::Draw() { const float bz = GetBufferedZ(); // Statically initialise some strings, so we don't have to do // lots of allocation every time this function is called static const CStr strSprite("sprite"); static const CStr strSpriteOver("sprite_over"); static const CStr strSpritePressed("sprite_pressed"); static const CStr strSpriteDisabled("sprite_disabled"); static const CStr strCellId("cell_id"); CGUISpriteInstance& sprite = GetSetting(strSprite); CGUISpriteInstance& sprite_over = GetSetting(strSpriteOver); CGUISpriteInstance& sprite_pressed = GetSetting(strSpritePressed); CGUISpriteInstance& sprite_disabled = GetSetting(strSpriteDisabled); const i32 cell_id = GetSetting(strCellId); DrawButton(m_CachedActualSize, bz, sprite, sprite_over, sprite_pressed, sprite_disabled, cell_id); 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; +} Index: ps/trunk/source/gui/CButton.h =================================================================== --- ps/trunk/source/gui/CButton.h (revision 22968) +++ ps/trunk/source/gui/CButton.h (revision 22969) @@ -1,61 +1,66 @@ /* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef INCLUDED_CBUTTON #define INCLUDED_CBUTTON #include "gui/IGUIButtonBehavior.h" #include "gui/IGUIObject.h" #include "gui/IGUITextOwner.h" class CButton : public IGUIButtonBehavior, public IGUITextOwner { GUI_OBJECT(CButton) public: CButton(CGUI& pGUI); virtual ~CButton(); /** * @see IGUIObject#ResetStates() */ virtual void ResetStates() { IGUIButtonBehavior::ResetStates(); } /** * @see IGUIObject#HandleMessage() */ virtual void HandleMessage(SGUIMessage& Message); /** * Draws the Button */ virtual void Draw(); protected: /** * Sets up text, should be called every time changes has been * made that can change the visual. */ void SetupText(); /** + * Picks the text color depending on current object settings. + */ + const CGUIColor& ChooseColor(); + + /** * Placement of text. */ CPos m_TextPos; }; #endif // INCLUDED_CBUTTON Index: ps/trunk/source/gui/CCheckBox.cpp =================================================================== --- ps/trunk/source/gui/CCheckBox.cpp (revision 22968) +++ ps/trunk/source/gui/CCheckBox.cpp (revision 22969) @@ -1,85 +1,80 @@ /* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "CCheckBox.h" CCheckBox::CCheckBox(CGUI& pGUI) : IGUIObject(pGUI), IGUIButtonBehavior(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"); AddSetting("sprite_disabled"); AddSetting("sprite2"); AddSetting("sprite2_over"); AddSetting("sprite2_pressed"); AddSetting("sprite2_disabled"); } CCheckBox::~CCheckBox() { } void CCheckBox::HandleMessage(SGUIMessage& Message) { // Important IGUIButtonBehavior::HandleMessage(Message); switch (Message.type) { case GUIM_PRESSED: { // Switch to opposite. SetSetting("checked", !GetSetting("checked"), true); break; } default: break; } } void CCheckBox::Draw() { if (GetSetting("checked")) DrawButton( m_CachedActualSize, GetBufferedZ(), GetSetting("sprite2"), GetSetting("sprite2_over"), GetSetting("sprite2_pressed"), GetSetting("sprite2_disabled"), GetSetting("cell_id")); else DrawButton( m_CachedActualSize, GetBufferedZ(), GetSetting("sprite"), GetSetting("sprite_over"), GetSetting("sprite_pressed"), GetSetting("sprite_disabled"), GetSetting("cell_id")); } Index: ps/trunk/source/gui/CDropDown.h =================================================================== --- ps/trunk/source/gui/CDropDown.h (revision 22968) +++ ps/trunk/source/gui/CDropDown.h (revision 22969) @@ -1,128 +1,126 @@ /* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ /* GUI Object - Drop Down (list) --Overview-- Works just like a list-box, but it hides all the elements that aren't selected. They can be brought up by pressing the control. */ #ifndef INCLUDED_CDROPDOWN #define INCLUDED_CDROPDOWN #include "gui/CGUIList.h" #include "gui/CList.h" #include "gui/IGUIScrollBar.h" #include /** * Drop Down * * The control can be pressed, but we will not inherent * this behavior from IGUIButtonBehavior, because when * you press this control, the list with elements will * immediately appear, and not first after release * (which is the whole gist of the IGUIButtonBehavior). */ class CDropDown : public CList { GUI_OBJECT(CDropDown) public: CDropDown(CGUI& pGUI); virtual ~CDropDown(); -// virtual void ResetStates() { IGUIButtonBehavior::ResetStates(); } - /** * @see IGUIObject#HandleMessage() */ virtual void HandleMessage(SGUIMessage& Message); /** * Handle events manually to catch keyboard inputting. */ virtual InReaction ManuallyHandleEvent(const SDL_Event_* ev); /** * Draws the Button */ virtual void Draw(); // This is one of the few classes we actually need to redefine this function // this is because the size of the control changes whether it is open // or closed. virtual bool IsMouseOver() const; virtual float GetBufferedZ() const; protected: /** * If the size changed, the texts have to be updated as * the word wrapping depends on the size. */ virtual void UpdateCachedSize(); /** * Sets up text, should be called every time changes has been * made that can change the visual. */ void SetupText(); // Sets up the cached GetListRect. Decided whether it should // have a scrollbar, and so on. virtual void SetupListRect(); // Specify a new List rectangle. virtual CRect GetListRect() const; /** * Placement of text. */ CPos m_TextPos; // Is the dropdown opened? bool m_Open; // I didn't cache this at first, but it's just as easy as caching // m_CachedActualSize, so I thought, what the heck it's used a lot. CRect m_CachedListRect; // Hide scrollbar when it's not needed bool m_HideScrollBar; // Not necessarily the element that is selected, this is just // which element should be highlighted. When opening the dropdown // it is set to "selected", but then when moving the mouse it will // change. int m_ElementHighlight; // Stores any text entered by the user for quick access to an element // (ie if you type "acro" it will take you to acropolis). std::string m_InputBuffer; // used to know if we want to restart anew or add to m_inputbuffer. double m_TimeOfLastInput; }; #endif // INCLUDED_CDROPDOWN Index: ps/trunk/source/gui/IGUIButtonBehavior.cpp =================================================================== --- ps/trunk/source/gui/IGUIButtonBehavior.cpp (revision 22968) +++ ps/trunk/source/gui/IGUIButtonBehavior.cpp (revision 22969) @@ -1,153 +1,141 @@ /* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #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() { } void IGUIButtonBehavior::HandleMessage(SGUIMessage& Message) { const bool enabled = GetSetting("enabled"); - CStrW soundPath; // TODO Gee: easier access functions switch (Message.type) { case GUIM_MOUSE_ENTER: if (enabled) PlaySound("sound_enter"); break; case GUIM_MOUSE_LEAVE: if (enabled) PlaySound("sound_leave"); break; case GUIM_MOUSE_DBLCLICK_LEFT: if (!enabled) 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"); break; case GUIM_MOUSE_PRESS_LEFT: if (!enabled) { PlaySound("sound_disabled"); break; } PlaySound("sound_pressed"); SendEvent(GUIM_PRESSED, "press"); m_Pressed = true; break; case GUIM_MOUSE_DBLCLICK_RIGHT: if (!enabled) 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"); break; case GUIM_MOUSE_PRESS_RIGHT: if (!enabled) { PlaySound("sound_disabled"); break; } // Button was right-clicked PlaySound("sound_pressed"); SendEvent(GUIM_PRESSED_MOUSE_RIGHT, "pressright"); m_PressedRight = true; break; case GUIM_MOUSE_RELEASE_RIGHT: if (!enabled) break; if (m_PressedRight) { m_PressedRight = false; PlaySound("sound_released"); } break; case GUIM_MOUSE_RELEASE_LEFT: if (!enabled) break; if (m_Pressed) { m_Pressed = false; PlaySound("sound_released"); } break; default: break; } } -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")) 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); } Index: ps/trunk/source/gui/IGUIButtonBehavior.h =================================================================== --- ps/trunk/source/gui/IGUIButtonBehavior.h (revision 22968) +++ ps/trunk/source/gui/IGUIButtonBehavior.h (revision 22969) @@ -1,100 +1,91 @@ /* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ /* Interface class that enhance the IGUIObject with buttony behavior (click and release to click a button), and the GUI message GUIM_PRESSED. When creating a class with extended settings and buttony behavior, just do a multiple inheritance. */ #ifndef INCLUDED_IGUIBUTTONBEHAVIOR #define INCLUDED_IGUIBUTTONBEHAVIOR #include "gui/IGUIObject.h" class CGUISpriteInstance; /** * Appends button behaviours to the IGUIObject. * Can be used with multiple inheritance alongside * IGUISettingsObject and such. */ class IGUIButtonBehavior : virtual public IGUIObject { public: IGUIButtonBehavior(CGUI& pGUI); virtual ~IGUIButtonBehavior(); /** * @see IGUIObject#HandleMessage() */ virtual void HandleMessage(SGUIMessage& Message); /** * This is a function that lets a button being drawn, * it regards if it's over, disabled, pressed and such. * 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, 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() */ virtual void ResetStates() { // Notify the gui that we aren't hovered anymore UpdateMouseOver(nullptr); m_Pressed = false; m_PressedRight = false; } /** * Everybody knows how a button works, you don't simply press it, * you have to first press the button, and then release it... * in between those two steps you can actually leave the button * 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