UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_tooltip.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 "../cl_shared.h"
26#include "../input/cl_keys.h"
27#include "node/ui_node_window.h"
28#include "ui_tooltip.h"
29#include "ui_nodes.h"
30#include "ui_parse.h"
31#include "ui_render.h"
32#include "ui_input.h"
33
34static const vec4_t tooltipBG = { 0.0f, 0.0f, 0.0f, 0.7f };
35static const vec4_t tooltipColor = { 1.0f, 1.0f, 1.0f, 1.0f };
36
40int UI_DrawTooltip (const char* string, int x, int y, int maxWidth)
41{
42 const char* font = "f_small";
43 int height = 0, width = 0;
44
45 if (Q_strnull(string) || !font)
46 return 0;
47
48 R_FontTextSize(font, string, maxWidth, LONGLINES_WRAP, &width, &height, nullptr, nullptr);
49
50 if (!width)
51 return 0;
52
53 x += 5;
54 y += 5;
55
56 if (x + width + 3 > VID_NORM_WIDTH)
57 x -= width + 10;
58
59 if (y + height + 3 > VID_NORM_HEIGHT)
60 y -= height + 10;
61
62 UI_DrawFill(x - 1, y - 1, width + 4, height + 4, tooltipBG);
64 UI_DrawString(font, ALIGN_UL, x + 1, y + 1, x + 1, maxWidth, 0, string);
65 R_Color(nullptr);
66
67 return width;
68}
69
73void UI_Tooltip (const uiNode_t* node, int x, int y)
74{
75 const char* string;
76 const char* key = nullptr;
77 const char* tooltip = nullptr;
78 static const int maxWidth = 200;
79
80 /* check values */
81 if (node->key)
82 key = Key_KeynumToString(node->key->key);
83 if (node->tooltip)
84 tooltip = UI_GetReferenceString(node, node->tooltip);
85
86 /* normalize */
87 if (tooltip && tooltip[0] == '\0')
88 tooltip = nullptr;
89 if (key && key[0] == '\0')
90 key = nullptr;
91
92 /* create tooltip */
93 if (key && tooltip) {
94 char buf[MAX_VAR];
95 Com_sprintf(buf, sizeof(buf), _("Key: %s"), key);
96 string = va("%s\n%s", tooltip, buf);
97 } else if (tooltip) {
98 string = tooltip;
99 } else if (key) {
100 string = va(_("Key: %s"), key);
101 } else {
102 return;
103 }
104
105 UI_DrawTooltip(string, x, y, maxWidth);
106}
unsigned int key
Definition cl_input.cpp:64
const char * Key_KeynumToString(int keynum)
Convert a given keynum to string.
Definition cl_keys.cpp:485
Header file for keyboard handler.
#define VID_NORM_WIDTH
Definition cl_renderer.h:40
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_WRAP
#define VID_NORM_HEIGHT
Definition cl_renderer.h:41
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition r_state.cpp:1011
Share stuff between the different cgame implementations.
#define _(String)
Definition cl_shared.h:44
voidpf void * buf
Definition ioapi.h:42
@ ALIGN_UL
Definition scripts.h:90
bool Q_strnull(const char *string)
Definition shared.h:138
#define MAX_VAR
Definition shared.h:36
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition shared.cpp:494
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don't need to have varargs versions of all text functi...
Definition shared.cpp:410
Atomic structure used to define most of the UI.
Definition ui_nodes.h:80
char * tooltip
Definition ui_nodes.h:99
struct uiKeyBinding_s * key
Definition ui_nodes.h:100
vec_t vec4_t[4]
Definition ufotypes.h:40
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)
void UI_DrawFill(int x, int y, int w, int h, const vec4_t color)
Fills a box of pixels with a single color.
Definition ui_render.cpp:37
static const vec4_t tooltipColor
static const vec4_t tooltipBG
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.