UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_special.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23*/
24
25#include "../../cl_shared.h"
26#include "../ui_nodes.h"
27#include "../ui_parse.h"
28#include "../ui_actions.h"
29#include "../ui_behaviour.h"
30#include "../ui_lua.h"
31#include "ui_node_window.h"
32#include "ui_node_special.h"
34
36
42{
43 const value_t* prop = UI_GetPropertyFromBehaviour(node->parent->behaviour, node->name);
44 if (prop && prop->type == V_UI_ACTION) {
46 uiAction_t*& value = Com_GetValue<uiAction_t*>(node->parent, prop);
47 if (value != nullptr && node->super == nullptr) {
48 Com_Printf("UI_FuncNodeLoaded: '%s' already defined. Previous function ignored (\"%s\")\n", prop->string, UI_GetPath(node));
49 }
50 value = node->onClick;
51 }
52}
53
55{
56 behaviour->name = "func";
57 behaviour->isVirtual = true;
58 behaviour->isFunction = true;
59 behaviour->manager = UINodePtr(new uiFuncNode());
60 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiFuncNode_t *");
61}
62
64{
65 behaviour->name = "";
66 behaviour->isVirtual = true;
67 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiNullNode_t *");
68}
69
73static void UI_ConfuncCommand_f (void)
74{
75 uiNode_t* node = static_cast<uiNode_t*>(Cmd_Userdata());
76 assert(node);
77 assert(UI_NodeInstanceOf(node, "confunc"));
78 if (node->onClick != nullptr) {
80 }
81 if (node->lua_onClick != LUA_NOREF) {
83 }
84}
85
91static bool UI_ConFuncIsVirtual (const uiNode_t* const node)
92{
93 /* magic way to know if it is a dummy node (used for inherited confunc) */
94 const uiNode_t* dummy = static_cast<const uiNode_t*>(Cmd_GetUserdata(node->name));
95 assert(node);
96 assert(UI_NodeInstanceOf(node, "confunc"));
97 return (dummy != nullptr && dummy->parent == nullptr);
98}
99
104{
105 /* register confunc non inherited */
106 if (node->super == nullptr) {
107 /* don't add a callback twice */
108 if (!Cmd_Exists(node->name)) {
109 Cmd_AddCommand(node->name, UI_ConfuncCommand_f, "Confunc callback");
110 Cmd_AddUserdata(node->name, node);
111 } else {
112 Com_Printf("UI_ParseNodeBody: Command name for confunc '%s' already registered\n", UI_GetPath(node));
113 }
114 } else {
115 /* convert a confunc to an "inherited" confunc if it is possible */
116 if (Cmd_Exists(node->name)) {
117 if (UI_ConFuncIsVirtual(node))
118 return;
119 }
120
121 uiNode_t* dummy = UI_AllocNode(node->name, "confunc", false);
122 Cmd_AddCommand(node->name, UI_ConfuncCommand_f, "Inherited confunc callback");
123 Cmd_AddUserdata(dummy->name, dummy);
124 }
125}
126
128{
129 uiNode::clone(source, clone);
131}
132
138{
139 onWindowClosed(node);
140 if (Cmd_Exists(node->name)) {
141 uiNode_t* userData = (uiNode_t*)Cmd_GetUserdata(node->name);
142 if (userData && userData == node)
143 Cmd_RemoveCommand(node->name);
144 }
145 uiNode::deleteNode(node);
146}
147
152{
153 if (UI_ConFuncIsVirtual(node)) {
154 const value_t* property = UI_GetPropertyFromBehaviour(node->behaviour, "onClick");
155 uiNode_t* userData = static_cast<uiNode_t*>(Cmd_GetUserdata(node->name));
156 UI_AddListener(userData, property, node);
157 }
158}
159
164{
165 if (UI_ConFuncIsVirtual(node)) {
166 const value_t* property = UI_GetPropertyFromBehaviour(node->behaviour, "onClick");
167 uiNode_t* userData = static_cast<uiNode_t*>(Cmd_GetUserdata(node->name));
168 UI_RemoveListener(userData, property, node);
169 }
170}
171
173{
174 behaviour->name = "confunc";
175 behaviour->isVirtual = true;
176 behaviour->isFunction = true;
177 behaviour->manager = UINodePtr(new uiConFuncNode());
178 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiConFuncNode_t *");
179}
180
181static void UI_CvarListenerNodeCallback (const char* cvarName, const char* oldValue, const char* newValue, void* data)
182{
183 linkedList_t* list = static_cast<linkedList_t*>(data);
184 linkedList_t* params = nullptr;
185 LIST_AddString(&params, oldValue);
186 LIST_AddString(&params, newValue);
187 while (list) {
188 uiNode_t* node = static_cast<uiNode_t*>(list->data);
189 UI_ExecuteEventActionsEx(node, node->onClick, params);
190 list = list->next;
191 }
192}
193
198{
201 if (l == nullptr) {
202 Com_Printf("Node %s is not binded: cvar %s not found\n", UI_GetPath(node), node->name);
203 return;
204 }
205
206 if (LIST_GetPointer(static_cast<linkedList_t*>(l->data), node) == nullptr) {
207 LIST_AddPointer(reinterpret_cast<linkedList_t**>(&l->data), node);
208 }
209}
210
215{
216 cvar_t* var;
217
218 var = Cvar_FindVar(node->name);
219 if (var == nullptr)
220 return;
221
223 while (l) {
225 LIST_Remove(reinterpret_cast<linkedList_t**>(&l->data), node);
226 if (LIST_IsEmpty(static_cast<linkedList_t*>(l->data))) {
228 }
229 break;
230 }
231 l = l->next;
232 }
233}
234
239
241{
242 uiNode::deleteNode(node);
243 onWindowClosed(node);
244}
245
247{
248 uiNode::clone(source, clone);
250}
251
252static void UI_CvarListenerNodeForceBind (uiNode_t* node, const uiCallContext_t* context)
253{
255}
256
258{
259 behaviour->name = "cvarlistener";
260 behaviour->isVirtual = true;
261 behaviour->isFunction = true;
262 behaviour->manager = UINodePtr(new uiCvarNode());
263 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiCvarListenerNode_t *");
264
265 /* Force to bind the node to the cvar */
267}
Share stuff between the different cgame implementations.
void onWindowOpened(uiNode_t *node, linkedList_t *params) override
Callback every time the parent window is opened (pushed into the active window stack).
void onWindowClosed(uiNode_t *node) override
Callback every time the parent window is closed (pop from the active window stack).
void clone(uiNode_t const *source, uiNode_t *clone) override
void deleteNode(uiNode_t *node) override
Cleanup tasks on removing a console function.
void onLoaded(uiNode_t *node) override
Call after the script initialized the node.
void onWindowClosed(uiNode_t *node)
Callback every time the parent window is closed (pop from the active window stack).
void deleteNode(uiNode_t *node)
void clone(uiNode_t const *source, uiNode_t *clone)
void onWindowOpened(uiNode_t *node, linkedList_t *params)
void onLoaded(uiNode_t *node)
Call after the script initialized the node.
virtual void deleteNode(uiNode_t *node)
virtual void clone(uiNode_t const *source, uiNode_t *clone)
bool Cmd_Exists(const char *cmdName)
Checks whether a function exists already.
Definition cmd.cpp:887
void Cmd_RemoveCommand(const char *cmdName)
Removes a command from script interface.
Definition cmd.cpp:786
void Cmd_AddUserdata(const char *cmdName, void *userdata)
Adds userdata to the console command.
Definition cmd.cpp:724
void * Cmd_GetUserdata(const char *cmdName)
Fetches the userdata for a console command.
Definition cmd.cpp:700
void Cmd_AddCommand(const char *cmdName, xcommand_t function, const char *desc)
Add a new command to the script interface.
Definition cmd.cpp:744
void * Cmd_Userdata(void)
Return the userdata of the called command.
Definition cmd.cpp:534
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
cvarChangeListener_t * Cvar_RegisterChangeListener(const char *varName, cvarChangeListenerFunc_t listenerFunc)
Registers a listener that is executed each time a cvar changed its value.
Definition cvar.cpp:446
void Cvar_UnRegisterChangeListener(const char *varName, cvarChangeListenerFunc_t listenerFunc)
Unregisters a cvar change listener.
Definition cvar.cpp:487
cvar_t * Cvar_FindVar(const char *varName)
Searches for a cvar given by parameter.
Definition cvar.cpp:106
linkedList_t * LIST_GetPointer(linkedList_t *list, const void *data)
Searches for the first occurrence of a given pointer.
Definition list.cpp:91
void LIST_AddString(linkedList_t **listDest, const char *data)
Adds an string to a new or to an already existing linked list. The string is copied here.
Definition list.cpp:139
void LIST_AddPointer(linkedList_t **listDest, void *data)
Adds just a pointer to a new or to an already existing linked list.
Definition list.cpp:153
bool LIST_Remove(linkedList_t **list, const void *data)
Definition list.cpp:213
bool LIST_IsEmpty(const linkedList_t *list)
Checks whether the given list is empty.
Definition list.cpp:335
QGL_EXTERN GLsizei const GLvoid * data
Definition r_gl.h:89
T & Com_GetValue(void *const object, value_t const *const value)
Definition scripts.h:174
Header for lua script functions.
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition cvar.h:71
cvarChangeListener_t * changeListener
Definition cvar.h:83
struct cvarChangeListener_s * next
Definition cvar.h:63
cvarChangeListenerFunc_t exec
Definition cvar.h:62
void * data
Definition list.h:31
linkedList_t * next
Definition list.h:32
Atomic element to store UI scripts The parser use this atom to translate script action into many tree...
Definition ui_actions.h:144
node behaviour, how a node work
const char * name
void * lua_SWIG_typeinfo
UINodePtr manager
Contain the context of the calling of a function.
Definition ui_actions.h:208
Atomic structure used to define most of the UI.
Definition ui_nodes.h:80
uiNode_t const * super
Definition ui_nodes.h:84
uiBehaviour_t * behaviour
Definition ui_nodes.h:83
uiNode_t * parent
Definition ui_nodes.h:92
LUA_EVENT lua_onClick
Definition ui_nodes.h:148
char name[MAX_VAR]
Definition ui_nodes.h:82
struct uiAction_s * onClick
Definition ui_nodes.h:135
const char * string
Definition scripts.h:168
valueTypes_t type
Definition scripts.h:169
void UI_ExecuteEventActionsEx(uiNode_t *source, const uiAction_t *firstAction, linkedList_t *params)
void UI_RemoveListener(uiNode_t *node, const value_t *property, uiNode_t *functionNode)
Remove a function callback from a node event. There can be more than on listener.
void UI_AddListener(uiNode_t *node, const value_t *property, const uiNode_t *functionNode)
Add a callback of a function into a node event. There can be more than on listener.
void UI_ExecuteConFuncActions(uiNode_t *source, const uiAction_t *firstAction)
allow to inject command param into cmd of confunc command
const value_t * UI_GetPropertyFromBehaviour(const uiBehaviour_t *behaviour, const char *name)
Get a property from a behaviour or his inheritance It use a dichotomic search.
const struct value_s * UI_RegisterNodeMethod(uiBehaviour_t *behaviour, const char *name, uiNodeMethod_t function)
Register a node method to a behaviour.
bool UI_ExecuteLuaConFunc(uiNode_t *node, LUA_FUNCTION fcn)
Executes a lua based confunc node.
Definition ui_lua.cpp:323
Basic lua initialization for the ui.
void * UI_SWIG_TypeQuery(const char *name)
This function queries the SWIG type table for a type information structure. It is used in combination...
bool UI_NodeInstanceOf(const uiNode_t *node, const char *behaviourName)
Check the node inheritance.
Definition ui_node.cpp:453
SharedPtr< uiNode > UINodePtr
static bool UI_ConFuncIsVirtual(const uiNode_t *const node)
Checks whether the given node is a virtual confunc that can be overridden from inheriting nodes.
void UI_RegisterNullNode(uiBehaviour_t *behaviour)
static void UI_ConfuncCommand_f(void)
Callback to execute a confunc.
static void UI_CvarListenerNodeCallback(const char *cvarName, const char *oldValue, const char *newValue, void *data)
static void UI_CvarListenerNodeForceBind(uiNode_t *node, const uiCallContext_t *context)
void UI_RegisterCvarFuncNode(uiBehaviour_t *behaviour)
void UI_RegisterFuncNode(uiBehaviour_t *behaviour)
void UI_RegisterConFuncNode(uiBehaviour_t *behaviour)
static void UI_CvarListenerNodeBind(uiNode_t *node)
Callback every time the parent window is opened (pushed into the active window stack).
uiNode_t * UI_AllocNode(const char *name, const char *type, bool isDynamic)
Allocate a node into the UI memory.
Definition ui_nodes.cpp:381
const char * UI_GetPath(const uiNode_t *node)
Return a path from a window to a node.
Definition ui_nodes.cpp:174
#define V_UI_ACTION
Definition ui_parse.h:54