1 | package CFPlus::UI; |
1 | package DC::UI; |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use List::Util (); |
6 | use List::Util (); |
7 | use Event; |
|
|
8 | |
7 | |
9 | use CFPlus; |
8 | use DC; |
10 | use CFPlus::Pod; |
9 | use DC::Pod; |
11 | use CFPlus::Texture; |
10 | use DC::Texture; |
12 | |
11 | |
13 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
14 | |
13 | |
15 | our $LAYOUT; |
14 | our $LAYOUT; |
16 | our $ROOT; |
15 | our $ROOT; |
17 | our $TOOLTIP; |
16 | our $TOOLTIP; |
18 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
19 | |
18 | |
20 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
21 | |
20 | |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
21 | our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub { |
|
|
22 | $_[0]->stop; |
|
|
23 | |
23 | if (!$GRAB) { |
24 | if (!$GRAB) { |
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
25 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
25 | if (length $widget->{tooltip}) { |
26 | if (length $widget->{tooltip}) { |
26 | if ($TOOLTIP->{owner} != $widget) { |
27 | if ($TOOLTIP->{owner} != $widget) { |
|
|
28 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
27 | $TOOLTIP->hide; |
29 | $TOOLTIP->hide; |
28 | |
30 | |
29 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner} = $widget; |
|
|
32 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
30 | |
33 | |
31 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | return if $ENV{CFPLUS_DEBUG} & 8; |
32 | |
35 | |
33 | my $tip = $widget->{tooltip}; |
|
|
34 | |
|
|
35 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
36 | |
|
|
37 | $TOOLTIP->set_tooltip_from ($widget); |
36 | $TOOLTIP->set_tooltip_from ($widget); |
38 | $TOOLTIP->show; |
37 | $TOOLTIP->show; |
39 | } |
38 | } |
40 | |
39 | |
41 | return; |
40 | return; |
42 | } |
41 | } |
43 | } |
42 | } |
44 | } |
43 | } |
45 | |
44 | |
46 | $TOOLTIP->hide; |
45 | $TOOLTIP->hide; |
|
|
46 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
47 | delete $TOOLTIP->{owner}; |
47 | delete $TOOLTIP->{owner}; |
48 | }); |
48 | }; |
49 | |
49 | |
50 | sub get_layout { |
50 | sub get_layout { |
51 | my $layout; |
51 | my $layout; |
52 | |
52 | |
53 | for (grep { $_->{name} } values %WIDGET) { |
53 | for (grep { $_->{name} } values %WIDGET) { |
… | |
… | |
88 | my $hover = $HOVER; $HOVER = $widget; |
88 | my $hover = $HOVER; $HOVER = $widget; |
89 | |
89 | |
90 | $hover->update if $hover && $hover->{can_hover}; |
90 | $hover->update if $hover && $hover->{can_hover}; |
91 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
91 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
92 | |
92 | |
93 | $TOOLTIP_WATCHER->start; |
93 | $TOOLTIP_WATCHER->again; |
94 | } |
94 | } |
95 | } |
95 | } |
96 | |
96 | |
97 | sub feed_sdl_button_down_event { |
97 | sub feed_sdl_button_down_event { |
98 | my ($ev) = @_; |
98 | my ($ev) = @_; |
… | |
… | |
104 | my $widget = $ROOT->find_widget ($x, $y); |
104 | my $widget = $ROOT->find_widget ($x, $y); |
105 | |
105 | |
106 | $GRAB = $widget; |
106 | $GRAB = $widget; |
107 | $GRAB->update if $GRAB; |
107 | $GRAB->update if $GRAB; |
108 | |
108 | |
109 | $TOOLTIP_WATCHER->cb->(); |
109 | $TOOLTIP_WATCHER->invoke; |
110 | } |
110 | } |
111 | |
111 | |
112 | if ($GRAB) { |
112 | if ($GRAB) { |
113 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
113 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
114 | # mousewheel |
114 | # mousewheel |
115 | $ev->{dx} = 0; |
|
|
116 | $ev->{dy} = $ev->{button} * 2 - 9; |
115 | my $delta = $ev->{button} * 2 - 9; |
|
|
116 | my $shift = $ev->{mod} & DC::KMOD_SHIFT; |
|
|
117 | |
|
|
118 | $ev->{dx} = $shift ? $delta : 0; |
|
|
119 | $ev->{dy} = $shift ? 0 : $delta; |
|
|
120 | |
117 | $GRAB->emit (mouse_wheel => $ev); |
121 | $GRAB->emit (mouse_wheel => $ev); |
118 | } else { |
122 | } else { |
119 | $GRAB->emit (button_down => $ev) |
123 | $GRAB->emit (button_down => $ev) |
120 | } |
124 | } |
121 | } |
125 | } |
… | |
… | |
135 | my $grab = $GRAB; undef $GRAB; |
139 | my $grab = $GRAB; undef $GRAB; |
136 | $grab->update if $grab; |
140 | $grab->update if $grab; |
137 | $GRAB->update if $GRAB; |
141 | $GRAB->update if $GRAB; |
138 | |
142 | |
139 | check_hover $widget; |
143 | check_hover $widget; |
140 | $TOOLTIP_WATCHER->cb->(); |
144 | $TOOLTIP_WATCHER->invoke; |
141 | } |
145 | } |
142 | } |
146 | } |
143 | |
147 | |
144 | sub feed_sdl_motion_event { |
148 | sub feed_sdl_motion_event { |
145 | my ($ev) = @_; |
149 | my ($ev) = @_; |
… | |
… | |
207 | reconfigure_widgets; |
211 | reconfigure_widgets; |
208 | } |
212 | } |
209 | |
213 | |
210 | ############################################################################# |
214 | ############################################################################# |
211 | |
215 | |
212 | package CFPlus::UI::Event; |
216 | package DC::UI::Event; |
213 | |
217 | |
214 | sub xy { |
218 | sub xy { |
215 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
219 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
216 | } |
220 | } |
217 | |
221 | |
218 | ############################################################################# |
222 | ############################################################################# |
219 | |
223 | |
220 | package CFPlus::UI::Base; |
224 | package DC::UI::Base; |
221 | |
225 | |
222 | use strict; |
226 | use strict; |
223 | |
227 | |
224 | use CFPlus::OpenGL; |
228 | use DC::OpenGL; |
225 | |
229 | |
226 | sub new { |
230 | sub new { |
227 | my $class = shift; |
231 | my $class = shift; |
228 | |
232 | |
229 | my $self = bless { |
233 | my $self = bless { |
… | |
… | |
234 | h => undef, |
238 | h => undef, |
235 | can_events => 1, |
239 | can_events => 1, |
236 | @_ |
240 | @_ |
237 | }, $class; |
241 | }, $class; |
238 | |
242 | |
239 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
243 | DC::weaken ($DC::UI::WIDGET{$self+0} = $self); |
240 | |
244 | |
241 | for (keys %$self) { |
245 | for (keys %$self) { |
242 | if (/^on_(.*)$/) { |
246 | if (/^on_(.*)$/) { |
243 | $self->connect ($1 => delete $self->{$_}); |
247 | $self->connect ($1 => delete $self->{$_}); |
244 | } |
248 | } |
245 | } |
249 | } |
246 | |
250 | |
247 | if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { |
251 | if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) { |
248 | $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; |
252 | $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x}; |
249 | $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; |
253 | $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y}; |
250 | $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; |
254 | $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w}; |
251 | $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; |
255 | $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h}; |
252 | |
256 | |
253 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
257 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
254 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
258 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
255 | |
259 | |
256 | $self->show if $layout->{show}; |
260 | $self->show if $layout->{show}; |
… | |
… | |
265 | $self->hide; |
269 | $self->hide; |
266 | $self->emit ("destroy"); |
270 | $self->emit ("destroy"); |
267 | %$self = (); |
271 | %$self = (); |
268 | } |
272 | } |
269 | |
273 | |
|
|
274 | sub TO_JSON { |
|
|
275 | { "\fw" => $_[0]{s_id} } |
|
|
276 | } |
|
|
277 | |
270 | sub show { |
278 | sub show { |
271 | my ($self) = @_; |
279 | my ($self) = @_; |
272 | |
280 | |
273 | return if $self->{parent}; |
281 | return if $self->{parent}; |
274 | |
282 | |
275 | $CFPlus::UI::ROOT->add ($self); |
283 | $DC::UI::ROOT->add ($self); |
276 | } |
284 | } |
277 | |
285 | |
278 | sub set_visible { |
286 | sub set_visible { |
279 | my ($self) = @_; |
287 | my ($self) = @_; |
280 | |
288 | |
… | |
… | |
301 | delete $self->{root}; |
309 | delete $self->{root}; |
302 | |
310 | |
303 | undef $GRAB if $GRAB == $self; |
311 | undef $GRAB if $GRAB == $self; |
304 | undef $HOVER if $HOVER == $self; |
312 | undef $HOVER if $HOVER == $self; |
305 | |
313 | |
306 | $CFPlus::UI::TOOLTIP_WATCHER->cb->() |
314 | $DC::UI::TOOLTIP_WATCHER->invoke |
307 | if $TOOLTIP->{owner} == $self; |
315 | if $TOOLTIP->{owner} == $self; |
308 | |
316 | |
309 | $self->emit ("focus_out"); |
317 | $self->emit ("focus_out"); |
310 | $self->emit (visibility_change => 0); |
318 | $self->emit (visibility_change => 0); |
311 | } |
319 | } |
… | |
… | |
313 | sub set_visibility { |
321 | sub set_visibility { |
314 | my ($self, $visible) = @_; |
322 | my ($self, $visible) = @_; |
315 | |
323 | |
316 | return if $self->{visible} == $visible; |
324 | return if $self->{visible} == $visible; |
317 | |
325 | |
318 | $visible ? $self->hide |
326 | $visible ? $self->show |
319 | : $self->show; |
327 | : $self->hide; |
320 | } |
328 | } |
321 | |
329 | |
322 | sub toggle_visibility { |
330 | sub toggle_visibility { |
323 | my ($self) = @_; |
331 | my ($self) = @_; |
324 | |
332 | |
… | |
… | |
330 | sub hide { |
338 | sub hide { |
331 | my ($self) = @_; |
339 | my ($self) = @_; |
332 | |
340 | |
333 | $self->set_invisible; |
341 | $self->set_invisible; |
334 | |
342 | |
|
|
343 | # extra $parent copy for 5.8.8+ bug workaround |
|
|
344 | # (otherwise $_[0] in remove gets freed |
|
|
345 | if (my $parent = $self->{parent}) { |
335 | $self->{parent}->remove ($self) |
346 | $parent->remove ($self); |
336 | if $self->{parent}; |
347 | } |
337 | } |
348 | } |
338 | |
349 | |
339 | sub move_abs { |
350 | sub move_abs { |
340 | my ($self, $x, $y, $z) = @_; |
351 | my ($self, $x, $y, $z) = @_; |
341 | |
352 | |
… | |
… | |
353 | $self->{force_h} = $h; |
364 | $self->{force_h} = $h; |
354 | |
365 | |
355 | $self->realloc; |
366 | $self->realloc; |
356 | } |
367 | } |
357 | |
368 | |
|
|
369 | # traverse the widget chain up to find the maximum "physical" size constraints |
|
|
370 | sub get_max_wh { |
|
|
371 | my ($self) = @_; |
|
|
372 | |
|
|
373 | return $self->{parent}->get_max_wh |
|
|
374 | if $self->{parent}; |
|
|
375 | |
|
|
376 | ($::WIDTH, $::HEIGHT) |
|
|
377 | } |
|
|
378 | |
358 | sub size_request { |
379 | sub size_request { |
359 | require Carp; |
380 | require Carp; |
360 | Carp::confess "size_request is abstract"; |
381 | Carp::confess "size_request is abstract"; |
361 | } |
382 | } |
362 | |
383 | |
… | |
… | |
368 | my ($self, $x, $y, $w, $h) = @_; |
389 | my ($self, $x, $y, $w, $h) = @_; |
369 | |
390 | |
370 | if ($self->{aspect}) { |
391 | if ($self->{aspect}) { |
371 | my ($ow, $oh) = ($w, $h); |
392 | my ($ow, $oh) = ($w, $h); |
372 | |
393 | |
373 | $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; |
394 | $w = List::Util::min $w, DC::ceil $h * $self->{aspect}; |
374 | $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; |
395 | $h = List::Util::min $h, DC::ceil $w / $self->{aspect}; |
375 | |
396 | |
376 | # use alignment to adjust x, y |
397 | # use alignment to adjust x, y |
377 | |
398 | |
378 | $x += int 0.5 * ($ow - $w); |
399 | $x += int 0.5 * ($ow - $w); |
379 | $y += int 0.5 * ($oh - $h); |
400 | $y += int 0.5 * ($oh - $h); |
… | |
… | |
420 | |
441 | |
421 | return if $self->{tooltip} eq $tooltip; |
442 | return if $self->{tooltip} eq $tooltip; |
422 | |
443 | |
423 | $self->{tooltip} = $tooltip; |
444 | $self->{tooltip} = $tooltip; |
424 | |
445 | |
425 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
446 | if ($DC::UI::TOOLTIP->{owner} == $self) { |
426 | delete $CFPlus::UI::TOOLTIP->{owner}; |
447 | delete $DC::UI::TOOLTIP->{owner}; |
427 | $CFPlus::UI::TOOLTIP_WATCHER->cb->(); |
448 | $DC::UI::TOOLTIP_WATCHER->invoke; |
428 | } |
449 | } |
429 | } |
450 | } |
430 | |
451 | |
431 | # translate global coordinates to local coordinate system |
452 | # translate global coordinates to local coordinate system |
432 | sub coord2local { |
453 | sub coord2local { |
433 | my ($self, $x, $y) = @_; |
454 | my ($self, $x, $y) = @_; |
434 | |
455 | |
435 | Carp::confess unless $self->{parent};#d# |
456 | return (undef, undef) unless $self->{parent}; |
436 | |
457 | |
437 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
458 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
438 | } |
459 | } |
439 | |
460 | |
440 | # translate local coordinates to global coordinate system |
461 | # translate local coordinates to global coordinate system |
441 | sub coord2global { |
462 | sub coord2global { |
442 | my ($self, $x, $y) = @_; |
463 | my ($self, $x, $y) = @_; |
443 | |
464 | |
444 | Carp::confess unless $self->{parent};#d# |
465 | return (undef, undef) unless $self->{parent}; |
445 | |
466 | |
446 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
467 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
447 | } |
468 | } |
448 | |
469 | |
449 | sub invoke_focus_in { |
470 | sub invoke_focus_in { |
… | |
… | |
498 | sub connect { |
519 | sub connect { |
499 | my ($self, $signal, $cb) = @_; |
520 | my ($self, $signal, $cb) = @_; |
500 | |
521 | |
501 | push @{ $self->{signal_cb}{$signal} }, $cb; |
522 | push @{ $self->{signal_cb}{$signal} }, $cb; |
502 | |
523 | |
503 | defined wantarray and CFPlus::guard { |
524 | defined wantarray and DC::guard { |
504 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
525 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
505 | @{ $self->{signal_cb}{$signal} }; |
526 | @{ $self->{signal_cb}{$signal} }; |
506 | } |
527 | } |
507 | } |
528 | } |
508 | |
529 | |
… | |
… | |
540 | |
561 | |
541 | # parent |
562 | # parent |
542 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
563 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
543 | } |
564 | } |
544 | |
565 | |
545 | sub find_widget { |
566 | #sub find_widget { |
546 | my ($self, $x, $y) = @_; |
567 | # in .xs |
547 | |
|
|
548 | return () unless $self->{can_events}; |
|
|
549 | |
|
|
550 | return $self |
|
|
551 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
552 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
|
|
553 | |
|
|
554 | () |
|
|
555 | } |
|
|
556 | |
568 | |
557 | sub set_parent { |
569 | sub set_parent { |
558 | my ($self, $parent) = @_; |
570 | my ($self, $parent) = @_; |
559 | |
571 | |
560 | CFPlus::weaken ($self->{parent} = $parent); |
572 | DC::weaken ($self->{parent} = $parent); |
561 | $self->set_visible if $parent->{visible}; |
573 | $self->set_visible if $parent->{visible}; |
562 | } |
574 | } |
563 | |
575 | |
564 | sub realloc { |
576 | sub realloc { |
565 | my ($self) = @_; |
577 | my ($self) = @_; |
… | |
… | |
591 | |
603 | |
592 | # using global variables seems a bit hacky, but passing through all drawing |
604 | # using global variables seems a bit hacky, but passing through all drawing |
593 | # functions seems pointless. |
605 | # functions seems pointless. |
594 | our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn |
606 | our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn |
595 | |
607 | |
596 | sub draw { |
608 | #sub draw { |
597 | my ($self) = @_; |
609 | #CFPlus.xs |
598 | |
|
|
599 | return unless $self->{h} && $self->{w}; |
|
|
600 | |
|
|
601 | # update screen rectangle |
|
|
602 | local $draw_x = $draw_x + $self->{x}; |
|
|
603 | local $draw_y = $draw_y + $self->{y}; |
|
|
604 | |
|
|
605 | # skip widgets that are entirely outside the drawing area |
|
|
606 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
|
|
607 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
|
|
608 | |
|
|
609 | glPushMatrix; |
|
|
610 | glTranslate $self->{x}, $self->{y}, 0; |
|
|
611 | |
|
|
612 | if ($self == $HOVER && $self->{can_hover}) { |
|
|
613 | glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2; |
|
|
614 | glEnable GL_BLEND; |
|
|
615 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
616 | glBegin GL_QUADS; |
|
|
617 | glVertex 0 , 0; |
|
|
618 | glVertex $self->{w}, 0; |
|
|
619 | glVertex $self->{w}, $self->{h}; |
|
|
620 | glVertex 0 , $self->{h}; |
|
|
621 | glEnd; |
|
|
622 | glDisable GL_BLEND; |
|
|
623 | } |
|
|
624 | |
|
|
625 | if ($ENV{CFPLUS_DEBUG} & 1) { |
|
|
626 | glPushMatrix; |
|
|
627 | glColor 1, 1, 0, 1; |
|
|
628 | glTranslate 0.375, 0.375; |
|
|
629 | glBegin GL_LINE_LOOP; |
|
|
630 | glVertex 0 , 0; |
|
|
631 | glVertex $self->{w} - 1, 0; |
|
|
632 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
633 | glVertex 0 , $self->{h} - 1; |
|
|
634 | glEnd; |
|
|
635 | glPopMatrix; |
|
|
636 | #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
|
|
637 | } |
|
|
638 | |
|
|
639 | $self->_draw; |
|
|
640 | glPopMatrix; |
|
|
641 | } |
|
|
642 | |
610 | |
643 | sub _draw { |
611 | sub _draw { |
644 | my ($self) = @_; |
612 | my ($self) = @_; |
645 | |
613 | |
646 | warn "no draw defined for $self\n"; |
614 | warn "no draw defined for $self\n"; |
647 | } |
615 | } |
648 | |
616 | |
649 | my $cntx;#d# |
|
|
650 | sub DESTROY { |
617 | sub DESTROY { |
651 | my ($self) = @_; |
618 | my ($self) = @_; |
652 | |
619 | |
653 | return if CFPlus::in_destruct; |
620 | return if DC::in_destruct; |
654 | |
621 | |
|
|
622 | local $@; |
655 | eval { $self->destroy }; |
623 | eval { $self->destroy }; |
656 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
624 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
657 | |
625 | |
658 | delete $WIDGET{$self+0}; |
626 | delete $WIDGET{$self+0}; |
659 | } |
627 | } |
660 | |
628 | |
661 | ############################################################################# |
629 | ############################################################################# |
662 | |
630 | |
663 | package CFPlus::UI::DrawBG; |
631 | package DC::UI::DrawBG; |
664 | |
632 | |
665 | our @ISA = CFPlus::UI::Base::; |
633 | our @ISA = DC::UI::Base::; |
666 | |
634 | |
667 | use strict; |
635 | use strict; |
668 | use CFPlus::OpenGL; |
636 | use DC::OpenGL; |
669 | |
637 | |
670 | sub new { |
638 | sub new { |
671 | my $class = shift; |
639 | my $class = shift; |
672 | |
|
|
673 | # range [value, low, high, page] |
|
|
674 | |
640 | |
675 | $class->SUPER::new ( |
641 | $class->SUPER::new ( |
676 | #bg => [0, 0, 0, 0.2], |
642 | #bg => [0, 0, 0, 0.2], |
677 | #active_bg => [1, 1, 1, 0.5], |
643 | #active_bg => [1, 1, 1, 0.5], |
678 | @_ |
644 | @_ |
… | |
… | |
690 | my ($w, $h) = @$self{qw(w h)}; |
656 | my ($w, $h) = @$self{qw(w h)}; |
691 | |
657 | |
692 | glEnable GL_BLEND; |
658 | glEnable GL_BLEND; |
693 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
659 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
694 | glColor_premultiply @$color; |
660 | glColor_premultiply @$color; |
695 | |
|
|
696 | glBegin GL_QUADS; |
|
|
697 | glVertex 0 , 0; |
|
|
698 | glVertex 0 , $h; |
|
|
699 | glVertex $w, $h; |
661 | glRect 0, 0, $w, $h; |
700 | glVertex $w, 0; |
|
|
701 | glEnd; |
|
|
702 | |
|
|
703 | glDisable GL_BLEND; |
662 | glDisable GL_BLEND; |
704 | } |
663 | } |
705 | } |
664 | } |
706 | |
665 | |
707 | ############################################################################# |
666 | ############################################################################# |
708 | |
667 | |
709 | package CFPlus::UI::Empty; |
668 | package DC::UI::Empty; |
710 | |
669 | |
711 | our @ISA = CFPlus::UI::Base::; |
670 | our @ISA = DC::UI::Base::; |
712 | |
671 | |
713 | sub new { |
672 | sub new { |
714 | my ($class, %arg) = @_; |
673 | my ($class, %arg) = @_; |
715 | $class->SUPER::new (can_events => 0, %arg); |
674 | $class->SUPER::new (can_events => 0, %arg); |
716 | } |
675 | } |
… | |
… | |
723 | |
682 | |
724 | sub draw { } |
683 | sub draw { } |
725 | |
684 | |
726 | ############################################################################# |
685 | ############################################################################# |
727 | |
686 | |
728 | package CFPlus::UI::Container; |
687 | package DC::UI::Container; |
729 | |
688 | |
730 | our @ISA = CFPlus::UI::Base::; |
689 | our @ISA = DC::UI::Base::; |
731 | |
690 | |
732 | sub new { |
691 | sub new { |
733 | my ($class, %arg) = @_; |
692 | my ($class, %arg) = @_; |
734 | |
693 | |
735 | my $children = delete $arg{children}; |
694 | my $children = delete $arg{children}; |
… | |
… | |
739 | can_events => 0, |
698 | can_events => 0, |
740 | %arg, |
699 | %arg, |
741 | ); |
700 | ); |
742 | |
701 | |
743 | $self->add (@$children) |
702 | $self->add (@$children) |
744 | if $children; |
703 | if $children && @$children; |
745 | |
704 | |
746 | $self |
705 | $self |
747 | } |
706 | } |
748 | |
707 | |
749 | sub realloc { |
708 | sub realloc { |
… | |
… | |
758 | my ($self, @widgets) = @_; |
717 | my ($self, @widgets) = @_; |
759 | |
718 | |
760 | $_->set_parent ($self) |
719 | $_->set_parent ($self) |
761 | for @widgets; |
720 | for @widgets; |
762 | |
721 | |
|
|
722 | # TODO: only do this in widgets that need it, e.g. root, fixed |
763 | use sort 'stable'; |
723 | use sort 'stable'; |
764 | |
724 | |
765 | $self->{children} = [ |
725 | $self->{children} = [ |
766 | sort { $a->{z} <=> $b->{z} } |
726 | sort { $a->{z} <=> $b->{z} } |
767 | @{$self->{children}}, @widgets |
727 | @{$self->{children}}, @widgets |
768 | ]; |
728 | ]; |
769 | |
729 | |
770 | $self->realloc; |
730 | $self->realloc; |
|
|
731 | |
|
|
732 | $self->emit (c_add => \@widgets); |
|
|
733 | |
|
|
734 | map $_+0, @widgets |
771 | } |
735 | } |
772 | |
736 | |
773 | sub children { |
737 | sub children { |
774 | @{ $_[0]{children} } |
738 | @{ $_[0]{children} } |
775 | } |
739 | } |
776 | |
740 | |
777 | sub remove { |
741 | sub remove { |
778 | my ($self, $child) = @_; |
742 | my ($self, @widgets) = @_; |
779 | |
743 | |
|
|
744 | $self->emit (c_remove => \@widgets); |
|
|
745 | |
|
|
746 | for my $child (@widgets) { |
780 | delete $child->{parent}; |
747 | delete $child->{parent}; |
781 | $child->hide; |
748 | $child->hide; |
782 | |
|
|
783 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
749 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
750 | } |
784 | |
751 | |
785 | $self->realloc; |
752 | $self->realloc; |
786 | } |
753 | } |
787 | |
754 | |
788 | sub clear { |
755 | sub clear { |
789 | my ($self) = @_; |
756 | my ($self) = @_; |
790 | |
757 | |
791 | my $children = delete $self->{children}; |
758 | my $children = $self->{children}; |
792 | $self->{children} = []; |
759 | $self->{children} = []; |
793 | |
760 | |
794 | for (@$children) { |
761 | for (@$children) { |
795 | delete $_->{parent}; |
762 | delete $_->{parent}; |
796 | $_->hide; |
763 | $_->hide; |
… | |
… | |
816 | } |
783 | } |
817 | |
784 | |
818 | sub _draw { |
785 | sub _draw { |
819 | my ($self) = @_; |
786 | my ($self) = @_; |
820 | |
787 | |
821 | $_->draw for @{$self->{children}}; |
788 | $_->draw for $self->visible_children; |
822 | } |
789 | } |
823 | |
790 | |
824 | ############################################################################# |
791 | ############################################################################# |
825 | |
792 | |
826 | package CFPlus::UI::Bin; |
793 | package DC::UI::Bin; |
827 | |
794 | |
828 | our @ISA = CFPlus::UI::Container::; |
795 | our @ISA = DC::UI::Container::; |
829 | |
796 | |
830 | sub new { |
797 | sub new { |
831 | my ($class, %arg) = @_; |
798 | my ($class, %arg) = @_; |
832 | |
799 | |
833 | my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; |
800 | my $child = (delete $arg{child}) || new DC::UI::Empty::; |
834 | |
801 | |
835 | $class->SUPER::new (children => [$child], %arg) |
802 | $class->SUPER::new (children => [$child], %arg) |
836 | } |
803 | } |
837 | |
804 | |
838 | sub add { |
805 | sub add { |
839 | my ($self, $child) = @_; |
806 | my ($self, $child) = @_; |
840 | |
807 | |
841 | $self->SUPER::remove ($_) for @{ $self->{children} }; |
808 | $self->clear; |
842 | $self->SUPER::add ($child); |
809 | $self->SUPER::add ($child); |
843 | } |
810 | } |
844 | |
811 | |
845 | sub remove { |
812 | sub remove { |
846 | my ($self, $widget) = @_; |
813 | my ($self, $widget) = @_; |
847 | |
814 | |
848 | $self->SUPER::remove ($widget); |
815 | $self->SUPER::remove ($widget); |
849 | |
816 | |
850 | $self->{children} = [new CFPlus::UI::Empty] |
817 | $self->{children} = [new DC::UI::Empty] |
851 | unless @{$self->{children}}; |
818 | unless @{$self->{children}}; |
852 | } |
819 | } |
853 | |
820 | |
854 | sub child { $_[0]->{children}[0] } |
821 | sub child { $_[0]->{children}[0] } |
855 | |
822 | |
… | |
… | |
864 | |
831 | |
865 | 1 |
832 | 1 |
866 | } |
833 | } |
867 | |
834 | |
868 | ############################################################################# |
835 | ############################################################################# |
869 | |
|
|
870 | # back-buffered drawing area |
836 | # back-buffered drawing area |
871 | |
837 | |
872 | package CFPlus::UI::Window; |
838 | package DC::UI::Window; |
873 | |
839 | |
874 | our @ISA = CFPlus::UI::Bin::; |
840 | our @ISA = DC::UI::Bin::; |
875 | |
841 | |
876 | use CFPlus::OpenGL; |
842 | use DC::OpenGL; |
877 | |
843 | |
878 | sub new { |
844 | sub new { |
879 | my ($class, %arg) = @_; |
845 | my ($class, %arg) = @_; |
880 | |
846 | |
881 | my $self = $class->SUPER::new (%arg); |
847 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
903 | } |
869 | } |
904 | |
870 | |
905 | sub render_child { |
871 | sub render_child { |
906 | my ($self) = @_; |
872 | my ($self) = @_; |
907 | |
873 | |
908 | $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { |
874 | $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub { |
909 | glClearColor 0, 0, 0, 0; |
875 | glClearColor 0, 0, 0, 0; |
910 | glClear GL_COLOR_BUFFER_BIT; |
876 | glClear GL_COLOR_BUFFER_BIT; |
911 | |
877 | |
912 | { |
878 | { |
913 | package CFPlus::UI::Base; |
879 | package DC::UI::Base; |
914 | |
880 | |
915 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
881 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
916 | (0, 0, $self->{w}, $self->{h}); |
882 | (0, 0, $self->{w}, $self->{h}); |
917 | |
883 | |
918 | $self->_render; |
884 | $self->_render; |
… | |
… | |
935 | glDisable GL_TEXTURE_2D; |
901 | glDisable GL_TEXTURE_2D; |
936 | } |
902 | } |
937 | |
903 | |
938 | ############################################################################# |
904 | ############################################################################# |
939 | |
905 | |
940 | package CFPlus::UI::ViewPort; |
906 | package DC::UI::ViewPort; |
941 | |
907 | |
|
|
908 | use List::Util qw(min max); |
|
|
909 | |
942 | our @ISA = CFPlus::UI::Window::; |
910 | our @ISA = DC::UI::Window::; |
943 | |
911 | |
944 | sub new { |
912 | sub new { |
945 | my $class = shift; |
913 | my $class = shift; |
946 | |
914 | |
947 | $class->SUPER::new ( |
915 | $class->SUPER::new ( |
… | |
… | |
954 | sub size_request { |
922 | sub size_request { |
955 | my ($self) = @_; |
923 | my ($self) = @_; |
956 | |
924 | |
957 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
925 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
958 | |
926 | |
959 | $w = 10 if $self->{scroll_x}; |
927 | $w = 1 if $self->{scroll_x}; |
960 | $h = 10 if $self->{scroll_y}; |
928 | $h = 1 if $self->{scroll_y}; |
961 | |
929 | |
962 | ($w, $h) |
930 | ($w, $h) |
963 | } |
931 | } |
964 | |
932 | |
965 | sub invoke_size_allocate { |
933 | sub invoke_size_allocate { |
… | |
… | |
977 | } |
945 | } |
978 | |
946 | |
979 | sub set_offset { |
947 | sub set_offset { |
980 | my ($self, $x, $y) = @_; |
948 | my ($self, $x, $y) = @_; |
981 | |
949 | |
|
|
950 | my $x = max 0, min $self->child->{w} - $self->{w}, int $x; |
|
|
951 | my $y = max 0, min $self->child->{h} - $self->{h}, int $y; |
|
|
952 | |
|
|
953 | if ($x != $self->{view_x} or $y != $self->{view_y}) { |
982 | $self->{view_x} = int $x; |
954 | $self->{view_x} = $x; |
983 | $self->{view_y} = int $y; |
955 | $self->{view_y} = $y; |
984 | |
956 | |
|
|
957 | $self->emit (changed => $x, $y); |
985 | $self->update; |
958 | $self->update; |
|
|
959 | } |
|
|
960 | } |
|
|
961 | |
|
|
962 | sub set_center { |
|
|
963 | my ($self, $x, $y) = @_; |
|
|
964 | |
|
|
965 | $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5); |
|
|
966 | } |
|
|
967 | |
|
|
968 | sub make_visible { |
|
|
969 | my ($self, $x, $y, $border) = @_; |
|
|
970 | |
|
|
971 | if ( $x < $self->{view_x} + $self->{w} * $border |
|
|
972 | || $x > $self->{view_x} + $self->{w} * (1 - $border) |
|
|
973 | || $y < $self->{view_y} + $self->{h} * $border |
|
|
974 | || $y > $self->{view_y} + $self->{h} * (1 - $border) |
|
|
975 | ) { |
|
|
976 | $self->set_center ($x, $y); |
|
|
977 | } |
986 | } |
978 | } |
987 | |
979 | |
988 | # hmm, this does not work for topleft of $self... but we should not ask for that |
980 | # hmm, this does not work for topleft of $self... but we should not ask for that |
989 | sub coord2local { |
981 | sub coord2local { |
990 | my ($self, $x, $y) = @_; |
982 | my ($self, $x, $y) = @_; |
… | |
… | |
1005 | my ($self, $x, $y) = @_; |
997 | my ($self, $x, $y) = @_; |
1006 | |
998 | |
1007 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
999 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
1008 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1000 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1009 | ) { |
1001 | ) { |
1010 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1002 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1011 | } else { |
1003 | } else { |
1012 | $self->CFPlus::UI::Base::find_widget ($x, $y) |
1004 | $self->DC::UI::Base::find_widget ($x, $y) |
1013 | } |
1005 | } |
1014 | } |
1006 | } |
1015 | |
1007 | |
1016 | sub _render { |
1008 | sub _render { |
1017 | my ($self) = @_; |
1009 | my ($self) = @_; |
1018 | |
1010 | |
1019 | local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; |
1011 | local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x}; |
1020 | local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; |
1012 | local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y}; |
1021 | |
1013 | |
1022 | CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1014 | DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1023 | |
1015 | |
1024 | $self->SUPER::_render; |
1016 | $self->SUPER::_render; |
1025 | } |
1017 | } |
1026 | |
1018 | |
1027 | ############################################################################# |
1019 | ############################################################################# |
1028 | |
1020 | |
1029 | package CFPlus::UI::ScrolledWindow; |
1021 | package DC::UI::ScrolledWindow; |
1030 | |
1022 | |
1031 | our @ISA = CFPlus::UI::HBox::; |
1023 | our @ISA = DC::UI::Table::; |
1032 | |
1024 | |
1033 | sub new { |
1025 | sub new { |
1034 | my ($class, %arg) = @_; |
1026 | my ($class, %arg) = @_; |
1035 | |
1027 | |
1036 | my $child = delete $arg{child}; |
1028 | my $child = delete $arg{child}; |
1037 | |
1029 | |
1038 | my $self; |
1030 | my $self; |
1039 | |
1031 | |
1040 | my $slider = new CFPlus::UI::Slider |
1032 | my $hslider = new DC::UI::Slider |
|
|
1033 | c_col => 0, |
|
|
1034 | c_row => 1, |
|
|
1035 | vertical => 0, |
|
|
1036 | range => [0, 0, 1, 0.01], # HACK fix |
|
|
1037 | on_changed => sub { |
|
|
1038 | $self->{hpos} = $_[1]; |
|
|
1039 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
|
|
1040 | }, |
|
|
1041 | ; |
|
|
1042 | |
|
|
1043 | my $vslider = new DC::UI::Slider |
|
|
1044 | c_col => 1, |
|
|
1045 | c_row => 0, |
1041 | vertical => 1, |
1046 | vertical => 1, |
1042 | range => [0, 0, 1, 0.01], # HACK fix |
1047 | range => [0, 0, 1, 0.01], # HACK fix |
1043 | on_changed => sub { |
1048 | on_changed => sub { |
1044 | $self->{vp}->set_offset (0, $_[1]); |
1049 | $self->{vpos} = $_[1]; |
|
|
1050 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1045 | }, |
1051 | }, |
1046 | ; |
1052 | ; |
1047 | |
1053 | |
1048 | $self = $class->SUPER::new ( |
1054 | $self = $class->SUPER::new ( |
1049 | vp => (new CFPlus::UI::ViewPort expand => 1), |
1055 | scroll_x => 0, |
|
|
1056 | scroll_y => 1, |
1050 | can_events => 1, |
1057 | can_events => 1, |
1051 | slider => $slider, |
1058 | hslider => $hslider, |
|
|
1059 | vslider => $vslider, |
|
|
1060 | col_expand => [1, 0], |
|
|
1061 | row_expand => [1, 0], |
1052 | %arg, |
1062 | %arg, |
1053 | ); |
1063 | ); |
1054 | |
1064 | |
|
|
1065 | $self->{vp} = new DC::UI::ViewPort |
|
|
1066 | c_col => 0, |
|
|
1067 | c_row => 0, |
|
|
1068 | expand => 1, |
|
|
1069 | scroll_x => $self->{scroll_x}, |
|
|
1070 | scroll_y => $self->{scroll_y}, |
|
|
1071 | on_changed => sub { |
|
|
1072 | my ($vp, $x, $y) = @_; |
|
|
1073 | |
|
|
1074 | $vp->{parent}{hslider}->set_value ($x); |
|
|
1075 | $vp->{parent}{vslider}->set_value ($y); |
|
|
1076 | |
|
|
1077 | 0 |
|
|
1078 | }, |
|
|
1079 | on_size_allocate => sub { |
|
|
1080 | my ($vp, $w, $h) = @_; |
|
|
1081 | $vp->{parent}->update_slider; |
|
|
1082 | 0 |
|
|
1083 | }, |
|
|
1084 | ; |
|
|
1085 | |
1055 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
1086 | $self->SUPER::add ($self->{vp}); |
|
|
1087 | |
1056 | $self->add ($child) if $child; |
1088 | $self->add ($child) if $child; |
1057 | |
1089 | |
1058 | $self |
1090 | $self |
1059 | } |
1091 | } |
1060 | |
|
|
1061 | #TODO# update range on size_allocate depending on child |
|
|
1062 | |
1092 | |
1063 | sub add { |
1093 | sub add { |
1064 | my ($self, $widget) = @_; |
1094 | my ($self, $widget) = @_; |
1065 | |
1095 | |
1066 | $self->{vp}->add ($self->{child} = $widget); |
1096 | $self->{vp}->add ($self->{child} = $widget); |
1067 | } |
1097 | } |
1068 | |
1098 | |
|
|
1099 | sub set_offset { shift->{vp}->set_offset (@_) } |
|
|
1100 | sub set_center { shift->{vp}->set_center (@_) } |
|
|
1101 | sub make_visible { shift->{vp}->make_visible (@_) } |
|
|
1102 | |
|
|
1103 | sub update_slider { |
|
|
1104 | my ($self) = @_; |
|
|
1105 | |
|
|
1106 | my $child = ($self->{vp} or return)->child; |
|
|
1107 | |
|
|
1108 | if ($self->{scroll_x}) { |
|
|
1109 | my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w}); |
|
|
1110 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
|
|
1111 | |
|
|
1112 | my $visible = $w1 > $w2; |
|
|
1113 | if ($visible != $self->{hslider_visible}) { |
|
|
1114 | $self->{hslider_visible} = $visible; |
|
|
1115 | $visible ? $self->SUPER::add ($self->{hslider}) |
|
|
1116 | : $self->SUPER::remove ($self->{hslider}); |
|
|
1117 | } |
|
|
1118 | } |
|
|
1119 | |
|
|
1120 | if ($self->{scroll_y}) { |
|
|
1121 | my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h}); |
|
|
1122 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
|
|
1123 | |
|
|
1124 | my $visible = $h1 > $h2; |
|
|
1125 | if ($visible != $self->{vslider_visible}) { |
|
|
1126 | $self->{vslider_visible} = $visible; |
|
|
1127 | $visible ? $self->SUPER::add ($self->{vslider}) |
|
|
1128 | : $self->SUPER::remove ($self->{vslider}); |
|
|
1129 | } |
|
|
1130 | } |
|
|
1131 | } |
|
|
1132 | |
|
|
1133 | sub start_dragging { |
|
|
1134 | my ($self, $ev) = @_; |
|
|
1135 | |
|
|
1136 | $self->grab_focus; |
|
|
1137 | |
|
|
1138 | my $ox = $self->{vp}{view_x}; |
|
|
1139 | my $oy = $self->{vp}{view_y}; |
|
|
1140 | |
|
|
1141 | $self->{motion} = sub { |
|
|
1142 | my ($ev, $x, $y) = @_; |
|
|
1143 | |
|
|
1144 | $ox -= $ev->{xrel}; |
|
|
1145 | $oy -= $ev->{yrel}; |
|
|
1146 | |
|
|
1147 | $self->{vp}->set_offset ($ox, $oy); |
|
|
1148 | }; |
|
|
1149 | } |
|
|
1150 | |
1069 | sub invoke_mouse_wheel { |
1151 | sub invoke_mouse_wheel { |
1070 | my ($self, $ev) = @_; |
1152 | my ($self, $ev) = @_; |
1071 | |
1153 | |
1072 | return 0 unless $ev->{dy}; # only vertical movements |
|
|
1073 | |
|
|
1074 | $self->{slider}->emit (mouse_wheel => $ev); |
1154 | $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible}; |
|
|
1155 | $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible}; |
1075 | |
1156 | |
1076 | 1 |
1157 | 1 |
1077 | } |
1158 | } |
1078 | |
1159 | |
1079 | sub update_slider { |
1160 | sub invoke_button_down { |
1080 | my ($self) = @_; |
1161 | my ($self, $ev, $x, $y) = @_; |
1081 | |
1162 | |
1082 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); |
1163 | if ($ev->{button} == 2) { |
1083 | } |
1164 | $self->start_dragging ($ev); |
|
|
1165 | return 1; |
|
|
1166 | } |
1084 | |
1167 | |
1085 | sub update { |
1168 | 0 |
1086 | my ($self) = @_; |
1169 | } |
1087 | |
1170 | |
1088 | $self->SUPER::update; |
1171 | sub invoke_button_up { |
|
|
1172 | my ($self, $ev, $x, $y) = @_; |
1089 | |
1173 | |
1090 | $self->update_slider; |
1174 | if (delete $self->{motion}) { |
|
|
1175 | return 1; |
|
|
1176 | } |
|
|
1177 | |
|
|
1178 | 0 |
|
|
1179 | } |
|
|
1180 | |
|
|
1181 | sub invoke_mouse_motion { |
|
|
1182 | my ($self, $ev, $x, $y) = @_; |
|
|
1183 | |
|
|
1184 | if ($self->{motion}) { |
|
|
1185 | $self->{motion}->($ev, $x, $y); |
|
|
1186 | return 1; |
|
|
1187 | } |
|
|
1188 | |
|
|
1189 | 0 |
1091 | } |
1190 | } |
1092 | |
1191 | |
1093 | sub invoke_size_allocate { |
1192 | sub invoke_size_allocate { |
1094 | my ($self, $w, $h) = @_; |
1193 | my ($self, $w, $h) = @_; |
1095 | |
1194 | |
1096 | $self->update_slider; |
1195 | $self->update_slider; |
1097 | |
|
|
1098 | $self->SUPER::invoke_size_allocate ($w, $h) |
1196 | $self->SUPER::invoke_size_allocate ($w, $h) |
1099 | } |
1197 | } |
1100 | |
1198 | |
1101 | ############################################################################# |
1199 | ############################################################################# |
1102 | |
1200 | |
1103 | package CFPlus::UI::Frame; |
1201 | package DC::UI::Frame; |
1104 | |
1202 | |
1105 | our @ISA = CFPlus::UI::Bin::; |
1203 | our @ISA = DC::UI::Bin::; |
1106 | |
1204 | |
1107 | use CFPlus::OpenGL; |
1205 | use DC::OpenGL; |
1108 | |
1206 | |
1109 | sub new { |
1207 | sub new { |
1110 | my $class = shift; |
1208 | my $class = shift; |
1111 | |
1209 | |
1112 | $class->SUPER::new ( |
1210 | $class->SUPER::new ( |
… | |
… | |
1122 | my ($w, $h) = @$self{qw(w h)}; |
1220 | my ($w, $h) = @$self{qw(w h)}; |
1123 | |
1221 | |
1124 | glEnable GL_BLEND; |
1222 | glEnable GL_BLEND; |
1125 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1223 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1126 | glColor_premultiply @{ $self->{bg} }; |
1224 | glColor_premultiply @{ $self->{bg} }; |
1127 | |
|
|
1128 | glBegin GL_QUADS; |
|
|
1129 | glVertex 0 , 0; |
|
|
1130 | glVertex 0 , $h; |
|
|
1131 | glVertex $w, $h; |
1225 | glRect 0, 0, $w, $h; |
1132 | glVertex $w, 0; |
|
|
1133 | glEnd; |
|
|
1134 | |
|
|
1135 | glDisable GL_BLEND; |
1226 | glDisable GL_BLEND; |
1136 | } |
1227 | } |
1137 | |
1228 | |
1138 | $self->SUPER::_draw; |
1229 | $self->SUPER::_draw; |
1139 | } |
1230 | } |
1140 | |
1231 | |
1141 | ############################################################################# |
1232 | ############################################################################# |
1142 | |
1233 | |
1143 | package CFPlus::UI::FancyFrame; |
1234 | package DC::UI::FancyFrame; |
1144 | |
1235 | |
1145 | our @ISA = CFPlus::UI::Bin::; |
1236 | our @ISA = DC::UI::Bin::; |
1146 | |
1237 | |
1147 | use CFPlus::OpenGL; |
1238 | use DC::OpenGL; |
1148 | |
1239 | |
1149 | sub new { |
1240 | sub new { |
1150 | my ($class, %arg) = @_; |
1241 | my ($class, %arg) = @_; |
1151 | |
1242 | |
1152 | if ((exists $arg{label}) && !ref $arg{label}) { |
1243 | if ((exists $arg{label}) && !ref $arg{label}) { |
1153 | $arg{label} = new CFPlus::UI::Label |
1244 | $arg{label} = new DC::UI::Label |
1154 | align => 1, |
1245 | align => 1, |
1155 | valign => 0, |
1246 | valign => 0, |
1156 | text => $arg{label}, |
1247 | text => $arg{label}, |
1157 | fontsize => ($arg{border} || 0.8) * 0.75; |
1248 | fontsize => ($arg{border} || 0.8) * 0.75; |
1158 | } |
1249 | } |
… | |
… | |
1170 | |
1261 | |
1171 | sub add { |
1262 | sub add { |
1172 | my ($self, @widgets) = @_; |
1263 | my ($self, @widgets) = @_; |
1173 | |
1264 | |
1174 | $self->SUPER::add (@widgets); |
1265 | $self->SUPER::add (@widgets); |
1175 | $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; |
1266 | $self->DC::UI::Container::add ($self->{label}) if $self->{label}; |
1176 | } |
1267 | } |
1177 | |
1268 | |
1178 | sub border { |
1269 | sub border { |
1179 | int $_[0]{border} * $::FONTSIZE |
1270 | int $_[0]{border} * $::FONTSIZE |
1180 | } |
1271 | } |
… | |
… | |
1238 | } |
1329 | } |
1239 | } |
1330 | } |
1240 | |
1331 | |
1241 | ############################################################################# |
1332 | ############################################################################# |
1242 | |
1333 | |
1243 | package CFPlus::UI::Toplevel; |
1334 | package DC::UI::Toplevel; |
1244 | |
1335 | |
1245 | our @ISA = CFPlus::UI::Bin::; |
1336 | our @ISA = DC::UI::Bin::; |
1246 | |
1337 | |
1247 | use CFPlus::OpenGL; |
1338 | use DC::OpenGL; |
1248 | |
1339 | |
1249 | my $bg = |
1340 | my $bg = |
1250 | new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", |
1341 | new_from_file DC::Texture DC::find_rcfile "d1_bg.png", |
1251 | mipmap => 1, wrap => 1; |
1342 | mipmap => 1, wrap => 1; |
1252 | |
1343 | |
1253 | my @border = |
1344 | my @border = |
1254 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1345 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
1255 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1346 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
1347 | |
|
|
1348 | my @icon = |
|
|
1349 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
|
|
1350 | qw(x1_move.png x1_resize.png); |
1256 | |
1351 | |
1257 | sub new { |
1352 | sub new { |
1258 | my ($class, %arg) = @_; |
1353 | my ($class, %arg) = @_; |
1259 | |
1354 | |
1260 | my $self = $class->SUPER::new ( |
1355 | my $self = $class->SUPER::new ( |
… | |
… | |
1265 | min_w => 64, |
1360 | min_w => 64, |
1266 | min_h => 32, |
1361 | min_h => 32, |
1267 | %arg, |
1362 | %arg, |
1268 | ); |
1363 | ); |
1269 | |
1364 | |
1270 | $self->{title_widget} = new CFPlus::UI::Label |
1365 | $self->{title_widget} = new DC::UI::Label |
1271 | align => 0, |
1366 | align => 0, |
1272 | valign => 1, |
1367 | valign => 1, |
1273 | text => $self->{title}, |
1368 | text => $self->{title}, |
1274 | fontsize => $self->{border}, |
1369 | fontsize => $self->{border}, |
1275 | if exists $self->{title}; |
1370 | if exists $self->{title}; |
1276 | |
1371 | |
1277 | if ($self->{has_close_button}) { |
1372 | if ($self->{has_close_button}) { |
1278 | $self->{close_button} = |
1373 | $self->{close_button} = |
1279 | new CFPlus::UI::ImageButton |
1374 | new DC::UI::ImageButton |
1280 | path => 'x1_close.png', |
1375 | path => 'x1_close.png', |
1281 | on_activate => sub { $self->emit ("delete") }; |
1376 | on_activate => sub { $self->emit ("delete") }; |
1282 | |
1377 | |
1283 | $self->CFPlus::UI::Container::add ($self->{close_button}); |
1378 | $self->DC::UI::Container::add ($self->{close_button}); |
1284 | } |
1379 | } |
1285 | |
1380 | |
1286 | $self |
1381 | $self |
1287 | } |
1382 | } |
1288 | |
1383 | |
1289 | sub add { |
1384 | sub add { |
1290 | my ($self, @widgets) = @_; |
1385 | my ($self, @widgets) = @_; |
1291 | |
1386 | |
1292 | $self->SUPER::add (@widgets); |
1387 | $self->SUPER::add (@widgets); |
1293 | $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1388 | $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1294 | $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1389 | $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1295 | } |
1390 | } |
1296 | |
1391 | |
1297 | sub border { |
1392 | sub border { |
1298 | int $_[0]{border} * $::FONTSIZE |
1393 | int $_[0]{border} * $::FONTSIZE |
|
|
1394 | } |
|
|
1395 | |
|
|
1396 | sub get_max_wh { |
|
|
1397 | my ($self) = @_; |
|
|
1398 | |
|
|
1399 | return ($self->{w}, $self->{h}) |
|
|
1400 | if $self->{visible} && $self->{w}; |
|
|
1401 | |
|
|
1402 | $self->SUPER::get_max_wh |
1299 | } |
1403 | } |
1300 | |
1404 | |
1301 | sub size_request { |
1405 | sub size_request { |
1302 | my ($self) = @_; |
1406 | my ($self) = @_; |
1303 | |
1407 | |
… | |
… | |
1429 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1533 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1430 | |
1534 | |
1431 | my $border = $self->border; |
1535 | my $border = $self->border; |
1432 | |
1536 | |
1433 | glColor @{ $self->{border_bg} }; |
1537 | glColor @{ $self->{border_bg} }; |
1434 | $border[0]->draw_quad_alpha (0, 0, $w, $border); |
1538 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1435 | $border[1]->draw_quad_alpha (0, $border, $border, $ch); |
1539 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1436 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1540 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1437 | $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); |
1541 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
|
|
1542 | |
|
|
1543 | # move |
|
|
1544 | my $w2 = ($w - $border) * .5; |
|
|
1545 | my $h2 = ($h - $border) * .5; |
|
|
1546 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
|
|
1547 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
|
|
1548 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
|
|
1549 | |
|
|
1550 | # resize |
|
|
1551 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
|
|
1552 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
|
|
1553 | unless $self->{has_close_button}; |
|
|
1554 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
|
|
1555 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
1438 | |
1556 | |
1439 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1557 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1440 | glColor @{ $self->{bg} }; |
1558 | glColor @{ $self->{bg} }; |
1441 | |
1559 | |
1442 | # TODO: repeat texture not scale |
1560 | # TODO: repeat texture not scale |
… | |
… | |
1461 | if $self->{close_button}; |
1579 | if $self->{close_button}; |
1462 | } |
1580 | } |
1463 | |
1581 | |
1464 | ############################################################################# |
1582 | ############################################################################# |
1465 | |
1583 | |
1466 | package CFPlus::UI::Table; |
1584 | package DC::UI::Table; |
1467 | |
1585 | |
1468 | our @ISA = CFPlus::UI::Base::; |
1586 | our @ISA = DC::UI::Container::; |
1469 | |
1587 | |
1470 | use List::Util qw(max sum); |
1588 | use List::Util qw(max sum); |
1471 | |
1589 | |
1472 | use CFPlus::OpenGL; |
1590 | use DC::OpenGL; |
1473 | |
1591 | |
1474 | sub new { |
1592 | sub new { |
1475 | my $class = shift; |
1593 | my $class = shift; |
1476 | |
1594 | |
1477 | $class->SUPER::new ( |
1595 | $class->SUPER::new ( |
1478 | col_expand => [], |
1596 | col_expand => [], |
|
|
1597 | row_expand => [], |
1479 | @_, |
1598 | @_, |
1480 | ) |
1599 | ) |
1481 | } |
1600 | } |
1482 | |
1601 | |
1483 | sub children { |
|
|
1484 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1485 | } |
|
|
1486 | |
|
|
1487 | sub add { |
1602 | sub add { |
|
|
1603 | my ($self, @widgets) = @_; |
|
|
1604 | |
|
|
1605 | for my $child (@widgets) { |
|
|
1606 | $child->{c_rowspan} ||= 1; |
|
|
1607 | $child->{c_colspan} ||= 1; |
|
|
1608 | } |
|
|
1609 | |
|
|
1610 | $self->SUPER::add (@widgets); |
|
|
1611 | } |
|
|
1612 | |
|
|
1613 | sub add_at { |
1488 | my ($self) = shift; |
1614 | my $self = shift; |
|
|
1615 | |
|
|
1616 | my @widgets; |
1489 | |
1617 | |
1490 | while (@_) { |
1618 | while (@_) { |
1491 | my ($x, $y, $child) = splice @_, 0, 3, (); |
1619 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1492 | $child->set_parent ($self); |
|
|
1493 | $self->{children}[$y][$x] = $child; |
|
|
1494 | } |
|
|
1495 | |
1620 | |
1496 | $self->{force_realloc} = 1; |
1621 | $child->{c_row} = $row; |
1497 | $self->{force_size_alloc} = 1; |
1622 | $child->{c_col} = $col; |
1498 | $self->realloc; |
|
|
1499 | } |
|
|
1500 | |
1623 | |
1501 | sub remove { |
1624 | push @widgets, $child; |
|
|
1625 | } |
|
|
1626 | |
|
|
1627 | $self->add (@widgets); |
|
|
1628 | } |
|
|
1629 | |
|
|
1630 | sub get_wh { |
1502 | my ($self, $child) = @_; |
1631 | my ($self) = @_; |
1503 | |
1632 | |
1504 | # TODO: not yet implemented |
1633 | my (@w, @h); |
1505 | } |
|
|
1506 | |
|
|
1507 | # TODO: move to container class maybe? send children a signal on removal? |
|
|
1508 | sub clear { |
|
|
1509 | my ($self) = @_; |
|
|
1510 | |
1634 | |
1511 | my @children = $self->children; |
1635 | my @children = $self->children; |
1512 | delete $self->{children}; |
1636 | |
|
|
1637 | # first pass, columns |
|
|
1638 | for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) { |
|
|
1639 | my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; |
|
|
1640 | |
|
|
1641 | my $sw = sum @w[$c .. $c + $cs - 1]; |
|
|
1642 | |
|
|
1643 | if ($w > $sw) { |
|
|
1644 | $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
|
|
1645 | } |
1513 | |
1646 | } |
1514 | for (@children) { |
|
|
1515 | delete $_->{parent}; |
|
|
1516 | $_->hide; |
|
|
1517 | } |
|
|
1518 | |
1647 | |
1519 | $self->realloc; |
1648 | # second pass, rows |
1520 | } |
1649 | for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { |
1521 | |
|
|
1522 | sub get_wh { |
|
|
1523 | my ($self) = @_; |
|
|
1524 | |
|
|
1525 | my (@w, @h); |
|
|
1526 | |
|
|
1527 | for my $y (0 .. $#{$self->{children}}) { |
|
|
1528 | my $row = $self->{children}[$y] |
|
|
1529 | or next; |
|
|
1530 | |
|
|
1531 | for my $x (0 .. $#$row) { |
|
|
1532 | my $widget = $row->[$x] |
|
|
1533 | or next; |
|
|
1534 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
1650 | my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; |
1535 | |
1651 | |
1536 | $w[$x] = max $w[$x], $w; |
1652 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1537 | $h[$y] = max $h[$y], $h; |
1653 | |
|
|
1654 | if ($h > $sh) { |
|
|
1655 | $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1538 | } |
1656 | } |
1539 | } |
1657 | } |
1540 | |
1658 | |
1541 | (\@w, \@h) |
1659 | (\@w, \@h) |
1542 | } |
1660 | } |
… | |
… | |
1558 | my ($ws, $hs) = $self->get_wh; |
1676 | my ($ws, $hs) = $self->get_wh; |
1559 | |
1677 | |
1560 | my $req_w = (sum @$ws) || 1; |
1678 | my $req_w = (sum @$ws) || 1; |
1561 | my $req_h = (sum @$hs) || 1; |
1679 | my $req_h = (sum @$hs) || 1; |
1562 | |
1680 | |
1563 | # TODO: nicer code && do row_expand |
1681 | # now linearly scale the rows/columns to the allocated size |
1564 | my @col_expand = @{$self->{col_expand}}; |
1682 | my @col_expand = @{$self->{col_expand}}; |
1565 | @col_expand = (1) x @$ws unless @col_expand; |
1683 | @col_expand = (1) x @$ws unless @col_expand; |
1566 | my $col_expand = (sum @col_expand) || 1; |
1684 | my $col_expand = (sum @col_expand) || 1; |
1567 | |
1685 | |
1568 | # linearly scale sizes |
|
|
1569 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1686 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1570 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
1571 | |
1687 | |
1572 | CFPlus::UI::harmonize $ws; |
1688 | DC::UI::harmonize $ws; |
|
|
1689 | |
|
|
1690 | my @row_expand = @{$self->{row_expand}}; |
|
|
1691 | @row_expand = (1) x @$ws unless @row_expand; |
|
|
1692 | my $row_expand = (sum @row_expand) || 1; |
|
|
1693 | |
|
|
1694 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
|
|
1695 | |
1573 | CFPlus::UI::harmonize $hs; |
1696 | DC::UI::harmonize $hs; |
1574 | |
1697 | |
1575 | my $y; |
1698 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
|
|
1699 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1576 | |
1700 | |
1577 | for my $r (0 .. $#{$self->{children}}) { |
1701 | for my $widget ($self->children) { |
1578 | my $row = $self->{children}[$r] |
1702 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)}; |
1579 | or next; |
|
|
1580 | |
1703 | |
1581 | my $x = 0; |
1704 | $widget->configure ( |
1582 | my $row_h = $hs->[$r]; |
1705 | $x[$c], $y[$r], |
|
|
1706 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1583 | |
1707 | ); |
1584 | for my $c (0 .. $#$row) { |
1708 | } |
1585 | my $col_w = $ws->[$c]; |
|
|
1586 | |
1709 | |
1587 | if (my $widget = $row->[$c]) { |
1710 | 1 |
1588 | $widget->configure ($x, $y, $col_w, $row_h); |
1711 | } |
1589 | } |
|
|
1590 | |
1712 | |
1591 | $x += $col_w; |
1713 | ############################################################################# |
|
|
1714 | |
|
|
1715 | package DC::UI::Fixed; |
|
|
1716 | |
|
|
1717 | use List::Util qw(min max); |
|
|
1718 | |
|
|
1719 | our @ISA = DC::UI::Container::; |
|
|
1720 | |
|
|
1721 | sub _scale($$$) { |
|
|
1722 | my ($rel, $val, $max) = @_; |
|
|
1723 | |
|
|
1724 | $rel ? $val * $max : $val |
|
|
1725 | } |
|
|
1726 | |
|
|
1727 | sub size_request { |
|
|
1728 | my ($self) = @_; |
|
|
1729 | |
|
|
1730 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
|
|
1731 | |
|
|
1732 | # determine overall size by querying abs widgets |
|
|
1733 | for my $child ($self->visible_children) { |
|
|
1734 | unless ($child->{c_rel}) { |
|
|
1735 | my $x = $child->{c_x}; |
|
|
1736 | my $y = $child->{c_y}; |
|
|
1737 | |
|
|
1738 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
|
|
1739 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
1592 | } |
1740 | } |
|
|
1741 | } |
1593 | |
1742 | |
1594 | $y += $row_h; |
1743 | my $W = $x2 - $x1; |
|
|
1744 | my $H = $y2 - $y1; |
|
|
1745 | |
|
|
1746 | # now layout remaining widgets |
|
|
1747 | for my $child ($self->visible_children) { |
|
|
1748 | if ($child->{c_rel}) { |
|
|
1749 | my $x = _scale $child->{c_rel}, $child->{c_x}, $W; |
|
|
1750 | my $y = _scale $child->{c_rel}, $child->{c_y}, $H; |
|
|
1751 | |
|
|
1752 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
|
|
1753 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
|
|
1754 | } |
|
|
1755 | } |
|
|
1756 | |
|
|
1757 | my $W = $x2 - $x1; |
|
|
1758 | my $H = $y2 - $y1; |
|
|
1759 | |
|
|
1760 | ($W, $H) |
|
|
1761 | } |
|
|
1762 | |
|
|
1763 | sub invoke_size_allocate { |
|
|
1764 | my ($self, $W, $H) = @_; |
|
|
1765 | |
|
|
1766 | for my $child ($self->visible_children) { |
|
|
1767 | my $x = _scale $child->{c_rel}, $child->{c_x}, $W; |
|
|
1768 | my $y = _scale $child->{c_rel}, $child->{c_y}, $H; |
|
|
1769 | |
|
|
1770 | $x += $child->{c_halign} * $child->{req_w}; |
|
|
1771 | $y += $child->{c_valign} * $child->{req_h}; |
|
|
1772 | |
|
|
1773 | $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h}); |
1595 | } |
1774 | } |
1596 | |
1775 | |
1597 | 1 |
1776 | 1 |
1598 | } |
1777 | } |
1599 | |
1778 | |
1600 | sub find_widget { |
|
|
1601 | my ($self, $x, $y) = @_; |
|
|
1602 | |
|
|
1603 | $x -= $self->{x}; |
|
|
1604 | $y -= $self->{y}; |
|
|
1605 | |
|
|
1606 | my $res; |
|
|
1607 | |
|
|
1608 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
1609 | $res = $_->find_widget ($x, $y) |
|
|
1610 | and return $res; |
|
|
1611 | } |
|
|
1612 | |
|
|
1613 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
1614 | } |
|
|
1615 | |
|
|
1616 | sub _draw { |
|
|
1617 | my ($self) = @_; |
|
|
1618 | |
|
|
1619 | for (grep $_, @{$self->{children}}) { |
|
|
1620 | $_->draw for grep $_, @$_; |
|
|
1621 | } |
|
|
1622 | } |
|
|
1623 | |
|
|
1624 | ############################################################################# |
1779 | ############################################################################# |
1625 | |
1780 | |
1626 | package CFPlus::UI::Box; |
1781 | package DC::UI::Box; |
1627 | |
1782 | |
1628 | our @ISA = CFPlus::UI::Container::; |
1783 | our @ISA = DC::UI::Container::; |
1629 | |
1784 | |
1630 | sub size_request { |
1785 | sub size_request { |
1631 | my ($self) = @_; |
1786 | my ($self) = @_; |
|
|
1787 | |
|
|
1788 | my @children = $self->visible_children; |
1632 | |
1789 | |
1633 | $self->{vertical} |
1790 | $self->{vertical} |
1634 | ? ( |
1791 | ? ( |
1635 | (List::Util::max map $_->{req_w}, @{$self->{children}}), |
1792 | (List::Util::max map $_->{req_w}, @children), |
1636 | (List::Util::sum map $_->{req_h}, @{$self->{children}}), |
1793 | (List::Util::sum map $_->{req_h}, @children), |
1637 | ) |
1794 | ) |
1638 | : ( |
1795 | : ( |
1639 | (List::Util::sum map $_->{req_w}, @{$self->{children}}), |
1796 | (List::Util::sum map $_->{req_w}, @children), |
1640 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1797 | (List::Util::max map $_->{req_h}, @children), |
1641 | ) |
1798 | ) |
1642 | } |
1799 | } |
1643 | |
1800 | |
1644 | sub invoke_size_allocate { |
1801 | sub invoke_size_allocate { |
1645 | my ($self, $w, $h) = @_; |
1802 | my ($self, $w, $h) = @_; |
… | |
… | |
1666 | $req[$_] += $space * $children[$_]{expand} |
1823 | $req[$_] += $space * $children[$_]{expand} |
1667 | for 0 .. $#children; |
1824 | for 0 .. $#children; |
1668 | } |
1825 | } |
1669 | } |
1826 | } |
1670 | |
1827 | |
1671 | CFPlus::UI::harmonize \@req; |
1828 | DC::UI::harmonize \@req; |
1672 | |
1829 | |
1673 | my $pos = 0; |
1830 | my $pos = 0; |
1674 | for (0 .. $#children) { |
1831 | for (0 .. $#children) { |
1675 | my $alloc = $req[$_]; |
1832 | my $alloc = $req[$_]; |
1676 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1833 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
… | |
… | |
1681 | 1 |
1838 | 1 |
1682 | } |
1839 | } |
1683 | |
1840 | |
1684 | ############################################################################# |
1841 | ############################################################################# |
1685 | |
1842 | |
1686 | package CFPlus::UI::HBox; |
1843 | package DC::UI::HBox; |
1687 | |
1844 | |
1688 | our @ISA = CFPlus::UI::Box::; |
1845 | our @ISA = DC::UI::Box::; |
1689 | |
1846 | |
1690 | sub new { |
1847 | sub new { |
1691 | my $class = shift; |
1848 | my $class = shift; |
1692 | |
1849 | |
1693 | $class->SUPER::new ( |
1850 | $class->SUPER::new ( |
… | |
… | |
1696 | ) |
1853 | ) |
1697 | } |
1854 | } |
1698 | |
1855 | |
1699 | ############################################################################# |
1856 | ############################################################################# |
1700 | |
1857 | |
1701 | package CFPlus::UI::VBox; |
1858 | package DC::UI::VBox; |
1702 | |
1859 | |
1703 | our @ISA = CFPlus::UI::Box::; |
1860 | our @ISA = DC::UI::Box::; |
1704 | |
1861 | |
1705 | sub new { |
1862 | sub new { |
1706 | my $class = shift; |
1863 | my $class = shift; |
1707 | |
1864 | |
1708 | $class->SUPER::new ( |
1865 | $class->SUPER::new ( |
… | |
… | |
1711 | ) |
1868 | ) |
1712 | } |
1869 | } |
1713 | |
1870 | |
1714 | ############################################################################# |
1871 | ############################################################################# |
1715 | |
1872 | |
1716 | package CFPlus::UI::Label; |
1873 | package DC::UI::Label; |
1717 | |
1874 | |
1718 | our @ISA = CFPlus::UI::DrawBG::; |
1875 | our @ISA = DC::UI::DrawBG::; |
1719 | |
1876 | |
1720 | use CFPlus::OpenGL; |
1877 | use DC::OpenGL; |
1721 | |
1878 | |
1722 | sub new { |
1879 | sub new { |
1723 | my ($class, %arg) = @_; |
1880 | my ($class, %arg) = @_; |
1724 | |
1881 | |
1725 | my $self = $class->SUPER::new ( |
1882 | my $self = $class->SUPER::new ( |
… | |
… | |
1730 | #text => initial text |
1887 | #text => initial text |
1731 | #markup => initial narkup |
1888 | #markup => initial narkup |
1732 | #max_w => maximum pixel width |
1889 | #max_w => maximum pixel width |
1733 | #style => 0, # render flags |
1890 | #style => 0, # render flags |
1734 | ellipsise => 3, # end |
1891 | ellipsise => 3, # end |
1735 | layout => (new CFPlus::Layout), |
1892 | layout => (new DC::Layout), |
1736 | fontsize => 1, |
1893 | fontsize => 1, |
1737 | align => -1, |
1894 | align => -1, |
1738 | valign => -1, |
1895 | valign => -1, |
1739 | padding_x => 2, |
1896 | padding_x => 2, |
1740 | padding_y => 2, |
1897 | padding_y => 2, |
1741 | can_events => 0, |
1898 | can_events => 0, |
1742 | %arg |
1899 | %arg |
1743 | ); |
1900 | ); |
1744 | |
1901 | |
1745 | if (exists $self->{template}) { |
1902 | if (exists $self->{template}) { |
1746 | my $layout = new CFPlus::Layout; |
1903 | my $layout = new DC::Layout; |
1747 | $layout->set_text (delete $self->{template}); |
1904 | $layout->set_text (delete $self->{template}); |
1748 | $self->{template} = $layout; |
1905 | $self->{template} = $layout; |
1749 | } |
1906 | } |
1750 | |
1907 | |
1751 | if (exists $self->{markup}) { |
1908 | if (exists $self->{markup}) { |
… | |
… | |
1769 | |
1926 | |
1770 | delete $self->{ox}; |
1927 | delete $self->{ox}; |
1771 | $self->SUPER::realloc; |
1928 | $self->SUPER::realloc; |
1772 | } |
1929 | } |
1773 | |
1930 | |
|
|
1931 | sub clear { |
|
|
1932 | my ($self) = @_; |
|
|
1933 | |
|
|
1934 | $self->set_text (""); |
|
|
1935 | } |
|
|
1936 | |
1774 | sub set_text { |
1937 | sub set_text { |
1775 | my ($self, $text) = @_; |
1938 | my ($self, $text) = @_; |
1776 | |
1939 | |
1777 | return if $self->{text} eq "T$text"; |
1940 | return if $self->{text} eq "T$text"; |
1778 | $self->{text} = "T$text"; |
1941 | $self->{text} = "T$text"; |
… | |
… | |
1801 | |
1964 | |
1802 | sub size_request { |
1965 | sub size_request { |
1803 | my ($self) = @_; |
1966 | my ($self) = @_; |
1804 | |
1967 | |
1805 | $self->{size_req} ||= do { |
1968 | $self->{size_req} ||= do { |
|
|
1969 | my ($max_w, $max_h) = $self->get_max_wh; |
|
|
1970 | |
1806 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1971 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1807 | $self->{layout}->set_width ($self->{max_w} || -1); |
1972 | $self->{layout}->set_width ($self->{max_w} || $max_w || -1); |
1808 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1973 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1809 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1974 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1810 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1975 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1811 | |
1976 | |
1812 | my ($w, $h) = $self->{layout}->size; |
1977 | my ($w, $h) = $self->{layout}->size; |
… | |
… | |
1884 | : ($self->{w} - $size->[0]) * 0.5); |
2049 | : ($self->{w} - $size->[0]) * 0.5); |
1885 | |
2050 | |
1886 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2051 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
1887 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
2052 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
1888 | : ($self->{h} - $size->[1]) * 0.5); |
2053 | : ($self->{h} - $size->[1]) * 0.5); |
|
|
2054 | |
|
|
2055 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
1889 | }; |
2056 | }; |
1890 | |
2057 | |
1891 | my $w = List::Util::min $self->{w} + 4, $size->[0]; |
2058 | # unless ($self->{list}) { |
1892 | my $h = List::Util::min $self->{h} + 2, $size->[1]; |
2059 | # $self->{list} = DC::OpenGL::glGenList; |
1893 | |
2060 | # DC::OpenGL::glNewList $self->{list}; |
1894 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2061 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
|
|
2062 | # DC::OpenGL::glEndList; |
|
|
2063 | # } |
|
|
2064 | # |
|
|
2065 | # DC::OpenGL::glCallList $self->{list}; |
|
|
2066 | |
|
|
2067 | $self->{layout}->draw; |
1895 | } |
2068 | } |
|
|
2069 | |
|
|
2070 | #sub destroy { |
|
|
2071 | # my ($self) = @_; |
|
|
2072 | # |
|
|
2073 | # DC::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
|
|
2074 | # |
|
|
2075 | # $self->SUPER::destroy; |
|
|
2076 | #} |
1896 | |
2077 | |
1897 | ############################################################################# |
2078 | ############################################################################# |
1898 | |
2079 | |
1899 | package CFPlus::UI::EntryBase; |
2080 | package DC::UI::EntryBase; |
1900 | |
2081 | |
1901 | our @ISA = CFPlus::UI::Label::; |
2082 | our @ISA = DC::UI::Label::; |
1902 | |
2083 | |
1903 | use CFPlus::OpenGL; |
2084 | use DC::OpenGL; |
1904 | |
2085 | |
1905 | sub new { |
2086 | sub new { |
1906 | my $class = shift; |
2087 | my $class = shift; |
1907 | |
2088 | |
1908 | $class->SUPER::new ( |
2089 | $class->SUPER::new ( |
1909 | fg => [1, 1, 1], |
2090 | fg => [1, 1, 1], |
1910 | bg => [0, 0, 0, 0.2], |
2091 | bg => [0, 0, 0, 0.2], |
|
|
2092 | outline => [0.6, 0.3, 0.1], |
1911 | active_bg => [1, 1, 1, 0.5], |
2093 | active_bg => [0, 0, 1, .2], |
1912 | active_fg => [0, 0, 0], |
2094 | active_fg => [1, 1, 1], |
|
|
2095 | active_outline => [1, 1, 0], |
1913 | can_hover => 1, |
2096 | can_hover => 1, |
1914 | can_focus => 1, |
2097 | can_focus => 1, |
1915 | valign => 0, |
2098 | valign => 0, |
1916 | can_events => 1, |
2099 | can_events => 1, |
1917 | ellipsise => 0, |
2100 | ellipsise => 0, |
|
|
2101 | padding_x => 4, |
|
|
2102 | padding_y => 2, |
1918 | #text => ... |
2103 | #text => ... |
1919 | #hidden => "*", |
2104 | #hidden => "*", |
1920 | @_ |
2105 | @_ |
1921 | ) |
2106 | ) |
1922 | } |
2107 | } |
… | |
… | |
1967 | my $sym = $ev->{sym}; |
2152 | my $sym = $ev->{sym}; |
1968 | my $uni = $ev->{unicode}; |
2153 | my $uni = $ev->{unicode}; |
1969 | |
2154 | |
1970 | my $text = $self->get_text; |
2155 | my $text = $self->get_text; |
1971 | |
2156 | |
|
|
2157 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
|
|
2158 | |
1972 | if ($uni == 8) { |
2159 | if ($uni == 8) { |
1973 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2160 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1974 | } elsif ($uni == 127) { |
2161 | } elsif ($uni == 127) { |
1975 | substr $text, $self->{cursor}, 1, ""; |
2162 | substr $text, $self->{cursor}, 1, ""; |
1976 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
2163 | } elsif ($sym == DC::SDLK_LEFT) { |
1977 | --$self->{cursor} if $self->{cursor}; |
2164 | --$self->{cursor} if $self->{cursor}; |
1978 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
2165 | } elsif ($sym == DC::SDLK_RIGHT) { |
1979 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2166 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1980 | } elsif ($sym == CFPlus::SDLK_HOME) { |
2167 | } elsif ($sym == DC::SDLK_HOME) { |
1981 | # what a hack |
2168 | # what a hack |
1982 | $self->{cursor} = |
2169 | $self->{cursor} = |
1983 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2170 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
1984 | ? length $1 |
2171 | ? length $1 |
1985 | : 0; |
2172 | : 0; |
1986 | } elsif ($sym == CFPlus::SDLK_END) { |
2173 | } elsif ($sym == DC::SDLK_END) { |
1987 | # uh, again |
2174 | # uh, again |
1988 | $self->{cursor} = |
2175 | $self->{cursor} = |
1989 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2176 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
1990 | ? $self->{cursor} + length $1 |
2177 | ? $self->{cursor} + length $1 |
1991 | : length $self->{text}; |
2178 | : length $self->{text}; |
… | |
… | |
2055 | glColor_premultiply @{$self->{bg}}; |
2242 | glColor_premultiply @{$self->{bg}}; |
2056 | } |
2243 | } |
2057 | |
2244 | |
2058 | glEnable GL_BLEND; |
2245 | glEnable GL_BLEND; |
2059 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2246 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2060 | glBegin GL_QUADS; |
|
|
2061 | glVertex 0 , 0; |
|
|
2062 | glVertex 0 , $self->{h}; |
|
|
2063 | glVertex $self->{w}, $self->{h}; |
2247 | glRect 0, 0, $self->{w}, $self->{h}; |
2064 | glVertex $self->{w}, 0; |
|
|
2065 | glEnd; |
|
|
2066 | glDisable GL_BLEND; |
2248 | glDisable GL_BLEND; |
2067 | |
2249 | |
2068 | $self->SUPER::_draw; |
2250 | $self->SUPER::_draw; |
2069 | |
2251 | |
2070 | #TODO: force update every cursor change :( |
2252 | #TODO: force update every cursor change :( |
… | |
… | |
2072 | |
2254 | |
2073 | unless (exists $self->{cur_h}) { |
2255 | unless (exists $self->{cur_h}) { |
2074 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2256 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2075 | utf8::encode $text; |
2257 | utf8::encode $text; |
2076 | |
2258 | |
2077 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
2259 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
2078 | } |
2260 | } |
2079 | |
2261 | |
|
|
2262 | glColor_premultiply @{$self->{active_fg}}; |
2080 | glBegin GL_LINES; |
2263 | glBegin GL_LINES; |
2081 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
2264 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy}; |
2082 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2265 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2083 | glEnd; |
2266 | glEnd; |
|
|
2267 | |
|
|
2268 | glLineWidth 3; |
|
|
2269 | glColor @{$self->{active_outline}}; |
|
|
2270 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2271 | glLineWidth 1; |
|
|
2272 | |
|
|
2273 | } else { |
|
|
2274 | glColor @{$self->{outline}}; |
|
|
2275 | glBegin GL_LINE_STRIP; |
|
|
2276 | glVertex .5, $self->{h} * .5; |
|
|
2277 | glVertex .5, $self->{h} - 2.5; |
|
|
2278 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
|
|
2279 | glVertex $self->{w} - .5, $self->{h} * .5; |
|
|
2280 | glEnd; |
2084 | } |
2281 | } |
2085 | } |
2282 | } |
2086 | |
2283 | |
2087 | ############################################################################# |
2284 | ############################################################################# |
2088 | |
2285 | |
2089 | package CFPlus::UI::Entry; |
2286 | package DC::UI::Entry; |
2090 | |
2287 | |
2091 | our @ISA = CFPlus::UI::EntryBase::; |
2288 | our @ISA = DC::UI::EntryBase::; |
2092 | |
2289 | |
2093 | use CFPlus::OpenGL; |
2290 | use DC::OpenGL; |
2094 | |
2291 | |
2095 | sub invoke_key_down { |
2292 | sub invoke_key_down { |
2096 | my ($self, $ev) = @_; |
2293 | my ($self, $ev) = @_; |
2097 | |
2294 | |
2098 | my $sym = $ev->{sym}; |
2295 | my $sym = $ev->{sym}; |
… | |
… | |
2104 | $self->{history_pointer} = -1; |
2301 | $self->{history_pointer} = -1; |
2105 | $self->{history_saveback} = ''; |
2302 | $self->{history_saveback} = ''; |
2106 | $self->emit (activate => $txt); |
2303 | $self->emit (activate => $txt); |
2107 | $self->update; |
2304 | $self->update; |
2108 | |
2305 | |
2109 | } elsif ($sym == CFPlus::SDLK_UP) { |
2306 | } elsif ($sym == DC::SDLK_UP) { |
2110 | if ($self->{history_pointer} < 0) { |
2307 | if ($self->{history_pointer} < 0) { |
2111 | $self->{history_saveback} = $self->get_text; |
2308 | $self->{history_saveback} = $self->get_text; |
2112 | } |
2309 | } |
2113 | if (@{$self->{history} || []} > 0) { |
2310 | if (@{$self->{history} || []} > 0) { |
2114 | $self->{history_pointer}++; |
2311 | $self->{history_pointer}++; |
… | |
… | |
2116 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2313 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2117 | } |
2314 | } |
2118 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2315 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2119 | } |
2316 | } |
2120 | |
2317 | |
2121 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2318 | } elsif ($sym == DC::SDLK_DOWN) { |
2122 | $self->{history_pointer}--; |
2319 | $self->{history_pointer}--; |
2123 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2320 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2124 | |
2321 | |
2125 | if ($self->{history_pointer} >= 0) { |
2322 | if ($self->{history_pointer} >= 0) { |
2126 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2323 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
… | |
… | |
2135 | 1 |
2332 | 1 |
2136 | } |
2333 | } |
2137 | |
2334 | |
2138 | ############################################################################# |
2335 | ############################################################################# |
2139 | |
2336 | |
2140 | package CFPlus::UI::TextEdit; |
2337 | package DC::UI::TextEdit; |
2141 | |
2338 | |
2142 | our @ISA = CFPlus::UI::EntryBase::; |
2339 | our @ISA = DC::UI::EntryBase::; |
2143 | |
2340 | |
2144 | use CFPlus::OpenGL; |
2341 | use DC::OpenGL; |
|
|
2342 | |
|
|
2343 | sub new { |
|
|
2344 | my $class = shift; |
|
|
2345 | |
|
|
2346 | $class->SUPER::new ( |
|
|
2347 | padding_y => 4, |
|
|
2348 | |
|
|
2349 | @_ |
|
|
2350 | ) |
|
|
2351 | } |
2145 | |
2352 | |
2146 | sub move_cursor_ver { |
2353 | sub move_cursor_ver { |
2147 | my ($self, $dy) = @_; |
2354 | my ($self, $dy) = @_; |
2148 | |
2355 | |
2149 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
2356 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
… | |
… | |
2161 | sub invoke_key_down { |
2368 | sub invoke_key_down { |
2162 | my ($self, $ev) = @_; |
2369 | my ($self, $ev) = @_; |
2163 | |
2370 | |
2164 | my $sym = $ev->{sym}; |
2371 | my $sym = $ev->{sym}; |
2165 | |
2372 | |
2166 | if ($sym == CFPlus::SDLK_UP) { |
2373 | if ($sym == DC::SDLK_UP) { |
2167 | $self->move_cursor_ver (-1); |
2374 | $self->move_cursor_ver (-1); |
2168 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2375 | } elsif ($sym == DC::SDLK_DOWN) { |
2169 | $self->move_cursor_ver (+1); |
2376 | $self->move_cursor_ver (+1); |
2170 | } else { |
2377 | } else { |
2171 | return $self->SUPER::invoke_key_down ($ev) |
2378 | return $self->SUPER::invoke_key_down ($ev) |
2172 | } |
2379 | } |
2173 | |
2380 | |
2174 | 1 |
2381 | 1 |
2175 | } |
2382 | } |
2176 | |
2383 | |
2177 | ############################################################################# |
2384 | ############################################################################# |
2178 | |
2385 | |
2179 | package CFPlus::UI::Button; |
2386 | package DC::UI::ButtonBin; |
2180 | |
2387 | |
2181 | our @ISA = CFPlus::UI::Label::; |
2388 | our @ISA = DC::UI::Bin::; |
2182 | |
2389 | |
2183 | use CFPlus::OpenGL; |
2390 | use DC::OpenGL; |
2184 | |
2391 | |
2185 | my @tex = |
2392 | my @tex = |
2186 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2393 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2187 | qw(b1_button_active.png); |
2394 | qw(b1_button_inactive.png b1_button_active.png); |
2188 | |
2395 | |
2189 | sub new { |
2396 | sub new { |
2190 | my $class = shift; |
2397 | my $class = shift; |
2191 | |
2398 | |
2192 | $class->SUPER::new ( |
2399 | $class->SUPER::new ( |
2193 | padding_x => 4, |
|
|
2194 | padding_y => 4, |
|
|
2195 | fg => [1, 1, 1], |
|
|
2196 | active_fg => [0, 0, 1], |
|
|
2197 | can_hover => 1, |
2400 | can_hover => 1, |
2198 | align => 0, |
2401 | align => 0, |
2199 | valign => 0, |
2402 | valign => 0, |
2200 | can_events => 1, |
2403 | can_events => 1, |
2201 | @_ |
2404 | @_ |
… | |
… | |
2213 | } |
2416 | } |
2214 | |
2417 | |
2215 | sub _draw { |
2418 | sub _draw { |
2216 | my ($self) = @_; |
2419 | my ($self) = @_; |
2217 | |
2420 | |
2218 | local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg}; |
|
|
2219 | |
|
|
2220 | glEnable GL_TEXTURE_2D; |
2421 | glEnable GL_TEXTURE_2D; |
2221 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2422 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2222 | glColor 0, 0, 0, 1; |
2423 | glColor 0, 0, 0, 1; |
2223 | |
2424 | |
|
|
2425 | my $tex = $tex[$GRAB == $self]; |
2224 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2426 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2225 | |
2427 | |
2226 | glDisable GL_TEXTURE_2D; |
2428 | glDisable GL_TEXTURE_2D; |
2227 | |
2429 | |
2228 | $self->SUPER::_draw; |
2430 | $self->SUPER::_draw; |
2229 | } |
2431 | } |
2230 | |
2432 | |
2231 | ############################################################################# |
2433 | ############################################################################# |
2232 | |
2434 | |
2233 | package CFPlus::UI::CheckBox; |
2435 | package DC::UI::Button; |
2234 | |
2436 | |
2235 | our @ISA = CFPlus::UI::DrawBG::; |
2437 | our @ISA = DC::UI::Label::; |
|
|
2438 | |
|
|
2439 | use DC::OpenGL; |
2236 | |
2440 | |
2237 | my @tex = |
2441 | my @tex = |
2238 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2442 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
|
|
2443 | qw(b1_button_inactive.png b1_button_active.png); |
|
|
2444 | |
|
|
2445 | sub new { |
|
|
2446 | my $class = shift; |
|
|
2447 | |
|
|
2448 | $class->SUPER::new ( |
|
|
2449 | padding_x => 8, |
|
|
2450 | padding_y => 4, |
|
|
2451 | fg => [1.0, 1.0, 1.0], |
|
|
2452 | active_fg => [0.8, 0.8, 0.8], |
|
|
2453 | can_hover => 1, |
|
|
2454 | align => 0, |
|
|
2455 | valign => 0, |
|
|
2456 | can_events => 1, |
|
|
2457 | @_ |
|
|
2458 | ) |
|
|
2459 | } |
|
|
2460 | |
|
|
2461 | sub invoke_button_up { |
|
|
2462 | my ($self, $ev, $x, $y) = @_; |
|
|
2463 | |
|
|
2464 | $self->emit ("activate") |
|
|
2465 | if $x >= 0 && $x < $self->{w} |
|
|
2466 | && $y >= 0 && $y < $self->{h}; |
|
|
2467 | |
|
|
2468 | 1 |
|
|
2469 | } |
|
|
2470 | |
|
|
2471 | sub _draw { |
|
|
2472 | my ($self) = @_; |
|
|
2473 | |
|
|
2474 | local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg}; |
|
|
2475 | |
|
|
2476 | glEnable GL_TEXTURE_2D; |
|
|
2477 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2478 | glColor 0, 0, 0, 1; |
|
|
2479 | |
|
|
2480 | my $tex = $tex[$GRAB == $self]; |
|
|
2481 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
|
|
2482 | |
|
|
2483 | glDisable GL_TEXTURE_2D; |
|
|
2484 | |
|
|
2485 | $self->SUPER::_draw; |
|
|
2486 | } |
|
|
2487 | |
|
|
2488 | ############################################################################# |
|
|
2489 | |
|
|
2490 | package DC::UI::CheckBox; |
|
|
2491 | |
|
|
2492 | our @ISA = DC::UI::DrawBG::; |
|
|
2493 | |
|
|
2494 | my @tex = |
|
|
2495 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2239 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2496 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2240 | |
2497 | |
2241 | use CFPlus::OpenGL; |
2498 | use DC::OpenGL; |
2242 | |
2499 | |
2243 | sub new { |
2500 | sub new { |
2244 | my $class = shift; |
2501 | my $class = shift; |
2245 | |
2502 | |
2246 | $class->SUPER::new ( |
2503 | $class->SUPER::new ( |
… | |
… | |
2286 | sub _draw { |
2543 | sub _draw { |
2287 | my ($self) = @_; |
2544 | my ($self) = @_; |
2288 | |
2545 | |
2289 | $self->SUPER::_draw; |
2546 | $self->SUPER::_draw; |
2290 | |
2547 | |
2291 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
2548 | glTranslate $self->{padding_x}, $self->{padding_y}, 0; |
2292 | |
2549 | |
2293 | my ($w, $h) = @$self{qw(w h)}; |
2550 | my ($w, $h) = @$self{qw(w h)}; |
2294 | |
2551 | |
2295 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2552 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2296 | |
2553 | |
… | |
… | |
2303 | glDisable GL_TEXTURE_2D; |
2560 | glDisable GL_TEXTURE_2D; |
2304 | } |
2561 | } |
2305 | |
2562 | |
2306 | ############################################################################# |
2563 | ############################################################################# |
2307 | |
2564 | |
2308 | package CFPlus::UI::Image; |
2565 | package DC::UI::Image; |
2309 | |
2566 | |
2310 | our @ISA = CFPlus::UI::Base::; |
2567 | our @ISA = DC::UI::Base::; |
2311 | |
2568 | |
2312 | use CFPlus::OpenGL; |
2569 | use DC::OpenGL; |
2313 | |
2570 | |
2314 | our %texture_cache; |
2571 | our %texture_cache; |
2315 | |
2572 | |
2316 | sub new { |
2573 | sub new { |
2317 | my $class = shift; |
2574 | my $class = shift; |
2318 | |
2575 | |
2319 | my $self = $class->SUPER::new ( |
2576 | my $self = $class->SUPER::new ( |
2320 | can_events => 0, |
2577 | can_events => 0, |
|
|
2578 | scale => 1, |
2321 | @_, |
2579 | @_, |
2322 | ); |
2580 | ); |
2323 | |
2581 | |
2324 | $self->{path} || $self->{tex} |
2582 | $self->{path} || $self->{tex} |
2325 | or Carp::croak "'path' or 'tex' attributes required"; |
2583 | or Carp::croak "'path' or 'tex' attributes required"; |
2326 | |
2584 | |
2327 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2585 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2328 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2586 | new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; |
2329 | |
2587 | |
2330 | CFPlus::weaken $texture_cache{$self->{path}}; |
2588 | DC::weaken $texture_cache{$self->{path}}; |
2331 | |
2589 | |
2332 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2590 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2333 | |
2591 | |
2334 | $self |
2592 | $self |
2335 | } |
2593 | } |
2336 | |
2594 | |
2337 | sub STORABLE_freeze { |
2595 | sub STORABLE_freeze { |
2338 | my ($self, $cloning) = @_; |
2596 | my ($self, $cloning) = @_; |
2339 | |
2597 | |
2340 | $self->{path} |
2598 | $self->{path} |
2341 | or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; |
2599 | or die "cannot serialise DC::UI::Image on non-loadable images\n"; |
2342 | |
2600 | |
2343 | $self->{path} |
2601 | $self->{path} |
2344 | } |
2602 | } |
2345 | |
2603 | |
2346 | sub STORABLE_attach { |
2604 | sub STORABLE_attach { |
… | |
… | |
2350 | } |
2608 | } |
2351 | |
2609 | |
2352 | sub size_request { |
2610 | sub size_request { |
2353 | my ($self) = @_; |
2611 | my ($self) = @_; |
2354 | |
2612 | |
2355 | ($self->{tex}{w}, $self->{tex}{h}) |
2613 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2356 | } |
2614 | } |
2357 | |
2615 | |
2358 | sub _draw { |
2616 | sub _draw { |
2359 | my ($self) = @_; |
2617 | my ($self) = @_; |
2360 | |
2618 | |
… | |
… | |
2370 | } |
2628 | } |
2371 | |
2629 | |
2372 | glEnable GL_TEXTURE_2D; |
2630 | glEnable GL_TEXTURE_2D; |
2373 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2631 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2374 | |
2632 | |
2375 | $tex->draw_quad (0, 0, $w, $h); |
2633 | $tex->draw_quad_alpha (0, 0, $w, $h); |
2376 | |
2634 | |
2377 | glDisable GL_TEXTURE_2D; |
2635 | glDisable GL_TEXTURE_2D; |
2378 | } |
2636 | } |
2379 | |
2637 | |
2380 | ############################################################################# |
2638 | ############################################################################# |
2381 | |
2639 | |
2382 | package CFPlus::UI::ImageButton; |
2640 | package DC::UI::ImageButton; |
2383 | |
2641 | |
2384 | our @ISA = CFPlus::UI::Image::; |
2642 | our @ISA = DC::UI::Image::; |
2385 | |
2643 | |
2386 | use CFPlus::OpenGL; |
2644 | use DC::OpenGL; |
2387 | |
2645 | |
2388 | my %textures; |
2646 | my %textures; |
2389 | |
2647 | |
2390 | sub new { |
2648 | sub new { |
2391 | my $class = shift; |
2649 | my $class = shift; |
… | |
… | |
2401 | can_events => 1, |
2659 | can_events => 1, |
2402 | @_ |
2660 | @_ |
2403 | ); |
2661 | ); |
2404 | } |
2662 | } |
2405 | |
2663 | |
|
|
2664 | sub invoke_button_down { |
|
|
2665 | my ($self, $ev, $x, $y) = @_; |
|
|
2666 | |
|
|
2667 | 1 |
|
|
2668 | } |
|
|
2669 | |
2406 | sub invoke_button_up { |
2670 | sub invoke_button_up { |
2407 | my ($self, $ev, $x, $y) = @_; |
2671 | my ($self, $ev, $x, $y) = @_; |
2408 | |
2672 | |
2409 | $self->emit ("activate") |
2673 | $self->emit ("activate") |
2410 | if $x >= 0 && $x < $self->{w} |
2674 | if $x >= 0 && $x < $self->{w} |
… | |
… | |
2413 | 1 |
2677 | 1 |
2414 | } |
2678 | } |
2415 | |
2679 | |
2416 | ############################################################################# |
2680 | ############################################################################# |
2417 | |
2681 | |
2418 | package CFPlus::UI::VGauge; |
2682 | package DC::UI::VGauge; |
2419 | |
2683 | |
2420 | our @ISA = CFPlus::UI::Base::; |
2684 | our @ISA = DC::UI::Base::; |
2421 | |
2685 | |
2422 | use List::Util qw(min max); |
2686 | use List::Util qw(min max); |
2423 | |
2687 | |
2424 | use CFPlus::OpenGL; |
2688 | use DC::OpenGL; |
2425 | |
2689 | |
2426 | my %tex = ( |
2690 | my %tex = ( |
2427 | food => [ |
2691 | food => [ |
2428 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2692 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2429 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2693 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2430 | ], |
2694 | ], |
2431 | grace => [ |
2695 | grace => [ |
2432 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2696 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2433 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2697 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2434 | ], |
2698 | ], |
2435 | hp => [ |
2699 | hp => [ |
2436 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2700 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2437 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2701 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2438 | ], |
2702 | ], |
2439 | mana => [ |
2703 | mana => [ |
2440 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2704 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2441 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2705 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2442 | ], |
2706 | ], |
2443 | ); |
2707 | ); |
2444 | |
2708 | |
2445 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
2709 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
… | |
… | |
2548 | glDisable GL_TEXTURE_2D; |
2812 | glDisable GL_TEXTURE_2D; |
2549 | } |
2813 | } |
2550 | |
2814 | |
2551 | ############################################################################# |
2815 | ############################################################################# |
2552 | |
2816 | |
|
|
2817 | package DC::UI::Progress; |
|
|
2818 | |
|
|
2819 | our @ISA = DC::UI::Label::; |
|
|
2820 | |
|
|
2821 | use DC::OpenGL; |
|
|
2822 | |
|
|
2823 | sub new { |
|
|
2824 | my ($class, %arg) = @_; |
|
|
2825 | |
|
|
2826 | my $self = $class->SUPER::new ( |
|
|
2827 | fg => [1, 1, 1], |
|
|
2828 | bg => [0, 0, 1, 0.2], |
|
|
2829 | bar => [0.7, 0.5, 0.1, 0.8], |
|
|
2830 | outline => [0.4, 0.3, 0], |
|
|
2831 | fontsize => 0.9, |
|
|
2832 | valign => 0, |
|
|
2833 | align => 0, |
|
|
2834 | can_events => 1, |
|
|
2835 | ellipsise => 1, |
|
|
2836 | label => "%d%%", |
|
|
2837 | %arg, |
|
|
2838 | ); |
|
|
2839 | |
|
|
2840 | $self->set_value ($arg{value} || -1); |
|
|
2841 | |
|
|
2842 | $self |
|
|
2843 | } |
|
|
2844 | |
|
|
2845 | sub set_label { |
|
|
2846 | my ($self, $label) = @_; |
|
|
2847 | |
|
|
2848 | return if $self->{label} eq $label; |
|
|
2849 | $self->{label} = $label; |
|
|
2850 | |
|
|
2851 | $self->DC::UI::Progress::set_value (0 + delete $self->{value}); |
|
|
2852 | } |
|
|
2853 | |
|
|
2854 | sub set_value { |
|
|
2855 | my ($self, $value) = @_; |
|
|
2856 | |
|
|
2857 | if ($self->{value} ne $value) { |
|
|
2858 | $self->{value} = $value; |
|
|
2859 | |
|
|
2860 | if ($value < 0) { |
|
|
2861 | $self->set_text ("-"); |
|
|
2862 | } else { |
|
|
2863 | $self->set_text (sprintf $self->{label}, $value * 100); |
|
|
2864 | } |
|
|
2865 | |
|
|
2866 | $self->update; |
|
|
2867 | } |
|
|
2868 | } |
|
|
2869 | |
|
|
2870 | sub _draw { |
|
|
2871 | my ($self) = @_; |
|
|
2872 | |
|
|
2873 | glEnable GL_BLEND; |
|
|
2874 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2875 | |
|
|
2876 | if ($self->{value} >= 0) { |
|
|
2877 | my $s = int 2 + ($self->{w} - 4) * $self->{value}; |
|
|
2878 | |
|
|
2879 | glColor_premultiply @{$self->{bar}}; |
|
|
2880 | glRect 2, 2, $s, $self->{h} - 2; |
|
|
2881 | glColor_premultiply @{$self->{bg}}; |
|
|
2882 | glRect $s, 2, $self->{w} - 2, $self->{h} - 2; |
|
|
2883 | } |
|
|
2884 | |
|
|
2885 | glColor_premultiply @{$self->{outline}}; |
|
|
2886 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2887 | |
|
|
2888 | glDisable GL_BLEND; |
|
|
2889 | |
|
|
2890 | { |
|
|
2891 | local $self->{bg}; # do not draw background |
|
|
2892 | $self->SUPER::_draw; |
|
|
2893 | } |
|
|
2894 | } |
|
|
2895 | |
|
|
2896 | ############################################################################# |
|
|
2897 | |
|
|
2898 | package DC::UI::ExperienceProgress; |
|
|
2899 | |
|
|
2900 | our @ISA = DC::UI::Progress::; |
|
|
2901 | |
|
|
2902 | sub new { |
|
|
2903 | my ($class, %arg) = @_; |
|
|
2904 | |
|
|
2905 | my $self = $class->SUPER::new ( |
|
|
2906 | tooltip => sub { |
|
|
2907 | my ($self) = @_; |
|
|
2908 | |
|
|
2909 | sprintf "level %d\n%s points\n%s next level\n%s to go", |
|
|
2910 | $self->{lvl}, |
|
|
2911 | ::formsep ($self->{exp}), |
|
|
2912 | ::formsep ($self->{nxt}), |
|
|
2913 | ::formsep ($self->{nxt} - $self->{exp}), |
|
|
2914 | }, |
|
|
2915 | %arg |
|
|
2916 | ); |
|
|
2917 | |
|
|
2918 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
|
|
2919 | if $::CONN; |
|
|
2920 | |
|
|
2921 | $self |
|
|
2922 | } |
|
|
2923 | |
|
|
2924 | sub DESTROY { |
|
|
2925 | my ($self) = @_; |
|
|
2926 | |
|
|
2927 | delete $::CONN->{on_exp_update}{$self+0} |
|
|
2928 | if $::CONN; |
|
|
2929 | |
|
|
2930 | $self->SUPER::DESTROY; |
|
|
2931 | } |
|
|
2932 | |
|
|
2933 | sub set_value { |
|
|
2934 | my ($self, $lvl, $exp) = @_; |
|
|
2935 | |
|
|
2936 | $self->{lvl} = $lvl; |
|
|
2937 | $self->{exp} = $exp; |
|
|
2938 | |
|
|
2939 | my $v = -1; |
|
|
2940 | |
|
|
2941 | if ($::CONN && (my $table = $::CONN->{exp_table})) { |
|
|
2942 | my $l0 = $table->[$lvl - 1]; |
|
|
2943 | my $l1 = $table->[$lvl]; |
|
|
2944 | |
|
|
2945 | $self->{nxt} = $l1; |
|
|
2946 | |
|
|
2947 | $v = ($exp - $l0) / ($l1 - $l0); |
|
|
2948 | } |
|
|
2949 | |
|
|
2950 | $self->SUPER::set_value ($v); |
|
|
2951 | } |
|
|
2952 | |
|
|
2953 | ############################################################################# |
|
|
2954 | |
2553 | package CFPlus::UI::Gauge; |
2955 | package DC::UI::Gauge; |
2554 | |
2956 | |
2555 | our @ISA = CFPlus::UI::VBox::; |
2957 | our @ISA = DC::UI::VBox::; |
2556 | |
2958 | |
2557 | sub new { |
2959 | sub new { |
2558 | my ($class, %arg) = @_; |
2960 | my ($class, %arg) = @_; |
2559 | |
2961 | |
2560 | my $self = $class->SUPER::new ( |
2962 | my $self = $class->SUPER::new ( |
… | |
… | |
2562 | can_hover => 1, |
2964 | can_hover => 1, |
2563 | can_events => 1, |
2965 | can_events => 1, |
2564 | %arg, |
2966 | %arg, |
2565 | ); |
2967 | ); |
2566 | |
2968 | |
2567 | $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); |
2969 | $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999"); |
2568 | $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2970 | $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2569 | $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); |
2971 | $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999"); |
2570 | |
2972 | |
2571 | $self |
2973 | $self |
2572 | } |
2974 | } |
2573 | |
2975 | |
2574 | sub set_fontsize { |
2976 | sub set_fontsize { |
… | |
… | |
2595 | $self->{value}->set_text ($val); |
2997 | $self->{value}->set_text ($val); |
2596 | } |
2998 | } |
2597 | |
2999 | |
2598 | ############################################################################# |
3000 | ############################################################################# |
2599 | |
3001 | |
2600 | package CFPlus::UI::Slider; |
3002 | package DC::UI::Slider; |
2601 | |
3003 | |
2602 | use strict; |
3004 | use strict; |
2603 | |
3005 | |
2604 | use CFPlus::OpenGL; |
3006 | use DC::OpenGL; |
2605 | |
3007 | |
2606 | our @ISA = CFPlus::UI::DrawBG::; |
3008 | our @ISA = DC::UI::DrawBG::; |
2607 | |
3009 | |
2608 | my @tex = |
3010 | my @tex = |
2609 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } |
3011 | map { new_from_file DC::Texture DC::find_rcfile $_ } |
2610 | qw(s1_slider.png s1_slider_bg.png); |
3012 | qw(s1_slider.png s1_slider_bg.png); |
2611 | |
3013 | |
2612 | sub new { |
3014 | sub new { |
2613 | my $class = shift; |
3015 | my $class = shift; |
2614 | |
3016 | |
… | |
… | |
2682 | |
3084 | |
2683 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
3085 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
2684 | |
3086 | |
2685 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
3087 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2686 | |
3088 | |
2687 | $self->invoke_mouse_motion ($ev, $x, $y) |
3089 | $self->invoke_mouse_motion ($ev, $x, $y); |
|
|
3090 | |
|
|
3091 | 1 |
2688 | } |
3092 | } |
2689 | |
3093 | |
2690 | sub invoke_mouse_motion { |
3094 | sub invoke_mouse_motion { |
2691 | my ($self, $ev, $x, $y) = @_; |
3095 | my ($self, $ev, $x, $y) = @_; |
2692 | |
3096 | |
… | |
… | |
2708 | sub invoke_mouse_wheel { |
3112 | sub invoke_mouse_wheel { |
2709 | my ($self, $ev) = @_; |
3113 | my ($self, $ev) = @_; |
2710 | |
3114 | |
2711 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
3115 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2712 | |
3116 | |
|
|
3117 | my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2; |
|
|
3118 | |
2713 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); |
3119 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2714 | |
3120 | |
2715 | ! ! $delta |
3121 | 1 |
2716 | } |
3122 | } |
2717 | |
3123 | |
2718 | sub update { |
3124 | sub update { |
2719 | my ($self) = @_; |
3125 | my ($self) = @_; |
2720 | |
3126 | |
… | |
… | |
2769 | glDisable GL_TEXTURE_2D; |
3175 | glDisable GL_TEXTURE_2D; |
2770 | } |
3176 | } |
2771 | |
3177 | |
2772 | ############################################################################# |
3178 | ############################################################################# |
2773 | |
3179 | |
2774 | package CFPlus::UI::ValSlider; |
3180 | package DC::UI::ValSlider; |
2775 | |
3181 | |
2776 | our @ISA = CFPlus::UI::HBox::; |
3182 | our @ISA = DC::UI::HBox::; |
2777 | |
3183 | |
2778 | sub new { |
3184 | sub new { |
2779 | my ($class, %arg) = @_; |
3185 | my ($class, %arg) = @_; |
2780 | |
3186 | |
2781 | my $range = delete $arg{range}; |
3187 | my $range = delete $arg{range}; |
2782 | |
3188 | |
2783 | my $self = $class->SUPER::new ( |
3189 | my $self = $class->SUPER::new ( |
2784 | slider => (new CFPlus::UI::Slider expand => 1, range => $range), |
3190 | slider => (new DC::UI::Slider expand => 1, range => $range), |
2785 | entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), |
3191 | entry => (new DC::UI::Label text => "", template => delete $arg{template}), |
2786 | to_value => sub { shift }, |
3192 | to_value => sub { shift }, |
2787 | from_value => sub { shift }, |
3193 | from_value => sub { shift }, |
2788 | %arg, |
3194 | %arg, |
2789 | ); |
3195 | ); |
2790 | |
3196 | |
… | |
… | |
2810 | sub set_range { shift->{slider}->set_range (@_) } |
3216 | sub set_range { shift->{slider}->set_range (@_) } |
2811 | sub set_value { shift->{slider}->set_value (@_) } |
3217 | sub set_value { shift->{slider}->set_value (@_) } |
2812 | |
3218 | |
2813 | ############################################################################# |
3219 | ############################################################################# |
2814 | |
3220 | |
2815 | package CFPlus::UI::TextScroller; |
3221 | package DC::UI::TextScroller; |
2816 | |
3222 | |
2817 | our @ISA = CFPlus::UI::HBox::; |
3223 | our @ISA = DC::UI::HBox::; |
2818 | |
3224 | |
2819 | use CFPlus::OpenGL; |
3225 | use DC::OpenGL; |
2820 | |
3226 | |
2821 | sub new { |
3227 | sub new { |
2822 | my $class = shift; |
3228 | my $class = shift; |
2823 | |
3229 | |
2824 | my $self = $class->SUPER::new ( |
3230 | my $self = $class->SUPER::new ( |
… | |
… | |
2826 | can_events => 1, |
3232 | can_events => 1, |
2827 | indent => 0, |
3233 | indent => 0, |
2828 | #font => default_font |
3234 | #font => default_font |
2829 | @_, |
3235 | @_, |
2830 | |
3236 | |
2831 | layout => (new CFPlus::Layout), |
3237 | layout => (new DC::Layout), |
2832 | par => [], |
3238 | par => [], |
|
|
3239 | max_par => 0, |
2833 | height => 0, |
3240 | height => 0, |
2834 | children => [ |
3241 | children => [ |
2835 | (new CFPlus::UI::Empty expand => 1), |
3242 | (new DC::UI::Empty expand => 1), |
2836 | (new CFPlus::UI::Slider vertical => 1), |
3243 | (new DC::UI::Slider vertical => 1), |
2837 | ], |
3244 | ], |
2838 | ); |
3245 | ); |
2839 | |
3246 | |
2840 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3247 | $self->{children}[1]->connect (changed => sub { $self->update }); |
2841 | |
3248 | |
… | |
… | |
2850 | } |
3257 | } |
2851 | |
3258 | |
2852 | sub size_request { |
3259 | sub size_request { |
2853 | my ($self) = @_; |
3260 | my ($self) = @_; |
2854 | |
3261 | |
2855 | my ($empty, $slider) = @{ $self->{children} }; |
3262 | my ($empty, $slider) = $self->visible_children; |
2856 | |
3263 | |
2857 | local $self->{children} = [$empty, $slider]; |
3264 | local $self->{children} = [$empty, $slider]; |
2858 | $self->SUPER::size_request |
3265 | $self->SUPER::size_request |
2859 | } |
3266 | } |
2860 | |
3267 | |
… | |
… | |
2962 | |
3369 | |
2963 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
3370 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
2964 | push @{$self->{par}}, $para; |
3371 | push @{$self->{par}}, $para; |
2965 | } |
3372 | } |
2966 | |
3373 | |
|
|
3374 | if (my $max = $self->{max_par}) { |
|
|
3375 | shift @{$self->{par}} while @{$self->{par}} > $max; |
|
|
3376 | } |
|
|
3377 | |
2967 | $self->{need_reflow}++; |
3378 | $self->{need_reflow}++; |
2968 | $self->update; |
3379 | $self->update; |
2969 | } |
3380 | } |
2970 | |
3381 | |
2971 | sub scroll_to_bottom { |
3382 | sub scroll_to_bottom { |
… | |
… | |
3020 | $ROOT->on_post_alloc ($self => sub { |
3431 | $ROOT->on_post_alloc ($self => sub { |
3021 | $self->force_uptodate; |
3432 | $self->force_uptodate; |
3022 | |
3433 | |
3023 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3434 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3024 | |
3435 | |
3025 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
3436 | $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub { |
3026 | glClearColor 0, 0, 0, 0; |
3437 | glClearColor 0, 0, 0, 0; |
3027 | glClear GL_COLOR_BUFFER_BIT; |
3438 | glClear GL_COLOR_BUFFER_BIT; |
3028 | |
3439 | |
3029 | package CFPlus::UI::Base; |
3440 | package DC::UI::Base; |
3030 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3441 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3031 | (0, 0, $self->{w}, $self->{h}); |
3442 | (0, 0, $self->{w}, $self->{h}); |
3032 | |
3443 | |
3033 | my $top = int $self->{children}[1]{range}[0]; |
3444 | my $top = int $self->{children}[1]{range}[0]; |
3034 | |
3445 | |
… | |
… | |
3045 | |
3456 | |
3046 | if ($y0 < $y + $h && $y < $y1) { |
3457 | if ($y0 < $y + $h && $y < $y1) { |
3047 | my $layout = $self->get_layout ($para); |
3458 | my $layout = $self->get_layout ($para); |
3048 | |
3459 | |
3049 | $layout->render ($para->{indent}, $y - $y0); |
3460 | $layout->render ($para->{indent}, $y - $y0); |
|
|
3461 | $layout->draw; |
3050 | |
3462 | |
3051 | if (my @w = @{ $para->{widget} }) { |
3463 | if (my @w = @{ $para->{widget} }) { |
3052 | my @s = $layout->get_shapes; |
3464 | my @s = $layout->get_shapes; |
3053 | |
3465 | |
3054 | for (@w) { |
3466 | for (@w) { |
… | |
… | |
3092 | $self->{children}[1]->draw; |
3504 | $self->{children}[1]->draw; |
3093 | } |
3505 | } |
3094 | |
3506 | |
3095 | ############################################################################# |
3507 | ############################################################################# |
3096 | |
3508 | |
3097 | package CFPlus::UI::Animator; |
3509 | package DC::UI::Animator; |
3098 | |
3510 | |
3099 | use CFPlus::OpenGL; |
3511 | use DC::OpenGL; |
3100 | |
3512 | |
3101 | our @ISA = CFPlus::UI::Bin::; |
3513 | our @ISA = DC::UI::Bin::; |
3102 | |
3514 | |
3103 | sub moveto { |
3515 | sub moveto { |
3104 | my ($self, $x, $y) = @_; |
3516 | my ($self, $x, $y) = @_; |
3105 | |
3517 | |
3106 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
3518 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
3134 | glPopMatrix; |
3546 | glPopMatrix; |
3135 | } |
3547 | } |
3136 | |
3548 | |
3137 | ############################################################################# |
3549 | ############################################################################# |
3138 | |
3550 | |
3139 | package CFPlus::UI::Flopper; |
3551 | package DC::UI::Flopper; |
3140 | |
3552 | |
3141 | our @ISA = CFPlus::UI::Button::; |
3553 | our @ISA = DC::UI::Button::; |
3142 | |
3554 | |
3143 | sub new { |
3555 | sub new { |
3144 | my $class = shift; |
3556 | my $class = shift; |
3145 | |
3557 | |
3146 | my $self = $class->SUPER::new ( |
3558 | my $self = $class->SUPER::new ( |
… | |
… | |
3158 | $self->{other}->toggle_visibility; |
3570 | $self->{other}->toggle_visibility; |
3159 | } |
3571 | } |
3160 | |
3572 | |
3161 | ############################################################################# |
3573 | ############################################################################# |
3162 | |
3574 | |
3163 | package CFPlus::UI::Tooltip; |
3575 | package DC::UI::Tooltip; |
3164 | |
3576 | |
3165 | our @ISA = CFPlus::UI::Bin::; |
3577 | our @ISA = DC::UI::Bin::; |
3166 | |
3578 | |
3167 | use CFPlus::OpenGL; |
3579 | use DC::OpenGL; |
3168 | |
3580 | |
3169 | sub new { |
3581 | sub new { |
3170 | my $class = shift; |
3582 | my $class = shift; |
3171 | |
3583 | |
3172 | $class->SUPER::new ( |
3584 | $class->SUPER::new ( |
… | |
… | |
3176 | } |
3588 | } |
3177 | |
3589 | |
3178 | sub set_tooltip_from { |
3590 | sub set_tooltip_from { |
3179 | my ($self, $widget) = @_; |
3591 | my ($self, $widget) = @_; |
3180 | |
3592 | |
3181 | $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1 |
|
|
3182 | if $widget->{tooltip} =~ /^#(.*)$/; |
|
|
3183 | |
|
|
3184 | my $tooltip = $widget->{tooltip}; |
3593 | my $tip = $widget->{tooltip}; |
|
|
3594 | $tip = $tip->($widget) if "CODE" eq ref $tip; |
|
|
3595 | |
|
|
3596 | $tip = DC::Pod::section_label tooltip => $1 |
|
|
3597 | if $tip =~ /^#(.*)$/; |
3185 | |
3598 | |
3186 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3599 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3187 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
3600 | $tip .= "\n\n" . (ref $widget) . "\n" |
3188 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3601 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3189 | . "req $widget->{req_w} $widget->{req_h}\n" |
3602 | . "req $widget->{req_w} $widget->{req_h}\n" |
3190 | . "visible $widget->{visible}"; |
3603 | . "visible $widget->{visible}"; |
3191 | } |
3604 | } |
3192 | |
3605 | |
3193 | $tooltip =~ s/^\n+//; |
3606 | $tip =~ s/^\n+//; |
3194 | $tooltip =~ s/\n+$//; |
3607 | $tip =~ s/\n+$//; |
3195 | |
3608 | |
3196 | $self->add (new CFPlus::UI::Label |
3609 | $self->add (new DC::UI::Label |
3197 | markup => $tooltip, |
3610 | markup => $tip, |
3198 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3611 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3199 | fontsize => 0.8, |
3612 | fontsize => 0.8, |
3200 | style => 1, # FLAG_INVERSE |
3613 | style => 1, # FLAG_INVERSE |
3201 | ellipsise => 0, |
3614 | ellipsise => 0, |
3202 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3615 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
… | |
… | |
3240 | } |
3653 | } |
3241 | |
3654 | |
3242 | sub _draw { |
3655 | sub _draw { |
3243 | my ($self) = @_; |
3656 | my ($self) = @_; |
3244 | |
3657 | |
3245 | glTranslate 0.375, 0.375; |
|
|
3246 | |
|
|
3247 | my ($w, $h) = @$self{qw(w h)}; |
3658 | my ($w, $h) = @$self{qw(w h)}; |
3248 | |
3659 | |
3249 | glColor 1, 0.8, 0.4; |
3660 | glColor 1, 0.8, 0.4; |
3250 | glBegin GL_QUADS; |
3661 | glRect 0, 0, $w, $h; |
3251 | glVertex 0 , 0; |
|
|
3252 | glVertex 0 , $h; |
|
|
3253 | glVertex $w, $h; |
|
|
3254 | glVertex $w, 0; |
|
|
3255 | glEnd; |
|
|
3256 | |
3662 | |
3257 | glColor 0, 0, 0; |
3663 | glColor 0, 0, 0; |
3258 | glBegin GL_LINE_LOOP; |
3664 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3259 | glVertex 0 , 0; |
|
|
3260 | glVertex 0 , $h; |
|
|
3261 | glVertex $w, $h; |
|
|
3262 | glVertex $w, 0; |
|
|
3263 | glEnd; |
|
|
3264 | |
3665 | |
3265 | glTranslate 2 - 0.375, 2 - 0.375; |
3666 | glTranslate 2, 2; |
3266 | |
3667 | |
3267 | $self->SUPER::_draw; |
3668 | $self->SUPER::_draw; |
3268 | } |
3669 | } |
3269 | |
3670 | |
3270 | ############################################################################# |
3671 | ############################################################################# |
3271 | |
3672 | |
3272 | package CFPlus::UI::Face; |
3673 | package DC::UI::Face; |
3273 | |
3674 | |
3274 | our @ISA = CFPlus::UI::DrawBG::; |
3675 | our @ISA = DC::UI::DrawBG::; |
3275 | |
3676 | |
3276 | use CFPlus::OpenGL; |
3677 | use DC::OpenGL; |
3277 | |
3678 | |
3278 | sub new { |
3679 | sub new { |
3279 | my $class = shift; |
3680 | my $class = shift; |
3280 | |
3681 | |
3281 | my $self = $class->SUPER::new ( |
3682 | my $self = $class->SUPER::new ( |
|
|
3683 | size_w => 32, |
|
|
3684 | size_h => 8, |
3282 | aspect => 1, |
3685 | aspect => 1, |
3283 | can_events => 0, |
3686 | can_events => 0, |
3284 | @_, |
3687 | @_, |
3285 | ); |
3688 | ); |
3286 | |
3689 | |
3287 | if ($self->{anim} && $self->{animspeed}) { |
3690 | if ($self->{anim} && $self->{animspeed}) { |
3288 | CFPlus::weaken (my $widget = $self); |
3691 | DC::weaken (my $widget = $self); |
3289 | |
3692 | |
3290 | $self->{timer} = Event->timer ( |
3693 | $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; |
3291 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3694 | $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { |
3292 | hard => 1, |
3695 | return unless $::CONN; |
3293 | interval => $self->{animspeed}, |
3696 | |
3294 | cb => sub { |
3697 | my $w = $widget |
|
|
3698 | or return; |
|
|
3699 | |
3295 | ++$widget->{frame}; |
3700 | ++$w->{frame}; |
|
|
3701 | $w->update_face; |
|
|
3702 | |
|
|
3703 | # somehow, $widget can go away |
3296 | $widget->update; |
3704 | $w->update; |
3297 | }, |
3705 | $w->update_timer; |
3298 | ); |
3706 | }; |
|
|
3707 | |
|
|
3708 | $self->update_face; |
|
|
3709 | $self->update_timer; |
3299 | } |
3710 | } |
3300 | |
3711 | |
3301 | $self |
3712 | $self |
3302 | } |
3713 | } |
3303 | |
3714 | |
|
|
3715 | sub update_timer { |
|
|
3716 | my ($self) = @_; |
|
|
3717 | |
|
|
3718 | return unless $self->{timer}; |
|
|
3719 | |
|
|
3720 | if ($self->{visible}) { |
|
|
3721 | $self->{timer}->start; |
|
|
3722 | } else { |
|
|
3723 | $self->{timer}->stop; |
|
|
3724 | } |
|
|
3725 | } |
|
|
3726 | |
|
|
3727 | sub update_face { |
|
|
3728 | my ($self) = @_; |
|
|
3729 | |
|
|
3730 | if ($::CONN) { |
|
|
3731 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
|
|
3732 | if ($anim && @$anim) { |
|
|
3733 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
|
|
3734 | delete $self->{face_change_cb}; |
|
|
3735 | |
|
|
3736 | if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) { |
|
|
3737 | unless ($tex->{name} || $tex->{loading}) { |
|
|
3738 | $tex->upload (sub { $self->reconfigure }); |
|
|
3739 | } |
|
|
3740 | } |
|
|
3741 | } |
|
|
3742 | } |
|
|
3743 | } |
|
|
3744 | } |
|
|
3745 | |
3304 | sub size_request { |
3746 | sub size_request { |
3305 | (32, 8) |
3747 | my ($self) = @_; |
|
|
3748 | |
|
|
3749 | if ($::CONN) { |
|
|
3750 | if (my $faceid = $::CONN->{face}[$self->{face}]{id}) { |
|
|
3751 | if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) { |
|
|
3752 | if ($tex->{name}) { |
|
|
3753 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
|
|
3754 | } elsif (!$tex->{loading}) { |
|
|
3755 | $tex->upload (sub { $self->reconfigure }); |
|
|
3756 | } |
|
|
3757 | } |
|
|
3758 | |
|
|
3759 | $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure }); |
|
|
3760 | } |
|
|
3761 | } |
|
|
3762 | |
|
|
3763 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3306 | } |
3764 | } |
3307 | |
3765 | |
3308 | sub update { |
3766 | sub update { |
3309 | my ($self) = @_; |
3767 | my ($self) = @_; |
3310 | |
3768 | |
3311 | return unless $self->{visible}; |
3769 | return unless $self->{visible}; |
3312 | |
3770 | |
3313 | $self->SUPER::update; |
3771 | $self->SUPER::update; |
3314 | } |
3772 | } |
3315 | |
3773 | |
|
|
3774 | sub invoke_visibility_change { |
|
|
3775 | my ($self) = @_; |
|
|
3776 | |
|
|
3777 | $self->update_timer; |
|
|
3778 | |
|
|
3779 | 0 |
|
|
3780 | } |
|
|
3781 | |
3316 | sub _draw { |
3782 | sub _draw { |
3317 | my ($self) = @_; |
3783 | my ($self) = @_; |
3318 | |
3784 | |
3319 | return unless $::CONN; |
|
|
3320 | |
|
|
3321 | $self->SUPER::_draw; |
3785 | $self->SUPER::_draw; |
3322 | |
3786 | |
3323 | my $face; |
3787 | if (my $tex = $self->{tex}) { |
3324 | |
|
|
3325 | if ($self->{frame}) { |
|
|
3326 | my $anim = $::CONN->{anim}[$self->{anim}]; |
|
|
3327 | |
|
|
3328 | $face = $anim->[ $self->{frame} % @$anim ] |
|
|
3329 | if $anim && @$anim; |
|
|
3330 | } |
|
|
3331 | |
|
|
3332 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; |
|
|
3333 | |
|
|
3334 | if ($tex) { |
|
|
3335 | glEnable GL_TEXTURE_2D; |
3788 | glEnable GL_TEXTURE_2D; |
3336 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3789 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3337 | glColor 0, 0, 0, 1; |
3790 | glColor 0, 0, 0, 1; |
3338 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3791 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3339 | glDisable GL_TEXTURE_2D; |
3792 | glDisable GL_TEXTURE_2D; |
… | |
… | |
3341 | } |
3794 | } |
3342 | |
3795 | |
3343 | sub destroy { |
3796 | sub destroy { |
3344 | my ($self) = @_; |
3797 | my ($self) = @_; |
3345 | |
3798 | |
3346 | $self->{timer}->cancel |
3799 | (delete $self->{timer})->cancel |
3347 | if $self->{timer}; |
3800 | if $self->{timer}; |
3348 | |
3801 | |
3349 | $self->SUPER::destroy; |
3802 | $self->SUPER::destroy; |
3350 | } |
3803 | } |
3351 | |
3804 | |
3352 | ############################################################################# |
3805 | ############################################################################# |
3353 | |
3806 | |
3354 | package CFPlus::UI::Buttonbar; |
3807 | package DC::UI::Buttonbar; |
3355 | |
3808 | |
3356 | our @ISA = CFPlus::UI::HBox::; |
3809 | our @ISA = DC::UI::HBox::; |
3357 | |
3810 | |
3358 | # TODO: should actually wrap buttons and other goodies. |
3811 | # TODO: should actually wrap buttons and other goodies. |
3359 | |
3812 | |
3360 | ############################################################################# |
3813 | ############################################################################# |
3361 | |
3814 | |
3362 | package CFPlus::UI::Menu; |
3815 | package DC::UI::Menu; |
3363 | |
3816 | |
3364 | our @ISA = CFPlus::UI::Toplevel::; |
3817 | our @ISA = DC::UI::Toplevel::; |
3365 | |
3818 | |
3366 | use CFPlus::OpenGL; |
3819 | use DC::OpenGL; |
3367 | |
3820 | |
3368 | sub new { |
3821 | sub new { |
3369 | my $class = shift; |
3822 | my $class = shift; |
3370 | |
3823 | |
3371 | my $self = $class->SUPER::new ( |
3824 | my $self = $class->SUPER::new ( |
3372 | items => [], |
3825 | items => [], |
3373 | z => 100, |
3826 | z => 100, |
3374 | @_, |
3827 | @_, |
3375 | ); |
3828 | ); |
3376 | |
3829 | |
3377 | $self->add ($self->{vbox} = new CFPlus::UI::VBox); |
3830 | $self->add ($self->{vbox} = new DC::UI::VBox); |
3378 | |
3831 | |
3379 | for my $item (@{ $self->{items} }) { |
3832 | for my $item (@{ $self->{items} }) { |
3380 | my ($widget, $cb, $tooltip) = @$item; |
3833 | my ($widget, $cb, $tooltip) = @$item; |
3381 | |
3834 | |
3382 | # handle various types of items, only text for now |
3835 | # handle various types of items, only text for now |
3383 | if (!ref $widget) { |
3836 | if (!ref $widget) { |
3384 | if ($widget =~ /\t/) { |
3837 | if ($widget =~ /\t/) { |
3385 | my ($left, $right) = split /\t/, $widget, 2; |
3838 | my ($left, $right) = split /\t/, $widget, 2; |
3386 | |
3839 | |
3387 | $widget = new CFPlus::UI::HBox |
3840 | $widget = new DC::UI::HBox |
3388 | can_hover => 1, |
3841 | can_hover => 1, |
3389 | can_events => 1, |
3842 | can_events => 1, |
3390 | tooltip => $tooltip, |
3843 | tooltip => $tooltip, |
3391 | children => [ |
3844 | children => [ |
3392 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3845 | (new DC::UI::Label markup => $left, expand => 1), |
3393 | (new CFPlus::UI::Label markup => $right, align => +1), |
3846 | (new DC::UI::Label markup => $right, align => +1), |
3394 | ], |
3847 | ], |
3395 | ; |
3848 | ; |
3396 | |
3849 | |
3397 | } else { |
3850 | } else { |
3398 | $widget = new CFPlus::UI::Label |
3851 | $widget = new DC::UI::Label |
3399 | can_hover => 1, |
3852 | can_hover => 1, |
3400 | can_events => 1, |
3853 | can_events => 1, |
3401 | markup => $widget, |
3854 | markup => $widget, |
3402 | tooltip => $tooltip; |
3855 | tooltip => $tooltip; |
3403 | } |
3856 | } |
… | |
… | |
3451 | 1 |
3904 | 1 |
3452 | } |
3905 | } |
3453 | |
3906 | |
3454 | ############################################################################# |
3907 | ############################################################################# |
3455 | |
3908 | |
3456 | package CFPlus::UI::Multiplexer; |
3909 | package DC::UI::Multiplexer; |
3457 | |
3910 | |
3458 | our @ISA = CFPlus::UI::Container::; |
3911 | our @ISA = DC::UI::Container::; |
3459 | |
3912 | |
3460 | sub new { |
3913 | sub new { |
3461 | my $class = shift; |
3914 | my $class = shift; |
3462 | |
3915 | |
3463 | my $self = $class->SUPER::new ( |
3916 | my $self = $class->SUPER::new ( |
… | |
… | |
3524 | $self->{current}->draw; |
3977 | $self->{current}->draw; |
3525 | } |
3978 | } |
3526 | |
3979 | |
3527 | ############################################################################# |
3980 | ############################################################################# |
3528 | |
3981 | |
3529 | package CFPlus::UI::Notebook; |
3982 | package DC::UI::Notebook; |
3530 | |
3983 | |
|
|
3984 | use DC::OpenGL; |
|
|
3985 | |
3531 | our @ISA = CFPlus::UI::VBox::; |
3986 | our @ISA = DC::UI::VBox::; |
3532 | |
3987 | |
3533 | sub new { |
3988 | sub new { |
3534 | my $class = shift; |
3989 | my $class = shift; |
3535 | |
3990 | |
3536 | my $self = $class->SUPER::new ( |
3991 | my $self = $class->SUPER::new ( |
3537 | buttonbar => (new CFPlus::UI::Buttonbar), |
3992 | buttonbar => (new DC::UI::Buttonbar), |
3538 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
3993 | multiplexer => (new DC::UI::Multiplexer expand => 1), |
|
|
3994 | active_outline => [.7, .7, 0.2], |
3539 | # filter => # will be put between multiplexer and $self |
3995 | # filter => # will be put between multiplexer and $self |
3540 | @_, |
3996 | @_, |
3541 | ); |
3997 | ); |
3542 | |
3998 | |
3543 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3999 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3544 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
4000 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
3545 | |
4001 | |
|
|
4002 | { |
|
|
4003 | Scalar::Util::weaken (my $wself = $self); |
|
|
4004 | |
|
|
4005 | $self->{multiplexer}->connect (c_add => sub { |
|
|
4006 | my ($mplex, $widgets) = @_; |
|
|
4007 | |
|
|
4008 | for my $child (@$widgets) { |
|
|
4009 | Scalar::Util::weaken $child; |
|
|
4010 | $child->{c_tab_} ||= do { |
|
|
4011 | my $tab = |
|
|
4012 | (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base") |
|
|
4013 | ? $child->{c_tab} |
|
|
4014 | : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
|
|
4015 | |
|
|
4016 | $tab->connect (activate => sub { |
|
|
4017 | $wself->set_current_page ($child); |
|
|
4018 | }); |
|
|
4019 | |
|
|
4020 | $tab |
|
|
4021 | }; |
|
|
4022 | |
|
|
4023 | $self->{buttonbar}->add ($child->{c_tab_}); |
|
|
4024 | } |
|
|
4025 | }); |
|
|
4026 | |
|
|
4027 | $self->{multiplexer}->connect (c_remove => sub { |
|
|
4028 | my ($mplex, $widgets) = @_; |
|
|
4029 | |
|
|
4030 | for my $child (@$widgets) { |
|
|
4031 | $wself->{buttonbar}->remove ($child->{c_tab_}); |
|
|
4032 | } |
|
|
4033 | }); |
|
|
4034 | } |
|
|
4035 | |
3546 | $self |
4036 | $self |
3547 | } |
4037 | } |
3548 | |
4038 | |
3549 | sub add { |
4039 | sub add { |
|
|
4040 | my ($self, @widgets) = @_; |
|
|
4041 | |
|
|
4042 | $self->{multiplexer}->add (@widgets) |
|
|
4043 | } |
|
|
4044 | |
|
|
4045 | sub remove { |
|
|
4046 | my ($self, @widgets) = @_; |
|
|
4047 | |
|
|
4048 | $self->{multiplexer}->remove (@widgets) |
|
|
4049 | } |
|
|
4050 | |
|
|
4051 | sub pages { |
|
|
4052 | my ($self) = @_; |
|
|
4053 | $self->{multiplexer}->children |
|
|
4054 | } |
|
|
4055 | |
|
|
4056 | sub add_tab { |
3550 | my ($self, $title, $widget, $tooltip) = @_; |
4057 | my ($self, $title, $widget, $tooltip) = @_; |
3551 | |
4058 | |
3552 | CFPlus::weaken $self; |
4059 | $title = [$title, $tooltip] unless ref $title; |
|
|
4060 | $widget->{c_tab} = $title; |
3553 | |
4061 | |
3554 | $self->{buttonbar}->add (new CFPlus::UI::Button |
|
|
3555 | markup => $title, |
|
|
3556 | tooltip => $tooltip, |
|
|
3557 | on_activate => sub { $self->set_current_page ($widget) }, |
|
|
3558 | ); |
|
|
3559 | |
|
|
3560 | $self->{multiplexer}->add ($widget); |
4062 | $self->add ($widget); |
3561 | } |
4063 | } |
3562 | |
4064 | |
3563 | sub get_current_page { |
4065 | sub get_current_page { |
3564 | my ($self) = @_; |
4066 | my ($self) = @_; |
3565 | |
4067 | |
… | |
… | |
3571 | |
4073 | |
3572 | $self->{multiplexer}->set_current_page ($page); |
4074 | $self->{multiplexer}->set_current_page ($page); |
3573 | $self->emit (page_changed => $self->{multiplexer}{current}); |
4075 | $self->emit (page_changed => $self->{multiplexer}{current}); |
3574 | } |
4076 | } |
3575 | |
4077 | |
|
|
4078 | sub _draw { |
|
|
4079 | my ($self) = @_; |
|
|
4080 | |
|
|
4081 | $self->SUPER::_draw (); |
|
|
4082 | |
|
|
4083 | if (my $cur = $self->{multiplexer}{current}) { |
|
|
4084 | if ($cur = $cur->{c_tab_}) { |
|
|
4085 | glTranslate $self->{buttonbar}{x} + $cur->{x}, |
|
|
4086 | $self->{buttonbar}{y} + $cur->{y}; |
|
|
4087 | glLineWidth 3; |
|
|
4088 | #glEnable GL_BLEND; |
|
|
4089 | #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
4090 | glColor @{$self->{active_outline}}; |
|
|
4091 | glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5; |
|
|
4092 | glLineWidth 1; |
|
|
4093 | #glDisable GL_BLEND; |
|
|
4094 | } |
|
|
4095 | } |
|
|
4096 | } |
|
|
4097 | |
3576 | ############################################################################# |
4098 | ############################################################################# |
3577 | |
4099 | |
3578 | package CFPlus::UI::Selector; |
4100 | package DC::UI::Selector; |
3579 | |
4101 | |
3580 | use utf8; |
4102 | use utf8; |
3581 | |
4103 | |
3582 | our @ISA = CFPlus::UI::Button::; |
4104 | our @ISA = DC::UI::Button::; |
3583 | |
4105 | |
3584 | sub new { |
4106 | sub new { |
3585 | my $class = shift; |
4107 | my $class = shift; |
3586 | |
4108 | |
3587 | my $self = $class->SUPER::new ( |
4109 | my $self = $class->SUPER::new ( |
… | |
… | |
3604 | my ($value, $title, $tooltip) = @$_; |
4126 | my ($value, $title, $tooltip) = @$_; |
3605 | |
4127 | |
3606 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
4128 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
3607 | } |
4129 | } |
3608 | |
4130 | |
3609 | CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
4131 | DC::UI::Menu->new (items => \@menu_items)->popup ($ev); |
3610 | } |
4132 | } |
3611 | |
4133 | |
3612 | sub _set_value { |
4134 | sub _set_value { |
3613 | my ($self, $value) = @_; |
4135 | my ($self, $value) = @_; |
3614 | |
4136 | |
3615 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} } |
4137 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} }; |
|
|
4138 | $item ||= $self->{options}[0] |
3616 | or return; |
4139 | or return; |
3617 | |
4140 | |
3618 | $self->{value} = $item->[0]; |
4141 | $self->{value} = $item->[0]; |
3619 | $self->set_markup ("$item->[1] ⇓"); |
4142 | $self->set_markup ("$item->[1] ⇓"); |
3620 | $self->set_tooltip ($item->[2]); |
4143 | # $self->set_tooltip ($item->[2]); |
3621 | } |
4144 | } |
3622 | |
4145 | |
3623 | sub set_value { |
4146 | sub set_value { |
3624 | my ($self, $value) = @_; |
4147 | my ($self, $value) = @_; |
3625 | |
4148 | |
… | |
… | |
3627 | |
4150 | |
3628 | $self->_set_value ($value); |
4151 | $self->_set_value ($value); |
3629 | $self->emit (changed => $value); |
4152 | $self->emit (changed => $value); |
3630 | } |
4153 | } |
3631 | |
4154 | |
|
|
4155 | sub set_options { |
|
|
4156 | my ($self, $options) = @_; |
|
|
4157 | |
|
|
4158 | $self->{options} = $options; |
|
|
4159 | $self->_set_value ($self->{value}); |
|
|
4160 | } |
|
|
4161 | |
3632 | ############################################################################# |
4162 | ############################################################################# |
3633 | |
4163 | |
3634 | package CFPlus::UI::Statusbox; |
4164 | package DC::UI::Statusbox; |
3635 | |
4165 | |
3636 | our @ISA = CFPlus::UI::VBox::; |
4166 | our @ISA = DC::UI::VBox::; |
3637 | |
4167 | |
3638 | sub new { |
4168 | sub new { |
3639 | my $class = shift; |
4169 | my $class = shift; |
3640 | |
4170 | |
3641 | my $self = $class->SUPER::new ( |
4171 | my $self = $class->SUPER::new ( |
3642 | fontsize => 0.8, |
4172 | fontsize => 0.8, |
3643 | @_, |
4173 | @_, |
3644 | ); |
4174 | ); |
3645 | |
4175 | |
3646 | CFPlus::weaken (my $this = $self); |
4176 | DC::weaken (my $this = $self); |
3647 | |
4177 | |
3648 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
4178 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
3649 | |
4179 | |
3650 | $self |
4180 | $self |
3651 | } |
4181 | } |
3652 | |
4182 | |
3653 | sub reorder { |
4183 | sub reorder { |
3654 | my ($self) = @_; |
4184 | my ($self) = @_; |
3655 | my $NOW = Time::HiRes::time; |
4185 | my $NOW = Time::HiRes::time; |
3656 | |
4186 | |
3657 | # freeze display when hovering over any label |
4187 | # freeze display when hovering over any label |
3658 | return if $CFPlus::UI::TOOLTIP->{owner} |
4188 | return if $DC::UI::TOOLTIP->{owner} |
3659 | && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, |
4189 | && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, |
3660 | values %{ $self->{item} }; |
4190 | values %{ $self->{item} }; |
3661 | |
4191 | |
3662 | while (my ($k, $v) = each %{ $self->{item} }) { |
4192 | while (my ($k, $v) = each %{ $self->{item} }) { |
3663 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4193 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
3664 | } |
4194 | } |
|
|
4195 | |
|
|
4196 | $self->{timer}->set (1, 1); |
3665 | |
4197 | |
3666 | my @widgets; |
4198 | my @widgets; |
3667 | |
4199 | |
3668 | my @items = sort { |
4200 | my @items = sort { |
3669 | $a->{pri} <=> $b->{pri} |
4201 | $a->{pri} <=> $b->{pri} |
3670 | or $b->{id} <=> $a->{id} |
4202 | or $b->{id} <=> $a->{id} |
3671 | } values %{ $self->{item} }; |
4203 | } values %{ $self->{item} }; |
3672 | |
|
|
3673 | $self->{timer}->interval (1); |
|
|
3674 | |
4204 | |
3675 | my $count = 10 + 1; |
4205 | my $count = 10 + 1; |
3676 | for my $item (@items) { |
4206 | for my $item (@items) { |
3677 | last unless --$count; |
4207 | last unless --$count; |
3678 | |
4208 | |
… | |
… | |
3685 | for ($short) { |
4215 | for ($short) { |
3686 | s/^\s+//; |
4216 | s/^\s+//; |
3687 | s/\s+/ /g; |
4217 | s/\s+/ /g; |
3688 | } |
4218 | } |
3689 | |
4219 | |
3690 | new CFPlus::UI::Label |
4220 | new DC::UI::Label |
3691 | markup => $short, |
4221 | markup => $short, |
3692 | tooltip => $item->{tooltip}, |
4222 | tooltip => $item->{tooltip}, |
3693 | tooltip_font => $::FONT_PROP, |
4223 | tooltip_font => $::FONT_PROP, |
3694 | tooltip_width => 0.67, |
4224 | tooltip_width => 0.67, |
3695 | fontsize => $item->{fontsize} || $self->{fontsize}, |
4225 | fontsize => $item->{fontsize} || $self->{fontsize}, |
… | |
… | |
3702 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4232 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
3703 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4233 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
3704 | $label->update; |
4234 | $label->update; |
3705 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4235 | $label->set_max_size (undef, $label->{req_h} * $diff) |
3706 | if $diff < 1; |
4236 | if $diff < 1; |
3707 | $self->{timer}->interval (1/30); |
4237 | $self->{timer}->set (1/30, 1/30); |
3708 | } else { |
4238 | } else { |
3709 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4239 | $label->{fg}[3] = $item->{fg}[3] || 1; |
3710 | } |
4240 | } |
3711 | |
4241 | |
3712 | push @widgets, $label; |
4242 | push @widgets, $label; |
… | |
… | |
3774 | $self->SUPER::destroy; |
4304 | $self->SUPER::destroy; |
3775 | } |
4305 | } |
3776 | |
4306 | |
3777 | ############################################################################# |
4307 | ############################################################################# |
3778 | |
4308 | |
3779 | package CFPlus::UI::Root; |
4309 | package DC::UI::Root; |
3780 | |
4310 | |
3781 | our @ISA = CFPlus::UI::Container::; |
4311 | our @ISA = DC::UI::Container::; |
3782 | |
4312 | |
3783 | use List::Util qw(min max); |
4313 | use List::Util qw(min max); |
3784 | |
4314 | |
3785 | use CFPlus::OpenGL; |
4315 | use DC::OpenGL; |
3786 | |
4316 | |
3787 | sub new { |
4317 | sub new { |
3788 | my $class = shift; |
4318 | my $class = shift; |
3789 | |
4319 | |
3790 | my $self = $class->SUPER::new ( |
4320 | my $self = $class->SUPER::new ( |
3791 | visible => 1, |
4321 | visible => 1, |
3792 | @_, |
4322 | @_, |
3793 | ); |
4323 | ); |
3794 | |
4324 | |
3795 | CFPlus::weaken ($self->{root} = $self); |
4325 | DC::weaken ($self->{root} = $self); |
3796 | |
4326 | |
3797 | $self |
4327 | $self |
3798 | } |
4328 | } |
3799 | |
4329 | |
3800 | sub size_request { |
4330 | sub size_request { |
… | |
… | |
3848 | } |
4378 | } |
3849 | |
4379 | |
3850 | sub update { |
4380 | sub update { |
3851 | my ($self) = @_; |
4381 | my ($self) = @_; |
3852 | |
4382 | |
3853 | $::WANT_REFRESH++; |
4383 | $::WANT_REFRESH = 1; |
3854 | } |
4384 | } |
3855 | |
4385 | |
3856 | sub add { |
4386 | sub add { |
3857 | my ($self, @children) = @_; |
4387 | my ($self, @children) = @_; |
3858 | |
4388 | |
… | |
… | |
3895 | while ($self->{refresh_hook}) { |
4425 | while ($self->{refresh_hook}) { |
3896 | $_->() |
4426 | $_->() |
3897 | for values %{delete $self->{refresh_hook}}; |
4427 | for values %{delete $self->{refresh_hook}}; |
3898 | } |
4428 | } |
3899 | |
4429 | |
3900 | if ($self->{realloc}) { |
4430 | while ($self->{realloc}) { |
3901 | my %queue; |
4431 | my %queue; |
3902 | my @queue; |
4432 | my @queue; |
3903 | my $widget; |
4433 | my $widget; |
3904 | |
4434 | |
3905 | outer: |
4435 | outer: |
… | |
… | |
3952 | } |
4482 | } |
3953 | } |
4483 | } |
3954 | |
4484 | |
3955 | delete $self->{realloc}{$widget+0}; |
4485 | delete $self->{realloc}{$widget+0}; |
3956 | } |
4486 | } |
3957 | } |
|
|
3958 | |
4487 | |
3959 | while (my $size_alloc = delete $self->{size_alloc}) { |
4488 | while (my $size_alloc = delete $self->{size_alloc}) { |
3960 | my @queue = sort { $b->{visible} <=> $a->{visible} } |
4489 | my @queue = sort { $a->{visible} <=> $b->{visible} } |
3961 | values %$size_alloc; |
4490 | values %$size_alloc; |
3962 | |
4491 | |
3963 | while () { |
4492 | while () { |
3964 | my $widget = pop @queue || last; |
4493 | my $widget = pop @queue || last; |
3965 | |
4494 | |
3966 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
4495 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3967 | |
4496 | |
3968 | $w = 0 if $w < 0; |
|
|
3969 | $h = 0 if $h < 0; |
|
|
3970 | |
|
|
3971 | $w = max $widget->{min_w}, $w; |
4497 | $w = max $widget->{min_w}, $w; |
3972 | $h = max $widget->{min_h}, $h; |
4498 | $h = max $widget->{min_h}, $h; |
3973 | |
4499 | |
3974 | # $w = min $self->{w} - $widget->{x}, $w if $self->{w}; |
4500 | # $w = min $self->{w} - $widget->{x}, $w if $self->{w}; |
3975 | # $h = min $self->{h} - $widget->{y}, $h if $self->{h}; |
4501 | # $h = min $self->{h} - $widget->{y}, $h if $self->{h}; |
3976 | |
4502 | |
3977 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
4503 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
3978 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
4504 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
3979 | |
4505 | |
3980 | $w = int $w + 0.5; |
4506 | $w = int $w + 0.5; |
3981 | $h = int $h + 0.5; |
4507 | $h = int $h + 0.5; |
3982 | |
4508 | |
3983 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
4509 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
3984 | $widget->{old_w} = $widget->{w}; |
4510 | $widget->{old_w} = $widget->{w}; |
3985 | $widget->{old_h} = $widget->{h}; |
4511 | $widget->{old_h} = $widget->{h}; |
3986 | |
4512 | |
3987 | $widget->{w} = $w; |
4513 | $widget->{w} = $w; |
3988 | $widget->{h} = $h; |
4514 | $widget->{h} = $h; |
3989 | |
4515 | |
3990 | $widget->emit (size_allocate => $w, $h); |
4516 | $widget->emit (size_allocate => $w, $h); |
|
|
4517 | } |
3991 | } |
4518 | } |
3992 | } |
4519 | } |
3993 | } |
4520 | } |
3994 | |
4521 | |
3995 | while ($self->{post_alloc_hook}) { |
4522 | while ($self->{post_alloc_hook}) { |
3996 | $_->() |
4523 | $_->() |
3997 | for values %{delete $self->{post_alloc_hook}}; |
4524 | for values %{delete $self->{post_alloc_hook}}; |
3998 | } |
4525 | } |
3999 | |
|
|
4000 | |
4526 | |
4001 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
4527 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
4002 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
4528 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
4003 | glClear GL_COLOR_BUFFER_BIT; |
4529 | glClear GL_COLOR_BUFFER_BIT; |
4004 | |
4530 | |
… | |
… | |
4007 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4533 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4008 | glMatrixMode GL_MODELVIEW; |
4534 | glMatrixMode GL_MODELVIEW; |
4009 | glLoadIdentity; |
4535 | glLoadIdentity; |
4010 | |
4536 | |
4011 | { |
4537 | { |
4012 | package CFPlus::UI::Base; |
4538 | package DC::UI::Base; |
4013 | |
4539 | |
4014 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4540 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4015 | (0, 0, $self->{w}, $self->{h}); |
4541 | (0, 0, $self->{w}, $self->{h}); |
4016 | |
4542 | |
4017 | $self->_draw; |
4543 | $self->_draw; |
4018 | } |
4544 | } |
4019 | } |
4545 | } |
4020 | |
4546 | |
4021 | ############################################################################# |
4547 | ############################################################################# |
4022 | |
4548 | |
4023 | package CFPlus::UI; |
4549 | package DC::UI; |
4024 | |
4550 | |
4025 | $ROOT = new CFPlus::UI::Root; |
4551 | $ROOT = new DC::UI::Root; |
4026 | $TOOLTIP = new CFPlus::UI::Tooltip z => 900; |
4552 | $TOOLTIP = new DC::UI::Tooltip z => 900; |
4027 | |
4553 | |
4028 | 1 |
4554 | 1 |
4029 | |
4555 | |