The Battle for Wesnoth  1.17.0-dev
floating_label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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/surface.hpp"
20 #include <string>
21 
22 namespace font {
23 
24 /**
25  * structure which will hide all current floating labels, and cause floating labels
26  * instantiated after it is created to be displayed
27  */
29 {
32 };
33 
35 
37 
39 {
40 public:
41  floating_label(const std::string& text, const surface& surface = nullptr);
42 
43  void set_font_size(int font_size) {font_size_ = font_size;}
44 
45  // set the location on the screen to display the text.
46  void set_position(double xpos, double ypos){
47  xpos_ = xpos;
48  ypos_ = ypos;
49  }
50  // set the amount to move the text each frame
51  void set_move(double xmove, double ymove){
52  xmove_ = xmove;
53  ymove_ = ymove;
54  }
55  // set the number of frames to display the text for, or -1 to display until removed
56  void set_lifetime(int lifetime, int fadeout = 100);
57  void set_color(const color_t& color) {color_ = color;}
58  void set_bg_color(const color_t& bg_color) {
59  bgcolor_ = bg_color;
60  bgalpha_ = bg_color.a;
61  }
62  void set_border_size(int border) {border_ = border;}
63  // set width for word wrapping (use -1 to disable it)
64  void set_width(int w) {width_ = w;}
65  void set_height(int h) { height_ = h; }
66  void set_clip_rect(const SDL_Rect& r) {clip_rect_ = r;}
67  void set_alignment(ALIGN align) {align_ = align;}
68  void set_scroll_mode(LABEL_SCROLL_MODE scroll) {scroll_ = scroll;}
69  void use_markup(bool b) {use_markup_ = b;}
70 
71  void move(double xmove, double ymove);
72  void draw(int time, surface screen);
73  void undraw(surface screen);
74 
75  surface create_surface();
76 
77  bool expired(int time) const { return lifetime_ >= 0 && get_time_alive(time) > lifetime_ + fadeout_; }
78 
79  void show(const bool value) { visible_ = value; }
80 
81  LABEL_SCROLL_MODE scroll() const { return scroll_; }
82 
83 private:
84 
85  int get_time_alive(int current_time) const { return current_time - time_start_; }
86  int xpos(std::size_t width) const;
87  SDL_Point get_loc(int time);
88  surface get_surface(int time);
89  surface surf_, buf_;
90  SDL_Rect buf_pos_;
91  int fadeout_;
93  std::string text_;
95  color_t color_, bgcolor_;
96  int bgalpha_;
97  double xpos_, ypos_, xmove_, ymove_;
98  int lifetime_;
99  int width_, height_;
100  SDL_Rect clip_rect_;
101  bool visible_;
103  int border_;
106 };
107 
108 
109 /**
110  * add a label floating on the screen above everything else.
111  * @returns a handle to the label which can be used with other label functions
112  */
113 int add_floating_label(const floating_label& flabel);
114 
115 
116 /** moves the floating label given by 'handle' by (xmove,ymove) */
117 void move_floating_label(int handle, double xmove, double ymove);
118 
119 /** moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP */
120 void scroll_floating_labels(double xmove, double ymove);
121 
122 /** removes the floating label given by 'handle' from the screen */
123 /** if fadeout is given, the label fades out over that duration */
124 void remove_floating_label(int handle, int fadeout = 0);
125 
126 /** hides or shows a floating label */
127 void show_floating_label(int handle, bool show);
128 
129 SDL_Rect get_floating_label_rect(int handle);
130 void draw_floating_labels(surface screen);
131 void undraw_floating_labels(surface screen);
132 
133 } // end namespace font
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
#define h
void set_font_size(int font_size)
int get_time_alive(int current_time) const
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
bool expired(int time) const
void set_bg_color(const color_t &bg_color)
uint8_t a
Alpha value.
Definition: color.hpp:187
void set_position(double xpos, double ypos)
void set_color(const color_t &color)
void set_scroll_mode(LABEL_SCROLL_MODE scroll)
structure which will hide all current floating labels, and cause floating labels instantiated after i...
void undraw_floating_labels(surface screen)
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)
LABEL_SCROLL_MODE scroll_
std::shared_ptr< halo_record > handle
Definition: halo.hpp:30
void draw_floating_labels(surface screen)
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:140