UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_spinner.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24*/
25
26#include "../ui_nodes.h"
27#include "../ui_parse.h"
28#include "../ui_behaviour.h"
29#include "../ui_main.h"
30#include "../ui_input.h"
31#include "../ui_timer.h"
32#include "../ui_actions.h"
33#include "../ui_render.h"
34#include "../ui_sprite.h"
35#include "ui_node_spinner.h"
37
39#include "../../input/cl_keys.h"
40
42
43#define EXTRADATA_TYPE spinnerExtraData_t
44#define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
45#define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
46
48static uiTimer_t* capturedTimer = nullptr;
49
56{
57 if (!down)
58 return incValue(node);
59 return decValue(node);
60}
61
63{
65 switch (timer->calledTime) {
66 case 1:
67 timer->delay = 50;
68 break;
69 }
70}
71
73{
74 uiSpinnerNode* b = dynamic_cast<uiSpinnerNode*>(node->behaviour->manager.get());
75 b->repeat(node, timer);
76}
77
86{
87 switch ((spinnerMode_t)EXTRADATA(node).mode) {
89 return true;
91 return false;
92 case SPINNER_NORMAL:
93 if (EXTRADATA(node).horizontal)
94 return x > node->box.size[0] * 0.5;
95 return y < node->box.size[1] * 0.5;
96 default:
97 return false;
98 }
99}
100
101void uiSpinnerNode::onMouseDown (uiNode_t* node, int x, int y, int button)
102{
103 const bool disabled = node->disabled || node->parent->disabled;
104 if (disabled)
105 return;
106
107 if (button == K_MOUSE1) {
108 UI_SetMouseCapture(node);
109 UI_NodeAbsoluteToRelativePos(node, &x, &y);
111 if (EXTRADATA(node).inverted)
116 }
117}
118
119void uiSpinnerNode::onMouseUp (uiNode_t* node, int x, int y, int button)
120{
121 if (button == K_MOUSE1 && UI_GetMouseCapture() == node) {
123 }
124}
125
131{
132 if (capturedTimer) {
134 capturedTimer = nullptr;
135 }
136}
137
141bool uiSpinnerNode::onScroll (uiNode_t* node, int deltaX, int deltaY)
142{
143 bool down = deltaY > 0;
144 const bool disabled = node->disabled || node->parent->disabled;
145 if (deltaY == 0)
146 return false;
147 if (disabled)
148 return false;
149 return step(node, down);
150}
151
153{
154 vec2_t pos;
155 const float delta = getDelta(node);
156 const bool disabled = node->disabled || node->parent->disabled;
157
158 UI_GetNodeAbsPos(node, pos);
159
160 uiSpriteStatus_t status;
161 uiSpriteStatus_t topStatus;
162 uiSpriteStatus_t bottomStatus;
163
164 if (disabled || delta == 0) {
165 status = SPRITE_STATUS_DISABLED;
166 topStatus = SPRITE_STATUS_DISABLED;
167 bottomStatus = SPRITE_STATUS_DISABLED;
168 } else {
169 const float value = getValue(node);
170 const float min = getMin(node);
171 const float max = getMax(node);
172
173 status = SPRITE_STATUS_NORMAL;
174
175 bool increaseLocation = isPositionIncrease(node, mousePosX - pos[0], mousePosY - pos[1]);
176
177 /* top button status */
178 if (node->state && increaseLocation) {
179 topStatus = SPRITE_STATUS_HOVER;
180 } else {
181 topStatus = SPRITE_STATUS_NORMAL;
182 }
183 /* bottom button status */
184 if (node->state && !increaseLocation) {
185 bottomStatus = SPRITE_STATUS_HOVER;
186 } else {
187 bottomStatus = SPRITE_STATUS_NORMAL;
188 }
189
190 if (value >= max) {
191 if (EXTRADATA(node).inverted)
192 bottomStatus = SPRITE_STATUS_DISABLED;
193 else
194 topStatus = SPRITE_STATUS_DISABLED;
195 }
196 if (value <= min) {
197 if (EXTRADATA(node).inverted)
198 topStatus = SPRITE_STATUS_DISABLED;
199 else
200 bottomStatus = SPRITE_STATUS_DISABLED;
201 }
202 }
203
204 if (EXTRADATA(node).background)
205 UI_DrawSpriteInBox(false, EXTRADATA(node).background, status, pos[0], pos[1], node->box.size[0], node->box.size[1]);
206 if (!EXTRADATA(node).horizontal) {
207 if (EXTRADATA(node).topIcon)
208 UI_DrawSpriteInBox(false, EXTRADATA(node).topIcon, topStatus, pos[0], pos[1], node->box.size[0], node->box.size[1]);
209 if (EXTRADATA(node).bottomIcon)
210 UI_DrawSpriteInBox(false, EXTRADATA(node).bottomIcon, bottomStatus, pos[0], pos[1], node->box.size[0], node->box.size[1]);
211 } else {
212 if (EXTRADATA(node).topIcon) /* Top becomes right */
213 UI_DrawSpriteInBox(false, EXTRADATA(node).topIcon, topStatus, pos[0] + node->box.size[0] / 2, pos[1], node->box.size[0] / 2, node->box.size[1]);
214 if (EXTRADATA(node).bottomIcon) /* Bottom becomes left */
215 UI_DrawSpriteInBox(false, EXTRADATA(node).bottomIcon, bottomStatus, pos[0], pos[1], node->box.size[0] / 2, node->box.size[1]);
216 }
217}
218
223
226 UI_EXTRADATA(node, spinnerExtraData_t).background = sprite;
227}
228
231 UI_EXTRADATA(node, spinnerExtraData_t).bottomIcon = sprite;
232}
233
234void UI_Spinner_SetTopIconByName(uiNode_t* node, const char* name) {
236 UI_EXTRADATA(node, spinnerExtraData_t).topIcon = sprite;
237}
238
239
241{
242 behaviour->name = "spinner";
243 behaviour->extends = "abstractvalue";
244 behaviour->manager = UINodePtr(new uiSpinnerNode());
245 behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
246 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiSpinnerNode_t *");
247
251 UI_RegisterExtradataNodeProperty(behaviour, "background", V_UI_SPRITEREF, EXTRADATA_TYPE, background);
252
256 UI_RegisterExtradataNodeProperty(behaviour, "topIcon", V_UI_SPRITEREF, EXTRADATA_TYPE, topIcon);
257
261 UI_RegisterExtradataNodeProperty(behaviour, "bottomIcon", V_UI_SPRITEREF, EXTRADATA_TYPE, bottomIcon);
262
270
274 UI_RegisterExtradataNodeProperty(behaviour, "horizontal", V_BOOL, EXTRADATA_TYPE, horizontal);
275
279 UI_RegisterExtradataNodeProperty(behaviour, "inverted", V_BOOL, EXTRADATA_TYPE, inverted);
280
281 Com_RegisterConstInt("SPINNER_NORMAL", SPINNER_NORMAL);
282 Com_RegisterConstInt("SPINNER_ONLY_INC", SPINNER_ONLY_INCREASE);
283 Com_RegisterConstInt("SPINNER_ONLY_DEC", SPINNER_ONLY_DECREASE);
284}
int mousePosY
Definition cl_input.cpp:76
int down
Definition cl_input.cpp:66
int mousePosX
Definition cl_input.cpp:76
External (non-keyboard) input devices.
Header file for keyboard handler.
@ K_MOUSE1
Definition cl_keys.h:46
PointerType get() const
Definition sharedptr.h:197
float getValue(uiNode_t const *node)
void onLoading(uiNode_t *node) override
bool decValue(uiNode_t *node)
bool incValue(uiNode_t *node)
float getDelta(uiNode_t const *node)
float getMax(uiNode_t const *node)
float getMin(uiNode_t const *node)
bool isPositionIncrease(uiNode_t *node, int x, int y)
Check a position relative to the node to check action is can produce.
void onCapturedMouseLost(uiNode_t *node) override
Called when the node have lost the captured node We clean cached data.
bool step(uiNode_t *node, bool down)
change the value of the spinner of one step
void onMouseUp(uiNode_t *node, int x, int y, int button) override
void onMouseDown(uiNode_t *node, int x, int y, int button) override
void repeat(uiNode_t *node, struct uiTimer_s *timer)
bool onScroll(uiNode_t *node, int deltaX, int deltaY) override
void draw(uiNode_t *node) override
void onLoading(uiNode_t *node) override
const char int mode
Definition ioapi.h:41
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
void Com_RegisterConstInt(const char *name, int value)
Register mappings between script strings and enum values for values of the type V_INT.
Definition scripts.cpp:198
@ V_BOOL
Definition scripts.h:50
@ V_INT
Definition scripts.h:52
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
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
uiNode_t * parent
Definition ui_nodes.h:92
uiBox_t box
Definition ui_nodes.h:96
bool state
Definition ui_nodes.h:106
vec_t vec2_t[2]
Definition ufotypes.h:38
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
void UI_SetMouseCapture(uiNode_t *node)
Captured the mouse into a node.
Definition ui_input.cpp:516
uiNode_t * UI_GetMouseCapture(void)
Return the captured node.
Definition ui_input.cpp:508
void UI_MouseRelease(void)
Release the captured node.
Definition ui_input.cpp:526
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_NodeAbsoluteToRelativePos(const uiNode_t *node, int *x, int *y)
Update an absolute position to a relative one.
Definition ui_node.cpp:604
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)
void UI_Spinner_SetBottomIconByName(uiNode_t *node, const char *name)
static uiTimer_t * capturedTimer
static void UI_SpinnerNodeRepeat(uiNode_t *node, uiTimer_t *timer)
void UI_Spinner_SetTopIconByName(uiNode_t *node, const char *name)
void UI_RegisterSpinnerNode(uiBehaviour_t *behaviour)
void UI_Spinner_SetBackgroundByName(uiNode_t *node, const char *name)
static bool capturedDownButton
spinnerMode_t
@ SPINNER_ONLY_DECREASE
@ SPINNER_ONLY_INCREASE
@ SPINNER_NORMAL
#define UI_EXTRADATA(NODE, TYPE)
Definition ui_nodes.h:185
#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)
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
uiTimer_t * UI_AllocTimer(uiNode_t *node, int firstDelay, timerCallback_t callback)
Allocate a new time for a node.
Definition ui_timer.cpp:123
void UI_TimerStart(uiTimer_t *timer)
Restart a timer.
Definition ui_timer.cpp:150
void UI_TimerRelease(uiTimer_t *timer)
Release the timer. It no more exists.
Definition ui_timer.cpp:176