-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
floating_label.hpp
192 lines (145 loc) · 3.7 KB
/
floating_label.hpp
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
/*
Copyright (C) 2003 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.
*/
#pragma once
#include "color.hpp"
#include "sdl/texture.hpp"
#include <string>
namespace font
{
/**
* Struct which will hide all current floating labels, and cause floating labels
* instantiated after it is created to be displayed.
*/
struct floating_label_context
{
floating_label_context();
~floating_label_context();
};
enum ALIGN { LEFT_ALIGN, CENTER_ALIGN, RIGHT_ALIGN };
enum LABEL_SCROLL_MODE { ANCHOR_LABEL_SCREEN, ANCHOR_LABEL_MAP };
class floating_label
{
public:
explicit floating_label(const std::string& text);
void set_font_size(int font_size)
{
font_size_ = font_size;
}
/** Set the location on the screen to display the text. */
void set_position(double xpos, double ypos)
{
xpos_ = xpos;
ypos_ = ypos;
}
/** Set the amount to move the text each frame. */
void set_move(double xmove, double ymove)
{
xmove_ = xmove;
ymove_ = ymove;
}
/** Set the number of frames to display the text for, or -1 to display until removed. */
void set_lifetime(int lifetime)
{
lifetime_ = lifetime;
alpha_change_ = -255 / lifetime_;
}
void set_color(const color_t& color)
{
color_ = color;
}
void set_bg_color(const color_t& bg_color)
{
bgcolor_ = bg_color;
fill_background_ = bgcolor_.a != 255;
}
void set_border_size(int border)
{
border_ = border;
}
/** Set width for word wrapping (use -1 to disable it). */
void set_width(int w)
{
width_ = w;
}
void set_height(int h)
{
height_ = h;
}
void set_clip_rect(const SDL_Rect& r)
{
clip_rect_ = r;
}
void set_alignment(ALIGN align)
{
align_ = align;
}
void set_scroll_mode(LABEL_SCROLL_MODE scroll)
{
scroll_ = scroll;
}
void use_markup(bool b)
{
use_markup_ = b;
}
bool expired() const
{
return lifetime_ == 0;
}
void show(const bool value)
{
visible_ = value;
}
LABEL_SCROLL_MODE scroll() const
{
return scroll_;
}
void move(double xmove, double ymove);
/** Draws this label. */
void draw();
/** Creates a texture of the label's text. */
texture create_texture();
private:
int xpos(size_t width) const;
texture texture_;
std::string text_;
color_t color_, bgcolor_;
double xpos_, ypos_, xmove_, ymove_;
int width_, height_;
int font_size_;
int lifetime_;
int border_;
int alpha_change_;
unsigned int current_alpha_;
SDL_Rect clip_rect_;
font::ALIGN align_;
LABEL_SCROLL_MODE scroll_;
bool visible_;
bool use_markup_;
bool fill_background_;
};
/**
* Add a label floating on the screen above everything else.
* @returns a handle to the label which can be used with other label functions
*/
int add_floating_label(const floating_label& flabel);
/** Moves the floating label given by 'handle' by (xmove, ymove). */
void move_floating_label(int handle, double xmove, double ymove);
/** Moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP. */
void scroll_floating_labels(double xmove, double ymove);
/** Removes the floating label given by 'handle' from the screen. */
void remove_floating_label(int handle);
/** Hides or shows a floating label. */
void show_floating_label(int handle, bool show);
/** Renders all floating labels. */
void draw_floating_labels();
SDL_Rect get_floating_label_rect(int handle);
} // end namespace font