UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_string.cpp
Go to the documentation of this file.
1
26
27/*
28Copyright (C) 2002-2025 UFO: Alien Invasion.
29
30This program is free software; you can redistribute it and/or
31modify it under the terms of the GNU General Public License
32as published by the Free Software Foundation; either version 2
33of the License, or (at your option) any later version.
34
35This program is distributed in the hope that it will be useful,
36but WITHOUT ANY WARRANTY; without even the implied warranty of
37MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
38
39See the GNU General Public License for more details.
40
41You should have received a copy of the GNU General Public License
42along with this program; if not, write to the Free Software
43Foundation, 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"
55
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
91void 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}
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,...
Definition r_font.cpp:522
longlines_t
@ LONGLINES_PRETTYCHOP
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition r_state.cpp:1011
void drawTooltip(const uiNode_t *node, int x, int y) const override
Custom tooltip of string node.
void draw(uiNode_t *node) override
void onLoading(uiNode_t *node) override
align_t
We need this here for checking the boundaries from script values.
Definition scripts.h:89
@ V_INT
Definition scripts.h:52
Header for lua script functions.
void Q_strcat(char *dest, size_t destsize, const char *format,...)
Safely (without overflowing the destination buffer) concatenates two strings.
Definition shared.cpp:475
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
bool disabled
Definition ui_nodes.h:102
char * tooltip
Definition ui_nodes.h:99
char * text
Definition ui_nodes.h:121
int contentAlign
Definition ui_nodes.h:120
vec4_t color
Definition ui_nodes.h:127
uiBox_t box
Definition ui_nodes.h:96
int padding
Definition ui_nodes.h:109
vec4_t disabledColor
Definition ui_nodes.h:103
float vec_t
Definition ufotypes.h:37
vec_t vec2_t[2]
Definition ufotypes.h:38
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
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_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)
#define EXTRADATACONST(node)
void UI_RegisterStringNode(uiBehaviour_t *behaviour)
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
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)
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
void UI_Tooltip(const uiNode_t *node, int x, int y)
Wrapper for UI tooltips.
int UI_DrawTooltip(const char *string, int x, int y, int maxWidth)
Generic tooltip function.
#define Vector4Set(v, r, g, b, a)
Definition vector.h:62