The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
label.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Philippe Plantier <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "global.hpp"
18 
19 #include "widgets/label.hpp"
20 #include "marked-up_text.hpp"
21 #include "text.hpp"
22 #include "video.hpp"
23 
24 namespace gui {
25 
26 label::label(CVideo& video, const std::string& text, int size, const SDL_Color& color, const bool auto_join) : widget(video, auto_join), text_(text), size_(size), color_(color)
27 {
28 #ifdef SDL_GPU
29  render_text();
30 #else
32 #endif
33 }
34 
36 {
37  if (text_ == text)
38  return text_;
39 
40  text_ = text;
41 #ifdef SDL_GPU
42  render_text();
43 #else
45 #endif
46  set_dirty();
47  return text_;
48 }
49 
51 {
52  return text_;
53 }
54 
55 const SDL_Color& label::set_color(const SDL_Color& color)
56 {
57  color_ = color;
58 #ifdef SDL_GPU
59  render_text();
60 #endif
61  set_dirty();
62  return get_color();
63 }
64 
65 const SDL_Color& label::get_color() const
66 {
67  return (enabled()) ? color_ : font::DISABLED_COLOR;
68 }
69 
71 {
72  const SDL_Rect& loc = location();
73  if (!text_.empty() && loc.w > 0 && loc.h > 0)
74 #ifdef SDL_GPU
75  video().draw_texture(text_image_, loc.x, loc.y);
76 #else
77  font::draw_text(&video(), loc, size_, get_color(), text_, loc.x, loc.y);
78 #endif
79 }
80 
82 {
83  SDL_Rect area = font::text_area(text_, size_);
84  set_measurements(area.w, area.h);
85 }
86 
87 #ifdef SDL_GPU
88 void label::render_text()
89 {
90  font::ttext txt;
91 
92  txt.set_text(text_, true);
93  const Uint32 color = (color_.r << 16) + (color_.g << 16) + (color_.b);
94  txt.set_foreground_color(color);
95  txt.set_font_size(size_);
96 
97  text_image_ = txt.render_as_texture();
98  set_measurements(text_image_.width(), text_image_.height());
99 }
100 #endif
101 
102 
103 }
SDL_Rect text_area(const std::string &text, int size, int style)
Calculate the size of a text (in pixels) if it were to be drawn.
const SDL_Color & set_color(const SDL_Color &color)
Definition: label.cpp:55
const std::string & set_text(const std::string &text)
Definition: label.cpp:35
bool enabled() const
Definition: widget.cpp:212
Definition: video.hpp:58
static surface render_text(const std::string &text, int fontsize, const SDL_Color &color, int style, bool use_markup)
Definition: font.cpp:813
label(CVideo &video, const std::string &text, int size=font::SIZE_NORMAL, const SDL_Color &color=font::NORMAL_COLOR, const bool auto_join=true)
Definition: label.cpp:26
CVideo & video() const
Definition: widget.hpp:83
General purpose widgets.
void update_label_size()
Definition: label.cpp:81
const SDL_Color & get_color() const
Definition: label.cpp:65
ttext & set_font_size(const unsigned font_size)
Definition: text.cpp:406
SDL_Color color_
Definition: font.cpp:605
tformula< t_string > text_
The text to draw.
Definition: canvas.cpp:1268
-file util.hpp
void set_measurements(int w, int h)
Definition: widget.cpp:129
const SDL_Color DISABLED_COLOR
Definition: font.cpp:576
void set_dirty(bool dirty=true)
Definition: widget.cpp:217
GLuint color
Definition: glew.h:5801
const std::string & get_text() const
Definition: label.cpp:50
int size_
Definition: label.hpp:45
SDL_Color color_
Definition: label.hpp:46
SDL_Rect draw_text(surface &dst, const SDL_Rect &area, int size, const SDL_Color &color, const std::string &txt, int x, int y, bool use_tooltips, int style)
Function to draw text on a surface.
virtual void draw_contents()
Definition: label.cpp:70
GLsizeiptr size
Definition: glew.h:1649
std::string text_
Definition: label.hpp:44
bool set_text(const std::string &text, const bool markedup)
Sets the text to render.
Definition: text.cpp:360
SDL_Rect const & location() const
Definition: widget.cpp:144
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Text class.
Definition: text.hpp:66
ttext & set_foreground_color(const Uint32 color)
Definition: text.cpp:429