-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
image.cpp
182 lines (144 loc) · 4.46 KB
/
image.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
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
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 "gui/widgets/image.hpp"
#include "../../image.hpp" // We want the file in src/
#include "gui/core/widget_definition.hpp"
#include "gui/core/window_builder.hpp"
#include "gui/core/log.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/widgets/settings.hpp"
#include "utils/functional.hpp"
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
namespace gui2
{
// ------------ WIDGET -----------{
REGISTER_WIDGET(image)
image::image(const implementation::builder_image& builder)
: styled_widget(builder, get_control_type())
{
}
point image::calculate_best_size() const
{
surface image(::image::get_image(::image::locator(get_label())));
if(!image) {
DBG_GUI_L << LOG_HEADER << " empty image return default.\n";
return get_config_default_size();
}
const point minimum = get_config_default_size();
const point maximum = get_config_maximum_size();
point result {image->w, image->h};
if(minimum.x > 0 && result.x < minimum.x) {
DBG_GUI_L << LOG_HEADER << " increase width to minimum.\n";
result.x = minimum.x;
} else if(maximum.x > 0 && result.x > maximum.x) {
DBG_GUI_L << LOG_HEADER << " decrease width to maximum.\n";
result.x = maximum.x;
}
if(minimum.y > 0 && result.y < minimum.y) {
DBG_GUI_L << LOG_HEADER << " increase height to minimum.\n";
result.y = minimum.y;
} else if(maximum.y > 0 && result.y > maximum.y) {
DBG_GUI_L << LOG_HEADER << " decrease height to maximum.\n";
result.y = maximum.y;
}
DBG_GUI_L << LOG_HEADER << " result " << result << ".\n";
return result;
}
void image::set_active(const bool /*active*/)
{
/* DO NOTHING */
}
bool image::get_active() const
{
return true;
}
unsigned image::get_state() const
{
return ENABLED;
}
bool image::disable_click_dismiss() const
{
return false;
}
// }---------- DEFINITION ---------{
image_definition::image_definition(const config& cfg)
: styled_widget_definition(cfg)
{
DBG_GUI_P << "Parsing image " << id << '\n';
load_resolutions<resolution>(cfg);
}
/*WIKI
* @page = GUIWidgetDefinitionWML
* @order = 1_image
*
* == Image ==
*
* @macro = image_description
*
* The definition of an image. The label field of the widget is used as the
* name of file to show. The widget normally has no event interaction so only
* one state is defined.
*
* The following states exist:
* * state_enabled, the image is enabled.
* @begin{parent}{name="gui/"}
* @begin{tag}{name="image_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
* @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
* @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
* @end{tag}{name="state_enabled"}
* @end{tag}{name="resolution"}
* @end{tag}{name="image_definition"}
* @end{parent}{name="gui/"}
*/
image_definition::resolution::resolution(const config& cfg)
: resolution_definition(cfg)
{
// Note the order should be the same as the enum state_t in image.hpp.
state.emplace_back(cfg.child("state_enabled"));
}
// }---------- BUILDER -----------{
/*WIKI_MACRO
* @begin{macro}{image_description}
* An image shows a static image.
* @end{macro}
*/
/*WIKI
* @page = GUIWidgetInstanceWML
* @order = 2_image
*
* == Image ==
*
* @macro = image_description
*
* An image has no extra fields.
* @begin{parent}{name="gui/window/resolution/grid/row/column/"}
* @begin{tag}{name="image"}{min=0}{max=-1}{super="generic/widget_instance"}
* @end{tag}{name="image"}
* @end{parent}{name="gui/window/resolution/grid/row/column/"}
*/
namespace implementation
{
builder_image::builder_image(const config& cfg) : builder_styled_widget(cfg)
{
}
widget* builder_image::build() const
{
image* widget = new image(*this);
DBG_GUI_G << "Window builder: placed image '" << id << "' with definition '"
<< definition << "'.\n";
return widget;
}
} // namespace implementation
// }------------ END --------------
} // namespace gui2