The Battle for Wesnoth  1.19.0-dev
menu_style.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2024
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 
25 namespace gui {
26 
27  //static initializations
28 menu::imgsel_style menu::bluebg_style("dialogs/selection", true,
29  0x000000, 0x000000,
30  0.35, 0.0);
31 
33 
34  //constructors
36  cell_padding_(font::SIZE_NORMAL * 3/5), thickness_(0),
37  normal_rgb_(0x000000), selected_rgb_(0x000099),
38  normal_alpha_(0.2), selected_alpha_(0.6)
39 {}
40 
42 {}
43 menu::imgsel_style::imgsel_style(const std::string &img_base, bool has_bg,
44  int normal_rgb, int selected_rgb,
45  double normal_alpha, double selected_alpha)
46  : img_base_(img_base), has_background_(has_bg), initialized_(false), load_failed_(false),
47  normal_rgb2_(normal_rgb), selected_rgb2_(selected_rgb),
48  normal_alpha2_(normal_alpha), selected_alpha2_(selected_alpha)
49 {}
51 {}
52 
53 std::size_t menu::style::get_font_size() const { return font_size_; }
54 std::size_t menu::style::get_cell_padding() const { return cell_padding_; }
55 std::size_t menu::style::get_thickness() const { return thickness_; }
56 
57 bool menu::imgsel_style::load_image(const std::string &img_sub)
58 {
59  std::string path = img_base_ + "-" + img_sub + ".png";
61  img_map_[img_sub] = image;
62  return bool(image);
63 }
64 
66 {
67  if(!initialized_)
68  {
69 
70  if( load_image("border-botleft")
71  && load_image("border-botright")
72  && load_image("border-topleft")
73  && load_image("border-topright")
74  && load_image("border-left")
75  && load_image("border-right")
76  && load_image("border-top")
77  && load_image("border-bottom") )
78  {
79  thickness_ = std::min(
80  img_map_["border-top"].h(),
81  img_map_["border-left"].w());
82 
83 
84  if(has_background_ && !load_image("background"))
85  {
86  load_failed_ = true;
87  }
88  else
89  {
90  normal_rgb_ = normal_rgb2_;
91  normal_alpha_ = normal_alpha2_;
92  selected_rgb_ = selected_rgb2_;
93  selected_alpha_ = selected_alpha2_;
94 
95  load_failed_ = false;
96  }
97  initialized_ = true;
98  }
99  else
100  {
101  thickness_ = 0;
102  initialized_ = true;
103  load_failed_ = true;
104  }
105  }
106  return (!load_failed_);
107 }
108 
110 {
111  img_map_.clear();
112 }
113 
114 void menu::imgsel_style::draw_row_bg(menu& menu_ref, const std::size_t row_index, const SDL_Rect& rect, ROW_TYPE type)
115 {
116  if(type == SELECTED_ROW && has_background_ && !load_failed_) {
117  draw::blit(img_map_["background"], rect);
118  }
119  else {
120  style::draw_row_bg(menu_ref, row_index, rect, type);
121  }
122 }
123 
124 void menu::imgsel_style::draw_row(menu& menu_ref, const std::size_t row_index, const SDL_Rect& rect, ROW_TYPE type)
125 {
126  if(!load_failed_) {
127  //draw item inside
128  style::draw_row(menu_ref, row_index, rect, type);
129 
130  if(type == SELECTED_ROW) {
131  // draw border
132  texture image;
133  SDL_Rect area;
134  auto clipper = draw::reduce_clip(rect);
135  area.x = rect.x;
136  area.y = rect.y;
137 
138  image = img_map_["border-top"];
139  area.x = rect.x;
140  area.y = rect.y;
141  area.w = image.w();
142  area.h = image.h();
143  do {
144  draw::blit(image, area);
145  area.x += area.w;
146  } while( area.x < rect.x + rect.w );
147 
148  image = img_map_["border-left"];
149  area.x = rect.x;
150  area.y = rect.y;
151  area.w = image.w();
152  area.h = image.h();
153  do {
154  draw::blit(image, area);
155  area.y += area.h;
156  } while( area.y < rect.y + rect.h );
157 
158  image = img_map_["border-right"];
159  area.x = rect.x + rect.w - thickness_;
160  area.y = rect.y;
161  area.w = image.w();
162  area.h = image.h();
163  do {
164  draw::blit(image, area);
165  area.y += area.h;
166  } while( area.y < rect.y + rect.h );
167 
168  image = img_map_["border-bottom"];
169  area.x = rect.x;
170  area.y = rect.y + rect.h - thickness_;
171  area.w = image.w();
172  area.h = image.h();
173  do {
174  draw::blit(image, area);
175  area.x += area.w;
176  } while( area.x < rect.x + rect.w );
177 
178  image = img_map_["border-topleft"];
179  area.x = rect.x;
180  area.y = rect.y;
181  area.w = image.w();
182  area.h = image.h();
183  draw::blit(image, area);
184 
185  image = img_map_["border-topright"];
186  area.x = rect.x + rect.w - image.w();
187  area.y = rect.y;
188  area.w = image.w();
189  area.h = image.h();
190  draw::blit(image, area);
191 
192  image = img_map_["border-botleft"];
193  area.x = rect.x;
194  area.y = rect.y + rect.h - image.h();
195  area.w = image.w();
196  area.h = image.h();
197  draw::blit(image, area);
198 
199  image = img_map_["border-botright"];
200  area.x = rect.x + rect.w - image.w();
201  area.y = rect.y + rect.h - image.h();
202  area.w = image.w();
203  area.h = image.h();
204  draw::blit(image, area);
205  }
206  }
207  else {
208  //default drawing
209  style::draw_row(menu_ref, row_index, rect, type);
210  }
211 }
212 
214 {
215  SDL_Rect bounds = style::item_size(imi);
216 
217  bounds.w += 2 * thickness_;
218  bounds.h += 2 * thickness_ + 4;
219 
220  return bounds;
221 }
222 
223 
224 } //namesapce gui
virtual SDL_Rect item_size(const indented_menu_item &imi) const
Definition: menu_style.cpp:213
virtual void draw_row(menu &menu_ref, const std::size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu_style.cpp:124
imgsel_style(const std::string &img_base, bool has_bg, int normal_rgb, int selected_rgb, double normal_alpha, double selected_alpha)
Definition: menu_style.cpp:43
bool load_image(const std::string &img_sub)
Definition: menu_style.cpp:57
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:114
virtual SDL_Rect item_size(const indented_menu_item &imi) const
Definition: menu.cpp:438
std::size_t get_thickness() const
Definition: menu_style.cpp:55
std::size_t get_font_size() const
Definition: menu_style.cpp:53
virtual void draw_row_bg(menu &menu_ref, const std::size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:466
std::size_t get_cell_padding() const
Definition: menu_style.cpp:54
virtual ~style()
Definition: menu_style.cpp:41
virtual void draw_row(menu &menu_ref, const std::size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:489
Superclass of the help_menu, which displays the left-hand pane of the GUI1 help browser.
Definition: menu.hpp:46
friend class imgsel_style
Definition: menu.hpp:105
static style & default_style
Definition: menu.hpp:106
friend class style
Definition: menu.hpp:104
static imgsel_style bluebg_style
Definition: menu.hpp:107
ROW_TYPE
Definition: menu.hpp:49
@ SELECTED_ROW
Definition: menu.hpp:49
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
Drawing functions, for drawing things on the screen.
int w
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:457
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
Definition: draw.cpp:310
Collection of helper functions relating to Pango formatting.
const int SIZE_NORMAL
Definition: constants.cpp:20
std::string path
Definition: filesystem.cpp:83
General purpose widgets.
Functions to load and save images from/to disk.
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:960
The only kind of row still supported by the menu class.
Definition: menu.hpp:33
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
#define h