Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
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 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
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
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "CButton.h"
21 :
22 : #include "gui/CGUI.h"
23 : #include "gui/CGUIText.h"
24 : #include "gui/SettingTypes/CGUIColor.h"
25 : #include "lib/debug.h"
26 : #include "maths/Rect.h"
27 : #include "maths/Size2D.h"
28 :
29 : #include <limits>
30 : #include <vector>
31 :
32 0 : CButton::CButton(CGUI& pGUI)
33 : : IGUIObject(pGUI),
34 : IGUIButtonBehavior(*static_cast<IGUIObject*>(this)),
35 : IGUITextOwner(*static_cast<IGUIObject*>(this)),
36 0 : m_BufferZone(this, "buffer_zone"),
37 0 : m_Caption(this, "caption"),
38 0 : m_Font(this, "font"),
39 0 : m_Sprite(this, "sprite"),
40 0 : m_SpriteOver(this, "sprite_over"),
41 0 : m_SpritePressed(this, "sprite_pressed"),
42 0 : m_SpriteDisabled(this, "sprite_disabled"),
43 0 : m_TextColor(this, "textcolor"),
44 0 : m_TextColorOver(this, "textcolor_over"),
45 0 : m_TextColorPressed(this, "textcolor_pressed"),
46 0 : m_TextColorDisabled(this, "textcolor_disabled"),
47 0 : m_MouseEventMask(this)
48 : {
49 0 : AddText();
50 0 : }
51 :
52 0 : void CButton::SetupText()
53 : {
54 0 : ENSURE(m_GeneratedTexts.size() == 1);
55 :
56 0 : m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, GetActualSize().GetWidth(), m_BufferZone, m_TextAlign, this);
57 0 : CalculateTextPosition(GetActualSize(), m_TextPos, m_GeneratedTexts[0]);
58 0 : }
59 :
60 0 : void CButton::ResetStates()
61 : {
62 0 : IGUIObject::ResetStates();
63 0 : IGUIButtonBehavior::ResetStates();
64 0 : }
65 :
66 0 : void CButton::HandleSizeChanged()
67 : {
68 0 : IGUIObject::HandleSizeChanged();
69 0 : IGUITextOwner::HandleSizeChanged();
70 0 : }
71 :
72 0 : CSize2D CButton::GetTextSize()
73 : {
74 0 : UpdateText();
75 0 : return m_GeneratedTexts[0].GetSize();
76 : }
77 :
78 0 : CSize2D CButton::GetPreferredTextSize()
79 : {
80 0 : return CGUIText{m_pGUI, m_Caption, m_Font, std::numeric_limits<float>::max(), m_BufferZone, m_TextAlign, this}.GetSize();
81 : }
82 :
83 0 : void CButton::HandleMessage(SGUIMessage& Message)
84 : {
85 0 : IGUIObject::HandleMessage(Message);
86 0 : IGUIButtonBehavior::HandleMessage(Message);
87 0 : IGUITextOwner::HandleMessage(Message);
88 0 : }
89 :
90 0 : void CButton::Draw(CCanvas2D& canvas)
91 : {
92 0 : m_pGUI.DrawSprite(
93 : GetButtonSprite(m_Sprite, m_SpriteOver, m_SpritePressed, m_SpriteDisabled),
94 : canvas,
95 0 : GetActualSize(),
96 0 : m_VisibleArea);
97 :
98 0 : DrawText(canvas, 0, ChooseColor(), m_TextPos, m_VisibleArea);
99 0 : }
100 :
101 0 : bool CButton::IsMouseOver() const
102 : {
103 0 : if (!IGUIObject::IsMouseOver())
104 : return false;
105 0 : if (!m_MouseEventMask)
106 : return true;
107 0 : return m_MouseEventMask.IsMouseOver(m_pGUI.GetMousePos(), GetActualSize());
108 : }
109 :
110 0 : const CGUIColor& CButton::ChooseColor()
111 : {
112 0 : if (!m_Enabled)
113 0 : return *m_TextColorDisabled ? m_TextColorDisabled : m_TextColor;
114 :
115 0 : if (!m_MouseHovering)
116 0 : return m_TextColor;
117 :
118 0 : if (m_Pressed)
119 0 : return *m_TextColorPressed ? m_TextColorPressed : m_TextColor;
120 :
121 0 : return *m_TextColorOver ? m_TextColorOver : m_TextColor;
122 : }
|