The Battle for Wesnoth  1.15.12+dev
floating_label.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "color.hpp"
18 #include "sdl/surface.hpp"
19 #include <string>
20 
21 namespace font {
22 
23 /**
24  * structure which will hide all current floating labels, and cause floating labels
25  * instantiated after it is created to be displayed
26  */
28 {
31 };
32 
34 
36 
38 {
39 public:
40  floating_label(const std::string& text, const surface& surface = nullptr);
41 
42  void set_font_size(int font_size) {font_size_ = font_size;}
43 
44  // set the location on the screen to display the text.
45  void set_position(double xpos, double ypos){
46  xpos_ = xpos;
47  ypos_ = ypos;
48  }
49  // set the amount to move the text each frame
50  void set_move(double xmove, double ymove){
51  xmove_ = xmove;
52  ymove_ = ymove;
53  }
54  // set the number of frames to display the text for, or -1 to display until removed
55  void set_lifetime(int lifetime, int fadeout = 100);
56  void set_color(const color_t& color) {color_ = color;}
57  void set_bg_color(const color_t& bg_color) {
58  bgcolor_ = bg_color;
59  bgalpha_ = bg_color.a;
60  }
61  void set_border_size(int border) {border_ = border;}
62  // set width for word wrapping (use -1 to disable it)
63  void set_width(int w) {width_ = w;}
64  void set_height(int h) { height_ = h; }
65  void set_clip_rect(const SDL_Rect& r) {clip_rect_ = r;}
66  void set_alignment(ALIGN align) {align_ = align;}
67  void set_scroll_mode(LABEL_SCROLL_MODE scroll) {scroll_ = scroll;}
68  void use_markup(bool b) {use_markup_ = b;}
69 
70  void move(double xmove, double ymove);
71  void draw(int time, surface screen);
72  void undraw(surface screen);
73 
74  surface create_surface();
75 
76  bool expired(int time) const { return lifetime_ >= 0 && get_time_alive(time) > lifetime_ + fadeout_; }
77 
78  void show(const bool value) { visible_ = value; }
79 
80  LABEL_SCROLL_MODE scroll() const { return scroll_; }
81 
82 private:
83 
84  int get_time_alive(int current_time) const { return current_time - time_start_; }
85  int xpos(std::size_t width) const;
86  SDL_Point get_loc(int time);
87  surface get_surface(int time);
88  surface surf_, buf_;
89  SDL_Rect buf_pos_;
90  int fadeout_;
92  std::string text_;
94  color_t color_, bgcolor_;
95  int bgalpha_;
96  double xpos_, ypos_, xmove_, ymove_;
97  int lifetime_;
98  int width_, height_;
99  SDL_Rect clip_rect_;
100  bool visible_;
102  int border_;
105 };
106 
107 
108 /**
109  * add a label floating on the screen above everything else.
110  * @returns a handle to the label which can be used with other label functions
111  */
112 int add_floating_label(const floating_label& flabel);
113 
114 
115 /** moves the floating label given by 'handle' by (xmove,ymove) */
116 void move_floating_label(int handle, double xmove, double ymove);
117 
118 /** moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP */
119 void scroll_floating_labels(double xmove, double ymove);
120 
121 /** removes the floating label given by 'handle' from the screen */
122 /** if fadeout is given, the label fades out over that duration */
123 void remove_floating_label(int handle, int fadeout = 0);
124 
125 /** hides or shows a floating label */
126 void show_floating_label(int handle, bool show);
127 
128 SDL_Rect get_floating_label_rect(int handle);
129 void draw_floating_labels(surface screen);
130 void undraw_floating_labels(surface screen);
131 
132 } // 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:186
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:29
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:139