UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_checkbox.cpp
Go to the documentation of this file.
1
13
14/*
15Copyright (C) 2002-2025 UFO: Alien Invasion.
16
17This program is free software; you can redistribute it and/or
18modify it under the terms of the GNU General Public License
19as published by the Free Software Foundation; either version 2
20of the License, or (at your option) any later version.
21
22This program is distributed in the hope that it will be useful,
23but WITHOUT ANY WARRANTY; without even the implied warranty of
24MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25
26See the GNU General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with this program; if not, write to the Free Software
30Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31
32*/
33
34#include "../ui_nodes.h"
35#include "../ui_parse.h"
36#include "../ui_behaviour.h"
37#include "../ui_main.h"
38#include "../ui_actions.h"
39#include "../ui_render.h"
40#include "../ui_sound.h"
41#include "../ui_sprite.h"
42#include "../ui_lua.h"
43
44#include "ui_node_checkbox.h"
47
49
50#define EXTRADATA_TYPE checkboxExtraData_t
51#define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
52
54{
55 const float value = getValue(node);
56 vec2_t pos;
57 uiSprite_t* icon = nullptr;
59
60 /* outer status */
61 if (node->disabled) {
63 } else if (node->state) {
64 status = SPRITE_STATUS_HOVER;
65 } else {
66 status = SPRITE_STATUS_NORMAL;
67 }
68
69 /* inner status */
70 if (value == 0) {
71 icon = EXTRADATA(node).iconUnchecked;
72 } else if (value > 0) {
73 icon = EXTRADATA(node).iconChecked;
74 } else { /* value < 0 */
75 icon = EXTRADATA(node).iconUnknown;
76 }
77
78 UI_GetNodeAbsPos(node, pos);
79
80 if (EXTRADATA(node).background) {
81 UI_DrawSpriteInBox(false, EXTRADATA(node).background, status, pos[0], pos[1], node->box.size[0], node->box.size[1]);
82 }
83 if (icon) {
84 UI_DrawSpriteInBox(false, icon, status, pos[0], pos[1], node->box.size[0], node->box.size[1]);
85 }
86}
87
92{
93 toggle(node);
94}
95
97 if (node->disabled)
98 return;
99
100 /* toggle value */
101 const float last = getValue(node);
102 float value = (last > 0) ? 0 : 1;
103
104 /* save result */
105 setValue(node, value);
106}
107
108static void UI_CheckBoxNodeCallActivate (uiNode_t* node, const uiCallContext_t* context)
109{
110 UI_Node_Activate(node);
111}
112
116void uiCheckBoxNode::onLeftClick (uiNode_t* node, int x, int y)
117{
118 if (node->disabled)
119 return;
120
121 onActivate(node);
122 if (node->onClick) {
123 UI_ExecuteEventActions(node, node->onClick);
124 }
125 if (node->lua_onClick != LUA_NOREF) {
126 UI_ExecuteLuaEventScript_XY(node, node->lua_onClick, x, y);
127 }
128 UI_PlaySound("click1");
129}
130
135{
137 setRange(node, -1, 1);
138}
139
142 UI_EXTRADATA(node, checkboxExtraData_t).background = sprite;
143}
144
147 UI_EXTRADATA(node, checkboxExtraData_t).iconChecked = sprite;
148}
149
152 UI_EXTRADATA(node, checkboxExtraData_t).iconUnchecked = sprite;
153}
154
156 uiCheckBoxNode* b=static_cast<uiCheckBoxNode*>(node->behaviour->manager.get());
157 b->toggle(node);
158}
159
162 UI_EXTRADATA(node, checkboxExtraData_t).iconUnknown = sprite;
163}
164
166 uiCheckBoxNode* b=static_cast<uiCheckBoxNode*>(node->behaviour->manager.get());
167 return ((int)(b->getValue(node)) != 0);
168}
169
171 uiCheckBoxNode* b=static_cast<uiCheckBoxNode*>(node->behaviour->manager.get());
172 return (int)(b->getValue(node) != 0 ? 1 : 0);
173}
174
176{
177 behaviour->name = "checkbox";
178 behaviour->extends = "abstractvalue";
179 behaviour->manager = UINodePtr(new uiCheckBoxNode());
180 behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
181 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiCheckBoxNode_t *");
182
184 UI_RegisterExtradataNodeProperty(behaviour, "iconChecked", V_UI_SPRITEREF, EXTRADATA_TYPE, iconChecked);
186 UI_RegisterExtradataNodeProperty(behaviour, "iconUnchecked", V_UI_SPRITEREF, EXTRADATA_TYPE, iconUnchecked);
188 UI_RegisterExtradataNodeProperty(behaviour, "iconIndeterminate", V_UI_SPRITEREF, EXTRADATA_TYPE, iconUnknown);
190 UI_RegisterExtradataNodeProperty(behaviour, "background", V_UI_SPRITEREF, EXTRADATA_TYPE, background);
191
192 /* Call it to toggle the node status. */
194}
PointerType get() const
Definition sharedptr.h:197
float getValue(uiNode_t const *node)
void onLoading(uiNode_t *node) override
bool setValue(uiNode_t *node, float value)
void setRange(uiNode_t *node, float min, float max)
void onActivate(uiNode_t *node) override
Activate the node. Can be used without the mouse (ie. a button will execute onClick).
void toggle(uiNode_t *node)
void onLeftClick(uiNode_t *node, int x, int y) override
Handles checkboxes clicks.
void draw(uiNode_t *node) override
void onLoading(uiNode_t *node) override
Handled before the begin of the load of the node from the script.
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
Header for lua script functions.
node behaviour, how a node work
const char * name
void * lua_SWIG_typeinfo
UINodePtr manager
const char * extends
intptr_t extraDataSize
vec2_t size
Definition ui_nodes.h:52
Contain the context of the calling of a function.
Definition ui_actions.h:208
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_onClick
Definition ui_nodes.h:148
uiBox_t box
Definition ui_nodes.h:96
struct uiAction_s * onClick
Definition ui_nodes.h:135
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)
const struct value_s * UI_RegisterNodeMethod(uiBehaviour_t *behaviour, const char *name, uiNodeMethod_t function)
Register a node method to a behaviour.
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
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_Node_Activate(uiNode_t *node)
Definition ui_node.cpp:271
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 common thing for GUI controls which allow to edit a value (scroolbar, spinner,...
int UI_CheckBox_ValueAsInteger(uiNode_t *node)
void UI_CheckBox_Toggle(uiNode_t *node)
void UI_RegisterCheckBoxNode(uiBehaviour_t *behaviour)
void UI_CheckBox_SetIconUnknownByName(uiNode_t *node, const char *name)
void UI_CheckBox_SetIconCheckedByName(uiNode_t *node, const char *name)
bool UI_CheckBox_ValueAsBoolean(uiNode_t *node)
void UI_CheckBox_SetBackgroundByName(uiNode_t *node, const char *name)
void UI_CheckBox_SetIconUncheckedByName(uiNode_t *node, const char *name)
static void UI_CheckBoxNodeCallActivate(uiNode_t *node, const uiCallContext_t *context)
#define UI_EXTRADATA(NODE, TYPE)
Definition ui_nodes.h:185
#define V_UI_SPRITEREF
Definition ui_parse.h:56
void UI_PlaySound(const char *soundFile)
Plays a ui sound.
Definition ui_sound.cpp:35
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_NORMAL
Definition ui_sprite.h:33