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