Debian Codegraph @

Examples/GUIStyle/ZZ_Theme/button_zz.cpp

raw
1 #include <ClanLib/display.h>
2 #include <ClanLib/gl.h>
3
4 #include "button_zz.h"
5 #include "stylemanager_zz.h"
6 #include <ClanLib/gui.h>
7
8 #pragma warning ( push, 1 )
9 #pragma warning ( disable: 4244 ) // conversion from 'int' to 'float', possible loss of data
10
11
12 CL_Button_ZZ::CL_Button_ZZ(
13 CL_Button *button,
14 CL_StyleManager_ZZ *style,
15 ButtonType button_type,
16 ButtonSize button_size)
17 :
18 CL_ComponentStyle(button),
19 button_type_(button_type),
20 button_size_(button_size),
21 style_(style),
22 button_(button),
23 timer_(10),
24 surface_up_(0),
25 surface_down_(0),
26 surface_highlighted_(0),
27 surface_disabled_(0),
28 down_(false),
29 highlighted_(false),
30 resources_(style->get_resources())
31 {
32 font_ = new CL_Font("Button/font", resources_);
33
34 CL_Size text_size = font_->get_size(button_->get_text().c_str());
35 {
36 slots_.connect(button_->sig_mouse_enter(),
37 this, &CL_Button_ZZ::on_mouse_enter);
38 slots_.connect(button_->sig_mouse_leave(),
39 this, &CL_Button_ZZ::on_mouse_leave);
40 slots_.connect(timer_.sig_timer(),
41 this, &CL_Button_ZZ::on_timer);
42 slots_.connect(button_->sig_get_preferred_size(),
43 this, &CL_Button_ZZ::on_get_preferred_size);
44 }
45
46 timer_.disable();
47 highlight_modifier_ = 200;
48 clr_font_.set_color(
49 highlight_modifier_,
50 highlight_modifier_,
51 highlight_modifier_);
52
53 slots_.connect(button_->sig_paint(), this, &CL_Button_ZZ::on_paint);
54 slots_.connect(button_->sig_pressed(), this, &CL_Button_ZZ::on_pressed);
55 slots_.connect(button_->sig_released(), this, &CL_Button_ZZ::on_released);
56 slots_.connect(button_->sig_set_options(), this, &CL_Button_ZZ::on_set_options);
57
58 button->find_preferred_size();
59 }
60
61 CL_Button_ZZ::~CL_Button_ZZ()
62 {
63 delete font_;
64 delete surface_disabled_;
65 delete surface_up_;
66 delete surface_down_;
67 delete surface_highlighted_;
68 }
69
70 void CL_Button_ZZ::on_get_preferred_size(CL_Size &size)
71 {
72 if (button_type_ == type_bitmap)
73 {
74 if (surface_up_)
75 {
76 size.width = surface_up_->get_width();
77 size.height = surface_up_->get_height();
78 }
79 }
80 else
81 {
82 size.width = font_->get_width(button_->get_text()) + 4;
83 size.height = font_->get_height(button_->get_text()) + 4;
84 }
85 }
86
87 void CL_Button_ZZ::on_mouse_enter()
88 {
89 timer_.enable();
90 highlighted_ = true;
91
92 highlighting_direction_ = true;
93 }
94
95 void CL_Button_ZZ::on_mouse_leave()
96 {
97 timer_.enable();
98 highlighted_ = false;
99 highlighting_direction_ = false;
100 }
101
102 void CL_Button_ZZ::on_timer()
103 {
104 if (highlighting_direction_)
105 {
106 highlight_modifier_ += 2;
107 if (highlight_modifier_ > 255)
108 {
109 highlight_modifier_ = 255;
110 timer_.disable();
111 }
112 }
113 else
114 {
115 highlight_modifier_ -= 2;
116 if (highlight_modifier_ < 200)
117 {
118 highlight_modifier_ = 200;
119 timer_.disable();
120 }
121 }
122 clr_font_.set_color(
123 highlight_modifier_,
124 highlight_modifier_,
125 highlight_modifier_);
126 }
127
128 void CL_Button_ZZ::on_paint()
129 {
130 if(button_type_ == type_text)
131 on_paint_anim();
132 else
133 if (button_type_ == type_bitmap)
134 on_paint_texture();
135 else
136 cl_throw_error("CL_Button_ZZ::on_paint, Unknown button type " +
137 CL_String::from_int(button_type_));
138 }
139
140 void CL_Button_ZZ::on_paint_texture()
141 {
142 CL_Surface * surface = 0;
143 if(down_)
144 {
145 surface = surface_down_;
146 }
147 else
148 if (highlighted_)
149 {
150 surface = surface_highlighted_;
151 }
152 else
153 if (!button_->is_enabled())
154 {
155 surface = surface_disabled_;
156 }
157 else
158 surface = surface_up_;
159
160 if (surface)
161 surface->draw(button_->get_screen_x(), button_->get_screen_y());
162 else
163 if (surface_up_)
164 surface_up_->draw(button_->get_screen_x(), button_->get_screen_y());
165 }
166
167 void CL_Button_ZZ::on_paint_anim()
168 {
169 int text_width = font_->get_width(button_->get_text());
170 int button_width = button_->get_width();
171 int text_height = font_->get_height();
172 int button_height = button_->get_height();
173
174 int x_pos = button_->get_screen_x() + (button_width - text_width) / 2;
175 int y_pos = button_->get_screen_y() + (button_height - text_height) / 2;
176
177 bool being_clicked = button_->has_mouse_over() ? down_ : false;
178 if((button_->is_down() && !being_clicked) || (!button_->is_down() && being_clicked))
179 {
180 font_->set_color(
181 255 / 255.0f,
182 255 / 255.0f,
183 255 / 255.0f);
184 x_pos += 1;
185 y_pos += 2;
186 }
187 else
188 {
189 if(highlighted_)
190 {
191 }
192 font_->set_color(
193 clr_font_.get_red() / 255.0f,
194 clr_font_.get_green() / 255.0f,
195 clr_font_.get_blue() / 255.0f);
196 }
197 font_->draw(x_pos, y_pos, button_->get_text());
198 }
199
200 void CL_Button_ZZ::on_pressed()
201 {
202 down_ = true;
203 }
204
205 void CL_Button_ZZ::on_released()
206 {
207 down_ = false;
208 }
209
210 void CL_Button_ZZ::set_button_type(ButtonType const button_type)
211 {
212 button_type_ = button_type;
213 button_->find_preferred_size();
214 }
215
216 void CL_Button_ZZ::set_button_size(ButtonSize const button_size)
217 {
218 if (button_type_ == type_text)
219 {
220 if (button_size_ != button_size)
221 {
222 if (button_size_ == size_big)
223 {
224 delete font_;
225 font_ = new CL_Font("Button/font_big", resources_);
226 }
227 else
228 {
229 delete font_;
230 font_ = new CL_Font("Button/font", resources_);
231 }
232 button_size_ = button_size;
233
234 button_->find_preferred_size();
235 }
236 }
237 else
238 button_size_ = button_size;
239 }
240
241 void CL_Button_ZZ::set_surface_up(CL_Surface * surface_up)
242 {
243 delete surface_up_;
244 surface_up_ = surface_up;
245
246 if (button_type_ == type_bitmap)
247 button_->find_preferred_size();
248 }
249
250 void CL_Button_ZZ::set_surface_down(CL_Surface * surface_down)
251 {
252 delete surface_down_;
253 surface_down_ = surface_down;
254 }
255
256 void CL_Button_ZZ::set_surface_highlighted(CL_Surface * surface_highlighted)
257 {
258 delete surface_highlighted_;
259 surface_highlighted_ = surface_highlighted;
260 }
261
262 void CL_Button_ZZ::set_surface_disabled(CL_Surface * surface_disabled)
263 {
264 delete surface_disabled_;
265 surface_disabled_ = surface_disabled;
266 }
267
268 void CL_Button_ZZ::set_surfaces(CL_Surface * surface_up,
269 CL_Surface * surface_down,
270 CL_Surface * surface_highlighted,
271 CL_Surface * surface_disabled)
272 {
273 delete surface_up_;
274 surface_up_ = surface_up;
275 delete surface_down_;
276 surface_down_ = surface_down;
277 delete surface_highlighted_;
278 surface_highlighted_ = surface_highlighted;
279 delete surface_disabled_;
280 surface_disabled_ = surface_disabled;
281
282 if (button_type_ == type_bitmap)
283 button_->find_preferred_size();
284 }
285
286 void CL_Button_ZZ::set_font(CL_Font * font)
287 {
288 if (font)
289 {
290 delete font_;
291 font_ = font;
292
293 if (button_type_ == type_text)
294 button_->find_preferred_size();
295 }
296 }
297
298 void CL_Button_ZZ::on_set_options(const CL_DomElement &options)
299 {
300 if (options.has_attribute("surface_up"))
301 set_surface_up(new CL_Surface(options.get_attribute("surface_up"), resources_));
302
303 if (options.has_attribute("surface_down"))
304 set_surface_down(new CL_Surface(options.get_attribute("surface_down"), resources_));
305
306 if (options.has_attribute("surface_disabled"))
307 set_surface_disabled(new CL_Surface(options.get_attribute("surface_disabled"), resources_));
308
309 if (options.has_attribute("surface_highlighted"))
310 set_surface_highlighted(new CL_Surface(options.get_attribute("surface_highlighted"), resources_));
311
312 if (options.has_attribute("font"))
313 set_font(new CL_Font(options.get_attribute("font"), resources_));
314
315 std::string const btn_type(options.get_attribute("type"));
316 if (btn_type == "bitmap")
317 set_button_type(type_bitmap);
318 else
319 set_button_type(type_text);
320
321 std::string const btn_size = options.get_attribute("size");
322 if (btn_size == "big")
323 set_button_size(size_big);
324 else
325 set_button_size(size_normal);
326
327 // Calculate type of button, if not setuped
328 if (!options.has_attribute("text"))
329 {
330 if (surface_up_)
331 set_button_type(type_bitmap);
332 }
333
334 if(options.has_attribute("font"))
335 set_font(new CL_Font(options.get_attribute("font"), resources_));
336 else
337 {
338 if (button_type_ == type_text)
339 {
340 if (button_size_ == size_big)
341 {
342 delete font_;
343 font_ = new CL_Font("Button/font_big", resources_);
344 }
345 else
346 {
347 delete font_;
348 font_ = new CL_Font("Button/font", resources_);
349 }
350 }
351 }
352 }
353
354 #pragma warning ( pop )