libGumball 0.0.1
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//! Takes in a UI element, updates it and all of its children.
20#define GUM_update(obj) ((GUM_update)(GBL_OBJECT(obj)))
21
22//! Takes in two UI elements, adds the second one as a child of the first.
23#define GUM_add_child(self, child) (GblObject_addChild(GBL_OBJECT(self), GBL_OBJECT(child)))
24
25//! Takes in two UI elements, removes the second one as a child of the first.
26#define GUM_remove_child(self, child) (GblObject_removeChild(GBL_OBJECT(self), GBL_OBJECT(child)))
27
28/*!
29 * Takes in a UI element and an index, returns the child of that element at that index as a
30 * GblObject that you can cast to the appropriate type.
31 *
32 * Example:
33 * \code GUM_Widget *pChild = GUM_WIDGET(GUM_get_child_at(pObj, 0)); \endcode
34*/
35#define GUM_get_child_at(self, index) (GblObject_findChildByIndex(GBL_OBJECT(self), index))
36
37//! Decrements the reference count of a UI element, freeing it if it reaches zero.
38//! Also recursively unrefs all of its children.
39#define GUM_unref(obj) ((GUM_unref)(GBL_OBJECT(obj)))
40
41/*!
42 * Connects an element's (typically a GUM_Button) signal to a callback.
43 * The callback should take in a pointer to the UI element type that emitted the signal, and return void.
44 * Optionally takes in userdata (a void* of whatever you want to pass in).
45 * See GUM_Button for signal names
46 *
47 * \code
48 * void buttonCallback(GUM_Button *pButton) {
49 * void *ud = GUM_userData();
50 * printf("Button pressed!\n");
51 * printf("UserData: %i\n", *(int*)ud);
52 * }
53 *
54 * auto pButton = GUM_Button_create();
55 * int data = 42;
56 * GUM_connect(pButton, "onPressPrimary", buttonCallback, &data);
57 * \endcode
58 })
59*/
60#define GUM_connect(emitter, signal, callback, /* userdata=nullptr */...) (GBL_CONNECT(emitter, signal, emitter, callback __VA_OPT__(,) __VA_ARGS__))
61
62//! Inside a callback connected to a signal, returns the userdata that was passed in
63#define GUM_userData() GblClosure_currentUserdata()
64
65/*!
66 * \brief Draws all the UI elements in the draw queue
67 *
68 * Drawable elements are added to the draw queue when they are created
69 * Elements are drawn from z-index 0 to z-index 255, with 255 being the front
70 * If two elements have the same z-index, they are drawn in the order they were created (or enabled!)
71 * Optionally takes in a GUM_Renderer if your backend needs one (defaults to nullptr)
72*/
73#define GUM_draw(/* renderer=nullptr */...) GUM_draw_(__VA_OPT__(__VA_ARGS__,) nullptr)
74
75
76//! Disables drawing for a given element
77#define GUM_draw_disable(obj) ((GUM_draw_disable) (GBL_OBJECT(obj)))
78
79//! Enables drawing for a given element
80#define GUM_draw_enable(obj) ((GUM_draw_enable) (GBL_OBJECT(obj)))
81
82//! Disables drawing for a given element and all of its children
83#define GUM_draw_disableAll(obj) ((GUM_draw_disableAll) (GBL_OBJECT(obj)))
84
85//! Enables drawing for a given element and all of its children
86#define GUM_draw_enableAll(obj) ((GUM_draw_enableAll) (GBL_OBJECT(obj)))
87
88//! Looks up the property of an element by name, storing its value in the pointer passed as a variadic argument
89#define GUM_property(obj, name, /*value*/ ...) (GblObject_property(GBL_OBJECT(obj), name, __VA_ARGS__))
90
91//! Sets the property with the given name to the value given by the pointer passed through the variadic argument list
92#define GUM_setProperty(obj, name, /*value*/...) (GblObject_setProperty(GBL_OBJECT(obj), name, __VA_ARGS__))
93
94////////// Implementation details, Grugs please ignore
95//!\cond
96#define GUM_draw_(renderer, ...) (GUM_draw)(renderer)
97
98GBL_EXPORT GBL_RESULT (GUM_draw) (GUM_Renderer *pRenderer) GBL_NOEXCEPT;
99GBL_EXPORT GBL_RESULT (GUM_update) (GblObject *pSelf) GBL_NOEXCEPT;
100GBL_EXPORT GBL_RESULT (GUM_unref) (GblObject *pSelf) GBL_NOEXCEPT;
101GBL_EXPORT void (GUM_draw_enable) (GblObject *pSelf) GBL_NOEXCEPT;
102GBL_EXPORT void (GUM_draw_disable) (GblObject *pSelf) GBL_NOEXCEPT;
103GBL_EXPORT void (GUM_draw_enableAll) (GblObject *pSelf) GBL_NOEXCEPT;
104GBL_EXPORT void (GUM_draw_disableAll) (GblObject *pSelf) GBL_NOEXCEPT;
105//!\endcond
106
107#endif
#define GUM_draw(...)
Draws all the UI elements in the draw queue.
Definition gumball_common.h:73
#define GUM_draw_enableAll(obj)
Enables drawing for a given element and all of its children.
Definition gumball_common.h:86
#define GUM_draw_disable(obj)
Disables drawing for a given element.
Definition gumball_common.h:77
#define GUM_draw_enable(obj)
Enables drawing for a given element.
Definition gumball_common.h:80
#define GUM_update(obj)
Takes in a UI element, updates it and all of its children.
Definition gumball_common.h:20
#define GUM_unref(obj)
Decrements the reference count of a UI element, freeing it if it reaches zero.
Definition gumball_common.h:39
#define GUM_draw_disableAll(obj)
Disables drawing for a given element and all of its children.
Definition gumball_common.h:83