The Battle for Wesnoth  1.15.0-dev
floating_label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "color.hpp"
18 #include "sdl/texture.hpp"
19 
20 #include <string>
21 
22 namespace font
23 {
24 /** Helper struct which removes the floating label with the given id upon destruction. */
26 {
29 
30  int id;
31 };
32 
33 /**
34  * Struct which will hide all current floating labels, and cause floating labels
35  * instantiated after it is created to be displayed.
36  */
38 {
41 };
42 
44 
46 
48 {
49 public:
50  explicit floating_label(const std::string& text);
51 
52  void set_font_size(int font_size)
53  {
54  font_size_ = font_size;
55  }
56 
57  /** Set the location on the screen to display the text. */
58  void set_position(double xpos, double ypos)
59  {
60  xpos_ = xpos;
61  ypos_ = ypos;
62  }
63 
64  /** Set the amount to move the text each frame. */
65  void set_move(double xmove, double ymove)
66  {
67  xmove_ = xmove;
68  ymove_ = ymove;
69  }
70 
71  /** Set the number of frames to display the text for, or -1 to display until removed. */
72  void set_lifetime(int lifetime)
73  {
74  lifetime_ = lifetime;
75  alpha_change_ = -255 / lifetime_;
76  }
77 
78  void set_color(const color_t& color)
79  {
80  color_ = color;
81  }
82 
83  void set_bg_color(const color_t& bg_color)
84  {
85  bgcolor_ = bg_color;
86  fill_background_ = bgcolor_.a != 255;
87  }
88 
89  void set_border_size(int border)
90  {
91  border_ = border;
92  }
93 
94  /** Set width for word wrapping (use -1 to disable it). */
95  void set_width(int w)
96  {
97  width_ = w;
98  }
99 
100  void set_height(int h)
101  {
102  height_ = h;
103  }
104 
105  void set_clip_rect(const SDL_Rect& r)
106  {
107  clip_rect_ = r;
108  }
109 
110  void set_alignment(ALIGN align)
111  {
112  align_ = align;
113  }
114 
116  {
117  scroll_ = scroll;
118  }
119 
120  void use_markup(bool b)
121  {
122  use_markup_ = b;
123  }
124 
125  bool expired() const
126  {
127  return lifetime_ == 0;
128  }
129 
130  void show(const bool value)
131  {
132  visible_ = value;
133  }
134 
136  {
137  return scroll_;
138  }
139 
140  void move(double xmove, double ymove);
141 
142  /** Draws this label. */
143  void draw();
144 
145  /** Creates a texture of the label's text. */
146  texture create_texture();
147 
148 private:
149  int xpos(std::size_t width) const;
150 
152 
153  std::string text_;
154 
155  color_t color_, bgcolor_;
156 
157  double xpos_, ypos_, xmove_, ymove_;
158 
159  int width_, height_;
162  int border_;
164 
165  unsigned int current_alpha_;
166 
167  SDL_Rect clip_rect_;
168 
170 
172 
173  bool visible_;
176 };
177 
178 /**
179  * Add a label floating on the screen above everything else.
180  * @returns a handle to the label which can be used with other label functions
181  */
182 int add_floating_label(const floating_label& flabel);
183 
184 /** Moves the floating label given by 'handle' by (xmove, ymove). */
185 void move_floating_label(int handle, double xmove, double ymove);
186 
187 /** Moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP. */
188 void scroll_floating_labels(double xmove, double ymove);
189 
190 /** Removes the floating label given by 'handle' from the screen. */
191 void remove_floating_label(int handle);
192 
193 /** Hides or shows a floating label. */
194 void show_floating_label(int handle, bool show);
195 
196 /** Renders all floating labels. */
197 void draw_floating_labels();
198 
199 SDL_Rect get_floating_label_rect(int handle);
200 
201 } // end namespace font
void draw_floating_labels()
Renders all floating labels.
void set_move(double xmove, double ymove)
Set the amount to move the text each frame.
void set_clip_rect(const SDL_Rect &r)
Collection of helper functions relating to Pango formatting.
void scroll_floating_labels(double xmove, double ymove)
Moves all floating labels that have &#39;scroll_mode&#39; set to ANCHOR_LABEL_MAP.
void show_floating_label(int handle, bool value)
Hides or shows a floating label.
void remove_floating_label(int handle)
Removes the floating label given by &#39;handle&#39; from the screen.
#define h
void set_font_size(int font_size)
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:26
void show(const bool value)
void move_floating_label(int handle, double xmove, double ymove)
Moves the floating label given by &#39;handle&#39; by (xmove, ymove).
#define b
void set_alignment(ALIGN align)
LABEL_SCROLL_MODE scroll() const
void set_lifetime(int lifetime)
Set the number of frames to display the text for, or -1 to display until removed. ...
unsigned int current_alpha_
void set_bg_color(const color_t &bg_color)
uint8_t a
Alpha value.
Definition: color.hpp:186
void set_position(double xpos, double ypos)
Set the location on the screen to display the text.
void set_color(const color_t &color)
void set_scroll_mode(LABEL_SCROLL_MODE scroll)
Struct which will hide all current floating labels, and cause floating labels instantiated after it i...
int add_floating_label(const floating_label &flabel)
Add a label floating on the screen above everything else.
SDL_Rect get_floating_label_rect(int handle)
int w
void set_border_size(int border)
Helper struct which removes the floating label with the given id upon destruction.
void set_width(int w)
Set width for word wrapping (use -1 to disable it).
LABEL_SCROLL_MODE scroll_
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
void show(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Shows a tip.
Definition: tooltip.cpp:153