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