UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_radiobutton.cpp
Go to the documentation of this file.
1 
17 /*
18 Copyright (C) 2002-2020 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  Com_Printf("UI_RadioButtonNodeClick: node '%s' doesn't have a valid cvar assigned\n", UI_GetPath(node));
124  return;
125  }
126 
127  /* its not a cvar! */
129  char const* const cvarName = Q_strstart((char const*)(EXTRADATA(node).cvar), "*cvar:");
130  if (!cvarName)
131  return;
132 
133  UI_GetReferenceFloat(node, EXTRADATA(node).cvar);
134  /* Is we click on the already selected button, we can continue */
136  return;
137 
138  if (EXTRADATA(node).string == nullptr) {
139  Cvar_SetValue(cvarName, EXTRADATA(node).value);
140  } else {
141  Cvar_Set(cvarName, "%s", EXTRADATA(node).string);
142  }
143  if (node->onChange) {
144  UI_ExecuteEventActions(node, node->onChange);
145  }
146  if (node->lua_onChange != LUA_NOREF) {
148  }
149 }
150 
154 void uiRadioButtonNode::onLeftClick (uiNode_t* node, int x, int y)
155 {
156  if (node->onClick) {
157  UI_ExecuteEventActions(node, node->onClick);
158  }
159  if (node->lua_onClick != LUA_NOREF) {
160  UI_ExecuteLuaEventScript_XY(node, node->lua_onClick, x, y);
161  }
162 
163  onActivate(node);
164 }
165 
166 void UI_RadioButton_SetValue (uiNode_t* node, const char* value) {
167 
168  /* This is a special case: we have a situation where the node already has a value reference
169  (either being float or cvar). We now want to replace this value reference by a new cvar. So we first
170  need to free the existing reference, then create new cvar reference (just a string starting with
171  '*cvar' and store it. */
172  Mem_Free(*(void**)(EXTRADATA(node).string));
173  *(void**)EXTRADATA(node).string= Mem_StrDup(value);
174  uiRadioButtonNode* b=static_cast<uiRadioButtonNode*>(node->behaviour->manager.get());
175  b->onActivate(node);
176 }
177 
178 void UI_RadioButton_SetValue (uiNode_t* node, float value) {
179  EXTRADATA(node).value = value;
180  uiRadioButtonNode* b=static_cast<uiRadioButtonNode*>(node->behaviour->manager.get());
181  b->onActivate(node);
182 }
183 
185  uiSprite_t* sprite = UI_GetSpriteByName(name);
186  EXTRADATA(node).background = sprite;
187 }
188 
189 void UI_RadioButton_SetIconByName (uiNode_t* node, const char* name) {
190  uiSprite_t* sprite = UI_GetSpriteByName(name);
191  EXTRADATA(node).icon = sprite;}
192 
194 {
195  behaviour->name = "radiobutton";
196  behaviour->manager = UINodePtr(new uiRadioButtonNode());
197  behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
198  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiRadioButtonNode_t *");
199 
200  /* Numerical value defining the radiobutton. Cvar is updated with this value when the radio button is selected. */
201  UI_RegisterExtradataNodeProperty(behaviour, "value", V_FLOAT, EXTRADATA_TYPE, value);
202  /* String Value defining the radiobutton. Cvar is updated with this value when the radio button is selected. */
203  UI_RegisterExtradataNodeProperty(behaviour, "stringValue", V_CVAR_OR_STRING, EXTRADATA_TYPE, string);
204 
205  /* Cvar name shared with the radio button group to identify when a radio button is selected. */
206  UI_RegisterExtradataNodeProperty(behaviour, "cvar", V_UI_CVAR, EXTRADATA_TYPE, cvar);
207  /* Icon used to display the node */
209  UI_RegisterExtradataNodeProperty(behaviour, "flipicon", V_BOOL, EXTRADATA_TYPE, flipIcon);
210  /* Sprite used to display the background */
211  UI_RegisterExtradataNodeProperty(behaviour, "background", V_UI_SPRITEREF, EXTRADATA_TYPE, background);
212 }
void UI_RegisterRadioButtonNode(uiBehaviour_t *behaviour)
vec2_t size
Definition: ui_nodes.h:52
#define EXTRADATA_TYPE
uiNode_t * parent
Definition: ui_nodes.h:92
LUA_EVENT lua_onChange
Definition: ui_nodes.h:162
#define EPSILON
const char * name
Definition: ui_behaviour.h:41
struct uiAction_s * onChange
Definition: ui_nodes.h:143
#define UI_4STATUS_TEX_HEIGHT
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 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 Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
void UI_RadioButton_SetBackgroundByName(uiNode_t *node, const char *name)
#define Mem_StrDup(in)
Definition: mem.h:48
struct uiAction_s * onClick
Definition: ui_nodes.h:135
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
void onActivate(uiNode_t *node) override
Activate the node. Can be used without the mouse (ie. a button will execute onClick) ...
bool state
Definition: ui_nodes.h:106
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
#define V_UI_CVAR
Definition: ui_parse.h:59
void UI_RadioButton_SetValue(uiNode_t *node, const char *value)
void draw(uiNode_t *node) override
Handles RadioButton draw.
static bool UI_RadioButtonNodeIsSelected(uiNode_t *node)
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
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
PointerType get() const
Definition: sharedptr.h:197
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...
bool disabled
Definition: ui_nodes.h:102
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 UI_RadioButton_SetIconByName(uiNode_t *node, const char *name)
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
uiSpriteStatus_t
Definition: ui_sprite.h:32
#define Mem_Free(ptr)
Definition: mem.h:35
vec_t vec2_t[2]
Definition: ufotypes.h:38
char * image
Definition: ui_nodes.h:123
#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
LUA_EVENT lua_onClick
Definition: ui_nodes.h:148
#define Q_streq(a, b)
Definition: shared.h:136
uiBox_t box
Definition: ui_nodes.h:96
#define EXTRADATA(node)
void onLeftClick(uiNode_t *node, int x, int y) override
Handles radio button clicks.
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
Definition: ui_sprite.cpp:115
void Cvar_SetValue(const char *varName, float value)
Expands value to a string and calls Cvar_Set.
Definition: cvar.cpp:671