Atrinik Client  4.0
tooltip.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (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. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
32 #include <global.h>
33 
35 static char tooltip_text[HUGE_BUF * 4];
39 static int tooltip_x = -1;
41 static int tooltip_y = -1;
42 static int tooltip_h = -1;
43 static int tooltip_w = -1;
44 static uint32_t tooltip_created = 0;
45 static uint32_t tooltip_delay = 0;
46 static uint8_t tooltip_opacity = 0;
47 
60 void tooltip_create(int mx, int my, font_struct *font, const char *text)
61 {
62  if (tooltip_font != NULL) {
63  font_free(tooltip_font);
64  }
65 
66  tooltip_delay = 0;
67  FONT_INCREF(font);
68  tooltip_font = font;
69  tooltip_x = mx;
70  tooltip_y = my;
71  tooltip_w = -1;
72  tooltip_h = -1;
73  strncpy(tooltip_text, text, sizeof(tooltip_text) - 1);
74  tooltip_text[sizeof(tooltip_text) - 1] = '\0';
75 }
76 
82 void tooltip_enable_delay(uint32_t delay)
83 {
84  tooltip_delay = delay;
85 
86  if (tooltip_created + delay < SDL_GetTicks()) {
87  tooltip_created = SDL_GetTicks();
88  }
89 }
90 
96 void tooltip_multiline(int max_width)
97 {
98  SDL_Rect box;
99 
100  box.x = 0;
101  box.y = 0;
102  box.w = max_width;
103  box.h = 0;
104  text_show(NULL, tooltip_font, tooltip_text, 3, 0, COLOR_WHITE, TEXT_MARKUP | TEXT_WORD_WRAP | TEXT_HEIGHT, &box);
105  tooltip_w = max_width;
106  tooltip_h = box.h;
107 
108  box.h = 0;
109  text_show(NULL, tooltip_font, tooltip_text, 3, 0, COLOR_WHITE, TEXT_MARKUP | TEXT_WORD_WRAP | TEXT_MAX_WIDTH, &box);
110  tooltip_w = box.w;
111 }
112 
116 void tooltip_show(void)
117 {
118  SDL_Rect box, text_box;
119 
120  /* No tooltip to show. */
121  if (tooltip_x == -1 || tooltip_y == -1) {
122  return;
123  }
124 
125  if (tooltip_delay) {
126  if (SDL_GetTicks() - tooltip_created < tooltip_delay) {
127  tooltip_opacity = 0;
128  return;
129  }
130 
131  tooltip_created = SDL_GetTicks() + tooltip_delay;
132  } else {
133  tooltip_opacity = 255;
134  }
135 
136  text_box.w = tooltip_w;
137  text_box.h = tooltip_h;
138 
139  if (tooltip_w == -1 || tooltip_h == -1) {
140  text_get_width_height(tooltip_font,
141  tooltip_text,
142  TEXT_MARKUP,
143  &text_box,
144  tooltip_w == -1 ? &text_box.w : NULL,
145  tooltip_h == -1 ? &text_box.h : NULL);
146  }
147 
148  /* Generate the tooltip's background. */
149  box.x = tooltip_x + 9;
150  box.y = tooltip_y + 17;
151  box.w = text_box.w + 4;
152  box.h = text_box.h;
153 
154  /* Push the tooltip to the left if it would go beyond maximum screen
155  * size. */
156  if (box.x + box.w >= ScreenSurface->w) {
157  box.x -= (box.x + box.w + 1) - ScreenSurface->w;
158  }
159 
160  if (box.y + box.h >= ScreenSurface->h) {
161  box.y -= (box.y + box.h + 1) - ScreenSurface->h;
162  }
163 
164  boxRGBA(ScreenSurface, box.x, box.y, box.x + box.w, box.y + box.h, 255, 255, 255, tooltip_opacity);
165  text_show_format(ScreenSurface, tooltip_font, box.x + 3, box.y, COLOR_BLACK, TEXT_MARKUP | TEXT_WORD_WRAP, &text_box, "[alpha=%d]%s[/alpha]", tooltip_opacity, tooltip_text);
166 
167  if (tooltip_delay) {
168  tooltip_opacity = MIN(255, tooltip_opacity + 25);
169  }
170 }
171 
175 void tooltip_dismiss(void)
176 {
177  tooltip_x = -1;
178  tooltip_y = -1;
179  tooltip_w = -1;
180  tooltip_h = -1;
181 
182  if (tooltip_font != NULL) {
183  font_free(tooltip_font);
184  tooltip_font = NULL;
185  }
186 }
187 
197 {
198  if (tooltip_x == -1 || tooltip_y == -1) {
199  return 0;
200  }
201 
202  return 1;
203 }
#define COLOR_BLACK
Definition: text.h:323
#define TEXT_WORD_WRAP
Definition: text.h:226
void text_show(SDL_Surface *surface, font_struct *font, const char *text, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box)
Definition: text.c:1983
static font_struct * tooltip_font
Definition: tooltip.c:37
int tooltip_need_redraw(void)
Definition: tooltip.c:196
void tooltip_create(int mx, int my, font_struct *font, const char *text)
Definition: tooltip.c:60
static char tooltip_text[HUGE_BUF *4]
Definition: tooltip.c:35
void font_free(font_struct *font)
Definition: text.c:262
SDL_Surface * ScreenSurface
Definition: main.c:47
#define TEXT_MARKUP
Definition: text.h:224
static int tooltip_y
Definition: tooltip.c:41
void tooltip_show(void)
Definition: tooltip.c:116
void text_get_width_height(font_struct *font, const char *text, uint64_t flags, SDL_Rect *box, uint16_t *w, uint16_t *h)
Definition: text.c:2410
#define FONT_INCREF(font)
Definition: text.h:67
#define TEXT_HEIGHT
Definition: text.h:241
void tooltip_enable_delay(uint32_t delay)
Definition: tooltip.c:82
void tooltip_dismiss(void)
Definition: tooltip.c:175
static int tooltip_x
Definition: tooltip.c:39
void tooltip_multiline(int max_width)
Definition: tooltip.c:96
void text_show_format(SDL_Surface *surface, font_struct *font, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box, const char *format,...)
Definition: text.c:2289
#define TEXT_MAX_WIDTH
Definition: text.h:277
#define COLOR_WHITE
Definition: text.h:289