UFO: Alien Invasion
Doxygen documentation generating
ui_node_radiobutton.cpp
Go to the documentation of this file.
1 
17 /*
18 Copyright (C) 2002-2023 UFO: Alien Invasion.
19 
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License
22 as published by the Free Software Foundation; either version 2
23 of the License, or (at your option) any later version.
24 
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 
29 See the GNU General Public License for more details.
30 
31 You should have received a copy of the GNU General Public License
32 along with this program; if not, write to the Free Software
33 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 
35 */
36 
37 #include "../ui_main.h"
38 #include "../ui_actions.h"
39 #include "../ui_sprite.h"
40 #include "../ui_parse.h"
41 #include "../ui_behaviour.h"
42 #include "../ui_input.h"
43 #include "../ui_render.h"
44 #include "../ui_lua.h"
45 
46 #include "ui_node_radiobutton.h"
47 #include "ui_node_abstractnode.h"
48 
49 #include "../../../common/scripts_lua.h"
50 
51 #define EXTRADATA_TYPE radioButtonExtraData_t
52 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
53 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
54 
55 #define EPSILON 0.001f
56 
58 #define UI_4STATUS_TEX_HEIGHT 64
59 
61 {
62  if (EXTRADATA(node).string == nullptr) {
63  const float current = UI_GetReferenceFloat(node, EXTRADATA(node).cvar);
64  return current > EXTRADATA(node).value - EPSILON && current < EXTRADATA(node).value + EPSILON;
65  } else {
66  const char* current = UI_GetReferenceString(node, EXTRADATA(node).cvar);
67  return Q_streq(current, EXTRADATA(node).string);
68  }
69 }
70 
76 {
77  vec2_t pos;
78  uiSpriteStatus_t iconStatus;
79  const bool disabled = node->disabled || node->parent->disabled;
80  int texY;
81  const char* image;
82  const bool isSelected = UI_RadioButtonNodeIsSelected(node);
83 
84  if (disabled) {
85  iconStatus = SPRITE_STATUS_DISABLED;
86  texY = UI_4STATUS_TEX_HEIGHT * 2;
87  } else if (isSelected) {
88  iconStatus = SPRITE_STATUS_CLICKED;
89  texY = UI_4STATUS_TEX_HEIGHT * 3;
90  } else if (node->state) {
91  iconStatus = SPRITE_STATUS_HOVER;
92  texY = UI_4STATUS_TEX_HEIGHT;
93  } else {
94  iconStatus = SPRITE_STATUS_NORMAL;
95  texY = 0;
96  }
97 
98  UI_GetNodeAbsPos(node, pos);
99 
100  image = UI_GetReferenceString(node, node->image);
101  if (image) {
102  const int texX = 0;
103  UI_DrawNormImageByName(false, pos[0], pos[1], node->box.size[0], node->box.size[1],
104  texX + node->box.size[0], texY + node->box.size[1], texX, texY, image);
105  }
106 
107  if (EXTRADATA(node).background) {
108  UI_DrawSpriteInBox(false, EXTRADATA(node).background, iconStatus, pos[0], pos[1], node->box.size[0], node->box.size[1]);
109  }
110 
111  if (EXTRADATA(node).icon) {
112  UI_DrawSpriteInBox(EXTRADATA(node).flipIcon, EXTRADATA(node).icon, iconStatus, pos[0], pos[1], node->box.size[0], node->box.size[1]);
113  }
114 }
115 
120 {
121  /* no cvar given? */
122  if (!EXTRADATA(node).cvar || !*(char*)(EXTRADATA(node).cvar)) {
123  return;
124  }
125 
126  /* its not a cvar! */
128  char const* const cvarName = Q_strstart((char const*)(EXTRADATA(node).cvar), "*cvar:");
129  if (!cvarName)
130  return;
131 
132  UI_GetReferenceFloat(node, EXTRADATA(node).cvar);
133  /* Is we click on the already selected button, we can continue */
135  return;
136 
137  if (EXTRADATA(node).string == nullptr) {
138  Cvar_SetValue(cvarName, EXTRADATA(node).value);
139  } else {
140  Cvar_Set(cvarName, "%s", EXTRADATA(node).string);
141  }
142  if (node->onChange) {
143  UI_ExecuteEventActions(node, node->onChange);
144  }
145  if (node->lua_onChange != LUA_NOREF) {
147  }
148 }
149 
153 void uiRadioButtonNode::onLeftClick (uiNode_t* node, int x, int y)
154 {
155  if (node->onClick) {
156  UI_ExecuteEventActions(node, node->onClick);
157  }
158  if (node->lua_onClick != LUA_NOREF) {
159  UI_ExecuteLuaEventScript_XY(node, node->lua_onClick, x, y);
160  }
161 
162  onActivate(node);
163 }
164 
165 
166 const char* UI_RadioButton_GetCvar (uiNode_t* node) {
167  return EXTRADATA(node).cvar;
168 }
169 
170 void UI_RadioButton_SetCvar (uiNode_t* node, const char* name) {
171  cvar_t* var = Cvar_Get(name);
172  EXTRADATA(node).cvar = var->name;
173 }
174 
175 void UI_RadioButton_SetValue (uiNode_t* node, const char* value) {
176 
177  /* This is a special case: we have a situation where the node already has a value reference
178  (either being float or cvar). We now want to replace this value reference by a new cvar. So we first
179  need to free the existing reference, then create new cvar reference (just a string starting with
180  '*cvar' and store it. */
181  Mem_Free(*(void**)(EXTRADATA(node).string));
182  *(void**)EXTRADATA(node).string= Mem_StrDup(value);
183  uiRadioButtonNode* b=static_cast<uiRadioButtonNode*>(node->behaviour->manager.get());
184  b->onActivate(node);
185 }
186 
187 void UI_RadioButton_SetValue (uiNode_t* node, float value) {
188  EXTRADATA(node).value = value;
189  uiRadioButtonNode* b=static_cast<uiRadioButtonNode*>(node->behaviour->manager.get());
190  b->onActivate(node);
191 }
192 
195  EXTRADATA(node).background = sprite;
196 }
197 
198 void UI_RadioButton_SetIconByName (uiNode_t* node, const char* name) {
200  EXTRADATA(node).icon = sprite;}
201 
203 {
204  behaviour->name = "radiobutton";
205  behaviour->manager = UINodePtr(new uiRadioButtonNode());
206  behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
207  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiRadioButtonNode_t *");
208 
209  /* Numerical value defining the radiobutton. Cvar is updated with this value when the radio button is selected. */
210  UI_RegisterExtradataNodeProperty(behaviour, "value", V_FLOAT, EXTRADATA_TYPE, value);
211  /* String Value defining the radiobutton. Cvar is updated with this value when the radio button is selected. */
212  UI_RegisterExtradataNodeProperty(behaviour, "stringValue", V_CVAR_OR_STRING, EXTRADATA_TYPE, string);
213 
214  /* Cvar name shared with the radio button group to identify when a radio button is selected. */
215  UI_RegisterExtradataNodeProperty(behaviour, "cvar", V_UI_CVAR, EXTRADATA_TYPE, cvar);
216  /* Icon used to display the node */
218  UI_RegisterExtradataNodeProperty(behaviour, "flipicon", V_BOOL, EXTRADATA_TYPE, flipIcon);
219  /* Sprite used to display the background */
220  UI_RegisterExtradataNodeProperty(behaviour, "background", V_UI_SPRITEREF, EXTRADATA_TYPE, background);
221 }
bool disabled
Definition: ui_nodes.h:102
char * name
Definition: cvar.h:72
void UI_RegisterRadioButtonNode(uiBehaviour_t *behaviour)
#define EXTRADATA_TYPE
struct uiAction_s * onChange
Definition: ui_nodes.h:143
#define EPSILON
uiBehaviour_t * behaviour
Definition: ui_nodes.h:83
const char * UI_RadioButton_GetCvar(uiNode_t *node)
uiBox_t box
Definition: ui_nodes.h:96
struct uiAction_s * onClick
Definition: ui_nodes.h:135
#define UI_4STATUS_TEX_HEIGHT
bool UI_ExecuteLuaEventScript(uiNode_t *node, LUA_EVENT event)
Executes a lua event handler.
Definition: ui_lua.cpp:71
#define V_UI_SPRITEREF
Definition: ui_parse.h:56
void UI_DrawSpriteInBox(bool flip, const uiSprite_t *sprite, uiSpriteStatus_t status, int posX, int posY, int sizeX, int sizeY)
Definition: ui_sprite.cpp:187
void UI_RadioButton_SetBackgroundByName(uiNode_t *node, const char *name)
#define Mem_StrDup(in)
Definition: mem.h:48
intptr_t extraDataSize
Definition: ui_behaviour.h:54
UINodePtr manager
Definition: ui_behaviour.h:43
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
PointerType get() const
Definition: sharedptr.h:197
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
char * image
Definition: ui_nodes.h:123
bool state
Definition: ui_nodes.h:106
void draw(uiNode_t *node) override
Handles RadioButton draw.
void onActivate(uiNode_t *node) override
Activate the node. Can be used without the mouse (ie. a button will execute onClick) ...
const image_t * UI_DrawNormImageByName(bool flip, float x, float y, float w, float h, float sh, float th, float sl, float tl, const char *name)
Draws an image or parts of it.
Definition: ui_render.cpp:203
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
#define V_UI_CVAR
Definition: ui_parse.h:59
void UI_RadioButton_SetValue(uiNode_t *node, const char *value)
static bool UI_RadioButtonNodeIsSelected(uiNode_t *node)
SharedPtr< uiNode > UINodePtr
char const * Q_strstart(char const *str, char const *start)
Matches the start of a string.
Definition: shared.cpp:587
uiNode_t * parent
Definition: ui_nodes.h:92
void UI_ExecuteEventActions(uiNode_t *source, const uiAction_t *firstAction)
Definition: ui_actions.cpp:726
bool UI_ExecuteLuaEventScript_XY(uiNode_t *node, LUA_EVENT event, int x, int y)
Executes a lua event handler with (x,y) argument.
Definition: ui_lua.cpp:143
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
Definition: ui_parse.cpp:1406
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_GetNodeAbsPos(const uiNode_t *node, vec2_t pos)
Returns the absolute position of a node.
Definition: ui_node.cpp:514
float UI_GetReferenceFloat(const uiNode_t *const node, const void *ref)
Returns the value of the reference variable.
Definition: ui_parse.cpp:1434
void onLeftClick(uiNode_t *node, int x, int y) override
Handles radio button clicks.
void UI_RadioButton_SetCvar(uiNode_t *node, const char *name)
void UI_RadioButton_SetIconByName(uiNode_t *node, const char *name)
Definition: scripts.h:50
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
LUA_EVENT lua_onClick
Definition: ui_nodes.h:148
uiSpriteStatus_t
Definition: ui_sprite.h:32
#define Mem_Free(ptr)
Definition: mem.h:35
vec_t vec2_t[2]
Definition: ufotypes.h:38
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
#define V_CVAR_OR_STRING
Definition: ui_parse.h:69
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
Definition: cvar.cpp:615
#define Q_streq(a, b)
Definition: shared.h:136
const char * name
Definition: ui_behaviour.h:41
#define EXTRADATA(node)
node behaviour, how a node work
Definition: ui_behaviour.h:39
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
Definition: ui_sprite.cpp:115
vec2_t size
Definition: ui_nodes.h:52
void Cvar_SetValue(const char *varName, float value)
Expands value to a string and calls Cvar_Set.
Definition: cvar.cpp:671
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
LUA_EVENT lua_onChange
Definition: ui_nodes.h:162