UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_bar.cpp
Go to the documentation of this file.
1
7
8/*
9Copyright (C) 2002-2025 UFO: Alien Invasion.
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
20See the GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
23along with this program; if not, write to the Free Software
24Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26*/
27
28#include "../ui_nodes.h"
29#include "../ui_behaviour.h"
30#include "../ui_parse.h"
31#include "../ui_main.h"
32#include "../ui_input.h"
33#include "../ui_render.h"
34#include "../ui_actions.h"
35#include "ui_node_bar.h"
38
39#include "../../input/cl_keys.h"
40
42
43#define EXTRADATA_TYPE barExtraData_t
44#define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
45
47{
48 vec4_t color;
49 float fac;
50 vec2_t nodepos;
51 const float min = getMin(node);
52 const float max = getMax(node);
53 const float value = getValue(node);
54
55 UI_GetNodeAbsPos(node, nodepos);
56
57 if (node->state && !EXTRADATA(node).readOnly) {
58 Vector4Copy(node->color, color);
59 } else {
60 const float scale = EXTRADATA(node).noHover ? 1.0 : 0.8;
61 VectorScale(node->color, scale, color);
62 color[3] = node->color[3];
63 }
64
65 /* shoud it return an error? */
66 if (max > min)
67 fac = (value - min) / (max - min);
68 else
69 fac = 1;
70 if (fac <= 0 || fac > 1)
71 return;
72
73 switch (EXTRADATA(node).orientation) {
74 case ALIGN_UC:
75 UI_DrawFill(nodepos[0] + node->padding, nodepos[1] + node->padding + node->box.size[1] - fac * node->box.size[1], node->box.size[0] - 2 * node->padding, fac * node->box.size[1] - 2 * node->padding , color);
76 break;
77 case ALIGN_LC:
78 UI_DrawFill(nodepos[0] + node->padding, nodepos[1] + node->padding, node->box.size[0] - 2 * node->padding, fac * node->box.size[1] - 2 * node->padding, color);
79 break;
80 case ALIGN_CR:
81 UI_DrawFill(nodepos[0] + node->padding, nodepos[1] + node->padding, fac * node->box.size[0] - 2 * node->padding, node->box.size[1] - 2 * node->padding, color);
82 break;
83 case ALIGN_CL:
84 UI_DrawFill(nodepos[0] + node->padding + node->box.size[0] - fac * node->box.size[0], nodepos[1] + node->padding, fac * node->box.size[0] - 2 * node->padding, node->box.size[1] - 2 * node->padding, color);
85 break;
86 default:
87 Com_Printf("UI_BarNodeDraw: Orientation %d not supported\n", EXTRADATA(node).orientation);
88 break;
89 }
90}
91
95void uiBarNode::onCapturedMouseMove (uiNode_t* node, int x, int y)
96{
97 UI_NodeAbsoluteToRelativePos(node, &x, &y);
98
99 if (x < 0)
100 x = 0;
101 else if (x > node->box.size[0])
102 x = node->box.size[0];
103 if (y < 0)
104 y = 0;
105 else if (y > node->box.size[1])
106 y = node->box.size[1];
107
108 float frac;
109 const float min = getMin(node);
110 const float max = getMax(node);
111
112 switch (EXTRADATA(node).orientation) {
113 case ALIGN_UC:
114 frac = (node->box.size[1] - (float) y) / node->box.size[1];
115 break;
116 case ALIGN_LC:
117 frac = (float) y / node->box.size[1];
118 break;
119 case ALIGN_CR:
120 frac = (float) x / node->box.size[0];
121 break;
122 case ALIGN_CL:
123 frac = (node->box.size[0] - (float) x) / node->box.size[0];
124 break;
125 default:
126 frac = 0;
127 Com_Printf("UI_BarNodeCapturedMouseMove: Orientation %d not supported\n", EXTRADATA(node).orientation);
128 break;
129 }
130
131 setValue(node, min + frac * (max - min));
132}
133
134void uiBarNode::onMouseDown (uiNode_t* node, int x, int y, int button)
135{
136 if (node->disabled || EXTRADATA(node).readOnly)
137 return;
138
139 if (button == K_MOUSE1) {
140 UI_SetMouseCapture(node);
141 onCapturedMouseMove(node, x, y);
142 }
143}
144
145void uiBarNode::onMouseUp (uiNode_t* node, int x, int y, int button)
146{
147 if (button == K_MOUSE1)
149}
150
155{
156 Vector4Set(node->color, 1, 1, 1, 1);
157 EXTRADATA(node).orientation = ALIGN_CR;
158}
159
161{
162 behaviour->name = "bar";
163 behaviour->extends = "abstractvalue";
164 behaviour->manager = UINodePtr(new uiBarNode());
165 behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
166 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiBarNode_t *");
167
171 UI_RegisterExtradataNodeProperty(behaviour, "direction", V_ALIGN, EXTRADATA_TYPE, orientation);
175 UI_RegisterExtradataNodeProperty(behaviour, "readonly", V_BOOL, EXTRADATA_TYPE, readOnly);
179 UI_RegisterExtradataNodeProperty(behaviour, "nohover", V_BOOL, EXTRADATA_TYPE, noHover);
180}
Header file for keyboard handler.
@ K_MOUSE1
Definition cl_keys.h:46
float getValue(uiNode_t const *node)
bool setValue(uiNode_t *node, float value)
float getMax(uiNode_t const *node)
float getMin(uiNode_t const *node)
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 draw(uiNode_t *node) override
void onCapturedMouseMove(uiNode_t *node, int x, int y) override
Called when the node is captured by the mouse.
void onLoading(uiNode_t *node) override
Called before loading. Used to set default attribute values.
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
@ ALIGN_CR
Definition scripts.h:95
@ ALIGN_CL
Definition scripts.h:93
@ ALIGN_LC
Definition scripts.h:97
@ ALIGN_UC
Definition scripts.h:91
@ V_BOOL
Definition scripts.h:50
@ V_ALIGN
Definition scripts.h:61
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
vec4_t color
Definition ui_nodes.h:127
uiBox_t box
Definition ui_nodes.h:96
int padding
Definition ui_nodes.h:109
bool state
Definition ui_nodes.h:106
vec_t vec4_t[4]
Definition ufotypes.h:40
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
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)
Define common thing for GUI controls which allow to edit a value (scroolbar, spinner,...
void UI_RegisterBarNode(uiBehaviour_t *behaviour)
static const vec3_t scale
void UI_DrawFill(int x, int y, int w, int h, const vec4_t color)
Fills a box of pixels with a single color.
Definition ui_render.cpp:37
#define Vector4Set(v, r, g, b, a)
Definition vector.h:62
#define Vector4Copy(src, dest)
Definition vector.h:53
#define VectorScale(in, scale, out)
Definition vector.h:79