The Battle for Wesnoth  1.17.14+dev
floating_label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "color.hpp"
19 #include "sdl/point.hpp"
20 #include "sdl/rect.hpp"
21 #include "sdl/surface.hpp"
22 #include "sdl/texture.hpp"
23 #include <string>
24 
25 namespace font {
26 
27 /**
28  * structure which will hide all current floating labels, and cause floating labels
29  * instantiated after it is created to be displayed
30  */
32 {
35 };
36 
38 
40 
42 {
43 public:
44  floating_label(const std::string& text, const surface& surface = nullptr);
45 
46  void set_font_size(int font_size) {font_size_ = font_size;}
47 
48  // set the location on the screen to display the text.
49  void set_position(double xpos, double ypos){
50  xpos_ = xpos;
51  ypos_ = ypos;
52  }
53  // set the amount to move the text each frame
54  void set_move(double xmove, double ymove){
55  xmove_ = xmove;
56  ymove_ = ymove;
57  }
58  // set the number of frames to display the text for, or -1 to display until removed
59  void set_lifetime(int lifetime, int fadeout = 100);
60  void set_color(const color_t& color) {color_ = color;}
61  void set_bg_color(const color_t& bg_color) {
62  bgcolor_ = bg_color;
63  }
64  void set_border_size(int border) {border_ = border;}
65  // set width for word wrapping (use -1 to disable it)
66  void set_width(int w) {width_ = w;}
67  void set_height(int h) { height_ = h; }
68  void set_clip_rect(const SDL_Rect& r) {clip_rect_ = r;}
69  void set_alignment(ALIGN align) {align_ = align;}
70  void set_scroll_mode(LABEL_SCROLL_MODE scroll) {scroll_ = scroll;}
71  void use_markup(bool b) {use_markup_ = b;}
72 
73  /** Mark the last drawn location as requiring redraw. */
74  void undraw();
75  /** Change the floating label's position. */
76  void move(double xmove, double ymove);
77  /** Finalize draw position and alpha, and queue redrawing if changed. */
78  void update(int time);
79  /** Draw the label to the screen. */
80  void draw();
81 
82  /**
83  * Ensure a texture for this floating label exists, creating one if needed.
84  *
85  * @returns true if the texture exists, false in the case of failure.
86  */
87  bool create_texture();
88 
89  /** Return the size of the label in drawing coordinates */
90  SDL_Point get_draw_size() const
91  {
92  return get_bg_rect({0, 0, tex_.w(), tex_.h()}).size();
93  }
94 
95  bool expired(int time) const { return lifetime_ >= 0 && get_time_alive(time) > lifetime_ + fadeout_; }
96 
97  void show(const bool value) { visible_ = value; }
98 
99  LABEL_SCROLL_MODE scroll() const { return scroll_; }
100 
101  // TODO: Might be good to have more getters, right?
102  int get_fade_time() const { return fadeout_; }
103 
104 private:
105 
106  int get_time_alive(int current_time) const { return current_time - time_start_; }
107  int xpos(std::size_t width) const;
108  point get_pos(int time);
109  uint8_t get_alpha(int time);
110  rect get_bg_rect(const rect& text_rect) const;
113  uint8_t alpha_;
114  int fadeout_;
116  std::string text_;
118  color_t color_, bgcolor_;
119  double xpos_, ypos_, xmove_, ymove_;
121  int width_, height_;
122  SDL_Rect clip_rect_;
123  bool visible_;
125  int border_;
128 };
129 
130 
131 /**
132  * add a label floating on the screen above everything else.
133  * @returns a handle to the label which can be used with other label functions
134  */
135 int add_floating_label(const floating_label& flabel);
136 
137 
138 /** moves the floating label given by 'handle' by (xmove,ymove) */
139 void move_floating_label(int handle, double xmove, double ymove);
140 
141 /** moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP */
142 void scroll_floating_labels(double xmove, double ymove);
143 
144 /** removes the floating label given by 'handle' from the screen */
145 /** if fadeout is given, the label fades out over that duration */
146 /** if fadeout is less than 0, it uses the fadeout setting from the label */
147 void remove_floating_label(int handle, int fadeout = 0);
148 
149 /** hides or shows a floating label */
150 void show_floating_label(int handle, bool show);
151 
152 SDL_Rect get_floating_label_rect(int handle);
153 void draw_floating_labels();
155 
156 } // end namespace font
void draw_floating_labels()
void remove_floating_label(int handle, int fadeout)
removes the floating label given by &#39;handle&#39; from the screen
void set_move(double xmove, double ymove)
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 update_floating_labels()
#define h
void set_font_size(int font_size)
int get_time_alive(int current_time) const
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:32
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
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
void set_alignment(ALIGN align)
LABEL_SCROLL_MODE scroll() const
bool expired(int time) const
void set_bg_color(const color_t &bg_color)
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:58
void set_position(double xpos, double ypos)
void set_color(const color_t &color)
void set_scroll_mode(LABEL_SCROLL_MODE scroll)
static void update()
structure which will hide all current floating labels, and cause floating labels instantiated after i...
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:46
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
Holds a 2D point.
Definition: point.hpp:24
SDL_Rect get_floating_label_rect(int handle)
int w
void set_border_size(int border)
Contains the SDL_Rect helper code.
Definition: draw.hpp:42
LABEL_SCROLL_MODE scroll_
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
SDL_Point get_draw_size() const
Return the size of the label in drawing coordinates.
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:81