libgui 3.00
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
GuiElement.h
1/****************************************************************************
2 * libgui
3 * Daryl Borth 2009-2026
4 * GuiElement.h
5 ***************************************************************************/
6#pragma once
7
8#include <vector>
9#include <cstdint>
10
11#define MAX_TRIGGERS 5
12
13typedef void (*UpdateCallback)(void * e);
14
15class GuiTrigger;
16class GuiWindow;
17class InputController;
18
21enum class ALIGN_V {
22 TOP,
23 BOTTOM,
24 MIDDLE
25};
26
29enum class ALIGN_H {
30 LEFT,
31 RIGHT,
32 CENTRE
33};
34
37enum class STATE {
38 DEFAULT,
39 SELECTED,
40 CLICKED,
41 HELD,
42 DISABLED
43};
44
47enum EFFECT {
48 SLIDE_TOP = (1u << 0),
49 SLIDE_BOTTOM = (1u << 1),
50 SLIDE_RIGHT = (1u << 2),
51 SLIDE_LEFT = (1u << 3),
52 SLIDE_IN = (1u << 4),
53 SLIDE_OUT = (1u << 5),
54 FADE = (1u << 6),
55 SCALE = (1u << 7),
56 COLOR_TRANSITION = (1u << 8)
57};
58
62{
63 public:
65 GuiElement();
67 virtual ~GuiElement();
70 void setParent(GuiElement * e);
75 virtual void remove(GuiElement*) {}
77 void setRemoveOnDestroy(bool autoRemove) { removeOnDestroy = autoRemove; }
81 int getLeft();
85 int getTop();
88 void setMinY(int y);
91 int getMinY();
94 void setMaxY(int y);
97 int getMaxY();
100 void setMinX(int x);
103 int getMinX();
106 void setMaxX(int x);
109 int getMaxX();
112 int getWidth();
115 int getHeight();
119 void setSize(int w, int h);
122 bool isVisible();
125 bool isSelectable();
128 bool isClickable();
131 bool isHoldable();
134 void setSelectable(bool s);
137 void setClickable(bool c);
140 void setHoldable(bool h);
143 STATE getState();
146 int getStateChan();
149 void setAlpha(int a);
153 int getAlpha();
156 void setScale(float s);
159 void setScaleX(float s);
162 void setScaleY(float s);
166 void setScale(int w, int h);
169 float getScale();
172 float getScaleX();
175 float getScaleY();
178 void setTrigger(GuiTrigger * t);
182 void setTrigger(uint8_t i, GuiTrigger * t);
185 bool isRumble();
188 void setRumble(bool r);
193 void setEffect(int e, int a, int t=0);
198 void setEffectOnOver(int e, int a, int t=0);
201 void setEffectGrow(int percent = 110);
204 int getEffect();
209 bool isInside(int x, int y);
213 void setPosition(int x, int y);
216 void updateEffects();
219 void setUpdateCallback(UpdateCallback u);
222 int isFocused();
225 virtual void setVisible(bool v);
228 virtual void setFocus(int f);
232 virtual void setState(STATE s, int c = -1);
234 virtual void resetState();
237 virtual int getSelected();
241 virtual void setAlignment(ALIGN_H hor, ALIGN_V vert);
245 virtual void getNaturalSize(int& w, int& h);
248 virtual void setLayoutPosition(int x, int y);
250 virtual void setLayoutSize(int w, int h);
253 virtual void layout();
256 virtual void collectNavigable(std::vector<GuiElement*>& out);
258 virtual void resetText();
261 virtual void update(InputController * c);
263 virtual void draw() = 0;
266 virtual GuiWindow* getAsWindow() { return nullptr; }
267 protected:
268 GuiTrigger * trigger[MAX_TRIGGERS];
269 UpdateCallback updateCB;
272 int focus;
273 int width;
274 int height;
277 int ymin;
278 int ymax;
279 int xmin;
280 int xmax;
283 int alpha;
285 float xscale;
286 float yscale;
287 float scaleDyn;
300 bool holdable;
301 bool visible;
302 bool rumble;
303};
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:290
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:285
int ymax
Element's max Y offset allowed.
Definition GuiElement.h:278
int alphaDyn
Element alpha, dynamic (multiplied by alpha value for blending/fading effects)
Definition GuiElement.h:284
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:280
virtual int getSelected()
Gets whether or not the element is in STATE::SELECTED.
int xmin
Element's min X offset allowed.
Definition GuiElement.h:279
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:289
int ymin
Element's min Y offset allowed.
Definition GuiElement.h:277
int alpha
Element alpha value (0-255)
Definition GuiElement.h:283
void setRemoveOnDestroy(bool autoRemove)
Whether to notify parent on destruction to allow removal.
Definition GuiElement.h:77
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:298
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:282
int stateChan
Which controller channel is responsible for the last change in state.
Definition GuiElement.h:297
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:292
void setTrigger(GuiTrigger *t)
Set a new GuiTrigger for the element.
int width
Element width.
Definition GuiElement.h:273
ALIGN_H alignmentHor
Horizontal element alignment, respective to parent element (LEFT, RIGHT, CENTRE)
Definition GuiElement.h:294
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:287
void setEffectGrow(int percent=110)
Shortcut to SetEffectOnOver(EFFECT::SCALE, 4, percent)
int effectsOver
Effects to enable when wiimote cursor is over this element.
Definition GuiElement.h:291
UpdateCallback updateCB
Callback function to call when this element is updated.
Definition GuiElement.h:269
int effects
Currently enabled effect(s).
Definition GuiElement.h:288
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:274
bool holdable
Whether or not this element is holdable (can change to HELD state)
Definition GuiElement.h:300
bool clickable
Whether or not this element is clickable (can change to CLICKED state)
Definition GuiElement.h:299
bool isClickable()
Checks whether or not the element is clickable.
GuiElement * parentElement
Parent element.
Definition GuiElement.h:270
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:268
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:302
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:301
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:295
int getMaxY()
Gets the maximum y offset of the element.
void setParent(GuiElement *e)
Set the element's parent.
virtual void draw()=0
Called constantly to redraw the element.
int focus
Element focus (-1 = focus disabled, 0 = not focused, 1 = focused)
Definition GuiElement.h:272
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:286
int yoffset
Element Y offset.
Definition GuiElement.h:276
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:293
STATE state
Element state (DEFAULT, SELECTED, CLICKED, DISABLED)
Definition GuiElement.h:296
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.
virtual void remove(GuiElement *)
Removes the specified child.
Definition GuiElement.h:75
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:281
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:271
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.
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...
virtual GuiWindow * getAsWindow()
Attempts to cast this element as a GuiWindow (default implementation returns nullptr)
Definition GuiElement.h:266
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:275
Input-condition matcher attached to a GuiElement (via setSimpleTrigger() etc.) and evaluated against ...
Definition GuiTrigger.h:34
Allows GuiElements to be grouped together into a "window".
Definition GuiWindow.h:11
Logical controller for one input channel.
STATE
Interaction state of a GuiElement (default, selected, clicked, held, disabled).
Definition GuiElement.h:37
EFFECT
Bitmask of transition effects (slide, fade, scale, color) applied to a GuiElement.
Definition GuiElement.h:47
ALIGN_V
Vertical alignment of a GuiElement relative to its parent.
Definition GuiElement.h:21
ALIGN_H
Horizontal alignment of a GuiElement relative to its parent.
Definition GuiElement.h:29