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) { |
27 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
28 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
… | |
… | |
30 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
32 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
32 | |
33 | |
33 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | |
35 | |
35 | my $tip = $widget->{tooltip}; |
|
|
36 | |
|
|
37 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
38 | |
|
|
39 | $TOOLTIP->set_tooltip_from ($widget); |
36 | $TOOLTIP->set_tooltip_from ($widget); |
40 | $TOOLTIP->show; |
37 | $TOOLTIP->show; |
41 | } |
38 | } |
42 | |
39 | |
43 | return; |
40 | return; |
… | |
… | |
46 | } |
43 | } |
47 | |
44 | |
48 | $TOOLTIP->hide; |
45 | $TOOLTIP->hide; |
49 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
46 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
47 | delete $TOOLTIP->{owner}; |
51 | }); |
48 | }; |
52 | |
49 | |
53 | sub get_layout { |
50 | sub get_layout { |
54 | my $layout; |
51 | my $layout; |
55 | |
52 | |
56 | for (grep { $_->{name} } values %WIDGET) { |
53 | for (grep { $_->{name} } values %WIDGET) { |
… | |
… | |
91 | my $hover = $HOVER; $HOVER = $widget; |
88 | my $hover = $HOVER; $HOVER = $widget; |
92 | |
89 | |
93 | $hover->update if $hover && $hover->{can_hover}; |
90 | $hover->update if $hover && $hover->{can_hover}; |
94 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
91 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
95 | |
92 | |
96 | $TOOLTIP_WATCHER->start; |
93 | $TOOLTIP_WATCHER->again; |
97 | } |
94 | } |
98 | } |
95 | } |
99 | |
96 | |
100 | sub feed_sdl_button_down_event { |
97 | sub feed_sdl_button_down_event { |
101 | my ($ev) = @_; |
98 | my ($ev) = @_; |
… | |
… | |
107 | my $widget = $ROOT->find_widget ($x, $y); |
104 | my $widget = $ROOT->find_widget ($x, $y); |
108 | |
105 | |
109 | $GRAB = $widget; |
106 | $GRAB = $widget; |
110 | $GRAB->update if $GRAB; |
107 | $GRAB->update if $GRAB; |
111 | |
108 | |
112 | $TOOLTIP_WATCHER->cb->(); |
109 | $TOOLTIP_WATCHER->invoke; |
113 | } |
110 | } |
114 | |
111 | |
115 | if ($GRAB) { |
112 | if ($GRAB) { |
116 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
113 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
117 | # mousewheel |
114 | # mousewheel |
118 | my $delta = $ev->{button} * 2 - 9; |
115 | my $delta = $ev->{button} * 2 - 9; |
119 | my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; |
116 | my $shift = $ev->{mod} & DC::KMOD_SHIFT; |
120 | |
117 | |
121 | $ev->{dx} = $shift ? $delta : 0; |
118 | $ev->{dx} = $shift ? $delta : 0; |
122 | $ev->{dy} = $shift ? 0 : $delta; |
119 | $ev->{dy} = $shift ? 0 : $delta; |
123 | |
120 | |
124 | $GRAB->emit (mouse_wheel => $ev); |
121 | $GRAB->emit (mouse_wheel => $ev); |
… | |
… | |
142 | my $grab = $GRAB; undef $GRAB; |
139 | my $grab = $GRAB; undef $GRAB; |
143 | $grab->update if $grab; |
140 | $grab->update if $grab; |
144 | $GRAB->update if $GRAB; |
141 | $GRAB->update if $GRAB; |
145 | |
142 | |
146 | check_hover $widget; |
143 | check_hover $widget; |
147 | $TOOLTIP_WATCHER->cb->(); |
144 | $TOOLTIP_WATCHER->invoke; |
148 | } |
145 | } |
149 | } |
146 | } |
150 | |
147 | |
151 | sub feed_sdl_motion_event { |
148 | sub feed_sdl_motion_event { |
152 | my ($ev) = @_; |
149 | my ($ev) = @_; |
… | |
… | |
214 | reconfigure_widgets; |
211 | reconfigure_widgets; |
215 | } |
212 | } |
216 | |
213 | |
217 | ############################################################################# |
214 | ############################################################################# |
218 | |
215 | |
219 | package CFPlus::UI::Event; |
216 | package DC::UI::Event; |
220 | |
217 | |
221 | sub xy { |
218 | sub xy { |
222 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
219 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
223 | } |
220 | } |
224 | |
221 | |
225 | ############################################################################# |
222 | ############################################################################# |
226 | |
223 | |
227 | package CFPlus::UI::Base; |
224 | package DC::UI::Base; |
228 | |
225 | |
229 | use strict; |
226 | use strict; |
230 | |
227 | |
231 | use CFPlus::OpenGL; |
228 | use DC::OpenGL; |
232 | |
229 | |
233 | sub new { |
230 | sub new { |
234 | my $class = shift; |
231 | my $class = shift; |
235 | |
232 | |
236 | my $self = bless { |
233 | my $self = bless { |
… | |
… | |
241 | h => undef, |
238 | h => undef, |
242 | can_events => 1, |
239 | can_events => 1, |
243 | @_ |
240 | @_ |
244 | }, $class; |
241 | }, $class; |
245 | |
242 | |
246 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
243 | DC::weaken ($DC::UI::WIDGET{$self+0} = $self); |
247 | |
244 | |
248 | for (keys %$self) { |
245 | for (keys %$self) { |
249 | if (/^on_(.*)$/) { |
246 | if (/^on_(.*)$/) { |
250 | $self->connect ($1 => delete $self->{$_}); |
247 | $self->connect ($1 => delete $self->{$_}); |
251 | } |
248 | } |
252 | } |
249 | } |
253 | |
250 | |
254 | if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { |
251 | if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) { |
255 | $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}; |
256 | $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}; |
257 | $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}; |
258 | $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}; |
259 | |
256 | |
260 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
257 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
261 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
258 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
262 | |
259 | |
263 | $self->show if $layout->{show}; |
260 | $self->show if $layout->{show}; |
… | |
… | |
273 | $self->emit ("destroy"); |
270 | $self->emit ("destroy"); |
274 | %$self = (); |
271 | %$self = (); |
275 | } |
272 | } |
276 | |
273 | |
277 | sub TO_JSON { |
274 | sub TO_JSON { |
278 | { __widget_ref__ => $_[0]{s_id} } |
275 | { "\fw" => $_[0]{s_id} } |
279 | } |
276 | } |
280 | |
277 | |
281 | sub show { |
278 | sub show { |
282 | my ($self) = @_; |
279 | my ($self) = @_; |
283 | |
280 | |
284 | return if $self->{parent}; |
281 | return if $self->{parent}; |
285 | |
282 | |
286 | $CFPlus::UI::ROOT->add ($self); |
283 | $DC::UI::ROOT->add ($self); |
287 | } |
284 | } |
288 | |
285 | |
289 | sub set_visible { |
286 | sub set_visible { |
290 | my ($self) = @_; |
287 | my ($self) = @_; |
291 | |
288 | |
… | |
… | |
312 | delete $self->{root}; |
309 | delete $self->{root}; |
313 | |
310 | |
314 | undef $GRAB if $GRAB == $self; |
311 | undef $GRAB if $GRAB == $self; |
315 | undef $HOVER if $HOVER == $self; |
312 | undef $HOVER if $HOVER == $self; |
316 | |
313 | |
317 | $CFPlus::UI::TOOLTIP_WATCHER->cb->() |
314 | $DC::UI::TOOLTIP_WATCHER->invoke |
318 | if $TOOLTIP->{owner} == $self; |
315 | if $TOOLTIP->{owner} == $self; |
319 | |
316 | |
320 | $self->emit ("focus_out"); |
317 | $self->emit ("focus_out"); |
321 | $self->emit (visibility_change => 0); |
318 | $self->emit (visibility_change => 0); |
322 | } |
319 | } |
… | |
… | |
341 | sub hide { |
338 | sub hide { |
342 | my ($self) = @_; |
339 | my ($self) = @_; |
343 | |
340 | |
344 | $self->set_invisible; |
341 | $self->set_invisible; |
345 | |
342 | |
|
|
343 | # extra $parent copy for 5.8.8+ bug workaround |
|
|
344 | # (otherwise $_[0] in remove gets freed |
|
|
345 | if (my $parent = $self->{parent}) { |
346 | $self->{parent}->remove ($self) |
346 | $parent->remove ($self); |
347 | if $self->{parent}; |
347 | } |
348 | } |
348 | } |
349 | |
349 | |
350 | sub move_abs { |
350 | sub move_abs { |
351 | my ($self, $x, $y, $z) = @_; |
351 | my ($self, $x, $y, $z) = @_; |
352 | |
352 | |
… | |
… | |
364 | $self->{force_h} = $h; |
364 | $self->{force_h} = $h; |
365 | |
365 | |
366 | $self->realloc; |
366 | $self->realloc; |
367 | } |
367 | } |
368 | |
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 | |
369 | sub size_request { |
379 | sub size_request { |
370 | require Carp; |
380 | require Carp; |
371 | Carp::confess "size_request is abstract"; |
381 | Carp::confess "size_request is abstract"; |
372 | } |
382 | } |
373 | |
383 | |
… | |
… | |
379 | my ($self, $x, $y, $w, $h) = @_; |
389 | my ($self, $x, $y, $w, $h) = @_; |
380 | |
390 | |
381 | if ($self->{aspect}) { |
391 | if ($self->{aspect}) { |
382 | my ($ow, $oh) = ($w, $h); |
392 | my ($ow, $oh) = ($w, $h); |
383 | |
393 | |
384 | $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; |
394 | $w = List::Util::min $w, DC::ceil $h * $self->{aspect}; |
385 | $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; |
395 | $h = List::Util::min $h, DC::ceil $w / $self->{aspect}; |
386 | |
396 | |
387 | # use alignment to adjust x, y |
397 | # use alignment to adjust x, y |
388 | |
398 | |
389 | $x += int 0.5 * ($ow - $w); |
399 | $x += int 0.5 * ($ow - $w); |
390 | $y += int 0.5 * ($oh - $h); |
400 | $y += int 0.5 * ($oh - $h); |
… | |
… | |
431 | |
441 | |
432 | return if $self->{tooltip} eq $tooltip; |
442 | return if $self->{tooltip} eq $tooltip; |
433 | |
443 | |
434 | $self->{tooltip} = $tooltip; |
444 | $self->{tooltip} = $tooltip; |
435 | |
445 | |
436 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
446 | if ($DC::UI::TOOLTIP->{owner} == $self) { |
437 | delete $CFPlus::UI::TOOLTIP->{owner}; |
447 | delete $DC::UI::TOOLTIP->{owner}; |
438 | $CFPlus::UI::TOOLTIP_WATCHER->cb->(); |
448 | $DC::UI::TOOLTIP_WATCHER->invoke; |
439 | } |
449 | } |
440 | } |
450 | } |
441 | |
451 | |
442 | # translate global coordinates to local coordinate system |
452 | # translate global coordinates to local coordinate system |
443 | sub coord2local { |
453 | sub coord2local { |
444 | my ($self, $x, $y) = @_; |
454 | my ($self, $x, $y) = @_; |
445 | |
455 | |
446 | Carp::confess unless $self->{parent};#d# |
456 | return (undef, undef) unless $self->{parent}; |
447 | |
457 | |
448 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
458 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
449 | } |
459 | } |
450 | |
460 | |
451 | # translate local coordinates to global coordinate system |
461 | # translate local coordinates to global coordinate system |
452 | sub coord2global { |
462 | sub coord2global { |
453 | my ($self, $x, $y) = @_; |
463 | my ($self, $x, $y) = @_; |
454 | |
464 | |
455 | Carp::confess unless $self->{parent};#d# |
465 | return (undef, undef) unless $self->{parent}; |
456 | |
466 | |
457 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
467 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
458 | } |
468 | } |
459 | |
469 | |
460 | sub invoke_focus_in { |
470 | sub invoke_focus_in { |
… | |
… | |
509 | sub connect { |
519 | sub connect { |
510 | my ($self, $signal, $cb) = @_; |
520 | my ($self, $signal, $cb) = @_; |
511 | |
521 | |
512 | push @{ $self->{signal_cb}{$signal} }, $cb; |
522 | push @{ $self->{signal_cb}{$signal} }, $cb; |
513 | |
523 | |
514 | defined wantarray and CFPlus::guard { |
524 | defined wantarray and DC::guard { |
515 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
525 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
516 | @{ $self->{signal_cb}{$signal} }; |
526 | @{ $self->{signal_cb}{$signal} }; |
517 | } |
527 | } |
518 | } |
528 | } |
519 | |
529 | |
… | |
… | |
557 | # in .xs |
567 | # in .xs |
558 | |
568 | |
559 | sub set_parent { |
569 | sub set_parent { |
560 | my ($self, $parent) = @_; |
570 | my ($self, $parent) = @_; |
561 | |
571 | |
562 | CFPlus::weaken ($self->{parent} = $parent); |
572 | DC::weaken ($self->{parent} = $parent); |
563 | $self->set_visible if $parent->{visible}; |
573 | $self->set_visible if $parent->{visible}; |
564 | } |
574 | } |
565 | |
575 | |
566 | sub realloc { |
576 | sub realloc { |
567 | my ($self) = @_; |
577 | my ($self) = @_; |
… | |
… | |
605 | } |
615 | } |
606 | |
616 | |
607 | sub DESTROY { |
617 | sub DESTROY { |
608 | my ($self) = @_; |
618 | my ($self) = @_; |
609 | |
619 | |
610 | return if CFPlus::in_destruct; |
620 | return if DC::in_destruct; |
611 | |
621 | |
|
|
622 | local $@; |
612 | eval { $self->destroy }; |
623 | eval { $self->destroy }; |
613 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
624 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
614 | |
625 | |
615 | delete $WIDGET{$self+0}; |
626 | delete $WIDGET{$self+0}; |
616 | } |
627 | } |
617 | |
628 | |
618 | ############################################################################# |
629 | ############################################################################# |
619 | |
630 | |
620 | package CFPlus::UI::DrawBG; |
631 | package DC::UI::DrawBG; |
621 | |
632 | |
622 | our @ISA = CFPlus::UI::Base::; |
633 | our @ISA = DC::UI::Base::; |
623 | |
634 | |
624 | use strict; |
635 | use strict; |
625 | use CFPlus::OpenGL; |
636 | use DC::OpenGL; |
626 | |
637 | |
627 | sub new { |
638 | sub new { |
628 | my $class = shift; |
639 | my $class = shift; |
629 | |
|
|
630 | # range [value, low, high, page] |
|
|
631 | |
640 | |
632 | $class->SUPER::new ( |
641 | $class->SUPER::new ( |
633 | #bg => [0, 0, 0, 0.2], |
642 | #bg => [0, 0, 0, 0.2], |
634 | #active_bg => [1, 1, 1, 0.5], |
643 | #active_bg => [1, 1, 1, 0.5], |
635 | @_ |
644 | @_ |
… | |
… | |
647 | my ($w, $h) = @$self{qw(w h)}; |
656 | my ($w, $h) = @$self{qw(w h)}; |
648 | |
657 | |
649 | glEnable GL_BLEND; |
658 | glEnable GL_BLEND; |
650 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
659 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
651 | glColor_premultiply @$color; |
660 | glColor_premultiply @$color; |
652 | |
|
|
653 | glBegin GL_QUADS; |
|
|
654 | glVertex 0 , 0; |
|
|
655 | glVertex 0 , $h; |
|
|
656 | glVertex $w, $h; |
661 | glRect 0, 0, $w, $h; |
657 | glVertex $w, 0; |
|
|
658 | glEnd; |
|
|
659 | |
|
|
660 | glDisable GL_BLEND; |
662 | glDisable GL_BLEND; |
661 | } |
663 | } |
662 | } |
664 | } |
663 | |
665 | |
664 | ############################################################################# |
666 | ############################################################################# |
665 | |
667 | |
666 | package CFPlus::UI::Empty; |
668 | package DC::UI::Empty; |
667 | |
669 | |
668 | our @ISA = CFPlus::UI::Base::; |
670 | our @ISA = DC::UI::Base::; |
669 | |
671 | |
670 | sub new { |
672 | sub new { |
671 | my ($class, %arg) = @_; |
673 | my ($class, %arg) = @_; |
672 | $class->SUPER::new (can_events => 0, %arg); |
674 | $class->SUPER::new (can_events => 0, %arg); |
673 | } |
675 | } |
… | |
… | |
680 | |
682 | |
681 | sub draw { } |
683 | sub draw { } |
682 | |
684 | |
683 | ############################################################################# |
685 | ############################################################################# |
684 | |
686 | |
685 | package CFPlus::UI::Container; |
687 | package DC::UI::Container; |
686 | |
688 | |
687 | our @ISA = CFPlus::UI::Base::; |
689 | our @ISA = DC::UI::Base::; |
688 | |
690 | |
689 | sub new { |
691 | sub new { |
690 | my ($class, %arg) = @_; |
692 | my ($class, %arg) = @_; |
691 | |
693 | |
692 | my $children = delete $arg{children}; |
694 | my $children = delete $arg{children}; |
… | |
… | |
725 | @{$self->{children}}, @widgets |
727 | @{$self->{children}}, @widgets |
726 | ]; |
728 | ]; |
727 | |
729 | |
728 | $self->realloc; |
730 | $self->realloc; |
729 | |
731 | |
|
|
732 | $self->emit (c_add => \@widgets); |
|
|
733 | |
730 | map $_+0, @widgets |
734 | map $_+0, @widgets |
731 | } |
735 | } |
732 | |
736 | |
733 | sub children { |
737 | sub children { |
734 | @{ $_[0]{children} } |
738 | @{ $_[0]{children} } |
735 | } |
739 | } |
736 | |
740 | |
737 | sub remove { |
741 | sub remove { |
738 | my ($self, $child) = @_; |
742 | my ($self, @widgets) = @_; |
739 | |
743 | |
|
|
744 | $self->emit (c_remove => \@widgets); |
|
|
745 | |
|
|
746 | for my $child (@widgets) { |
740 | delete $child->{parent}; |
747 | delete $child->{parent}; |
741 | $child->hide; |
748 | $child->hide; |
742 | |
|
|
743 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
749 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
750 | } |
744 | |
751 | |
745 | $self->realloc; |
752 | $self->realloc; |
746 | } |
753 | } |
747 | |
754 | |
748 | sub clear { |
755 | sub clear { |
… | |
… | |
776 | } |
783 | } |
777 | |
784 | |
778 | sub _draw { |
785 | sub _draw { |
779 | my ($self) = @_; |
786 | my ($self) = @_; |
780 | |
787 | |
781 | $_->draw for @{$self->{children}}; |
788 | $_->draw for $self->visible_children; |
782 | } |
789 | } |
783 | |
790 | |
784 | ############################################################################# |
791 | ############################################################################# |
785 | |
792 | |
786 | package CFPlus::UI::Bin; |
793 | package DC::UI::Bin; |
787 | |
794 | |
788 | our @ISA = CFPlus::UI::Container::; |
795 | our @ISA = DC::UI::Container::; |
789 | |
796 | |
790 | sub new { |
797 | sub new { |
791 | my ($class, %arg) = @_; |
798 | my ($class, %arg) = @_; |
792 | |
799 | |
793 | my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; |
800 | my $child = (delete $arg{child}) || new DC::UI::Empty::; |
794 | |
801 | |
795 | $class->SUPER::new (children => [$child], %arg) |
802 | $class->SUPER::new (children => [$child], %arg) |
796 | } |
803 | } |
797 | |
804 | |
798 | sub add { |
805 | sub add { |
799 | my ($self, $child) = @_; |
806 | my ($self, $child) = @_; |
800 | |
807 | |
801 | $self->SUPER::remove ($_) for @{ $self->{children} }; |
808 | $self->clear; |
802 | $self->SUPER::add ($child); |
809 | $self->SUPER::add ($child); |
803 | } |
810 | } |
804 | |
811 | |
805 | sub remove { |
812 | sub remove { |
806 | my ($self, $widget) = @_; |
813 | my ($self, $widget) = @_; |
807 | |
814 | |
808 | $self->SUPER::remove ($widget); |
815 | $self->SUPER::remove ($widget); |
809 | |
816 | |
810 | $self->{children} = [new CFPlus::UI::Empty] |
817 | $self->{children} = [new DC::UI::Empty] |
811 | unless @{$self->{children}}; |
818 | unless @{$self->{children}}; |
812 | } |
819 | } |
813 | |
820 | |
814 | sub child { $_[0]->{children}[0] } |
821 | sub child { $_[0]->{children}[0] } |
815 | |
822 | |
… | |
… | |
824 | |
831 | |
825 | 1 |
832 | 1 |
826 | } |
833 | } |
827 | |
834 | |
828 | ############################################################################# |
835 | ############################################################################# |
829 | |
|
|
830 | # back-buffered drawing area |
836 | # back-buffered drawing area |
831 | |
837 | |
832 | package CFPlus::UI::Window; |
838 | package DC::UI::Window; |
833 | |
839 | |
834 | our @ISA = CFPlus::UI::Bin::; |
840 | our @ISA = DC::UI::Bin::; |
835 | |
841 | |
836 | use CFPlus::OpenGL; |
842 | use DC::OpenGL; |
837 | |
843 | |
838 | sub new { |
844 | sub new { |
839 | my ($class, %arg) = @_; |
845 | my ($class, %arg) = @_; |
840 | |
846 | |
841 | my $self = $class->SUPER::new (%arg); |
847 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
863 | } |
869 | } |
864 | |
870 | |
865 | sub render_child { |
871 | sub render_child { |
866 | my ($self) = @_; |
872 | my ($self) = @_; |
867 | |
873 | |
868 | $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 { |
869 | glClearColor 0, 0, 0, 0; |
875 | glClearColor 0, 0, 0, 0; |
870 | glClear GL_COLOR_BUFFER_BIT; |
876 | glClear GL_COLOR_BUFFER_BIT; |
871 | |
877 | |
872 | { |
878 | { |
873 | package CFPlus::UI::Base; |
879 | package DC::UI::Base; |
874 | |
880 | |
875 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
881 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
876 | (0, 0, $self->{w}, $self->{h}); |
882 | (0, 0, $self->{w}, $self->{h}); |
877 | |
883 | |
878 | $self->_render; |
884 | $self->_render; |
… | |
… | |
895 | glDisable GL_TEXTURE_2D; |
901 | glDisable GL_TEXTURE_2D; |
896 | } |
902 | } |
897 | |
903 | |
898 | ############################################################################# |
904 | ############################################################################# |
899 | |
905 | |
900 | package CFPlus::UI::ViewPort; |
906 | package DC::UI::ViewPort; |
901 | |
907 | |
902 | use List::Util qw(min max); |
908 | use List::Util qw(min max); |
903 | |
909 | |
904 | our @ISA = CFPlus::UI::Window::; |
910 | our @ISA = DC::UI::Window::; |
905 | |
911 | |
906 | sub new { |
912 | sub new { |
907 | my $class = shift; |
913 | my $class = shift; |
908 | |
914 | |
909 | $class->SUPER::new ( |
915 | $class->SUPER::new ( |
… | |
… | |
951 | $self->emit (changed => $x, $y); |
957 | $self->emit (changed => $x, $y); |
952 | $self->update; |
958 | $self->update; |
953 | } |
959 | } |
954 | } |
960 | } |
955 | |
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 | } |
|
|
978 | } |
|
|
979 | |
956 | # 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 |
957 | sub coord2local { |
981 | sub coord2local { |
958 | my ($self, $x, $y) = @_; |
982 | my ($self, $x, $y) = @_; |
959 | |
983 | |
960 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
984 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
… | |
… | |
975 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
999 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
976 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1000 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
977 | ) { |
1001 | ) { |
978 | $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}) |
979 | } else { |
1003 | } else { |
980 | $self->CFPlus::UI::Base::find_widget ($x, $y) |
1004 | $self->DC::UI::Base::find_widget ($x, $y) |
981 | } |
1005 | } |
982 | } |
1006 | } |
983 | |
1007 | |
984 | sub _render { |
1008 | sub _render { |
985 | my ($self) = @_; |
1009 | my ($self) = @_; |
986 | |
1010 | |
987 | 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}; |
988 | 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}; |
989 | |
1013 | |
990 | CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1014 | DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
991 | |
1015 | |
992 | $self->SUPER::_render; |
1016 | $self->SUPER::_render; |
993 | } |
1017 | } |
994 | |
1018 | |
995 | ############################################################################# |
1019 | ############################################################################# |
996 | |
1020 | |
997 | package CFPlus::UI::ScrolledWindow; |
1021 | package DC::UI::ScrolledWindow; |
998 | |
1022 | |
999 | our @ISA = CFPlus::UI::Table::; |
1023 | our @ISA = DC::UI::Table::; |
1000 | |
1024 | |
1001 | sub new { |
1025 | sub new { |
1002 | my ($class, %arg) = @_; |
1026 | my ($class, %arg) = @_; |
1003 | |
1027 | |
1004 | my $child = delete $arg{child}; |
1028 | my $child = delete $arg{child}; |
1005 | |
1029 | |
1006 | my $self; |
1030 | my $self; |
1007 | |
1031 | |
1008 | my $hslider = new CFPlus::UI::Slider |
1032 | my $hslider = new DC::UI::Slider |
1009 | col => 0, |
1033 | c_col => 0, |
1010 | row => 1, |
1034 | c_row => 1, |
1011 | vertical => 0, |
1035 | vertical => 0, |
1012 | range => [0, 0, 1, 0.01], # HACK fix |
1036 | range => [0, 0, 1, 0.01], # HACK fix |
1013 | on_changed => sub { |
1037 | on_changed => sub { |
1014 | $self->{hpos} = $_[1]; |
1038 | $self->{hpos} = $_[1]; |
1015 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1039 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1016 | }, |
1040 | }, |
1017 | ; |
1041 | ; |
1018 | |
1042 | |
1019 | my $vslider = new CFPlus::UI::Slider |
1043 | my $vslider = new DC::UI::Slider |
1020 | col => 1, |
1044 | c_col => 1, |
1021 | row => 0, |
1045 | c_row => 0, |
1022 | vertical => 1, |
1046 | vertical => 1, |
1023 | range => [0, 0, 1, 0.01], # HACK fix |
1047 | range => [0, 0, 1, 0.01], # HACK fix |
1024 | on_changed => sub { |
1048 | on_changed => sub { |
1025 | $self->{vpos} = $_[1]; |
1049 | $self->{vpos} = $_[1]; |
1026 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1050 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
… | |
… | |
1036 | col_expand => [1, 0], |
1060 | col_expand => [1, 0], |
1037 | row_expand => [1, 0], |
1061 | row_expand => [1, 0], |
1038 | %arg, |
1062 | %arg, |
1039 | ); |
1063 | ); |
1040 | |
1064 | |
1041 | $self->{vp} = new CFPlus::UI::ViewPort |
1065 | $self->{vp} = new DC::UI::ViewPort |
1042 | col => 0, |
1066 | c_col => 0, |
1043 | row => 0, |
1067 | c_row => 0, |
1044 | expand => 1, |
1068 | expand => 1, |
1045 | scroll_x => $self->{scroll_x}, |
1069 | scroll_x => $self->{scroll_x}, |
1046 | scroll_y => $self->{scroll_y}, |
1070 | scroll_y => $self->{scroll_y}, |
1047 | on_changed => sub { |
1071 | on_changed => sub { |
1048 | my ($vp, $x, $y) = @_; |
1072 | my ($vp, $x, $y) = @_; |
… | |
… | |
1070 | my ($self, $widget) = @_; |
1094 | my ($self, $widget) = @_; |
1071 | |
1095 | |
1072 | $self->{vp}->add ($self->{child} = $widget); |
1096 | $self->{vp}->add ($self->{child} = $widget); |
1073 | } |
1097 | } |
1074 | |
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 | |
1075 | sub update_slider { |
1103 | sub update_slider { |
1076 | my ($self) = @_; |
1104 | my ($self) = @_; |
1077 | |
1105 | |
1078 | my $child = ($self->{vp} or return)->child; |
1106 | my $child = ($self->{vp} or return)->child; |
1079 | |
1107 | |
… | |
… | |
1168 | $self->SUPER::invoke_size_allocate ($w, $h) |
1196 | $self->SUPER::invoke_size_allocate ($w, $h) |
1169 | } |
1197 | } |
1170 | |
1198 | |
1171 | ############################################################################# |
1199 | ############################################################################# |
1172 | |
1200 | |
1173 | package CFPlus::UI::Frame; |
1201 | package DC::UI::Frame; |
1174 | |
1202 | |
1175 | our @ISA = CFPlus::UI::Bin::; |
1203 | our @ISA = DC::UI::Bin::; |
1176 | |
1204 | |
1177 | use CFPlus::OpenGL; |
1205 | use DC::OpenGL; |
1178 | |
1206 | |
1179 | sub new { |
1207 | sub new { |
1180 | my $class = shift; |
1208 | my $class = shift; |
1181 | |
1209 | |
1182 | $class->SUPER::new ( |
1210 | $class->SUPER::new ( |
… | |
… | |
1192 | my ($w, $h) = @$self{qw(w h)}; |
1220 | my ($w, $h) = @$self{qw(w h)}; |
1193 | |
1221 | |
1194 | glEnable GL_BLEND; |
1222 | glEnable GL_BLEND; |
1195 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1223 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1196 | glColor_premultiply @{ $self->{bg} }; |
1224 | glColor_premultiply @{ $self->{bg} }; |
1197 | |
|
|
1198 | glBegin GL_QUADS; |
|
|
1199 | glVertex 0 , 0; |
|
|
1200 | glVertex 0 , $h; |
|
|
1201 | glVertex $w, $h; |
1225 | glRect 0, 0, $w, $h; |
1202 | glVertex $w, 0; |
|
|
1203 | glEnd; |
|
|
1204 | |
|
|
1205 | glDisable GL_BLEND; |
1226 | glDisable GL_BLEND; |
1206 | } |
1227 | } |
1207 | |
1228 | |
1208 | $self->SUPER::_draw; |
1229 | $self->SUPER::_draw; |
1209 | } |
1230 | } |
1210 | |
1231 | |
1211 | ############################################################################# |
1232 | ############################################################################# |
1212 | |
1233 | |
1213 | package CFPlus::UI::FancyFrame; |
1234 | package DC::UI::FancyFrame; |
1214 | |
1235 | |
1215 | our @ISA = CFPlus::UI::Bin::; |
1236 | our @ISA = DC::UI::Bin::; |
1216 | |
1237 | |
1217 | use CFPlus::OpenGL; |
1238 | use DC::OpenGL; |
1218 | |
1239 | |
1219 | sub new { |
1240 | sub new { |
1220 | my ($class, %arg) = @_; |
1241 | my ($class, %arg) = @_; |
1221 | |
1242 | |
1222 | if ((exists $arg{label}) && !ref $arg{label}) { |
1243 | if ((exists $arg{label}) && !ref $arg{label}) { |
1223 | $arg{label} = new CFPlus::UI::Label |
1244 | $arg{label} = new DC::UI::Label |
1224 | align => 1, |
1245 | align => 1, |
1225 | valign => 0, |
1246 | valign => 0, |
1226 | text => $arg{label}, |
1247 | text => $arg{label}, |
1227 | fontsize => ($arg{border} || 0.8) * 0.75; |
1248 | fontsize => ($arg{border} || 0.8) * 0.75; |
1228 | } |
1249 | } |
… | |
… | |
1240 | |
1261 | |
1241 | sub add { |
1262 | sub add { |
1242 | my ($self, @widgets) = @_; |
1263 | my ($self, @widgets) = @_; |
1243 | |
1264 | |
1244 | $self->SUPER::add (@widgets); |
1265 | $self->SUPER::add (@widgets); |
1245 | $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; |
1266 | $self->DC::UI::Container::add ($self->{label}) if $self->{label}; |
1246 | } |
1267 | } |
1247 | |
1268 | |
1248 | sub border { |
1269 | sub border { |
1249 | int $_[0]{border} * $::FONTSIZE |
1270 | int $_[0]{border} * $::FONTSIZE |
1250 | } |
1271 | } |
… | |
… | |
1308 | } |
1329 | } |
1309 | } |
1330 | } |
1310 | |
1331 | |
1311 | ############################################################################# |
1332 | ############################################################################# |
1312 | |
1333 | |
1313 | package CFPlus::UI::Toplevel; |
1334 | package DC::UI::Toplevel; |
1314 | |
1335 | |
1315 | our @ISA = CFPlus::UI::Bin::; |
1336 | our @ISA = DC::UI::Bin::; |
1316 | |
1337 | |
1317 | use CFPlus::OpenGL; |
1338 | use DC::OpenGL; |
1318 | |
1339 | |
1319 | my $bg = |
1340 | my $bg = |
1320 | new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", |
1341 | new_from_file DC::Texture DC::find_rcfile "d1_bg.png", |
1321 | mipmap => 1, wrap => 1; |
1342 | mipmap => 1, wrap => 1; |
1322 | |
1343 | |
1323 | my @border = |
1344 | my @border = |
1324 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1345 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
1325 | 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); |
1326 | |
1347 | |
1327 | my @icon = |
1348 | my @icon = |
1328 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1349 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
1329 | qw(x1_move.png x1_resize.png); |
1350 | qw(x1_move.png x1_resize.png); |
1330 | |
1351 | |
1331 | sub new { |
1352 | sub new { |
1332 | my ($class, %arg) = @_; |
1353 | my ($class, %arg) = @_; |
1333 | |
1354 | |
… | |
… | |
1339 | min_w => 64, |
1360 | min_w => 64, |
1340 | min_h => 32, |
1361 | min_h => 32, |
1341 | %arg, |
1362 | %arg, |
1342 | ); |
1363 | ); |
1343 | |
1364 | |
1344 | $self->{title_widget} = new CFPlus::UI::Label |
1365 | $self->{title_widget} = new DC::UI::Label |
1345 | align => 0, |
1366 | align => 0, |
1346 | valign => 1, |
1367 | valign => 1, |
1347 | text => $self->{title}, |
1368 | text => $self->{title}, |
1348 | fontsize => $self->{border}, |
1369 | fontsize => $self->{border}, |
1349 | if exists $self->{title}; |
1370 | if exists $self->{title}; |
1350 | |
1371 | |
1351 | if ($self->{has_close_button}) { |
1372 | if ($self->{has_close_button}) { |
1352 | $self->{close_button} = |
1373 | $self->{close_button} = |
1353 | new CFPlus::UI::ImageButton |
1374 | new DC::UI::ImageButton |
1354 | path => 'x1_close.png', |
1375 | path => 'x1_close.png', |
1355 | on_activate => sub { $self->emit ("delete") }; |
1376 | on_activate => sub { $self->emit ("delete") }; |
1356 | |
1377 | |
1357 | $self->CFPlus::UI::Container::add ($self->{close_button}); |
1378 | $self->DC::UI::Container::add ($self->{close_button}); |
1358 | } |
1379 | } |
1359 | |
1380 | |
1360 | $self |
1381 | $self |
1361 | } |
1382 | } |
1362 | |
1383 | |
1363 | sub add { |
1384 | sub add { |
1364 | my ($self, @widgets) = @_; |
1385 | my ($self, @widgets) = @_; |
1365 | |
1386 | |
1366 | $self->SUPER::add (@widgets); |
1387 | $self->SUPER::add (@widgets); |
1367 | $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}; |
1368 | $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}; |
1369 | } |
1390 | } |
1370 | |
1391 | |
1371 | sub border { |
1392 | sub border { |
1372 | 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 |
1373 | } |
1403 | } |
1374 | |
1404 | |
1375 | sub size_request { |
1405 | sub size_request { |
1376 | my ($self) = @_; |
1406 | my ($self) = @_; |
1377 | |
1407 | |
… | |
… | |
1549 | if $self->{close_button}; |
1579 | if $self->{close_button}; |
1550 | } |
1580 | } |
1551 | |
1581 | |
1552 | ############################################################################# |
1582 | ############################################################################# |
1553 | |
1583 | |
1554 | package CFPlus::UI::Table; |
1584 | package DC::UI::Table; |
1555 | |
1585 | |
1556 | our @ISA = CFPlus::UI::Container::; |
1586 | our @ISA = DC::UI::Container::; |
1557 | |
1587 | |
1558 | use List::Util qw(max sum); |
1588 | use List::Util qw(max sum); |
1559 | |
1589 | |
1560 | use CFPlus::OpenGL; |
1590 | use DC::OpenGL; |
1561 | |
1591 | |
1562 | sub new { |
1592 | sub new { |
1563 | my $class = shift; |
1593 | my $class = shift; |
1564 | |
1594 | |
1565 | $class->SUPER::new ( |
1595 | $class->SUPER::new ( |
… | |
… | |
1571 | |
1601 | |
1572 | sub add { |
1602 | sub add { |
1573 | my ($self, @widgets) = @_; |
1603 | my ($self, @widgets) = @_; |
1574 | |
1604 | |
1575 | for my $child (@widgets) { |
1605 | for my $child (@widgets) { |
1576 | $child->{rowspan} ||= 1; |
1606 | $child->{c_rowspan} ||= 1; |
1577 | $child->{colspan} ||= 1; |
1607 | $child->{c_colspan} ||= 1; |
1578 | } |
1608 | } |
1579 | |
1609 | |
1580 | $self->SUPER::add (@widgets); |
1610 | $self->SUPER::add (@widgets); |
1581 | } |
1611 | } |
1582 | |
1612 | |
… | |
… | |
1586 | my @widgets; |
1616 | my @widgets; |
1587 | |
1617 | |
1588 | while (@_) { |
1618 | while (@_) { |
1589 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1619 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1590 | |
1620 | |
1591 | $child->{row} = $row; |
1621 | $child->{c_row} = $row; |
1592 | $child->{col} = $col; |
1622 | $child->{c_col} = $col; |
1593 | |
1623 | |
1594 | push @widgets, $child; |
1624 | push @widgets, $child; |
1595 | } |
1625 | } |
1596 | |
1626 | |
1597 | $self->add (@widgets); |
1627 | $self->add (@widgets); |
… | |
… | |
1603 | my (@w, @h); |
1633 | my (@w, @h); |
1604 | |
1634 | |
1605 | my @children = $self->children; |
1635 | my @children = $self->children; |
1606 | |
1636 | |
1607 | # first pass, columns |
1637 | # first pass, columns |
1608 | for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { |
1638 | for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) { |
1609 | my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; |
1639 | my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; |
1610 | |
1640 | |
1611 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1641 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1612 | |
1642 | |
1613 | if ($w > $sw) { |
1643 | if ($w > $sw) { |
1614 | $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1644 | $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1615 | } |
1645 | } |
1616 | } |
1646 | } |
1617 | |
1647 | |
1618 | # second pass, rows |
1648 | # second pass, rows |
1619 | for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { |
1649 | for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { |
1620 | my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; |
1650 | my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; |
1621 | |
1651 | |
1622 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1652 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1623 | |
1653 | |
1624 | if ($h > $sh) { |
1654 | if ($h > $sh) { |
1625 | $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1655 | $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1626 | } |
1656 | } |
1627 | } |
1657 | } |
1628 | |
1658 | |
1629 | (\@w, \@h) |
1659 | (\@w, \@h) |
1630 | } |
1660 | } |
… | |
… | |
1653 | @col_expand = (1) x @$ws unless @col_expand; |
1683 | @col_expand = (1) x @$ws unless @col_expand; |
1654 | my $col_expand = (sum @col_expand) || 1; |
1684 | my $col_expand = (sum @col_expand) || 1; |
1655 | |
1685 | |
1656 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1686 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1657 | |
1687 | |
1658 | CFPlus::UI::harmonize $ws; |
1688 | DC::UI::harmonize $ws; |
1659 | |
1689 | |
1660 | my @row_expand = @{$self->{row_expand}}; |
1690 | my @row_expand = @{$self->{row_expand}}; |
1661 | @row_expand = (1) x @$ws unless @row_expand; |
1691 | @row_expand = (1) x @$ws unless @row_expand; |
1662 | my $row_expand = (sum @row_expand) || 1; |
1692 | my $row_expand = (sum @row_expand) || 1; |
1663 | |
1693 | |
1664 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1694 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1665 | |
1695 | |
1666 | CFPlus::UI::harmonize $hs; |
1696 | DC::UI::harmonize $hs; |
1667 | |
1697 | |
1668 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1698 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1669 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1699 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1670 | |
1700 | |
1671 | for my $widget ($self->children) { |
1701 | for my $widget ($self->children) { |
1672 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; |
1702 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)}; |
1673 | |
1703 | |
1674 | $widget->configure ( |
1704 | $widget->configure ( |
1675 | $x[$c], $y[$r], |
1705 | $x[$c], $y[$r], |
1676 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1706 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1677 | ); |
1707 | ); |
… | |
… | |
1680 | 1 |
1710 | 1 |
1681 | } |
1711 | } |
1682 | |
1712 | |
1683 | ############################################################################# |
1713 | ############################################################################# |
1684 | |
1714 | |
1685 | package CFPlus::UI::Fixed; |
1715 | package DC::UI::Fixed; |
1686 | |
1716 | |
1687 | use List::Util qw(min max); |
1717 | use List::Util qw(min max); |
1688 | |
1718 | |
1689 | our @ISA = CFPlus::UI::Container::; |
1719 | our @ISA = DC::UI::Container::; |
1690 | |
|
|
1691 | sub add_fixed { |
|
|
1692 | my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_; |
|
|
1693 | |
|
|
1694 | $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h]; |
|
|
1695 | $self->SUPER::add ($child); |
|
|
1696 | } |
|
|
1697 | |
1720 | |
1698 | sub _scale($$$) { |
1721 | sub _scale($$$) { |
1699 | my ($mode, $val, $max) = @_; |
1722 | my ($rel, $val, $max) = @_; |
1700 | |
1723 | |
1701 | $mode eq "abs" ? $val |
1724 | $rel ? $val * $max : $val |
1702 | : $mode eq "rel" ? $val * $max |
|
|
1703 | : 0 |
|
|
1704 | } |
1725 | } |
1705 | |
1726 | |
1706 | sub size_request { |
1727 | sub size_request { |
1707 | my ($self) = @_; |
1728 | my ($self) = @_; |
1708 | |
1729 | |
1709 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
1730 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
1710 | |
1731 | |
1711 | # determine overall size by querying abs widgets |
1732 | # determine overall size by querying abs widgets |
1712 | for my $child ($self->visible_children) { |
1733 | for my $child ($self->visible_children) { |
1713 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1734 | unless ($child->{c_rel}) { |
|
|
1735 | my $x = $child->{c_x}; |
|
|
1736 | my $y = $child->{c_y}; |
1714 | |
1737 | |
1715 | if ($pos eq "abs") { |
|
|
1716 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1717 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1718 | |
|
|
1719 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
1738 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
1720 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
1739 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
1721 | } |
1740 | } |
1722 | } |
1741 | } |
1723 | |
1742 | |
1724 | my $W = $x2 - $x1; |
1743 | my $W = $x2 - $x1; |
1725 | my $H = $y2 - $y1; |
1744 | my $H = $y2 - $y1; |
1726 | |
1745 | |
1727 | # now layout remaining widgets |
1746 | # now layout remaining widgets |
1728 | for my $child ($self->visible_children) { |
1747 | for my $child ($self->visible_children) { |
1729 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
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; |
1730 | |
1751 | |
1731 | if ($pos ne "abs") { |
|
|
1732 | $x = _scale $pos, $x, $W; |
|
|
1733 | $y = _scale $pos, $x, $H; |
|
|
1734 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1735 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1736 | |
|
|
1737 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
1752 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
1738 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
1753 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
1739 | } |
1754 | } |
1740 | } |
1755 | } |
1741 | |
1756 | |
1742 | my $W = $x2 - $x1; |
1757 | my $W = $x2 - $x1; |
1743 | my $H = $y2 - $y1; |
1758 | my $H = $y2 - $y1; |
… | |
… | |
1747 | |
1762 | |
1748 | sub invoke_size_allocate { |
1763 | sub invoke_size_allocate { |
1749 | my ($self, $W, $H) = @_; |
1764 | my ($self, $W, $H) = @_; |
1750 | |
1765 | |
1751 | for my $child ($self->visible_children) { |
1766 | for my $child ($self->visible_children) { |
1752 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1767 | my $x = _scale $child->{c_rel}, $child->{c_x}, $W; |
|
|
1768 | my $y = _scale $child->{c_rel}, $child->{c_y}, $H; |
1753 | |
1769 | |
1754 | $x = _scale $pos, $x, $W; |
1770 | $x += $child->{c_halign} * $child->{req_w}; |
1755 | $y = _scale $pos, $x, $H; |
1771 | $y += $child->{c_valign} * $child->{req_h}; |
1756 | $w = _scale $size, $w, $W; |
|
|
1757 | $h = _scale $size, $h, $H; |
|
|
1758 | |
1772 | |
1759 | $child->configure ($x, $y, $w, $h); |
1773 | $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h}); |
1760 | } |
1774 | } |
1761 | |
1775 | |
1762 | 1 |
1776 | 1 |
1763 | } |
1777 | } |
1764 | |
1778 | |
1765 | ############################################################################# |
1779 | ############################################################################# |
1766 | |
1780 | |
1767 | package CFPlus::UI::Box; |
1781 | package DC::UI::Box; |
1768 | |
1782 | |
1769 | our @ISA = CFPlus::UI::Container::; |
1783 | our @ISA = DC::UI::Container::; |
1770 | |
1784 | |
1771 | sub size_request { |
1785 | sub size_request { |
1772 | my ($self) = @_; |
1786 | my ($self) = @_; |
|
|
1787 | |
|
|
1788 | my @children = $self->visible_children; |
1773 | |
1789 | |
1774 | $self->{vertical} |
1790 | $self->{vertical} |
1775 | ? ( |
1791 | ? ( |
1776 | (List::Util::max map $_->{req_w}, @{$self->{children}}), |
1792 | (List::Util::max map $_->{req_w}, @children), |
1777 | (List::Util::sum map $_->{req_h}, @{$self->{children}}), |
1793 | (List::Util::sum map $_->{req_h}, @children), |
1778 | ) |
1794 | ) |
1779 | : ( |
1795 | : ( |
1780 | (List::Util::sum map $_->{req_w}, @{$self->{children}}), |
1796 | (List::Util::sum map $_->{req_w}, @children), |
1781 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1797 | (List::Util::max map $_->{req_h}, @children), |
1782 | ) |
1798 | ) |
1783 | } |
1799 | } |
1784 | |
1800 | |
1785 | sub invoke_size_allocate { |
1801 | sub invoke_size_allocate { |
1786 | my ($self, $w, $h) = @_; |
1802 | my ($self, $w, $h) = @_; |
… | |
… | |
1807 | $req[$_] += $space * $children[$_]{expand} |
1823 | $req[$_] += $space * $children[$_]{expand} |
1808 | for 0 .. $#children; |
1824 | for 0 .. $#children; |
1809 | } |
1825 | } |
1810 | } |
1826 | } |
1811 | |
1827 | |
1812 | CFPlus::UI::harmonize \@req; |
1828 | DC::UI::harmonize \@req; |
1813 | |
1829 | |
1814 | my $pos = 0; |
1830 | my $pos = 0; |
1815 | for (0 .. $#children) { |
1831 | for (0 .. $#children) { |
1816 | my $alloc = $req[$_]; |
1832 | my $alloc = $req[$_]; |
1817 | $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)); |
… | |
… | |
1822 | 1 |
1838 | 1 |
1823 | } |
1839 | } |
1824 | |
1840 | |
1825 | ############################################################################# |
1841 | ############################################################################# |
1826 | |
1842 | |
1827 | package CFPlus::UI::HBox; |
1843 | package DC::UI::HBox; |
1828 | |
1844 | |
1829 | our @ISA = CFPlus::UI::Box::; |
1845 | our @ISA = DC::UI::Box::; |
1830 | |
1846 | |
1831 | sub new { |
1847 | sub new { |
1832 | my $class = shift; |
1848 | my $class = shift; |
1833 | |
1849 | |
1834 | $class->SUPER::new ( |
1850 | $class->SUPER::new ( |
… | |
… | |
1837 | ) |
1853 | ) |
1838 | } |
1854 | } |
1839 | |
1855 | |
1840 | ############################################################################# |
1856 | ############################################################################# |
1841 | |
1857 | |
1842 | package CFPlus::UI::VBox; |
1858 | package DC::UI::VBox; |
1843 | |
1859 | |
1844 | our @ISA = CFPlus::UI::Box::; |
1860 | our @ISA = DC::UI::Box::; |
1845 | |
1861 | |
1846 | sub new { |
1862 | sub new { |
1847 | my $class = shift; |
1863 | my $class = shift; |
1848 | |
1864 | |
1849 | $class->SUPER::new ( |
1865 | $class->SUPER::new ( |
… | |
… | |
1852 | ) |
1868 | ) |
1853 | } |
1869 | } |
1854 | |
1870 | |
1855 | ############################################################################# |
1871 | ############################################################################# |
1856 | |
1872 | |
1857 | package CFPlus::UI::Label; |
1873 | package DC::UI::Label; |
1858 | |
1874 | |
1859 | our @ISA = CFPlus::UI::DrawBG::; |
1875 | our @ISA = DC::UI::DrawBG::; |
1860 | |
1876 | |
1861 | use CFPlus::OpenGL; |
1877 | use DC::OpenGL; |
1862 | |
1878 | |
1863 | sub new { |
1879 | sub new { |
1864 | my ($class, %arg) = @_; |
1880 | my ($class, %arg) = @_; |
1865 | |
1881 | |
1866 | my $self = $class->SUPER::new ( |
1882 | my $self = $class->SUPER::new ( |
… | |
… | |
1871 | #text => initial text |
1887 | #text => initial text |
1872 | #markup => initial narkup |
1888 | #markup => initial narkup |
1873 | #max_w => maximum pixel width |
1889 | #max_w => maximum pixel width |
1874 | #style => 0, # render flags |
1890 | #style => 0, # render flags |
1875 | ellipsise => 3, # end |
1891 | ellipsise => 3, # end |
1876 | layout => (new CFPlus::Layout), |
1892 | layout => (new DC::Layout), |
1877 | fontsize => 1, |
1893 | fontsize => 1, |
1878 | align => -1, |
1894 | align => -1, |
1879 | valign => -1, |
1895 | valign => -1, |
1880 | padding_x => 2, |
1896 | padding_x => 2, |
1881 | padding_y => 2, |
1897 | padding_y => 2, |
1882 | can_events => 0, |
1898 | can_events => 0, |
1883 | %arg |
1899 | %arg |
1884 | ); |
1900 | ); |
1885 | |
1901 | |
1886 | if (exists $self->{template}) { |
1902 | if (exists $self->{template}) { |
1887 | my $layout = new CFPlus::Layout; |
1903 | my $layout = new DC::Layout; |
1888 | $layout->set_text (delete $self->{template}); |
1904 | $layout->set_text (delete $self->{template}); |
1889 | $self->{template} = $layout; |
1905 | $self->{template} = $layout; |
1890 | } |
1906 | } |
1891 | |
1907 | |
1892 | if (exists $self->{markup}) { |
1908 | if (exists $self->{markup}) { |
… | |
… | |
1910 | |
1926 | |
1911 | delete $self->{ox}; |
1927 | delete $self->{ox}; |
1912 | $self->SUPER::realloc; |
1928 | $self->SUPER::realloc; |
1913 | } |
1929 | } |
1914 | |
1930 | |
|
|
1931 | sub clear { |
|
|
1932 | my ($self) = @_; |
|
|
1933 | |
|
|
1934 | $self->set_text (""); |
|
|
1935 | } |
|
|
1936 | |
1915 | sub set_text { |
1937 | sub set_text { |
1916 | my ($self, $text) = @_; |
1938 | my ($self, $text) = @_; |
1917 | |
1939 | |
1918 | return if $self->{text} eq "T$text"; |
1940 | return if $self->{text} eq "T$text"; |
1919 | $self->{text} = "T$text"; |
1941 | $self->{text} = "T$text"; |
… | |
… | |
1942 | |
1964 | |
1943 | sub size_request { |
1965 | sub size_request { |
1944 | my ($self) = @_; |
1966 | my ($self) = @_; |
1945 | |
1967 | |
1946 | $self->{size_req} ||= do { |
1968 | $self->{size_req} ||= do { |
|
|
1969 | my ($max_w, $max_h) = $self->get_max_wh; |
|
|
1970 | |
1947 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1971 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1948 | $self->{layout}->set_width ($self->{max_w} || -1); |
1972 | $self->{layout}->set_width ($self->{max_w} || $max_w || -1); |
1949 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1973 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1950 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1974 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1951 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1975 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1952 | |
1976 | |
1953 | my ($w, $h) = $self->{layout}->size; |
1977 | my ($w, $h) = $self->{layout}->size; |
… | |
… | |
2025 | : ($self->{w} - $size->[0]) * 0.5); |
2049 | : ($self->{w} - $size->[0]) * 0.5); |
2026 | |
2050 | |
2027 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2051 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2028 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
2052 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
2029 | : ($self->{h} - $size->[1]) * 0.5); |
2053 | : ($self->{h} - $size->[1]) * 0.5); |
|
|
2054 | |
|
|
2055 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2030 | }; |
2056 | }; |
2031 | |
2057 | |
2032 | # unless ($self->{list}) { |
2058 | # unless ($self->{list}) { |
2033 | # $self->{list} = CFPlus::OpenGL::glGenList; |
2059 | # $self->{list} = DC::OpenGL::glGenList; |
2034 | # CFPlus::OpenGL::glNewList $self->{list}; |
2060 | # DC::OpenGL::glNewList $self->{list}; |
2035 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2061 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2036 | # CFPlus::OpenGL::glEndList; |
2062 | # DC::OpenGL::glEndList; |
2037 | # } |
2063 | # } |
2038 | # |
2064 | # |
2039 | # CFPlus::OpenGL::glCallList $self->{list}; |
2065 | # DC::OpenGL::glCallList $self->{list}; |
2040 | |
2066 | |
2041 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2067 | $self->{layout}->draw; |
2042 | } |
2068 | } |
2043 | |
2069 | |
2044 | #sub destroy { |
2070 | #sub destroy { |
2045 | # my ($self) = @_; |
2071 | # my ($self) = @_; |
2046 | # |
2072 | # |
2047 | # CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
2073 | # DC::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
2048 | # |
2074 | # |
2049 | # $self->SUPER::destroy; |
2075 | # $self->SUPER::destroy; |
2050 | #} |
2076 | #} |
2051 | |
2077 | |
2052 | ############################################################################# |
2078 | ############################################################################# |
2053 | |
2079 | |
2054 | package CFPlus::UI::EntryBase; |
2080 | package DC::UI::EntryBase; |
2055 | |
2081 | |
2056 | our @ISA = CFPlus::UI::Label::; |
2082 | our @ISA = DC::UI::Label::; |
2057 | |
2083 | |
2058 | use CFPlus::OpenGL; |
2084 | use DC::OpenGL; |
2059 | |
2085 | |
2060 | sub new { |
2086 | sub new { |
2061 | my $class = shift; |
2087 | my $class = shift; |
2062 | |
2088 | |
2063 | $class->SUPER::new ( |
2089 | $class->SUPER::new ( |
2064 | fg => [1, 1, 1], |
2090 | fg => [1, 1, 1], |
2065 | bg => [0, 0, 0, 0.2], |
2091 | bg => [0, 0, 0, 0.2], |
|
|
2092 | outline => [0.6, 0.3, 0.1], |
2066 | active_bg => [1, 1, 1, 0.5], |
2093 | active_bg => [0, 0, 1, .2], |
2067 | active_fg => [0, 0, 0], |
2094 | active_fg => [1, 1, 1], |
|
|
2095 | active_outline => [1, 1, 0], |
2068 | can_hover => 1, |
2096 | can_hover => 1, |
2069 | can_focus => 1, |
2097 | can_focus => 1, |
2070 | valign => 0, |
2098 | valign => 0, |
2071 | can_events => 1, |
2099 | can_events => 1, |
2072 | ellipsise => 0, |
2100 | ellipsise => 0, |
|
|
2101 | padding_x => 4, |
|
|
2102 | padding_y => 2, |
2073 | #text => ... |
2103 | #text => ... |
2074 | #hidden => "*", |
2104 | #hidden => "*", |
2075 | @_ |
2105 | @_ |
2076 | ) |
2106 | ) |
2077 | } |
2107 | } |
… | |
… | |
2128 | |
2158 | |
2129 | if ($uni == 8) { |
2159 | if ($uni == 8) { |
2130 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2160 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2131 | } elsif ($uni == 127) { |
2161 | } elsif ($uni == 127) { |
2132 | substr $text, $self->{cursor}, 1, ""; |
2162 | substr $text, $self->{cursor}, 1, ""; |
2133 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
2163 | } elsif ($sym == DC::SDLK_LEFT) { |
2134 | --$self->{cursor} if $self->{cursor}; |
2164 | --$self->{cursor} if $self->{cursor}; |
2135 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
2165 | } elsif ($sym == DC::SDLK_RIGHT) { |
2136 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2166 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2137 | } elsif ($sym == CFPlus::SDLK_HOME) { |
2167 | } elsif ($sym == DC::SDLK_HOME) { |
2138 | # what a hack |
2168 | # what a hack |
2139 | $self->{cursor} = |
2169 | $self->{cursor} = |
2140 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2170 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2141 | ? length $1 |
2171 | ? length $1 |
2142 | : 0; |
2172 | : 0; |
2143 | } elsif ($sym == CFPlus::SDLK_END) { |
2173 | } elsif ($sym == DC::SDLK_END) { |
2144 | # uh, again |
2174 | # uh, again |
2145 | $self->{cursor} = |
2175 | $self->{cursor} = |
2146 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2176 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2147 | ? $self->{cursor} + length $1 |
2177 | ? $self->{cursor} + length $1 |
2148 | : length $self->{text}; |
2178 | : length $self->{text}; |
… | |
… | |
2212 | glColor_premultiply @{$self->{bg}}; |
2242 | glColor_premultiply @{$self->{bg}}; |
2213 | } |
2243 | } |
2214 | |
2244 | |
2215 | glEnable GL_BLEND; |
2245 | glEnable GL_BLEND; |
2216 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2246 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2217 | glBegin GL_QUADS; |
|
|
2218 | glVertex 0 , 0; |
|
|
2219 | glVertex 0 , $self->{h}; |
|
|
2220 | glVertex $self->{w}, $self->{h}; |
2247 | glRect 0, 0, $self->{w}, $self->{h}; |
2221 | glVertex $self->{w}, 0; |
|
|
2222 | glEnd; |
|
|
2223 | glDisable GL_BLEND; |
2248 | glDisable GL_BLEND; |
2224 | |
2249 | |
2225 | $self->SUPER::_draw; |
2250 | $self->SUPER::_draw; |
2226 | |
2251 | |
2227 | #TODO: force update every cursor change :( |
2252 | #TODO: force update every cursor change :( |
… | |
… | |
2229 | |
2254 | |
2230 | unless (exists $self->{cur_h}) { |
2255 | unless (exists $self->{cur_h}) { |
2231 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2256 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2232 | utf8::encode $text; |
2257 | utf8::encode $text; |
2233 | |
2258 | |
2234 | @$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); |
2235 | } |
2260 | } |
2236 | |
2261 | |
|
|
2262 | glColor_premultiply @{$self->{active_fg}}; |
2237 | glBegin GL_LINES; |
2263 | glBegin GL_LINES; |
2238 | 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}; |
2239 | 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}; |
2240 | 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; |
2241 | } |
2281 | } |
2242 | } |
2282 | } |
2243 | |
2283 | |
2244 | ############################################################################# |
2284 | ############################################################################# |
2245 | |
2285 | |
2246 | package CFPlus::UI::Entry; |
2286 | package DC::UI::Entry; |
2247 | |
2287 | |
2248 | our @ISA = CFPlus::UI::EntryBase::; |
2288 | our @ISA = DC::UI::EntryBase::; |
2249 | |
2289 | |
2250 | use CFPlus::OpenGL; |
2290 | use DC::OpenGL; |
2251 | |
2291 | |
2252 | sub invoke_key_down { |
2292 | sub invoke_key_down { |
2253 | my ($self, $ev) = @_; |
2293 | my ($self, $ev) = @_; |
2254 | |
2294 | |
2255 | my $sym = $ev->{sym}; |
2295 | my $sym = $ev->{sym}; |
… | |
… | |
2261 | $self->{history_pointer} = -1; |
2301 | $self->{history_pointer} = -1; |
2262 | $self->{history_saveback} = ''; |
2302 | $self->{history_saveback} = ''; |
2263 | $self->emit (activate => $txt); |
2303 | $self->emit (activate => $txt); |
2264 | $self->update; |
2304 | $self->update; |
2265 | |
2305 | |
2266 | } elsif ($sym == CFPlus::SDLK_UP) { |
2306 | } elsif ($sym == DC::SDLK_UP) { |
2267 | if ($self->{history_pointer} < 0) { |
2307 | if ($self->{history_pointer} < 0) { |
2268 | $self->{history_saveback} = $self->get_text; |
2308 | $self->{history_saveback} = $self->get_text; |
2269 | } |
2309 | } |
2270 | if (@{$self->{history} || []} > 0) { |
2310 | if (@{$self->{history} || []} > 0) { |
2271 | $self->{history_pointer}++; |
2311 | $self->{history_pointer}++; |
… | |
… | |
2273 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2313 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2274 | } |
2314 | } |
2275 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2315 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2276 | } |
2316 | } |
2277 | |
2317 | |
2278 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2318 | } elsif ($sym == DC::SDLK_DOWN) { |
2279 | $self->{history_pointer}--; |
2319 | $self->{history_pointer}--; |
2280 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2320 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2281 | |
2321 | |
2282 | if ($self->{history_pointer} >= 0) { |
2322 | if ($self->{history_pointer} >= 0) { |
2283 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2323 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
… | |
… | |
2292 | 1 |
2332 | 1 |
2293 | } |
2333 | } |
2294 | |
2334 | |
2295 | ############################################################################# |
2335 | ############################################################################# |
2296 | |
2336 | |
2297 | package CFPlus::UI::TextEdit; |
2337 | package DC::UI::TextEdit; |
2298 | |
2338 | |
2299 | our @ISA = CFPlus::UI::EntryBase::; |
2339 | our @ISA = DC::UI::EntryBase::; |
2300 | |
2340 | |
2301 | 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 | } |
2302 | |
2352 | |
2303 | sub move_cursor_ver { |
2353 | sub move_cursor_ver { |
2304 | my ($self, $dy) = @_; |
2354 | my ($self, $dy) = @_; |
2305 | |
2355 | |
2306 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
2356 | my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
|
|
2357 | warn "cursor $self->{cursor} => $x $line\n";#d# |
2307 | |
2358 | |
2308 | $y += $dy; |
2359 | $line += $dy; |
2309 | |
2360 | |
2310 | if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { |
2361 | if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) { |
|
|
2362 | warn "index $x $line => $index\n";#d# |
2311 | $self->{cursor} = $index; |
2363 | $self->{cursor} = $index; |
2312 | delete $self->{cur_h}; |
2364 | delete $self->{cur_h}; |
2313 | $self->update; |
2365 | $self->update; |
2314 | return; |
2366 | return; |
2315 | } |
2367 | } |
… | |
… | |
2318 | sub invoke_key_down { |
2370 | sub invoke_key_down { |
2319 | my ($self, $ev) = @_; |
2371 | my ($self, $ev) = @_; |
2320 | |
2372 | |
2321 | my $sym = $ev->{sym}; |
2373 | my $sym = $ev->{sym}; |
2322 | |
2374 | |
2323 | if ($sym == CFPlus::SDLK_UP) { |
2375 | if ($sym == DC::SDLK_UP) { |
2324 | $self->move_cursor_ver (-1); |
2376 | $self->move_cursor_ver (-1); |
2325 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2377 | } elsif ($sym == DC::SDLK_DOWN) { |
2326 | $self->move_cursor_ver (+1); |
2378 | $self->move_cursor_ver (+1); |
2327 | } else { |
2379 | } else { |
2328 | return $self->SUPER::invoke_key_down ($ev) |
2380 | return $self->SUPER::invoke_key_down ($ev) |
2329 | } |
2381 | } |
2330 | |
2382 | |
2331 | 1 |
2383 | 1 |
2332 | } |
2384 | } |
2333 | |
2385 | |
2334 | ############################################################################# |
2386 | ############################################################################# |
2335 | |
2387 | |
2336 | package CFPlus::UI::Button; |
2388 | package DC::UI::ButtonBin; |
2337 | |
2389 | |
2338 | our @ISA = CFPlus::UI::Label::; |
2390 | our @ISA = DC::UI::Bin::; |
2339 | |
2391 | |
2340 | use CFPlus::OpenGL; |
2392 | use DC::OpenGL; |
2341 | |
2393 | |
2342 | my @tex = |
2394 | my @tex = |
2343 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2395 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2344 | qw(b1_button_inactive.png b1_button_active.png); |
2396 | qw(b1_button_inactive.png b1_button_active.png); |
2345 | |
2397 | |
2346 | sub new { |
2398 | sub new { |
2347 | my $class = shift; |
2399 | my $class = shift; |
2348 | |
2400 | |
2349 | $class->SUPER::new ( |
2401 | $class->SUPER::new ( |
|
|
2402 | can_hover => 1, |
|
|
2403 | align => 0, |
|
|
2404 | valign => 0, |
|
|
2405 | can_events => 1, |
|
|
2406 | @_ |
|
|
2407 | ) |
|
|
2408 | } |
|
|
2409 | |
|
|
2410 | sub invoke_button_up { |
|
|
2411 | my ($self, $ev, $x, $y) = @_; |
|
|
2412 | |
|
|
2413 | $self->emit ("activate") |
|
|
2414 | if $x >= 0 && $x < $self->{w} |
|
|
2415 | && $y >= 0 && $y < $self->{h}; |
|
|
2416 | |
|
|
2417 | 1 |
|
|
2418 | } |
|
|
2419 | |
|
|
2420 | sub _draw { |
|
|
2421 | my ($self) = @_; |
|
|
2422 | |
|
|
2423 | glEnable GL_TEXTURE_2D; |
|
|
2424 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2425 | glColor 0, 0, 0, 1; |
|
|
2426 | |
|
|
2427 | my $tex = $tex[$GRAB == $self]; |
|
|
2428 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
|
|
2429 | |
|
|
2430 | glDisable GL_TEXTURE_2D; |
|
|
2431 | |
|
|
2432 | $self->SUPER::_draw; |
|
|
2433 | } |
|
|
2434 | |
|
|
2435 | ############################################################################# |
|
|
2436 | |
|
|
2437 | package DC::UI::Button; |
|
|
2438 | |
|
|
2439 | our @ISA = DC::UI::Label::; |
|
|
2440 | |
|
|
2441 | use DC::OpenGL; |
|
|
2442 | |
|
|
2443 | my @tex = |
|
|
2444 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
|
|
2445 | qw(b1_button_inactive.png b1_button_active.png); |
|
|
2446 | |
|
|
2447 | sub new { |
|
|
2448 | my $class = shift; |
|
|
2449 | |
|
|
2450 | $class->SUPER::new ( |
2350 | padding_x => 4, |
2451 | padding_x => 8, |
2351 | padding_y => 4, |
2452 | padding_y => 4, |
2352 | fg => [1.0, 1.0, 1.0], |
2453 | fg => [1.0, 1.0, 1.0], |
2353 | active_fg => [0.8, 0.8, 0.8], |
2454 | active_fg => [0.8, 0.8, 0.8], |
2354 | can_hover => 1, |
2455 | can_hover => 1, |
2355 | align => 0, |
2456 | align => 0, |
… | |
… | |
2386 | $self->SUPER::_draw; |
2487 | $self->SUPER::_draw; |
2387 | } |
2488 | } |
2388 | |
2489 | |
2389 | ############################################################################# |
2490 | ############################################################################# |
2390 | |
2491 | |
2391 | package CFPlus::UI::CheckBox; |
2492 | package DC::UI::CheckBox; |
2392 | |
2493 | |
2393 | our @ISA = CFPlus::UI::DrawBG::; |
2494 | our @ISA = DC::UI::DrawBG::; |
2394 | |
2495 | |
2395 | my @tex = |
2496 | my @tex = |
2396 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2497 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2397 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2498 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2398 | |
2499 | |
2399 | use CFPlus::OpenGL; |
2500 | use DC::OpenGL; |
2400 | |
2501 | |
2401 | sub new { |
2502 | sub new { |
2402 | my $class = shift; |
2503 | my $class = shift; |
2403 | |
2504 | |
2404 | $class->SUPER::new ( |
2505 | $class->SUPER::new ( |
… | |
… | |
2444 | sub _draw { |
2545 | sub _draw { |
2445 | my ($self) = @_; |
2546 | my ($self) = @_; |
2446 | |
2547 | |
2447 | $self->SUPER::_draw; |
2548 | $self->SUPER::_draw; |
2448 | |
2549 | |
2449 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
2550 | glTranslate $self->{padding_x}, $self->{padding_y}, 0; |
2450 | |
2551 | |
2451 | my ($w, $h) = @$self{qw(w h)}; |
2552 | my ($w, $h) = @$self{qw(w h)}; |
2452 | |
2553 | |
2453 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2554 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2454 | |
2555 | |
… | |
… | |
2461 | glDisable GL_TEXTURE_2D; |
2562 | glDisable GL_TEXTURE_2D; |
2462 | } |
2563 | } |
2463 | |
2564 | |
2464 | ############################################################################# |
2565 | ############################################################################# |
2465 | |
2566 | |
2466 | package CFPlus::UI::Image; |
2567 | package DC::UI::Image; |
2467 | |
2568 | |
2468 | our @ISA = CFPlus::UI::Base::; |
2569 | our @ISA = DC::UI::Base::; |
2469 | |
2570 | |
2470 | use CFPlus::OpenGL; |
2571 | use DC::OpenGL; |
2471 | |
2572 | |
2472 | our %texture_cache; |
2573 | our %texture_cache; |
2473 | |
2574 | |
2474 | sub new { |
2575 | sub new { |
2475 | my $class = shift; |
2576 | my $class = shift; |
2476 | |
2577 | |
2477 | my $self = $class->SUPER::new ( |
2578 | my $self = $class->SUPER::new ( |
2478 | can_events => 0, |
2579 | can_events => 0, |
|
|
2580 | scale => 1, |
2479 | @_, |
2581 | @_, |
2480 | ); |
2582 | ); |
2481 | |
2583 | |
2482 | $self->{path} || $self->{tex} |
2584 | $self->{path} || $self->{tex} |
2483 | or Carp::croak "'path' or 'tex' attributes required"; |
2585 | or Carp::croak "'path' or 'tex' attributes required"; |
2484 | |
2586 | |
2485 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2587 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2486 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2588 | new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; |
2487 | |
2589 | |
2488 | CFPlus::weaken $texture_cache{$self->{path}}; |
2590 | DC::weaken $texture_cache{$self->{path}}; |
2489 | |
2591 | |
2490 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2592 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2491 | |
2593 | |
2492 | $self |
2594 | $self |
2493 | } |
2595 | } |
2494 | |
2596 | |
2495 | sub STORABLE_freeze { |
2597 | sub STORABLE_freeze { |
2496 | my ($self, $cloning) = @_; |
2598 | my ($self, $cloning) = @_; |
2497 | |
2599 | |
2498 | $self->{path} |
2600 | $self->{path} |
2499 | or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; |
2601 | or die "cannot serialise DC::UI::Image on non-loadable images\n"; |
2500 | |
2602 | |
2501 | $self->{path} |
2603 | $self->{path} |
2502 | } |
2604 | } |
2503 | |
2605 | |
2504 | sub STORABLE_attach { |
2606 | sub STORABLE_attach { |
… | |
… | |
2508 | } |
2610 | } |
2509 | |
2611 | |
2510 | sub size_request { |
2612 | sub size_request { |
2511 | my ($self) = @_; |
2613 | my ($self) = @_; |
2512 | |
2614 | |
2513 | ($self->{tex}{w}, $self->{tex}{h}) |
2615 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2514 | } |
2616 | } |
2515 | |
2617 | |
2516 | sub _draw { |
2618 | sub _draw { |
2517 | my ($self) = @_; |
2619 | my ($self) = @_; |
2518 | |
2620 | |
… | |
… | |
2528 | } |
2630 | } |
2529 | |
2631 | |
2530 | glEnable GL_TEXTURE_2D; |
2632 | glEnable GL_TEXTURE_2D; |
2531 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2633 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2532 | |
2634 | |
2533 | $tex->draw_quad (0, 0, $w, $h); |
2635 | $tex->draw_quad_alpha (0, 0, $w, $h); |
2534 | |
2636 | |
2535 | glDisable GL_TEXTURE_2D; |
2637 | glDisable GL_TEXTURE_2D; |
2536 | } |
2638 | } |
2537 | |
2639 | |
2538 | ############################################################################# |
2640 | ############################################################################# |
2539 | |
2641 | |
2540 | package CFPlus::UI::ImageButton; |
2642 | package DC::UI::ImageButton; |
2541 | |
2643 | |
2542 | our @ISA = CFPlus::UI::Image::; |
2644 | our @ISA = DC::UI::Image::; |
2543 | |
2645 | |
2544 | use CFPlus::OpenGL; |
2646 | use DC::OpenGL; |
2545 | |
2647 | |
2546 | my %textures; |
2648 | my %textures; |
2547 | |
2649 | |
2548 | sub new { |
2650 | sub new { |
2549 | my $class = shift; |
2651 | my $class = shift; |
… | |
… | |
2559 | can_events => 1, |
2661 | can_events => 1, |
2560 | @_ |
2662 | @_ |
2561 | ); |
2663 | ); |
2562 | } |
2664 | } |
2563 | |
2665 | |
|
|
2666 | sub invoke_button_down { |
|
|
2667 | my ($self, $ev, $x, $y) = @_; |
|
|
2668 | |
|
|
2669 | 1 |
|
|
2670 | } |
|
|
2671 | |
2564 | sub invoke_button_up { |
2672 | sub invoke_button_up { |
2565 | my ($self, $ev, $x, $y) = @_; |
2673 | my ($self, $ev, $x, $y) = @_; |
2566 | |
2674 | |
2567 | $self->emit ("activate") |
2675 | $self->emit ("activate") |
2568 | if $x >= 0 && $x < $self->{w} |
2676 | if $x >= 0 && $x < $self->{w} |
… | |
… | |
2571 | 1 |
2679 | 1 |
2572 | } |
2680 | } |
2573 | |
2681 | |
2574 | ############################################################################# |
2682 | ############################################################################# |
2575 | |
2683 | |
2576 | package CFPlus::UI::VGauge; |
2684 | package DC::UI::VGauge; |
2577 | |
2685 | |
2578 | our @ISA = CFPlus::UI::Base::; |
2686 | our @ISA = DC::UI::Base::; |
2579 | |
2687 | |
2580 | use List::Util qw(min max); |
2688 | use List::Util qw(min max); |
2581 | |
2689 | |
2582 | use CFPlus::OpenGL; |
2690 | use DC::OpenGL; |
2583 | |
2691 | |
2584 | my %tex = ( |
2692 | my %tex = ( |
2585 | food => [ |
2693 | food => [ |
2586 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2694 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2587 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2695 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2588 | ], |
2696 | ], |
2589 | grace => [ |
2697 | grace => [ |
2590 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2698 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2591 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2699 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2592 | ], |
2700 | ], |
2593 | hp => [ |
2701 | hp => [ |
2594 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2702 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2595 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2703 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2596 | ], |
2704 | ], |
2597 | mana => [ |
2705 | mana => [ |
2598 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2706 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2599 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2707 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2600 | ], |
2708 | ], |
2601 | ); |
2709 | ); |
2602 | |
2710 | |
2603 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
2711 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
… | |
… | |
2706 | glDisable GL_TEXTURE_2D; |
2814 | glDisable GL_TEXTURE_2D; |
2707 | } |
2815 | } |
2708 | |
2816 | |
2709 | ############################################################################# |
2817 | ############################################################################# |
2710 | |
2818 | |
|
|
2819 | package DC::UI::Progress; |
|
|
2820 | |
|
|
2821 | our @ISA = DC::UI::Label::; |
|
|
2822 | |
|
|
2823 | use DC::OpenGL; |
|
|
2824 | |
|
|
2825 | sub new { |
|
|
2826 | my ($class, %arg) = @_; |
|
|
2827 | |
|
|
2828 | my $self = $class->SUPER::new ( |
|
|
2829 | fg => [1, 1, 1], |
|
|
2830 | bg => [0, 0, 1, 0.2], |
|
|
2831 | bar => [0.7, 0.5, 0.1, 0.8], |
|
|
2832 | outline => [0.4, 0.3, 0], |
|
|
2833 | fontsize => 0.9, |
|
|
2834 | valign => 0, |
|
|
2835 | align => 0, |
|
|
2836 | can_events => 1, |
|
|
2837 | ellipsise => 1, |
|
|
2838 | label => "%d%%", |
|
|
2839 | %arg, |
|
|
2840 | ); |
|
|
2841 | |
|
|
2842 | $self->set_value ($arg{value} || -1); |
|
|
2843 | |
|
|
2844 | $self |
|
|
2845 | } |
|
|
2846 | |
|
|
2847 | sub set_label { |
|
|
2848 | my ($self, $label) = @_; |
|
|
2849 | |
|
|
2850 | return if $self->{label} eq $label; |
|
|
2851 | $self->{label} = $label; |
|
|
2852 | |
|
|
2853 | $self->DC::UI::Progress::set_value (0 + delete $self->{value}); |
|
|
2854 | } |
|
|
2855 | |
|
|
2856 | sub set_value { |
|
|
2857 | my ($self, $value) = @_; |
|
|
2858 | |
|
|
2859 | if ($self->{value} ne $value) { |
|
|
2860 | $self->{value} = $value; |
|
|
2861 | |
|
|
2862 | if ($value < 0) { |
|
|
2863 | $self->set_text ("-"); |
|
|
2864 | } else { |
|
|
2865 | $self->set_text (sprintf $self->{label}, $value * 100); |
|
|
2866 | } |
|
|
2867 | |
|
|
2868 | $self->update; |
|
|
2869 | } |
|
|
2870 | } |
|
|
2871 | |
|
|
2872 | sub _draw { |
|
|
2873 | my ($self) = @_; |
|
|
2874 | |
|
|
2875 | glEnable GL_BLEND; |
|
|
2876 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2877 | |
|
|
2878 | if ($self->{value} >= 0) { |
|
|
2879 | my $s = int 2 + ($self->{w} - 4) * $self->{value}; |
|
|
2880 | |
|
|
2881 | glColor_premultiply @{$self->{bar}}; |
|
|
2882 | glRect 2, 2, $s, $self->{h} - 2; |
|
|
2883 | glColor_premultiply @{$self->{bg}}; |
|
|
2884 | glRect $s, 2, $self->{w} - 2, $self->{h} - 2; |
|
|
2885 | } |
|
|
2886 | |
|
|
2887 | glColor_premultiply @{$self->{outline}}; |
|
|
2888 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2889 | |
|
|
2890 | glDisable GL_BLEND; |
|
|
2891 | |
|
|
2892 | { |
|
|
2893 | local $self->{bg}; # do not draw background |
|
|
2894 | $self->SUPER::_draw; |
|
|
2895 | } |
|
|
2896 | } |
|
|
2897 | |
|
|
2898 | ############################################################################# |
|
|
2899 | |
|
|
2900 | package DC::UI::ExperienceProgress; |
|
|
2901 | |
|
|
2902 | our @ISA = DC::UI::Progress::; |
|
|
2903 | |
|
|
2904 | sub new { |
|
|
2905 | my ($class, %arg) = @_; |
|
|
2906 | |
|
|
2907 | my $self = $class->SUPER::new ( |
|
|
2908 | tooltip => sub { |
|
|
2909 | my ($self) = @_; |
|
|
2910 | |
|
|
2911 | sprintf "level %d\n%s points\n%s next level\n%s to go", |
|
|
2912 | $self->{lvl}, |
|
|
2913 | ::formsep ($self->{exp}), |
|
|
2914 | ::formsep ($self->{nxt}), |
|
|
2915 | ::formsep ($self->{nxt} - $self->{exp}), |
|
|
2916 | }, |
|
|
2917 | %arg |
|
|
2918 | ); |
|
|
2919 | |
|
|
2920 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
|
|
2921 | if $::CONN; |
|
|
2922 | |
|
|
2923 | $self |
|
|
2924 | } |
|
|
2925 | |
|
|
2926 | sub DESTROY { |
|
|
2927 | my ($self) = @_; |
|
|
2928 | |
|
|
2929 | delete $::CONN->{on_exp_update}{$self+0} |
|
|
2930 | if $::CONN; |
|
|
2931 | |
|
|
2932 | $self->SUPER::DESTROY; |
|
|
2933 | } |
|
|
2934 | |
|
|
2935 | sub set_value { |
|
|
2936 | my ($self, $lvl, $exp) = @_; |
|
|
2937 | |
|
|
2938 | $self->{lvl} = $lvl; |
|
|
2939 | $self->{exp} = $exp; |
|
|
2940 | |
|
|
2941 | my $v = -1; |
|
|
2942 | |
|
|
2943 | if ($::CONN && (my $table = $::CONN->{exp_table})) { |
|
|
2944 | my $l0 = $table->[$lvl - 1]; |
|
|
2945 | my $l1 = $table->[$lvl]; |
|
|
2946 | |
|
|
2947 | $self->{nxt} = $l1; |
|
|
2948 | |
|
|
2949 | $v = ($exp - $l0) / ($l1 - $l0); |
|
|
2950 | } |
|
|
2951 | |
|
|
2952 | $self->SUPER::set_value ($v); |
|
|
2953 | } |
|
|
2954 | |
|
|
2955 | ############################################################################# |
|
|
2956 | |
2711 | package CFPlus::UI::Gauge; |
2957 | package DC::UI::Gauge; |
2712 | |
2958 | |
2713 | our @ISA = CFPlus::UI::VBox::; |
2959 | our @ISA = DC::UI::VBox::; |
2714 | |
2960 | |
2715 | sub new { |
2961 | sub new { |
2716 | my ($class, %arg) = @_; |
2962 | my ($class, %arg) = @_; |
2717 | |
2963 | |
2718 | my $self = $class->SUPER::new ( |
2964 | my $self = $class->SUPER::new ( |
… | |
… | |
2720 | can_hover => 1, |
2966 | can_hover => 1, |
2721 | can_events => 1, |
2967 | can_events => 1, |
2722 | %arg, |
2968 | %arg, |
2723 | ); |
2969 | ); |
2724 | |
2970 | |
2725 | $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); |
2971 | $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999"); |
2726 | $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2972 | $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2727 | $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); |
2973 | $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999"); |
2728 | |
2974 | |
2729 | $self |
2975 | $self |
2730 | } |
2976 | } |
2731 | |
2977 | |
2732 | sub set_fontsize { |
2978 | sub set_fontsize { |
… | |
… | |
2753 | $self->{value}->set_text ($val); |
2999 | $self->{value}->set_text ($val); |
2754 | } |
3000 | } |
2755 | |
3001 | |
2756 | ############################################################################# |
3002 | ############################################################################# |
2757 | |
3003 | |
2758 | package CFPlus::UI::Slider; |
3004 | package DC::UI::Slider; |
2759 | |
3005 | |
2760 | use strict; |
3006 | use strict; |
2761 | |
3007 | |
2762 | use CFPlus::OpenGL; |
3008 | use DC::OpenGL; |
2763 | |
3009 | |
2764 | our @ISA = CFPlus::UI::DrawBG::; |
3010 | our @ISA = DC::UI::DrawBG::; |
2765 | |
3011 | |
2766 | my @tex = |
3012 | my @tex = |
2767 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } |
3013 | map { new_from_file DC::Texture DC::find_rcfile $_ } |
2768 | qw(s1_slider.png s1_slider_bg.png); |
3014 | qw(s1_slider.png s1_slider_bg.png); |
2769 | |
3015 | |
2770 | sub new { |
3016 | sub new { |
2771 | my $class = shift; |
3017 | my $class = shift; |
2772 | |
3018 | |
… | |
… | |
2840 | |
3086 | |
2841 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
3087 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
2842 | |
3088 | |
2843 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
3089 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2844 | |
3090 | |
2845 | $self->invoke_mouse_motion ($ev, $x, $y) |
3091 | $self->invoke_mouse_motion ($ev, $x, $y); |
|
|
3092 | |
|
|
3093 | 1 |
2846 | } |
3094 | } |
2847 | |
3095 | |
2848 | sub invoke_mouse_motion { |
3096 | sub invoke_mouse_motion { |
2849 | my ($self, $ev, $x, $y) = @_; |
3097 | my ($self, $ev, $x, $y) = @_; |
2850 | |
3098 | |
… | |
… | |
2866 | sub invoke_mouse_wheel { |
3114 | sub invoke_mouse_wheel { |
2867 | my ($self, $ev) = @_; |
3115 | my ($self, $ev) = @_; |
2868 | |
3116 | |
2869 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
3117 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2870 | |
3118 | |
2871 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
3119 | my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2; |
2872 | |
3120 | |
2873 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
3121 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2874 | |
3122 | |
2875 | ! ! $delta |
3123 | 1 |
2876 | } |
3124 | } |
2877 | |
3125 | |
2878 | sub update { |
3126 | sub update { |
2879 | my ($self) = @_; |
3127 | my ($self) = @_; |
2880 | |
3128 | |
… | |
… | |
2929 | glDisable GL_TEXTURE_2D; |
3177 | glDisable GL_TEXTURE_2D; |
2930 | } |
3178 | } |
2931 | |
3179 | |
2932 | ############################################################################# |
3180 | ############################################################################# |
2933 | |
3181 | |
2934 | package CFPlus::UI::ValSlider; |
3182 | package DC::UI::ValSlider; |
2935 | |
3183 | |
2936 | our @ISA = CFPlus::UI::HBox::; |
3184 | our @ISA = DC::UI::HBox::; |
2937 | |
3185 | |
2938 | sub new { |
3186 | sub new { |
2939 | my ($class, %arg) = @_; |
3187 | my ($class, %arg) = @_; |
2940 | |
3188 | |
2941 | my $range = delete $arg{range}; |
3189 | my $range = delete $arg{range}; |
2942 | |
3190 | |
2943 | my $self = $class->SUPER::new ( |
3191 | my $self = $class->SUPER::new ( |
2944 | slider => (new CFPlus::UI::Slider expand => 1, range => $range), |
3192 | slider => (new DC::UI::Slider expand => 1, range => $range), |
2945 | entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), |
3193 | entry => (new DC::UI::Label text => "", template => delete $arg{template}), |
2946 | to_value => sub { shift }, |
3194 | to_value => sub { shift }, |
2947 | from_value => sub { shift }, |
3195 | from_value => sub { shift }, |
2948 | %arg, |
3196 | %arg, |
2949 | ); |
3197 | ); |
2950 | |
3198 | |
… | |
… | |
2970 | sub set_range { shift->{slider}->set_range (@_) } |
3218 | sub set_range { shift->{slider}->set_range (@_) } |
2971 | sub set_value { shift->{slider}->set_value (@_) } |
3219 | sub set_value { shift->{slider}->set_value (@_) } |
2972 | |
3220 | |
2973 | ############################################################################# |
3221 | ############################################################################# |
2974 | |
3222 | |
2975 | package CFPlus::UI::TextScroller; |
3223 | package DC::UI::TextScroller; |
2976 | |
3224 | |
2977 | our @ISA = CFPlus::UI::HBox::; |
3225 | our @ISA = DC::UI::HBox::; |
2978 | |
3226 | |
2979 | use CFPlus::OpenGL; |
3227 | use DC::OpenGL; |
2980 | |
3228 | |
2981 | sub new { |
3229 | sub new { |
2982 | my $class = shift; |
3230 | my $class = shift; |
2983 | |
3231 | |
2984 | my $self = $class->SUPER::new ( |
3232 | my $self = $class->SUPER::new ( |
… | |
… | |
2986 | can_events => 1, |
3234 | can_events => 1, |
2987 | indent => 0, |
3235 | indent => 0, |
2988 | #font => default_font |
3236 | #font => default_font |
2989 | @_, |
3237 | @_, |
2990 | |
3238 | |
2991 | layout => (new CFPlus::Layout), |
3239 | layout => (new DC::Layout), |
2992 | par => [], |
3240 | par => [], |
2993 | max_par => 0, |
3241 | max_par => 0, |
2994 | height => 0, |
3242 | height => 0, |
2995 | children => [ |
3243 | children => [ |
2996 | (new CFPlus::UI::Empty expand => 1), |
3244 | (new DC::UI::Empty expand => 1), |
2997 | (new CFPlus::UI::Slider vertical => 1), |
3245 | (new DC::UI::Slider vertical => 1), |
2998 | ], |
3246 | ], |
2999 | ); |
3247 | ); |
3000 | |
3248 | |
3001 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3249 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3002 | |
3250 | |
… | |
… | |
3011 | } |
3259 | } |
3012 | |
3260 | |
3013 | sub size_request { |
3261 | sub size_request { |
3014 | my ($self) = @_; |
3262 | my ($self) = @_; |
3015 | |
3263 | |
3016 | my ($empty, $slider) = @{ $self->{children} }; |
3264 | my ($empty, $slider) = $self->visible_children; |
3017 | |
3265 | |
3018 | local $self->{children} = [$empty, $slider]; |
3266 | local $self->{children} = [$empty, $slider]; |
3019 | $self->SUPER::size_request |
3267 | $self->SUPER::size_request |
3020 | } |
3268 | } |
3021 | |
3269 | |
… | |
… | |
3185 | $ROOT->on_post_alloc ($self => sub { |
3433 | $ROOT->on_post_alloc ($self => sub { |
3186 | $self->force_uptodate; |
3434 | $self->force_uptodate; |
3187 | |
3435 | |
3188 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3436 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3189 | |
3437 | |
3190 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
3438 | $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub { |
3191 | glClearColor 0, 0, 0, 0; |
3439 | glClearColor 0, 0, 0, 0; |
3192 | glClear GL_COLOR_BUFFER_BIT; |
3440 | glClear GL_COLOR_BUFFER_BIT; |
3193 | |
3441 | |
3194 | package CFPlus::UI::Base; |
3442 | package DC::UI::Base; |
3195 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3443 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3196 | (0, 0, $self->{w}, $self->{h}); |
3444 | (0, 0, $self->{w}, $self->{h}); |
3197 | |
3445 | |
3198 | my $top = int $self->{children}[1]{range}[0]; |
3446 | my $top = int $self->{children}[1]{range}[0]; |
3199 | |
3447 | |
… | |
… | |
3210 | |
3458 | |
3211 | if ($y0 < $y + $h && $y < $y1) { |
3459 | if ($y0 < $y + $h && $y < $y1) { |
3212 | my $layout = $self->get_layout ($para); |
3460 | my $layout = $self->get_layout ($para); |
3213 | |
3461 | |
3214 | $layout->render ($para->{indent}, $y - $y0); |
3462 | $layout->render ($para->{indent}, $y - $y0); |
|
|
3463 | $layout->draw; |
3215 | |
3464 | |
3216 | if (my @w = @{ $para->{widget} }) { |
3465 | if (my @w = @{ $para->{widget} }) { |
3217 | my @s = $layout->get_shapes; |
3466 | my @s = $layout->get_shapes; |
3218 | |
3467 | |
3219 | for (@w) { |
3468 | for (@w) { |
… | |
… | |
3257 | $self->{children}[1]->draw; |
3506 | $self->{children}[1]->draw; |
3258 | } |
3507 | } |
3259 | |
3508 | |
3260 | ############################################################################# |
3509 | ############################################################################# |
3261 | |
3510 | |
3262 | package CFPlus::UI::Animator; |
3511 | package DC::UI::Animator; |
3263 | |
3512 | |
3264 | use CFPlus::OpenGL; |
3513 | use DC::OpenGL; |
3265 | |
3514 | |
3266 | our @ISA = CFPlus::UI::Bin::; |
3515 | our @ISA = DC::UI::Bin::; |
3267 | |
3516 | |
3268 | sub moveto { |
3517 | sub moveto { |
3269 | my ($self, $x, $y) = @_; |
3518 | my ($self, $x, $y) = @_; |
3270 | |
3519 | |
3271 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
3520 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
3299 | glPopMatrix; |
3548 | glPopMatrix; |
3300 | } |
3549 | } |
3301 | |
3550 | |
3302 | ############################################################################# |
3551 | ############################################################################# |
3303 | |
3552 | |
3304 | package CFPlus::UI::Flopper; |
3553 | package DC::UI::Flopper; |
3305 | |
3554 | |
3306 | our @ISA = CFPlus::UI::Button::; |
3555 | our @ISA = DC::UI::Button::; |
3307 | |
3556 | |
3308 | sub new { |
3557 | sub new { |
3309 | my $class = shift; |
3558 | my $class = shift; |
3310 | |
3559 | |
3311 | my $self = $class->SUPER::new ( |
3560 | my $self = $class->SUPER::new ( |
… | |
… | |
3323 | $self->{other}->toggle_visibility; |
3572 | $self->{other}->toggle_visibility; |
3324 | } |
3573 | } |
3325 | |
3574 | |
3326 | ############################################################################# |
3575 | ############################################################################# |
3327 | |
3576 | |
3328 | package CFPlus::UI::Tooltip; |
3577 | package DC::UI::Tooltip; |
3329 | |
3578 | |
3330 | our @ISA = CFPlus::UI::Bin::; |
3579 | our @ISA = DC::UI::Bin::; |
3331 | |
3580 | |
3332 | use CFPlus::OpenGL; |
3581 | use DC::OpenGL; |
3333 | |
3582 | |
3334 | sub new { |
3583 | sub new { |
3335 | my $class = shift; |
3584 | my $class = shift; |
3336 | |
3585 | |
3337 | $class->SUPER::new ( |
3586 | $class->SUPER::new ( |
… | |
… | |
3341 | } |
3590 | } |
3342 | |
3591 | |
3343 | sub set_tooltip_from { |
3592 | sub set_tooltip_from { |
3344 | my ($self, $widget) = @_; |
3593 | my ($self, $widget) = @_; |
3345 | |
3594 | |
3346 | $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1 |
|
|
3347 | if $widget->{tooltip} =~ /^#(.*)$/; |
|
|
3348 | |
|
|
3349 | my $tooltip = $widget->{tooltip}; |
3595 | my $tip = $widget->{tooltip}; |
|
|
3596 | $tip = $tip->($widget) if "CODE" eq ref $tip; |
|
|
3597 | |
|
|
3598 | $tip = DC::Pod::section_label tooltip => $1 |
|
|
3599 | if $tip =~ /^#(.*)$/; |
3350 | |
3600 | |
3351 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3601 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3352 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
3602 | $tip .= "\n\n" . (ref $widget) . "\n" |
3353 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3603 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3354 | . "req $widget->{req_w} $widget->{req_h}\n" |
3604 | . "req $widget->{req_w} $widget->{req_h}\n" |
3355 | . "visible $widget->{visible}"; |
3605 | . "visible $widget->{visible}"; |
3356 | } |
3606 | } |
3357 | |
3607 | |
3358 | $tooltip =~ s/^\n+//; |
3608 | $tip =~ s/^\n+//; |
3359 | $tooltip =~ s/\n+$//; |
3609 | $tip =~ s/\n+$//; |
3360 | |
3610 | |
3361 | $self->add (new CFPlus::UI::Label |
3611 | $self->add (new DC::UI::Label |
3362 | markup => $tooltip, |
3612 | markup => $tip, |
3363 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3613 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3364 | fontsize => 0.8, |
3614 | fontsize => 0.8, |
3365 | style => 1, # FLAG_INVERSE |
3615 | style => 1, # FLAG_INVERSE |
3366 | ellipsise => 0, |
3616 | ellipsise => 0, |
3367 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3617 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
… | |
… | |
3405 | } |
3655 | } |
3406 | |
3656 | |
3407 | sub _draw { |
3657 | sub _draw { |
3408 | my ($self) = @_; |
3658 | my ($self) = @_; |
3409 | |
3659 | |
3410 | glTranslate 0.375, 0.375; |
|
|
3411 | |
|
|
3412 | my ($w, $h) = @$self{qw(w h)}; |
3660 | my ($w, $h) = @$self{qw(w h)}; |
3413 | |
3661 | |
3414 | glColor 1, 0.8, 0.4; |
3662 | glColor 1, 0.8, 0.4; |
3415 | glBegin GL_QUADS; |
3663 | glRect 0, 0, $w, $h; |
3416 | glVertex 0 , 0; |
|
|
3417 | glVertex 0 , $h; |
|
|
3418 | glVertex $w, $h; |
|
|
3419 | glVertex $w, 0; |
|
|
3420 | glEnd; |
|
|
3421 | |
3664 | |
3422 | glColor 0, 0, 0; |
3665 | glColor 0, 0, 0; |
3423 | glBegin GL_LINE_LOOP; |
3666 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3424 | glVertex 0 , 0; |
|
|
3425 | glVertex 0 , $h; |
|
|
3426 | glVertex $w, $h; |
|
|
3427 | glVertex $w, 0; |
|
|
3428 | glEnd; |
|
|
3429 | |
3667 | |
3430 | glTranslate 2 - 0.375, 2 - 0.375; |
3668 | glTranslate 2, 2; |
3431 | |
3669 | |
3432 | $self->SUPER::_draw; |
3670 | $self->SUPER::_draw; |
3433 | } |
3671 | } |
3434 | |
3672 | |
3435 | ############################################################################# |
3673 | ############################################################################# |
3436 | |
3674 | |
3437 | package CFPlus::UI::Face; |
3675 | package DC::UI::Face; |
3438 | |
3676 | |
3439 | our @ISA = CFPlus::UI::DrawBG::; |
3677 | our @ISA = DC::UI::DrawBG::; |
3440 | |
3678 | |
3441 | use CFPlus::OpenGL; |
3679 | use DC::OpenGL; |
3442 | |
3680 | |
3443 | sub new { |
3681 | sub new { |
3444 | my $class = shift; |
3682 | my $class = shift; |
3445 | |
3683 | |
3446 | my $self = $class->SUPER::new ( |
3684 | my $self = $class->SUPER::new ( |
… | |
… | |
3450 | can_events => 0, |
3688 | can_events => 0, |
3451 | @_, |
3689 | @_, |
3452 | ); |
3690 | ); |
3453 | |
3691 | |
3454 | if ($self->{anim} && $self->{animspeed}) { |
3692 | if ($self->{anim} && $self->{animspeed}) { |
3455 | CFPlus::weaken (my $widget = $self); |
3693 | DC::weaken (my $widget = $self); |
3456 | |
3694 | |
3457 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
3695 | $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; |
3458 | $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; |
3696 | $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { |
3459 | $self->{timer} = Event->timer ( |
|
|
3460 | parked => 1, |
|
|
3461 | cb => sub { |
|
|
3462 | return unless $::CONN && $widget; |
3697 | return unless $::CONN; |
3463 | |
3698 | |
|
|
3699 | my $w = $widget |
|
|
3700 | or return; |
|
|
3701 | |
3464 | ++$widget->{frame}; |
3702 | ++$w->{frame}; |
3465 | $widget->update_face; |
3703 | $w->update_face; |
|
|
3704 | |
|
|
3705 | # somehow, $widget can go away |
3466 | $widget->update; |
3706 | $w->update; |
3467 | |
|
|
3468 | $widget->update_timer; |
3707 | $w->update_timer; |
3469 | }, |
|
|
3470 | ); |
3708 | }; |
3471 | |
3709 | |
3472 | $self->update_face; |
3710 | $self->update_face; |
3473 | $self->update_timer; |
3711 | $self->update_timer; |
3474 | } |
3712 | } |
3475 | |
3713 | |
… | |
… | |
3480 | my ($self) = @_; |
3718 | my ($self) = @_; |
3481 | |
3719 | |
3482 | return unless $self->{timer}; |
3720 | return unless $self->{timer}; |
3483 | |
3721 | |
3484 | if ($self->{visible}) { |
3722 | if ($self->{visible}) { |
3485 | $self->{timer}->at ( |
|
|
3486 | $self->{anim_start} |
|
|
3487 | + $self->{animspeed} |
|
|
3488 | * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} |
|
|
3489 | ); |
|
|
3490 | $self->{timer}->start; |
3723 | $self->{timer}->start; |
3491 | } else { |
3724 | } else { |
3492 | $self->{timer}->stop; |
3725 | $self->{timer}->stop; |
3493 | } |
3726 | } |
3494 | } |
3727 | } |
3495 | |
3728 | |
3496 | sub update_face { |
3729 | sub update_face { |
3497 | my ($self) = @_; |
3730 | my ($self) = @_; |
3498 | |
3731 | |
3499 | return unless $::CONN; |
3732 | if ($::CONN) { |
3500 | |
|
|
3501 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
3733 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
3502 | if ($anim && @$anim) { |
3734 | if ($anim && @$anim) { |
3503 | delete $self->{wait_face}; |
|
|
3504 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
3735 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
|
|
3736 | delete $self->{face_change_cb}; |
|
|
3737 | |
3505 | $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; |
3738 | if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) { |
|
|
3739 | unless ($tex->{name} || $tex->{loading}) { |
|
|
3740 | $tex->upload (sub { $self->reconfigure }); |
|
|
3741 | } |
|
|
3742 | } |
|
|
3743 | } |
3506 | } |
3744 | } |
3507 | } |
3745 | } |
3508 | } |
3746 | } |
3509 | |
3747 | |
3510 | sub size_request { |
3748 | sub size_request { |
3511 | my ($self) = @_; |
3749 | my ($self) = @_; |
3512 | |
3750 | |
3513 | if ($::CONN) { |
3751 | if ($::CONN) { |
3514 | if (my $faceid = $::CONN->{faceid}[$self->{face}]) { |
3752 | if (my $faceid = $::CONN->{face}[$self->{face}]{id}) { |
3515 | if (my $tex = $::CONN->{texture}[$faceid]) { |
3753 | if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) { |
3516 | $self->{tex} = $tex; |
3754 | if ($tex->{name}) { |
3517 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
3755 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
3518 | } else { |
3756 | } elsif (!$tex->{loading}) { |
3519 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
3757 | $tex->upload (sub { $self->reconfigure }); |
3520 | $self->realloc; |
|
|
3521 | }); |
3758 | } |
3522 | } |
3759 | } |
|
|
3760 | |
|
|
3761 | $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure }); |
3523 | } |
3762 | } |
3524 | } |
3763 | } |
3525 | |
3764 | |
3526 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3765 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3527 | } |
3766 | } |
… | |
… | |
3565 | $self->SUPER::destroy; |
3804 | $self->SUPER::destroy; |
3566 | } |
3805 | } |
3567 | |
3806 | |
3568 | ############################################################################# |
3807 | ############################################################################# |
3569 | |
3808 | |
3570 | package CFPlus::UI::Buttonbar; |
3809 | package DC::UI::Buttonbar; |
3571 | |
3810 | |
3572 | our @ISA = CFPlus::UI::HBox::; |
3811 | our @ISA = DC::UI::HBox::; |
3573 | |
3812 | |
3574 | # TODO: should actually wrap buttons and other goodies. |
3813 | # TODO: should actually wrap buttons and other goodies. |
3575 | |
3814 | |
3576 | ############################################################################# |
3815 | ############################################################################# |
3577 | |
3816 | |
3578 | package CFPlus::UI::Menu; |
3817 | package DC::UI::Menu; |
3579 | |
3818 | |
3580 | our @ISA = CFPlus::UI::Toplevel::; |
3819 | our @ISA = DC::UI::Toplevel::; |
3581 | |
3820 | |
3582 | use CFPlus::OpenGL; |
3821 | use DC::OpenGL; |
3583 | |
3822 | |
3584 | sub new { |
3823 | sub new { |
3585 | my $class = shift; |
3824 | my $class = shift; |
3586 | |
3825 | |
3587 | my $self = $class->SUPER::new ( |
3826 | my $self = $class->SUPER::new ( |
3588 | items => [], |
3827 | items => [], |
3589 | z => 100, |
3828 | z => 100, |
3590 | @_, |
3829 | @_, |
3591 | ); |
3830 | ); |
3592 | |
3831 | |
3593 | $self->add ($self->{vbox} = new CFPlus::UI::VBox); |
3832 | $self->add ($self->{vbox} = new DC::UI::VBox); |
3594 | |
3833 | |
3595 | for my $item (@{ $self->{items} }) { |
3834 | for my $item (@{ $self->{items} }) { |
3596 | my ($widget, $cb, $tooltip) = @$item; |
3835 | my ($widget, $cb, $tooltip) = @$item; |
3597 | |
3836 | |
3598 | # handle various types of items, only text for now |
3837 | # handle various types of items, only text for now |
3599 | if (!ref $widget) { |
3838 | if (!ref $widget) { |
3600 | if ($widget =~ /\t/) { |
3839 | if ($widget =~ /\t/) { |
3601 | my ($left, $right) = split /\t/, $widget, 2; |
3840 | my ($left, $right) = split /\t/, $widget, 2; |
3602 | |
3841 | |
3603 | $widget = new CFPlus::UI::HBox |
3842 | $widget = new DC::UI::HBox |
3604 | can_hover => 1, |
3843 | can_hover => 1, |
3605 | can_events => 1, |
3844 | can_events => 1, |
3606 | tooltip => $tooltip, |
3845 | tooltip => $tooltip, |
3607 | children => [ |
3846 | children => [ |
3608 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3847 | (new DC::UI::Label markup => $left, expand => 1), |
3609 | (new CFPlus::UI::Label markup => $right, align => +1), |
3848 | (new DC::UI::Label markup => $right, align => +1), |
3610 | ], |
3849 | ], |
3611 | ; |
3850 | ; |
3612 | |
3851 | |
3613 | } else { |
3852 | } else { |
3614 | $widget = new CFPlus::UI::Label |
3853 | $widget = new DC::UI::Label |
3615 | can_hover => 1, |
3854 | can_hover => 1, |
3616 | can_events => 1, |
3855 | can_events => 1, |
3617 | markup => $widget, |
3856 | markup => $widget, |
3618 | tooltip => $tooltip; |
3857 | tooltip => $tooltip; |
3619 | } |
3858 | } |
… | |
… | |
3667 | 1 |
3906 | 1 |
3668 | } |
3907 | } |
3669 | |
3908 | |
3670 | ############################################################################# |
3909 | ############################################################################# |
3671 | |
3910 | |
3672 | package CFPlus::UI::Multiplexer; |
3911 | package DC::UI::Multiplexer; |
3673 | |
3912 | |
3674 | our @ISA = CFPlus::UI::Container::; |
3913 | our @ISA = DC::UI::Container::; |
3675 | |
3914 | |
3676 | sub new { |
3915 | sub new { |
3677 | my $class = shift; |
3916 | my $class = shift; |
3678 | |
3917 | |
3679 | my $self = $class->SUPER::new ( |
3918 | my $self = $class->SUPER::new ( |
… | |
… | |
3740 | $self->{current}->draw; |
3979 | $self->{current}->draw; |
3741 | } |
3980 | } |
3742 | |
3981 | |
3743 | ############################################################################# |
3982 | ############################################################################# |
3744 | |
3983 | |
3745 | package CFPlus::UI::Notebook; |
3984 | package DC::UI::Notebook; |
3746 | |
3985 | |
|
|
3986 | use DC::OpenGL; |
|
|
3987 | |
3747 | our @ISA = CFPlus::UI::VBox::; |
3988 | our @ISA = DC::UI::VBox::; |
3748 | |
3989 | |
3749 | sub new { |
3990 | sub new { |
3750 | my $class = shift; |
3991 | my $class = shift; |
3751 | |
3992 | |
3752 | my $self = $class->SUPER::new ( |
3993 | my $self = $class->SUPER::new ( |
3753 | buttonbar => (new CFPlus::UI::Buttonbar), |
3994 | buttonbar => (new DC::UI::Buttonbar), |
3754 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
3995 | multiplexer => (new DC::UI::Multiplexer expand => 1), |
|
|
3996 | active_outline => [.7, .7, 0.2], |
3755 | # filter => # will be put between multiplexer and $self |
3997 | # filter => # will be put between multiplexer and $self |
3756 | @_, |
3998 | @_, |
3757 | ); |
3999 | ); |
3758 | |
4000 | |
3759 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
4001 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3760 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
4002 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
3761 | |
4003 | |
|
|
4004 | { |
|
|
4005 | Scalar::Util::weaken (my $wself = $self); |
|
|
4006 | |
|
|
4007 | $self->{multiplexer}->connect (c_add => sub { |
|
|
4008 | my ($mplex, $widgets) = @_; |
|
|
4009 | |
|
|
4010 | for my $child (@$widgets) { |
|
|
4011 | Scalar::Util::weaken $child; |
|
|
4012 | $child->{c_tab_} ||= do { |
|
|
4013 | my $tab = |
|
|
4014 | (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base") |
|
|
4015 | ? $child->{c_tab} |
|
|
4016 | : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
|
|
4017 | |
|
|
4018 | $tab->connect (activate => sub { |
|
|
4019 | $wself->set_current_page ($child); |
|
|
4020 | }); |
|
|
4021 | |
|
|
4022 | $tab |
|
|
4023 | }; |
|
|
4024 | |
|
|
4025 | $self->{buttonbar}->add ($child->{c_tab_}); |
|
|
4026 | } |
|
|
4027 | }); |
|
|
4028 | |
|
|
4029 | $self->{multiplexer}->connect (c_remove => sub { |
|
|
4030 | my ($mplex, $widgets) = @_; |
|
|
4031 | |
|
|
4032 | for my $child (@$widgets) { |
|
|
4033 | $wself->{buttonbar}->remove ($child->{c_tab_}); |
|
|
4034 | } |
|
|
4035 | }); |
|
|
4036 | } |
|
|
4037 | |
3762 | $self |
4038 | $self |
3763 | } |
4039 | } |
3764 | |
4040 | |
3765 | sub add { |
4041 | sub add { |
|
|
4042 | my ($self, @widgets) = @_; |
|
|
4043 | |
|
|
4044 | $self->{multiplexer}->add (@widgets) |
|
|
4045 | } |
|
|
4046 | |
|
|
4047 | sub remove { |
|
|
4048 | my ($self, @widgets) = @_; |
|
|
4049 | |
|
|
4050 | $self->{multiplexer}->remove (@widgets) |
|
|
4051 | } |
|
|
4052 | |
|
|
4053 | sub pages { |
|
|
4054 | my ($self) = @_; |
|
|
4055 | $self->{multiplexer}->children |
|
|
4056 | } |
|
|
4057 | |
|
|
4058 | sub add_tab { |
3766 | my ($self, $title, $widget, $tooltip) = @_; |
4059 | my ($self, $title, $widget, $tooltip) = @_; |
3767 | |
4060 | |
3768 | CFPlus::weaken $self; |
4061 | $title = [$title, $tooltip] unless ref $title; |
|
|
4062 | $widget->{c_tab} = $title; |
3769 | |
4063 | |
3770 | unless (ref $title) { |
|
|
3771 | $title = new CFPlus::UI::Button |
|
|
3772 | markup => $title, |
|
|
3773 | tooltip => $tooltip, |
|
|
3774 | ); |
|
|
3775 | } |
|
|
3776 | |
|
|
3777 | $title->connect (activate => sub { $self->set_current_page ($widget) }); |
|
|
3778 | $self->{buttonbar}->add ($title); |
|
|
3779 | |
|
|
3780 | $self->{multiplexer}->add ($widget); |
4064 | $self->add ($widget); |
3781 | } |
4065 | } |
3782 | |
4066 | |
3783 | sub get_current_page { |
4067 | sub get_current_page { |
3784 | my ($self) = @_; |
4068 | my ($self) = @_; |
3785 | |
4069 | |
… | |
… | |
3791 | |
4075 | |
3792 | $self->{multiplexer}->set_current_page ($page); |
4076 | $self->{multiplexer}->set_current_page ($page); |
3793 | $self->emit (page_changed => $self->{multiplexer}{current}); |
4077 | $self->emit (page_changed => $self->{multiplexer}{current}); |
3794 | } |
4078 | } |
3795 | |
4079 | |
|
|
4080 | sub _draw { |
|
|
4081 | my ($self) = @_; |
|
|
4082 | |
|
|
4083 | $self->SUPER::_draw (); |
|
|
4084 | |
|
|
4085 | if (my $cur = $self->{multiplexer}{current}) { |
|
|
4086 | if ($cur = $cur->{c_tab_}) { |
|
|
4087 | glTranslate $self->{buttonbar}{x} + $cur->{x}, |
|
|
4088 | $self->{buttonbar}{y} + $cur->{y}; |
|
|
4089 | glLineWidth 3; |
|
|
4090 | #glEnable GL_BLEND; |
|
|
4091 | #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
4092 | glColor @{$self->{active_outline}}; |
|
|
4093 | glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5; |
|
|
4094 | glLineWidth 1; |
|
|
4095 | #glDisable GL_BLEND; |
|
|
4096 | } |
|
|
4097 | } |
|
|
4098 | } |
|
|
4099 | |
3796 | ############################################################################# |
4100 | ############################################################################# |
3797 | |
4101 | |
3798 | package CFPlus::UI::Selector; |
4102 | package DC::UI::Selector; |
3799 | |
4103 | |
3800 | use utf8; |
4104 | use utf8; |
3801 | |
4105 | |
3802 | our @ISA = CFPlus::UI::Button::; |
4106 | our @ISA = DC::UI::Button::; |
3803 | |
4107 | |
3804 | sub new { |
4108 | sub new { |
3805 | my $class = shift; |
4109 | my $class = shift; |
3806 | |
4110 | |
3807 | my $self = $class->SUPER::new ( |
4111 | my $self = $class->SUPER::new ( |
… | |
… | |
3824 | my ($value, $title, $tooltip) = @$_; |
4128 | my ($value, $title, $tooltip) = @$_; |
3825 | |
4129 | |
3826 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
4130 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
3827 | } |
4131 | } |
3828 | |
4132 | |
3829 | CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
4133 | DC::UI::Menu->new (items => \@menu_items)->popup ($ev); |
3830 | } |
4134 | } |
3831 | |
4135 | |
3832 | sub _set_value { |
4136 | sub _set_value { |
3833 | my ($self, $value) = @_; |
4137 | my ($self, $value) = @_; |
3834 | |
4138 | |
3835 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} } |
4139 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} }; |
|
|
4140 | $item ||= $self->{options}[0] |
3836 | or return; |
4141 | or return; |
3837 | |
4142 | |
3838 | $self->{value} = $item->[0]; |
4143 | $self->{value} = $item->[0]; |
3839 | $self->set_markup ("$item->[1] ⇓"); |
4144 | $self->set_markup ("$item->[1] ⇓"); |
3840 | $self->set_tooltip ($item->[2]); |
4145 | # $self->set_tooltip ($item->[2]); |
3841 | } |
4146 | } |
3842 | |
4147 | |
3843 | sub set_value { |
4148 | sub set_value { |
3844 | my ($self, $value) = @_; |
4149 | my ($self, $value) = @_; |
3845 | |
4150 | |
… | |
… | |
3847 | |
4152 | |
3848 | $self->_set_value ($value); |
4153 | $self->_set_value ($value); |
3849 | $self->emit (changed => $value); |
4154 | $self->emit (changed => $value); |
3850 | } |
4155 | } |
3851 | |
4156 | |
|
|
4157 | sub set_options { |
|
|
4158 | my ($self, $options) = @_; |
|
|
4159 | |
|
|
4160 | $self->{options} = $options; |
|
|
4161 | $self->_set_value ($self->{value}); |
|
|
4162 | } |
|
|
4163 | |
3852 | ############################################################################# |
4164 | ############################################################################# |
3853 | |
4165 | |
3854 | package CFPlus::UI::Statusbox; |
4166 | package DC::UI::Statusbox; |
3855 | |
4167 | |
3856 | our @ISA = CFPlus::UI::VBox::; |
4168 | our @ISA = DC::UI::VBox::; |
3857 | |
4169 | |
3858 | sub new { |
4170 | sub new { |
3859 | my $class = shift; |
4171 | my $class = shift; |
3860 | |
4172 | |
3861 | my $self = $class->SUPER::new ( |
4173 | my $self = $class->SUPER::new ( |
3862 | fontsize => 0.8, |
4174 | fontsize => 0.8, |
3863 | @_, |
4175 | @_, |
3864 | ); |
4176 | ); |
3865 | |
4177 | |
3866 | CFPlus::weaken (my $this = $self); |
4178 | DC::weaken (my $this = $self); |
3867 | |
4179 | |
3868 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
4180 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
3869 | |
4181 | |
3870 | $self |
4182 | $self |
3871 | } |
4183 | } |
3872 | |
4184 | |
3873 | sub reorder { |
4185 | sub reorder { |
3874 | my ($self) = @_; |
4186 | my ($self) = @_; |
3875 | my $NOW = Time::HiRes::time; |
4187 | my $NOW = Time::HiRes::time; |
3876 | |
4188 | |
3877 | # freeze display when hovering over any label |
4189 | # freeze display when hovering over any label |
3878 | return if $CFPlus::UI::TOOLTIP->{owner} |
4190 | return if $DC::UI::TOOLTIP->{owner} |
3879 | && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, |
4191 | && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, |
3880 | values %{ $self->{item} }; |
4192 | values %{ $self->{item} }; |
3881 | |
4193 | |
3882 | while (my ($k, $v) = each %{ $self->{item} }) { |
4194 | while (my ($k, $v) = each %{ $self->{item} }) { |
3883 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4195 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
3884 | } |
4196 | } |
|
|
4197 | |
|
|
4198 | $self->{timer}->set (1, 1); |
3885 | |
4199 | |
3886 | my @widgets; |
4200 | my @widgets; |
3887 | |
4201 | |
3888 | my @items = sort { |
4202 | my @items = sort { |
3889 | $a->{pri} <=> $b->{pri} |
4203 | $a->{pri} <=> $b->{pri} |
3890 | or $b->{id} <=> $a->{id} |
4204 | or $b->{id} <=> $a->{id} |
3891 | } values %{ $self->{item} }; |
4205 | } values %{ $self->{item} }; |
3892 | |
|
|
3893 | $self->{timer}->interval (1); |
|
|
3894 | |
4206 | |
3895 | my $count = 10 + 1; |
4207 | my $count = 10 + 1; |
3896 | for my $item (@items) { |
4208 | for my $item (@items) { |
3897 | last unless --$count; |
4209 | last unless --$count; |
3898 | |
4210 | |
… | |
… | |
3905 | for ($short) { |
4217 | for ($short) { |
3906 | s/^\s+//; |
4218 | s/^\s+//; |
3907 | s/\s+/ /g; |
4219 | s/\s+/ /g; |
3908 | } |
4220 | } |
3909 | |
4221 | |
3910 | new CFPlus::UI::Label |
4222 | new DC::UI::Label |
3911 | markup => $short, |
4223 | markup => $short, |
3912 | tooltip => $item->{tooltip}, |
4224 | tooltip => $item->{tooltip}, |
3913 | tooltip_font => $::FONT_PROP, |
4225 | tooltip_font => $::FONT_PROP, |
3914 | tooltip_width => 0.67, |
4226 | tooltip_width => 0.67, |
3915 | fontsize => $item->{fontsize} || $self->{fontsize}, |
4227 | fontsize => $item->{fontsize} || $self->{fontsize}, |
… | |
… | |
3922 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4234 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
3923 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4235 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
3924 | $label->update; |
4236 | $label->update; |
3925 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4237 | $label->set_max_size (undef, $label->{req_h} * $diff) |
3926 | if $diff < 1; |
4238 | if $diff < 1; |
3927 | $self->{timer}->interval (1/30); |
4239 | $self->{timer}->set (1/30, 1/30); |
3928 | } else { |
4240 | } else { |
3929 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4241 | $label->{fg}[3] = $item->{fg}[3] || 1; |
3930 | } |
4242 | } |
3931 | |
4243 | |
3932 | push @widgets, $label; |
4244 | push @widgets, $label; |
… | |
… | |
3994 | $self->SUPER::destroy; |
4306 | $self->SUPER::destroy; |
3995 | } |
4307 | } |
3996 | |
4308 | |
3997 | ############################################################################# |
4309 | ############################################################################# |
3998 | |
4310 | |
3999 | package CFPlus::UI::Root; |
4311 | package DC::UI::Root; |
4000 | |
4312 | |
4001 | our @ISA = CFPlus::UI::Container::; |
4313 | our @ISA = DC::UI::Container::; |
4002 | |
4314 | |
4003 | use List::Util qw(min max); |
4315 | use List::Util qw(min max); |
4004 | |
4316 | |
4005 | use CFPlus::OpenGL; |
4317 | use DC::OpenGL; |
4006 | |
4318 | |
4007 | sub new { |
4319 | sub new { |
4008 | my $class = shift; |
4320 | my $class = shift; |
4009 | |
4321 | |
4010 | my $self = $class->SUPER::new ( |
4322 | my $self = $class->SUPER::new ( |
4011 | visible => 1, |
4323 | visible => 1, |
4012 | @_, |
4324 | @_, |
4013 | ); |
4325 | ); |
4014 | |
4326 | |
4015 | CFPlus::weaken ($self->{root} = $self); |
4327 | DC::weaken ($self->{root} = $self); |
4016 | |
4328 | |
4017 | $self |
4329 | $self |
4018 | } |
4330 | } |
4019 | |
4331 | |
4020 | sub size_request { |
4332 | sub size_request { |
… | |
… | |
4068 | } |
4380 | } |
4069 | |
4381 | |
4070 | sub update { |
4382 | sub update { |
4071 | my ($self) = @_; |
4383 | my ($self) = @_; |
4072 | |
4384 | |
4073 | $::WANT_REFRESH++; |
4385 | $::WANT_REFRESH = 1; |
4074 | } |
4386 | } |
4075 | |
4387 | |
4076 | sub add { |
4388 | sub add { |
4077 | my ($self, @children) = @_; |
4389 | my ($self, @children) = @_; |
4078 | |
4390 | |
… | |
… | |
4223 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4535 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4224 | glMatrixMode GL_MODELVIEW; |
4536 | glMatrixMode GL_MODELVIEW; |
4225 | glLoadIdentity; |
4537 | glLoadIdentity; |
4226 | |
4538 | |
4227 | { |
4539 | { |
4228 | package CFPlus::UI::Base; |
4540 | package DC::UI::Base; |
4229 | |
4541 | |
4230 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4542 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4231 | (0, 0, $self->{w}, $self->{h}); |
4543 | (0, 0, $self->{w}, $self->{h}); |
4232 | |
4544 | |
4233 | $self->_draw; |
4545 | $self->_draw; |
4234 | } |
4546 | } |
4235 | } |
4547 | } |
4236 | |
4548 | |
4237 | ############################################################################# |
4549 | ############################################################################# |
4238 | |
4550 | |
4239 | package CFPlus::UI; |
4551 | package DC::UI; |
4240 | |
4552 | |
4241 | $ROOT = new CFPlus::UI::Root; |
4553 | $ROOT = new DC::UI::Root; |
4242 | $TOOLTIP = new CFPlus::UI::Tooltip z => 900; |
4554 | $TOOLTIP = new DC::UI::Tooltip z => 900; |
4243 | |
4555 | |
4244 | 1 |
4556 | 1 |
4245 | |
4557 | |