UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_linechart.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, 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"
30#include "ui_node_linechart.h"
31
33
35
36#define EXTRADATA_TYPE lineChartExtraData_t
37#define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
38
42typedef struct line_s {
43 char id[MAX_VAR];
44 bool visible;
46 bool dots;
47 int* pointList;
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
133bool 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
176bool 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
209bool 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
236bool 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}
CGAME_HARD_LINKED_FUNCTIONS linkedList_t * LIST_Add(linkedList_t **listDest, void const *data, size_t length)
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition r_state.cpp:1011
void draw(uiNode_t *node) override
Drawing routine of the lineChart node.
void deleteNode(uiNode_t *node) override
Cleanup tasks on removing a lineChart.
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
void LIST_Delete(linkedList_t **list)
Definition list.cpp:195
#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
#define Mem_PoolAllocTypeN(type, n, pool)
Definition mem.h:42
#define Mem_Free(ptr)
Definition mem.h:35
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
void R_DrawLineStrip(int points, int *verts)
2 dimensional line strip
Definition r_draw.cpp:477
QGL_EXTERN GLint i
Definition r_gl.h:113
@ V_BOOL
Definition scripts.h:50
@ V_COLOR
Definition scripts.h:57
Header for lua script functions.
#define Q_streq(a, b)
Definition shared.h:136
#define Q_strneq(a, b, n)
Definition shared.h:137
bool Q_strnull(const char *string)
Definition shared.h:138
#define MAX_VAR
Definition shared.h:36
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
Structure describes a line.
char id[MAX_VAR]
extradata for the linechart node
node behaviour, how a node work
const char * name
void * lua_SWIG_typeinfo
UINodePtr manager
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
uiBox_t box
Definition ui_nodes.h:96
vec_t vec3_t[3]
Definition ufotypes.h:39
vec_t vec4_t[4]
Definition ufotypes.h:40
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Internal data use by the UI package.
memPool_t * ui_dynPool
Definition ui_main.cpp:41
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:526
SharedPtr< uiNode > UINodePtr
#define EXTRADATA_TYPE
#define EXTRADATA(node)
bool UI_AddLineChartCoord(uiNode_t *node, const char *id, int x, int y)
Add a data point to a line chart.
bool UI_ShowChartDots(uiNode_t *node, const char *id, bool visible)
Shows/hides small dots at data points of a chart.
bool UI_ShowChartLine(uiNode_t *node, const char *id, bool visible)
Shows/hides a chart line.
#define EXTRADATA(node)
void UI_RegisterLineChartNode(uiBehaviour_t *behaviour)
Registers lineChart node.
bool UI_ClearLineChart(uiNode_t *node)
Clears all drawings froma lineChart.
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.
bool UI_ClearLineChart(uiNode_t *node)
Clears all drawings froma lineChart.
const char * UI_GetPath(const uiNode_t *node)
Return a path from a window to a node.
Definition ui_nodes.cpp:174
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
#define Vector4Copy(src, dest)
Definition vector.h:53