The Battle for Wesnoth  1.19.13+dev
floating_label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
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/texture.hpp"
22 
23 #include <chrono>
24 #include <string>
25 
26 namespace font {
27 
28 /**
29  * structure which will hide all current floating labels, and cause floating labels
30  * instantiated after it is created to be displayed
31  */
33 {
36 };
37 
39 
41 
43 {
44  using clock = std::chrono::steady_clock;
45 
46 public:
47  floating_label(const std::string& text);
48 
49  void set_font_size(int font_size) {font_size_ = font_size;}
50 
51  // set the location on the screen to display the text.
52  void set_position(double xpos, double ypos){
53  xpos_ = xpos;
54  ypos_ = ypos;
55  }
56  // set the amount to move the text each millisecond
57  void set_move(double xmove, double ymove){
58  xmove_ = xmove;
59  ymove_ = ymove;
60  }
61  // set the number of milliseconds to display the text for, or -1 to display until removed
62  void set_lifetime(const std::chrono::milliseconds& lifetime, const std::chrono::milliseconds& fadeout = std::chrono::milliseconds{100});
63  void set_color(const color_t& color) {color_ = color;}
64  void set_bg_color(const color_t& bg_color) {
65  bgcolor_ = bg_color;
66  }
67  void set_border_size(int border) {border_ = border;}
68  // set width for word wrapping (use -1 to disable it)
69  void set_width(int w) {width_ = w;}
70  void set_height(int h) { height_ = h; }
71  void set_clip_rect(const rect& r) {clip_rect_ = r;}
72  void set_alignment(ALIGN align) {align_ = align;}
74  void use_markup(bool b) {use_markup_ = b;}
75 
76  /** Mark the last drawn location as requiring redraw. */
77  void undraw();
78  /** Change the floating label's position. */
79  void move(double xmove, double ymove);
80  /** Finalize draw position and alpha, and queue redrawing if changed. */
81  void update(const clock::time_point& time);
82  /** Draw the label to the screen. */
83  void draw();
84 
85  /**
86  * Ensure a texture for this floating label exists, creating one if needed.
87  *
88  * @returns true if the texture exists, false in the case of failure.
89  */
90  bool create_texture();
91 
92  void clear_texture();
93 
94  /** Return the size of the label in drawing coordinates */
96  {
97  return get_bg_rect({0, 0, tex_.w(), tex_.h()}).size();
98  }
99 
100  bool expired(const clock::time_point& time) const
101  {
102  return lifetime_ >= std::chrono::milliseconds{0} && get_time_alive(time) > lifetime_ + fadeout_;
103  }
104 
105  void show(const bool value) { visible_ = value; }
106 
107  LABEL_SCROLL_MODE scroll() const { return scroll_; }
108 
109  // TODO: Might be good to have more getters, right?
110  auto get_fade_time() const { return fadeout_; }
111 
112 private:
113 
114  clock::duration get_time_alive(const clock::time_point& current_time) const;
115  int xpos(std::size_t width) const;
116  point get_pos(const clock::time_point& time) const;
117  uint8_t get_alpha(const clock::time_point& time) const;
118  rect get_bg_rect(const rect& text_rect) const;
121  uint8_t alpha_;
122  std::chrono::milliseconds fadeout_;
123  clock::time_point time_start_;
124  std::string text_;
128  std::chrono::milliseconds lifetime_;
131  bool visible_;
133  int border_;
136 };
137 
138 
139 /**
140  * add a label floating on the screen above everything else.
141  * @returns a handle to the label which can be used with other label functions
142  */
143 int add_floating_label(const floating_label& flabel);
144 
145 
146 /** moves the floating label given by 'handle' by (xmove,ymove) */
147 void move_floating_label(int handle, double xmove, double ymove);
148 
149 /** moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP */
150 void scroll_floating_labels(double xmove, double ymove);
151 
152 /** removes the floating label given by 'handle' from the screen */
153 /** if fadeout is given, the label fades out over that duration */
154 /** if fadeout is less than 0, it uses the fadeout setting from the label */
155 void remove_floating_label(int handle, const std::chrono::milliseconds& fadeout = std::chrono::milliseconds{0});
156 
157 /** hides or shows a floating label */
158 void show_floating_label(int handle, bool show);
159 
160 // TODO: refactor out. This only gives size, not position
162 void draw_floating_labels();
164 
165 } // end namespace font
point get_draw_size() const
Return the size of the label in drawing coordinates.
std::chrono::milliseconds lifetime_
uint8_t get_alpha(const clock::time_point &time) const
void show(const bool value)
void set_move(double xmove, double ymove)
rect get_bg_rect(const rect &text_rect) const
LABEL_SCROLL_MODE scroll_
point get_pos(const clock::time_point &time) const
LABEL_SCROLL_MODE scroll() const
auto get_fade_time() const
clock::time_point time_start_
bool create_texture()
Ensure a texture for this floating label exists, creating one if needed.
void set_position(double xpos, double ypos)
floating_label(const std::string &text)
void set_alignment(ALIGN align)
int xpos(std::size_t width) const
void update(const clock::time_point &time)
Finalize draw position and alpha, and queue redrawing if changed.
void set_lifetime(const std::chrono::milliseconds &lifetime, const std::chrono::milliseconds &fadeout=std::chrono::milliseconds{100})
std::chrono::milliseconds fadeout_
void set_color(const color_t &color)
void set_border_size(int border)
void move(double xmove, double ymove)
Change the floating label's position.
void set_bg_color(const color_t &bg_color)
void set_clip_rect(const rect &r)
void set_scroll_mode(LABEL_SCROLL_MODE scroll)
bool expired(const clock::time_point &time) const
clock::duration get_time_alive(const clock::time_point &current_time) const
std::chrono::steady_clock clock
void draw()
Draw the label to the screen.
void undraw()
Mark the last drawn location as requiring redraw.
void set_font_size(int font_size)
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
int w() const
The draw-space width of the texture, in pixels.
Definition: texture.hpp:103
int h() const
The draw-space height of the texture, in pixels.
Definition: texture.hpp:112
@ border
The border of the map.
int w
Graphical text output.
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
rect get_floating_label_rect(int handle)
void show_floating_label(int handle, bool value)
hides or shows a floating label
void scroll_floating_labels(double xmove, double ymove)
moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP
void remove_floating_label(int handle, const std::chrono::milliseconds &fadeout)
removes the floating label given by 'handle' from the screen
void update_floating_labels()
void move_floating_label(int handle, double xmove, double ymove)
moves the floating label given by 'handle' by (xmove,ymove)
void draw_floating_labels()
@ CENTER_ALIGN
@ ANCHOR_LABEL_SCREEN
@ ANCHOR_LABEL_MAP
void show(const gui2::tracked_drawable &target)
Displays the fps report popup for the given tracked_drawable.
Definition: fps_report.cpp:131
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
Contains the SDL_Rect helper code.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:61
structure which will hide all current floating labels, and cause floating labels instantiated after i...
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
#define h
#define b