UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_special.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 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"
33 #include "ui_node_abstractnode.h"
34 
35 #include "../../../common/scripts_lua.h"
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 
73 static 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) {
79  UI_ExecuteConFuncActions(node, node->onClick);
80  }
81  if (node->lua_onClick != LUA_NOREF) {
82  UI_ExecuteLuaConFunc (node, node->lua_onClick);
83  }
84 }
85 
91 static 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 
127 void uiConFuncNode::clone (const uiNode_t* source, uiNode_t* clone)
128 {
129  uiNode::clone(source, clone);
130  onLoaded(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 
181 static 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) {
224  if (l->exec == UI_CvarListenerNodeCallback) {
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 
236 {
238 }
239 
241 {
242  uiNode::deleteNode(node);
243  onWindowClosed(node);
244 }
245 
246 void uiCvarNode::clone (const uiNode_t* source, uiNode_t* clone)
247 {
248  uiNode::clone(source, clone);
250 }
251 
252 static 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 */
266  UI_RegisterNodeMethod(behaviour, "forceBind", UI_CvarListenerNodeForceBind);
267 }
void UI_RegisterFuncNode(uiBehaviour_t *behaviour)
void Cmd_AddCommand(const char *cmdName, xcommand_t function, const char *desc)
Add a new command to the script interface.
Definition: cmd.cpp:744
bool UI_ExecuteLuaConFunc(uiNode_t *node, LUA_FUNCTION fcn)
Executes a lua based confunc node.
Definition: ui_lua.cpp:323
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
uiNode_t * parent
Definition: ui_nodes.h:92
void Cmd_RemoveCommand(const char *cmdName)
Removes a command from script interface.
Definition: cmd.cpp:786
bool UI_NodeInstanceOf(const uiNode_t *node, const char *behaviourName)
Check the node inheritance.
Definition: ui_node.cpp:441
void Cvar_UnRegisterChangeListener(const char *varName, cvarChangeListenerFunc_t listenerFunc)
Unregisters a cvar change listener.
Definition: cvar.cpp:487
static void UI_CvarListenerNodeBind(uiNode_t *node)
Callback every time the parent window is opened (pushed into the active window stack) ...
void onLoaded(uiNode_t *node) override
Call after the script initialized the node.
const char * name
Definition: ui_behaviour.h:41
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
void * data
Definition: list.h:31
char name[MAX_VAR]
Definition: ui_nodes.h:82
uiBehaviour_t * behaviour
Definition: ui_nodes.h:83
UINodePtr manager
Definition: ui_behaviour.h:43
void deleteNode(uiNode_t *node)
void UI_RegisterNullNode(uiBehaviour_t *behaviour)
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
valueTypes_t type
Definition: scripts.h:170
virtual void clone(uiNode_t const *source, uiNode_t *clone)
struct uiAction_s * onClick
Definition: ui_nodes.h:135
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.
uiNode_t * UI_AllocNode(const char *name, const char *type, bool isDynamic)
Allocate a node into the UI memory.
Definition: ui_nodes.cpp:381
void onLoaded(uiNode_t *node)
Call after the script initialized the node.
void * Cmd_Userdata(void)
Return the userdata of the called command.
Definition: cmd.cpp:534
cvarChangeListenerFunc_t exec
Definition: cvar.h:62
bool LIST_Remove(linkedList_t **list, const void *data)
Definition: list.cpp:213
void * Cmd_GetUserdata(const char *cmdName)
Fetches the userdata for a console command.
Definition: cmd.cpp:700
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
bool Cmd_Exists(const char *cmdName)
Checks whether a function exists already.
Definition: cmd.cpp:887
void UI_ExecuteEventActionsEx(uiNode_t *source, const uiAction_t *firstAction, linkedList_t *params)
Definition: ui_actions.cpp:735
SharedPtr< uiNode > UINodePtr
const char * UI_GetPath(const uiNode_t *node)
Return a path from a window to a node.
Definition: ui_nodes.cpp:174
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
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.
Definition: ui_actions.cpp:835
static void UI_CvarListenerNodeForceBind(uiNode_t *node, const uiCallContext_t *context)
static void UI_ConfuncCommand_f(void)
Callback to execute a confunc.
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
void onWindowOpened(uiNode_t *node, linkedList_t *params) override
Callback every time the parent window is opened (pushed into the active window stack) ...
const char * string
Definition: scripts.h:169
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_SWIG_TypeQuery(const char *name)
This function queries the SWIG type table for a type information structure. It is used in combination...
void deleteNode(uiNode_t *node) override
Cleanup tasks on removing a console function.
void onWindowClosed(uiNode_t *node)
Callback every time the parent window is closed (pop from the active window stack) ...
Contain the context of the calling of a function.
Definition: ui_actions.h:208
void Cmd_AddUserdata(const char *cmdName, void *userdata)
Adds userdata to the console command.
Definition: cmd.cpp:724
void UI_ExecuteConFuncActions(uiNode_t *source, const uiAction_t *firstAction)
allow to inject command param into cmd of confunc command
Definition: ui_actions.cpp:717
cvarChangeListener_t * changeListener
Definition: cvar.h:83
node behaviour, how a node work
Definition: ui_behaviour.h:39
void UI_RegisterCvarFuncNode(uiBehaviour_t *behaviour)
void clone(uiNode_t const *source, uiNode_t *clone) override
void onWindowClosed(uiNode_t *node) override
Callback every time the parent window is closed (pop from the active window stack) ...
linkedList_t * LIST_GetPointer(linkedList_t *list, const void *data)
Searches for the first occurrence of a given pointer.
Definition: list.cpp:91
static void UI_CvarListenerNodeCallback(const char *cvarName, const char *oldValue, const char *newValue, void *data)
uiNode_t const * super
Definition: ui_nodes.h:84
linkedList_t * next
Definition: list.h:32
virtual void deleteNode(uiNode_t *node)
void clone(uiNode_t const *source, uiNode_t *clone)
GLsizei const GLvoid * data
Definition: r_gl.h:152
#define V_UI_ACTION
Definition: ui_parse.h:54
struct cvarChangeListener_s * next
Definition: cvar.h:63
LUA_EVENT lua_onClick
Definition: ui_nodes.h:148
bool LIST_IsEmpty(const linkedList_t *list)
Checks whether the given list is empty.
Definition: list.cpp:335
void UI_RegisterConFuncNode(uiBehaviour_t *behaviour)
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. ...
Definition: ui_actions.cpp:906
cvar_t * Cvar_FindVar(const char *varName)
Searches for a cvar given by parameter.
Definition: cvar.cpp:106
Atomic element to store UI scripts The parser use this atom to translate script action into many tree...
Definition: ui_actions.h:144
void onWindowOpened(uiNode_t *node, linkedList_t *params)
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
const struct value_s * UI_RegisterNodeMethod(uiBehaviour_t *behaviour, const char *name, uiNodeMethod_t function)
Register a node method to a behaviour.