The Battle for Wesnoth  1.15.2+dev
helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/widgets/helper.hpp"
18 
19 #include "color.hpp"
20 #include "formula/callable.hpp"
21 #include "formula/string_utils.hpp"
22 #include "gui/core/log.hpp"
23 #include "sdl/point.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "sdl/rect.hpp"
26 #include "tstring.hpp"
27 
28 #include <SDL2/SDL.h>
29 
30 namespace gui2
31 {
32 SDL_Rect create_rect(const point& origin, const point& size)
33 {
34  return {origin.x, origin.y, size.x, size.y};
35 }
36 
38 {
39  static std::map<std::string, font::pango_text::FONT_STYLE> font_style_map {
43  {"underline", font::pango_text::STYLE_UNDERLINE},
44  };
45 
46  if(style.empty()) {
48  }
49 
50  if(font_style_map.find(style) == font_style_map.end()) {
51  ERR_GUI_G << "Unknown style '" << style << "', using 'normal' instead." << std::endl;
53  }
54 
55  return font_style_map[style];
56 }
57 
58 color_t decode_color(const std::string& color)
59 {
60  return color_t::from_rgba_string(color);
61 }
62 
63 PangoAlignment decode_text_alignment(const std::string& alignment)
64 {
65  if(alignment == "center") {
66  return PANGO_ALIGN_CENTER;
67  } else if(alignment == "right") {
68  return PANGO_ALIGN_RIGHT;
69  }
70 
71  if(!alignment.empty() && alignment != "left") {
72  ERR_GUI_E << "Invalid text alignment '" << alignment << "', falling back to 'left'." << std::endl;
73  }
74 
75  return PANGO_ALIGN_LEFT;
76 }
77 
78 std::string encode_text_alignment(const PangoAlignment alignment)
79 {
80  switch(alignment) {
81  case PANGO_ALIGN_LEFT:
82  return "left";
83  case PANGO_ALIGN_RIGHT:
84  return "right";
85  case PANGO_ALIGN_CENTER:
86  return "center";
87  }
88 
89  assert(false);
90  // FIXME: without this "styled_widget reaches end of non-void function" in release mode
91  throw "Control should not reach this point.";
92 }
93 
94 t_string missing_widget(const std::string& id)
95 {
96  return t_string(VGETTEXT("Mandatory widget '$id' hasn't been defined.", {{"id", id}}));
97 }
98 
100 {
101  variable.add("screen_width", wfl::variant(settings::screen_width));
102  variable.add("screen_height", wfl::variant(settings::screen_height));
103  variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
104  variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
105  variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
106 }
107 
109 {
112 
113  return result;
114 }
115 
117 {
118  int x, y;
119  SDL_GetMouseState(&x, &y);
120 
121  return point(x, y);
122 }
123 
124 std::string debug_truncate(const std::string& text)
125 {
126  return text.substr(0, 15);
127 }
128 
129 } // namespace gui2
Define the common log macros for the gui toolkit.
#define ERR_GUI_E
Definition: log.hpp:37
std::string encode_text_alignment(const PangoAlignment alignment)
Converts a text alignment to its string representation.
Definition: helper.cpp:78
SDL_Rect create_rect(const point &origin, const point &size)
Creates a rectangle.
Definition: helper.cpp:32
#define ERR_GUI_G
Definition: log.hpp:43
unsigned gamemap_width
The size of the map area, if not available equal to the screen size.
Definition: settings.cpp:30
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:63
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:99
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
This file contains the settings handling of the widget library.
map_formula_callable & add(const std::string &key, const variant &value)
Definition: callable.hpp:252
unsigned gamemap_height
Definition: settings.cpp:31
unsigned gamemap_x_offset
The offset between the left edge of the screen and the gamemap.
Definition: settings.cpp:28
std::string debug_truncate(const std::string &text)
Returns a truncated version of the text.
Definition: helper.cpp:124
Holds a 2D point.
Definition: point.hpp:23
unsigned screen_width
The screen resolution should be available for all widgets since their drawing method will depend on i...
Definition: settings.cpp:25
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
point get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:116
Contains the SDL_Rect helper code.
unsigned screen_height
Definition: settings.cpp:26
static color_t from_rgba_string(const std::string &c)
Creates a new color_t object from a string variable in "R,G,B,A" format.
Definition: color.cpp:20
font::pango_text::FONT_STYLE decode_font_style(const std::string &style)
Converts a font style string to a font style.
Definition: helper.cpp:37
int y
y coordinate.
Definition: point.hpp:47
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:94
color_t decode_color(const std::string &color)
Converts a color string to a color.
Definition: helper.cpp:58