The Battle for Wesnoth  1.19.0-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, 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;}
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_;
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
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)
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)
floating_label(const std::string &text, const surface &surface=nullptr)
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
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:79
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