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 "IGUIButtonBehavior.h"
21 :
22 : #include "gui/CGUISprite.h"
23 : #include "gui/ObjectBases/IGUIObject.h"
24 :
25 :
26 1 : const CStr IGUIButtonBehavior::EventNamePress = "Press";
27 1 : const CStr IGUIButtonBehavior::EventNamePressRight = "PressRight";
28 1 : const CStr IGUIButtonBehavior::EventNamePressRightDisabled = "PressRightDisabled";
29 1 : const CStr IGUIButtonBehavior::EventNameDoublePress = "DoublePress";
30 1 : const CStr IGUIButtonBehavior::EventNameDoublePressRight = "DoublePressRight";
31 1 : const CStr IGUIButtonBehavior::EventNameRelease = "Release";
32 1 : const CStr IGUIButtonBehavior::EventNameReleaseRight = "ReleaseRight";
33 :
34 0 : IGUIButtonBehavior::IGUIButtonBehavior(IGUIObject& pObject)
35 : : m_pObject(pObject),
36 : m_Pressed(),
37 : m_PressedRight(),
38 : m_SoundDisabled(&pObject, "sound_disabled"),
39 : m_SoundEnter(&pObject, "sound_enter"),
40 : m_SoundLeave(&pObject, "sound_leave"),
41 : m_SoundPressed(&pObject, "sound_pressed"),
42 0 : m_SoundReleased(&pObject, "sound_released")
43 : {
44 0 : }
45 :
46 0 : IGUIButtonBehavior::~IGUIButtonBehavior()
47 : {
48 0 : }
49 :
50 0 : void IGUIButtonBehavior::ResetStates()
51 : {
52 0 : if (m_Pressed)
53 : {
54 0 : m_Pressed = false;
55 0 : m_pObject.PlaySound(m_SoundReleased);
56 0 : m_pObject.SendEvent(GUIM_PRESSED_MOUSE_RELEASE, EventNameRelease);
57 : }
58 :
59 0 : if (m_PressedRight)
60 : {
61 0 : m_PressedRight = false;
62 0 : m_pObject.PlaySound(m_SoundReleased);
63 0 : m_pObject.SendEvent(GUIM_PRESSED_MOUSE_RELEASE_RIGHT, EventNameReleaseRight);
64 : }
65 0 : }
66 :
67 0 : void IGUIButtonBehavior::HandleMessage(SGUIMessage& Message)
68 : {
69 : // TODO Gee: easier access functions
70 0 : switch (Message.type)
71 : {
72 0 : case GUIM_MOUSE_ENTER:
73 0 : if (m_pObject.IsEnabled())
74 0 : m_pObject.PlaySound(m_SoundEnter);
75 0 : break;
76 :
77 0 : case GUIM_MOUSE_LEAVE:
78 0 : if (m_pObject.IsEnabled())
79 0 : m_pObject.PlaySound(m_SoundLeave);
80 0 : break;
81 :
82 0 : case GUIM_MOUSE_DBLCLICK_LEFT:
83 0 : if (!m_pObject.IsEnabled())
84 0 : break;
85 :
86 : // Since GUIM_MOUSE_PRESS_LEFT also gets called twice in a
87 : // doubleclick event, we let it handle playing sounds.
88 0 : m_pObject.SendEvent(GUIM_DOUBLE_PRESSED, EventNameDoublePress);
89 0 : break;
90 :
91 0 : case GUIM_MOUSE_PRESS_LEFT:
92 0 : if (!m_pObject.IsEnabled())
93 : {
94 0 : m_pObject.PlaySound(m_SoundDisabled);
95 0 : break;
96 : }
97 :
98 0 : m_pObject.PlaySound(m_SoundPressed);
99 0 : m_pObject.SendEvent(GUIM_PRESSED, EventNamePress);
100 0 : m_Pressed = true;
101 0 : break;
102 :
103 0 : case GUIM_MOUSE_DBLCLICK_RIGHT:
104 0 : if (!m_pObject.IsEnabled())
105 0 : break;
106 :
107 : // Since GUIM_MOUSE_PRESS_RIGHT also gets called twice in a
108 : // doubleclick event, we let it handle playing sounds.
109 0 : m_pObject.SendEvent(GUIM_DOUBLE_PRESSED_MOUSE_RIGHT, EventNameDoublePressRight);
110 0 : break;
111 :
112 0 : case GUIM_MOUSE_PRESS_RIGHT:
113 0 : if (!m_pObject.IsEnabled())
114 : {
115 0 : m_pObject.SendEvent(GUIM_PRESSED_MOUSE_RIGHT_DISABLED, EventNamePressRightDisabled);
116 0 : m_pObject.PlaySound(m_SoundDisabled);
117 0 : break;
118 : }
119 :
120 : // Button was right-clicked
121 0 : m_pObject.PlaySound(m_SoundPressed);
122 0 : m_pObject.SendEvent(GUIM_PRESSED_MOUSE_RIGHT, EventNamePressRight);
123 0 : m_PressedRight = true;
124 0 : break;
125 :
126 0 : case GUIM_MOUSE_RELEASE_RIGHT:
127 0 : if (m_pObject.IsEnabled())
128 0 : ResetStates();
129 0 : break;
130 :
131 0 : case GUIM_MOUSE_RELEASE_LEFT:
132 0 : if (m_pObject.IsEnabled())
133 0 : ResetStates();
134 0 : break;
135 :
136 0 : default:
137 0 : break;
138 : }
139 0 : }
140 :
141 0 : const CGUISpriteInstance& IGUIButtonBehavior::GetButtonSprite(const CGUISpriteInstance& sprite, const CGUISpriteInstance& sprite_over, const CGUISpriteInstance& sprite_pressed, const CGUISpriteInstance& sprite_disabled) const
142 : {
143 0 : if (!m_pObject.IsEnabled())
144 0 : return sprite_disabled ? sprite_disabled : sprite;
145 :
146 0 : if (!m_pObject.IsMouseHovering())
147 0 : return sprite;
148 :
149 0 : if (m_Pressed)
150 0 : return sprite_pressed ? sprite_pressed : sprite;
151 :
152 0 : return sprite_over ? sprite_over : sprite;
153 3 : }
|