… | |
… | |
7 | |
7 | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $TOPLEVEL; |
12 | our $ROOT; |
|
|
13 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
14 | our $BUTTON_STATE; |
|
|
15 | |
|
|
16 | sub check_tooltip { |
|
|
17 | if (!$GRAB) { |
|
|
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
19 | if (length $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_text ($widget->{tooltip}); |
|
|
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
|
|
30 | $TOOLTIP->show; |
|
|
31 | } |
|
|
32 | |
|
|
33 | return; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->hide; |
|
|
39 | delete $TOOLTIP->{owner}; |
|
|
40 | } |
14 | |
41 | |
15 | # class methods for events |
42 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
43 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
44 | $FOCUS->key_down ($_[0]) if $FOCUS; |
18 | } |
45 | } |
… | |
… | |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
48 | $FOCUS->key_up ($_[0]) if $FOCUS; |
22 | } |
49 | } |
23 | |
50 | |
24 | sub feed_sdl_button_down_event { |
51 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
52 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
53 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
54 | |
28 | if (!$BUTTON_STATE) { |
55 | if (!$BUTTON_STATE) { |
29 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
56 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
57 | |
31 | $GRAB = $widget; |
58 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
59 | $GRAB->update if $GRAB; |
33 | } |
|
|
34 | |
60 | |
|
|
61 | check_tooltip; |
|
|
62 | } |
|
|
63 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
64 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
65 | |
37 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
66 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
67 | } |
39 | |
68 | |
40 | sub feed_sdl_button_up_event { |
69 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
70 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
71 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
72 | |
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
73 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
74 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
75 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
76 | |
48 | $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
77 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
78 | |
50 | if (!$BUTTON_STATE) { |
79 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
80 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
81 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
82 | $GRAB->update if $GRAB; |
|
|
83 | |
|
|
84 | check_tooltip; |
54 | } |
85 | } |
55 | } |
86 | } |
56 | |
87 | |
57 | sub feed_sdl_motion_event { |
88 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
89 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
90 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
91 | |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
92 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
93 | |
63 | if ($widget != $HOVER) { |
94 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
95 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
96 | |
66 | $hover->update if $hover; |
97 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER; |
98 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
|
|
69 | |
99 | |
|
|
100 | check_tooltip; |
|
|
101 | } |
|
|
102 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
103 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
|
|
104 | } |
|
|
105 | |
|
|
106 | # convert position array to integers |
|
|
107 | sub harmonize { |
|
|
108 | my ($vals) = @_; |
|
|
109 | |
|
|
110 | my $rem = 0; |
|
|
111 | |
|
|
112 | for (@$vals) { |
|
|
113 | my $i = int $_ + $rem; |
|
|
114 | $rem += $_ - $i; |
|
|
115 | $_ = $i; |
|
|
116 | } |
71 | } |
117 | } |
72 | |
118 | |
73 | ############################################################################# |
119 | ############################################################################# |
74 | |
120 | |
75 | package CFClient::UI::Base; |
121 | package CFClient::UI::Base; |
76 | |
122 | |
77 | use strict; |
123 | use strict; |
78 | |
124 | |
79 | use SDL::OpenGL; |
125 | use CFClient::OpenGL; |
80 | |
126 | |
81 | sub new { |
127 | sub new { |
82 | my $class = shift; |
128 | my $class = shift; |
83 | |
129 | |
84 | my $self = bless { |
130 | my $self = bless { |
85 | x => 0, |
131 | x => 0, |
86 | y => 0, |
132 | y => 0, |
87 | z => 0, |
133 | z => 0, |
88 | w => -1, |
134 | can_events => 1, |
89 | h => -1, |
|
|
90 | @_ |
135 | @_ |
91 | }, $class; |
136 | }, $class; |
92 | |
137 | |
93 | for (keys %$self) { |
138 | for (keys %$self) { |
94 | if (/^connect_(.*)$/) { |
139 | if (/^connect_(.*)$/) { |
… | |
… | |
97 | } |
142 | } |
98 | |
143 | |
99 | $self |
144 | $self |
100 | } |
145 | } |
101 | |
146 | |
|
|
147 | sub destroy { |
|
|
148 | my ($self) = @_; |
|
|
149 | |
|
|
150 | $self->hide; |
|
|
151 | %$self = (); |
|
|
152 | } |
|
|
153 | |
|
|
154 | sub show { |
|
|
155 | my ($self) = @_; |
|
|
156 | |
|
|
157 | return if $self->{parent}; |
|
|
158 | |
|
|
159 | $CFClient::UI::ROOT->add ($self); |
|
|
160 | } |
|
|
161 | |
|
|
162 | sub hide { |
|
|
163 | my ($self) = @_; |
|
|
164 | |
|
|
165 | return unless $self->{parent}; |
|
|
166 | |
|
|
167 | $self->{parent}->remove ($self); |
|
|
168 | } |
|
|
169 | |
102 | sub move { |
170 | sub move { |
103 | my ($self, $x, $y, $z) = @_; |
171 | my ($self, $x, $y, $z) = @_; |
|
|
172 | |
104 | $self->{x} = $x; |
173 | $self->{x} = int $x; |
105 | $self->{y} = $y; |
174 | $self->{y} = int $y; |
106 | $self->{z} = $z if defined $z; |
175 | $self->{z} = $z if defined $z; |
107 | } |
|
|
108 | |
176 | |
109 | sub needs_redraw { |
177 | $self->update; |
110 | 0 |
178 | } |
|
|
179 | |
|
|
180 | sub set_size { |
|
|
181 | my ($self, $w, $h) = @_; |
|
|
182 | |
|
|
183 | $self->{user_w} = $w; |
|
|
184 | $self->{user_h} = $h; |
|
|
185 | |
|
|
186 | $self->check_size; |
111 | } |
187 | } |
112 | |
188 | |
113 | sub size_request { |
189 | sub size_request { |
114 | require Carp; |
190 | require Carp; |
115 | Carp::confess "size_request is abtract"; |
191 | Carp::confess "size_request is abstract"; |
116 | } |
192 | } |
117 | |
193 | |
118 | sub _size_allocate { |
194 | sub configure { |
119 | my ($self, $x, $y, $w, $h) = @_; |
195 | my ($self, $x, $y, $w, $h) = @_; |
120 | |
196 | |
|
|
197 | if ($self->{aspect}) { |
|
|
198 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
199 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
200 | |
|
|
201 | # use alignment to adjust x, y |
|
|
202 | |
|
|
203 | $x += int +($w - $w2) * 0.5; |
|
|
204 | $y += int +($h - $h2) * 0.5; |
|
|
205 | |
|
|
206 | ($w, $h) = ($w2, $h2); |
|
|
207 | } |
|
|
208 | |
|
|
209 | if ($self->{x} != $x || $self->{y} != $y) { |
121 | $self->{x} = $x; |
210 | $self->{x} = $x; |
122 | $self->{y} = $y; |
211 | $self->{y} = $y; |
|
|
212 | $self->update; |
|
|
213 | } |
123 | |
214 | |
124 | return unless $self->{w} != $w || $self->{h} != $h; |
215 | if ($self->{w} != $w || $self->{h} != $h) { |
125 | |
|
|
126 | $self->{w} = $w; |
216 | $self->{w} = $w; |
127 | $self->{h} = $h; |
217 | $self->{h} = $h; |
128 | |
218 | |
|
|
219 | $self->size_allocate ($w, $h); |
|
|
220 | $self->update; |
129 | 1 |
221 | } |
130 | } |
222 | } |
131 | |
223 | |
132 | sub size_allocate { |
224 | sub size_allocate { |
|
|
225 | # nothing to be done |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub set_max_size { |
133 | my ($self, $x, $y, $w, $h) = @_; |
229 | my ($self, $w, $h) = @_; |
134 | |
230 | |
135 | $self->_size_allocate ($x, $y, $w, $h); |
231 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
232 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
|
|
233 | } |
|
|
234 | |
|
|
235 | # return top left coordinates |
|
|
236 | sub _topleft { |
|
|
237 | my ($self, $x, $y) = @_; |
|
|
238 | |
|
|
239 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
136 | } |
240 | } |
137 | |
241 | |
138 | # translate global coordinates to local coordinate system |
242 | # translate global coordinates to local coordinate system |
139 | sub translate { |
243 | sub coord2local { |
140 | my ($self, $x, $y) = @_; |
244 | my ($self, $x, $y) = @_; |
141 | |
245 | |
142 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
246 | my ($X, $Y) = $self->_topleft; |
|
|
247 | ($x - $X, $y - $Y) |
|
|
248 | } |
|
|
249 | |
|
|
250 | # translate local coordinates to global coordinate system |
|
|
251 | sub coord2global { |
|
|
252 | my ($self, $x, $y) = @_; |
|
|
253 | |
|
|
254 | my ($X, $Y) = $self->_topleft; |
|
|
255 | ($x + $X, $y + $Y) |
143 | } |
256 | } |
144 | |
257 | |
145 | sub focus_in { |
258 | sub focus_in { |
146 | my ($self) = @_; |
259 | my ($self) = @_; |
147 | |
260 | |
148 | return if $FOCUS == $self; |
261 | return if $FOCUS == $self; |
|
|
262 | return unless $self->{can_focus}; |
149 | |
263 | |
150 | my $focus = $FOCUS; $FOCUS = $self; |
264 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
265 | |
|
|
266 | $self->emit (focus_in => $focus); |
|
|
267 | |
151 | $focus->update if $focus; |
268 | $focus->update if $focus; |
152 | $FOCUS->update; |
269 | $FOCUS->update; |
153 | } |
270 | } |
154 | |
271 | |
155 | sub focus_out { |
272 | sub focus_out { |
156 | my ($self) = @_; |
273 | my ($self) = @_; |
157 | |
274 | |
158 | return unless $FOCUS == $self; |
275 | return unless $FOCUS == $self; |
159 | |
276 | |
160 | my $focus = $FOCUS; undef $FOCUS; |
277 | my $focus = $FOCUS; undef $FOCUS; |
|
|
278 | |
|
|
279 | $self->emit (focus_out => $focus); |
|
|
280 | |
161 | $focus->update if $focus; #? |
281 | $focus->update if $focus; #? |
162 | } |
282 | } |
163 | |
283 | |
164 | sub mouse_motion { } |
284 | sub mouse_motion { } |
165 | sub button_up { } |
285 | sub button_up { } |
… | |
… | |
186 | glPushMatrix; |
306 | glPushMatrix; |
187 | glTranslate $self->{x}, $self->{y}, 0; |
307 | glTranslate $self->{x}, $self->{y}, 0; |
188 | $self->_draw; |
308 | $self->_draw; |
189 | glPopMatrix; |
309 | glPopMatrix; |
190 | |
310 | |
191 | if ($self == $HOVER) { |
311 | if ($self == $HOVER && $self->{can_hover}) { |
192 | my ($x, $y) = @$self{qw(x y)}; |
312 | my ($x, $y) = @$self{qw(x y)}; |
193 | |
313 | |
194 | glColor 0, 0, 1, 0.2; |
314 | glColor 1, 0.8, 0.5, 0.2; |
195 | glEnable GL_BLEND; |
315 | glEnable GL_BLEND; |
196 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
316 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
197 | glBegin GL_QUADS; |
317 | glBegin GL_QUADS; |
198 | glVertex $x , $y; |
318 | glVertex $x , $y; |
199 | glVertex $x + $self->{w}, $y; |
319 | glVertex $x + $self->{w}, $y; |
… | |
… | |
211 | } |
331 | } |
212 | |
332 | |
213 | sub find_widget { |
333 | sub find_widget { |
214 | my ($self, $x, $y) = @_; |
334 | my ($self, $x, $y) = @_; |
215 | |
335 | |
|
|
336 | return () unless $self->{can_events}; |
|
|
337 | |
216 | return $self |
338 | return $self |
217 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
339 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
218 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
340 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
219 | |
341 | |
220 | () |
342 | () |
221 | } |
343 | } |
222 | |
344 | |
223 | sub set_parent { |
345 | sub set_parent { |
224 | my ($self, $par) = @_; |
346 | my ($self, $parent) = @_; |
225 | |
347 | |
226 | $self->{parent} = $par; |
|
|
227 | Scalar::Util::weaken $self->{parent}; |
348 | Scalar::Util::weaken ($self->{parent} = $parent); |
228 | } |
349 | } |
229 | |
350 | |
230 | sub get_parent { |
351 | sub check_size { |
|
|
352 | my ($self) = @_; |
|
|
353 | |
231 | $_[0]->{parent} |
354 | $self->{parent} |
|
|
355 | or return 1; |
|
|
356 | |
|
|
357 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
358 | ? @$self{qw(user_w user_h)} |
|
|
359 | : $self->size_request; |
|
|
360 | |
|
|
361 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
362 | $self->{req_w} = $w; |
|
|
363 | $self->{req_h} = $h; |
|
|
364 | |
|
|
365 | $self->{parent}->check_size |
|
|
366 | or $self->size_allocate ( |
|
|
367 | (List::Util::max $self->{w}, $w), |
|
|
368 | (List::Util::max $self->{h}, $h), |
|
|
369 | ); |
|
|
370 | |
|
|
371 | 1 |
|
|
372 | } else { |
|
|
373 | 0 |
|
|
374 | } |
232 | } |
375 | } |
233 | |
376 | |
234 | sub update { |
377 | sub update { |
235 | my ($self) = @_; |
378 | my ($self) = @_; |
236 | |
379 | |
… | |
… | |
239 | } |
382 | } |
240 | |
383 | |
241 | sub connect { |
384 | sub connect { |
242 | my ($self, $signal, $cb) = @_; |
385 | my ($self, $signal, $cb) = @_; |
243 | |
386 | |
244 | push @{ $self->{cb}{$signal} }, $cb; |
387 | push @{ $self->{signal_cb}{$signal} }, $cb; |
245 | } |
388 | } |
246 | |
389 | |
247 | sub emit { |
390 | sub emit { |
248 | my ($self, $signal, @args) = @_; |
391 | my ($self, $signal, @args) = @_; |
249 | |
392 | |
|
|
393 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
250 | $_->($self, @args) |
394 | $cb->($self, @args); |
251 | for @{$self->{cb}{$signal} || []}; |
395 | } |
252 | } |
396 | } |
253 | |
397 | |
254 | sub DESTROY { |
398 | sub DESTROY { |
255 | my ($self) = @_; |
399 | my ($self) = @_; |
256 | |
400 | |
… | |
… | |
262 | package CFClient::UI::DrawBG; |
406 | package CFClient::UI::DrawBG; |
263 | |
407 | |
264 | our @ISA = CFClient::UI::Base::; |
408 | our @ISA = CFClient::UI::Base::; |
265 | |
409 | |
266 | use strict; |
410 | use strict; |
267 | use SDL::OpenGL; |
411 | use CFClient::OpenGL; |
268 | |
412 | |
269 | sub new { |
413 | sub new { |
270 | my $class = shift; |
414 | my $class = shift; |
271 | |
415 | |
272 | # range [value, low, high, page] |
416 | # range [value, low, high, page] |
… | |
… | |
301 | |
445 | |
302 | package CFClient::UI::Empty; |
446 | package CFClient::UI::Empty; |
303 | |
447 | |
304 | our @ISA = CFClient::UI::Base::; |
448 | our @ISA = CFClient::UI::Base::; |
305 | |
449 | |
|
|
450 | sub new { |
|
|
451 | my ($class, %arg) = @_; |
|
|
452 | $class->SUPER::new (can_events => 0, %arg); |
|
|
453 | } |
|
|
454 | |
306 | sub size_request { |
455 | sub size_request { |
307 | (0, 0) |
456 | (0, 0) |
308 | } |
457 | } |
309 | |
458 | |
310 | sub draw { } |
459 | sub draw { } |
… | |
… | |
318 | sub new { |
467 | sub new { |
319 | my ($class, %arg) = @_; |
468 | my ($class, %arg) = @_; |
320 | |
469 | |
321 | my $children = delete $arg{children} || []; |
470 | my $children = delete $arg{children} || []; |
322 | |
471 | |
323 | my $self = $class->SUPER::new (children => [], %arg); |
472 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
324 | $self->add ($_) for @$children; |
473 | $self->add ($_) for @$children; |
325 | |
474 | |
326 | $self |
475 | $self |
327 | } |
476 | } |
328 | |
477 | |
329 | sub add { |
478 | sub add { |
330 | my ($self, $chld) = @_; |
479 | my ($self, $child) = @_; |
331 | |
480 | |
332 | $chld->set_parent ($self); |
481 | $child->set_parent ($self); |
|
|
482 | |
|
|
483 | use sort 'stable'; |
333 | |
484 | |
334 | $self->{children} = [ |
485 | $self->{children} = [ |
335 | sort { $a->{z} <=> $b->{z} } |
486 | sort { $a->{z} <=> $b->{z} } |
336 | @{$self->{children}}, $chld |
487 | @{$self->{children}}, $child |
337 | ]; |
488 | ]; |
338 | |
489 | |
339 | $self->{w} = $self->{h} = -1; |
490 | $child->check_size; |
|
|
491 | } |
|
|
492 | |
|
|
493 | sub remove { |
|
|
494 | my ($self, $child) = @_; |
|
|
495 | |
|
|
496 | delete $child->{parent}; |
|
|
497 | |
|
|
498 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
499 | |
|
|
500 | $self->check_size; |
340 | $self->update; |
501 | $self->update; |
341 | } |
|
|
342 | |
|
|
343 | sub remove { |
|
|
344 | my ($self, $widget) = @_; |
|
|
345 | |
|
|
346 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
|
|
347 | |
|
|
348 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
|
|
349 | } |
502 | } |
350 | |
503 | |
351 | sub find_widget { |
504 | sub find_widget { |
352 | my ($self, $x, $y) = @_; |
505 | my ($self, $x, $y) = @_; |
353 | |
506 | |
… | |
… | |
383 | |
536 | |
384 | $class->SUPER::new (children => [$child], %arg) |
537 | $class->SUPER::new (children => [$child], %arg) |
385 | } |
538 | } |
386 | |
539 | |
387 | sub add { |
540 | sub add { |
388 | my ($self, $widget) = @_; |
541 | my ($self, $child) = @_; |
389 | |
542 | |
390 | $self->{children} = []; |
543 | $self->{children} = []; |
391 | |
544 | |
392 | $self->SUPER::add ($widget); |
545 | $self->SUPER::add ($child); |
393 | } |
546 | } |
394 | |
547 | |
395 | sub remove { |
548 | sub remove { |
396 | my ($self, $widget) = @_; |
549 | my ($self, $widget) = @_; |
397 | |
550 | |
… | |
… | |
406 | sub size_request { |
559 | sub size_request { |
407 | $_[0]{children}[0]->size_request |
560 | $_[0]{children}[0]->size_request |
408 | } |
561 | } |
409 | |
562 | |
410 | sub size_allocate { |
563 | sub size_allocate { |
411 | my ($self, $x, $y, $w, $h) = @_; |
564 | my ($self, $w, $h) = @_; |
412 | |
565 | |
413 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
414 | |
|
|
415 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
566 | $self->{children}[0]->configure (0, 0, $w, $h); |
416 | } |
567 | } |
417 | |
568 | |
418 | ############################################################################# |
569 | ############################################################################# |
419 | |
570 | |
420 | package CFClient::UI::Window; |
571 | package CFClient::UI::Window; |
421 | |
572 | |
422 | our @ISA = CFClient::UI::Bin::; |
573 | our @ISA = CFClient::UI::Bin::; |
423 | |
574 | |
424 | use SDL::OpenGL; |
575 | use CFClient::OpenGL; |
425 | |
576 | |
426 | sub new { |
577 | sub new { |
427 | my ($class, %arg) = @_; |
578 | my ($class, %arg) = @_; |
428 | |
579 | |
429 | my $self = $class->SUPER::new (%arg); |
580 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
438 | } |
589 | } |
439 | |
590 | |
440 | sub render_chld { |
591 | sub render_chld { |
441 | my ($self) = @_; |
592 | my ($self) = @_; |
442 | |
593 | |
443 | $self->{texture} = |
594 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
444 | CFClient::Texture->new_from_opengl ( |
595 | glClearColor 0, 0, 0, 1; |
445 | $self->{w}, $self->{h}, sub { $self->child->draw } |
596 | glClear GL_COLOR_BUFFER_BIT; |
446 | ); |
597 | $self->child->draw; |
|
|
598 | }; |
447 | } |
599 | } |
448 | |
600 | |
449 | sub size_allocate { |
601 | sub size_allocate { |
450 | my ($self, $x, $y, $w, $h) = @_; |
602 | my ($self, $w, $h) = @_; |
451 | |
603 | |
452 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
453 | |
|
|
454 | $self->child->size_allocate (0, 0, $w, $h); |
604 | $self->child->configure (0, 0, $w, $h); |
455 | |
605 | |
456 | $self->render_chld; |
606 | $self->render_chld; |
457 | } |
607 | } |
458 | |
608 | |
459 | sub _draw { |
609 | sub _draw { |
… | |
… | |
475 | glDisable GL_TEXTURE_2D; |
625 | glDisable GL_TEXTURE_2D; |
476 | } |
626 | } |
477 | |
627 | |
478 | ############################################################################# |
628 | ############################################################################# |
479 | |
629 | |
|
|
630 | package CFClient::UI::ViewPort; |
|
|
631 | |
|
|
632 | our @ISA = CFClient::UI::Window::; |
|
|
633 | |
|
|
634 | sub new { die } |
|
|
635 | |
|
|
636 | sub size_request { |
|
|
637 | my ($self) = @_; |
|
|
638 | |
|
|
639 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
|
|
640 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
|
|
641 | |
|
|
642 | @$self{qw(child_w child_h)} |
|
|
643 | } |
|
|
644 | |
|
|
645 | sub _draw { |
|
|
646 | my ($self) = @_; |
|
|
647 | |
|
|
648 | $self->{children}[1]->draw; |
|
|
649 | } |
|
|
650 | |
|
|
651 | |
|
|
652 | ############################################################################# |
|
|
653 | |
480 | package CFClient::UI::Frame; |
654 | package CFClient::UI::Frame; |
481 | |
655 | |
482 | our @ISA = CFClient::UI::Bin::; |
656 | our @ISA = CFClient::UI::Bin::; |
483 | |
657 | |
484 | use SDL::OpenGL; |
658 | use CFClient::OpenGL; |
485 | |
659 | |
486 | sub size_request { |
660 | sub new { |
487 | my ($self) = @_; |
661 | my $class = shift; |
488 | my $chld = $self->child |
|
|
489 | or return (0, 0); |
|
|
490 | |
662 | |
491 | $chld->move (2, 2); |
663 | my $self = $class->SUPER::new ( |
492 | |
664 | bg => [1, 1, 1, 1], |
493 | map { $_ + 4 } $chld->size_request; |
665 | border_bg => [1, 1, 1, 1], |
494 | } |
666 | border => 0.8, |
495 | |
667 | @_ |
496 | sub size_allocate { |
|
|
497 | my ($self, $x, $y, $w, $h) = @_; |
|
|
498 | |
668 | ); |
499 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
500 | |
669 | |
501 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
670 | $self |
502 | } |
671 | } |
503 | |
672 | |
504 | sub _draw { |
673 | sub _draw { |
505 | my ($self) = @_; |
674 | my ($self) = @_; |
506 | |
675 | |
507 | my $chld = $self->child; |
|
|
508 | |
|
|
509 | my ($w, $h) = $chld->size_request; |
|
|
510 | |
|
|
511 | glBegin GL_QUADS; |
|
|
512 | glColor 0, 0, 0; |
|
|
513 | glVertex 0 , 0; |
|
|
514 | glVertex 0 , $h + 4; |
|
|
515 | glVertex $w + 4 , $h + 4; |
|
|
516 | glVertex $w + 4 , 0; |
|
|
517 | glEnd; |
|
|
518 | |
|
|
519 | $chld->draw; |
|
|
520 | } |
|
|
521 | |
|
|
522 | ############################################################################# |
|
|
523 | |
|
|
524 | package CFClient::UI::FancyFrame; |
|
|
525 | |
|
|
526 | our @ISA = CFClient::UI::Bin::; |
|
|
527 | |
|
|
528 | use SDL::OpenGL; |
|
|
529 | |
|
|
530 | my @tex = |
|
|
531 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
532 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
533 | |
|
|
534 | sub size_request { |
|
|
535 | my ($self) = @_; |
|
|
536 | |
|
|
537 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
538 | |
|
|
539 | my ($w, $h) = $self->SUPER::size_request; |
|
|
540 | |
|
|
541 | $h += $tex[1]->{h}; |
|
|
542 | $h += $tex[4]->{h}; |
|
|
543 | $w += $tex[2]->{w}; |
|
|
544 | $w += $tex[3]->{w}; |
|
|
545 | |
|
|
546 | ($w, $h) |
|
|
547 | } |
|
|
548 | |
|
|
549 | sub size_allocate { |
|
|
550 | my ($self, $x, $y, $w, $h) = @_; |
|
|
551 | |
|
|
552 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
553 | |
|
|
554 | $h -= $tex[1]{h}; |
|
|
555 | $h -= $tex[4]{h}; |
|
|
556 | $w -= $tex[2]{w}; |
|
|
557 | $w -= $tex[3]{w}; |
|
|
558 | |
|
|
559 | $h = $h < 0 ? 0 : $h; |
|
|
560 | $w = $w < 0 ? 0 : $w; |
|
|
561 | |
|
|
562 | my $child = $self->child; |
|
|
563 | |
|
|
564 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
|
|
565 | } |
|
|
566 | |
|
|
567 | sub button_down { |
|
|
568 | my ($self, $ev, $x, $y) = @_; |
|
|
569 | |
|
|
570 | if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w} |
|
|
571 | && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) { |
|
|
572 | |
|
|
573 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
574 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
575 | |
|
|
576 | $self->{motion} = sub { |
|
|
577 | my ($ev, $x, $y) = @_; |
|
|
578 | |
|
|
579 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
580 | |
|
|
581 | $self->{user_w} = $bw + $x - $ox; |
|
|
582 | $self->{user_h} = $bh + $y - $oy; |
|
|
583 | $self->update; |
|
|
584 | }; |
|
|
585 | |
|
|
586 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
587 | && $y >= 0 && $y < $tex[1]{h}) { |
|
|
588 | |
|
|
589 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
590 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
591 | |
|
|
592 | $self->{motion} = sub { |
|
|
593 | my ($ev, $x, $y) = @_; |
|
|
594 | |
|
|
595 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
596 | |
|
|
597 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
598 | $self->update; |
|
|
599 | }; |
|
|
600 | } |
|
|
601 | } |
|
|
602 | |
|
|
603 | sub button_up { |
|
|
604 | my ($self, $ev, $x, $y) = @_; |
|
|
605 | |
|
|
606 | delete $self->{motion}; |
|
|
607 | } |
|
|
608 | |
|
|
609 | sub mouse_motion { |
|
|
610 | my ($self, $ev, $x, $y) = @_; |
|
|
611 | |
|
|
612 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
613 | } |
|
|
614 | |
|
|
615 | sub _draw { |
|
|
616 | my ($self) = @_; |
|
|
617 | |
|
|
618 | my ($w, $h) = ($self->{w}, $self->{h}); |
676 | my ($w, $h) = ($self->{w}, $self->{h}); |
619 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
|
|
620 | |
677 | |
621 | glEnable GL_BLEND; |
678 | glEnable GL_BLEND; |
622 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
679 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
623 | glEnable GL_TEXTURE_2D; |
680 | glEnable GL_TEXTURE_2D; |
624 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
681 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
625 | |
682 | |
626 | my $top = $tex[1]; |
683 | # glBegin GL_QUADS; |
|
|
684 | # glColor 0, 0, 0, 0; |
|
|
685 | # glVertex 0 , 0; |
|
|
686 | # glVertex 0 , $h; |
|
|
687 | # glVertex $w, $h; |
|
|
688 | # glVertex $w, 0; |
|
|
689 | # glEnd; |
|
|
690 | |
|
|
691 | |
|
|
692 | $self->child->draw; |
|
|
693 | glDisable GL_BLEND; |
|
|
694 | glDisable GL_TEXTURE_2D; |
|
|
695 | } |
|
|
696 | |
|
|
697 | ############################################################################# |
|
|
698 | |
|
|
699 | package CFClient::UI::FancyFrame; |
|
|
700 | |
|
|
701 | our @ISA = CFClient::UI::Bin::; |
|
|
702 | |
|
|
703 | use CFClient::OpenGL; |
|
|
704 | |
|
|
705 | my @tex = |
|
|
706 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
707 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
708 | |
|
|
709 | sub new { |
|
|
710 | my $class = shift; |
|
|
711 | |
|
|
712 | # TODO: user_x, user_y, overwrite moveto? |
|
|
713 | |
|
|
714 | my $self = $class->SUPER::new ( |
|
|
715 | bg => [1, 1, 1, 1], |
|
|
716 | border_bg => [1, 1, 1, 1], |
|
|
717 | border => 0.8, |
|
|
718 | can_events => 1, |
|
|
719 | @_ |
|
|
720 | ); |
|
|
721 | |
|
|
722 | $self->{title} &&= new CFClient::UI::Label |
|
|
723 | align => 0, |
|
|
724 | valign => 1, |
|
|
725 | text => $self->{title}, |
|
|
726 | fontsize => 1; |
|
|
727 | |
|
|
728 | $self |
|
|
729 | } |
|
|
730 | |
|
|
731 | sub border { |
|
|
732 | int $_[0]{border} * $::FONTSIZE |
|
|
733 | } |
|
|
734 | |
|
|
735 | sub size_request { |
|
|
736 | my ($self) = @_; |
|
|
737 | |
|
|
738 | my ($w, $h) = $self->SUPER::size_request; |
|
|
739 | |
|
|
740 | ( |
|
|
741 | $w + $self->border * 2, |
|
|
742 | $h + $self->border * 2, |
|
|
743 | ) |
|
|
744 | } |
|
|
745 | |
|
|
746 | sub size_allocate { |
|
|
747 | my ($self, $w, $h) = @_; |
|
|
748 | |
|
|
749 | $h -= List::Util::max 0, $self->border * 2; |
|
|
750 | $w -= List::Util::max 0, $self->border * 2; |
|
|
751 | |
|
|
752 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
753 | if $self->{title}; |
|
|
754 | |
|
|
755 | $self->child->configure ($self->border, $self->border, $w, $h); |
|
|
756 | } |
|
|
757 | |
|
|
758 | sub button_down { |
|
|
759 | my ($self, $ev, $x, $y) = @_; |
|
|
760 | |
|
|
761 | my $border = $self->border; |
|
|
762 | |
|
|
763 | if ($x < $self->{w} && $x >= $self->{w} - $border |
|
|
764 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
|
|
765 | |
|
|
766 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
|
|
767 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
768 | |
|
|
769 | $self->{motion} = sub { |
|
|
770 | my ($ev, $x, $y) = @_; |
|
|
771 | |
|
|
772 | ($x, $y) = ($ev->{x}, $ev->{y}); |
|
|
773 | |
|
|
774 | $self->{user_w} = $bw + $x - $ox; |
|
|
775 | $self->{user_h} = $bh + $y - $oy; |
|
|
776 | $self->check_size; |
|
|
777 | }; |
|
|
778 | |
|
|
779 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
780 | && $y >= 0 && $y < $border) { |
|
|
781 | |
|
|
782 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
|
|
783 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
784 | |
|
|
785 | $self->{motion} = sub { |
|
|
786 | my ($ev, $x, $y) = @_; |
|
|
787 | |
|
|
788 | ($x, $y) = ($ev->{x}, $ev->{y}); |
|
|
789 | |
|
|
790 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
791 | $self->update; |
|
|
792 | }; |
|
|
793 | } |
|
|
794 | } |
|
|
795 | |
|
|
796 | sub button_up { |
|
|
797 | my ($self, $ev, $x, $y) = @_; |
|
|
798 | |
|
|
799 | delete $self->{motion}; |
|
|
800 | } |
|
|
801 | |
|
|
802 | sub mouse_motion { |
|
|
803 | my ($self, $ev, $x, $y) = @_; |
|
|
804 | |
|
|
805 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
806 | } |
|
|
807 | |
|
|
808 | sub _draw { |
|
|
809 | my ($self) = @_; |
|
|
810 | |
|
|
811 | my ($w, $h ) = ($self->{w}, $self->{h}); |
|
|
812 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
|
|
813 | |
|
|
814 | glEnable GL_BLEND; |
|
|
815 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
816 | glEnable GL_TEXTURE_2D; |
|
|
817 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
818 | |
|
|
819 | my $border = $self->border; |
|
|
820 | |
|
|
821 | glColor @{ $self->{border_bg} }; |
627 | $top->draw_quad (0, 0, $w, $top->{h}); |
822 | $tex[1]->draw_quad (0, 0, $w, $border); |
628 | |
823 | $tex[3]->draw_quad (0, $border, $border, $ch); |
629 | my $left = $tex[3]; |
824 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
630 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
825 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
631 | |
|
|
632 | my $right = $tex[2]; |
|
|
633 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
|
|
634 | |
|
|
635 | my $bottom = $tex[4]; |
|
|
636 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
|
|
637 | |
826 | |
638 | my $bg = $tex[0]; |
827 | my $bg = $tex[0]; |
639 | |
828 | |
640 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
829 | # TODO: repeat texture not scale |
641 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
642 | |
|
|
643 | my $rep_x = $cw / $bg->{w}; |
830 | my $rep_x = $cw / $bg->{w}; |
644 | my $rep_y = $ch / $bg->{h}; |
831 | my $rep_y = $ch / $bg->{h}; |
645 | |
832 | |
646 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
833 | glColor @{ $self->{bg} }; |
|
|
834 | |
|
|
835 | $bg->{s} = $rep_x; |
|
|
836 | $bg->{t} = $rep_y; |
|
|
837 | $bg->{wrap_mode} = 1; |
|
|
838 | $bg->draw_quad ($border, $border, $cw, $ch); |
647 | |
839 | |
648 | glDisable GL_TEXTURE_2D; |
840 | glDisable GL_TEXTURE_2D; |
649 | glDisable GL_BLEND; |
841 | glDisable GL_BLEND; |
650 | |
842 | |
|
|
843 | $self->{title}->draw if $self->{title}; |
651 | $self->child->draw; |
844 | $self->child->draw; |
652 | |
|
|
653 | } |
845 | } |
654 | |
846 | |
655 | ############################################################################# |
847 | ############################################################################# |
656 | |
848 | |
657 | package CFClient::UI::Table; |
849 | package CFClient::UI::Table; |
658 | |
850 | |
659 | our @ISA = CFClient::UI::Base::; |
851 | our @ISA = CFClient::UI::Base::; |
660 | |
852 | |
661 | use List::Util qw(max sum); |
853 | use List::Util qw(max sum); |
662 | |
854 | |
663 | use SDL::OpenGL; |
855 | use CFClient::OpenGL; |
664 | |
856 | |
665 | sub new { |
857 | sub new { |
666 | my $class = shift; |
858 | my $class = shift; |
667 | |
859 | |
668 | $class->SUPER::new ( |
860 | $class->SUPER::new ( |
… | |
… | |
670 | @_ |
862 | @_ |
671 | ) |
863 | ) |
672 | } |
864 | } |
673 | |
865 | |
674 | sub add { |
866 | sub add { |
675 | my ($self, $x, $y, $chld) = @_; |
867 | my ($self, $x, $y, $child) = @_; |
676 | |
868 | |
|
|
869 | $child->set_parent ($self); |
677 | $self->{children}[$y][$x] = $chld; |
870 | $self->{children}[$y][$x] = $child; |
678 | $chld->set_parent ($self); |
|
|
679 | |
871 | |
680 | $self->{w} = $self->{h} = -1; |
872 | $child->check_size; |
|
|
873 | } |
|
|
874 | |
|
|
875 | # TODO: move to container class maybe? send childs a signal on removal? |
|
|
876 | sub clear { |
|
|
877 | my ($self) = @_; |
|
|
878 | |
|
|
879 | delete $self->{children}; |
681 | $self->update; |
880 | $self->update; |
682 | } |
881 | } |
683 | |
882 | |
684 | sub get_wh { |
883 | sub get_wh { |
685 | my ($self) = @_; |
884 | my ($self) = @_; |
… | |
… | |
691 | or next; |
890 | or next; |
692 | |
891 | |
693 | for my $x (0 .. $#$row) { |
892 | for my $x (0 .. $#$row) { |
694 | my $widget = $row->[$x] |
893 | my $widget = $row->[$x] |
695 | or next; |
894 | or next; |
696 | my ($w, $h) = $widget->size_request; |
895 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
697 | |
896 | |
698 | $w[$x] = max $w[$x], $w; |
897 | $w[$x] = max $w[$x], $w; |
699 | $h[$y] = max $h[$y], $h; |
898 | $h[$y] = max $h[$y], $h; |
700 | } |
899 | } |
701 | } |
900 | } |
… | |
… | |
713 | (sum @$hs), |
912 | (sum @$hs), |
714 | ) |
913 | ) |
715 | } |
914 | } |
716 | |
915 | |
717 | sub size_allocate { |
916 | sub size_allocate { |
718 | my ($self, $x, $y, $w, $h) = @_; |
917 | my ($self, $w, $h) = @_; |
719 | |
|
|
720 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
721 | |
918 | |
722 | my ($ws, $hs) = $self->get_wh; |
919 | my ($ws, $hs) = $self->get_wh; |
723 | |
920 | |
724 | my $req_w = sum @$ws; |
921 | my $req_w = sum @$ws; |
725 | my $req_h = sum @$hs; |
922 | my $req_h = sum @$hs; |
… | |
… | |
730 | my $col_expand = (sum @col_expand) || 1; |
927 | my $col_expand = (sum @col_expand) || 1; |
731 | |
928 | |
732 | # linearly scale sizes |
929 | # linearly scale sizes |
733 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
930 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
734 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
931 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
932 | |
|
|
933 | CFClient::UI::harmonize $ws; |
|
|
934 | CFClient::UI::harmonize $hs; |
735 | |
935 | |
736 | my $y; |
936 | my $y; |
737 | |
937 | |
738 | for my $r (0 .. $#{$self->{children}}) { |
938 | for my $r (0 .. $#{$self->{children}}) { |
739 | my $row = $self->{children}[$r] |
939 | my $row = $self->{children}[$r] |
… | |
… | |
744 | |
944 | |
745 | for my $c (0 .. $#$row) { |
945 | for my $c (0 .. $#$row) { |
746 | my $col_w = $ws->[$c]; |
946 | my $col_w = $ws->[$c]; |
747 | |
947 | |
748 | if (my $widget = $row->[$c]) { |
948 | if (my $widget = $row->[$c]) { |
749 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
949 | $widget->configure ($x, $y, $col_w, $row_h); |
750 | } |
950 | } |
751 | |
951 | |
752 | $x += $col_w; |
952 | $x += $col_w; |
753 | } |
953 | } |
754 | |
954 | |
… | |
… | |
799 | (List::Util::max map $_->[1], @alloc), |
999 | (List::Util::max map $_->[1], @alloc), |
800 | ) |
1000 | ) |
801 | } |
1001 | } |
802 | |
1002 | |
803 | sub size_allocate { |
1003 | sub size_allocate { |
804 | my ($self, $x, $y, $w, $h) = @_; |
1004 | my ($self, $w, $h) = @_; |
805 | |
|
|
806 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
807 | |
1005 | |
808 | ($h, $w) = ($w, $h); |
1006 | ($h, $w) = ($w, $h); |
809 | |
1007 | |
810 | my $children = $self->{children}; |
1008 | my $children = $self->{children}; |
811 | |
1009 | |
812 | my @h = map +($_->size_request)[0], @$children; |
1010 | my @h = map $_->{req_w}, @$children; |
813 | |
1011 | |
814 | my $req_h = List::Util::sum @h; |
1012 | my $req_h = List::Util::sum @h; |
815 | |
1013 | |
816 | if ($req_h > $h) { |
1014 | if ($req_h > $h) { |
817 | # ah well, not enough space |
1015 | # ah well, not enough space |
… | |
… | |
827 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
1025 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
828 | $h[$_] = $alloc_h; |
1026 | $h[$_] = $alloc_h; |
829 | } |
1027 | } |
830 | } |
1028 | } |
831 | |
1029 | |
|
|
1030 | CFClient::UI::harmonize \@h; |
|
|
1031 | |
832 | my $y = 0; |
1032 | my $y = 0; |
833 | for (0 .. $#$children) { |
1033 | for (0 .. $#$children) { |
834 | my $child = $children->[$_]; |
1034 | my $child = $children->[$_]; |
835 | my $h = $h[$_]; |
1035 | my $h = $h[$_]; |
836 | $child->size_allocate ($y, 0, $h, $w); |
1036 | $child->configure ($y, 0, $h, $w); |
837 | |
1037 | |
838 | $y += $h; |
1038 | $y += $h; |
839 | } |
1039 | } |
|
|
1040 | |
|
|
1041 | 1 |
840 | } |
1042 | } |
841 | |
1043 | |
842 | ############################################################################# |
1044 | ############################################################################# |
843 | |
1045 | |
844 | package CFClient::UI::VBox; |
1046 | package CFClient::UI::VBox; |
… | |
… | |
857 | (List::Util::sum map $_->[1], @alloc), |
1059 | (List::Util::sum map $_->[1], @alloc), |
858 | ) |
1060 | ) |
859 | } |
1061 | } |
860 | |
1062 | |
861 | sub size_allocate { |
1063 | sub size_allocate { |
862 | my ($self, $x, $y, $w, $h) = @_; |
1064 | my ($self, $w, $h) = @_; |
863 | |
|
|
864 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
865 | |
1065 | |
866 | my $children = $self->{children}; |
1066 | my $children = $self->{children}; |
867 | |
1067 | |
868 | my @h = map +($_->size_request)[1], @$children; |
1068 | my @h = map $_->{req_h}, @$children; |
869 | |
1069 | |
870 | my $req_h = List::Util::sum @h; |
1070 | my $req_h = List::Util::sum @h; |
871 | |
1071 | |
872 | if ($req_h > $h) { |
1072 | if ($req_h > $h) { |
873 | # ah well, not enough space |
1073 | # ah well, not enough space |
… | |
… | |
881 | |
1081 | |
882 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
1082 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
883 | } |
1083 | } |
884 | } |
1084 | } |
885 | |
1085 | |
|
|
1086 | CFClient::UI::harmonize \@h; |
|
|
1087 | |
886 | my $y = 0; |
1088 | my $y = 0; |
887 | for (0 .. $#$children) { |
1089 | for (0 .. $#$children) { |
888 | my $child = $children->[$_]; |
1090 | my $child = $children->[$_]; |
889 | my $h = $h[$_]; |
1091 | my $h = $h[$_]; |
890 | $child->size_allocate (0, $y, $w, $h); |
1092 | $child->configure (0, $y, $w, $h); |
891 | |
1093 | |
892 | $y += $h; |
1094 | $y += $h; |
893 | } |
1095 | } |
|
|
1096 | |
|
|
1097 | 1 |
894 | } |
1098 | } |
895 | |
1099 | |
896 | ############################################################################# |
1100 | ############################################################################# |
897 | |
1101 | |
898 | package CFClient::UI::Label; |
1102 | package CFClient::UI::Label; |
899 | |
1103 | |
900 | our @ISA = CFClient::UI::Base::; |
1104 | our @ISA = CFClient::UI::Base::; |
901 | |
1105 | |
902 | use SDL::OpenGL; |
1106 | use CFClient::OpenGL; |
903 | |
1107 | |
904 | sub new { |
1108 | sub new { |
905 | my ($class, %arg) = @_; |
1109 | my ($class, %arg) = @_; |
906 | |
1110 | |
907 | my $self = $class->SUPER::new ( |
1111 | my $self = $class->SUPER::new ( |
908 | fg => [1, 1, 1], |
1112 | fg => [1, 1, 1], |
909 | height => $::FONTSIZE, |
1113 | #font => default_font |
|
|
1114 | fontsize => 1, |
910 | text => "", |
1115 | text => "", |
911 | align => -1, |
1116 | align => -1, |
|
|
1117 | valign => -1, |
912 | padding => 2, |
1118 | padding => 2, |
913 | layout => new CFClient::Layout, |
1119 | layout => new CFClient::Layout, |
|
|
1120 | can_events => 0, |
914 | %arg |
1121 | %arg |
915 | ); |
1122 | ); |
916 | |
1123 | |
917 | $self->set_text ($self->{text}); |
1124 | if (exists $self->{template}) { |
|
|
1125 | my $layout = new CFClient::Layout; |
|
|
1126 | $layout->set_text (delete $self->{template}); |
|
|
1127 | $self->{template} = $layout; |
|
|
1128 | } |
|
|
1129 | |
|
|
1130 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
|
|
1131 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
918 | |
1132 | |
919 | $self |
1133 | $self |
920 | } |
1134 | } |
921 | |
1135 | |
922 | sub escape_text { |
1136 | sub escape_text { |
… | |
… | |
930 | } |
1144 | } |
931 | |
1145 | |
932 | sub set_text { |
1146 | sub set_text { |
933 | my ($self, $text) = @_; |
1147 | my ($self, $text) = @_; |
934 | |
1148 | |
935 | $self->{text} = $text; |
|
|
936 | $self->{layout}->set_markup ($text); |
1149 | $self->{layout}->set_text ($text); |
937 | |
1150 | |
938 | delete $self->{texture}; |
1151 | delete $self->{texture}; |
939 | # $self->{w} = $self->{h} = -1; |
1152 | $self->check_size; |
940 | $self->update; |
1153 | $self->update; |
941 | } |
1154 | } |
942 | |
1155 | |
943 | sub get_text { |
1156 | sub set_markup { |
944 | my ($self, $text) = @_; |
1157 | my ($self, $markup) = @_; |
945 | |
1158 | |
946 | $self->{text} |
1159 | $self->{layout}->set_markup ($markup); |
|
|
1160 | |
|
|
1161 | delete $self->{texture}; |
|
|
1162 | $self->check_size; |
|
|
1163 | $self->update; |
947 | } |
1164 | } |
948 | |
1165 | |
949 | sub size_request { |
1166 | sub size_request { |
950 | my ($self) = @_; |
1167 | my ($self) = @_; |
951 | |
1168 | |
952 | $self->{layout}->set_width; |
1169 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1170 | $self->{layout}->set_width ($self->{max_w} || -1); |
953 | $self->{layout}->set_height ($self->{height}); |
1171 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1172 | |
954 | my ($w, $h) = $self->{layout}->size; |
1173 | my ($w, $h) = $self->{layout}->size; |
|
|
1174 | |
|
|
1175 | if (exists $self->{template}) { |
|
|
1176 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1177 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1178 | |
|
|
1179 | my ($w2, $h2) = $self->{template}->size; |
|
|
1180 | |
|
|
1181 | $w = List::Util::max $w, $w2; |
|
|
1182 | $h = List::Util::max $h, $h2; |
|
|
1183 | } |
955 | |
1184 | |
956 | ( |
1185 | ( |
957 | $w + $self->{padding} * 2, |
1186 | $w + $self->{padding} * 2, |
958 | $h + $self->{padding} * 2, |
1187 | $h + $self->{padding} * 2, |
959 | ) |
1188 | ) |
960 | } |
1189 | } |
961 | |
1190 | |
962 | sub size_allocate { |
1191 | sub size_allocate { |
963 | my ($self, $x, $y, $w, $h) = @_; |
1192 | my ($self, $w, $h) = @_; |
964 | |
|
|
965 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
966 | |
1193 | |
967 | delete $self->{texture}; |
1194 | delete $self->{texture}; |
968 | } |
1195 | } |
969 | |
1196 | |
970 | sub update { |
1197 | sub set_fontsize { |
971 | my ($self) = @_; |
1198 | my ($self, $fontsize) = @_; |
972 | |
1199 | |
|
|
1200 | $self->{fontsize} = $fontsize; |
973 | delete $self->{texture}; |
1201 | delete $self->{texture}; |
|
|
1202 | $self->check_size; |
974 | $self->SUPER::update; |
1203 | $self->update; |
975 | } |
1204 | } |
976 | |
1205 | |
977 | sub _draw { |
1206 | sub _draw { |
978 | my ($self) = @_; |
1207 | my ($self) = @_; |
979 | |
1208 | |
980 | my $tex = $self->{texture} ||= do { |
1209 | my $tex = $self->{texture} ||= do { |
|
|
1210 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
981 | $self->{layout}->set_width ($self->{w}); |
1211 | $self->{layout}->set_width ($self->{w}); |
982 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
1212 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
983 | new_from_layout CFClient::Texture $self->{layout} |
1213 | new_from_layout CFClient::Texture $self->{layout} |
984 | }; |
1214 | }; |
985 | |
1215 | |
986 | glEnable GL_BLEND; |
1216 | glEnable GL_BLEND; |
987 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1217 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
988 | glEnable GL_TEXTURE_2D; |
1218 | glEnable GL_TEXTURE_2D; |
989 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1219 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
990 | |
1220 | |
991 | glColor @{$self->{fg}}; |
1221 | glColor @{$self->{fg}}; |
992 | |
1222 | |
993 | my $x = |
1223 | $self->{ox} = int ( |
994 | $self->{align} < 0 ? $self->{padding} |
1224 | $self->{align} < 0 ? $self->{padding} |
995 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1225 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
996 | : ($self->{w} - $tex->{w}) * 0.5; |
1226 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1227 | ); |
997 | |
1228 | |
998 | glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0; |
1229 | $self->{oy} = int ( |
999 | $tex->draw_quad (0, 0); |
1230 | $self->{valign} < 0 ? $self->{padding} |
|
|
1231 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1232 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1233 | ); |
|
|
1234 | |
|
|
1235 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1000 | |
1236 | |
1001 | glDisable GL_TEXTURE_2D; |
1237 | glDisable GL_TEXTURE_2D; |
1002 | glDisable GL_BLEND; |
1238 | glDisable GL_BLEND; |
1003 | } |
1239 | } |
1004 | |
1240 | |
1005 | ############################################################################# |
1241 | ############################################################################# |
1006 | |
1242 | |
1007 | package CFClient::UI::Entry; |
1243 | package CFClient::UI::EntryBase; |
1008 | |
1244 | |
1009 | our @ISA = CFClient::UI::Label::; |
1245 | our @ISA = CFClient::UI::Label::; |
1010 | |
1246 | |
1011 | use SDL; |
1247 | use CFClient::OpenGL; |
1012 | use SDL::OpenGL; |
|
|
1013 | |
1248 | |
1014 | sub new { |
1249 | sub new { |
1015 | my $class = shift; |
1250 | my $class = shift; |
1016 | |
1251 | |
1017 | $class->SUPER::new ( |
1252 | $class->SUPER::new ( |
1018 | fg => [1, 1, 1], |
1253 | fg => [1, 1, 1], |
1019 | bg => [0, 0, 0, 0.2], |
1254 | bg => [0, 0, 0, 0.2], |
1020 | active_bg => [1, 1, 1, 0.5], |
1255 | active_bg => [1, 1, 1, 0.5], |
1021 | active_fg => [0, 0, 0], |
1256 | active_fg => [0, 0, 0], |
|
|
1257 | can_hover => 1, |
|
|
1258 | can_focus => 1, |
|
|
1259 | valign => 0, |
|
|
1260 | can_events => 1, |
1022 | @_ |
1261 | @_ |
1023 | ) |
1262 | ) |
1024 | } |
1263 | } |
1025 | |
1264 | |
1026 | sub _set_text { |
1265 | sub _set_text { |
1027 | my ($self, $text) = @_; |
1266 | my ($self, $text) = @_; |
1028 | |
1267 | |
|
|
1268 | delete $self->{cur_h}; |
|
|
1269 | |
|
|
1270 | return if $self->{text} eq $text; |
|
|
1271 | |
|
|
1272 | delete $self->{texture}; |
|
|
1273 | |
1029 | $self->{last_activity} = $::NOW; |
1274 | $self->{last_activity} = $::NOW; |
1030 | |
|
|
1031 | $self->{text} = $text; |
1275 | $self->{text} = $text; |
1032 | $self->{layout}->set_width ($self->{w}); |
|
|
1033 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
|
|
1034 | |
1276 | |
1035 | $text =~ s/./*/g if $self->{hidden}; |
1277 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1278 | $self->{layout}->set_text ("$text "); |
1036 | |
1279 | |
1037 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1280 | $self->emit (changed => $self->{text}); |
|
|
1281 | } |
1038 | |
1282 | |
1039 | $text = substr $text, 0, $self->{cursor}; |
1283 | sub get_text { |
1040 | utf8::encode $text; |
1284 | $_[0]{text} |
1041 | |
|
|
1042 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1043 | } |
1285 | } |
1044 | |
1286 | |
1045 | sub size_request { |
1287 | sub size_request { |
1046 | my ($self) = @_; |
1288 | my ($self) = @_; |
1047 | |
1289 | |
… | |
… | |
1049 | |
1291 | |
1050 | ($w + 1, $h) # add 1 for cursor |
1292 | ($w + 1, $h) # add 1 for cursor |
1051 | } |
1293 | } |
1052 | |
1294 | |
1053 | sub size_allocate { |
1295 | sub size_allocate { |
1054 | my ($self, $x, $y, $w, $h) = @_; |
1296 | my ($self, $w, $h) = @_; |
1055 | |
1297 | |
1056 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1298 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1057 | |
|
|
1058 | $self->_set_text ($self->{text}); |
|
|
1059 | } |
1299 | } |
1060 | |
1300 | |
1061 | sub set_text { |
1301 | sub set_text { |
1062 | my ($self, $text) = @_; |
1302 | my ($self, $text) = @_; |
1063 | |
1303 | |
… | |
… | |
1067 | } |
1307 | } |
1068 | |
1308 | |
1069 | sub key_down { |
1309 | sub key_down { |
1070 | my ($self, $ev) = @_; |
1310 | my ($self, $ev) = @_; |
1071 | |
1311 | |
1072 | my $mod = $ev->key_mod; |
1312 | my $mod = $ev->{mod}; |
1073 | my $sym = $ev->key_sym; |
1313 | my $sym = $ev->{sym}; |
1074 | |
|
|
1075 | my $uni = $ev->key_unicode; |
1314 | my $uni = $ev->{unicode}; |
1076 | |
1315 | |
1077 | my $text = $self->get_text; |
1316 | my $text = $self->get_text; |
1078 | |
1317 | |
1079 | if ($sym == SDLK_BACKSPACE) { |
1318 | if ($sym == 8) { |
1080 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1319 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1081 | } elsif ($sym == SDLK_DELETE) { |
1320 | } elsif ($sym == 127) { |
1082 | substr $text, $self->{cursor}, 1, ""; |
1321 | substr $text, $self->{cursor}, 1, ""; |
1083 | } elsif ($sym == SDLK_LEFT) { |
1322 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1084 | --$self->{cursor} if $self->{cursor}; |
1323 | --$self->{cursor} if $self->{cursor}; |
1085 | } elsif ($sym == SDLK_RIGHT) { |
1324 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1086 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1325 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1087 | } elsif ($sym == SDLK_HOME) { |
1326 | } elsif ($sym == CFClient::SDLK_HOME) { |
1088 | $self->{cursor} = 0; |
1327 | $self->{cursor} = 0; |
1089 | } elsif ($sym == SDLK_END) { |
1328 | } elsif ($sym == CFClient::SDLK_END) { |
1090 | $self->{cursor} = length $text; |
1329 | $self->{cursor} = length $text; |
|
|
1330 | } elsif ($sym == 27) { |
|
|
1331 | $self->emit ('escape'); |
1091 | } elsif ($uni) { |
1332 | } elsif ($uni) { |
1092 | substr $text, $self->{cursor}++, 0, chr $uni; |
1333 | substr $text, $self->{cursor}++, 0, chr $uni; |
1093 | } |
1334 | } |
1094 | |
1335 | |
1095 | $self->_set_text ($text); |
1336 | $self->_set_text ($text); |
… | |
… | |
1149 | |
1390 | |
1150 | $self->SUPER::_draw; |
1391 | $self->SUPER::_draw; |
1151 | |
1392 | |
1152 | #TODO: force update every cursor change :( |
1393 | #TODO: force update every cursor change :( |
1153 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1394 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1395 | |
|
|
1396 | unless (exists $self->{cur_h}) { |
|
|
1397 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1398 | utf8::encode $text; |
|
|
1399 | |
|
|
1400 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1401 | } |
|
|
1402 | |
1154 | glColor @{$self->{fg}}; |
1403 | glColor @{$self->{fg}}; |
1155 | glBegin GL_LINES; |
1404 | glBegin GL_LINES; |
1156 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1157 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1405 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1406 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1158 | glEnd; |
1407 | glEnd; |
1159 | } |
1408 | } |
1160 | } |
1409 | } |
1161 | |
1410 | |
|
|
1411 | package CFClient::UI::Entry; |
|
|
1412 | |
|
|
1413 | our @ISA = CFClient::UI::EntryBase::; |
|
|
1414 | |
|
|
1415 | use CFClient::OpenGL; |
|
|
1416 | |
|
|
1417 | sub key_down { |
|
|
1418 | my ($self, $ev) = @_; |
|
|
1419 | |
|
|
1420 | my $sym = $ev->{sym}; |
|
|
1421 | |
|
|
1422 | if ($sym == 13) { |
|
|
1423 | $self->emit (activate => $self->get_text); |
|
|
1424 | $self->update; |
|
|
1425 | |
|
|
1426 | } else { |
|
|
1427 | $self->SUPER::key_down ($ev); |
|
|
1428 | } |
|
|
1429 | |
|
|
1430 | } |
|
|
1431 | |
1162 | ############################################################################# |
1432 | ############################################################################# |
1163 | |
1433 | |
1164 | package CFClient::UI::Button; |
1434 | package CFClient::UI::Button; |
1165 | |
1435 | |
1166 | our @ISA = CFClient::UI::Label::; |
1436 | our @ISA = CFClient::UI::Label::; |
1167 | |
1437 | |
1168 | use SDL; |
1438 | use CFClient::OpenGL; |
1169 | use SDL::OpenGL; |
|
|
1170 | |
1439 | |
1171 | my @tex = |
1440 | my @tex = |
1172 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1441 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1173 | qw(b1_button_active.png); |
1442 | qw(b1_button_active.png); |
1174 | |
1443 | |
1175 | sub new { |
1444 | sub new { |
1176 | my $class = shift; |
1445 | my $class = shift; |
1177 | |
1446 | |
1178 | $class->SUPER::new ( |
1447 | $class->SUPER::new ( |
1179 | padding => 4, |
1448 | padding => 4, |
1180 | fg => [1, 1, 1], |
1449 | fg => [1, 1, 1], |
1181 | bg => [1, 1, 1, 0.2], |
1450 | bg => [1, 1, 1, 0.2], |
1182 | active_fg => [1, 1, 0], |
|
|
1183 | # active_bg => [0, 0, 0, 0.5], |
1451 | active_fg => [0, 0, 1], |
1184 | # border_fg => [1, 1, 0], |
1452 | can_hover => 1, |
|
|
1453 | align => 0, |
|
|
1454 | valign => 0, |
|
|
1455 | can_events => 1, |
1185 | @_ |
1456 | @_ |
1186 | ) |
1457 | ) |
1187 | } |
1458 | } |
1188 | |
1459 | |
1189 | sub button_up { |
1460 | sub button_up { |
… | |
… | |
1197 | |
1468 | |
1198 | sub _draw { |
1469 | sub _draw { |
1199 | my ($self) = @_; |
1470 | my ($self) = @_; |
1200 | |
1471 | |
1201 | local $self->{fg} = $self->{fg}; |
1472 | local $self->{fg} = $self->{fg}; |
1202 | my $tex = $tex[0]; |
|
|
1203 | |
|
|
1204 | glEnable GL_BLEND; |
|
|
1205 | glEnable GL_TEXTURE_2D; |
|
|
1206 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1207 | |
1473 | |
1208 | if ($GRAB == $self) { |
1474 | if ($GRAB == $self) { |
1209 | $self->{fg} = $self->{active_fg}; |
1475 | $self->{fg} = $self->{active_fg}; |
1210 | } |
1476 | } |
1211 | |
1477 | |
1212 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1478 | glEnable GL_BLEND; |
|
|
1479 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1480 | glEnable GL_TEXTURE_2D; |
1213 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1481 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1482 | glColor 0, 0, 0, 1; |
1214 | |
1483 | |
1215 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1484 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1216 | |
1485 | |
1217 | glDisable GL_TEXTURE_2D; |
1486 | glDisable GL_TEXTURE_2D; |
1218 | glDisable GL_BLEND; |
1487 | glDisable GL_BLEND; |
1219 | |
1488 | |
1220 | $self->SUPER::_draw; |
1489 | $self->SUPER::_draw; |
… | |
… | |
1224 | |
1493 | |
1225 | package CFClient::UI::CheckBox; |
1494 | package CFClient::UI::CheckBox; |
1226 | |
1495 | |
1227 | our @ISA = CFClient::UI::DrawBG::; |
1496 | our @ISA = CFClient::UI::DrawBG::; |
1228 | |
1497 | |
1229 | use SDL; |
1498 | my @tex = |
1230 | use SDL::OpenGL; |
1499 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1500 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
1501 | |
|
|
1502 | use CFClient::OpenGL; |
1231 | |
1503 | |
1232 | sub new { |
1504 | sub new { |
1233 | my $class = shift; |
1505 | my $class = shift; |
1234 | |
1506 | |
1235 | $class->SUPER::new ( |
1507 | $class->SUPER::new ( |
1236 | padding => 2, |
1508 | padding => 2, |
1237 | fg => [1, 1, 1], |
1509 | fg => [1, 1, 1], |
1238 | active_fg => [1, 1, 0], |
1510 | active_fg => [1, 1, 0], |
1239 | state => 0, |
1511 | state => 0, |
|
|
1512 | can_hover => 1, |
1240 | @_ |
1513 | @_ |
1241 | ) |
1514 | ) |
1242 | } |
1515 | } |
1243 | |
1516 | |
1244 | sub size_request { |
1517 | sub size_request { |
1245 | my ($self) = @_; |
1518 | my ($self) = @_; |
1246 | |
1519 | |
1247 | ($self->{padding} * 2 + 6) x 2 |
1520 | ($self->{padding} * 2 + 6) x 2 |
1248 | } |
|
|
1249 | |
|
|
1250 | sub size_allocate { |
|
|
1251 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1252 | |
|
|
1253 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1254 | } |
1521 | } |
1255 | |
1522 | |
1256 | sub button_down { |
1523 | sub button_down { |
1257 | my ($self, $ev, $x, $y) = @_; |
1524 | my ($self, $ev, $x, $y) = @_; |
1258 | |
1525 | |
… | |
… | |
1269 | $self->SUPER::_draw; |
1536 | $self->SUPER::_draw; |
1270 | |
1537 | |
1271 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1538 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1272 | |
1539 | |
1273 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1540 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1274 | |
1541 | |
1275 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1542 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1276 | |
1543 | |
1277 | glBegin GL_LINE_LOOP; |
1544 | glEnable GL_BLEND; |
1278 | glVertex 0 , 0; |
1545 | glEnable GL_TEXTURE_2D; |
1279 | glVertex 0 , $s; |
1546 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1280 | glVertex $s, $s; |
|
|
1281 | glVertex $s, 0; |
|
|
1282 | glEnd; |
|
|
1283 | |
1547 | |
1284 | if ($self->{state}) { |
1548 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1285 | glBegin GL_LINES; |
|
|
1286 | glVertex 0 , 0; |
|
|
1287 | glVertex $s, $s; |
|
|
1288 | glVertex $s, 0; |
|
|
1289 | glVertex 0 , $s; |
|
|
1290 | glEnd; |
|
|
1291 | } |
|
|
1292 | } |
|
|
1293 | |
1549 | |
1294 | ############################################################################# |
1550 | $tex->draw_quad (0, 0, $s, $s); |
1295 | |
1551 | |
|
|
1552 | glDisable GL_TEXTURE_2D; |
|
|
1553 | glDisable GL_BLEND; |
|
|
1554 | } |
|
|
1555 | |
|
|
1556 | ############################################################################# |
|
|
1557 | |
1296 | package CFClient::UI::Slider; |
1558 | package CFClient::UI::Image; |
1297 | |
1559 | |
1298 | use strict; |
|
|
1299 | |
|
|
1300 | use SDL::OpenGL; |
|
|
1301 | |
|
|
1302 | our @ISA = CFClient::UI::DrawBG::; |
1560 | our @ISA = CFClient::UI::Base::; |
|
|
1561 | |
|
|
1562 | use CFClient::OpenGL; |
|
|
1563 | use Carp qw/confess/; |
|
|
1564 | |
|
|
1565 | our %loaded_images; |
1303 | |
1566 | |
1304 | sub new { |
1567 | sub new { |
1305 | my $class = shift; |
1568 | my $class = shift; |
1306 | |
1569 | |
|
|
1570 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1571 | |
|
|
1572 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1573 | |
|
|
1574 | $loaded_images{$self->{image}} ||= |
|
|
1575 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1576 | |
|
|
1577 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1578 | |
|
|
1579 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1580 | |
|
|
1581 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1582 | |
|
|
1583 | $self |
|
|
1584 | } |
|
|
1585 | |
|
|
1586 | sub size_request { |
|
|
1587 | my ($self) = @_; |
|
|
1588 | |
|
|
1589 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1590 | } |
|
|
1591 | |
|
|
1592 | sub _draw { |
|
|
1593 | my ($self) = @_; |
|
|
1594 | |
|
|
1595 | my $tex = $self->{tex}; |
|
|
1596 | |
|
|
1597 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1598 | |
|
|
1599 | if ($self->{rot90}) { |
|
|
1600 | glRotate 90, 0, 0, 1; |
|
|
1601 | glTranslate 0, -$self->{w}, 0; |
|
|
1602 | |
|
|
1603 | ($w, $h) = ($h, $w); |
|
|
1604 | } |
|
|
1605 | |
|
|
1606 | glEnable GL_BLEND; |
|
|
1607 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1608 | glEnable GL_TEXTURE_2D; |
|
|
1609 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1610 | |
|
|
1611 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1612 | |
|
|
1613 | glDisable GL_BLEND; |
|
|
1614 | glDisable GL_TEXTURE_2D; |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | ############################################################################# |
|
|
1618 | |
|
|
1619 | package CFClient::UI::VGauge; |
|
|
1620 | |
|
|
1621 | our @ISA = CFClient::UI::Base::; |
|
|
1622 | |
|
|
1623 | use List::Util qw(min max); |
|
|
1624 | |
|
|
1625 | use CFClient::OpenGL; |
|
|
1626 | |
|
|
1627 | my %tex = ( |
|
|
1628 | food => [ |
|
|
1629 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1630 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1631 | ], |
|
|
1632 | grace => [ |
|
|
1633 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1634 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
|
|
1635 | ], |
|
|
1636 | hp => [ |
|
|
1637 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1638 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1639 | ], |
|
|
1640 | mana => [ |
|
|
1641 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1642 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
|
|
1643 | ], |
|
|
1644 | ); |
|
|
1645 | |
|
|
1646 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1647 | sub new { |
|
|
1648 | my $class = shift; |
|
|
1649 | |
|
|
1650 | my $self = $class->SUPER::new ( |
|
|
1651 | type => 'food', |
|
|
1652 | @_ |
|
|
1653 | ); |
|
|
1654 | |
|
|
1655 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1656 | |
|
|
1657 | $self |
|
|
1658 | } |
|
|
1659 | |
|
|
1660 | sub size_request { |
|
|
1661 | my ($self) = @_; |
|
|
1662 | |
|
|
1663 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1664 | #@$tex{qw(w h)} |
|
|
1665 | (0, 0) |
|
|
1666 | } |
|
|
1667 | |
|
|
1668 | sub set_max { |
|
|
1669 | my ($self, $max) = @_; |
|
|
1670 | |
|
|
1671 | $self->{max_val} = $max; |
|
|
1672 | } |
|
|
1673 | |
|
|
1674 | sub set_value { |
|
|
1675 | my ($self, $val, $max) = @_; |
|
|
1676 | |
|
|
1677 | $self->set_max ($max) |
|
|
1678 | if defined $max; |
|
|
1679 | |
|
|
1680 | $max = $self->{max_val}; |
|
|
1681 | $self->{val} = $val; |
|
|
1682 | |
|
|
1683 | $self->update; |
|
|
1684 | } |
|
|
1685 | |
|
|
1686 | sub _draw { |
|
|
1687 | my ($self) = @_; |
|
|
1688 | |
|
|
1689 | my $tex = $tex{$self->{type}}; |
|
|
1690 | my ($t1, $t2, $t3) = @$tex; |
|
|
1691 | |
|
|
1692 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1693 | |
|
|
1694 | if ($self->{vertical}) { |
|
|
1695 | glRotate 90, 0, 0, 1; |
|
|
1696 | glTranslate 0, -$self->{w}, 0; |
|
|
1697 | |
|
|
1698 | ($w, $h) = ($h, $w); |
|
|
1699 | } |
|
|
1700 | |
|
|
1701 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1702 | |
|
|
1703 | my $ycut1 = max 0, min 1, $ycut; |
|
|
1704 | my $ycut2 = max 0, min 1, $ycut - 1; |
|
|
1705 | |
|
|
1706 | my $h1 = $self->{h} * (1 - $ycut1); |
|
|
1707 | my $h2 = $self->{h} * (1 - $ycut2); |
|
|
1708 | |
|
|
1709 | glEnable GL_BLEND; |
|
|
1710 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1711 | glEnable GL_TEXTURE_2D; |
|
|
1712 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1713 | |
|
|
1714 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1715 | glBegin GL_QUADS; |
|
|
1716 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1717 | glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1718 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1719 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1720 | glEnd; |
|
|
1721 | |
|
|
1722 | my $ycut1 = List::Util::min 1, $ycut; |
|
|
1723 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1724 | glBegin GL_QUADS; |
|
|
1725 | glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1726 | glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1727 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1728 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1729 | glEnd; |
|
|
1730 | |
|
|
1731 | if ($t3) { |
|
|
1732 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
|
|
1733 | glBegin GL_QUADS; |
|
|
1734 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1735 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
|
|
1736 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
|
|
1737 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1738 | glEnd; |
|
|
1739 | } |
|
|
1740 | |
|
|
1741 | glDisable GL_BLEND; |
|
|
1742 | glDisable GL_TEXTURE_2D; |
|
|
1743 | } |
|
|
1744 | |
|
|
1745 | ############################################################################# |
|
|
1746 | |
|
|
1747 | package CFClient::UI::Gauge; |
|
|
1748 | |
|
|
1749 | our @ISA = CFClient::UI::VBox::; |
|
|
1750 | |
|
|
1751 | sub new { |
|
|
1752 | my ($class, %arg) = @_; |
|
|
1753 | |
|
|
1754 | my $self = $class->SUPER::new ( |
|
|
1755 | tooltip => $arg{type}, |
|
|
1756 | %arg, |
|
|
1757 | ); |
|
|
1758 | |
|
|
1759 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); |
|
|
1760 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); |
|
|
1761 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); |
|
|
1762 | |
|
|
1763 | $self |
|
|
1764 | } |
|
|
1765 | |
|
|
1766 | sub set_fontsize { |
|
|
1767 | my ($self, $fsize) = @_; |
|
|
1768 | |
|
|
1769 | $self->{value}->set_fontsize ($fsize); |
|
|
1770 | $self->{max} ->set_fontsize ($fsize); |
|
|
1771 | } |
|
|
1772 | |
|
|
1773 | sub set_value { |
|
|
1774 | my ($self, $val, $max) = @_; |
|
|
1775 | |
|
|
1776 | $self->set_max ($max) |
|
|
1777 | if defined $max; |
|
|
1778 | |
|
|
1779 | $self->{gauge}->set_value ($val, $max); |
|
|
1780 | $self->{value}->set_text ($val); |
|
|
1781 | } |
|
|
1782 | |
|
|
1783 | sub set_max { |
|
|
1784 | my ($self, $max) = @_; |
|
|
1785 | |
|
|
1786 | $self->{gauge}->set_max ($max); |
|
|
1787 | $self->{max}->set_text ($max); |
|
|
1788 | } |
|
|
1789 | |
|
|
1790 | ############################################################################# |
|
|
1791 | |
|
|
1792 | package CFClient::UI::Slider; |
|
|
1793 | |
|
|
1794 | use strict; |
|
|
1795 | |
|
|
1796 | use CFClient::OpenGL; |
|
|
1797 | |
|
|
1798 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1799 | |
|
|
1800 | my @tex = |
|
|
1801 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1802 | qw(s1_slider.png s1_slider_bg.png); |
|
|
1803 | |
|
|
1804 | sub new { |
|
|
1805 | my $class = shift; |
|
|
1806 | |
1307 | # range [value, low, high, page] |
1807 | # range [value, low, high, page] |
1308 | |
1808 | |
|
|
1809 | # TODO: 0-width page |
|
|
1810 | # TODO: req_w/h are wrong with vertical |
|
|
1811 | # TODO: calculations are off |
1309 | my $self = $class->SUPER::new ( |
1812 | my $self = $class->SUPER::new ( |
1310 | fg => [1, 1, 1], |
1813 | fg => [1, 1, 1], |
1311 | active_fg => [0, 0, 0], |
1814 | active_fg => [0, 0, 0], |
1312 | range => [0, 0, 100, 10], |
1815 | range => [0, 0, 100, 10], |
1313 | req_w => 40, |
1816 | req_w => 20, |
1314 | req_h => 10, |
1817 | req_h => 20, |
1315 | vertical => 0, |
1818 | vertical => 0, |
|
|
1819 | can_hover => 1, |
|
|
1820 | inner_pad => 5, |
1316 | @_ |
1821 | @_ |
1317 | ); |
1822 | ); |
1318 | |
1823 | |
1319 | $self |
1824 | $self |
1320 | } |
1825 | } |
… | |
… | |
1341 | if ($GRAB == $self) { |
1846 | if ($GRAB == $self) { |
1342 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1847 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1343 | |
1848 | |
1344 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1849 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1345 | |
1850 | |
|
|
1851 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1852 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1853 | |
|
|
1854 | $x -= $inner_pad_px; # substract the padding |
1346 | $x = $x * ($hi - $lo) / $w + $lo; |
1855 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
1347 | $x = $lo if $x < $lo; |
1856 | $x = $lo if $x < $lo; |
1348 | $x = $hi - $page if $x > $hi - $page; |
1857 | $x = $hi - $page if $x > $hi - $page; |
1349 | $self->{range}[0] = $x; |
1858 | $self->{range}[0] = $x; |
1350 | |
1859 | |
1351 | $self->emit (changed => $x); |
1860 | $self->emit (changed => $x); |
1352 | $self->update; |
1861 | $self->update; |
1353 | } |
1862 | } |
|
|
1863 | } |
|
|
1864 | |
|
|
1865 | # the inner_* stuff is for generating a padding for the slider handle, |
|
|
1866 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1867 | # correct propably, but it does the job for now |
|
|
1868 | sub _calc_inner_pad_px { |
|
|
1869 | my ($self, $w) = @_; |
|
|
1870 | ($w / 100) * $self->{inner_pad} # % to pixels |
1354 | } |
1871 | } |
1355 | |
1872 | |
1356 | sub _draw { |
1873 | sub _draw { |
1357 | my ($self) = @_; |
1874 | my ($self) = @_; |
1358 | |
1875 | |
… | |
… | |
1372 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1889 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1373 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1890 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1374 | |
1891 | |
1375 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1892 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1376 | |
1893 | |
|
|
1894 | $hi = $value + 1 if $lo == $hi; |
|
|
1895 | |
|
|
1896 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1897 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1898 | |
1377 | $page = int $page * $w / ($hi - $lo); |
1899 | $page = int $page * $inner_w / ($hi - $lo); |
1378 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1900 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1379 | |
1901 | |
1380 | $w -= $page; |
1902 | $w -= $page; |
1381 | $page &= ~1; |
1903 | $page &= ~1; |
1382 | glTranslate $page * 0.5, 0, 0; |
1904 | glTranslate $page * 0.5, 0, 0; |
1383 | $page ||= 2; |
1905 | $page ||= 2; |
1384 | |
1906 | |
1385 | glColor @$fg; |
|
|
1386 | glBegin GL_LINES; |
|
|
1387 | glVertex 0, 0; glVertex 0, $h; |
|
|
1388 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1389 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1390 | glEnd; |
|
|
1391 | |
|
|
1392 | my $knob_a = $value - $page * 0.5; |
1907 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
1393 | my $knob_b = $value + $page * 0.5; |
1908 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
1394 | |
1909 | |
1395 | glBegin GL_QUADS; |
1910 | glEnable GL_BLEND; |
1396 | glColor @$fg; |
1911 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1397 | glVertex $knob_a, 0; |
1912 | glEnable GL_TEXTURE_2D; |
1398 | glVertex $knob_a, $h; |
1913 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1399 | glVertex $knob_b, $h; |
|
|
1400 | glVertex $knob_b, 0; |
|
|
1401 | |
1914 | |
1402 | if ($knob_a < $knob_b - 2) { |
1915 | # draw background |
1403 | glColor @$bg; |
1916 | $tex[1]->draw_quad (0, 0, $w, $h); |
1404 | glVertex $knob_a + 1, 1; |
|
|
1405 | glVertex $knob_a + 1, $h - 1; |
|
|
1406 | glVertex $knob_b - 1, $h - 1; |
|
|
1407 | glVertex $knob_b - 1, 1; |
|
|
1408 | } |
|
|
1409 | glEnd; |
|
|
1410 | } |
|
|
1411 | |
1917 | |
1412 | ############################################################################# |
1918 | # draw handle |
|
|
1919 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
1413 | |
1920 | |
|
|
1921 | glDisable GL_BLEND; |
|
|
1922 | glDisable GL_TEXTURE_2D; |
|
|
1923 | } |
|
|
1924 | |
|
|
1925 | ############################################################################# |
|
|
1926 | |
1414 | package CFClient::UI::MapWidget; |
1927 | package CFClient::UI::TextView; |
1415 | |
1928 | |
1416 | use strict; |
|
|
1417 | |
|
|
1418 | use List::Util qw(min max); |
|
|
1419 | |
|
|
1420 | use SDL; |
|
|
1421 | use SDL::OpenGL; |
|
|
1422 | |
|
|
1423 | our @ISA = CFClient::UI::Base::; |
1929 | our @ISA = CFClient::UI::HBox::; |
|
|
1930 | |
|
|
1931 | use CFClient::OpenGL; |
1424 | |
1932 | |
1425 | sub new { |
1933 | sub new { |
1426 | my $class = shift; |
1934 | my $class = shift; |
1427 | |
1935 | |
1428 | $class->SUPER::new ( |
1936 | my $self = $class->SUPER::new ( |
1429 | z => -1, |
1937 | fontsize => 1, |
1430 | list => (glGenLists 1), |
1938 | #font => default_font |
1431 | @_ |
1939 | @_, |
|
|
1940 | |
|
|
1941 | layout => (new CFClient::Layout), |
|
|
1942 | par => [], |
|
|
1943 | height => 0, |
|
|
1944 | children => [ |
|
|
1945 | (new CFClient::UI::Empty expand => 1), |
|
|
1946 | (new CFClient::UI::Slider vertical => 1), |
|
|
1947 | ], |
1432 | ) |
1948 | ); |
1433 | } |
|
|
1434 | |
1949 | |
1435 | sub key_down { |
1950 | $self->{children}[1]->connect (changed => sub { |
1436 | print "MAPKEYDOWN\n"; |
1951 | $self->update; |
1437 | } |
1952 | }); |
1438 | |
1953 | |
1439 | sub key_up { |
1954 | $self |
1440 | } |
1955 | } |
1441 | |
1956 | |
1442 | sub size_request { |
1957 | sub set_fontsize { |
|
|
1958 | my ($self, $fontsize) = @_; |
|
|
1959 | |
|
|
1960 | $self->{fontsize} = $fontsize; |
|
|
1961 | $self->reflow; |
|
|
1962 | } |
|
|
1963 | |
|
|
1964 | sub text_height { |
|
|
1965 | my ($self, $text) = @_; |
|
|
1966 | |
|
|
1967 | my $layout = $self->{layout}; |
|
|
1968 | |
|
|
1969 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1970 | $layout->set_width ($self->{w}); |
|
|
1971 | $layout->set_text ($text); |
1443 | ( |
1972 | |
1444 | 1 + 32 * int $::WIDTH / 32, |
1973 | ($layout->size)[1] |
1445 | 1 + 32 * int $::HEIGHT / 32, |
1974 | } |
1446 | ) |
1975 | |
|
|
1976 | sub reflow { |
|
|
1977 | my ($self) = @_; |
|
|
1978 | |
|
|
1979 | $self->{need_reflow}++; |
|
|
1980 | $self->update; |
|
|
1981 | } |
|
|
1982 | |
|
|
1983 | sub size_allocate { |
|
|
1984 | my ($self, $w, $h) = @_; |
|
|
1985 | |
|
|
1986 | $self->SUPER::size_allocate ($w, $h); |
|
|
1987 | |
|
|
1988 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1989 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1990 | $self->{layout}->set_width ($self->{children}[0]{w}); |
|
|
1991 | |
|
|
1992 | $self->reflow; |
|
|
1993 | } |
|
|
1994 | |
|
|
1995 | sub add_paragraph { |
|
|
1996 | my ($self, $color, $text) = @_; |
|
|
1997 | |
|
|
1998 | #TODO: intelligently "reformat" paragraph |
|
|
1999 | |
|
|
2000 | my $height = $self->text_height ($text); |
|
|
2001 | |
|
|
2002 | $self->{height} += $height; |
|
|
2003 | |
|
|
2004 | push @{$self->{par}}, [$height, $color, $text]; |
|
|
2005 | |
|
|
2006 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
|
|
2007 | $self->{children}[1]->update; |
1447 | } |
2008 | } |
1448 | |
2009 | |
1449 | sub update { |
2010 | sub update { |
1450 | my ($self) = @_; |
2011 | my ($self) = @_; |
1451 | |
2012 | |
1452 | $self->{need_update} = 1; |
|
|
1453 | $self->SUPER::update; |
2013 | $self->SUPER::update; |
1454 | } |
|
|
1455 | |
2014 | |
1456 | sub draw { |
2015 | return unless $self->{h} > 0; |
1457 | my ($self) = @_; |
|
|
1458 | |
2016 | |
|
|
2017 | delete $self->{texture}; |
|
|
2018 | |
|
|
2019 | $ROOT->on_refresh ($self, sub { |
1459 | if (delete $self->{need_update}) { |
2020 | if (delete $self->{need_reflow}) { |
1460 | glNewList $self->{list}, GL_COMPILE; |
2021 | my $height = 0; |
1461 | |
2022 | |
1462 | my $mx = $::CONN->{mapx}; |
2023 | $height += $_->[0] = $self->text_height ($_->[2]) |
1463 | my $my = $::CONN->{mapy}; |
2024 | for @{$self->{par}}; |
1464 | |
2025 | |
1465 | my $map = $::CONN->{map}; |
2026 | $self->{height} = $height; |
1466 | |
2027 | |
1467 | my ($xofs, $yofs); |
2028 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
1468 | |
2029 | |
1469 | my $sw = 1 + int $::WIDTH / 32; |
2030 | delete $self->{texture}; |
1470 | my $sh = 1 + int $::HEIGHT / 32; |
|
|
1471 | |
|
|
1472 | if ($::CONN->{mapw} > $sw) { |
|
|
1473 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
|
|
1474 | } else { |
|
|
1475 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
|
|
1476 | } |
2031 | } |
1477 | |
2032 | |
1478 | if ($::CONN->{maph} > $sh) { |
2033 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
1479 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
2034 | glClearColor 0, 0, 0, 1; |
1480 | } else { |
2035 | glClear GL_COLOR_BUFFER_BIT; |
1481 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
2036 | |
|
|
2037 | glEnable GL_BLEND; |
|
|
2038 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2039 | glEnable GL_TEXTURE_2D; |
|
|
2040 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2041 | |
|
|
2042 | my $top = int $self->{children}[1]{range}[0]; |
|
|
2043 | |
|
|
2044 | my $y0 = $top; |
|
|
2045 | my $y1 = $top + $self->{h}; |
|
|
2046 | |
|
|
2047 | my $y = 0; |
|
|
2048 | |
|
|
2049 | my $layout = $self->{layout}; |
|
|
2050 | |
|
|
2051 | $layout->set_font ($self->{font}) if $self->{font}; |
|
|
2052 | |
|
|
2053 | for my $par (@{$self->{par}}) { |
|
|
2054 | my $h = $par->[0]; |
|
|
2055 | |
|
|
2056 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2057 | $layout->set_text ($par->[2]); |
|
|
2058 | |
|
|
2059 | glColor @{ $par->[1] }; |
|
|
2060 | my ($W, $H) = $layout->size; |
|
|
2061 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
2062 | } |
|
|
2063 | |
|
|
2064 | $y += $h; |
|
|
2065 | } |
|
|
2066 | |
|
|
2067 | glDisable GL_TEXTURE_2D; |
|
|
2068 | glDisable GL_BLEND; |
1482 | } |
2069 | }; |
|
|
2070 | }); |
|
|
2071 | } |
1483 | |
2072 | |
|
|
2073 | sub _draw { |
|
|
2074 | my ($self) = @_; |
|
|
2075 | |
|
|
2076 | if ($self->{texture}) { |
1484 | glEnable GL_TEXTURE_2D; |
2077 | glEnable GL_TEXTURE_2D; |
1485 | glEnable GL_BLEND; |
|
|
1486 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1487 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2078 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1488 | glColor 1, 1, 1, 1; |
2079 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
1489 | |
|
|
1490 | my $sw4 = ($sw + 3) & ~3; |
|
|
1491 | my $darkness = "\x00" x ($sw4 * $sh); |
|
|
1492 | |
|
|
1493 | for my $x (0 .. $sw - 1) { |
|
|
1494 | my $row = $map->[$x + $xofs]; |
|
|
1495 | for my $y (0 .. $sh - 1) { |
|
|
1496 | |
|
|
1497 | my $cell = $row->[$y + $yofs] |
|
|
1498 | or next; |
|
|
1499 | |
|
|
1500 | my $dark = $cell->[0]; |
|
|
1501 | if ($dark < 0) { |
|
|
1502 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
1503 | } else { |
|
|
1504 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
|
|
1505 | } |
|
|
1506 | |
|
|
1507 | for my $num (grep $_, @$cell[1,2,3]) { |
|
|
1508 | my $tex = $::CONN->{face}[$num]{texture} || next; |
|
|
1509 | |
|
|
1510 | my ($w, $h) = @$tex{qw(w h)}; |
|
|
1511 | |
|
|
1512 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
|
|
1513 | } |
|
|
1514 | } |
|
|
1515 | } |
|
|
1516 | |
|
|
1517 | # if (1) { # higher quality darkness |
|
|
1518 | # $lighting =~ s/(.)/$1$1$1/gs; |
|
|
1519 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
|
|
1520 | # |
|
|
1521 | # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
|
|
1522 | # |
|
|
1523 | # $lighting = $pb->get_pixels; |
|
|
1524 | # $lighting =~ s/(.)../$1/gs; |
|
|
1525 | # } |
|
|
1526 | |
|
|
1527 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1528 | |
|
|
1529 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1530 | my @conv = ( |
|
|
1531 | 0.1, 0.1, 0.1, |
|
|
1532 | 0.1, 0.2, 0.1, |
|
|
1533 | 0.1, 0.1, 0.1, |
|
|
1534 | ); |
|
|
1535 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1536 | glConvolutionFilter2D GL_CONVOLUTION_2D, |
|
|
1537 | GL_ALPHA, 3, 3, GL_ALPHA, GL_FLOAT, |
|
|
1538 | pack "f*", @conv; |
|
|
1539 | glEnable GL_CONVOLUTION_2D; |
|
|
1540 | } |
|
|
1541 | |
|
|
1542 | $darkness = new CFClient::Texture |
|
|
1543 | w => $sw4, |
|
|
1544 | h => $sh, |
|
|
1545 | data => $darkness, |
|
|
1546 | internalformat => GL_ALPHA, |
|
|
1547 | format => GL_ALPHA; |
|
|
1548 | |
|
|
1549 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1550 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
1551 | |
|
|
1552 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1553 | |
|
|
1554 | glDisable GL_TEXTURE_2D; |
2080 | glDisable GL_TEXTURE_2D; |
1555 | glDisable GL_BLEND; |
|
|
1556 | |
|
|
1557 | glEndList; |
|
|
1558 | } |
2081 | } |
1559 | |
2082 | |
1560 | glCallList $self->{list}; |
2083 | $self->{children}[1]->draw; |
1561 | |
2084 | |
1562 | if ($FOCUS != $self) { |
|
|
1563 | glEnable GL_BLEND; |
|
|
1564 | glColor 0, 0, 1, 0.4; |
|
|
1565 | glBegin GL_QUADS; |
|
|
1566 | glVertex 0, 0; |
|
|
1567 | glVertex 0, $::HEIGHT; |
|
|
1568 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1569 | glVertex $::WIDTH, 0; |
|
|
1570 | glEnd; |
|
|
1571 | glDisable GL_BLEND; |
|
|
1572 | } |
|
|
1573 | } |
|
|
1574 | |
|
|
1575 | my %DIR = ( |
|
|
1576 | SDLK_KP8, [1, "north"], |
|
|
1577 | SDLK_KP9, [2, "northeast"], |
|
|
1578 | SDLK_KP6, [3, "east"], |
|
|
1579 | SDLK_KP3, [4, "southeast"], |
|
|
1580 | SDLK_KP2, [5, "south"], |
|
|
1581 | SDLK_KP1, [6, "southwest"], |
|
|
1582 | SDLK_KP4, [7, "west"], |
|
|
1583 | SDLK_KP7, [8, "northwest"], |
|
|
1584 | |
|
|
1585 | SDLK_UP, [1, "north"], |
|
|
1586 | SDLK_RIGHT, [3, "east"], |
|
|
1587 | SDLK_DOWN, [5, "south"], |
|
|
1588 | SDLK_LEFT, [7, "west"], |
|
|
1589 | ); |
|
|
1590 | |
|
|
1591 | sub key_down { |
|
|
1592 | my ($self, $ev) = @_; |
|
|
1593 | |
|
|
1594 | my $mod = $ev->key_mod; |
|
|
1595 | my $sym = $ev->key_sym; |
|
|
1596 | |
|
|
1597 | if ($sym == SDLK_KP5) { |
|
|
1598 | $::CONN->user_send ("command stay fire"); |
|
|
1599 | } elsif ($sym == SDLK_a) { |
|
|
1600 | $::CONN->user_send ("command apply"); |
|
|
1601 | } elsif (exists $DIR{$sym}) { |
|
|
1602 | if ($mod & KMOD_SHIFT) { |
|
|
1603 | $self->{shft}++; |
|
|
1604 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1605 | } elsif ($mod & KMOD_CTRL) { |
|
|
1606 | $self->{ctrl}++; |
|
|
1607 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1608 | } else { |
|
|
1609 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1610 | } |
|
|
1611 | } |
|
|
1612 | } |
|
|
1613 | |
|
|
1614 | sub key_up { |
|
|
1615 | my ($self, $ev) = @_; |
|
|
1616 | |
|
|
1617 | my $mod = $ev->key_mod; |
|
|
1618 | my $sym = $ev->key_sym; |
|
|
1619 | |
|
|
1620 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1621 | $::CONN->user_send ("command fire_stop"); |
|
|
1622 | } |
|
|
1623 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1624 | $::CONN->user_send ("command run_stop"); |
|
|
1625 | } |
|
|
1626 | } |
2085 | } |
1627 | |
2086 | |
1628 | ############################################################################# |
2087 | ############################################################################# |
1629 | |
2088 | |
1630 | package CFClient::UI::Animator; |
2089 | package CFClient::UI::Animator; |
1631 | |
2090 | |
1632 | use SDL::OpenGL; |
2091 | use CFClient::OpenGL; |
1633 | |
2092 | |
1634 | our @ISA = CFClient::UI::Bin::; |
2093 | our @ISA = CFClient::UI::Bin::; |
1635 | |
2094 | |
1636 | sub moveto { |
2095 | sub moveto { |
1637 | my ($self, $x, $y) = @_; |
2096 | my ($self, $x, $y) = @_; |
… | |
… | |
1667 | glPopMatrix; |
2126 | glPopMatrix; |
1668 | } |
2127 | } |
1669 | |
2128 | |
1670 | ############################################################################# |
2129 | ############################################################################# |
1671 | |
2130 | |
1672 | package CFClient::UI::Toplevel; |
2131 | package CFClient::UI::Flopper; |
|
|
2132 | |
|
|
2133 | our @ISA = CFClient::UI::Button::; |
|
|
2134 | |
|
|
2135 | sub new { |
|
|
2136 | my $class = shift; |
|
|
2137 | |
|
|
2138 | my $self = $class->SUPER::new ( |
|
|
2139 | state => 0, |
|
|
2140 | connect_activate => \&toggle_flopper, |
|
|
2141 | can_events => 1, |
|
|
2142 | @_ |
|
|
2143 | ); |
|
|
2144 | |
|
|
2145 | if ($self->{state}) { |
|
|
2146 | $self->{state} = 0; |
|
|
2147 | $self->toggle_flopper; |
|
|
2148 | } |
|
|
2149 | |
|
|
2150 | $self |
|
|
2151 | } |
|
|
2152 | |
|
|
2153 | sub toggle_flopper { |
|
|
2154 | my ($self) = @_; |
|
|
2155 | |
|
|
2156 | # TODO: use animation |
|
|
2157 | if ($self->{state} = !$self->{state}) { |
|
|
2158 | $CFClient::UI::ROOT->add ($self->{other}); |
|
|
2159 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
|
|
2160 | $self->emit ("open"); |
|
|
2161 | } else { |
|
|
2162 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
2163 | $self->emit ("close"); |
|
|
2164 | } |
|
|
2165 | |
|
|
2166 | $self->emit (changed => $self->{state}); |
|
|
2167 | } |
|
|
2168 | |
|
|
2169 | ############################################################################# |
|
|
2170 | |
|
|
2171 | package CFClient::UI::Tooltip; |
|
|
2172 | |
|
|
2173 | our @ISA = CFClient::UI::Bin::; |
|
|
2174 | |
|
|
2175 | use CFClient::OpenGL; |
|
|
2176 | |
|
|
2177 | sub new { |
|
|
2178 | my $class = shift; |
|
|
2179 | |
|
|
2180 | $class->SUPER::new ( |
|
|
2181 | @_, |
|
|
2182 | can_events => 0, |
|
|
2183 | ) |
|
|
2184 | } |
|
|
2185 | |
|
|
2186 | sub set_text { |
|
|
2187 | my ($self, $text) = @_; |
|
|
2188 | |
|
|
2189 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2190 | $self->{label}->set_text ($text); |
|
|
2191 | $self->add ($self->{label}); |
|
|
2192 | } |
|
|
2193 | |
|
|
2194 | sub size_request { |
|
|
2195 | my ($self) = @_; |
|
|
2196 | |
|
|
2197 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2198 | |
|
|
2199 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2200 | |
|
|
2201 | ($w + 4, $h + 4) |
|
|
2202 | } |
|
|
2203 | |
|
|
2204 | sub _draw { |
|
|
2205 | my ($self) = @_; |
|
|
2206 | |
|
|
2207 | glPushMatrix; |
|
|
2208 | glTranslate 0.375, 0.375; |
|
|
2209 | |
|
|
2210 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2211 | |
|
|
2212 | glColor 1, 0.8, 0.4; |
|
|
2213 | glBegin GL_QUADS; |
|
|
2214 | glVertex 0 , 0; |
|
|
2215 | glVertex 0 , $h; |
|
|
2216 | glVertex $w, $h; |
|
|
2217 | glVertex $w, 0; |
|
|
2218 | glEnd; |
|
|
2219 | |
|
|
2220 | glColor 0, 0, 0; |
|
|
2221 | glBegin GL_LINE_LOOP; |
|
|
2222 | glVertex 0 , 0; |
|
|
2223 | glVertex 0 , $h; |
|
|
2224 | glVertex $w, $h; |
|
|
2225 | glVertex $w, 0; |
|
|
2226 | glEnd; |
|
|
2227 | |
|
|
2228 | glPopMatrix; |
|
|
2229 | |
|
|
2230 | glTranslate 2, 2; |
|
|
2231 | $self->SUPER::_draw; |
|
|
2232 | } |
|
|
2233 | |
|
|
2234 | ############################################################################# |
|
|
2235 | |
|
|
2236 | package CFClient::UI::Root; |
1673 | |
2237 | |
1674 | our @ISA = CFClient::UI::Container::; |
2238 | our @ISA = CFClient::UI::Container::; |
|
|
2239 | |
|
|
2240 | use CFClient::OpenGL; |
|
|
2241 | |
|
|
2242 | sub check_size { |
|
|
2243 | my ($self) = @_; |
|
|
2244 | |
|
|
2245 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
|
|
2246 | } |
1675 | |
2247 | |
1676 | sub size_request { |
2248 | sub size_request { |
1677 | ($::WIDTH, $::HEIGHT) |
2249 | ($::WIDTH, $::HEIGHT) |
1678 | } |
2250 | } |
1679 | |
2251 | |
1680 | sub size_allocate { |
2252 | sub configure { |
1681 | my ($self, $x, $y, $w, $h) = @_; |
2253 | my ($self, $x, $y, $w, $h) = @_; |
1682 | |
2254 | |
1683 | $self->_size_allocate ($x, $y, $w, $h); |
2255 | $self->SUPER::configure ($x, $y, $w, $h); |
1684 | |
2256 | |
1685 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
|
|
1686 | for @{$self->{children}}; |
2257 | for my $child (@{$self->{children}}) { |
1687 | } |
2258 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
1688 | |
2259 | |
1689 | sub translate { |
2260 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2261 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2262 | $child->configure ($X, $Y, $W,$H); |
|
|
2263 | } |
|
|
2264 | } |
|
|
2265 | |
|
|
2266 | sub _topleft { |
1690 | my ($self, $x, $y) = @_; |
2267 | my ($self, $x, $y) = @_; |
1691 | |
2268 | |
1692 | ($x, $y) |
2269 | ($x, $y) |
1693 | } |
2270 | } |
1694 | |
2271 | |
1695 | sub update { |
2272 | sub update { |
1696 | my ($self) = @_; |
2273 | my ($self) = @_; |
1697 | |
2274 | |
1698 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2275 | $self->check_size; |
1699 | ::refresh (); |
2276 | ::refresh (); |
1700 | } |
2277 | } |
1701 | |
2278 | |
1702 | sub add { |
2279 | sub add { |
1703 | my ($self, $widget) = @_; |
2280 | my ($self, $child) = @_; |
1704 | |
2281 | |
|
|
2282 | # integerize window positions |
|
|
2283 | $child->{x} = int $child->{x}; |
|
|
2284 | $child->{y} = int $child->{y}; |
|
|
2285 | |
1705 | $self->SUPER::add ($widget); |
2286 | $self->SUPER::add ($child); |
|
|
2287 | } |
1706 | |
2288 | |
1707 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
2289 | sub on_refresh { |
|
|
2290 | my ($self, $id, $cb) = @_; |
|
|
2291 | |
|
|
2292 | $self->{refresh_hook}{$id} = $cb; |
1708 | } |
2293 | } |
1709 | |
2294 | |
1710 | sub draw { |
2295 | sub draw { |
1711 | my ($self) = @_; |
2296 | my ($self) = @_; |
1712 | |
2297 | |
|
|
2298 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2299 | $_->() for values %$rcb; |
|
|
2300 | } |
|
|
2301 | |
|
|
2302 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2303 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2304 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2305 | |
|
|
2306 | glMatrixMode GL_PROJECTION; |
|
|
2307 | glLoadIdentity; |
|
|
2308 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2309 | glMatrixMode GL_MODELVIEW; |
|
|
2310 | glLoadIdentity; |
|
|
2311 | |
1713 | $self->_draw; |
2312 | $self->_draw; |
1714 | } |
2313 | } |
1715 | |
2314 | |
1716 | ############################################################################# |
2315 | ############################################################################# |
1717 | |
2316 | |
1718 | package CFClient::UI; |
2317 | package CFClient::UI; |
1719 | |
2318 | |
|
|
2319 | $ROOT = new CFClient::UI::Root; |
1720 | $TOPLEVEL = new CFClient::UI::Toplevel; |
2320 | $TOOLTIP = new CFClient::UI::Tooltip; |
1721 | |
2321 | |
1722 | 1 |
2322 | 1 |
1723 | |
2323 | |