UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_option.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 "../ui_main.h"
26 #include "../ui_parse.h"
27 #include "../ui_behaviour.h"
28 #include "../ui_sprite.h"
29 #include "ui_node_abstractnode.h"
30 #include "ui_node_option.h"
31 
32 #include "../../client.h" /* gettext _() */
33 
34 #include "../../../common/scripts_lua.h"
35 
40 
41 #define EXTRADATA_TYPE optionExtraData_t
42 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
43 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
44 
45 static const value_t* propertyCollapsed;
46 
47 
54 {
55  int count = 0;
56  while (option) {
57  int localCount = 0;
58  assert(option->behaviour == ui_optionBehaviour);
59  if (option->invis) {
60  option = option->next;
61  continue;
62  }
63  if (OPTIONEXTRADATA(option).collapsed) {
64  OPTIONEXTRADATA(option).childCount = 0;
65  option = option->next;
66  count++;
67  continue;
68  }
69  if (option->firstChild)
70  localCount = UI_OptionUpdateCache(option->firstChild);
71  OPTIONEXTRADATA(option).childCount = localCount;
72  count += 1 + localCount;
73  option = option->next;
74  }
75  return count;
76 }
77 
79 {
80  uiNode_t* child = node->firstChild;
81  int count = 0;
82 
83  while (child && child->behaviour == ui_optionBehaviour) {
84  UI_Validate(child);
85  if (!child->invis) {
86  if (EXTRADATA(child).collapsed)
87  count += 1 + EXTRADATA(child).childCount;
88  else
89  count += 1;
90  }
91  child = child->next;
92  }
93  EXTRADATA(node).childCount = count;
94  node->invalidated = false;
95 }
96 
97 void uiOptionNode::onPropertyChanged (uiNode_t* node, const value_t* property)
98 {
99  if (property == propertyCollapsed) {
100  UI_Invalidate(node);
101  return;
102  }
103  uiLocatedNode::onPropertyChanged(node, property);
104 }
105 
112 static void UI_InitOption (uiNode_t* option, const char* label, const char* value)
113 {
114  assert(option);
115  assert(option->behaviour == ui_optionBehaviour);
116  Q_strncpyz(OPTIONEXTRADATA(option).label, label, sizeof(OPTIONEXTRADATA(option).label));
117  Q_strncpyz(OPTIONEXTRADATA(option).value, value, sizeof(OPTIONEXTRADATA(option).value));
118 }
119 
126 uiNode_t* UI_AllocOptionNode (const char* name, const char* label, const char* value)
127 {
128  uiNode_t* option;
129  option = UI_AllocNode(name, "option", true);
130  UI_InitOption(option, label, value);
131  return option;
132 }
133 
134 void UI_Option_SetLabel (uiNode_t* node, const char* text) {
135  Q_strncpyz(OPTIONEXTRADATA(node).label, text, sizeof(OPTIONEXTRADATA(node).label));
136 }
137 
138 void UI_Option_SetValue (uiNode_t* node, const char* text) {
139  Q_strncpyz(OPTIONEXTRADATA(node).value, text, sizeof(OPTIONEXTRADATA(node).value));
140 }
141 
142 void UI_Option_SetIconByName (uiNode_t* node, const char* name) {
143  uiSprite_t* sprite = UI_GetSpriteByName(name);
144  OPTIONEXTRADATA(node).icon = sprite;
145 }
146 
148 {
149  behaviour->name = "option";
150  behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
151  behaviour->manager = UINodePtr(new uiOptionNode());
152  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiOptionNode_t *");
153 
157  UI_RegisterExtradataNodeProperty(behaviour, "label", V_STRING, EXTRADATA_TYPE, label);
158 
162  UI_RegisterExtradataNodeProperty(behaviour, "value", V_STRING, EXTRADATA_TYPE, value);
163 
167  propertyCollapsed = UI_RegisterExtradataNodeProperty(behaviour, "collapsed", V_BOOL, EXTRADATA_TYPE, collapsed);
168 
169  /* Icon used to display the node
170  */
172  UI_RegisterExtradataNodeProperty(behaviour, "flipicon", V_BOOL, EXTRADATA_TYPE, flipIcon);
173 
174  ui_optionBehaviour = behaviour;
175 }
bool invalidated
Definition: ui_nodes.h:104
void UI_Validate(uiNode_t *node)
Validate a node tree.
Definition: ui_node.cpp:1066
uiNode_t * next
Definition: ui_nodes.h:91
static void UI_InitOption(uiNode_t *option, const char *label, const char *value)
Initializes an option with a very little set of values.
const char * name
Definition: ui_behaviour.h:41
void UI_Invalidate(uiNode_t *node)
Invalidate a node and all his parent to request a layout update.
Definition: ui_node.cpp:1053
uiBehaviour_t * behaviour
Definition: ui_nodes.h:83
UINodePtr manager
Definition: ui_behaviour.h:43
#define V_UI_SPRITEREF
Definition: ui_parse.h:56
void UI_Option_SetIconByName(uiNode_t *node, const char *name)
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
uiNode_t * UI_AllocNode(const char *name, const char *type, bool isDynamic)
Allocate a node into the UI memory.
Definition: ui_nodes.cpp:381
bool invis
Definition: ui_nodes.h:101
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
uiNode_t * UI_AllocOptionNode(const char *name, const char *label, const char *value)
Initializes an option with a very little set of values.
#define OPTIONEXTRADATA(node)
void onPropertyChanged(uiNode_t *node, const value_t *property) override
SharedPtr< uiNode > UINodePtr
void UI_Option_SetLabel(uiNode_t *node, const char *text)
#define EXTRADATA(node)
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
int UI_OptionUpdateCache(uiNode_t *option)
update option cache about child, according to collapse and visible status
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
void * UI_SWIG_TypeQuery(const char *name)
This function queries the SWIG type table for a type information structure. It is used in combination...
QGL_EXTERN GLuint count
Definition: r_gl.h:99
void UI_RegisterOptionNode(uiBehaviour_t *behaviour)
intptr_t extraDataSize
Definition: ui_behaviour.h:54
Definition: scripts.h:50
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
void UI_Option_SetValue(uiNode_t *node, const char *text)
#define EXTRADATA_TYPE
virtual void onPropertyChanged(uiNode_t *node, const value_t *property)
uiNode_t * firstChild
Definition: ui_nodes.h:89
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
Definition: ui_sprite.cpp:115
const uiBehaviour_t * ui_optionBehaviour
static const value_t * propertyCollapsed
void doLayout(uiNode_t *node) override
Call to update the node layout. This common code revalidates the node tree.