UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_checkbox.cpp
Go to the documentation of this file.
1 
14 /*
15 Copyright (C) 2002-2020 UFO: Alien Invasion.
16 
17 This program is free software; you can redistribute it and/or
18 modify it under the terms of the GNU General Public License
19 as published by the Free Software Foundation; either version 2
20 of the License, or (at your option) any later version.
21 
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25 
26 See the GNU General Public License for more details.
27 
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, 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"
45 #include "ui_node_abstractnode.h"
46 #include "ui_node_abstractvalue.h"
47 
48 #include "../../../common/scripts_lua.h"
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) {
62  status = SPRITE_STATUS_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 
108 static void UI_CheckBoxNodeCallActivate (uiNode_t* node, const uiCallContext_t* context)
109 {
110  UI_Node_Activate(node);
111 }
112 
116 void 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 
140 void UI_CheckBox_SetBackgroundByName (uiNode_t* node, const char* name) {
141  uiSprite_t* sprite = UI_GetSpriteByName(name);
142  UI_EXTRADATA(node, checkboxExtraData_t).background = sprite;
143 }
144 
145 void UI_CheckBox_SetIconCheckedByName (uiNode_t* node, const char* name) {
146  uiSprite_t* sprite = UI_GetSpriteByName(name);
147  UI_EXTRADATA(node, checkboxExtraData_t).iconChecked = sprite;
148 }
149 
151  uiSprite_t* sprite = UI_GetSpriteByName(name);
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 
160 void UI_CheckBox_SetIconUnknownByName (uiNode_t* node, const char* name) {
161  uiSprite_t* sprite = UI_GetSpriteByName(name);
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 }
vec2_t size
Definition: ui_nodes.h:52
static void UI_CheckBoxNodeCallActivate(uiNode_t *node, const uiCallContext_t *context)
const char * name
Definition: ui_behaviour.h:41
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_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_PlaySound(const char *soundFile)
Plays a ui sound.
Definition: ui_sound.cpp:35
Define common thing for GUI controls which allow to edit a value (scroolbar, spinner, and more)
void UI_CheckBox_Toggle(uiNode_t *node)
void UI_CheckBox_SetIconUncheckedByName(uiNode_t *node, const char *name)
void onLeftClick(uiNode_t *node, int x, int y) override
Handles checkboxes clicks.
int UI_CheckBox_ValueAsInteger(uiNode_t *node)
void UI_CheckBox_SetBackgroundByName(uiNode_t *node, const char *name)
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
bool state
Definition: ui_nodes.h:106
#define EXTRADATA(node)
SharedPtr< uiNode > UINodePtr
void onLoading(uiNode_t *node) override
Handled before the begin of the load of the node from the script.
void UI_Node_Activate(uiNode_t *node)
Definition: ui_node.cpp:271
void UI_CheckBox_SetIconUnknownByName(uiNode_t *node, const char *name)
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
#define UI_EXTRADATA(NODE, TYPE)
Definition: ui_nodes.h:185
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
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
#define EXTRADATA_TYPE
Contain the context of the calling of a function.
Definition: ui_actions.h:208
void UI_CheckBox_SetIconCheckedByName(uiNode_t *node, const char *name)
intptr_t extraDataSize
Definition: ui_behaviour.h:54
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 onActivate(uiNode_t *node) override
Activate the node. Can be used without the mouse (ie. a button will execute onClick) ...
void UI_RegisterCheckBoxNode(uiBehaviour_t *behaviour)
const char * extends
Definition: ui_behaviour.h:42
uiSpriteStatus_t
Definition: ui_sprite.h:32
vec_t vec2_t[2]
Definition: ufotypes.h:38
bool setValue(uiNode_t *node, float value)
void draw(uiNode_t *node) override
void onLoading(uiNode_t *node) override
LUA_EVENT lua_onClick
Definition: ui_nodes.h:148
uiBox_t box
Definition: ui_nodes.h:96
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
Definition: ui_sprite.cpp:115
float getValue(uiNode_t const *node)
bool UI_CheckBox_ValueAsBoolean(uiNode_t *node)
void setRange(uiNode_t *node, float min, float max)
const struct value_s * UI_RegisterNodeMethod(uiBehaviour_t *behaviour, const char *name, uiNodeMethod_t function)
Register a node method to a behaviour.
void toggle(uiNode_t *node)