The Battle for Wesnoth  1.19.4+dev
floating_label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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);
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;}
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  void clear_texture();
90 
91  /** Return the size of the label in drawing coordinates */
92  SDL_Point get_draw_size() const
93  {
94  return get_bg_rect({0, 0, tex_.w(), tex_.h()}).size();
95  }
96 
97  bool expired(int time) const { return lifetime_ >= 0 && get_time_alive(time) > lifetime_ + fadeout_; }
98 
99  void show(const bool value) { visible_ = value; }
100 
101  LABEL_SCROLL_MODE scroll() const { return scroll_; }
102 
103  // TODO: Might be good to have more getters, right?
104  int get_fade_time() const { return fadeout_; }
105 
106 private:
107 
108  int get_time_alive(int current_time) const { return current_time - time_start_; }
109  int xpos(std::size_t width) const;
110  point get_pos(int time);
111  uint8_t get_alpha(int time);
112  rect get_bg_rect(const rect& text_rect) const;
115  uint8_t alpha_;
116  int fadeout_;
118  std::string text_;
124  SDL_Rect clip_rect_;
125  bool visible_;
127  int border_;
130 };
131 
132 
133 /**
134  * add a label floating on the screen above everything else.
135  * @returns a handle to the label which can be used with other label functions
136  */
137 int add_floating_label(const floating_label& flabel);
138 
139 
140 /** moves the floating label given by 'handle' by (xmove,ymove) */
141 void move_floating_label(int handle, double xmove, double ymove);
142 
143 /** moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP */
144 void scroll_floating_labels(double xmove, double ymove);
145 
146 /** removes the floating label given by 'handle' from the screen */
147 /** if fadeout is given, the label fades out over that duration */
148 /** if fadeout is less than 0, it uses the fadeout setting from the label */
149 void remove_floating_label(int handle, int fadeout = 0);
150 
151 /** hides or shows a floating label */
152 void show_floating_label(int handle, bool show);
153 
154 SDL_Rect get_floating_label_rect(int handle);
155 void draw_floating_labels();
157 
158 } // end namespace font
SDL_Point get_draw_size() const
Return the size of the label in drawing coordinates.
uint8_t get_alpha(int time)
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_
void update(int time)
Finalize draw position and alpha, and queue redrawing if changed.
bool expired(int time) const
void set_lifetime(int lifetime, int fadeout=100)
point get_pos(int time)
LABEL_SCROLL_MODE scroll() const
int get_time_alive(int current_time) const
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 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_clip_rect(const SDL_Rect &r)
void set_bg_color(const color_t &bg_color)
void set_scroll_mode(LABEL_SCROLL_MODE scroll)
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:105
int h() const
The draw-space height of the texture, in pixels.
Definition: texture.hpp:114
@ border
The border of the map.
int w
Collection of helper functions relating to Pango formatting.
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
void remove_floating_label(int handle, int fadeout)
removes the floating label given by 'handle' from the screen
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
SDL_Rect get_floating_label_rect(int handle)
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 std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Shows a tip.
Definition: tooltip.cpp:64
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
std::size_t size(const std::string &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:59
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:47
#define h
#define b