UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_radiobutton.cpp
Go to the documentation of this file.
1
16
17/*
18Copyright (C) 2002-2025 UFO: Alien Invasion.
19
20This program is free software; you can redistribute it and/or
21modify it under the terms of the GNU General Public License
22as published by the Free Software Foundation; either version 2
23of the License, or (at your option) any later version.
24
25This program is distributed in the hope that it will be useful,
26but WITHOUT ANY WARRANTY; without even the implied warranty of
27MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29See the GNU General Public License for more details.
30
31You should have received a copy of the GNU General Public License
32along with this program; if not, write to the Free Software
33Foundation, 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"
48
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;
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
153void 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
166const char* UI_RadioButton_GetCvar (uiNode_t* node) {
167 return EXTRADATA(node).cvar;
168}
169
170void UI_RadioButton_SetCvar (uiNode_t* node, const char* name) {
171 cvar_t* var = Cvar_Get(name);
172 EXTRADATA(node).cvar = var->name;
173}
174
175void 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
187void 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
198void 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. */
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}
PointerType get() const
Definition sharedptr.h:197
void onActivate(uiNode_t *node) override
Activate the node. Can be used without the mouse (ie. a button will execute onClick).
void onLeftClick(uiNode_t *node, int x, int y) override
Handles radio button clicks.
void draw(uiNode_t *node) override
Handles RadioButton draw.
void Cvar_SetValue(const char *varName, float value)
Expands value to a string and calls Cvar_Set.
Definition cvar.cpp:671
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
Definition cvar.cpp:615
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 Mem_Free(ptr)
Definition mem.h:35
#define Mem_StrDup(in)
Definition mem.h:48
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
@ V_BOOL
Definition scripts.h:50
@ V_FLOAT
Definition scripts.h:54
Header for lua script functions.
#define Q_streq(a, b)
Definition shared.h:136
char const * Q_strstart(char const *str, char const *start)
Matches the start of a string.
Definition shared.cpp:587
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition cvar.h:71
char * name
Definition cvar.h:72
node behaviour, how a node work
const char * name
void * lua_SWIG_typeinfo
UINodePtr manager
intptr_t extraDataSize
vec2_t size
Definition ui_nodes.h:52
Atomic structure used to define most of the UI.
Definition ui_nodes.h:80
bool disabled
Definition ui_nodes.h:102
uiBehaviour_t * behaviour
Definition ui_nodes.h:83
LUA_EVENT lua_onChange
Definition ui_nodes.h:162
uiNode_t * parent
Definition ui_nodes.h:92
struct uiAction_s * onChange
Definition ui_nodes.h:143
LUA_EVENT lua_onClick
Definition ui_nodes.h:148
uiBox_t box
Definition ui_nodes.h:96
struct uiAction_s * onClick
Definition ui_nodes.h:135
char * image
Definition ui_nodes.h:123
bool state
Definition ui_nodes.h:106
vec_t vec2_t[2]
Definition ufotypes.h:38
void UI_ExecuteEventActions(uiNode_t *source, const uiAction_t *firstAction)
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
bool UI_ExecuteLuaEventScript(uiNode_t *node, LUA_EVENT event)
Executes a lua event handler.
Definition ui_lua.cpp:71
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
Basic lua initialization for the ui.
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:526
SharedPtr< uiNode > UINodePtr
#define EXTRADATA_TYPE
#define EXTRADATA(node)
#define EPSILON
static bool UI_RadioButtonNodeIsSelected(uiNode_t *node)
const char * UI_RadioButton_GetCvar(uiNode_t *node)
void UI_RegisterRadioButtonNode(uiBehaviour_t *behaviour)
void UI_RadioButton_SetCvar(uiNode_t *node, const char *name)
#define EXTRADATA(node)
void UI_RadioButton_SetBackgroundByName(uiNode_t *node, const char *name)
void UI_RadioButton_SetValue(uiNode_t *node, const char *value)
#define UI_4STATUS_TEX_HEIGHT
void UI_RadioButton_SetIconByName(uiNode_t *node, const char *name)
float UI_GetReferenceFloat(const uiNode_t *const node, const void *ref)
Returns the value of the reference variable.
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
#define V_UI_SPRITEREF
Definition ui_parse.h:56
#define V_UI_CVAR
Definition ui_parse.h:59
#define V_CVAR_OR_STRING
Definition ui_parse.h:69
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.
void UI_DrawSpriteInBox(bool flip, const uiSprite_t *sprite, uiSpriteStatus_t status, int posX, int posY, int sizeX, int sizeY)
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
uiSpriteStatus_t
Definition ui_sprite.h:32
@ SPRITE_STATUS_DISABLED
Definition ui_sprite.h:35
@ SPRITE_STATUS_HOVER
Definition ui_sprite.h:34
@ SPRITE_STATUS_CLICKED
Definition ui_sprite.h:36
@ SPRITE_STATUS_NORMAL
Definition ui_sprite.h:33