UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_textlist.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_main.h"
26 #include "../ui_internal.h"
27 #include "../ui_font.h"
28 #include "../ui_actions.h"
29 #include "../ui_parse.h"
30 #include "../ui_render.h"
31 #include "../ui_data.h"
32 #include "../ui_lua.h"
33 
34 #include "ui_node_text.h"
35 #include "ui_node_textlist.h"
36 #include "ui_node_abstractnode.h"
37 
38 #include "../../client.h"
39 #include "../../../shared/parse.h"
40 
41 #include "../../../common/scripts_lua.h"
42 
43 #define EXTRADATA(node) UI_EXTRADATA(node, textExtraData_t)
44 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, textExtraData_t)
45 
52 static int UI_TextListNodeGetLine (const uiNode_t* node, int x, int y)
53 {
54  int lineHeight = EXTRADATACONST(node).lineHeight;
55  if (lineHeight == 0) {
56  const char* font = UI_GetFontFromNode(node);
57  lineHeight = UI_FontGetHeight(font);
58  }
59 
60  UI_NodeAbsoluteToRelativePos(node, &x, &y);
61  y -= node->padding;
62  return (int) (y / lineHeight) + EXTRADATACONST(node).super.scrollY.viewPos;
63 }
64 
65 void uiTextListNode::onMouseMove (uiNode_t* node, int x, int y)
66 {
67  EXTRADATA(node).lineUnderMouse = UI_TextListNodeGetLine(node, x, y);
68 }
69 
76 {
77  vec4_t colorHover;
78  vec4_t colorSelectedHover;
79  int count; /* variable x position */
80  const char* font = UI_GetFontFromNode(node);
81  vec2_t pos;
82  int currentY;
83  int viewSizeY;
84  int lineHeight;
85  int maxHeight;
86 
87  lineHeight = EXTRADATA(node).lineHeight;
88  if (lineHeight == 0)
89  lineHeight = UI_FontGetHeight(font);
90 
91  if (isSizeChange(node))
92  viewSizeY = node->box.size[1] / lineHeight;
93  else
94  viewSizeY = EXTRADATA(node).super.scrollY.viewSize;
95 
96  /* text box */
97  UI_GetNodeAbsPos(node, pos);
98  pos[0] += node->padding;
99  pos[1] += node->padding;
100 
101  /* prepare colors */
102  VectorScale(node->color, 0.8, colorHover);
103  colorHover[3] = node->color[3];
104  VectorScale(node->selectedColor, 0.8, colorSelectedHover);
105  colorSelectedHover[3] = node->selectedColor[3];
106 
107  /* skip lines over the scroll */
108  count = 0;
109  while (list && count < EXTRADATA(node).super.scrollY.viewPos) {
110  list = list->next;
111  count++;
112  }
113 
114  currentY = pos[1];
115  maxHeight = currentY + node->box.size[1] - node->padding - node->padding - node->padding - lineHeight;
116  while (list) {
117  const int width = node->box.size[0] - node->padding - node->padding;
118  const char* text = (const char*) list->data;
119  if (currentY > maxHeight)
120  break;
121 
122  /* highlight selected line */
123  if (count == EXTRADATA(node).textLineSelected && EXTRADATA(node).textLineSelected >= 0)
124  R_Color(node->selectedColor);
125  else
126  R_Color(node->color);
127 
128  /* highlight line under mouse */
129  if (node->state && count == EXTRADATA(node).lineUnderMouse) {
130  if (count == EXTRADATA(node).textLineSelected && EXTRADATA(node).textLineSelected >= 0)
131  R_Color(colorSelectedHover);
132  else
133  R_Color(colorHover);
134  }
135 
136  UI_DrawStringInBox(font, (align_t)node->contentAlign, pos[0], currentY, width, lineHeight, text);
137 
138  /* next entries' position */
139  currentY += lineHeight;
140 
141 
142  list = list->next;
143  count++;
144  }
145 
146  /* count all elements */
147  while (list) {
148  list = list->next;
149  count++;
150  }
151 
152  /* update scroll status */
153  setScrollY(node, -1, viewSizeY, count);
154 
155  R_Color(nullptr);
156 }
157 
162 {
163  const uiSharedData_t* shared;
164  shared = &ui_global.sharedData[EXTRADATA(node).dataID];
165 
166  /* nothing set yet? */
167  if (shared->type == UI_SHARED_NONE)
168  return;
169  if (shared->type != UI_SHARED_LINKEDLISTTEXT) {
170  Com_Printf("UI_TextListNodeDraw: Only linkedlist text supported (dataid %d).\n", EXTRADATA(node).dataID);
171  UI_ResetData(EXTRADATA(node).dataID);
172  return;
173  }
174 
175  drawText(node, shared->data.linkedListText);
176 }
177 
182 void uiTextListNode::onLeftClick (uiNode_t* node, int x, int y)
183 {
184  const int line = UI_TextListNodeGetLine(node, x, y);
185 
186  if (line < 0 || line >= EXTRADATA(node).super.scrollY.fullSize)
187  return;
188 
189  if (line != EXTRADATA(node).textLineSelected) {
190  EXTRADATA(node).textLineSelected = line;
191  EXTRADATA(node).textSelected = UI_TextNodeGetSelectedText(node, line);
192  if (node->onChange) {
193  UI_ExecuteEventActions(node, node->onChange);
194  }
195  if (node->lua_onChange != LUA_NOREF) {
197  }
198  }
199 
200  if (node->onClick) {
201  UI_ExecuteEventActions(node, node->onClick);
202  }
203  if (node->lua_onClick != LUA_NOREF) {
204  UI_ExecuteLuaEventScript_XY(node, node->lua_onClick, x, y);
205  }
206 }
207 
212 void uiTextListNode::onRightClick (uiNode_t* node, int x, int y)
213 {
214  const int line = UI_TextListNodeGetLine(node, x, y);
215 
216  if (line < 0 || line >= EXTRADATA(node).super.scrollY.fullSize)
217  return;
218 
219  if (line != EXTRADATA(node).textLineSelected) {
220  EXTRADATA(node).textLineSelected = line;
221  EXTRADATA(node).textSelected = UI_TextNodeGetSelectedText(node, line);
222  if (node->onChange) {
223  UI_ExecuteEventActions(node, node->onChange);
224  }
225  if (node->lua_onChange != LUA_NOREF) {
227  }
228  }
229 
230  if (node->onRightClick) {
232  }
233  if (node->lua_onRightClick != LUA_NOREF) {
235  }
236 }
237 
239 {
240  EXTRADATA(node).textLineSelected = -1;
241  EXTRADATA(node).textSelected = "";
242  Vector4Set(node->selectedColor, 1.0, 1.0, 1.0, 1.0);
243  Vector4Set(node->color, 1.0, 1.0, 1.0, 1.0);
244  node->contentAlign = ALIGN_CL;
245 }
246 
248 {
249  behaviour->name = "textlist";
250  behaviour->extends = "text";
251  behaviour->manager = UINodePtr(new uiTextListNode());
252  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiTextListNode_t *");
253 }
#define EXTRADATA(node)
static int UI_TextListNodeGetLine(const uiNode_t *node, int x, int y)
Get the line number under an absolute position.
vec2_t size
Definition: ui_nodes.h:52
void onLeftClick(uiNode_t *node, int x, int y) override
Calls the script command for a text node that is clickable.
int UI_DrawStringInBox(const char *fontID, align_t align, int x, int y, int width, int height, const char *text, longlines_t method)
draw a line into a bounding box
Definition: ui_render.cpp:359
LUA_EVENT lua_onChange
Definition: ui_nodes.h:162
uiGlobal_t ui_global
Definition: ui_main.cpp:38
const char * name
Definition: ui_behaviour.h:41
struct uiAction_s * onChange
Definition: ui_nodes.h:143
uiSharedData_t sharedData[UI_MAX_DATAID]
Holds shared data.
Definition: ui_internal.h:59
void * data
Definition: list.h:31
UINodePtr manager
Definition: ui_behaviour.h:43
bool setScrollY(uiNode_t *node, int viewPos, int viewSize, int fullSize)
Set the Y scroll to a position, and call event if need.
bool UI_ExecuteLuaEventScript(uiNode_t *node, LUA_EVENT event)
Executes a lua event handler.
Definition: ui_lua.cpp:71
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
void onMouseMove(uiNode_t *node, int x, int y) override
void onLoading(uiNode_t *node) override
struct uiAction_s * onClick
Definition: ui_nodes.h:135
void draw(uiNode_t *node) override
Draw a text node.
#define VectorScale(in, scale, out)
Definition: vector.h:79
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition: r_state.cpp:1011
align_t
We need this here for checking the boundaries from script values.
Definition: scripts.h:90
LUA_EVENT lua_onRightClick
Definition: ui_nodes.h:149
bool state
Definition: ui_nodes.h:106
void UI_ResetData(int dataId)
Reset a shared data. Type became NONE and value became nullptr.
Definition: ui_data.cpp:212
uiSharedType_t type
Definition: ui_data.h:45
linkedList_t * linkedListText
Holds a linked list for displaying in the UI.
Definition: ui_data.h:50
#define Vector4Set(v, r, g, b, a)
Definition: vector.h:62
bool isSizeChange(uiNode_t *node)
return true if the node size change and update the cache
void UI_RegisterTextListNode(uiBehaviour_t *behaviour)
SharedPtr< uiNode > UINodePtr
const char * UI_TextNodeGetSelectedText(uiNode_t *node, int num)
int padding
Definition: ui_nodes.h:109
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
void UI_ExecuteEventActions(uiNode_t *source, const uiAction_t *firstAction)
Definition: ui_actions.cpp:726
void onRightClick(uiNode_t *node, int x, int y) override
Calls the script command for a text node that is clickable via right mouse button.
bool UI_ExecuteLuaEventScript_XY(uiNode_t *node, LUA_EVENT event, int x, int y)
Executes a lua event handler with (x,y) argument.
Definition: ui_lua.cpp:143
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
#define EXTRADATACONST(node)
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
struct uiAction_s * onRightClick
Definition: ui_nodes.h:136
QGL_EXTERN GLuint count
Definition: r_gl.h:99
vec4_t selectedColor
Definition: ui_nodes.h:128
union uiSharedData_s::@18 data
const char * UI_GetFontFromNode(const uiNode_t *const node)
Return the font for a specific node or default font.
Definition: ui_font.cpp:145
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
int contentAlign
Definition: ui_nodes.h:120
vec4_t color
Definition: ui_nodes.h:127
const char * extends
Definition: ui_behaviour.h:42
int UI_FontGetHeight(const char *fontID)
Definition: ui_font.cpp:166
vec_t vec2_t[2]
Definition: ufotypes.h:38
linkedList_t * next
Definition: list.h:32
LUA_EVENT lua_onClick
Definition: ui_nodes.h:148
uiBox_t box
Definition: ui_nodes.h:96
void drawText(uiNode_t *node, const linkedList_t *list)
Handles line breaks and drawing for shared data text.
vec_t vec4_t[4]
Definition: ufotypes.h:40