UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_tooltip.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 "../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 
34 static const vec4_t tooltipBG = { 0.0f, 0.0f, 0.0f, 0.7f };
35 static const vec4_t tooltipColor = { 1.0f, 1.0f, 1.0f, 1.0f };
36 
40 int 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 
73 void 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 }
bool Q_strnull(const char *string)
Definition: shared.h:138
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
#define _(String)
Definition: cl_shared.h:43
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
char * tooltip
Definition: ui_nodes.h:99
voidpf void * buf
Definition: ioapi.h:42
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
unsigned int key
Definition: cl_input.cpp:68
#define MAX_VAR
Definition: shared.h:36
int UI_DrawTooltip(const char *string, int x, int y, int maxWidth)
Generic tooltip function.
Definition: ui_tooltip.cpp:40
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
Definition: ui_parse.cpp:1406
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
#define VID_NORM_WIDTH
Definition: cl_renderer.h:40
void UI_Tooltip(const uiNode_t *node, int x, int y)
Wrapper for UI tooltips.
Definition: ui_tooltip.cpp:73
#define VID_NORM_HEIGHT
Definition: cl_renderer.h:41
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
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
const char * Key_KeynumToString(int keynum)
Convert a given keynum to string.
Definition: cl_keys.cpp:485
static const vec4_t tooltipColor
Definition: ui_tooltip.cpp:35
static const vec4_t tooltipBG
Definition: ui_tooltip.cpp:34
struct uiKeyBinding_s * key
Definition: ui_nodes.h:100
vec_t vec4_t[4]
Definition: ufotypes.h:40