UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_abstractoption.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "../ui_main.h"
27 #include "../ui_internal.h"
28 #include "../ui_parse.h"
29 #include "../ui_draw.h"
30 #include "../ui_data.h"
31 #include "../ui_lua.h"
32 
33 #include "ui_node_abstractoption.h"
34 #include "ui_node_abstractnode.h"
35 
36 #include "../../../common/scripts_lua.h"
37 
38 #define EXTRADATA_TYPE abstractOptionExtraData_t
39 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
40 
45 {
46  uiNode_t* option;
47  assert(UI_Node_IsOptionContainer(node));
48  UI_SortOptions(&node->firstChild);
49 
52  option = node->firstChild;
53  while (option->next)
54  option = option->next;
55  node->lastChild = option;
56 }
57 
59 {
60  /* no cvar given? */
61  if (!EXTRADATA(node).cvar || !*EXTRADATA(node).cvar) {
62  Com_Printf("UI_AbstractOptionGetCurrentValue: node [%s] doesn't have a valid cvar assigned\n", UI_GetPath(node));
63  return nullptr;
64  }
65 
66  /* not a cvar? */
67  if (!Q_strstart(EXTRADATA(node).cvar, "*cvar:")) {
68  Com_Printf("UI_AbstractOptionGetCurrentValue: in node [%s], the name [%s] is not a value cvar\n", UI_GetPath(node), EXTRADATA(node).cvar);
69  return nullptr;
70  }
71 
72  return UI_GetReferenceString(node, EXTRADATA(node).cvar);
73 }
74 
75 void UI_AbstractOption_SetCurrentValue(uiNode_t* node, const char* value)
76 {
77  const char* cvarName = &EXTRADATA(node).cvar[6];
78  Cvar_Set(cvarName, "%s", value);
79  if (node->onChange) {
80  UI_ExecuteEventActions(node, node->onChange);
81  }
82  if (node->lua_onChange != LUA_NOREF) {
84  }
85 }
86 
89  EXTRADATA(node).lua_onViewChange = LUA_NOREF;
90 }
91 
93 {
94  uiNode_t* option = node->firstChild;
95 
96  if (EXTRADATA(node).dataId == 0) {
97  int count = 0;
98  while (option && option->behaviour == ui_optionBehaviour) {
99  UI_Validate(option);
100  if (!option->invis)
101  count++;
102  option = option->next;
103  }
104 
105  EXTRADATA(node).count = count;
106  }
107 
108  node->invalidated = false;
109 }
110 
116 {
117  if (node->firstChild && node->firstChild->behaviour == ui_optionBehaviour) {
118  return node->firstChild;
119  } else {
120  const int v = UI_GetDataVersion(EXTRADATA(node).dataId);
121  if (v != EXTRADATA(node).versionId) {
122  int count = 0;
123  uiNode_t* option = UI_GetOption(EXTRADATA(node).dataId);
124  while (option) {
125  if (!option->invis)
126  count++;
127  option = option->next;
128  }
129  EXTRADATA(node).count = count;
130  EXTRADATA(node).versionId = v;
131  }
132  return UI_GetOption(EXTRADATA(node).dataId);
133  }
134 }
135 
142 {
143  return 1;
144 }
145 
152 {
153  return 1;
154 }
155 
157  return EXTRADATA(node).dataId;
158 }
159 
161  return EXTRADATA(node).count;
162 }
163 
164 const char* UI_AbstractOption_GetCvar (uiNode_t* node) {
165  return EXTRADATA(node).cvar;
166 }
167 
168 void UI_AbstractOption_SetDataId (uiNode_t* node, int id) {
169  EXTRADATA(node).dataId = id;
170 }
171 
173  EXTRADATA(node).dataId = UI_GetDataIDByName(name);
174 }
175 
176 void UI_AbstractOption_SetCvar (uiNode_t* node, const char* name) {
177  cvar_t* var = Cvar_Get(name);
178  EXTRADATA(node).cvar = var->name;
179 }
180 
182  uiSprite_t* sprite = UI_GetSpriteByName(name);
183  UI_EXTRADATA(node, abstractOptionExtraData_t).background = sprite;
184 }
185 
187  return EXTRADATA(node).scrollY.viewPos;
188 }
189 
191  EXTRADATA(node).scrollY.move(pos);
192 }
193 
195  EXTRADATA(node).scrollY.set(-1, size, -1);
196 }
197 
199  EXTRADATA(node).scrollY.set(-1, -1, size);
200 }
201 
203  return EXTRADATA(node).scrollY.viewSize;
204 }
205 
207  return EXTRADATA(node).scrollY.fullSize;
208 }
209 
210 
212 {
213  behaviour->name = "abstractoption";
214  behaviour->isAbstract = true;
215  behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
216  behaviour->drawItselfChild = true;
217  behaviour->manager = UINodePtr(new uiAbstractOptionNode());
218  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiAbstractOptionNode_t *");
219 
221  UI_RegisterExtradataNodeProperty(behaviour, "dataid", V_UI_DATAID, EXTRADATA_TYPE, dataId);
223  UI_RegisterExtradataNodeProperty(behaviour, "lineheight", V_INT, EXTRADATA_TYPE, lineHeight);
224 
225  /* position of the vertical view (into the full number of elements the node contain) */
226  UI_RegisterExtradataNodeProperty(behaviour, "viewpos", V_INT, EXTRADATA_TYPE, scrollY.viewPos);
227  /* size of the vertical view (proportional to the number of elements the node can display without moving) */
228  UI_RegisterExtradataNodeProperty(behaviour, "viewsize", V_INT, EXTRADATA_TYPE, scrollY.viewSize);
229  /* full vertical size (proportional to the number of elements the node contain) */
230  UI_RegisterExtradataNodeProperty(behaviour, "fullsize", V_INT, EXTRADATA_TYPE, scrollY.fullSize);
231 
232  /* number of elements contain the node */
234 
235  /* Define the cvar containing the value of the current selected option */
236  UI_RegisterExtradataNodeProperty(behaviour, "cvar", V_UI_CVAR, EXTRADATA_TYPE, cvar);
237 
238  /* Called when one of the properties viewpos/viewsize/fullsize change */
239  UI_RegisterExtradataNodeProperty(behaviour, "onviewchange", V_UI_ACTION, EXTRADATA_TYPE, onViewChange);
240 }
int getCellHeight(uiNode_t *node) override
Return size of the cell, which is the size (in virtual "pixel") which represents 1 in the scroll valu...
bool invalidated
Definition: ui_nodes.h:104
void UI_Validate(uiNode_t *node)
Validate a node tree.
Definition: ui_node.cpp:1066
int UI_AbstractOption_GetDataId(uiNode_t *node)
LUA_EVENT lua_onChange
Definition: ui_nodes.h:162
int getCellWidth(uiNode_t *node) override
Return size of the cell, which is the size (in virtual "pixel") which represents 1 in the scroll valu...
uiNode_t * next
Definition: ui_nodes.h:91
void UI_SortOptions(uiNode_t **first)
Sort options by alphabet.
Definition: ui_data.cpp:273
const char * name
Definition: ui_behaviour.h:41
struct uiAction_s * onChange
Definition: ui_nodes.h:143
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
int UI_AbstractOption_Scroll_ViewSize(uiNode_t *node)
bool UI_Node_IsOptionContainer(uiNode_t const *node)
Definition: ui_node.cpp:54
const char * UI_AbstractOption_GetCurrentValue(uiNode_t *node)
uiBehaviour_t * behaviour
Definition: ui_nodes.h:83
UINodePtr manager
Definition: ui_behaviour.h:43
bool UI_ExecuteLuaEventScript(uiNode_t *node, LUA_EVENT event)
Executes a lua event handler.
Definition: ui_lua.cpp:71
#define EXTRADATA_TYPE
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
uiNode_t * UI_GetOption(int dataId)
Definition: ui_data.cpp:324
void UI_RegisterAbstractOptionNode(uiBehaviour_t *behaviour)
void UI_AbstractOption_SortOptions(uiNode_t *node)
Sort options by alphabet.
int UI_GetDataVersion(int textId)
Definition: ui_data.cpp:159
void initNode(uiNode_t *node) override
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
int UI_GetDataIDByName(const char *name)
Return a dataId by name.
Definition: ui_data.cpp:102
bool invis
Definition: ui_nodes.h:101
GLuint * id
Definition: r_gl.h:149
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
Definition: cvar.cpp:342
GLsizei size
Definition: r_gl.h:152
#define V_UI_CVAR
Definition: ui_parse.h:59
uiNode_t * lastChild
Definition: ui_nodes.h:90
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
char const * Q_strstart(char const *str, char const *start)
Matches the start of a string.
Definition: shared.cpp:587
#define UI_EXTRADATA(NODE, TYPE)
Definition: ui_nodes.h:185
bool drawItselfChild
Definition: ui_behaviour.h:50
void UI_ExecuteEventActions(uiNode_t *source, const uiAction_t *firstAction)
Definition: ui_actions.cpp:726
void UI_AbstractOption_SetCurrentValue(uiNode_t *node, const char *value)
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
Definition: ui_parse.cpp:1406
void UI_AbstractOption_Scroll_SetFullSize(uiNode_t *node, int size)
void UI_AbstractOption_SetBackgroundByName(uiNode_t *node, const char *name)
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
#define EXTRADATA(node)
void UI_AbstractOption_Scroll_SetCurrent(uiNode_t *node, int pos)
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 UI_AbstractOption_Scroll_SetViewSize(uiNode_t *node, int size)
QGL_EXTERN GLuint count
Definition: r_gl.h:99
void doLayout(uiNode_t *node) override
Call to update the node layout. This common code revalidates the node tree.
int UI_AbstractOption_Scroll_Current(uiNode_t *node)
virtual void initNode(uiNode_t *node)
int UI_AbstractOption_GetCount(uiNode_t *node)
intptr_t extraDataSize
Definition: ui_behaviour.h:54
node behaviour, how a node work
Definition: ui_behaviour.h:39
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
#define V_UI_DATAID
Definition: ui_parse.h:58
Definition: scripts.h:52
void UI_AbstractOption_SetDataIdByName(uiNode_t *node, const char *name)
const char * UI_AbstractOption_GetCvar(uiNode_t *node)
int UI_AbstractOption_Scroll_FullSize(uiNode_t *node)
uiNode_t * firstChild
Definition: ui_nodes.h:89
void UI_AbstractOption_SetDataId(uiNode_t *node, int id)
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
Definition: cvar.cpp:615
#define V_UI_ACTION
Definition: ui_parse.h:54
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
Definition: ui_sprite.cpp:115
const uiBehaviour_t * ui_optionBehaviour
QGL_EXTERN int GLboolean GLfloat * v
Definition: r_gl.h:120
char * name
Definition: cvar.h:72
uiNode_t * UI_AbstractOption_GetFirstOption(uiNode_t *node)
Return the first option of the node.
void UI_AbstractOption_SetCvar(uiNode_t *node, const char *name)