forked from wesnoth/wesnoth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_style.cpp
358 lines (312 loc) · 9.53 KB
/
menu_style.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
wesnoth menu styles Copyright (C) 2006 - 2014 by Patrick Parker <[email protected]>
wesnoth menu Copyright (C) 2003-5 by David White <[email protected]>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "global.hpp"
#include "widgets/menu.hpp"
#include "font.hpp"
#include "image.hpp"
#include "video.hpp"
namespace gui {
//static initializations
menu::imgsel_style menu::bluebg_style("dialogs/selection", true,
0x000000, 0x000000, 0x333333,
0.35, 0.0, 0.3);
menu::style menu::simple_style;
menu::style &menu::default_style = menu::bluebg_style;
//constructors
menu::style::style() : font_size_(font::SIZE_NORMAL),
cell_padding_(font::SIZE_NORMAL * 3/5), thickness_(0),
normal_rgb_(0x000000), selected_rgb_(0x000099), heading_rgb_(0x333333),
#ifdef SDL_GPU
normal_alpha_(50), selected_alpha_(150), heading_alpha_(75),
#else
normal_alpha_(0.2), selected_alpha_(0.6), heading_alpha_(0.3),
#endif
max_img_w_(-1), max_img_h_(-1)
{}
menu::style::~style()
{}
menu::imgsel_style::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)
: img_base_(img_base), has_background_(has_bg), initialized_(false), load_failed_(false),
normal_rgb2_(normal_rgb), selected_rgb2_(selected_rgb), heading_rgb2_(heading_rgb),
normal_alpha2_(normal_alpha), selected_alpha2_(selected_alpha), heading_alpha2_(heading_alpha)
{}
menu::imgsel_style::~imgsel_style()
{}
size_t menu::style::get_font_size() const { return font_size_; }
size_t menu::style::get_cell_padding() const { return cell_padding_; }
size_t menu::style::get_thickness() const { return thickness_; }
void menu::style::scale_images(int max_width, int max_height)
{
max_img_w_ = max_width;
max_img_h_ = max_height;
}
#ifdef SDL_GPU
sdl::timage menu::style::get_item_image(const image::locator& img_loc) const
{
sdl::timage img = image::get_texture(img_loc);
if(!img.null())
{
int scale = 100;
if(max_img_w_ > 0 && img.width() > max_img_w_) {
scale = (max_img_w_ * 100) / img.width();
}
if(max_img_h_ > 0 && img.height() > max_img_h_) {
scale = std::min<int>(scale, ((max_img_h_ * 100) / img.height()));
}
if(scale != 100)
{
img.set_scale(scale, scale);
return img;
}
}
return img;
}
#else
surface menu::style::get_item_image(const image::locator& img_loc) const
{
surface surf = image::get_image(img_loc);
if(!surf.null())
{
int scale = 100;
if(max_img_w_ > 0 && surf->w > max_img_w_) {
scale = (max_img_w_ * 100) / surf->w;
}
if(max_img_h_ > 0 && surf->h > max_img_h_) {
scale = std::min<int>(scale, ((max_img_h_ * 100) / surf->h));
}
if(scale != 100)
{
return scale_surface(surf, (scale * surf->w)/100, (scale * surf->h)/100);
}
}
return surf;
}
#endif
bool menu::imgsel_style::load_image(const std::string &img_sub)
{
#ifdef SDL_GPU
std::string path = img_base_ + "-" + img_sub + ".png";
sdl::timage image = image::get_image(path);
img_map_[img_sub] = image;
return(!image.null());
#else
std::string path = img_base_ + "-" + img_sub + ".png";
const surface image = image::get_image(path);
img_map_[img_sub] = image;
return(!image.null());
#endif
}
bool menu::imgsel_style::load_images()
{
if(!initialized_)
{
if( load_image("border-botleft")
&& load_image("border-botright")
&& load_image("border-topleft")
&& load_image("border-topright")
&& load_image("border-left")
&& load_image("border-right")
&& load_image("border-top")
&& load_image("border-bottom") )
{
#ifdef SDL_GPU
thickness_ = std::min(
img_map_["border-top"].height(),
img_map_["border-left"].width());
#else
thickness_ = std::min(
img_map_["border-top"]->h,
img_map_["border-left"]->w);
#endif
if(has_background_ && !load_image("background"))
{
load_failed_ = true;
}
else
{
normal_rgb_ = normal_rgb2_;
normal_alpha_ = normal_alpha2_;
selected_rgb_ = selected_rgb2_;
selected_alpha_ = selected_alpha2_;
heading_rgb_ = heading_rgb2_;
heading_alpha_ = heading_alpha2_;
load_failed_ = false;
}
initialized_ = true;
}
else
{
thickness_ = 0;
initialized_ = true;
load_failed_ = true;
}
}
return (!load_failed_);
}
void menu::imgsel_style::draw_row_bg(menu& menu_ref, const size_t row_index, const SDL_Rect& rect, ROW_TYPE type)
{
#ifdef SDL_GPU
if(type == SELECTED_ROW && has_background_ && !load_failed_) {
background_image_.set_scale(float(rect.w) / background_image_.width(),
float(rect.h) / background_image_.height());
menu_ref.video().draw_texture(background_image_, rect.x, rect.y);
}
#else
if(type == SELECTED_ROW && has_background_ && !load_failed_) {
if(bg_cache_.width != rect.w || bg_cache_.height != rect.h)
{
//draw scaled background image
//scale image each time (to prevent loss of quality)
bg_cache_.surf = scale_surface(img_map_["background"], rect.w, rect.h);
bg_cache_.width = rect.w;
bg_cache_.height = rect.h;
}
SDL_Rect clip = rect;
menu_ref.video().blit_surface(rect.x,rect.y,bg_cache_.surf,NULL,&clip);
}
#endif
else {
style::draw_row_bg(menu_ref, row_index, rect, type);
}
}
void menu::imgsel_style::draw_row(menu& menu_ref, const size_t row_index, const SDL_Rect& rect, ROW_TYPE type)
{
if(!load_failed_) {
//draw item inside
style::draw_row(menu_ref, row_index, rect, type);
#ifdef SDL_GPU
if(type == SELECTED_ROW) {
// draw border
sdl::timage image;
SDL_Rect area;
area.x = rect.x;
area.y = rect.y;
GPU_SetClip(get_render_target(), rect.x, rect.y, rect.w, rect.h);
image = img_map_["border-top"];
area.x = rect.x;
area.y = rect.y;
do {
menu_ref.video().draw_texture(image, area.x, area.y);
area.x += image.width();
} while( area.x < rect.x + rect.w );
image = img_map_["border-left"];
area.x = rect.x;
area.y = rect.y;
do {
menu_ref.video().draw_texture(image, area.x, area.y);
area.y += image.height();
} while( area.y < rect.y + rect.h );
image = img_map_["border-right"];
area.x = rect.x + rect.w - thickness_;
area.y = rect.y;
do {
menu_ref.video().draw_texture(image, area.x, area.y);
area.y += image.height();
} while( area.y < rect.y + rect.h );
image = img_map_["border-bottom"];
area.x = rect.x;
area.y = rect.y + rect.h - thickness_;
do {
menu_ref.video().draw_texture(image, area.x, area.y);
area.x += image.width();
} while( area.x < rect.x + rect.w );
image = img_map_["border-topleft"];
area.x = rect.x;
area.y = rect.y;
menu_ref.video().draw_texture(image, area.x, area.y);
image = img_map_["border-topright"];
area.x = rect.x + rect.w - image.width();
area.y = rect.y;
menu_ref.video().draw_texture(image, area.x, area.y);
image = img_map_["border-botleft"];
area.x = rect.x;
area.y = rect.y + rect.h - image.height();
menu_ref.video().draw_texture(image, area.x, area.y);
image = img_map_["border-botright"];
area.x = rect.x + rect.w - image.width();
area.y = rect.y + rect.h - image.height();
menu_ref.video().draw_texture(image, area.x, area.y);
GPU_UnsetClip(get_render_target());
}
}
#else
if(type == SELECTED_ROW) {
// draw border
surface image;
SDL_Rect area;
SDL_Rect clip = rect;
area.x = rect.x;
area.y = rect.y;
image = img_map_["border-top"];
area.x = rect.x;
area.y = rect.y;
do {
menu_ref.video().blit_surface(area.x,area.y,image,NULL,&clip);
area.x += image->w;
} while( area.x < rect.x + rect.w );
image = img_map_["border-left"];
area.x = rect.x;
area.y = rect.y;
do {
menu_ref.video().blit_surface(area.x,area.y,image,NULL,&clip);
area.y += image->h;
} while( area.y < rect.y + rect.h );
image = img_map_["border-right"];
area.x = rect.x + rect.w - thickness_;
area.y = rect.y;
do {
menu_ref.video().blit_surface(area.x,area.y,image,NULL,&clip);
area.y += image->h;
} while( area.y < rect.y + rect.h );
image = img_map_["border-bottom"];
area.x = rect.x;
area.y = rect.y + rect.h - thickness_;
do {
menu_ref.video().blit_surface(area.x,area.y,image,NULL,&clip);
area.x += image->w;
} while( area.x < rect.x + rect.w );
image = img_map_["border-topleft"];
area.x = rect.x;
area.y = rect.y;
menu_ref.video().blit_surface(area.x,area.y,image);
image = img_map_["border-topright"];
area.x = rect.x + rect.w - image->w;
area.y = rect.y;
menu_ref.video().blit_surface(area.x,area.y,image);
image = img_map_["border-botleft"];
area.x = rect.x;
area.y = rect.y + rect.h - image->h;
menu_ref.video().blit_surface(area.x,area.y,image);
image = img_map_["border-botright"];
area.x = rect.x + rect.w - image->w;
area.y = rect.y + rect.h - image->h;
menu_ref.video().blit_surface(area.x,area.y,image);
}
}
#endif
else {
//default drawing
style::draw_row(menu_ref, row_index, rect, type);
}
}
SDL_Rect menu::imgsel_style::item_size(const std::string& item) const
{
SDL_Rect bounds = style::item_size(item);
bounds.w += 2 * thickness_;
bounds.h += 2 * thickness_;
return bounds;
}
} //namesapce gui