Line data Source code
1 : /* Copyright (C) 2021 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 :
26 0 : CButton::CButton(CGUI& pGUI)
27 : : IGUIObject(pGUI),
28 : IGUIButtonBehavior(*static_cast<IGUIObject*>(this)),
29 : IGUITextOwner(*static_cast<IGUIObject*>(this)),
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"),
40 : m_TextColorDisabled(this, "textcolor_disabled"),
41 0 : m_MouseEventMask(this)
42 : {
43 0 : AddText();
44 0 : }
45 :
46 0 : CButton::~CButton()
47 : {
48 0 : }
49 :
50 0 : void CButton::SetupText()
51 : {
52 0 : ENSURE(m_GeneratedTexts.size() == 1);
53 :
54 0 : m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_CachedActualSize.GetWidth(), m_BufferZone, m_TextAlign, this);
55 0 : CalculateTextPosition(m_CachedActualSize, m_TextPos, m_GeneratedTexts[0]);
56 0 : }
57 :
58 0 : void CButton::ResetStates()
59 : {
60 0 : IGUIObject::ResetStates();
61 0 : IGUIButtonBehavior::ResetStates();
62 0 : }
63 :
64 0 : void CButton::UpdateCachedSize()
65 : {
66 0 : IGUIObject::UpdateCachedSize();
67 0 : IGUITextOwner::UpdateCachedSize();
68 0 : }
69 :
70 0 : CSize2D CButton::GetTextSize()
71 : {
72 0 : UpdateText();
73 0 : return m_GeneratedTexts[0].GetSize();
74 : }
75 :
76 0 : void CButton::HandleMessage(SGUIMessage& Message)
77 : {
78 0 : IGUIObject::HandleMessage(Message);
79 0 : IGUIButtonBehavior::HandleMessage(Message);
80 0 : IGUITextOwner::HandleMessage(Message);
81 0 : }
82 :
83 0 : void CButton::Draw(CCanvas2D& canvas)
84 : {
85 0 : m_pGUI.DrawSprite(
86 : GetButtonSprite(m_Sprite, m_SpriteOver, m_SpritePressed, m_SpriteDisabled),
87 : canvas,
88 : m_CachedActualSize);
89 :
90 0 : DrawText(canvas, 0, ChooseColor(), m_TextPos);
91 0 : }
92 :
93 0 : bool CButton::IsMouseOver() const
94 : {
95 0 : if (!IGUIObject::IsMouseOver())
96 0 : return false;
97 0 : if (!m_MouseEventMask)
98 0 : return true;
99 0 : return m_MouseEventMask.IsMouseOver(m_pGUI.GetMousePos(), m_CachedActualSize);
100 : }
101 :
102 0 : const CGUIColor& CButton::ChooseColor()
103 : {
104 0 : if (!m_Enabled)
105 0 : return *m_TextColorDisabled ? m_TextColorDisabled : m_TextColor;
106 :
107 0 : if (!m_MouseHovering)
108 0 : return m_TextColor;
109 :
110 0 : if (m_Pressed)
111 0 : return *m_TextColorPressed ? m_TextColorPressed : m_TextColor;
112 :
113 0 : return *m_TextColorOver ? m_TextColorOver : m_TextColor;
114 : }
|