UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_string.cpp
Go to the documentation of this file.
1 
27 /*
28 Copyright (C) 2002-2020 UFO: Alien Invasion.
29 
30 This program is free software; you can redistribute it and/or
31 modify it under the terms of the GNU General Public License
32 as published by the Free Software Foundation; either version 2
33 of the License, or (at your option) any later version.
34 
35 This program is distributed in the hope that it will be useful,
36 but WITHOUT ANY WARRANTY; without even the implied warranty of
37 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
38 
39 See the GNU General Public License for more details.
40 
41 You should have received a copy of the GNU General Public License
42 along with this program; if not, write to the Free Software
43 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 
45 */
46 
47 #include "../ui_nodes.h"
48 #include "../ui_font.h"
49 #include "../ui_parse.h"
50 #include "../ui_behaviour.h"
51 #include "../ui_tooltip.h"
52 #include "../ui_render.h"
53 #include "ui_node_string.h"
54 #include "ui_node_abstractnode.h"
55 
56 #include "../../../common/scripts_lua.h"
57 
58 #define EXTRADATA_TYPE stringExtraData_t
59 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
60 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
61 
63 {
64  vec2_t nodepos;
65  const char* font = UI_GetFontFromNode(node);
66  const char* ref = UI_GetReferenceString(node, node->text);
67  vec_t* color;
68 
69  if (!ref)
70  return;
71  UI_GetNodeAbsPos(node, nodepos);
72 
73  if (node->disabled)
74  color = node->disabledColor;
75  else
76  color = node->color;
77 
78  R_Color(color);
79  if (node->box.size[0] == 0)
80  UI_DrawString(font, (align_t)node->contentAlign, nodepos[0], nodepos[1], nodepos[0], node->box.size[0], 0, ref);
81  else
82  UI_DrawStringInBox(font, (align_t)node->contentAlign, nodepos[0] + node->padding, nodepos[1] + node->padding, node->box.size[0] - node->padding - node->padding, node->box.size[1] - node->padding - node->padding, ref, (longlines_t) EXTRADATA(node).longlines);
83  R_Color(nullptr);
84 }
85 
91 void uiStringNode::drawTooltip (const uiNode_t* node, int x, int y) const
92 {
93  if (node->tooltip) {
94  UI_Tooltip(node, x, y);
95  return;
96  }
97  const char* font = UI_GetFontFromNode(node);
98  const char* text = UI_GetReferenceString(node, node->text);
99  bool isTruncated;
100  if (!text)
101  return;
102 
103  const int maxWidth = node->box.size[0] - node->padding - node->padding;
104  const longlines_t longLines = (longlines_t)EXTRADATACONST(node).longlines;
105  R_FontTextSize(font, text, maxWidth, longLines, nullptr, nullptr, nullptr, &isTruncated);
106  if (!isTruncated)
107  return;
108 
109  const int tooltipWidth = 250;
110  static char tooltiptext[256];
111  tooltiptext[0] = '\0';
112  Q_strcat(tooltiptext, sizeof(tooltiptext), "%s", text);
113  UI_DrawTooltip(tooltiptext, x, y, tooltipWidth);
114 }
115 
117 {
118  node->padding = 3;
119  Vector4Set(node->color, 1.0, 1.0, 1.0, 1.0);
120  Vector4Set(node->disabledColor, 0.5, 0.5, 0.5, 1.0);
121  EXTRADATA(node).longlines = LONGLINES_PRETTYCHOP;
122 }
123 
125 {
126  behaviour->name = "string";
127  behaviour->manager = UINodePtr(new uiStringNode());
128  behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
129  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiStringNode_t *");
130 
131  /* What to do with text lines longer than node width. Default is to wordwrap them to make multiple lines.
132  * It can be LONGLINES_WRAP, LONGLINES_CHOP, LONGLINES_PRETTYCHOP
133  */
134  UI_RegisterExtradataNodeProperty(behaviour, "longlines", V_INT, EXTRADATA_TYPE, longlines);
135 }
vec2_t size
Definition: ui_nodes.h:52
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
const char * name
Definition: ui_behaviour.h:41
float vec_t
Definition: ufotypes.h:37
UINodePtr manager
Definition: ui_behaviour.h:43
char * tooltip
Definition: ui_nodes.h:99
#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
void R_FontTextSize(const char *fontId, const char *text, int maxWidth, longlines_t method, int *width, int *height, int *lines, bool *isTruncated)
Supply information about the size of the text when it is linewrapped and rendered, without actually rendering it. Any of the output parameters may be nullptr.
Definition: r_font.cpp:524
align_t
We need this here for checking the boundaries from script values.
Definition: scripts.h:90
#define Vector4Set(v, r, g, b, a)
Definition: vector.h:62
void UI_RegisterStringNode(uiBehaviour_t *behaviour)
vec4_t disabledColor
Definition: ui_nodes.h:103
SharedPtr< uiNode > UINodePtr
int UI_DrawTooltip(const char *string, int x, int y, int maxWidth)
Generic tooltip function.
Definition: ui_tooltip.cpp:40
int padding
Definition: ui_nodes.h:109
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
Definition: ui_parse.cpp:1406
#define EXTRADATA_TYPE
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
void onLoading(uiNode_t *node) override
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 UI_Tooltip(const uiNode_t *node, int x, int y)
Wrapper for UI tooltips.
Definition: ui_tooltip.cpp:73
intptr_t extraDataSize
Definition: ui_behaviour.h:54
const char * UI_GetFontFromNode(const uiNode_t *const node)
Return the font for a specific node or default font.
Definition: ui_font.cpp:145
node behaviour, how a node work
Definition: ui_behaviour.h:39
int contentAlign
Definition: ui_nodes.h:120
#define EXTRADATA(node)
vec4_t color
Definition: ui_nodes.h:127
void Q_strcat(char *dest, size_t destsize, const char *format,...)
Safely (without overflowing the destination buffer) concatenates two strings.
Definition: shared.cpp:475
vec_t vec2_t[2]
Definition: ufotypes.h:38
Definition: scripts.h:52
int UI_DrawString(const char *fontID, align_t align, int x, int y, int absX, int maxWidth, int lineHeight, const char *c, int boxHeight, int scrollPos, int *curLine, bool increaseLine, longlines_t method)
Definition: ui_render.cpp:371
uiBox_t box
Definition: ui_nodes.h:96
void draw(uiNode_t *node) override
#define EXTRADATACONST(node)
void drawTooltip(const uiNode_t *node, int x, int y) const override
Custom tooltip of string node.
longlines_t
Definition: cl_renderer.h:217
char * text
Definition: ui_nodes.h:121