| [25140] | 1 | /* Copyright (C) 2021 Wildfire Games.
|
|---|
| [27965] | 2 | * This file is part of 0 A.D.
|
|---|
| [6830] | 3 | *
|
|---|
| [27965] | 4 | * 0 A.D. is free software: you can redistribute it and/or modify
|
|---|
| [6830] | 5 | * it under the terms of the GNU General Public License as published by
|
|---|
| 6 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 7 | * (at your option) any later version.
|
|---|
| 8 | *
|
|---|
| [27965] | 9 | * 0 A.D. is distributed in the hope that it will be useful,
|
|---|
| [6830] | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | *
|
|---|
| 14 | * You should have received a copy of the GNU General Public License
|
|---|
| [27965] | 15 | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| [6830] | 16 | */
|
|---|
| 17 |
|
|---|
| [392] | 18 | #include "precompiled.h"
|
|---|
| [13521] | 19 |
|
|---|
| [290] | 20 | #include "CButton.h"
|
|---|
| [9] | 21 |
|
|---|
| [23008] | 22 | #include "gui/CGUI.h"
|
|---|
| [22976] | 23 | #include "gui/CGUIText.h"
|
|---|
| [23028] | 24 | #include "gui/SettingTypes/CGUIColor.h"
|
|---|
| [92] | 25 |
|
|---|
| [22741] | 26 | CButton::CButton(CGUI& pGUI)
|
|---|
| [23005] | 27 | : IGUIObject(pGUI),
|
|---|
| [23020] | 28 | IGUIButtonBehavior(*static_cast<IGUIObject*>(this)),
|
|---|
| 29 | IGUITextOwner(*static_cast<IGUIObject*>(this)),
|
|---|
| [25392] | 30 | m_BufferZone(this, "buffer_zone"),
|
|---|
| 31 | m_Caption(this, "caption"),
|
|---|
| 32 | m_Font(this, "font"),
|
|---|
| 33 | m_Sprite(this, "sprite"),
|
|---|
| 34 | m_SpriteOver(this, "sprite_over"),
|
|---|
| 35 | m_SpritePressed(this, "sprite_pressed"),
|
|---|
| 36 | m_SpriteDisabled(this, "sprite_disabled"),
|
|---|
| 37 | m_TextColor(this, "textcolor"),
|
|---|
| 38 | m_TextColorOver(this, "textcolor_over"),
|
|---|
| 39 | m_TextColorPressed(this, "textcolor_pressed"),
|
|---|
| [25408] | 40 | m_TextColorDisabled(this, "textcolor_disabled"),
|
|---|
| 41 | m_MouseEventMask(this)
|
|---|
| [9] | 42 | {
|
|---|
| [22679] | 43 | AddText();
|
|---|
| [9] | 44 | }
|
|---|
| 45 |
|
|---|
| 46 | CButton::~CButton()
|
|---|
| 47 | {
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| [290] | 50 | void CButton::SetupText()
|
|---|
| 51 | {
|
|---|
| [16931] | 52 | ENSURE(m_GeneratedTexts.size() == 1);
|
|---|
| [290] | 53 |
|
|---|
| [25392] | 54 | m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_CachedActualSize.GetWidth(), m_BufferZone, m_TextAlign, this);
|
|---|
| [22679] | 55 | CalculateTextPosition(m_CachedActualSize, m_TextPos, m_GeneratedTexts[0]);
|
|---|
| [290] | 56 | }
|
|---|
| 57 |
|
|---|
| [23020] | 58 | void CButton::ResetStates()
|
|---|
| 59 | {
|
|---|
| 60 | IGUIObject::ResetStates();
|
|---|
| 61 | IGUIButtonBehavior::ResetStates();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | void CButton::UpdateCachedSize()
|
|---|
| 65 | {
|
|---|
| 66 | IGUIObject::UpdateCachedSize();
|
|---|
| 67 | IGUITextOwner::UpdateCachedSize();
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| [25225] | 70 | CSize2D CButton::GetTextSize()
|
|---|
| 71 | {
|
|---|
| 72 | UpdateText();
|
|---|
| 73 | return m_GeneratedTexts[0].GetSize();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| [16931] | 76 | void CButton::HandleMessage(SGUIMessage& Message)
|
|---|
| [9] | 77 | {
|
|---|
| [23020] | 78 | IGUIObject::HandleMessage(Message);
|
|---|
| [74] | 79 | IGUIButtonBehavior::HandleMessage(Message);
|
|---|
| [290] | 80 | IGUITextOwner::HandleMessage(Message);
|
|---|
| [9] | 81 | }
|
|---|
| 82 |
|
|---|
| [25591] | 83 | void CButton::Draw(CCanvas2D& canvas)
|
|---|
| [9] | 84 | {
|
|---|
| [23008] | 85 | m_pGUI.DrawSprite(
|
|---|
| 86 | GetButtonSprite(m_Sprite, m_SpriteOver, m_SpritePressed, m_SpriteDisabled),
|
|---|
| [25591] | 87 | canvas,
|
|---|
| [23008] | 88 | m_CachedActualSize);
|
|---|
| 89 |
|
|---|
| [25591] | 90 | DrawText(canvas, 0, ChooseColor(), m_TextPos);
|
|---|
| [77] | 91 | }
|
|---|
| [22969] | 92 |
|
|---|
| [25408] | 93 | bool CButton::IsMouseOver() const
|
|---|
| 94 | {
|
|---|
| 95 | if (!IGUIObject::IsMouseOver())
|
|---|
| 96 | return false;
|
|---|
| 97 | if (!m_MouseEventMask)
|
|---|
| 98 | return true;
|
|---|
| 99 | return m_MouseEventMask.IsMouseOver(m_pGUI.GetMousePos(), m_CachedActualSize);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| [22969] | 102 | const CGUIColor& CButton::ChooseColor()
|
|---|
| 103 | {
|
|---|
| [23005] | 104 | if (!m_Enabled)
|
|---|
| [25392] | 105 | return *m_TextColorDisabled ? m_TextColorDisabled : m_TextColor;
|
|---|
| [22969] | 106 |
|
|---|
| 107 | if (!m_MouseHovering)
|
|---|
| [23005] | 108 | return m_TextColor;
|
|---|
| [22969] | 109 |
|
|---|
| 110 | if (m_Pressed)
|
|---|
| [25392] | 111 | return *m_TextColorPressed ? m_TextColorPressed : m_TextColor;
|
|---|
| [22969] | 112 |
|
|---|
| [25392] | 113 | return *m_TextColorOver ? m_TextColorOver : m_TextColor;
|
|---|
| [22969] | 114 | }
|
|---|