libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
GuiButton.cpp
1/****************************************************************************
2 * libgui
3 * Daryl Borth 2009-2026
4 * GuiButton.cpp
5 ***************************************************************************/
6
7#include "Gui.h"
8
10{
11 width = w;
12 height = h;
13 image = nullptr;
14 imageOver = nullptr;
15 imageHold = nullptr;
16 imageClick = nullptr;
17 icon = nullptr;
18 iconOver = nullptr;
19 iconHold = nullptr;
20 iconClick = nullptr;
21
22 for(int i=0; i < MAX_BTN_LABELS; i++)
23 {
24 label[i] = nullptr;
25 labelOver[i] = nullptr;
26 labelHold[i] = nullptr;
27 labelClick[i] = nullptr;
28 }
29
30 soundOver = nullptr;
31 soundHold = nullptr;
32 soundClick = nullptr;
33 selectable = true;
34 holdable = false;
35 clickable = true;
36}
37
41
43{
44 image = img;
45 if(img) img->setParent(this);
46}
48{
49 imageOver = img;
50 if(img) img->setParent(this);
51}
53{
54 imageHold = img;
55 if(img) img->setParent(this);
56}
58{
59 imageClick = img;
60 if(img) img->setParent(this);
61}
63{
64 icon = img;
65 if(img) img->setParent(this);
66}
68{
69 iconOver = img;
70 if(img) img->setParent(this);
71}
73{
74 iconHold = img;
75 if(img) img->setParent(this);
76}
78{
79 iconClick = img;
80 if(img) img->setParent(this);
81}
83{
84 if(n >= MAX_BTN_LABELS) return;
85 label[n] = txt;
86 if(txt) txt->setParent(this);
87}
89{
90 if(n >= MAX_BTN_LABELS) return;
91 labelOver[n] = txt;
92 if(txt) txt->setParent(this);
93}
95{
96 if(n >= MAX_BTN_LABELS) return;
97 labelHold[n] = txt;
98 if(txt) txt->setParent(this);
99}
101{
102 if(n >= MAX_BTN_LABELS) return;
103 labelClick[n] = txt;
104 if(txt) txt->setParent(this);
105}
107{
109
111 for(GuiElement * e : visuals)
112 if(e) e->setState(s, c);
113}
114
116{
118
120 for(GuiElement * e : visuals)
121 if(e) e->setState(state, stateChan);
122}
123
125{
126 soundOver = snd;
127}
129{
130 soundHold = snd;
131}
133{
134 soundClick = snd;
135}
136
141{
142 if(!this->isVisible())
143 return;
144
145 if(state == STATE::SELECTED || state == STATE::HELD)
146 {
147 if(imageOver)
148 imageOver->draw();
149 else if(image) // draw image
150 image->draw();
151
152 if(iconOver)
153 iconOver->draw();
154 else if(icon) // draw icon
155 icon->draw();
156
157 for(int i=0; i < MAX_BTN_LABELS; i++) {
158 if(labelOver[i])
159 labelOver[i]->draw();
160 else if(label[i])
161 label[i]->draw();
162 }
163 }
164 else
165 {
166 if(image) // draw image
167 image->draw();
168 if(icon) // draw icon
169 icon->draw();
170
171 for(int i=0; i < MAX_BTN_LABELS; i++) {
172 if(label[i])
173 label[i]->draw();
174 }
175 }
176
177 this->updateEffects();
178}
179
181{
182 for(int i=0; i<MAX_BTN_LABELS; i++)
183 {
184 if(label[i])
185 label[i]->resetText();
186 if(labelOver[i])
187 labelOver[i]->resetText();
188 }
189}
190
192{
193 if(state == STATE::CLICKED || state == STATE::DISABLED || !controller)
194 return;
195 else if(parentElement && parentElement->getState() == STATE::DISABLED)
196 return;
197
198 auto pad = controller->getPadData();
199 int currentChan = controller->getChannel();
200
201 // cursor
202 if(pad.validPointer && currentChan >= 0)
203 {
204 if(this->isInside(pad.cursor_x, pad.cursor_y))
205 {
206 if(state == STATE::DEFAULT) // we weren't on the button before!
207 {
208 this->setState(STATE::SELECTED, currentChan);
209
210 if(this->isRumble())
211 platform->getInput()->setRumble(currentChan, true);
212
213 if(soundOver)
214 soundOver->play();
215
216 if(effectsOver && !effects)
217 {
218 // initiate effects
222 }
223 }
224 }
225 else
226 {
227 if(state == STATE::SELECTED && (stateChan == currentChan || stateChan == -1))
228 this->resetState();
229
231 {
232 // initiate effects (in reverse)
235 effectTarget = 100;
236 }
237 }
238 }
239
240 // button triggers
241 if(this->isClickable())
242 {
243 for(int i=0; i<MAX_TRIGGERS; i++)
244 {
245 if(trigger[i] && trigger[i]->isClicked(controller))
246 {
247 if(currentChan == stateChan || stateChan == -1)
248 {
249 if(state == STATE::SELECTED)
250 {
251 if(!pad.validPointer || this->isInside(pad.cursor_x, pad.cursor_y))
252 {
253 this->setState(STATE::CLICKED, currentChan);
254 if(soundClick)
255 soundClick->play();
256 }
257 }
258 else if(trigger[i]->getType() == TRIGGER_TYPE::BUTTON_ONLY)
259 {
260 this->setState(STATE::CLICKED, currentChan);
261 }
262 else if(trigger[i]->getType() == TRIGGER_TYPE::BUTTON_ONLY_IN_FOCUS &&
263 parentElement->isFocused())
264 {
265 this->setState(STATE::CLICKED, currentChan);
266 }
267 }
268 }
269 }
270 }
271
272 if(this->isHoldable())
273 {
274 bool held = false;
275
276 for(int i=0; i<MAX_TRIGGERS; i++)
277 {
278 if(trigger[i])
279 {
280 // Evaluate transition to CLICKED via held trigger types
281 if(trigger[i]->isClicked(controller))
282 {
283 if(trigger[i]->getType() == TRIGGER_TYPE::HELD && state == STATE::SELECTED &&
284 (currentChan == stateChan || stateChan == -1))
285 {
286 this->setState(STATE::CLICKED, currentChan);
287 }
288 }
289
290 // Evaluate sustained hold
291 if(trigger[i]->isHeld(controller))
292 {
293 if(trigger[i]->getType() == TRIGGER_TYPE::HELD)
294 held = true;
295 }
296 }
297 }
298
299 if(!held && state == STATE::HELD && stateChan == currentChan)
300 {
301 this->resetState();
302 }
303 else if(held && state == STATE::CLICKED && stateChan == currentChan)
304 {
305 this->setState(STATE::HELD, currentChan);
306 }
307 }
308
309 if(updateCB)
310 updateCB(this);
311}
Umbrella header for the platform-agnostic libgui core.
GuiElement * iconClick
Button icon for STATE::CLICKED.
Definition GuiButton.h:90
void setLabel(GuiText *t, int n=0)
Sets the button's label.
Definition GuiButton.cpp:82
void resetState() override
Resets the state and mirrors it into the button's images and icons.
GuiText * labelOver[MAX_BTN_LABELS]
Label(s) to display for STATE::SELECTED.
Definition GuiButton.h:92
GuiElement * image
Button image (default): a GuiImage, GuiShape or any element.
Definition GuiButton.h:83
void update(InputController *c)
Constantly called to allow the GuiButton to respond to updated input data.
void setIconClick(GuiElement *i)
Sets the button's icon on click.
Definition GuiButton.cpp:77
GuiElement * imageOver
Button image for STATE::SELECTED.
Definition GuiButton.h:84
GuiSound * soundOver
Sound to play for STATE::SELECTED.
Definition GuiButton.h:95
GuiSound * soundHold
Sound to play for STATE::HELD.
Definition GuiButton.h:96
GuiElement * imageClick
Button image for STATE::CLICKED.
Definition GuiButton.h:86
GuiButton(int w=0, int h=0)
Constructor.
Definition GuiButton.cpp:9
void setSoundHold(GuiSound *s)
Sets the sound to play on hold.
GuiText * labelClick[MAX_BTN_LABELS]
Label(s) to display for STATE::CLICKED.
Definition GuiButton.h:94
void setImage(GuiElement *i)
Sets the button's image.
Definition GuiButton.cpp:42
void draw() override
Constantly called to draw the GuiButton.
void setImageHold(GuiElement *i)
Sets the button's image on hold.
Definition GuiButton.cpp:52
void setSoundClick(GuiSound *s)
Sets the sound to play on click.
void setIcon(GuiElement *i)
Sets the button's icon.
Definition GuiButton.cpp:62
void setSoundOver(GuiSound *s)
Sets the sound to play on over.
void setLabelClick(GuiText *t, int n=0)
Sets the button's label on click.
GuiElement * imageHold
Button image for STATE::HELD.
Definition GuiButton.h:85
~GuiButton()
Destructor.
Definition GuiButton.cpp:38
GuiElement * icon
Button icon (drawn after button image)
Definition GuiButton.h:87
void setImageClick(GuiElement *i)
Sets the button's image on click.
Definition GuiButton.cpp:57
void setLabelOver(GuiText *t, int n=0)
Sets the button's label on over (eg: different colored text)
Definition GuiButton.cpp:88
GuiSound * soundClick
Sound to play for STATE::CLICKED.
Definition GuiButton.h:97
GuiElement * iconOver
Button icon for STATE::SELECTED.
Definition GuiButton.h:88
GuiText * labelHold[MAX_BTN_LABELS]
Label(s) to display for STATE::HELD.
Definition GuiButton.h:93
void resetText()
Resets the text for all contained elements.
void setLabelHold(GuiText *t, int n=0)
Sets the button's label on hold.
Definition GuiButton.cpp:94
GuiText * label[MAX_BTN_LABELS]
Label(s) to display (default)
Definition GuiButton.h:91
void setState(STATE s, int c=-1) override
Sets the state and mirrors it into the button's images and icons, so a state-aware element such as Gu...
GuiElement * iconHold
Button icon for STATE::HELD.
Definition GuiButton.h:89
void setIconOver(GuiElement *i)
Sets the button's icon on over.
Definition GuiButton.cpp:67
void setIconHold(GuiElement *i)
Sets the button's icon on hold.
Definition GuiButton.cpp:72
void setImageOver(GuiElement *i)
Sets the button's image on over.
Definition GuiButton.cpp:47
bool isVisible()
Checks whether or not the element is visible.
int effectTarget
Effect target amount.
Definition GuiElement.h:285
int effectAmount
Effect amount.
Definition GuiElement.h:284
bool selectable
Whether or not this element selectable (can change to SELECTED state)
Definition GuiElement.h:293
int stateChan
Which controller channel is responsible for the last change in state.
Definition GuiElement.h:292
virtual void resetState()
Resets the element's state to STATE::DEFAULT.
int effectAmountOver
EffectAmount to set when wiimote cursor is over this element.
Definition GuiElement.h:287
int width
Element width.
Definition GuiElement.h:268
virtual void setState(STATE s, int c=-1)
Sets the element's state.
int effectsOver
Effects to enable when wiimote cursor is over this element.
Definition GuiElement.h:286
UpdateCallback updateCB
Callback function to call when this element is updated.
Definition GuiElement.h:264
int effects
Currently enabled effect(s).
Definition GuiElement.h:283
int height
Element height.
Definition GuiElement.h:269
bool holdable
Whether or not this element is holdable (can change to HELD state)
Definition GuiElement.h:295
bool clickable
Whether or not this element is clickable (can change to CLICKED state)
Definition GuiElement.h:294
bool isClickable()
Checks whether or not the element is clickable.
GuiElement * parentElement
Parent element.
Definition GuiElement.h:265
bool isRumble()
Checks whether rumble was requested by the element.
void updateEffects()
Updates the element's effects (dynamic values) Called by Draw(), used for animation purposes.
GuiTrigger * trigger[MAX_TRIGGERS]
GuiTriggers (input actions) that this element responds to.
Definition GuiElement.h:263
GuiElement()
Constructor.
Definition GuiElement.cpp:9
void setParent(GuiElement *e)
Set the element's parent.
int effectTargetOver
EffectTarget to set when wiimote cursor is over this element.
Definition GuiElement.h:288
STATE state
Element state (DEFAULT, SELECTED, CLICKED, DISABLED)
Definition GuiElement.h:291
bool isInside(int x, int y)
Checks whether the specified coordinates are within the element's boundaries.
bool isHoldable()
Checks whether or not the element is holdable.
Sound conversion and playback.
Definition GuiSound.h:27
Display, manage, and manipulate text in the GUI.
Definition GuiText.h:10
Logical controller for one input channel.
STATE
Interaction state of a GuiElement (default, selected, clicked, held, disabled).
Definition GuiElement.h:36