This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
/
tooltips.cpp
183 lines (148 loc) · 4.02 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
/* $Id: tooltips.cpp 48480 2011-02-12 16:20:24Z ivanovic $ */
/*
Copyright (C) 2003 - 2011 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 "font.hpp"
#include "foreach.hpp"
#include "game_display.hpp"
#include "help.hpp"
#include "marked-up_text.hpp"
#include "resources.hpp"
#include "video.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 = "")
: rect(r), message(msg), action(act)
{}
SDL_Rect rect;
std::string message;
std::string action;
};
std::vector<tooltip> tips;
std::vector<tooltip>::const_iterator current_tooltip = tips.end();
int tooltip_handle = 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);
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()
{
clear_tooltips();
video_ = NULL;
}
void clear_tooltips()
{
clear_tooltip();
tips.clear();
current_tooltip = tips.end();
}
void clear_tooltips(const SDL_Rect& rect)
{
for(std::vector<tooltip>::iterator i = tips.begin(); i != tips.end(); ) {
if(rects_overlap(i->rect,rect)) {
if (i==current_tooltip) {
clear_tooltip();
}
i = tips.erase(i);
current_tooltip = tips.end();
} else {
++i;
}
}
}
void add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action)
{
for(std::vector<tooltip>::iterator i = tips.begin(); i != tips.end(); ++i) {
if(rects_overlap(i->rect,rect)) {
*i = tooltip(rect, message, action);
return;
}
}
tips.push_back(tooltip(rect, message, action));
current_tooltip = tips.end();
}
void process(int mousex, int mousey)
{
for(std::vector<tooltip>::const_iterator i = tips.begin(); i != tips.end(); ++i) {
if(mousex > i->rect.x && mousey > i->rect.y &&
mousex < i->rect.x + i->rect.w && mousey < i->rect.y + i->rect.h) {
if(current_tooltip != i) {
show_tooltip(*i);
current_tooltip = i;
}
return;
}
}
clear_tooltip();
current_tooltip = tips.end();
}
bool click(int mousex, int mousey)
{
foreach(tooltip tip, tips) {
if(!tip.action.empty() && point_in_rect(mousex, mousey, tip.rect)) {
display* disp = resources::screen;
help::show_help(*disp, tip.action);
return true;
}
}
return false;
}
}