libGumball 0.0.2
C23-Based, libGimbal-powered UI Library
Loading...
Searching...
No Matches
gumball_common.h
Go to the documentation of this file.
1#ifndef GUM_COMMON_H
2#define GUM_COMMON_H
3
4// View this file's documentation online: https://libgumball.psyops.studio/gumball__common_8h.html
5
6/*! \file
7 * \ingroup elements
8 *
9 * gumball_common is a collection of macros that are
10 * common to all elements in libGumball.
11 *
12 * \author 2025 Agustín Bellagamba
13 * \copyright MIT License
14*/
15
16#include <gimbal/gimbal_meta.h>
17#include <gumball/types/gumball_renderer.h>
18
19//! Updates all the UI elements
20#define GUM_update() ((GUM_update)())
21
22//! Disables updating for a given element
23#define GUM_update_disable(element) ((GUM_update_disable) (GBL_OBJECT(element)))
24
25//! Enables updating for a given element
26#define GUM_update_enable(element) ((GUM_update_enable) (GBL_OBJECT(element)))
27
28//! Disables updating for a given element and all of its children
29#define GUM_update_disableAll(element) ((GUM_update_disableAll) (GBL_OBJECT(element)))
30
31//! Enables updating for a given element and all of its children
32#define GUM_update_enableAll(element) ((GUM_update_enableAll) (GBL_OBJECT(element)))
33
34/*!
35 * \brief Draws all the UI elements in the draw queue
36 *
37 * Drawable elements are added to the draw queue when they are created
38 * Elements are drawn from z-index 0 to z-index 255, with 255 being the front
39 * If two elements have the same z-index, they are drawn in the order they were created (or enabled!)
40 * Optionally takes in a GUM_Renderer if your backend needs one (defaults to nullptr)
41*/
42#define GUM_draw(/* renderer=nullptr */...) GUM_draw_(__VA_OPT__(__VA_ARGS__,) nullptr)
43
44
45//! Disables drawing for a given element
46#define GUM_draw_disable(obj) ((GUM_draw_disable) (GBL_OBJECT(obj)))
47
48//! Enables drawing for a given element
49#define GUM_draw_enable(obj) ((GUM_draw_enable) (GBL_OBJECT(obj)))
50
51//! Disables drawing for a given element and all of its children
52#define GUM_draw_disableAll(obj) ((GUM_draw_disableAll) (GBL_OBJECT(obj)))
53
54//! Enables drawing for a given element and all of its children
55#define GUM_draw_enableAll(obj) ((GUM_draw_enableAll) (GBL_OBJECT(obj)))
56
57//! Takes in two UI elements, adds the second one as a child of the first.
58#define GUM_add_child(self, child) (GblObject_addChild(GBL_OBJECT(self), GBL_OBJECT(child)))
59
60//! Takes in two UI elements, removes the second one as a child of the first.
61#define GUM_remove_child(self, child) (GblObject_removeChild(GBL_OBJECT(self), GBL_OBJECT(child)))
62
63/*!
64 * Takes in a UI element and an index, returns the child of that element at that index as a
65 * GblObject that you can cast to the appropriate type.
66 *
67 * Example:
68 * \code GUM_Widget *pChild = GUM_WIDGET(GUM_get_child_at(pObj, 0)); \endcode
69*/
70#define GUM_get_child_at(self, index) (GblObject_findChildByIndex(GBL_OBJECT(self), index))
71
72//! Decrements the reference count of a UI element, freeing it if it reaches zero.
73//! Also recursively unrefs all of its children.
74#define GUM_unref(obj) ((GUM_unref)(GBL_OBJECT(obj)))
75
76/*!
77 * Connects an element's (typically a GUM_Button) signal to a callback.
78 * The callback should take in a pointer to the UI element type that emitted the signal, and return void.
79 * Optionally takes in userdata (a void* of whatever you want to pass in).
80 * See GUM_Button for signal names
81 *
82 * \code {.c}
83 * void buttonCallback(GUM_Button *pButton) {
84 * void *ud = GUM_userData();
85 * printf("Button pressed!\n");
86 * printf("UserData: %i\n", *(int*)ud);
87 * }
88 *
89 * auto pButton = GUM_Button_create();
90 * int data = 42;
91 * GUM_connect(pButton, "onPressPrimary", buttonCallback, &data);
92 * \endcode
93*/
94#define GUM_connect(emitter, signal, callback, /* userdata=nullptr */...) (GBL_CONNECT(emitter, signal, emitter, callback __VA_OPT__(,) __VA_ARGS__))
95
96//! Inside a callback connected to a signal, returns the userdata that was passed in
97#define GUM_userData() GblClosure_currentUserdata()
98
99//! Looks up the property of an element by name, storing its value in the pointer passed as a variadic argument
100#define GUM_property(obj, name, /*value*/ ...) (GblObject_property(GBL_OBJECT(obj), name, __VA_ARGS__))
101
102//! Sets the property with the given name to the value given by the pointer passed through the variadic argument list
103#define GUM_setProperty(obj, name, /*value*/...) (GblObject_setProperty(GBL_OBJECT(obj), name, __VA_ARGS__))
104
105////////// Implementation details, Grugs please ignore
106//!\cond
107#define GUM_draw_(renderer, ...) (GUM_draw)(renderer)
108
109GBL_EXPORT GBL_RESULT (GUM_draw) (GUM_Renderer *pRenderer) GBL_NOEXCEPT;
110GBL_EXPORT GBL_RESULT (GUM_update) (void) GBL_NOEXCEPT;
111GBL_EXPORT GBL_RESULT (GUM_update_disable) (GblObject *pSelf) GBL_NOEXCEPT;
112GBL_EXPORT GBL_RESULT (GUM_update_enable) (GblObject *pSelf) GBL_NOEXCEPT;
113GBL_EXPORT GBL_RESULT (GUM_update_disableAll)(GblObject *pSelf) GBL_NOEXCEPT;
114GBL_EXPORT GBL_RESULT (GUM_update_enableAll)(GblObject *pSelf) GBL_NOEXCEPT;
115GBL_EXPORT GBL_RESULT (GUM_unref) (GblObject *pSelf) GBL_NOEXCEPT;
116GBL_EXPORT void (GUM_draw_enable) (GblObject *pSelf) GBL_NOEXCEPT;
117GBL_EXPORT void (GUM_draw_disable) (GblObject *pSelf) GBL_NOEXCEPT;
118GBL_EXPORT void (GUM_draw_enableAll) (GblObject *pSelf) GBL_NOEXCEPT;
119GBL_EXPORT void (GUM_draw_disableAll) (GblObject *pSelf) GBL_NOEXCEPT;
120//!\endcond
121
122#endif
#define GUM_update_disable(element)
Disables updating for a given element.
Definition gumball_common.h:23
#define GUM_draw(...)
Draws all the UI elements in the draw queue.
Definition gumball_common.h:42
#define GUM_draw_enableAll(obj)
Enables drawing for a given element and all of its children.
Definition gumball_common.h:55
#define GUM_draw_disable(obj)
Disables drawing for a given element.
Definition gumball_common.h:46
#define GUM_draw_enable(obj)
Enables drawing for a given element.
Definition gumball_common.h:49
#define GUM_update_disableAll(element)
Disables updating for a given element and all of its children.
Definition gumball_common.h:29
#define GUM_update()
Updates all the UI elements.
Definition gumball_common.h:20
#define GUM_update_enable(element)
Enables updating for a given element.
Definition gumball_common.h:26
#define GUM_unref(obj)
Decrements the reference count of a UI element, freeing it if it reaches zero.
Definition gumball_common.h:74
#define GUM_draw_disableAll(obj)
Disables drawing for a given element and all of its children.
Definition gumball_common.h:52
#define GUM_update_enableAll(element)
Enables updating for a given element and all of its children.
Definition gumball_common.h:32