UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_linechart.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "../ui_nodes.h"
26 #include "../ui_parse.h"
27 #include "../ui_internal.h"
28 #include "../ui_render.h"
29 #include "ui_node_abstractnode.h"
30 #include "ui_node_linechart.h"
31 
32 #include "../../renderer/r_draw.h"
33 
34 #include "../../../common/scripts_lua.h"
35 
36 #define EXTRADATA_TYPE lineChartExtraData_t
37 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
38 
42 typedef struct line_s {
43  char id[MAX_VAR];
44  bool visible;
46  bool dots;
47  int* pointList;
48  int maxPoints;
49  int numPoints;
50 } line_t;
51 
57 {
58  vec3_t pos;
59  UI_GetNodeAbsPos(node, pos);
60  pos[2] = 0;
61 
62  UI_Transform(pos, nullptr, nullptr);
63 
64  /* Draw axes */
65  if (EXTRADATA(node).displayAxes) {
66  int axes[6];
67  axes[0] = 0;
68  axes[1] = 0;
69  axes[2] = 0;
70  axes[3] = (int) node->box.size[1];
71  axes[4] = (int) node->box.size[0];
72  axes[5] = (int) node->box.size[1];
73  R_Color(EXTRADATA(node).axesColor);
74  R_DrawLineStrip(3, axes);
75  }
76 
77  LIST_Foreach(EXTRADATA(node).lines, line_t, line) {
78  if (!line->visible)
79  continue;
80  R_Color(line->lineColor);
81  R_DrawLineStrip(line->numPoints, line->pointList);
82 
83  if (!line->dots)
84  continue;
85 
86  for (int i = 0; i < line->numPoints; i++) {
87  R_DrawFill(line->pointList[i * 2] - 2, line->pointList[i * 2 + 1] - 2, 5, 5, line->lineColor);
88  }
89  }
90  R_Color(nullptr);
91 
92  UI_Transform(nullptr, nullptr, nullptr);
93 }
94 
100 {
101  UI_ClearLineChart(node);
102  LIST_Delete(&(EXTRADATA(node).lines));
103 }
104 
110 {
111  assert(node);
112 
113  LIST_Foreach(EXTRADATA(node).lines, line_t, line) {
114  if (line->numPoints > 0) {
115  line->numPoints = 0;
116  Mem_Free(line->pointList);
117  }
118  }
119  LIST_Delete(&(EXTRADATA(node).lines));
120 
121  return false;
122 }
123 
133 bool UI_AddLineChartLine (uiNode_t* node, const char* id, bool visible, const vec4_t color, bool dots, int numPoints)
134 {
135  assert(node);
136 
137  if (Q_strnull(id)) {
138  Com_Printf("UI_AddLineChartLine: Missing line identifier for extending lineChart '%s'\n", UI_GetPath(node));
139  return false;
140  }
141 
142  line_t newLine;
143  Q_strncpyz(newLine.id, id, MAX_VAR);
144  LIST_Foreach(EXTRADATA(node).lines, line_t, line) {
145  if (Q_streq(line->id, newLine.id)) {
146  Com_Printf("UI_AddLineChartLine: A line with id '%s' in lineChart '%s' already exists\n", newLine.id, UI_GetPath(node));
147  return false;
148  }
149  }
150 
151  newLine.visible = visible;
152  Vector4Copy(color, newLine.lineColor);
153  newLine.dots = dots;
154  newLine.maxPoints = 0;
155  newLine.numPoints = 0;
156  newLine.pointList = nullptr;
157 
158  if (numPoints >= 0) {
159  newLine.pointList = Mem_PoolAllocTypeN(int, numPoints * 2, ui_dynPool);
160  if (newLine.pointList != nullptr) {
161  newLine.maxPoints = numPoints;
162  }
163  }
164 
165  return (nullptr != LIST_Add(&(EXTRADATA(node).lines), &newLine, sizeof(newLine)));
166 }
167 
176 bool UI_AddLineChartCoord (uiNode_t* node, const char* id, int x, int y)
177 {
178  assert(node);
179 
180  if (Q_strnull(id)) {
181  Com_Printf("UI_AddLineChartCoord: Missing line identifier for extending lineChart '%s'\n", UI_GetPath(node));
182  return false;
183  }
184 
185  LIST_Foreach(EXTRADATA(node).lines, line_t, line) {
186  if (!Q_strneq(line->id, id, MAX_VAR))
187  continue;
188 
189  if (line->numPoints < line->maxPoints) {
190  line->pointList[line->numPoints * 2] = x;
191  line->pointList[line->numPoints * 2 + 1] = y;
192  line->numPoints++;
193  return true;
194  } else {
195  Com_Printf("UI_AddLineChartCoord: Line '%s' has already reached it's max capacity %d in lineChart '%s'\n", id, line->maxPoints, UI_GetPath(node));
196  }
197  }
198 
199  Com_Printf("UI_AddLineChartCoord: Line '%s' was not found in lineChart '%s'\n", id, UI_GetPath(node));
200  return false;
201 }
202 
209 bool UI_ShowChartLine (uiNode_t* node, const char* id, bool visible)
210 {
211  assert(node);
212 
213  if (Q_strnull(id)) {
214  Com_Printf("UI_ShowChartLine: Missing line identifier for configuring lineChart '%s'\n", UI_GetPath(node));
215  return false;
216  }
217 
218  LIST_Foreach(EXTRADATA(node).lines, line_t, line) {
219  if (!Q_strneq(line->id, id, MAX_VAR))
220  continue;
221 
222  line->visible = visible;
223  return true;
224  }
225 
226  Com_Printf("UI_ShowChartLine: Line '%s' was not found in lineChart '%s'\n", id, UI_GetPath(node));
227  return false;
228 }
229 
236 bool UI_ShowChartDots (uiNode_t* node, const char* id, bool visible)
237 {
238  assert(node);
239 
240  if (Q_strnull(id)) {
241  Com_Printf("UI_ShowChartDots: Missing line identifier for configuring lineChart '%s'\n", UI_GetPath(node));
242  return false;
243  }
244 
245  LIST_Foreach(EXTRADATA(node).lines, line_t, line) {
246  if (!Q_strneq(line->id, id, MAX_VAR))
247  continue;
248 
249  line->dots = visible;
250  return true;
251  }
252 
253  Com_Printf("UI_ShowChartDots: Line '%s' was not found in lineChart '%s'\n", id, UI_GetPath(node));
254  return false;
255 }
256 
262 {
263  behaviour->name = "linechart";
264  behaviour->manager = UINodePtr(new uiLineChartNode());
265  behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
266  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiLineChartNode_t *");
267 
268  /* If true, it display axes of the chart. */
269  UI_RegisterExtradataNodeProperty(behaviour, "displayaxes", V_BOOL, lineChartExtraData_t, displayAxes);
270  /* Axe color. */
271  UI_RegisterExtradataNodeProperty(behaviour, "axescolor", V_COLOR, lineChartExtraData_t, axesColor);
272 }
bool Q_strnull(const char *string)
Definition: shared.h:138
vec2_t size
Definition: ui_nodes.h:52
void R_DrawLineStrip(int points, int *verts)
2 dimensional line strip
Definition: r_draw.cpp:477
const char * name
Definition: ui_behaviour.h:41
void draw(uiNode_t *node) override
Drawing routine of the lineChart node.
char id[MAX_VAR]
UINodePtr manager
Definition: ui_behaviour.h:43
#define EXTRADATA_TYPE
Structure describes a line.
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
void UI_RegisterLineChartNode(uiBehaviour_t *behaviour)
Registers lineChart node.
#define EXTRADATA(node)
void LIST_Delete(linkedList_t **list)
Definition: list.cpp:195
bool UI_AddLineChartLine(uiNode_t *node, const char *id, bool visible, const vec4_t color, bool dots, int numPoints)
Add a line to the chart.
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition: r_state.cpp:1011
extradata for the linechart node
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
#define MAX_VAR
Definition: shared.h:36
SharedPtr< uiNode > UINodePtr
const char * UI_GetPath(const uiNode_t *node)
Return a path from a window to a node.
Definition: ui_nodes.cpp:174
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
void UI_Transform(const vec3_t transform, const vec3_t rotate, const vec3_t scale)
Pushes a new matrix, normalize to current resolution and move, rotate and scale the matrix to the giv...
Definition: ui_render.cpp:68
memPool_t * ui_dynPool
Definition: ui_main.cpp:41
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
struct line_s line_t
Structure describes a line.
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_GetNodeAbsPos(const uiNode_t *node, vec2_t pos)
Returns the absolute position of a node.
Definition: ui_node.cpp:514
CGAME_HARD_LINKED_FUNCTIONS linkedList_t * LIST_Add(linkedList_t **listDest, void const *data, size_t length)
#define Mem_PoolAllocTypeN(type, n, pool)
Definition: mem.h:42
#define Q_strneq(a, b, n)
Definition: shared.h:137
bool UI_ShowChartDots(uiNode_t *node, const char *id, bool visible)
Shows/hides small dots at data points of a chart.
intptr_t extraDataSize
Definition: ui_behaviour.h:54
QGL_EXTERN GLint i
Definition: r_gl.h:113
Definition: scripts.h:50
node behaviour, how a node work
Definition: ui_behaviour.h:39
#define Mem_Free(ptr)
Definition: mem.h:35
bool UI_ShowChartLine(uiNode_t *node, const char *id, bool visible)
Shows/hides a chart line.
#define LIST_Foreach(list, type, var)
Iterates over a linked list, it's safe to delete the returned entry from the list while looping over ...
Definition: list.h:41
vec_t vec3_t[3]
Definition: ufotypes.h:39
bool UI_ClearLineChart(uiNode_t *node)
Clears all drawings froma lineChart.
bool UI_AddLineChartCoord(uiNode_t *node, const char *id, int x, int y)
Add a data point to a line chart.
#define Q_streq(a, b)
Definition: shared.h:136
uiBox_t box
Definition: ui_nodes.h:96
void R_DrawFill(int x, int y, int w, int h, const vec4_t color)
Fills a box of pixels with a single color.
Definition: r_draw.cpp:188
vec4_t lineColor
#define Vector4Copy(src, dest)
Definition: vector.h:53
void deleteNode(uiNode_t *node) override
Cleanup tasks on removing a lineChart.
vec_t vec4_t[4]
Definition: ufotypes.h:40