The Battle for Wesnoth  1.17.10+dev
menu_style.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2022
3  by Patrick Parker <patrick_x99@hotmail.com>
4  Copyright (C) 2003 - 2005 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #define GETTEXT_DOMAIN "wesnoth-lib"
18 
19 #include "widgets/menu.hpp"
20 
21 #include "draw.hpp"
22 #include "font/constants.hpp"
23 #include "picture.hpp"
24 #include "lexical_cast.hpp"
25 #include "sdl/utils.hpp"
26 
27 namespace gui {
28 
29  //static initializations
30 menu::imgsel_style menu::bluebg_style("dialogs/selection", true,
31  0x000000, 0x000000, 0x333333,
32  0.35, 0.0, 0.3);
33 
35 
36  //constructors
38  cell_padding_(font::SIZE_NORMAL * 3/5), thickness_(0),
39  normal_rgb_(0x000000), selected_rgb_(0x000099), heading_rgb_(0x333333),
40  normal_alpha_(0.2), selected_alpha_(0.6), heading_alpha_(0.3),
41  max_img_w_(-1), max_img_h_(-1)
42 {}
43 
45 {}
46 menu::imgsel_style::imgsel_style(const std::string &img_base, bool has_bg,
47  int normal_rgb, int selected_rgb, int heading_rgb,
48  double normal_alpha, double selected_alpha, double heading_alpha)
49  : img_base_(img_base), has_background_(has_bg), initialized_(false), load_failed_(false),
50  normal_rgb2_(normal_rgb), selected_rgb2_(selected_rgb), heading_rgb2_(heading_rgb),
51  normal_alpha2_(normal_alpha), selected_alpha2_(selected_alpha), heading_alpha2_(heading_alpha)
52 {}
54 {}
55 
56 std::size_t menu::style::get_font_size() const { return font_size_; }
57 std::size_t menu::style::get_cell_padding() const { return cell_padding_; }
58 std::size_t menu::style::get_thickness() const { return thickness_; }
59 
60 void menu::style::scale_images(int max_width, int max_height)
61 {
62  max_img_w_ = max_width;
63  max_img_h_ = max_height;
64 }
65 
66 void menu::style::adjust_image_bounds(int& w, int& h) const
67 {
68  int scale = 100;
69  if(max_img_w_ > 0 && w > max_img_w_) {
70  scale = (max_img_w_ * 100) / w;
71  }
72  if(max_img_h_ > 0 && h > max_img_h_) {
73  scale = std::min<int>(scale, ((max_img_h_ * 100) / h));
74  }
75  if(scale != 100)
76  {
77  w = (scale * w)/100;
78  h = (scale * h)/100;
79  }
80 }
81 
82 bool menu::imgsel_style::load_image(const std::string &img_sub)
83 {
84  std::string path = img_base_ + "-" + img_sub + ".png";
85  const texture image = image::get_texture(path);
86  img_map_[img_sub] = image;
87  return bool(image);
88 }
89 
91 {
92  if(!initialized_)
93  {
94 
95  if( load_image("border-botleft")
96  && load_image("border-botright")
97  && load_image("border-topleft")
98  && load_image("border-topright")
99  && load_image("border-left")
100  && load_image("border-right")
101  && load_image("border-top")
102  && load_image("border-bottom") )
103  {
104  thickness_ = std::min(
105  img_map_["border-top"].h(),
106  img_map_["border-left"].w());
107 
108 
109  if(has_background_ && !load_image("background"))
110  {
111  load_failed_ = true;
112  }
113  else
114  {
121 
122  load_failed_ = false;
123  }
124  initialized_ = true;
125  }
126  else
127  {
128  thickness_ = 0;
129  initialized_ = true;
130  load_failed_ = true;
131  }
132  }
133  return (!load_failed_);
134 }
135 
137 {
138  img_map_.clear();
139 }
140 
141 void menu::imgsel_style::draw_row_bg(menu& menu_ref, const std::size_t row_index, const SDL_Rect& rect, ROW_TYPE type)
142 {
143  if(type == SELECTED_ROW && has_background_ && !load_failed_) {
144  draw::blit(img_map_["background"], rect);
145  }
146  else {
147  style::draw_row_bg(menu_ref, row_index, rect, type);
148  }
149 }
150 
151 void menu::imgsel_style::draw_row(menu& menu_ref, const std::size_t row_index, const SDL_Rect& rect, ROW_TYPE type)
152 {
153  if(!load_failed_) {
154  //draw item inside
155  style::draw_row(menu_ref, row_index, rect, type);
156 
157  if(type == SELECTED_ROW) {
158  // draw border
159  texture image;
160  SDL_Rect area;
161  auto clipper = draw::reduce_clip(rect);
162  area.x = rect.x;
163  area.y = rect.y;
164 
165  image = img_map_["border-top"];
166  area.x = rect.x;
167  area.y = rect.y;
168  area.w = image.w();
169  area.h = image.h();
170  do {
171  draw::blit(image, area);
172  area.x += area.w;
173  } while( area.x < rect.x + rect.w );
174 
175  image = img_map_["border-left"];
176  area.x = rect.x;
177  area.y = rect.y;
178  area.w = image.w();
179  area.h = image.h();
180  do {
181  draw::blit(image, area);
182  area.y += area.h;
183  } while( area.y < rect.y + rect.h );
184 
185  image = img_map_["border-right"];
186  area.x = rect.x + rect.w - thickness_;
187  area.y = rect.y;
188  area.w = image.w();
189  area.h = image.h();
190  do {
191  draw::blit(image, area);
192  area.y += area.h;
193  } while( area.y < rect.y + rect.h );
194 
195  image = img_map_["border-bottom"];
196  area.x = rect.x;
197  area.y = rect.y + rect.h - thickness_;
198  area.w = image.w();
199  area.h = image.h();
200  do {
201  draw::blit(image, area);
202  area.x += area.w;
203  } while( area.x < rect.x + rect.w );
204 
205  image = img_map_["border-topleft"];
206  area.x = rect.x;
207  area.y = rect.y;
208  area.w = image.w();
209  area.h = image.h();
210  draw::blit(image, area);
211 
212  image = img_map_["border-topright"];
213  area.x = rect.x + rect.w - image.w();
214  area.y = rect.y;
215  area.w = image.w();
216  area.h = image.h();
217  draw::blit(image, area);
218 
219  image = img_map_["border-botleft"];
220  area.x = rect.x;
221  area.y = rect.y + rect.h - image.h();
222  area.w = image.w();
223  area.h = image.h();
224  draw::blit(image, area);
225 
226  image = img_map_["border-botright"];
227  area.x = rect.x + rect.w - image.w();
228  area.y = rect.y + rect.h - image.h();
229  area.w = image.w();
230  area.h = image.h();
231  draw::blit(image, area);
232  }
233  }
234  else {
235  //default drawing
236  style::draw_row(menu_ref, row_index, rect, type);
237  }
238 }
239 
240 SDL_Rect menu::imgsel_style::item_size(const std::string& item) const
241 {
242  SDL_Rect bounds = style::item_size(item);
243 
244  bounds.w += 2 * thickness_;
245  bounds.h += 2 * thickness_ + 4;
246 
247  return bounds;
248 }
249 
250 
251 } //namesapce gui
virtual SDL_Rect item_size(const std::string &item) const
Definition: menu_style.cpp:240
Drawing functions, for drawing things on the screen.
virtual void draw_row_bg(menu &menu_ref, const std::size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu_style.cpp:141
std::size_t get_font_size() const
Definition: menu_style.cpp:56
imgsel_style(const std::string &img_base, bool has_bg, int normal_rgb, int selected_rgb, int heading_rgb, double normal_alpha, double selected_alpha, double heading_alpha)
Definition: menu_style.cpp:46
int w() const
The draw-space width of the texture, in pixels.
Definition: texture.hpp:105
int heading_rgb_
Definition: menu.hpp:58
Collection of helper functions relating to Pango formatting.
int max_img_w_
Definition: menu.hpp:60
double heading_alpha_
Definition: menu.hpp:59
New lexcical_cast header.
int h() const
The draw-space height of the texture, in pixels.
Definition: texture.hpp:114
ROW_TYPE
Definition: menu.hpp:35
int selected_rgb_
Definition: menu.hpp:58
std::map< std::string, texture > img_map_
Definition: menu.hpp:82
General purpose widgets.
bool load_image(const std::string &img_sub)
Definition: menu_style.cpp:82
virtual SDL_Rect item_size(const std::string &item) const
Definition: menu.cpp:752
#define h
double normal_alpha_
Definition: menu.hpp:59
static imgsel_style bluebg_style
Definition: menu.hpp:96
std::size_t cell_padding_
Definition: menu.hpp:55
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:32
const int SIZE_NORMAL
Definition: constants.cpp:20
void adjust_image_bounds(int &w, int &h) const
Definition: menu_style.cpp:66
friend class style
Definition: menu.hpp:93
void scale(size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
Definition: xbrz.cpp:1190
virtual void draw_row(menu &menu_ref, const std::size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:812
std::string path
Definition: game_config.cpp:39
static style & default_style
Definition: menu.hpp:95
double selected_alpha2_
Definition: menu.hpp:90
const std::string img_base_
Definition: menu.hpp:81
double selected_alpha_
Definition: menu.hpp:59
clip_setter reduce_clip(const SDL_Rect &clip)
Set the clipping area to the intersection of the current clipping area and the given rectangle...
Definition: draw.cpp:448
std::size_t get_cell_padding() const
Definition: menu_style.cpp:57
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:46
int w
int normal_rgb_
Definition: menu.hpp:58
double heading_alpha2_
Definition: menu.hpp:90
virtual void draw_row_bg(menu &menu_ref, const std::size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:785
void scale_images(int max_width, int max_height)
Definition: menu_style.cpp:60
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
Definition: draw.cpp:301
virtual ~style()
Definition: menu_style.cpp:44
std::size_t thickness_
Definition: menu.hpp:56
virtual void draw_row(menu &menu_ref, const std::size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu_style.cpp:151
std::size_t font_size_
Definition: menu.hpp:54
Functions to load and save images from/to disk.
int max_img_h_
Definition: menu.hpp:60
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:1128
friend class imgsel_style
Definition: menu.hpp:94
std::size_t get_thickness() const
Definition: menu_style.cpp:58