-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
tooltips.cpp
230 lines (186 loc) · 5.23 KB
/
tooltips.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "tooltips.hpp"
#include "floating_label.hpp"
#include "font.hpp"
#include "game_display.hpp"
#include "help/help.hpp"
#include "marked-up_text.hpp"
#include "resources.hpp"
#include "video.hpp"
#include <SDL.h> // Travis doesn't like this, although it works on my machine -> '#include <SDL_sound.h>
#include <boost/foreach.hpp>
namespace {
CVideo* video_ = NULL;
static const int font_size = font::SIZE_SMALL;
static const int text_width = 400;
struct tooltip
{
tooltip(const SDL_Rect& r, const std::string& msg, const std::string& act = "", bool use_markup = false, const surface& fg = surface())
: rect(r), message(msg), action(act), markup(use_markup), foreground(fg)
{}
SDL_Rect rect;
std::string message;
std::string action;
bool markup;
surface foreground;
};
std::map<int, tooltip> tips;
std::map<int, tooltip>::const_iterator current_tooltip = tips.end();
int tooltip_handle = 0;
int tooltip_id = 0;
surface current_background = NULL;
}
static void clear_tooltip()
{
if(tooltip_handle != 0) {
font::remove_floating_label(tooltip_handle);
tooltip_handle = 0;
}
}
static void show_tooltip(const tooltip& tip)
{
if(video_ == NULL) {
return;
}
clear_tooltip();
const SDL_Color bgcolor = {0,0,0,160};
SDL_Rect area = screen_area();
unsigned int border = 10;
font::floating_label flabel(tip.message, tip.foreground);
flabel.use_markup(tip.markup);
flabel.set_font_size(font_size);
flabel.set_color(font::NORMAL_COLOR);
flabel.set_clip_rect(area);
flabel.set_width(text_width);
flabel.set_alignment(font::LEFT_ALIGN);
flabel.set_bg_color(bgcolor);
flabel.set_border_size(border);
tooltip_handle = font::add_floating_label(flabel);
SDL_Rect rect = font::get_floating_label_rect(tooltip_handle);
//see if there is enough room to fit it above the tip area
if(tip.rect.y > rect.h) {
rect.y = tip.rect.y - rect.h;
} else {
rect.y = tip.rect.y + tip.rect.h;
}
rect.x = tip.rect.x;
if(rect.x < 0) {
rect.x = 0;
} else if(rect.x + rect.w > area.w) {
rect.x = area.w - rect.w;
}
font::move_floating_label(tooltip_handle,rect.x,rect.y);
}
namespace tooltips {
manager::manager(CVideo& video)
{
clear_tooltips();
video_ = &video;
}
manager::~manager()
{
try {
clear_tooltips();
} catch (...) {}
video_ = NULL;
}
void clear_tooltips()
{
clear_tooltip();
tips.clear();
current_tooltip = tips.end();
}
void clear_tooltips(const SDL_Rect& rect)
{
for(std::map<int,tooltip>::iterator i = tips.begin(); i != tips.end(); ) {
if(sdl::rects_overlap(i->second.rect,rect)) {
if (i==current_tooltip) {
clear_tooltip();
}
tips.erase(i++);
current_tooltip = tips.end();
} else {
++i;
}
}
}
bool update_tooltip(int id, const SDL_Rect& rect, const std::string& message,
const std::string& action, bool use_markup)
{
std::map<int, tooltip>::iterator it = tips.find(id);
if (it == tips.end() ) return false;
it->second.action = action;
it->second.markup = use_markup;
it->second.message = message;
it->second.rect = rect;
return true;
}
bool update_tooltip(int id, const SDL_Rect& rect, const std::string& message,
const std::string& action, bool use_markup, const surface& foreground)
{
std::map<int, tooltip>::iterator it = tips.find(id);
if (it == tips.end() ) return false;
it->second.action = action;
it->second.foreground = foreground;
it->second.markup = use_markup;
it->second.message = message;
it->second.rect = rect;
return true;
}
void remove_tooltip(int id)
{
tips.erase(id);
clear_tooltip();
}
int add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action, bool use_markup, const surface& foreground)
{
for(std::map<int, tooltip>::iterator it = tips.begin(); it != tips.end();) {
if(sdl::rects_overlap(it->second.rect,rect)) {
tips.erase(it++);
} else {
++it;
}
}
int id = tooltip_id++;
tips.insert(std::make_pair(id, tooltip(rect, message, action, use_markup, foreground) ));
current_tooltip = tips.end();
return id;
}
void process(int mousex, int mousey)
{
for(std::map<int, tooltip>::const_iterator i = tips.begin(); i != tips.end(); ++i) {
if(mousex > i->second.rect.x && mousey > i->second.rect.y &&
mousex < i->second.rect.x + i->second.rect.w && mousey < i->second.rect.y + i->second.rect.h) {
if(current_tooltip != i) {
show_tooltip(i->second);
current_tooltip = i;
}
return;
}
}
clear_tooltip();
current_tooltip = tips.end();
}
bool click(int mousex, int mousey)
{
for(std::map<int, tooltip>::const_iterator i = tips.begin(); i != tips.end(); ++i) {
if(!i->second.action.empty() && sdl::point_in_rect(mousex, mousey, i->second.rect)) {
display* disp = resources::screen;
help::show_help(disp->video(), i->second.action);
return true;
}
}
return false;
}
}