UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_bar.cpp
Go to the documentation of this file.
1 
8 /*
9 Copyright (C) 2002-2020 UFO: Alien Invasion.
10 
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 See the GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, 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"
36 #include "ui_node_abstractvalue.h"
37 #include "ui_node_abstractnode.h"
38 
39 #include "../../input/cl_keys.h"
40 
41 #include "../../../common/scripts_lua.h"
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 
95 void 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 
134 void 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 
145 void uiBarNode::onMouseUp (uiNode_t* node, int x, int y, int button)
146 {
147  if (button == K_MOUSE1)
148  UI_MouseRelease();
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 }
vec2_t size
Definition: ui_nodes.h:52
float getMin(uiNode_t const *node)
const char * name
Definition: ui_behaviour.h:41
static const vec3_t scale
#define EXTRADATA_TYPE
Definition: ui_node_bar.cpp:43
UINodePtr manager
Definition: ui_behaviour.h:43
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
Define common thing for GUI controls which allow to edit a value (scroolbar, spinner, and more)
void onCapturedMouseMove(uiNode_t *node, int x, int y) override
Called when the node is captured by the mouse.
Definition: ui_node_bar.cpp:95
#define VectorScale(in, scale, out)
Definition: vector.h:79
void UI_MouseRelease(void)
Release the captured node.
Definition: ui_input.cpp:526
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
float getMax(uiNode_t const *node)
void draw(uiNode_t *node) override
Definition: ui_node_bar.cpp:46
bool state
Definition: ui_nodes.h:106
#define Vector4Set(v, r, g, b, a)
Definition: vector.h:62
SharedPtr< uiNode > UINodePtr
int padding
Definition: ui_nodes.h:109
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
void UI_SetMouseCapture(uiNode_t *node)
Captured the mouse into a node.
Definition: ui_input.cpp:516
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
void onLoading(uiNode_t *node) override
Called before loading. Used to set default attribute values.
intptr_t extraDataSize
Definition: ui_behaviour.h:54
Definition: scripts.h:50
void UI_NodeAbsoluteToRelativePos(const uiNode_t *node, int *x, int *y)
Update an absolute position to a relative one.
Definition: ui_node.cpp:592
node behaviour, how a node work
Definition: ui_behaviour.h:39
vec4_t color
Definition: ui_nodes.h:127
const char * extends
Definition: ui_behaviour.h:42
vec_t vec2_t[2]
Definition: ufotypes.h:38
bool setValue(uiNode_t *node, float value)
uiBox_t box
Definition: ui_nodes.h:96
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
float getValue(uiNode_t const *node)
void onMouseUp(uiNode_t *node, int x, int y, int button) override
#define Vector4Copy(src, dest)
Definition: vector.h:53
#define EXTRADATA(node)
Definition: ui_node_bar.cpp:44
void UI_RegisterBarNode(uiBehaviour_t *behaviour)
void onMouseDown(uiNode_t *node, int x, int y, int button) override
vec_t vec4_t[4]
Definition: ufotypes.h:40