libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
GuiElement.cpp
1/****************************************************************************
2 * libgui
3 * Daryl Borth 2009-2026
4 * GuiElement.cpp
5 ***************************************************************************/
6
7#include "Gui.h"
8
10{
11 xoffset = 0;
12 yoffset = 0;
13 xmin = 0;
14 xmax = 0;
15 ymin = 0;
16 ymax = 0;
17 width = 0;
18 height = 0;
19 alpha = 255;
20 xscale = 1;
21 yscale = 1;
22 state = STATE::DEFAULT;
23 stateChan = -1;
24 for (int i = 0; i < MAX_TRIGGERS; i++)
25 trigger[i] = nullptr;
26 parentElement = nullptr;
27 removeOnDestroy = false;
28 rumble = true;
29 selectable = false;
30 clickable = false;
31 holdable = false;
32 visible = true;
33 focus = -1; // cannot be focused
34 updateCB = nullptr;
35 yoffsetDyn = 0;
36 xoffsetDyn = 0;
37 alphaDyn = -1;
38 scaleDyn = 1;
39 effects = 0;
40 effectAmount = 0;
41 effectTarget = 0;
42 effectsOver = 0;
45
46 // default alignment - align to top left
47 alignmentVert = ALIGN_V::TOP;
48 alignmentHor = ALIGN_H::LEFT;
49}
50
52{
54 parentElement->remove(this);
55 }
56}
57
62
67
69{
70 int x = 0;
71 int pWidth = 0;
72 int pLeft = 0;
73
75 {
76 pWidth = parentElement->getWidth();
77 pLeft = parentElement->getLeft();
78 }
79
80 if(effects & (EFFECT::SLIDE_IN | EFFECT::SLIDE_OUT))
81 pLeft += xoffsetDyn;
82
83 switch(alignmentHor)
84 {
85 case ALIGN_H::LEFT:
86 x = pLeft;
87 break;
88 case ALIGN_H::CENTRE:
89 x = pLeft + pWidth/2.0 - (width*xscale)/2.0;
90 break;
91 case ALIGN_H::RIGHT:
92 x = pLeft + pWidth - width*xscale;
93 break;
94 }
95 x += (width*(xscale - 1))/2.0; // correct offset for scaled images
96 return x + xoffset;
97}
98
100{
101 int y = 0;
102 int pHeight = 0;
103 int pTop = 0;
104
105 if(parentElement)
106 {
107 pHeight = parentElement->getHeight();
108 pTop = parentElement->getTop();
109 }
110
111 if(effects & (EFFECT::SLIDE_IN | EFFECT::SLIDE_OUT))
112 pTop += yoffsetDyn;
113
114 switch(alignmentVert)
115 {
116 case ALIGN_V::TOP:
117 y = pTop;
118 break;
119 case ALIGN_V::MIDDLE:
120 y = pTop + pHeight/2.0 - (height*yscale)/2.0;
121 break;
122 case ALIGN_V::BOTTOM:
123 y = pTop + pHeight - height*yscale;
124 break;
125 }
126 y += (height*(yscale - 1))/2.0; // correct offset for scaled images
127 return y + yoffset;
128}
129
131{
132 xmin = x;
133}
134
136{
137 return xmin;
138}
139
141{
142 xmax = x;
143}
144
146{
147 return xmax;
148}
149
151{
152 ymin = y;
153}
154
156{
157 return ymin;
158}
159
161{
162 ymax = y;
163}
164
166{
167 return ymax;
168}
169
171{
172 return width;
173}
174
176{
177 return height;
178}
179
180void GuiElement::setSize(int w, int h)
181{
182
183 width = w;
184 height = h;
185}
186
188{
189 return visible;
190}
191
193{
194 visible = v;
195}
196
198{
199 alpha = a;
200}
201
203{
204 int a = alpha;
205
206 if(alphaDyn >= 0)
207 a = alphaDyn;
208
209 if(parentElement)
210 a *= float(parentElement->getAlpha())/255.0f;
211
212 return a;
213}
214
216{
217 xscale = s;
218 yscale = s;
219}
220
222{
223 xscale = s;
224}
225
227{
228 yscale = s;
229}
230
231void GuiElement::setScale(int mw, int mh)
232{
233 xscale = 1.0f;
234 if(width > mw || height > mh)
235 {
236 if(width/(height*1.0) > mw/(mh*1.0))
237 xscale = mw/(width*1.0);
238 else
239 xscale = mh/(height*1.0);
240 }
241 yscale = xscale;
242}
243
245{
246 float s = xscale * scaleDyn;
247
248 if(parentElement)
249 s *= parentElement->getScale();
250
251 return s;
252}
253
255{
256 float s = xscale * scaleDyn;
257
258 if(parentElement)
259 s *= parentElement->getScale();
260
261 return s;
262}
263
265{
266 float s = yscale * scaleDyn;
267
268 if(parentElement)
269 s *= parentElement->getScaleY();
270
271 return s;
272}
273
275{
276 return state;
277}
278
280{
281 return stateChan;
282}
283
285{
286 state = s;
287 stateChan = c;
288}
289
291{
292 if(state != STATE::DISABLED)
293 {
294 state = STATE::DEFAULT;
295 stateChan = -1;
296 }
297}
298
300{
301 clickable = c;
302}
303
305{
306 selectable = s;
307}
308
310{
311 holdable = d;
312}
313
315{
316 if(state == STATE::DISABLED || state == STATE::CLICKED)
317 return false;
318 else
319 return selectable;
320}
321
323{
324 if(state == STATE::DISABLED ||
325 state == STATE::CLICKED ||
326 state == STATE::HELD)
327 return false;
328 else
329 return clickable;
330}
331
333{
334 if(state == STATE::DISABLED)
335 return false;
336 else
337 return holdable;
338}
339
341{
342 focus = f;
343}
344
346{
347 return focus;
348}
349
351{
352 bool set = false;
353 for (int i = 0; i < MAX_TRIGGERS; i++) {
354 if(!trigger[i]) {
355 trigger[i] = t;
356 set = true;
357 break;
358 }
359 }
360
361 // all were assigned, so we'll just overwrite the first one
362 if(!set)
363 trigger[0] = t;
364}
365
367{
368 if (i < MAX_TRIGGERS)
369 trigger[i] = t;
370}
371
373{
374 return rumble;
375}
376
378{
379 rumble = r;
380}
381
383{
384 return effects;
385}
386
387void GuiElement::setEffect(int eff, int amount, int target)
388{
389 if(eff & EFFECT::SLIDE_IN)
390 {
391 // these calculations overcompensate a little
392 if(eff & EFFECT::SLIDE_TOP)
393 yoffsetDyn = -platform->getVideo()->getScreenHeight();
394 else if(eff & EFFECT::SLIDE_LEFT)
395 xoffsetDyn = -platform->getVideo()->getScreenWidth();
396 else if(eff & EFFECT::SLIDE_BOTTOM)
397 yoffsetDyn = platform->getVideo()->getScreenHeight();
398 else if(eff & EFFECT::SLIDE_RIGHT)
399 xoffsetDyn = platform->getVideo()->getScreenWidth();
400 }
401 if(eff & EFFECT::FADE)
402 {
403 if(amount > 0)
404 alphaDyn = 0;
405 else if(amount < 0)
406 alphaDyn = alpha;
407 }
408
409 effects |= eff;
410 effectAmount = amount;
411 effectTarget = target;
412}
413
414void GuiElement::setEffectOnOver(int eff, int amount, int target)
415{
416 effectsOver |= eff;
417 effectAmountOver = amount;
418 effectTargetOver = target;
419}
420
422{
423 setEffectOnOver(EFFECT::SCALE, 4, 110);
424}
425
427{
428 if(effects & (EFFECT::SLIDE_IN | EFFECT::SLIDE_OUT))
429 {
430 if(effects & EFFECT::SLIDE_IN)
431 {
432 if(effects & EFFECT::SLIDE_LEFT)
433 {
435
436 if(xoffsetDyn >= 0)
437 {
438 xoffsetDyn = 0;
439 effects = 0;
440 }
441 }
442 else if(effects & EFFECT::SLIDE_RIGHT)
443 {
445
446 if(xoffsetDyn <= 0)
447 {
448 xoffsetDyn = 0;
449 effects = 0;
450 }
451 }
452 else if(effects & EFFECT::SLIDE_TOP)
453 {
455
456 if(yoffsetDyn >= 0)
457 {
458 yoffsetDyn = 0;
459 effects = 0;
460 }
461 }
462 else if(effects & EFFECT::SLIDE_BOTTOM)
463 {
465
466 if(yoffsetDyn <= 0)
467 {
468 yoffsetDyn = 0;
469 effects = 0;
470 }
471 }
472 }
473 else
474 {
475 if(effects & EFFECT::SLIDE_LEFT)
476 {
478
479 if(xoffsetDyn <= -platform->getVideo()->getScreenWidth())
480 effects = 0; // shut off effect
481 }
482 else if(effects & EFFECT::SLIDE_RIGHT)
483 {
485
486 if(xoffsetDyn >= platform->getVideo()->getScreenWidth())
487 effects = 0; // shut off effect
488 }
489 else if(effects & EFFECT::SLIDE_TOP)
490 {
492
493 if(yoffsetDyn <= -platform->getVideo()->getScreenHeight())
494 effects = 0; // shut off effect
495 }
496 else if(effects & EFFECT::SLIDE_BOTTOM)
497 {
499
500 if(yoffsetDyn >= platform->getVideo()->getScreenHeight())
501 effects = 0; // shut off effect
502 }
503 }
504 }
505 if(effects & EFFECT::FADE)
506 {
508
509 if(effectAmount < 0 && alphaDyn <= 0)
510 {
511 alphaDyn = 0;
512 effects = 0; // shut off effect
513 }
514 else if(effectAmount > 0 && alphaDyn >= alpha)
515 {
516 alphaDyn = alpha;
517 effects = 0; // shut off effect
518 }
519 }
520 if(effects & EFFECT::SCALE)
521 {
522 scaleDyn += float(effectAmount)*0.01f;
523 float effTar100 = float(effectTarget)*0.01f;
524
525 if((effectAmount < 0 && scaleDyn <= effTar100)
526 || (effectAmount > 0 && scaleDyn >= effTar100))
527 {
528 scaleDyn = effTar100;
529 effects = 0; // shut off effect
530 }
531 }
532}
533
535{
536 if(updateCB)
537 updateCB(this);
538}
539
540void GuiElement::setUpdateCallback(UpdateCallback u)
541{
542 updateCB = u;
543}
544
545void GuiElement::setPosition(int xoff, int yoff)
546{
547 xoffset = xoff;
548 yoffset = yoff;
549}
550
552{
553 alignmentHor = hor;
554 alignmentVert = vert;
555}
556
558{
559 return -1;
560}
561
563{
564}
565
566bool GuiElement::isInside(int x, int y)
567{
568 if(unsigned(x - this->getLeft()) < unsigned(width)
569 && unsigned(y - this->getTop()) < unsigned(height))
570 return true;
571 return false;
572}
573
574void GuiElement::getNaturalSize(int& w, int& h)
575{
576 w = width;
577 h = height;
578}
579
581{
582 this->setAlignment(ALIGN_H::LEFT, ALIGN_V::TOP);
583 this->setPosition(x, y);
584}
585
587{
588 width = w;
589 height = h;
590}
591
593{
594}
595
596void GuiElement::collectNavigable(std::vector<GuiElement*>& out)
597{
598 out.push_back(this);
599}
Umbrella header for the platform-agnostic libgui core.
void setEffect(int e, int a, int t=0)
Set an effect for the element.
bool isVisible()
Checks whether or not the element is visible.
int effectTarget
Effect target amount.
Definition GuiElement.h:285
void setMaxX(int x)
Sets the maximum x offset of the element.
void setEffectOnOver(int e, int a, int t=0)
Sets an effect to be enabled on wiimote cursor over.
float xscale
Element X scale (1 = 100%)
Definition GuiElement.h:280
int ymax
Element's max Y offset allowed.
Definition GuiElement.h:273
int alphaDyn
Element alpha, dynamic (multiplied by alpha value for blending/fading effects)
Definition GuiElement.h:279
int getTop()
Gets the current topmost coordinate of the element Considers vertical alignment, y offset,...
int xmax
Element's max X offset allowed.
Definition GuiElement.h:275
virtual int getSelected()
Gets whether or not the element is in STATE::SELECTED.
int xmin
Element's min X offset allowed.
Definition GuiElement.h:274
float getScaleX()
Gets the element's current x scale Considers scale, scaleDyn, and the parent element's GetScale() val...
int isFocused()
Checks whether the element is in focus.
int effectAmount
Effect amount.
Definition GuiElement.h:284
int ymin
Element's min Y offset allowed.
Definition GuiElement.h:272
int alpha
Element alpha value (0-255)
Definition GuiElement.h:278
virtual void resetText()
Called when the language has changed, to obtain new text values for all text elements.
int getStateChan()
Gets the controller channel that last changed the element's state.
bool selectable
Whether or not this element selectable (can change to SELECTED state)
Definition GuiElement.h:293
void setClickable(bool c)
Sets whether or not the element is clickable.
STATE getState()
Gets the element's current state.
int yoffsetDyn
Element Y offset, dynamic (added to yoffset value for animation effects)
Definition GuiElement.h:277
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
void setTrigger(GuiTrigger *t)
Set a new GuiTrigger for the element.
int width
Element width.
Definition GuiElement.h:268
ALIGN_H alignmentHor
Horizontal element alignment, respective to parent element (LEFT, RIGHT, CENTRE)
Definition GuiElement.h:289
virtual void setState(STATE s, int c=-1)
Sets the element's state.
void setRumble(bool r)
Sets whether or not the element is requesting a rumble event.
float scaleDyn
Element scale, dynamic (multiplied by alpha value for blending/fading effects)
Definition GuiElement.h:282
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
virtual void setFocus(int f)
Sets the element's focus.
void setMaxY(int y)
Sets the maximum y offset of the element.
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
float getScaleY()
Gets the element's current y scale Considers scale, scaleDyn, and the parent element's GetScale() val...
virtual void setVisible(bool v)
Sets the element's visibility.
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.
virtual void update(InputController *c)
Called constantly to allow the element to respond to the current input data.
int getEffect()
Gets the current element effects.
GuiTrigger * trigger[MAX_TRIGGERS]
GuiTriggers (input actions) that this element responds to.
Definition GuiElement.h:263
void setSize(int w, int h)
Sets the size (width/height) of the element.
int getWidth()
Gets the current width of the element.
bool rumble
Wiimote rumble (on/off) - set to on when this element requests a rumble event.
Definition GuiElement.h:297
GuiElement * getParent()
Gets the element's parent.
void setAlpha(int a)
Sets the element's alpha value.
GuiElement()
Constructor.
Definition GuiElement.cpp:9
bool visible
Visibility of the element.
Definition GuiElement.h:296
virtual void layout()
Brings the element's own layout up to date.
int getLeft()
Gets the current leftmost coordinate of the element Considers horizontal alignment,...
void setMinX(int x)
Sets the minimum x offset of the element.
void setScaleX(float s)
Sets the element's x scale.
virtual void setLayoutPosition(int x, int y)
Places the element so its top-left corner is at (x, y) relative to its parent's top-left (default: LE...
ALIGN_V alignmentVert
Horizontal element alignment, respective to parent element (TOP, BOTTOM, MIDDLE)
Definition GuiElement.h:290
int getMaxY()
Gets the maximum y offset of the element.
void setParent(GuiElement *e)
Set the element's parent.
int focus
Element focus (-1 = focus disabled, 0 = not focused, 1 = focused)
Definition GuiElement.h:267
int getMinX()
Gets the minimum x offset of the element.
void setScale(float s)
Sets the element's x and y scale.
int getMaxX()
Gets the maximum x offset of the element.
float yscale
Element Y scale (1 = 100%)
Definition GuiElement.h:281
int yoffset
Element Y offset.
Definition GuiElement.h:271
void setMinY(int y)
Sets the minimum y offset of the element.
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.
int getMinY()
Gets the minimum y offset of the element.
bool isSelectable()
Checks whether or not the element is selectable.
int xoffsetDyn
Element X offset, dynamic (added to xoffset value for animation effects)
Definition GuiElement.h:276
void setHoldable(bool h)
Sets whether or not the element is holdable.
bool removeOnDestroy
Whether to instruct parent to remove this element when it is destroyed.
Definition GuiElement.h:266
void setUpdateCallback(UpdateCallback u)
Sets a function to called after after Update() Callback function can be used to response to changes i...
void setPosition(int x, int y)
Sets the element's position.
float getScale()
Gets the element's current scale Considers scale, scaleDyn, and the parent element's GetScale() value...
bool isHoldable()
Checks whether or not the element is holdable.
virtual void collectNavigable(std::vector< GuiElement * > &out)
Appends the elements that selection can move between (default: this element; layout containers add th...
virtual ~GuiElement()
Destructor.
void setEffectGrow()
Shortcut to SetEffectOnOver(EFFECT::SCALE, 4, 110)
virtual void setLayoutSize(int w, int h)
Gives the element a size chosen by a container (default: sets width and height)
void setSelectable(bool s)
Sets whether or not the element is selectable.
virtual void getNaturalSize(int &w, int &h)
Layout protocol: how a layout container (GuiRow, GuiColumn, GuiGrid, GuiOverlay) asks about and arran...
int getHeight()
Gets the height of the element.
void setScaleY(float s)
Sets the element's y scale.
int getAlpha()
Gets the element's alpha value Considers alpha, alphaDyn, and the parent element's GetAlpha() value.
virtual void setAlignment(ALIGN_H hor, ALIGN_V vert)
Sets the element's alignment respective to its parent element.
int xoffset
Element X offset.
Definition GuiElement.h:270
Input-condition matcher attached to a GuiElement (via setSimpleTrigger() etc.) and evaluated against ...
Definition GuiTrigger.h:34
Logical controller for one input channel.
STATE
Interaction state of a GuiElement (default, selected, clicked, held, disabled).
Definition GuiElement.h:36
ALIGN_V
Vertical alignment of a GuiElement relative to its parent.
Definition GuiElement.h:20
ALIGN_H
Horizontal alignment of a GuiElement relative to its parent.
Definition GuiElement.h:28