-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
floating_label.hpp
128 lines (100 loc) · 3.69 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
/*
Copyright (C) 2003 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project https://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/surface.hpp"
#include <string>
namespace font {
/// structure 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:
floating_label(const std::string& text, const surface& surface = nullptr);
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);
void set_color(const color_t& color) {color_ = color;}
void set_bg_color(const color_t& bg_color) {
bgcolor_ = bg_color;
bgalpha_ = bg_color.a;
}
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;}
void move(double xmove, double ymove);
void draw(int time, surface screen);
void undraw(surface screen);
surface create_surface();
bool expired(int time) const { return get_time_alive(time) > lifetime_; }
void show(const bool value) { visible_ = value; }
LABEL_SCROLL_MODE scroll() const { return scroll_; }
private:
int get_time_alive(int current_time) const { return current_time - time_start_; }
int xpos(std::size_t width) const;
SDL_Point get_loc(int time);
surface get_surface(int time);
surface surf_, buf_;
SDL_Rect buf_pos_;
bool fadeout_;
int time_start_;
std::string text_;
int font_size_;
color_t color_, bgcolor_;
int bgalpha_;
double xpos_, ypos_, xmove_, ymove_;
int lifetime_;
int width_, height_;
SDL_Rect clip_rect_;
bool visible_;
font::ALIGN align_;
int border_;
LABEL_SCROLL_MODE scroll_;
bool use_markup_;
};
/// 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);
SDL_Rect get_floating_label_rect(int handle);
void draw_floating_labels(surface screen);
void undraw_floating_labels(surface screen);
} // end namespace font