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 | |
7 | |
8 | use CFPlus; |
8 | use Guard (); |
|
|
9 | |
|
|
10 | use DC; |
9 | use CFPlus::Pod; |
11 | use DC::Pod; |
10 | use CFPlus::Texture; |
12 | use DC::Texture; |
11 | |
13 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
14 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
15 | |
14 | our $LAYOUT; |
16 | our $LAYOUT; |
15 | our $ROOT; |
17 | our $ROOT; |
16 | our $TOOLTIP; |
18 | our $TOOLTIP; |
17 | our $BUTTON_STATE; |
19 | our $BUTTON_STATE; |
18 | |
20 | |
19 | our %WIDGET; # all widgets, weak-referenced |
21 | our %WIDGET; # all widgets, weak-referenced |
20 | |
22 | |
21 | our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub { |
23 | our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub { |
22 | $_[0]->stop; |
24 | $_[0]->stop; |
23 | |
25 | |
24 | if (!$GRAB) { |
26 | if (!$GRAB) { |
25 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
27 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
26 | if (length $widget->{tooltip}) { |
28 | if (length $widget->{tooltip}) { |
… | |
… | |
111 | |
113 | |
112 | if ($GRAB) { |
114 | if ($GRAB) { |
113 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
115 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
114 | # mousewheel |
116 | # mousewheel |
115 | my $delta = $ev->{button} * 2 - 9; |
117 | my $delta = $ev->{button} * 2 - 9; |
116 | my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; |
118 | my $shift = $ev->{mod} & DC::KMOD_SHIFT; |
117 | |
119 | |
118 | $ev->{dx} = $shift ? $delta : 0; |
120 | $ev->{dx} = $shift ? $delta : 0; |
119 | $ev->{dy} = $shift ? 0 : $delta; |
121 | $ev->{dy} = $shift ? 0 : $delta; |
120 | |
122 | |
121 | $GRAB->emit (mouse_wheel => $ev); |
123 | $GRAB->emit (mouse_wheel => $ev); |
… | |
… | |
189 | # call when resolution changes etc. |
191 | # call when resolution changes etc. |
190 | sub rescale_widgets { |
192 | sub rescale_widgets { |
191 | my ($sx, $sy) = @_; |
193 | my ($sx, $sy) = @_; |
192 | |
194 | |
193 | for my $widget (values %WIDGET) { |
195 | for my $widget (values %WIDGET) { |
194 | if ($widget->{is_toplevel}) { |
196 | if ($widget->{is_toplevel} || $widget->{c_rescale}) { |
195 | $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
197 | $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
196 | $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
198 | $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
197 | |
199 | |
198 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
200 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
199 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
201 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
… | |
… | |
211 | reconfigure_widgets; |
213 | reconfigure_widgets; |
212 | } |
214 | } |
213 | |
215 | |
214 | ############################################################################# |
216 | ############################################################################# |
215 | |
217 | |
216 | package CFPlus::UI::Event; |
218 | package DC::UI::Event; |
217 | |
219 | |
218 | sub xy { |
220 | sub xy { |
219 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
221 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
220 | } |
222 | } |
221 | |
223 | |
222 | ############################################################################# |
224 | ############################################################################# |
223 | |
225 | |
224 | package CFPlus::UI::Base; |
226 | package DC::UI::Base; |
225 | |
227 | |
226 | use strict; |
228 | use strict; |
227 | |
229 | |
228 | use CFPlus::OpenGL; |
230 | use DC::OpenGL; |
229 | |
231 | |
230 | sub new { |
232 | sub new { |
231 | my $class = shift; |
233 | my $class = shift; |
232 | |
234 | |
233 | my $self = bless { |
235 | my $self = bless { |
… | |
… | |
238 | h => undef, |
240 | h => undef, |
239 | can_events => 1, |
241 | can_events => 1, |
240 | @_ |
242 | @_ |
241 | }, $class; |
243 | }, $class; |
242 | |
244 | |
243 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
245 | DC::weaken ($DC::UI::WIDGET{$self+0} = $self); |
244 | |
246 | |
245 | for (keys %$self) { |
247 | for (keys %$self) { |
246 | if (/^on_(.*)$/) { |
248 | if (/^on_(.*)$/) { |
247 | $self->connect ($1 => delete $self->{$_}); |
249 | $self->connect ($1 => delete $self->{$_}); |
248 | } |
250 | } |
249 | } |
251 | } |
250 | |
252 | |
251 | if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { |
253 | if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) { |
252 | $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; |
254 | $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x}; |
253 | $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; |
255 | $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y}; |
254 | $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; |
256 | $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w}; |
255 | $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; |
257 | $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h}; |
256 | |
258 | |
257 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
259 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
258 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
260 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
259 | |
261 | |
260 | $self->show if $layout->{show}; |
262 | $self->show if $layout->{show}; |
… | |
… | |
278 | sub show { |
280 | sub show { |
279 | my ($self) = @_; |
281 | my ($self) = @_; |
280 | |
282 | |
281 | return if $self->{parent}; |
283 | return if $self->{parent}; |
282 | |
284 | |
283 | $CFPlus::UI::ROOT->add ($self); |
285 | $DC::UI::ROOT->add ($self); |
284 | } |
286 | } |
285 | |
287 | |
286 | sub set_visible { |
288 | sub set_visible { |
287 | my ($self) = @_; |
289 | my ($self) = @_; |
288 | |
290 | |
289 | return if $self->{visible}; |
291 | return if $self->{visible}; |
|
|
292 | |
|
|
293 | $self->{parent} && $self->{parent}{root}#d# |
|
|
294 | or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1); |
290 | |
295 | |
291 | $self->{root} = $self->{parent}{root}; |
296 | $self->{root} = $self->{parent}{root}; |
292 | $self->{visible} = $self->{parent}{visible} + 1; |
297 | $self->{visible} = $self->{parent}{visible} + 1; |
293 | |
298 | |
294 | $self->emit (visibility_change => 1); |
299 | $self->emit (visibility_change => 1); |
295 | |
300 | |
296 | $self->realloc if !exists $self->{req_w}; |
301 | $self->realloc if !exists $self->{req_w}; |
297 | |
302 | |
298 | $_->set_visible for $self->children; |
303 | $_->set_visible for $self->visible_children; |
299 | } |
304 | } |
300 | |
305 | |
301 | sub set_invisible { |
306 | sub set_invisible { |
302 | my ($self) = @_; |
307 | my ($self) = @_; |
303 | |
308 | |
… | |
… | |
309 | delete $self->{root}; |
314 | delete $self->{root}; |
310 | |
315 | |
311 | undef $GRAB if $GRAB == $self; |
316 | undef $GRAB if $GRAB == $self; |
312 | undef $HOVER if $HOVER == $self; |
317 | undef $HOVER if $HOVER == $self; |
313 | |
318 | |
314 | $CFPlus::UI::TOOLTIP_WATCHER->invoke |
319 | $DC::UI::TOOLTIP_WATCHER->invoke |
315 | if $TOOLTIP->{owner} == $self; |
320 | if $TOOLTIP->{owner} == $self; |
316 | |
321 | |
317 | $self->emit ("focus_out"); |
322 | $self->emit ("focus_out"); |
318 | $self->emit (visibility_change => 0); |
323 | $self->emit (visibility_change => 0); |
319 | } |
324 | } |
… | |
… | |
338 | sub hide { |
343 | sub hide { |
339 | my ($self) = @_; |
344 | my ($self) = @_; |
340 | |
345 | |
341 | $self->set_invisible; |
346 | $self->set_invisible; |
342 | |
347 | |
|
|
348 | # extra $parent copy for 5.8.8+ bug workaround |
|
|
349 | # (otherwise $_[0] in remove gets freed |
|
|
350 | if (my $parent = $self->{parent}) { |
343 | $self->{parent}->remove ($self) |
351 | $parent->remove ($self); |
344 | if $self->{parent}; |
352 | } |
345 | } |
353 | } |
346 | |
354 | |
347 | sub move_abs { |
355 | sub move_abs { |
348 | my ($self, $x, $y, $z) = @_; |
356 | my ($self, $x, $y, $z) = @_; |
349 | |
357 | |
… | |
… | |
365 | |
373 | |
366 | # traverse the widget chain up to find the maximum "physical" size constraints |
374 | # traverse the widget chain up to find the maximum "physical" size constraints |
367 | sub get_max_wh { |
375 | sub get_max_wh { |
368 | my ($self) = @_; |
376 | my ($self) = @_; |
369 | |
377 | |
|
|
378 | my ($w, $h) = @$self{qw(max_w max_h)}; |
|
|
379 | |
|
|
380 | if ($w <= 0 || $h <= 0) { |
|
|
381 | my ($mw, $mh) = $self->{parent} |
370 | return $self->{parent}->get_max_wh |
382 | ? $self->{parent}->get_max_wh |
371 | if $self->{parent}; |
|
|
372 | |
|
|
373 | ($::WIDTH, $::HEIGHT) |
383 | : ($::WIDTH, $::HEIGHT); |
|
|
384 | |
|
|
385 | $w = $mw if $w <= 0; |
|
|
386 | $h = $mh if $h <= 0; |
|
|
387 | } |
|
|
388 | |
|
|
389 | ($w, $h) |
374 | } |
390 | } |
375 | |
391 | |
376 | sub size_request { |
392 | sub size_request { |
377 | require Carp; |
393 | require Carp; |
378 | Carp::confess "size_request is abstract"; |
394 | Carp::confess "size_request is abstract"; |
… | |
… | |
386 | my ($self, $x, $y, $w, $h) = @_; |
402 | my ($self, $x, $y, $w, $h) = @_; |
387 | |
403 | |
388 | if ($self->{aspect}) { |
404 | if ($self->{aspect}) { |
389 | my ($ow, $oh) = ($w, $h); |
405 | my ($ow, $oh) = ($w, $h); |
390 | |
406 | |
391 | $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; |
407 | $w = List::Util::min $w, DC::ceil $h * $self->{aspect}; |
392 | $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; |
408 | $h = List::Util::min $h, DC::ceil $w / $self->{aspect}; |
393 | |
409 | |
394 | # use alignment to adjust x, y |
410 | # use alignment to adjust x, y |
395 | |
411 | |
396 | $x += int 0.5 * ($ow - $w); |
412 | $x += int 0.5 * ($ow - $w); |
397 | $y += int 0.5 * ($oh - $h); |
413 | $y += int 0.5 * ($oh - $h); |
… | |
… | |
438 | |
454 | |
439 | return if $self->{tooltip} eq $tooltip; |
455 | return if $self->{tooltip} eq $tooltip; |
440 | |
456 | |
441 | $self->{tooltip} = $tooltip; |
457 | $self->{tooltip} = $tooltip; |
442 | |
458 | |
443 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
459 | if ($DC::UI::TOOLTIP->{owner} == $self) { |
444 | delete $CFPlus::UI::TOOLTIP->{owner}; |
460 | delete $DC::UI::TOOLTIP->{owner}; |
445 | $CFPlus::UI::TOOLTIP_WATCHER->invoke; |
461 | $DC::UI::TOOLTIP_WATCHER->invoke; |
446 | } |
462 | } |
447 | } |
463 | } |
448 | |
464 | |
449 | # translate global coordinates to local coordinate system |
465 | # translate global coordinates to local coordinate system |
450 | sub coord2local { |
466 | sub coord2local { |
451 | my ($self, $x, $y) = @_; |
467 | my ($self, $x, $y) = @_; |
452 | |
468 | |
453 | Carp::confess unless $self->{parent};#d# |
469 | return (undef, undef) unless $self->{parent}; |
454 | |
470 | |
455 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
471 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
456 | } |
472 | } |
457 | |
473 | |
458 | # translate local coordinates to global coordinate system |
474 | # translate local coordinates to global coordinate system |
459 | sub coord2global { |
475 | sub coord2global { |
460 | my ($self, $x, $y) = @_; |
476 | my ($self, $x, $y) = @_; |
461 | |
477 | |
462 | Carp::confess unless $self->{parent};#d# |
478 | return (undef, undef) unless $self->{parent}; |
463 | |
479 | |
464 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
480 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
465 | } |
481 | } |
466 | |
482 | |
467 | sub invoke_focus_in { |
483 | sub invoke_focus_in { |
… | |
… | |
516 | sub connect { |
532 | sub connect { |
517 | my ($self, $signal, $cb) = @_; |
533 | my ($self, $signal, $cb) = @_; |
518 | |
534 | |
519 | push @{ $self->{signal_cb}{$signal} }, $cb; |
535 | push @{ $self->{signal_cb}{$signal} }, $cb; |
520 | |
536 | |
521 | defined wantarray and CFPlus::guard { |
537 | defined wantarray and Guard::guard { |
522 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
538 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
523 | @{ $self->{signal_cb}{$signal} }; |
539 | @{ $self->{signal_cb}{$signal} }; |
524 | } |
540 | } |
525 | } |
541 | } |
526 | |
542 | |
… | |
… | |
564 | # in .xs |
580 | # in .xs |
565 | |
581 | |
566 | sub set_parent { |
582 | sub set_parent { |
567 | my ($self, $parent) = @_; |
583 | my ($self, $parent) = @_; |
568 | |
584 | |
569 | CFPlus::weaken ($self->{parent} = $parent); |
585 | DC::weaken ($self->{parent} = $parent); |
570 | $self->set_visible if $parent->{visible}; |
586 | $self->set_visible if $parent->{visible}; |
571 | } |
587 | } |
572 | |
588 | |
573 | sub realloc { |
589 | sub realloc { |
574 | my ($self) = @_; |
590 | my ($self) = @_; |
… | |
… | |
612 | } |
628 | } |
613 | |
629 | |
614 | sub DESTROY { |
630 | sub DESTROY { |
615 | my ($self) = @_; |
631 | my ($self) = @_; |
616 | |
632 | |
617 | return if CFPlus::in_destruct; |
633 | return if DC::in_destruct; |
618 | |
634 | |
619 | local $@; |
635 | local $@; |
620 | eval { $self->destroy }; |
636 | eval { $self->destroy }; |
621 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
637 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
622 | |
638 | |
623 | delete $WIDGET{$self+0}; |
639 | delete $WIDGET{$self+0}; |
624 | } |
640 | } |
625 | |
641 | |
626 | ############################################################################# |
642 | ############################################################################# |
627 | |
643 | |
628 | package CFPlus::UI::DrawBG; |
644 | package DC::UI::DrawBG; |
629 | |
645 | |
630 | our @ISA = CFPlus::UI::Base::; |
646 | our @ISA = DC::UI::Base::; |
631 | |
647 | |
632 | use strict; |
648 | use strict; |
633 | use CFPlus::OpenGL; |
649 | use DC::OpenGL; |
634 | |
650 | |
635 | sub new { |
651 | sub new { |
636 | my $class = shift; |
652 | my $class = shift; |
637 | |
653 | |
638 | $class->SUPER::new ( |
654 | $class->SUPER::new ( |
… | |
… | |
660 | } |
676 | } |
661 | } |
677 | } |
662 | |
678 | |
663 | ############################################################################# |
679 | ############################################################################# |
664 | |
680 | |
665 | package CFPlus::UI::Empty; |
681 | package DC::UI::Empty; |
666 | |
682 | |
667 | our @ISA = CFPlus::UI::Base::; |
683 | our @ISA = DC::UI::Base::; |
668 | |
684 | |
669 | sub new { |
685 | sub new { |
670 | my ($class, %arg) = @_; |
686 | my ($class, %arg) = @_; |
671 | $class->SUPER::new (can_events => 0, %arg); |
687 | $class->SUPER::new (can_events => 0, %arg); |
672 | } |
688 | } |
… | |
… | |
679 | |
695 | |
680 | sub draw { } |
696 | sub draw { } |
681 | |
697 | |
682 | ############################################################################# |
698 | ############################################################################# |
683 | |
699 | |
684 | package CFPlus::UI::Container; |
700 | package DC::UI::Container; |
685 | |
701 | |
686 | our @ISA = CFPlus::UI::Base::; |
702 | our @ISA = DC::UI::Base::; |
687 | |
703 | |
688 | sub new { |
704 | sub new { |
689 | my ($class, %arg) = @_; |
705 | my ($class, %arg) = @_; |
690 | |
706 | |
691 | my $children = delete $arg{children}; |
707 | my $children = delete $arg{children}; |
… | |
… | |
785 | $_->draw for $self->visible_children; |
801 | $_->draw for $self->visible_children; |
786 | } |
802 | } |
787 | |
803 | |
788 | ############################################################################# |
804 | ############################################################################# |
789 | |
805 | |
790 | package CFPlus::UI::Bin; |
806 | package DC::UI::Bin; |
791 | |
807 | |
792 | our @ISA = CFPlus::UI::Container::; |
808 | our @ISA = DC::UI::Container::; |
793 | |
809 | |
794 | sub new { |
810 | sub new { |
795 | my ($class, %arg) = @_; |
811 | my ($class, %arg) = @_; |
796 | |
812 | |
797 | my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; |
813 | my $child = (delete $arg{child}) || new DC::UI::Empty::; |
798 | |
814 | |
799 | $class->SUPER::new (children => [$child], %arg) |
815 | $class->SUPER::new (children => [$child], %arg) |
800 | } |
816 | } |
801 | |
817 | |
802 | sub add { |
818 | sub add { |
… | |
… | |
809 | sub remove { |
825 | sub remove { |
810 | my ($self, $widget) = @_; |
826 | my ($self, $widget) = @_; |
811 | |
827 | |
812 | $self->SUPER::remove ($widget); |
828 | $self->SUPER::remove ($widget); |
813 | |
829 | |
814 | $self->{children} = [new CFPlus::UI::Empty] |
830 | $self->{children} = [new DC::UI::Empty] |
815 | unless @{$self->{children}}; |
831 | unless @{$self->{children}}; |
816 | } |
832 | } |
817 | |
833 | |
818 | sub child { $_[0]->{children}[0] } |
834 | sub child { $_[0]->{children}[0] } |
819 | |
835 | |
… | |
… | |
830 | } |
846 | } |
831 | |
847 | |
832 | ############################################################################# |
848 | ############################################################################# |
833 | # back-buffered drawing area |
849 | # back-buffered drawing area |
834 | |
850 | |
835 | package CFPlus::UI::Window; |
851 | package DC::UI::Window; |
836 | |
852 | |
837 | our @ISA = CFPlus::UI::Bin::; |
853 | our @ISA = DC::UI::Bin::; |
838 | |
854 | |
839 | use CFPlus::OpenGL; |
855 | use DC::OpenGL; |
840 | |
856 | |
841 | sub new { |
857 | sub new { |
842 | my ($class, %arg) = @_; |
858 | my ($class, %arg) = @_; |
843 | |
859 | |
844 | my $self = $class->SUPER::new (%arg); |
860 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
866 | } |
882 | } |
867 | |
883 | |
868 | sub render_child { |
884 | sub render_child { |
869 | my ($self) = @_; |
885 | my ($self) = @_; |
870 | |
886 | |
871 | $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { |
887 | $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub { |
872 | glClearColor 0, 0, 0, 0; |
888 | glClearColor 0, 0, 0, 0; |
873 | glClear GL_COLOR_BUFFER_BIT; |
889 | glClear GL_COLOR_BUFFER_BIT; |
874 | |
890 | |
875 | { |
891 | { |
876 | package CFPlus::UI::Base; |
892 | package DC::UI::Base; |
877 | |
893 | |
878 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
894 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
879 | (0, 0, $self->{w}, $self->{h}); |
895 | (0, 0, $self->{w}, $self->{h}); |
880 | |
896 | |
881 | $self->_render; |
897 | $self->_render; |
… | |
… | |
898 | glDisable GL_TEXTURE_2D; |
914 | glDisable GL_TEXTURE_2D; |
899 | } |
915 | } |
900 | |
916 | |
901 | ############################################################################# |
917 | ############################################################################# |
902 | |
918 | |
903 | package CFPlus::UI::ViewPort; |
919 | package DC::UI::ViewPort; |
904 | |
920 | |
905 | use List::Util qw(min max); |
921 | use List::Util qw(min max); |
906 | |
922 | |
907 | our @ISA = CFPlus::UI::Window::; |
923 | our @ISA = DC::UI::Window::; |
908 | |
924 | |
909 | sub new { |
925 | sub new { |
910 | my $class = shift; |
926 | my $class = shift; |
911 | |
927 | |
912 | $class->SUPER::new ( |
928 | $class->SUPER::new ( |
… | |
… | |
996 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
1012 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
997 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1013 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
998 | ) { |
1014 | ) { |
999 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1015 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1000 | } else { |
1016 | } else { |
1001 | $self->CFPlus::UI::Base::find_widget ($x, $y) |
1017 | $self->DC::UI::Base::find_widget ($x, $y) |
1002 | } |
1018 | } |
1003 | } |
1019 | } |
1004 | |
1020 | |
1005 | sub _render { |
1021 | sub _render { |
1006 | my ($self) = @_; |
1022 | my ($self) = @_; |
1007 | |
1023 | |
1008 | local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; |
1024 | local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x}; |
1009 | local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; |
1025 | local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y}; |
1010 | |
1026 | |
1011 | CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1027 | DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1012 | |
1028 | |
1013 | $self->SUPER::_render; |
1029 | $self->SUPER::_render; |
1014 | } |
1030 | } |
1015 | |
1031 | |
1016 | ############################################################################# |
1032 | ############################################################################# |
1017 | |
1033 | |
1018 | package CFPlus::UI::ScrolledWindow; |
1034 | package DC::UI::ScrolledWindow; |
1019 | |
1035 | |
1020 | our @ISA = CFPlus::UI::Table::; |
1036 | our @ISA = DC::UI::Table::; |
1021 | |
1037 | |
1022 | sub new { |
1038 | sub new { |
1023 | my ($class, %arg) = @_; |
1039 | my ($class, %arg) = @_; |
1024 | |
1040 | |
1025 | my $child = delete $arg{child}; |
1041 | my $child = delete $arg{child}; |
1026 | |
1042 | |
1027 | my $self; |
1043 | my $self; |
1028 | |
1044 | |
1029 | my $hslider = new CFPlus::UI::Slider |
1045 | my $hslider = new DC::UI::Slider |
1030 | c_col => 0, |
1046 | c_col => 0, |
1031 | c_row => 1, |
1047 | c_row => 1, |
1032 | vertical => 0, |
1048 | vertical => 0, |
1033 | range => [0, 0, 1, 0.01], # HACK fix |
1049 | range => [0, 0, 1, 0.01], # HACK fix |
1034 | on_changed => sub { |
1050 | on_changed => sub { |
1035 | $self->{hpos} = $_[1]; |
1051 | $self->{hpos} = $_[1]; |
1036 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1052 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1037 | }, |
1053 | }, |
1038 | ; |
1054 | ; |
1039 | |
1055 | |
1040 | my $vslider = new CFPlus::UI::Slider |
1056 | my $vslider = new DC::UI::Slider |
1041 | c_col => 1, |
1057 | c_col => 1, |
1042 | c_row => 0, |
1058 | c_row => 0, |
1043 | vertical => 1, |
1059 | vertical => 1, |
1044 | range => [0, 0, 1, 0.01], # HACK fix |
1060 | range => [0, 0, 1, 0.01], # HACK fix |
1045 | on_changed => sub { |
1061 | on_changed => sub { |
… | |
… | |
1057 | col_expand => [1, 0], |
1073 | col_expand => [1, 0], |
1058 | row_expand => [1, 0], |
1074 | row_expand => [1, 0], |
1059 | %arg, |
1075 | %arg, |
1060 | ); |
1076 | ); |
1061 | |
1077 | |
1062 | $self->{vp} = new CFPlus::UI::ViewPort |
1078 | $self->{vp} = new DC::UI::ViewPort |
1063 | c_col => 0, |
1079 | c_col => 0, |
1064 | c_row => 0, |
1080 | c_row => 0, |
1065 | expand => 1, |
1081 | expand => 1, |
1066 | scroll_x => $self->{scroll_x}, |
1082 | scroll_x => $self->{scroll_x}, |
1067 | scroll_y => $self->{scroll_y}, |
1083 | scroll_y => $self->{scroll_y}, |
… | |
… | |
1193 | $self->SUPER::invoke_size_allocate ($w, $h) |
1209 | $self->SUPER::invoke_size_allocate ($w, $h) |
1194 | } |
1210 | } |
1195 | |
1211 | |
1196 | ############################################################################# |
1212 | ############################################################################# |
1197 | |
1213 | |
1198 | package CFPlus::UI::Frame; |
1214 | package DC::UI::Frame; |
1199 | |
1215 | |
1200 | our @ISA = CFPlus::UI::Bin::; |
1216 | our @ISA = DC::UI::Bin::; |
1201 | |
1217 | |
1202 | use CFPlus::OpenGL; |
1218 | use DC::OpenGL; |
1203 | |
1219 | |
1204 | sub new { |
1220 | sub new { |
1205 | my $class = shift; |
1221 | my $class = shift; |
1206 | |
1222 | |
1207 | $class->SUPER::new ( |
1223 | $class->SUPER::new ( |
… | |
… | |
1226 | $self->SUPER::_draw; |
1242 | $self->SUPER::_draw; |
1227 | } |
1243 | } |
1228 | |
1244 | |
1229 | ############################################################################# |
1245 | ############################################################################# |
1230 | |
1246 | |
1231 | package CFPlus::UI::FancyFrame; |
1247 | package DC::UI::FancyFrame; |
1232 | |
1248 | |
1233 | our @ISA = CFPlus::UI::Bin::; |
1249 | our @ISA = DC::UI::Bin::; |
1234 | |
1250 | |
1235 | use CFPlus::OpenGL; |
1251 | use DC::OpenGL; |
1236 | |
1252 | |
1237 | sub new { |
1253 | sub new { |
1238 | my ($class, %arg) = @_; |
1254 | my ($class, %arg) = @_; |
1239 | |
1255 | |
1240 | if ((exists $arg{label}) && !ref $arg{label}) { |
1256 | if ((exists $arg{label}) && !ref $arg{label}) { |
1241 | $arg{label} = new CFPlus::UI::Label |
1257 | $arg{label} = new DC::UI::Label |
1242 | align => 1, |
1258 | align => 1, |
1243 | valign => 0, |
1259 | valign => 0.5, |
1244 | text => $arg{label}, |
1260 | text => $arg{label}, |
1245 | fontsize => ($arg{border} || 0.8) * 0.75; |
1261 | fontsize => ($arg{border} || 0.8) * 0.75; |
1246 | } |
1262 | } |
1247 | |
1263 | |
1248 | my $self = $class->SUPER::new ( |
1264 | my $self = $class->SUPER::new ( |
1249 | # label => "", |
1265 | # label => "", |
1250 | fg => [0.6, 0.3, 0.1], |
1266 | fg => undef, |
1251 | border => 0.8, |
1267 | border => 0.8, |
1252 | style => 'single', |
1268 | style => 'single', |
1253 | %arg, |
1269 | %arg, |
1254 | ); |
1270 | ); |
1255 | |
1271 | |
… | |
… | |
1258 | |
1274 | |
1259 | sub add { |
1275 | sub add { |
1260 | my ($self, @widgets) = @_; |
1276 | my ($self, @widgets) = @_; |
1261 | |
1277 | |
1262 | $self->SUPER::add (@widgets); |
1278 | $self->SUPER::add (@widgets); |
1263 | $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; |
1279 | $self->DC::UI::Container::add ($self->{label}) if $self->{label}; |
1264 | } |
1280 | } |
1265 | |
1281 | |
1266 | sub border { |
1282 | sub border { |
1267 | int $_[0]{border} * $::FONTSIZE |
1283 | int $_[0]{border} * $::FONTSIZE |
1268 | } |
1284 | } |
… | |
… | |
1308 | my $border = $self->border; |
1324 | my $border = $self->border; |
1309 | my ($w, $h) = ($self->{w}, $self->{h}); |
1325 | my ($w, $h) = ($self->{w}, $self->{h}); |
1310 | |
1326 | |
1311 | $child->draw; |
1327 | $child->draw; |
1312 | |
1328 | |
1313 | glColor @{$self->{fg}}; |
1329 | glColor @{$self->{fg} || $DC::THEME{fancyframe}}; |
1314 | glBegin GL_LINE_STRIP; |
1330 | glBegin GL_LINE_STRIP; |
1315 | glVertex $border * 1.5 , $border * 0.5 + 0.5; |
1331 | glVertex $border * 1.5 , $border * 0.5 + 0.5; |
1316 | glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
1332 | glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
1317 | glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1333 | glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1318 | glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1334 | glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
… | |
… | |
1326 | } |
1342 | } |
1327 | } |
1343 | } |
1328 | |
1344 | |
1329 | ############################################################################# |
1345 | ############################################################################# |
1330 | |
1346 | |
1331 | package CFPlus::UI::Toplevel; |
1347 | package DC::UI::Toplevel; |
1332 | |
1348 | |
1333 | our @ISA = CFPlus::UI::Bin::; |
1349 | our @ISA = DC::UI::Bin::; |
1334 | |
1350 | |
1335 | use CFPlus::OpenGL; |
1351 | use DC::OpenGL; |
1336 | |
1352 | |
1337 | my $bg = |
1353 | my $bg = |
1338 | new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", |
1354 | new_from_resource DC::Texture "d1_bg.png", |
1339 | mipmap => 1, wrap => 1; |
1355 | mipmap => 1, wrap => 1; |
1340 | |
1356 | |
1341 | my @border = |
1357 | my @border = |
1342 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1358 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
1343 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1359 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1344 | |
1360 | |
1345 | my @icon = |
1361 | my @icon = |
1346 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1362 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
1347 | qw(x1_move.png x1_resize.png); |
1363 | qw(x1_move.png x1_resize.png); |
1348 | |
1364 | |
1349 | sub new { |
1365 | sub new { |
1350 | my ($class, %arg) = @_; |
1366 | my ($class, %arg) = @_; |
1351 | |
1367 | |
1352 | my $self = $class->SUPER::new ( |
1368 | my $self = $class->SUPER::new ( |
1353 | bg => [1, 1, 1, 1], |
1369 | bg => [1, 1, 1, 1], |
1354 | border_bg => [1, 1, 1, 1], |
1370 | border_bg => [1, 1, 1, 1], |
1355 | border => 0.6, |
1371 | border => 0.8, |
1356 | can_events => 1, |
1372 | can_events => 1, |
1357 | min_w => 64, |
1373 | min_w => 64, |
1358 | min_h => 32, |
1374 | min_h => 32, |
1359 | %arg, |
1375 | %arg, |
1360 | ); |
1376 | ); |
1361 | |
1377 | |
1362 | $self->{title_widget} = new CFPlus::UI::Label |
1378 | $self->{title_widget} = new DC::UI::Label |
1363 | align => 0, |
1379 | align => 0.5, |
1364 | valign => 1, |
1380 | valign => 1, |
1365 | text => $self->{title}, |
1381 | text => $self->{title}, |
1366 | fontsize => $self->{border}, |
1382 | fontsize => $self->{border}, |
1367 | if exists $self->{title}; |
1383 | if exists $self->{title}; |
1368 | |
1384 | |
1369 | if ($self->{has_close_button}) { |
1385 | if ($self->{has_close_button}) { |
1370 | $self->{close_button} = |
1386 | $self->{close_button} = |
1371 | new CFPlus::UI::ImageButton |
1387 | new DC::UI::ImageButton |
1372 | path => 'x1_close.png', |
1388 | path => 'x1_close.png', |
1373 | on_activate => sub { $self->emit ("delete") }; |
1389 | on_activate => sub { $self->emit ("delete") }; |
1374 | |
1390 | |
1375 | $self->CFPlus::UI::Container::add ($self->{close_button}); |
1391 | $self->DC::UI::Container::add ($self->{close_button}); |
1376 | } |
1392 | } |
1377 | |
1393 | |
1378 | $self |
1394 | $self |
1379 | } |
1395 | } |
1380 | |
1396 | |
1381 | sub add { |
1397 | sub add { |
1382 | my ($self, @widgets) = @_; |
1398 | my ($self, @widgets) = @_; |
1383 | |
1399 | |
1384 | $self->SUPER::add (@widgets); |
1400 | $self->SUPER::add (@widgets); |
1385 | $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1401 | $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1386 | $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1402 | $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1387 | } |
1403 | } |
1388 | |
1404 | |
1389 | sub border { |
1405 | sub border { |
1390 | int $_[0]{border} * $::FONTSIZE |
1406 | int $_[0]{border} * $::FONTSIZE |
1391 | } |
1407 | } |
… | |
… | |
1529 | glEnable GL_TEXTURE_2D; |
1545 | glEnable GL_TEXTURE_2D; |
1530 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1546 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1531 | |
1547 | |
1532 | my $border = $self->border; |
1548 | my $border = $self->border; |
1533 | |
1549 | |
|
|
1550 | if ($border) { |
1534 | glColor @{ $self->{border_bg} }; |
1551 | glColor @{ $self->{border_bg} }; |
1535 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1552 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1536 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1553 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1537 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1554 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1538 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
1555 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
1539 | |
1556 | |
1540 | # move |
1557 | # move |
1541 | my $w2 = ($w - $border) * .5; |
1558 | my $w2 = ($w - $border) * .5; |
1542 | my $h2 = ($h - $border) * .5; |
1559 | my $h2 = ($h - $border) * .5; |
1543 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
1560 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
1544 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
1561 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
1545 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
1562 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
1546 | |
1563 | |
1547 | # resize |
1564 | # resize |
1548 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
1565 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
1549 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
1566 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
1550 | unless $self->{has_close_button}; |
1567 | unless $self->{has_close_button}; |
1551 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
1568 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
1552 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
1569 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
|
|
1570 | } |
1553 | |
1571 | |
1554 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1572 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1555 | glColor @{ $self->{bg} }; |
1573 | glColor @{ $self->{bg} }; |
1556 | |
1574 | |
1557 | # TODO: repeat texture not scale |
1575 | # TODO: repeat texture not scale |
… | |
… | |
1576 | if $self->{close_button}; |
1594 | if $self->{close_button}; |
1577 | } |
1595 | } |
1578 | |
1596 | |
1579 | ############################################################################# |
1597 | ############################################################################# |
1580 | |
1598 | |
1581 | package CFPlus::UI::Table; |
1599 | package DC::UI::Table; |
1582 | |
1600 | |
1583 | our @ISA = CFPlus::UI::Container::; |
1601 | our @ISA = DC::UI::Container::; |
1584 | |
1602 | |
1585 | use List::Util qw(max sum); |
1603 | use List::Util qw(max sum); |
1586 | |
1604 | |
1587 | use CFPlus::OpenGL; |
1605 | use DC::OpenGL; |
1588 | |
1606 | |
1589 | sub new { |
1607 | sub new { |
1590 | my $class = shift; |
1608 | my $class = shift; |
1591 | |
1609 | |
1592 | $class->SUPER::new ( |
1610 | $class->SUPER::new ( |
… | |
… | |
1636 | my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; |
1654 | my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; |
1637 | |
1655 | |
1638 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1656 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1639 | |
1657 | |
1640 | if ($w > $sw) { |
1658 | if ($w > $sw) { |
1641 | $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1659 | $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1642 | } |
1660 | } |
1643 | } |
1661 | } |
1644 | |
1662 | |
1645 | # second pass, rows |
1663 | # second pass, rows |
1646 | for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { |
1664 | for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { |
1647 | my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; |
1665 | my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; |
1648 | |
1666 | |
1649 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1667 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1650 | |
1668 | |
1651 | if ($h > $sh) { |
1669 | if ($h > $sh) { |
1652 | $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1670 | $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1653 | } |
1671 | } |
1654 | } |
1672 | } |
1655 | |
1673 | |
1656 | (\@w, \@h) |
1674 | (\@w, \@h) |
1657 | } |
1675 | } |
… | |
… | |
1680 | @col_expand = (1) x @$ws unless @col_expand; |
1698 | @col_expand = (1) x @$ws unless @col_expand; |
1681 | my $col_expand = (sum @col_expand) || 1; |
1699 | my $col_expand = (sum @col_expand) || 1; |
1682 | |
1700 | |
1683 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1701 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1684 | |
1702 | |
1685 | CFPlus::UI::harmonize $ws; |
1703 | DC::UI::harmonize $ws; |
1686 | |
1704 | |
1687 | my @row_expand = @{$self->{row_expand}}; |
1705 | my @row_expand = @{$self->{row_expand}}; |
1688 | @row_expand = (1) x @$ws unless @row_expand; |
1706 | @row_expand = (1) x @$ws unless @row_expand; |
1689 | my $row_expand = (sum @row_expand) || 1; |
1707 | my $row_expand = (sum @row_expand) || 1; |
1690 | |
1708 | |
1691 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1709 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1692 | |
1710 | |
1693 | CFPlus::UI::harmonize $hs; |
1711 | DC::UI::harmonize $hs; |
1694 | |
1712 | |
1695 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1713 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1696 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1714 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1697 | |
1715 | |
1698 | for my $widget ($self->children) { |
1716 | for my $widget ($self->children) { |
… | |
… | |
1707 | 1 |
1725 | 1 |
1708 | } |
1726 | } |
1709 | |
1727 | |
1710 | ############################################################################# |
1728 | ############################################################################# |
1711 | |
1729 | |
1712 | package CFPlus::UI::Fixed; |
1730 | package DC::UI::Fixed; |
1713 | |
1731 | |
1714 | use List::Util qw(min max); |
1732 | use List::Util qw(min max); |
1715 | |
1733 | |
1716 | our @ISA = CFPlus::UI::Container::; |
1734 | our @ISA = DC::UI::Container::; |
1717 | |
1735 | |
1718 | sub _scale($$$) { |
1736 | sub _scale($$$) { |
1719 | my ($rel, $val, $max) = @_; |
1737 | my ($rel, $val, $max) = @_; |
1720 | |
1738 | |
1721 | $rel ? $val * $max : $val |
1739 | $rel ? $val * $max : $val |
… | |
… | |
1773 | 1 |
1791 | 1 |
1774 | } |
1792 | } |
1775 | |
1793 | |
1776 | ############################################################################# |
1794 | ############################################################################# |
1777 | |
1795 | |
1778 | package CFPlus::UI::Box; |
1796 | package DC::UI::Box; |
1779 | |
1797 | |
1780 | our @ISA = CFPlus::UI::Container::; |
1798 | our @ISA = DC::UI::Container::; |
1781 | |
1799 | |
1782 | sub size_request { |
1800 | sub size_request { |
1783 | my ($self) = @_; |
1801 | my ($self) = @_; |
1784 | |
1802 | |
1785 | my @children = $self->visible_children; |
1803 | my @children = $self->visible_children; |
… | |
… | |
1820 | $req[$_] += $space * $children[$_]{expand} |
1838 | $req[$_] += $space * $children[$_]{expand} |
1821 | for 0 .. $#children; |
1839 | for 0 .. $#children; |
1822 | } |
1840 | } |
1823 | } |
1841 | } |
1824 | |
1842 | |
1825 | CFPlus::UI::harmonize \@req; |
1843 | DC::UI::harmonize \@req; |
1826 | |
1844 | |
1827 | my $pos = 0; |
1845 | my $pos = 0; |
1828 | for (0 .. $#children) { |
1846 | for (0 .. $#children) { |
1829 | my $alloc = $req[$_]; |
1847 | my $alloc = $req[$_]; |
1830 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1848 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
… | |
… | |
1835 | 1 |
1853 | 1 |
1836 | } |
1854 | } |
1837 | |
1855 | |
1838 | ############################################################################# |
1856 | ############################################################################# |
1839 | |
1857 | |
1840 | package CFPlus::UI::HBox; |
1858 | package DC::UI::HBox; |
1841 | |
1859 | |
1842 | our @ISA = CFPlus::UI::Box::; |
1860 | our @ISA = DC::UI::Box::; |
1843 | |
1861 | |
1844 | sub new { |
1862 | sub new { |
1845 | my $class = shift; |
1863 | my $class = shift; |
1846 | |
1864 | |
1847 | $class->SUPER::new ( |
1865 | $class->SUPER::new ( |
… | |
… | |
1850 | ) |
1868 | ) |
1851 | } |
1869 | } |
1852 | |
1870 | |
1853 | ############################################################################# |
1871 | ############################################################################# |
1854 | |
1872 | |
1855 | package CFPlus::UI::VBox; |
1873 | package DC::UI::VBox; |
1856 | |
1874 | |
1857 | our @ISA = CFPlus::UI::Box::; |
1875 | our @ISA = DC::UI::Box::; |
1858 | |
1876 | |
1859 | sub new { |
1877 | sub new { |
1860 | my $class = shift; |
1878 | my $class = shift; |
1861 | |
1879 | |
1862 | $class->SUPER::new ( |
1880 | $class->SUPER::new ( |
… | |
… | |
1865 | ) |
1883 | ) |
1866 | } |
1884 | } |
1867 | |
1885 | |
1868 | ############################################################################# |
1886 | ############################################################################# |
1869 | |
1887 | |
1870 | package CFPlus::UI::Label; |
1888 | package DC::UI::Label; |
1871 | |
1889 | |
1872 | our @ISA = CFPlus::UI::DrawBG::; |
1890 | our @ISA = DC::UI::DrawBG::; |
1873 | |
1891 | |
1874 | use CFPlus::OpenGL; |
1892 | use DC::OpenGL; |
1875 | |
1893 | |
1876 | sub new { |
1894 | sub new { |
1877 | my ($class, %arg) = @_; |
1895 | my ($class, %arg) = @_; |
1878 | |
1896 | |
1879 | my $self = $class->SUPER::new ( |
1897 | my $self = $class->SUPER::new ( |
… | |
… | |
1884 | #text => initial text |
1902 | #text => initial text |
1885 | #markup => initial narkup |
1903 | #markup => initial narkup |
1886 | #max_w => maximum pixel width |
1904 | #max_w => maximum pixel width |
1887 | #style => 0, # render flags |
1905 | #style => 0, # render flags |
1888 | ellipsise => 3, # end |
1906 | ellipsise => 3, # end |
1889 | layout => (new CFPlus::Layout), |
1907 | layout => (new DC::Layout), |
1890 | fontsize => 1, |
1908 | fontsize => 1, |
1891 | align => -1, |
1909 | align => 0.5, |
1892 | valign => -1, |
1910 | valign => 0.5, |
1893 | padding_x => 2, |
1911 | padding_x => 4, |
1894 | padding_y => 2, |
1912 | padding_y => 2, |
1895 | can_events => 0, |
1913 | can_events => 0, |
1896 | %arg |
1914 | %arg |
1897 | ); |
1915 | ); |
1898 | |
1916 | |
1899 | if (exists $self->{template}) { |
1917 | if (exists $self->{template}) { |
1900 | my $layout = new CFPlus::Layout; |
1918 | my $layout = new DC::Layout; |
1901 | $layout->set_text (delete $self->{template}); |
1919 | $layout->set_text (delete $self->{template}); |
1902 | $self->{template} = $layout; |
1920 | $self->{template} = $layout; |
1903 | } |
1921 | } |
1904 | |
1922 | |
1905 | if (exists $self->{markup}) { |
1923 | if (exists $self->{markup}) { |
… | |
… | |
1964 | |
1982 | |
1965 | $self->{size_req} ||= do { |
1983 | $self->{size_req} ||= do { |
1966 | my ($max_w, $max_h) = $self->get_max_wh; |
1984 | my ($max_w, $max_h) = $self->get_max_wh; |
1967 | |
1985 | |
1968 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1986 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1969 | $self->{layout}->set_width ($self->{max_w} || $max_w || -1); |
1987 | $self->{layout}->set_width ($max_w); |
1970 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1988 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1971 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1989 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1972 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1990 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1973 | |
1991 | |
1974 | my ($w, $h) = $self->{layout}->size; |
1992 | my ($w, $h) = $self->{layout}->size; |
1975 | |
1993 | |
1976 | if (exists $self->{template}) { |
1994 | if (exists $self->{template}) { |
1977 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1995 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1978 | $self->{template}->set_width ($self->{max_w} || -1); |
1996 | $self->{template}->set_width ($max_w); |
1979 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1997 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1980 | |
1998 | |
1981 | my ($w2, $h2) = $self->{template}->size; |
1999 | my ($w2, $h2) = $self->{template}->size; |
1982 | |
2000 | |
1983 | $w = List::Util::max $w, $w2; |
2001 | $w = List::Util::max $w, $w2; |
… | |
… | |
2039 | |
2057 | |
2040 | [$self->{layout}->size] |
2058 | [$self->{layout}->size] |
2041 | }; |
2059 | }; |
2042 | |
2060 | |
2043 | unless (exists $self->{ox}) { |
2061 | unless (exists $self->{ox}) { |
2044 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
2062 | $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2); |
2045 | : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} |
2063 | $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2); |
2046 | : ($self->{w} - $size->[0]) * 0.5); |
|
|
2047 | |
|
|
2048 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
|
|
2049 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
|
|
2050 | : ($self->{h} - $size->[1]) * 0.5); |
|
|
2051 | |
2064 | |
2052 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2065 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2053 | }; |
2066 | }; |
2054 | |
2067 | |
2055 | # unless ($self->{list}) { |
2068 | # unless ($self->{list}) { |
2056 | # $self->{list} = CFPlus::OpenGL::glGenList; |
2069 | # $self->{list} = DC::OpenGL::glGenList; |
2057 | # CFPlus::OpenGL::glNewList $self->{list}; |
2070 | # DC::OpenGL::glNewList $self->{list}; |
2058 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2071 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2059 | # CFPlus::OpenGL::glEndList; |
2072 | # DC::OpenGL::glEndList; |
2060 | # } |
2073 | # } |
2061 | # |
2074 | # |
2062 | # CFPlus::OpenGL::glCallList $self->{list}; |
2075 | # DC::OpenGL::glCallList $self->{list}; |
2063 | |
2076 | |
2064 | $self->{layout}->draw; |
2077 | $self->{layout}->draw; |
2065 | } |
2078 | } |
2066 | |
2079 | |
2067 | #sub destroy { |
2080 | #sub destroy { |
2068 | # my ($self) = @_; |
2081 | # my ($self) = @_; |
2069 | # |
2082 | # |
2070 | # CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
2083 | # DC::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
2071 | # |
2084 | # |
2072 | # $self->SUPER::destroy; |
2085 | # $self->SUPER::destroy; |
2073 | #} |
2086 | #} |
2074 | |
2087 | |
2075 | ############################################################################# |
2088 | ############################################################################# |
2076 | |
2089 | |
2077 | package CFPlus::UI::EntryBase; |
2090 | package DC::UI::EntryBase; |
2078 | |
2091 | |
2079 | our @ISA = CFPlus::UI::Label::; |
2092 | our @ISA = DC::UI::Label::; |
2080 | |
2093 | |
2081 | use CFPlus::OpenGL; |
2094 | use DC::OpenGL; |
2082 | |
2095 | |
2083 | sub new { |
2096 | sub new { |
2084 | my $class = shift; |
2097 | my $class = shift; |
2085 | |
2098 | |
2086 | $class->SUPER::new ( |
2099 | $class->SUPER::new ( |
2087 | fg => [1, 1, 1], |
2100 | fg => [1, 1, 1], |
2088 | bg => [0, 0, 0, 0.2], |
2101 | bg => [0, 0, 0, 0.2], |
2089 | outline => [0.6, 0.3, 0.1], |
2102 | outline => undef, |
2090 | active_bg => [0, 0, 1, .2], |
2103 | active_bg => [0, 0, 1, .2], |
2091 | active_fg => [1, 1, 1], |
2104 | active_fg => [1, 1, 1], |
2092 | active_outline => [1, 1, 0], |
2105 | active_outline => [1, 1, 0], |
2093 | can_hover => 1, |
2106 | can_hover => 1, |
2094 | can_focus => 1, |
2107 | can_focus => 1, |
|
|
2108 | align => 0, |
2095 | valign => 0, |
2109 | valign => 0.5, |
2096 | can_events => 1, |
2110 | can_events => 1, |
2097 | ellipsise => 0, |
2111 | ellipsise => 0, |
|
|
2112 | padding_x => 4, |
|
|
2113 | padding_y => 2, |
2098 | #text => ... |
2114 | #text => ... |
2099 | #hidden => "*", |
2115 | #hidden => "*", |
2100 | @_ |
2116 | @_ |
2101 | ) |
2117 | ) |
2102 | } |
2118 | } |
… | |
… | |
2149 | |
2165 | |
2150 | my $text = $self->get_text; |
2166 | my $text = $self->get_text; |
2151 | |
2167 | |
2152 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
2168 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
2153 | |
2169 | |
2154 | if ($uni == 8) { |
2170 | if ($sym == DC::SDLK_BACKSPACE) { |
2155 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2171 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2156 | } elsif ($uni == 127) { |
2172 | } elsif ($sym == DC::SDLK_DELETE) { |
2157 | substr $text, $self->{cursor}, 1, ""; |
2173 | substr $text, $self->{cursor}, 1, ""; |
2158 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
2174 | } elsif ($sym == DC::SDLK_LEFT) { |
2159 | --$self->{cursor} if $self->{cursor}; |
2175 | --$self->{cursor} if $self->{cursor}; |
2160 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
2176 | } elsif ($sym == DC::SDLK_RIGHT) { |
2161 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2177 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2162 | } elsif ($sym == CFPlus::SDLK_HOME) { |
2178 | } elsif ($sym == DC::SDLK_HOME) { |
2163 | # what a hack |
2179 | # what a hack |
2164 | $self->{cursor} = |
2180 | $self->{cursor} = |
2165 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2181 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2166 | ? length $1 |
2182 | ? length $1 |
2167 | : 0; |
2183 | : 0; |
2168 | } elsif ($sym == CFPlus::SDLK_END) { |
2184 | } elsif ($sym == DC::SDLK_END) { |
2169 | # uh, again |
2185 | # uh, again |
2170 | $self->{cursor} = |
2186 | $self->{cursor} = |
2171 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2187 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2172 | ? $self->{cursor} + length $1 |
2188 | ? $self->{cursor} + length $1 |
2173 | : length $self->{text}; |
2189 | : length $self->{text}; |
… | |
… | |
2264 | glColor @{$self->{active_outline}}; |
2280 | glColor @{$self->{active_outline}}; |
2265 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
2281 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
2266 | glLineWidth 1; |
2282 | glLineWidth 1; |
2267 | |
2283 | |
2268 | } else { |
2284 | } else { |
2269 | glColor @{$self->{outline}}; |
2285 | glColor @{$self->{outline} || $DC::THEME{entry_outline}}; |
2270 | glBegin GL_LINE_STRIP; |
2286 | glBegin GL_LINE_STRIP; |
2271 | glVertex .5, $self->{h} * .5; |
2287 | glVertex .5, $self->{h} * .5; |
2272 | glVertex .5, $self->{h} - 2.5; |
2288 | glVertex .5, $self->{h} - 2.5; |
2273 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
2289 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
2274 | glVertex $self->{w} - .5, $self->{h} * .5; |
2290 | glVertex $self->{w} - .5, $self->{h} * .5; |
… | |
… | |
2276 | } |
2292 | } |
2277 | } |
2293 | } |
2278 | |
2294 | |
2279 | ############################################################################# |
2295 | ############################################################################# |
2280 | |
2296 | |
2281 | package CFPlus::UI::Entry; |
2297 | package DC::UI::Entry; |
2282 | |
2298 | |
2283 | our @ISA = CFPlus::UI::EntryBase::; |
2299 | our @ISA = DC::UI::EntryBase::; |
2284 | |
2300 | |
2285 | use CFPlus::OpenGL; |
2301 | use DC::OpenGL; |
|
|
2302 | |
|
|
2303 | sub new { |
|
|
2304 | my $class = shift; |
|
|
2305 | |
|
|
2306 | $class->SUPER::new ( |
|
|
2307 | history_pointer => -1, |
|
|
2308 | @_ |
|
|
2309 | ) |
|
|
2310 | } |
|
|
2311 | |
2286 | |
2312 | |
2287 | sub invoke_key_down { |
2313 | sub invoke_key_down { |
2288 | my ($self, $ev) = @_; |
2314 | my ($self, $ev) = @_; |
2289 | |
2315 | |
2290 | my $sym = $ev->{sym}; |
2316 | my $sym = $ev->{sym}; |
… | |
… | |
2296 | $self->{history_pointer} = -1; |
2322 | $self->{history_pointer} = -1; |
2297 | $self->{history_saveback} = ''; |
2323 | $self->{history_saveback} = ''; |
2298 | $self->emit (activate => $txt); |
2324 | $self->emit (activate => $txt); |
2299 | $self->update; |
2325 | $self->update; |
2300 | |
2326 | |
2301 | } elsif ($sym == CFPlus::SDLK_UP) { |
2327 | } elsif ($sym == DC::SDLK_UP) { |
2302 | if ($self->{history_pointer} < 0) { |
2328 | if ($self->{history_pointer} < 0) { |
2303 | $self->{history_saveback} = $self->get_text; |
2329 | $self->{history_saveback} = $self->get_text; |
2304 | } |
2330 | } |
2305 | if (@{$self->{history} || []} > 0) { |
2331 | if (@{$self->{history} || []} > 0) { |
2306 | $self->{history_pointer}++; |
2332 | $self->{history_pointer}++; |
… | |
… | |
2308 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2334 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2309 | } |
2335 | } |
2310 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2336 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2311 | } |
2337 | } |
2312 | |
2338 | |
2313 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2339 | } elsif ($sym == DC::SDLK_DOWN) { |
2314 | $self->{history_pointer}--; |
2340 | $self->{history_pointer}--; |
2315 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2341 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2316 | |
2342 | |
2317 | if ($self->{history_pointer} >= 0) { |
2343 | if ($self->{history_pointer} >= 0) { |
2318 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2344 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2319 | } else { |
2345 | } else { |
|
|
2346 | if (defined $self->{history_saveback}) { |
2320 | $self->set_text ($self->{history_saveback}); |
2347 | $self->set_text ($self->{history_saveback}); |
|
|
2348 | $self->{history_saveback} = undef; |
|
|
2349 | } |
2321 | } |
2350 | } |
2322 | |
2351 | |
2323 | } else { |
2352 | } else { |
2324 | return $self->SUPER::invoke_key_down ($ev) |
2353 | return $self->SUPER::invoke_key_down ($ev) |
2325 | } |
2354 | } |
… | |
… | |
2327 | 1 |
2356 | 1 |
2328 | } |
2357 | } |
2329 | |
2358 | |
2330 | ############################################################################# |
2359 | ############################################################################# |
2331 | |
2360 | |
2332 | package CFPlus::UI::TextEdit; |
2361 | package DC::UI::TextEdit; |
2333 | |
2362 | |
2334 | our @ISA = CFPlus::UI::EntryBase::; |
2363 | our @ISA = DC::UI::EntryBase::; |
2335 | |
2364 | |
2336 | use CFPlus::OpenGL; |
2365 | use DC::OpenGL; |
|
|
2366 | |
|
|
2367 | sub new { |
|
|
2368 | my $class = shift; |
|
|
2369 | |
|
|
2370 | $class->SUPER::new ( |
|
|
2371 | padding_y => 4, |
|
|
2372 | |
|
|
2373 | @_ |
|
|
2374 | ) |
|
|
2375 | } |
2337 | |
2376 | |
2338 | sub move_cursor_ver { |
2377 | sub move_cursor_ver { |
2339 | my ($self, $dy) = @_; |
2378 | my ($self, $dy) = @_; |
2340 | |
2379 | |
2341 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
2380 | my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
2342 | |
2381 | |
2343 | $y += $dy; |
2382 | $line += $dy; |
2344 | |
2383 | |
2345 | if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { |
2384 | if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) { |
2346 | $self->{cursor} = $index; |
2385 | $self->{cursor} = $index; |
2347 | delete $self->{cur_h}; |
2386 | delete $self->{cur_h}; |
2348 | $self->update; |
2387 | $self->update; |
2349 | return; |
2388 | return; |
2350 | } |
2389 | } |
… | |
… | |
2353 | sub invoke_key_down { |
2392 | sub invoke_key_down { |
2354 | my ($self, $ev) = @_; |
2393 | my ($self, $ev) = @_; |
2355 | |
2394 | |
2356 | my $sym = $ev->{sym}; |
2395 | my $sym = $ev->{sym}; |
2357 | |
2396 | |
2358 | if ($sym == CFPlus::SDLK_UP) { |
2397 | if ($sym == DC::SDLK_UP) { |
2359 | $self->move_cursor_ver (-1); |
2398 | $self->move_cursor_ver (-1); |
2360 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2399 | } elsif ($sym == DC::SDLK_DOWN) { |
2361 | $self->move_cursor_ver (+1); |
2400 | $self->move_cursor_ver (+1); |
2362 | } else { |
2401 | } else { |
2363 | return $self->SUPER::invoke_key_down ($ev) |
2402 | return $self->SUPER::invoke_key_down ($ev) |
2364 | } |
2403 | } |
2365 | |
2404 | |
2366 | 1 |
2405 | 1 |
2367 | } |
2406 | } |
2368 | |
2407 | |
2369 | ############################################################################# |
2408 | ############################################################################# |
2370 | |
2409 | |
2371 | package CFPlus::UI::ButtonBin; |
2410 | package DC::UI::ButtonBin; |
2372 | |
2411 | |
2373 | our @ISA = CFPlus::UI::Bin::; |
2412 | our @ISA = DC::UI::Bin::; |
2374 | |
2413 | |
2375 | use CFPlus::OpenGL; |
2414 | use DC::OpenGL; |
2376 | |
2415 | |
2377 | my @tex = |
2416 | my @tex = |
2378 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2417 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2379 | qw(b1_button_inactive.png b1_button_active.png); |
2418 | qw(b1_button_inactive.png b1_button_active.png); |
2380 | |
2419 | |
2381 | sub new { |
2420 | sub new { |
2382 | my $class = shift; |
2421 | my $class = shift; |
2383 | |
2422 | |
2384 | $class->SUPER::new ( |
2423 | $class->SUPER::new ( |
2385 | can_hover => 1, |
2424 | can_hover => 1, |
2386 | align => 0, |
2425 | align => 0.5, |
2387 | valign => 0, |
2426 | valign => 0.5, |
2388 | can_events => 1, |
2427 | can_events => 1, |
2389 | @_ |
2428 | @_ |
2390 | ) |
2429 | ) |
2391 | } |
2430 | } |
2392 | |
2431 | |
… | |
… | |
2415 | $self->SUPER::_draw; |
2454 | $self->SUPER::_draw; |
2416 | } |
2455 | } |
2417 | |
2456 | |
2418 | ############################################################################# |
2457 | ############################################################################# |
2419 | |
2458 | |
2420 | package CFPlus::UI::Button; |
2459 | package DC::UI::Button; |
2421 | |
2460 | |
2422 | our @ISA = CFPlus::UI::Label::; |
2461 | our @ISA = DC::UI::Label::; |
2423 | |
2462 | |
2424 | use CFPlus::OpenGL; |
2463 | use DC::OpenGL; |
2425 | |
2464 | |
2426 | my @tex = |
2465 | my @tex = |
2427 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2466 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2428 | qw(b1_button_inactive.png b1_button_active.png); |
2467 | qw(b1_button_inactive.png b1_button_active.png); |
2429 | |
2468 | |
2430 | sub new { |
2469 | sub new { |
2431 | my $class = shift; |
2470 | my $class = shift; |
2432 | |
2471 | |
2433 | $class->SUPER::new ( |
2472 | $class->SUPER::new ( |
2434 | padding_x => 4, |
2473 | padding_x => 8, |
2435 | padding_y => 4, |
2474 | padding_y => 4, |
2436 | fg => [1.0, 1.0, 1.0], |
2475 | fg => [1.0, 1.0, 1.0], |
2437 | active_fg => [0.8, 0.8, 0.8], |
2476 | active_fg => [0.8, 0.8, 0.8], |
2438 | can_hover => 1, |
2477 | can_hover => 1, |
2439 | align => 0, |
2478 | align => 0.5, |
2440 | valign => 0, |
2479 | valign => 0.5, |
2441 | can_events => 1, |
2480 | can_events => 1, |
2442 | @_ |
2481 | @_ |
2443 | ) |
2482 | ) |
2444 | } |
2483 | } |
2445 | |
2484 | |
… | |
… | |
2470 | $self->SUPER::_draw; |
2509 | $self->SUPER::_draw; |
2471 | } |
2510 | } |
2472 | |
2511 | |
2473 | ############################################################################# |
2512 | ############################################################################# |
2474 | |
2513 | |
2475 | package CFPlus::UI::CheckBox; |
2514 | package DC::UI::CheckBox; |
2476 | |
2515 | |
2477 | our @ISA = CFPlus::UI::DrawBG::; |
2516 | our @ISA = DC::UI::DrawBG::; |
2478 | |
2517 | |
2479 | my @tex = |
2518 | my @tex = |
2480 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2519 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2481 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2520 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2482 | |
2521 | |
2483 | use CFPlus::OpenGL; |
2522 | use DC::OpenGL; |
2484 | |
2523 | |
2485 | sub new { |
2524 | sub new { |
2486 | my $class = shift; |
2525 | my $class = shift; |
2487 | |
2526 | |
2488 | $class->SUPER::new ( |
2527 | $class->SUPER::new ( |
|
|
2528 | fontsize => 1, |
2489 | padding_x => 2, |
2529 | padding_x => 2, |
2490 | padding_y => 2, |
2530 | padding_y => 2, |
2491 | fg => [1, 1, 1], |
2531 | fg => [1, 1, 1], |
2492 | active_fg => [1, 1, 0], |
2532 | active_fg => [1, 1, 0], |
2493 | bg => [0, 0, 0, 0.2], |
2533 | bg => [0, 0, 0, 0.2], |
… | |
… | |
2499 | } |
2539 | } |
2500 | |
2540 | |
2501 | sub size_request { |
2541 | sub size_request { |
2502 | my ($self) = @_; |
2542 | my ($self) = @_; |
2503 | |
2543 | |
2504 | (6) x 2 |
2544 | ($self->{fontsize} * $::FONTSIZE) x 2 |
2505 | } |
2545 | } |
2506 | |
2546 | |
2507 | sub toggle { |
2547 | sub toggle { |
2508 | my ($self) = @_; |
2548 | my ($self) = @_; |
2509 | |
2549 | |
… | |
… | |
2545 | glDisable GL_TEXTURE_2D; |
2585 | glDisable GL_TEXTURE_2D; |
2546 | } |
2586 | } |
2547 | |
2587 | |
2548 | ############################################################################# |
2588 | ############################################################################# |
2549 | |
2589 | |
2550 | package CFPlus::UI::Image; |
2590 | package DC::UI::Image; |
2551 | |
2591 | |
2552 | our @ISA = CFPlus::UI::Base::; |
2592 | our @ISA = DC::UI::Base::; |
2553 | |
2593 | |
2554 | use CFPlus::OpenGL; |
2594 | use DC::OpenGL; |
2555 | |
2595 | |
2556 | our %texture_cache; |
2596 | our %texture_cache; |
2557 | |
2597 | |
2558 | sub new { |
2598 | sub new { |
2559 | my $class = shift; |
2599 | my $class = shift; |
… | |
… | |
2566 | |
2606 | |
2567 | $self->{path} || $self->{tex} |
2607 | $self->{path} || $self->{tex} |
2568 | or Carp::croak "'path' or 'tex' attributes required"; |
2608 | or Carp::croak "'path' or 'tex' attributes required"; |
2569 | |
2609 | |
2570 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2610 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2571 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2611 | new_from_resource DC::Texture $self->{path}, mipmap => 1; |
2572 | |
2612 | |
2573 | CFPlus::weaken $texture_cache{$self->{path}}; |
2613 | DC::weaken $texture_cache{$self->{path}}; |
2574 | |
2614 | |
2575 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2615 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2576 | |
2616 | |
2577 | $self |
2617 | $self |
2578 | } |
2618 | } |
2579 | |
2619 | |
2580 | sub STORABLE_freeze { |
2620 | sub STORABLE_freeze { |
2581 | my ($self, $cloning) = @_; |
2621 | my ($self, $cloning) = @_; |
2582 | |
2622 | |
2583 | $self->{path} |
2623 | $self->{path} |
2584 | or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; |
2624 | or die "cannot serialise DC::UI::Image on non-loadable images\n"; |
2585 | |
2625 | |
2586 | $self->{path} |
2626 | $self->{path} |
2587 | } |
2627 | } |
2588 | |
2628 | |
2589 | sub STORABLE_attach { |
2629 | sub STORABLE_attach { |
… | |
… | |
2620 | glDisable GL_TEXTURE_2D; |
2660 | glDisable GL_TEXTURE_2D; |
2621 | } |
2661 | } |
2622 | |
2662 | |
2623 | ############################################################################# |
2663 | ############################################################################# |
2624 | |
2664 | |
2625 | package CFPlus::UI::ImageButton; |
2665 | package DC::UI::ImageButton; |
2626 | |
2666 | |
2627 | our @ISA = CFPlus::UI::Image::; |
2667 | our @ISA = DC::UI::Image::; |
2628 | |
2668 | |
2629 | use CFPlus::OpenGL; |
2669 | use DC::OpenGL; |
2630 | |
2670 | |
2631 | my %textures; |
2671 | my %textures; |
2632 | |
2672 | |
2633 | sub new { |
2673 | sub new { |
2634 | my $class = shift; |
2674 | my $class = shift; |
… | |
… | |
2637 | padding_x => 4, |
2677 | padding_x => 4, |
2638 | padding_y => 4, |
2678 | padding_y => 4, |
2639 | fg => [1, 1, 1], |
2679 | fg => [1, 1, 1], |
2640 | active_fg => [0, 0, 1], |
2680 | active_fg => [0, 0, 1], |
2641 | can_hover => 1, |
2681 | can_hover => 1, |
2642 | align => 0, |
2682 | align => 0.5, |
2643 | valign => 0, |
2683 | valign => 0.5, |
2644 | can_events => 1, |
2684 | can_events => 1, |
2645 | @_ |
2685 | @_ |
2646 | ); |
2686 | ); |
2647 | } |
2687 | } |
2648 | |
2688 | |
… | |
… | |
2662 | 1 |
2702 | 1 |
2663 | } |
2703 | } |
2664 | |
2704 | |
2665 | ############################################################################# |
2705 | ############################################################################# |
2666 | |
2706 | |
2667 | package CFPlus::UI::VGauge; |
2707 | package DC::UI::VGauge; |
2668 | |
2708 | |
2669 | our @ISA = CFPlus::UI::Base::; |
2709 | our @ISA = DC::UI::Base::; |
2670 | |
2710 | |
2671 | use List::Util qw(min max); |
2711 | use List::Util qw(min max); |
2672 | |
2712 | |
2673 | use CFPlus::OpenGL; |
2713 | use DC::OpenGL; |
2674 | |
2714 | |
2675 | my %tex = ( |
2715 | my %tex = ( |
2676 | food => [ |
2716 | food => [ |
2677 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2717 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2678 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2718 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2679 | ], |
2719 | ], |
2680 | grace => [ |
2720 | grace => [ |
2681 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2721 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2682 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2722 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2683 | ], |
2723 | ], |
2684 | hp => [ |
2724 | hp => [ |
2685 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2725 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2686 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2726 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2687 | ], |
2727 | ], |
2688 | mana => [ |
2728 | mana => [ |
2689 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2729 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2690 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2730 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2691 | ], |
2731 | ], |
2692 | ); |
2732 | ); |
2693 | |
2733 | |
2694 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
2734 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
… | |
… | |
2797 | glDisable GL_TEXTURE_2D; |
2837 | glDisable GL_TEXTURE_2D; |
2798 | } |
2838 | } |
2799 | |
2839 | |
2800 | ############################################################################# |
2840 | ############################################################################# |
2801 | |
2841 | |
2802 | package CFPlus::UI::Progress; |
2842 | package DC::UI::Progress; |
2803 | |
2843 | |
2804 | our @ISA = CFPlus::UI::Label::; |
2844 | our @ISA = DC::UI::Label::; |
2805 | |
2845 | |
2806 | use CFPlus::OpenGL; |
2846 | use DC::OpenGL; |
2807 | |
2847 | |
2808 | sub new { |
2848 | sub new { |
2809 | my ($class, %arg) = @_; |
2849 | my ($class, %arg) = @_; |
2810 | |
2850 | |
2811 | my $self = $class->SUPER::new ( |
2851 | my $self = $class->SUPER::new ( |
|
|
2852 | padding_x => 2, |
|
|
2853 | padding_y => 2, |
2812 | fg => [1, 1, 1], |
2854 | fg => [1, 1, 1], |
2813 | bg => [0, 0, 1, 0.2], |
2855 | bg => [0, 0, 1, 0.2], |
2814 | bar => [0.7, 0.5, 0.1, 0.8], |
2856 | bar => [0.7, 0.5, 0.1, 0.8], |
2815 | outline => [0.4, 0.3, 0], |
2857 | outline => [0.4, 0.3, 0], |
2816 | fontsize => 0.9, |
2858 | fontsize => 0.9, |
2817 | valign => 0, |
2859 | valign => 0.5, |
2818 | align => 0, |
2860 | align => 0.5, |
2819 | can_events => 1, |
2861 | can_events => 1, |
2820 | ellipsise => 1, |
2862 | ellipsise => 1, |
2821 | label => "%d%%", |
2863 | label => "%d%%", |
2822 | %arg, |
2864 | %arg, |
2823 | ); |
2865 | ); |
… | |
… | |
2831 | my ($self, $label) = @_; |
2873 | my ($self, $label) = @_; |
2832 | |
2874 | |
2833 | return if $self->{label} eq $label; |
2875 | return if $self->{label} eq $label; |
2834 | $self->{label} = $label; |
2876 | $self->{label} = $label; |
2835 | |
2877 | |
2836 | $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); |
2878 | $self->DC::UI::Progress::set_value (0 + delete $self->{value}); |
2837 | } |
2879 | } |
2838 | |
2880 | |
2839 | sub set_value { |
2881 | sub set_value { |
2840 | my ($self, $value) = @_; |
2882 | my ($self, $value) = @_; |
2841 | |
2883 | |
… | |
… | |
2856 | my ($self) = @_; |
2898 | my ($self) = @_; |
2857 | |
2899 | |
2858 | glEnable GL_BLEND; |
2900 | glEnable GL_BLEND; |
2859 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2901 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2860 | |
2902 | |
|
|
2903 | my $px = $self->{padding_x}; |
|
|
2904 | my $py = $self->{padding_y}; |
|
|
2905 | |
2861 | if ($self->{value} >= 0) { |
2906 | if ($self->{value} >= 0) { |
2862 | my $s = int 2 + ($self->{w} - 4) * $self->{value}; |
2907 | my $s = int $px + ($self->{w} - $px * 2) * $self->{value}; |
2863 | |
2908 | |
2864 | glColor_premultiply @{$self->{bar}}; |
2909 | glColor_premultiply @{$self->{bar}}; |
2865 | glRect 2, 2, $s, $self->{h} - 2; |
2910 | glRect $px, $py, $s, $self->{h} - $py; |
2866 | glColor_premultiply @{$self->{bg}}; |
2911 | glColor_premultiply @{$self->{bg}}; |
2867 | glRect $s, 2, $self->{w} - 2, $self->{h} - 2; |
2912 | glRect $s , $py, $self->{w} - $px, $self->{h} - $py; |
2868 | } |
2913 | } |
2869 | |
2914 | |
2870 | glColor_premultiply @{$self->{outline}}; |
2915 | glColor_premultiply @{$self->{outline}}; |
|
|
2916 | |
|
|
2917 | $px -= .5; |
|
|
2918 | $py -= .5; |
|
|
2919 | |
2871 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
2920 | glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py; |
2872 | |
2921 | |
2873 | glDisable GL_BLEND; |
2922 | glDisable GL_BLEND; |
2874 | |
2923 | |
2875 | { |
2924 | { |
2876 | local $self->{bg}; # do not draw background |
2925 | local $self->{bg}; # do not draw background |
… | |
… | |
2878 | } |
2927 | } |
2879 | } |
2928 | } |
2880 | |
2929 | |
2881 | ############################################################################# |
2930 | ############################################################################# |
2882 | |
2931 | |
2883 | package CFPlus::UI::ExperienceProgress; |
2932 | package DC::UI::ExperienceProgress; |
2884 | |
2933 | |
2885 | our @ISA = CFPlus::UI::Progress::; |
2934 | our @ISA = DC::UI::Progress::; |
2886 | |
2935 | |
2887 | sub new { |
2936 | sub new { |
2888 | my ($class, %arg) = @_; |
2937 | my ($class, %arg) = @_; |
2889 | |
2938 | |
|
|
2939 | my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : ""; |
|
|
2940 | |
2890 | my $self = $class->SUPER::new ( |
2941 | my $self = $class->SUPER::new ( |
|
|
2942 | %arg, |
2891 | tooltip => sub { |
2943 | tooltip => sub { |
2892 | my ($self) = @_; |
2944 | my ($self) = @_; |
2893 | |
2945 | |
2894 | sprintf "level %d\n%s points\n%s next level\n%s to go", |
2946 | sprintf "%slevel %d\n%s points\n%s next level\n%s to go / %d%%", |
|
|
2947 | $tt, |
2895 | $self->{lvl}, |
2948 | $self->{lvl}, |
2896 | ::formsep ($self->{exp}), |
2949 | ::formsep ($self->{exp}), |
2897 | ::formsep ($self->{nxt}), |
2950 | ::formsep ($self->{nxt}), |
2898 | ::formsep ($self->{nxt} - $self->{exp}), |
2951 | ::formsep ($self->{nxt} - $self->{exp}), |
|
|
2952 | $self->_percent * 100, |
2899 | }, |
2953 | }, |
2900 | %arg |
|
|
2901 | ); |
2954 | ); |
2902 | |
2955 | |
2903 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
2956 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
2904 | if $::CONN; |
2957 | if $::CONN; |
2905 | |
2958 | |
… | |
… | |
2913 | if $::CONN; |
2966 | if $::CONN; |
2914 | |
2967 | |
2915 | $self->SUPER::DESTROY; |
2968 | $self->SUPER::DESTROY; |
2916 | } |
2969 | } |
2917 | |
2970 | |
|
|
2971 | sub _percent { |
|
|
2972 | my ($self) = @_; |
|
|
2973 | |
|
|
2974 | my $table = $::CONN && $::CONN->{exp_table} |
|
|
2975 | or return -1; |
|
|
2976 | |
|
|
2977 | my $l0 = $table->[$self->{lvl} - 1]; |
|
|
2978 | my $l1 = $table->[$self->{lvl}]; |
|
|
2979 | |
|
|
2980 | $self->{nxt} = $l1; |
|
|
2981 | |
|
|
2982 | ($self->{exp} - $l0) / ($l1 - $l0) |
|
|
2983 | } |
|
|
2984 | |
2918 | sub set_value { |
2985 | sub set_value { |
2919 | my ($self, $lvl, $exp) = @_; |
2986 | my ($self, $lvl, $exp) = @_; |
2920 | |
2987 | |
2921 | $self->{lvl} = $lvl; |
2988 | $self->{lvl} = $lvl; |
2922 | $self->{exp} = $exp; |
2989 | $self->{exp} = $exp; |
2923 | |
2990 | |
2924 | my $v = -1; |
|
|
2925 | |
|
|
2926 | if ($::CONN && (my $table = $::CONN->{exp_table})) { |
|
|
2927 | my $l0 = $table->[$lvl - 1]; |
|
|
2928 | my $l1 = $table->[$lvl]; |
|
|
2929 | |
|
|
2930 | $self->{nxt} = $l1; |
|
|
2931 | |
|
|
2932 | $v = ($exp - $l0) / ($l1 - $l0); |
|
|
2933 | } |
|
|
2934 | |
|
|
2935 | $self->SUPER::set_value ($v); |
2991 | $self->SUPER::set_value ($self->_percent); |
2936 | } |
2992 | } |
2937 | |
2993 | |
2938 | ############################################################################# |
2994 | ############################################################################# |
2939 | |
2995 | |
2940 | package CFPlus::UI::Gauge; |
2996 | package DC::UI::Gauge; |
2941 | |
2997 | |
2942 | our @ISA = CFPlus::UI::VBox::; |
2998 | our @ISA = DC::UI::VBox::; |
2943 | |
2999 | |
2944 | sub new { |
3000 | sub new { |
2945 | my ($class, %arg) = @_; |
3001 | my ($class, %arg) = @_; |
2946 | |
3002 | |
2947 | my $self = $class->SUPER::new ( |
3003 | my $self = $class->SUPER::new ( |
… | |
… | |
2949 | can_hover => 1, |
3005 | can_hover => 1, |
2950 | can_events => 1, |
3006 | can_events => 1, |
2951 | %arg, |
3007 | %arg, |
2952 | ); |
3008 | ); |
2953 | |
3009 | |
2954 | $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); |
3010 | $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999"); |
2955 | $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
3011 | $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2956 | $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); |
3012 | $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999"); |
2957 | |
3013 | |
2958 | $self |
3014 | $self |
2959 | } |
3015 | } |
2960 | |
3016 | |
2961 | sub set_fontsize { |
3017 | sub set_fontsize { |
… | |
… | |
2982 | $self->{value}->set_text ($val); |
3038 | $self->{value}->set_text ($val); |
2983 | } |
3039 | } |
2984 | |
3040 | |
2985 | ############################################################################# |
3041 | ############################################################################# |
2986 | |
3042 | |
2987 | package CFPlus::UI::Slider; |
3043 | package DC::UI::Slider; |
2988 | |
3044 | |
2989 | use strict; |
3045 | use strict; |
2990 | |
3046 | |
2991 | use CFPlus::OpenGL; |
3047 | use DC::OpenGL; |
2992 | |
3048 | |
2993 | our @ISA = CFPlus::UI::DrawBG::; |
3049 | our @ISA = DC::UI::DrawBG::; |
2994 | |
3050 | |
2995 | my @tex = |
3051 | my @tex = |
2996 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } |
3052 | map { new_from_resource DC::Texture $_ } |
2997 | qw(s1_slider.png s1_slider_bg.png); |
3053 | qw(s1_slider.png s1_slider_bg.png); |
2998 | |
3054 | |
2999 | sub new { |
3055 | sub new { |
3000 | my $class = shift; |
3056 | my $class = shift; |
3001 | |
3057 | |
… | |
… | |
3038 | sub set_value { |
3094 | sub set_value { |
3039 | my ($self, $value) = @_; |
3095 | my ($self, $value) = @_; |
3040 | |
3096 | |
3041 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
3097 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
3042 | |
3098 | |
3043 | $hi = $lo + 1 if $hi <= $lo; |
3099 | $hi = $lo if $hi < $lo; |
3044 | |
3100 | |
3045 | $page = $hi - $lo if $page > $hi - $lo; |
3101 | $value = $hi - $page if $value > $hi - $page; |
3046 | |
|
|
3047 | $value = $lo if $value < $lo; |
3102 | $value = $lo if $value < $lo; |
3048 | $value = $hi - $page if $value > $hi - $page; |
|
|
3049 | |
3103 | |
3050 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
3104 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
3051 | if $unit; |
3105 | if $unit; |
3052 | |
3106 | |
3053 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
3107 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
… | |
… | |
3097 | sub invoke_mouse_wheel { |
3151 | sub invoke_mouse_wheel { |
3098 | my ($self, $ev) = @_; |
3152 | my ($self, $ev) = @_; |
3099 | |
3153 | |
3100 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
3154 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
3101 | |
3155 | |
3102 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
3156 | my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2; |
3103 | |
3157 | |
3104 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
3158 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
3105 | |
3159 | |
3106 | 1 |
3160 | 1 |
3107 | } |
3161 | } |
… | |
… | |
3117 | my ($self) = @_; |
3171 | my ($self) = @_; |
3118 | |
3172 | |
3119 | unless ($self->{knob_w}) { |
3173 | unless ($self->{knob_w}) { |
3120 | $self->set_value ($self->{range}[0]); |
3174 | $self->set_value ($self->{range}[0]); |
3121 | |
3175 | |
3122 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
3176 | my ($value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
3123 | my $range = ($hi - $page - $lo) || 1e-100; |
3177 | my $range = ($hi - $page - $lo) || 1e-10; |
3124 | |
3178 | |
3125 | my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; |
3179 | my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w}; |
3126 | |
3180 | |
3127 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
3181 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
3128 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
3182 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
3129 | |
3183 | |
3130 | $value = ($value - $lo) / $range; |
3184 | $value = ($value - $lo) / $range; |
… | |
… | |
3160 | glDisable GL_TEXTURE_2D; |
3214 | glDisable GL_TEXTURE_2D; |
3161 | } |
3215 | } |
3162 | |
3216 | |
3163 | ############################################################################# |
3217 | ############################################################################# |
3164 | |
3218 | |
3165 | package CFPlus::UI::ValSlider; |
3219 | package DC::UI::ValSlider; |
3166 | |
3220 | |
3167 | our @ISA = CFPlus::UI::HBox::; |
3221 | our @ISA = DC::UI::HBox::; |
3168 | |
3222 | |
3169 | sub new { |
3223 | sub new { |
3170 | my ($class, %arg) = @_; |
3224 | my ($class, %arg) = @_; |
3171 | |
3225 | |
3172 | my $range = delete $arg{range}; |
3226 | my $range = delete $arg{range}; |
3173 | |
3227 | |
3174 | my $self = $class->SUPER::new ( |
3228 | my $self = $class->SUPER::new ( |
3175 | slider => (new CFPlus::UI::Slider expand => 1, range => $range), |
3229 | slider => (new DC::UI::Slider expand => 1, range => $range), |
3176 | entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), |
3230 | entry => (new DC::UI::Label text => "", template => delete $arg{template}), |
3177 | to_value => sub { shift }, |
3231 | to_value => sub { shift }, |
3178 | from_value => sub { shift }, |
3232 | from_value => sub { shift }, |
3179 | %arg, |
3233 | %arg, |
3180 | ); |
3234 | ); |
3181 | |
3235 | |
… | |
… | |
3201 | sub set_range { shift->{slider}->set_range (@_) } |
3255 | sub set_range { shift->{slider}->set_range (@_) } |
3202 | sub set_value { shift->{slider}->set_value (@_) } |
3256 | sub set_value { shift->{slider}->set_value (@_) } |
3203 | |
3257 | |
3204 | ############################################################################# |
3258 | ############################################################################# |
3205 | |
3259 | |
3206 | package CFPlus::UI::TextScroller; |
3260 | package DC::UI::TextScroller; |
3207 | |
3261 | |
3208 | our @ISA = CFPlus::UI::HBox::; |
3262 | our @ISA = DC::UI::HBox::; |
3209 | |
3263 | |
3210 | use CFPlus::OpenGL; |
3264 | use DC::OpenGL; |
3211 | |
3265 | |
3212 | sub new { |
3266 | sub new { |
3213 | my $class = shift; |
3267 | my $class = shift; |
3214 | |
3268 | |
3215 | my $self = $class->SUPER::new ( |
3269 | my $self = $class->SUPER::new ( |
… | |
… | |
3217 | can_events => 1, |
3271 | can_events => 1, |
3218 | indent => 0, |
3272 | indent => 0, |
3219 | #font => default_font |
3273 | #font => default_font |
3220 | @_, |
3274 | @_, |
3221 | |
3275 | |
3222 | layout => (new CFPlus::Layout), |
3276 | layout => (new DC::Layout), |
3223 | par => [], |
3277 | par => [], |
3224 | max_par => 0, |
3278 | max_par => 0, |
3225 | height => 0, |
3279 | height => 0, |
3226 | children => [ |
3280 | children => [ |
3227 | (new CFPlus::UI::Empty expand => 1), |
3281 | (new DC::UI::Empty expand => 1), |
3228 | (new CFPlus::UI::Slider vertical => 1), |
3282 | (new DC::UI::Slider vertical => 1), |
3229 | ], |
3283 | ], |
3230 | ); |
3284 | ); |
3231 | |
3285 | |
3232 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3286 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3233 | |
3287 | |
… | |
… | |
3289 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
3343 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
3290 | $layout->set_markup ($para->{markup}); |
3344 | $layout->set_markup ($para->{markup}); |
3291 | |
3345 | |
3292 | $layout->set_shapes ( |
3346 | $layout->set_shapes ( |
3293 | map |
3347 | map |
3294 | +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), |
3348 | +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), |
3295 | @{$para->{widget}} |
3349 | @{$para->{widget}} |
3296 | ); |
3350 | ); |
3297 | |
3351 | |
3298 | $layout |
3352 | $layout |
3299 | } |
3353 | } |
… | |
… | |
3416 | $ROOT->on_post_alloc ($self => sub { |
3470 | $ROOT->on_post_alloc ($self => sub { |
3417 | $self->force_uptodate; |
3471 | $self->force_uptodate; |
3418 | |
3472 | |
3419 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3473 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3420 | |
3474 | |
3421 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
3475 | $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub { |
3422 | glClearColor 0, 0, 0, 0; |
3476 | glClearColor 0, 0, 0, 0; |
3423 | glClear GL_COLOR_BUFFER_BIT; |
3477 | glClear GL_COLOR_BUFFER_BIT; |
3424 | |
3478 | |
3425 | package CFPlus::UI::Base; |
3479 | package DC::UI::Base; |
3426 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3480 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3427 | (0, 0, $self->{w}, $self->{h}); |
3481 | (0, 0, $self->{w}, $self->{h}); |
3428 | |
3482 | |
3429 | my $top = int $self->{children}[1]{range}[0]; |
3483 | my $top = int $self->{children}[1]{range}[0]; |
3430 | |
3484 | |
… | |
… | |
3489 | $self->{children}[1]->draw; |
3543 | $self->{children}[1]->draw; |
3490 | } |
3544 | } |
3491 | |
3545 | |
3492 | ############################################################################# |
3546 | ############################################################################# |
3493 | |
3547 | |
3494 | package CFPlus::UI::Animator; |
3548 | package DC::UI::Animator; |
3495 | |
3549 | |
3496 | use CFPlus::OpenGL; |
3550 | use DC::OpenGL; |
3497 | |
3551 | |
3498 | our @ISA = CFPlus::UI::Bin::; |
3552 | our @ISA = DC::UI::Bin::; |
3499 | |
3553 | |
3500 | sub moveto { |
3554 | sub moveto { |
3501 | my ($self, $x, $y) = @_; |
3555 | my ($self, $x, $y) = @_; |
3502 | |
3556 | |
3503 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
3557 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
3531 | glPopMatrix; |
3585 | glPopMatrix; |
3532 | } |
3586 | } |
3533 | |
3587 | |
3534 | ############################################################################# |
3588 | ############################################################################# |
3535 | |
3589 | |
3536 | package CFPlus::UI::Flopper; |
3590 | package DC::UI::Flopper; |
3537 | |
3591 | |
3538 | our @ISA = CFPlus::UI::Button::; |
3592 | our @ISA = DC::UI::Button::; |
3539 | |
3593 | |
3540 | sub new { |
3594 | sub new { |
3541 | my $class = shift; |
3595 | my $class = shift; |
3542 | |
3596 | |
3543 | my $self = $class->SUPER::new ( |
3597 | my $self = $class->SUPER::new ( |
… | |
… | |
3555 | $self->{other}->toggle_visibility; |
3609 | $self->{other}->toggle_visibility; |
3556 | } |
3610 | } |
3557 | |
3611 | |
3558 | ############################################################################# |
3612 | ############################################################################# |
3559 | |
3613 | |
3560 | package CFPlus::UI::Tooltip; |
3614 | package DC::UI::Tooltip; |
3561 | |
3615 | |
3562 | our @ISA = CFPlus::UI::Bin::; |
3616 | our @ISA = DC::UI::Bin::; |
3563 | |
3617 | |
3564 | use CFPlus::OpenGL; |
3618 | use DC::OpenGL; |
3565 | |
3619 | |
3566 | sub new { |
3620 | sub new { |
3567 | my $class = shift; |
3621 | my $class = shift; |
3568 | |
3622 | |
3569 | $class->SUPER::new ( |
3623 | $class->SUPER::new ( |
… | |
… | |
3576 | my ($self, $widget) = @_; |
3630 | my ($self, $widget) = @_; |
3577 | |
3631 | |
3578 | my $tip = $widget->{tooltip}; |
3632 | my $tip = $widget->{tooltip}; |
3579 | $tip = $tip->($widget) if "CODE" eq ref $tip; |
3633 | $tip = $tip->($widget) if "CODE" eq ref $tip; |
3580 | |
3634 | |
3581 | $tip = CFPlus::Pod::section_label tooltip => $1 |
3635 | $tip = DC::Pod::section_label tooltip => $1 |
3582 | if $tip =~ /^#(.*)$/; |
3636 | if $tip =~ /^#(.*)$/; |
3583 | |
3637 | |
3584 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3638 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3585 | $tip .= "\n\n" . (ref $widget) . "\n" |
3639 | $tip .= "\n\n" . (ref $widget) . "\n" |
3586 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3640 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
… | |
… | |
3589 | } |
3643 | } |
3590 | |
3644 | |
3591 | $tip =~ s/^\n+//; |
3645 | $tip =~ s/^\n+//; |
3592 | $tip =~ s/\n+$//; |
3646 | $tip =~ s/\n+$//; |
3593 | |
3647 | |
3594 | $self->add (new CFPlus::UI::Label |
3648 | $self->add (new DC::UI::Label |
|
|
3649 | fg => $DC::THEME{tooltip_fg}, |
3595 | markup => $tip, |
3650 | markup => $tip, |
3596 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3651 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
3652 | align => 0, |
3597 | fontsize => 0.8, |
3653 | fontsize => 0.8, |
3598 | style => 1, # FLAG_INVERSE |
3654 | style => $DC::THEME{tooltip_style}, # FLAG_INVERSE |
3599 | ellipsise => 0, |
3655 | ellipsise => 0, |
3600 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3656 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3601 | ); |
3657 | ); |
3602 | } |
3658 | } |
3603 | |
3659 | |
… | |
… | |
3640 | sub _draw { |
3696 | sub _draw { |
3641 | my ($self) = @_; |
3697 | my ($self) = @_; |
3642 | |
3698 | |
3643 | my ($w, $h) = @$self{qw(w h)}; |
3699 | my ($w, $h) = @$self{qw(w h)}; |
3644 | |
3700 | |
3645 | glColor 1, 0.8, 0.4; |
3701 | glColor @{ $DC::THEME{tooltip_bg} }; |
3646 | glRect 0, 0, $w, $h; |
3702 | glRect 0, 0, $w, $h; |
3647 | |
3703 | |
3648 | glColor 0, 0, 0; |
3704 | glColor @{ $DC::THEME{tooltip_border} }; |
3649 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3705 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3650 | |
3706 | |
3651 | glTranslate 2, 2; |
3707 | glTranslate 2, 2; |
3652 | |
3708 | |
3653 | $self->SUPER::_draw; |
3709 | $self->SUPER::_draw; |
3654 | } |
3710 | } |
3655 | |
3711 | |
3656 | ############################################################################# |
3712 | ############################################################################# |
3657 | |
3713 | |
3658 | package CFPlus::UI::Face; |
3714 | package DC::UI::Face; |
3659 | |
3715 | |
3660 | our @ISA = CFPlus::UI::DrawBG::; |
3716 | our @ISA = DC::UI::DrawBG::; |
3661 | |
3717 | |
3662 | use CFPlus::OpenGL; |
3718 | use DC::OpenGL; |
3663 | |
3719 | |
3664 | sub new { |
3720 | sub new { |
3665 | my $class = shift; |
3721 | my $class = shift; |
3666 | |
3722 | |
3667 | my $self = $class->SUPER::new ( |
3723 | my $self = $class->SUPER::new ( |
… | |
… | |
3671 | can_events => 0, |
3727 | can_events => 0, |
3672 | @_, |
3728 | @_, |
3673 | ); |
3729 | ); |
3674 | |
3730 | |
3675 | if ($self->{anim} && $self->{animspeed}) { |
3731 | if ($self->{anim} && $self->{animspeed}) { |
3676 | CFPlus::weaken (my $widget = $self); |
3732 | DC::weaken (my $widget = $self); |
3677 | |
3733 | |
3678 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
3734 | $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; |
3679 | $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed}; |
3735 | $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { |
3680 | $self->{timer} = EV::timer_ns 0, 0, sub { |
|
|
3681 | return unless $::CONN; |
3736 | return unless $::CONN; |
3682 | |
3737 | |
3683 | my $w = $widget |
3738 | my $w = $widget |
3684 | or return; |
3739 | or return; |
3685 | |
3740 | |
… | |
… | |
3702 | my ($self) = @_; |
3757 | my ($self) = @_; |
3703 | |
3758 | |
3704 | return unless $self->{timer}; |
3759 | return unless $self->{timer}; |
3705 | |
3760 | |
3706 | if ($self->{visible}) { |
3761 | if ($self->{visible}) { |
3707 | $self->{timer}->set ( |
|
|
3708 | $self->{anim_start} |
|
|
3709 | + $self->{animspeed} |
|
|
3710 | * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed} |
|
|
3711 | ); |
|
|
3712 | $self->{timer}->start; |
3762 | $self->{timer}->start; |
3713 | } else { |
3763 | } else { |
3714 | $self->{timer}->stop; |
3764 | $self->{timer}->stop; |
3715 | } |
3765 | } |
3716 | } |
3766 | } |
… | |
… | |
3793 | $self->SUPER::destroy; |
3843 | $self->SUPER::destroy; |
3794 | } |
3844 | } |
3795 | |
3845 | |
3796 | ############################################################################# |
3846 | ############################################################################# |
3797 | |
3847 | |
3798 | package CFPlus::UI::Buttonbar; |
3848 | package DC::UI::Buttonbar; |
3799 | |
3849 | |
3800 | our @ISA = CFPlus::UI::HBox::; |
3850 | our @ISA = DC::UI::HBox::; |
3801 | |
3851 | |
3802 | # TODO: should actually wrap buttons and other goodies. |
3852 | # TODO: should actually wrap buttons and other goodies. |
3803 | |
3853 | |
3804 | ############################################################################# |
3854 | ############################################################################# |
3805 | |
3855 | |
3806 | package CFPlus::UI::Menu; |
3856 | package DC::UI::Menu; |
3807 | |
3857 | |
3808 | our @ISA = CFPlus::UI::Toplevel::; |
3858 | our @ISA = DC::UI::Toplevel::; |
3809 | |
3859 | |
3810 | use CFPlus::OpenGL; |
3860 | use DC::OpenGL; |
3811 | |
3861 | |
3812 | sub new { |
3862 | sub new { |
3813 | my $class = shift; |
3863 | my $class = shift; |
3814 | |
3864 | |
3815 | my $self = $class->SUPER::new ( |
3865 | my $self = $class->SUPER::new ( |
3816 | items => [], |
3866 | items => [], |
3817 | z => 100, |
3867 | z => 100, |
3818 | @_, |
3868 | @_, |
3819 | ); |
3869 | ); |
3820 | |
3870 | |
3821 | $self->add ($self->{vbox} = new CFPlus::UI::VBox); |
3871 | $self->add ($self->{vbox} = new DC::UI::VBox); |
3822 | |
3872 | |
3823 | for my $item (@{ $self->{items} }) { |
3873 | for my $item (@{ $self->{items} }) { |
3824 | my ($widget, $cb, $tooltip) = @$item; |
3874 | my ($widget, $cb, $tooltip) = @$item; |
3825 | |
3875 | |
3826 | # handle various types of items, only text for now |
3876 | # handle various types of items, only text for now |
3827 | if (!ref $widget) { |
3877 | if (!ref $widget) { |
3828 | if ($widget =~ /\t/) { |
3878 | if ($widget =~ /\t/) { |
3829 | my ($left, $right) = split /\t/, $widget, 2; |
3879 | my ($left, $right) = split /\t/, $widget, 2; |
3830 | |
3880 | |
3831 | $widget = new CFPlus::UI::HBox |
3881 | $widget = new DC::UI::HBox |
3832 | can_hover => 1, |
3882 | can_hover => 1, |
3833 | can_events => 1, |
3883 | can_events => 1, |
3834 | tooltip => $tooltip, |
3884 | tooltip => $tooltip, |
3835 | children => [ |
3885 | children => [ |
3836 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3886 | (new DC::UI::Label markup => $left , align => 0, expand => 1), |
3837 | (new CFPlus::UI::Label markup => $right, align => +1), |
3887 | (new DC::UI::Label markup => $right, align => 1), |
3838 | ], |
3888 | ], |
3839 | ; |
3889 | ; |
3840 | |
3890 | |
3841 | } else { |
3891 | } else { |
3842 | $widget = new CFPlus::UI::Label |
3892 | $widget = new DC::UI::Label |
3843 | can_hover => 1, |
3893 | can_hover => 1, |
3844 | can_events => 1, |
3894 | can_events => 1, |
|
|
3895 | align => 0, |
3845 | markup => $widget, |
3896 | markup => $widget, |
3846 | tooltip => $tooltip; |
3897 | tooltip => $tooltip; |
3847 | } |
3898 | } |
3848 | } |
3899 | } |
3849 | |
3900 | |
… | |
… | |
3864 | # maybe save $GRAB? must be careful about events... |
3915 | # maybe save $GRAB? must be careful about events... |
3865 | $GRAB = $self; |
3916 | $GRAB = $self; |
3866 | $self->{button} = $ev->{button}; |
3917 | $self->{button} = $ev->{button}; |
3867 | |
3918 | |
3868 | $self->show; |
3919 | $self->show; |
3869 | $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
3920 | |
|
|
3921 | my $x = $ev->{x}; |
|
|
3922 | my $y = $ev->{y}; |
|
|
3923 | |
|
|
3924 | $self->{root}->on_post_alloc ($self => sub { |
|
|
3925 | $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5); |
|
|
3926 | }); |
|
|
3927 | |
|
|
3928 | 1 # so it can be used inside event handlers |
3870 | } |
3929 | } |
3871 | |
3930 | |
3872 | sub invoke_mouse_motion { |
3931 | sub invoke_mouse_motion { |
3873 | my ($self, $ev, $x, $y) = @_; |
3932 | my ($self, $ev, $x, $y) = @_; |
3874 | |
3933 | |
… | |
… | |
3895 | 1 |
3954 | 1 |
3896 | } |
3955 | } |
3897 | |
3956 | |
3898 | ############################################################################# |
3957 | ############################################################################# |
3899 | |
3958 | |
3900 | package CFPlus::UI::Multiplexer; |
3959 | package DC::UI::Multiplexer; |
3901 | |
3960 | |
3902 | our @ISA = CFPlus::UI::Container::; |
3961 | our @ISA = DC::UI::Container::; |
3903 | |
3962 | |
3904 | sub new { |
3963 | sub new { |
3905 | my $class = shift; |
3964 | my $class = shift; |
3906 | |
3965 | |
3907 | my $self = $class->SUPER::new ( |
3966 | my $self = $class->SUPER::new ( |
3908 | @_, |
3967 | @_, |
3909 | ); |
3968 | ); |
3910 | |
3969 | |
3911 | $self->{current} = $self->{children}[0] |
3970 | $self->set_current_page (0); |
3912 | if @{ $self->{children} }; |
|
|
3913 | |
3971 | |
3914 | $self |
3972 | $self |
3915 | } |
3973 | } |
3916 | |
3974 | |
3917 | sub add { |
3975 | sub add { |
3918 | my ($self, @widgets) = @_; |
3976 | my ($self, @widgets) = @_; |
3919 | |
3977 | |
3920 | $self->SUPER::add (@widgets); |
3978 | $self->SUPER::add (@widgets); |
3921 | |
3979 | |
3922 | $self->{current} = $self->{children}[0] |
3980 | $self->set_current_page (0) |
3923 | if @{ $self->{children} }; |
3981 | if @widgets == @{ $self->{children} }; |
3924 | } |
3982 | } |
3925 | |
3983 | |
3926 | sub get_current_page { |
3984 | sub get_current_page { |
3927 | my ($self) = @_; |
3985 | my ($self) = @_; |
3928 | |
3986 | |
… | |
… | |
3934 | |
3992 | |
3935 | my $widget = ref $page_or_widget |
3993 | my $widget = ref $page_or_widget |
3936 | ? $page_or_widget |
3994 | ? $page_or_widget |
3937 | : $self->{children}[$page_or_widget]; |
3995 | : $self->{children}[$page_or_widget]; |
3938 | |
3996 | |
|
|
3997 | $self->{current}->set_invisible if $self->{current} && $self->{visible}; |
|
|
3998 | |
3939 | $self->{current} = $widget; |
3999 | if (($self->{current} = $widget)) { |
|
|
4000 | $self->{current}->set_visible if $self->{current} && $self->{visible}; |
3940 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
4001 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
3941 | |
4002 | |
3942 | $self->emit (page_changed => $self->{current}); |
4003 | $self->emit (page_changed => $self->{current}); |
|
|
4004 | } |
3943 | |
4005 | |
3944 | $self->realloc; |
4006 | $self->realloc; |
3945 | } |
4007 | } |
3946 | |
4008 | |
3947 | sub visible_children { |
4009 | sub visible_children { |
3948 | $_[0]{current} |
4010 | $_[0]{current} || () |
3949 | } |
4011 | } |
3950 | |
4012 | |
3951 | sub size_request { |
4013 | sub size_request { |
3952 | my ($self) = @_; |
4014 | my ($self) = @_; |
3953 | |
4015 | |
|
|
4016 | $self->{current} |
3954 | $self->{current}->size_request |
4017 | ? $self->{current}->size_request |
|
|
4018 | : (0, 0) |
3955 | } |
4019 | } |
3956 | |
4020 | |
3957 | sub invoke_size_allocate { |
4021 | sub invoke_size_allocate { |
3958 | my ($self, $w, $h) = @_; |
4022 | my ($self, $w, $h) = @_; |
3959 | |
4023 | |
3960 | $self->{current}->configure (0, 0, $w, $h); |
4024 | $self->{current}->configure (0, 0, $w, $h) |
|
|
4025 | if $self->{current}; |
3961 | |
4026 | |
3962 | 1 |
4027 | 1 |
3963 | } |
4028 | } |
3964 | |
4029 | |
3965 | sub _draw { |
4030 | sub _draw { |
3966 | my ($self) = @_; |
4031 | my ($self) = @_; |
3967 | |
4032 | |
3968 | $self->{current}->draw; |
4033 | $self->{current}->draw |
|
|
4034 | if $self->{current}; |
3969 | } |
4035 | } |
3970 | |
4036 | |
3971 | ############################################################################# |
4037 | ############################################################################# |
3972 | |
4038 | |
3973 | package CFPlus::UI::Notebook; |
4039 | package DC::UI::Notebook; |
3974 | |
4040 | |
3975 | use CFPlus::OpenGL; |
4041 | use DC::OpenGL; |
3976 | |
4042 | |
3977 | our @ISA = CFPlus::UI::VBox::; |
4043 | our @ISA = DC::UI::VBox::; |
3978 | |
4044 | |
3979 | sub new { |
4045 | sub new { |
3980 | my $class = shift; |
4046 | my $class = shift; |
3981 | |
4047 | |
3982 | my $self = $class->SUPER::new ( |
4048 | my $self = $class->SUPER::new ( |
3983 | buttonbar => (new CFPlus::UI::Buttonbar), |
4049 | buttonbar => (new DC::UI::Buttonbar), |
3984 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
4050 | multiplexer => (new DC::UI::Multiplexer expand => 1), |
3985 | active_outline => [.7, .7, 0.2], |
4051 | active_outline => [.7, .7, 0.2], |
3986 | # filter => # will be put between multiplexer and $self |
4052 | # filter => # will be put between multiplexer and $self |
3987 | @_, |
4053 | @_, |
3988 | ); |
4054 | ); |
3989 | |
4055 | |
… | |
… | |
3998 | |
4064 | |
3999 | for my $child (@$widgets) { |
4065 | for my $child (@$widgets) { |
4000 | Scalar::Util::weaken $child; |
4066 | Scalar::Util::weaken $child; |
4001 | $child->{c_tab_} ||= do { |
4067 | $child->{c_tab_} ||= do { |
4002 | my $tab = |
4068 | my $tab = |
4003 | (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") |
4069 | (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base") |
4004 | ? $child->{c_tab} |
4070 | ? $child->{c_tab} |
4005 | : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
4071 | : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
4006 | |
4072 | |
4007 | $tab->connect (activate => sub { |
4073 | $tab->connect (activate => sub { |
4008 | $wself->set_current_page ($child); |
4074 | $wself->set_current_page ($child); |
4009 | }); |
4075 | }); |
4010 | |
4076 | |
… | |
… | |
4040 | } |
4106 | } |
4041 | |
4107 | |
4042 | sub pages { |
4108 | sub pages { |
4043 | my ($self) = @_; |
4109 | my ($self) = @_; |
4044 | $self->{multiplexer}->children |
4110 | $self->{multiplexer}->children |
|
|
4111 | } |
|
|
4112 | |
|
|
4113 | sub page_index { |
|
|
4114 | my ($self, $widget) = @_; |
|
|
4115 | |
|
|
4116 | my $i = 0; |
|
|
4117 | for ($self->pages) { |
|
|
4118 | if ($_ eq $widget) { return $i }; |
|
|
4119 | $i++; |
|
|
4120 | } |
|
|
4121 | |
|
|
4122 | undef |
4045 | } |
4123 | } |
4046 | |
4124 | |
4047 | sub add_tab { |
4125 | sub add_tab { |
4048 | my ($self, $title, $widget, $tooltip) = @_; |
4126 | my ($self, $title, $widget, $tooltip) = @_; |
4049 | |
4127 | |
… | |
… | |
4086 | } |
4164 | } |
4087 | } |
4165 | } |
4088 | |
4166 | |
4089 | ############################################################################# |
4167 | ############################################################################# |
4090 | |
4168 | |
4091 | package CFPlus::UI::Selector; |
4169 | package DC::UI::Selector; |
4092 | |
4170 | |
4093 | use utf8; |
4171 | use utf8; |
4094 | |
4172 | |
4095 | our @ISA = CFPlus::UI::Button::; |
4173 | our @ISA = DC::UI::Button::; |
4096 | |
4174 | |
4097 | sub new { |
4175 | sub new { |
4098 | my $class = shift; |
4176 | my $class = shift; |
4099 | |
4177 | |
4100 | my $self = $class->SUPER::new ( |
4178 | my $self = $class->SUPER::new ( |
… | |
… | |
4117 | my ($value, $title, $tooltip) = @$_; |
4195 | my ($value, $title, $tooltip) = @$_; |
4118 | |
4196 | |
4119 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
4197 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
4120 | } |
4198 | } |
4121 | |
4199 | |
4122 | CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
4200 | DC::UI::Menu->new (items => \@menu_items)->popup ($ev); |
4123 | } |
4201 | } |
4124 | |
4202 | |
4125 | sub _set_value { |
4203 | sub _set_value { |
4126 | my ($self, $value) = @_; |
4204 | my ($self, $value) = @_; |
4127 | |
4205 | |
… | |
… | |
4150 | $self->_set_value ($self->{value}); |
4228 | $self->_set_value ($self->{value}); |
4151 | } |
4229 | } |
4152 | |
4230 | |
4153 | ############################################################################# |
4231 | ############################################################################# |
4154 | |
4232 | |
4155 | package CFPlus::UI::Statusbox; |
4233 | package DC::UI::Statusbox; |
4156 | |
4234 | |
4157 | our @ISA = CFPlus::UI::VBox::; |
4235 | our @ISA = DC::UI::VBox::; |
4158 | |
4236 | |
4159 | sub new { |
4237 | sub new { |
4160 | my $class = shift; |
4238 | my $class = shift; |
4161 | |
4239 | |
4162 | my $self = $class->SUPER::new ( |
4240 | my $self = $class->SUPER::new ( |
4163 | fontsize => 0.8, |
4241 | fontsize => 0.8, |
4164 | @_, |
4242 | @_, |
4165 | ); |
4243 | ); |
4166 | |
4244 | |
4167 | CFPlus::weaken (my $this = $self); |
4245 | DC::weaken (my $this = $self); |
4168 | |
4246 | |
4169 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
4247 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
4170 | |
4248 | |
4171 | $self |
4249 | $self |
4172 | } |
4250 | } |
4173 | |
4251 | |
4174 | sub reorder { |
4252 | sub reorder { |
4175 | my ($self) = @_; |
4253 | my ($self) = @_; |
4176 | my $NOW = Time::HiRes::time; |
4254 | my $NOW = EV::time; |
4177 | |
4255 | |
4178 | # freeze display when hovering over any label |
4256 | # freeze display when hovering over any label |
4179 | return if $CFPlus::UI::TOOLTIP->{owner} |
4257 | return if $DC::UI::TOOLTIP->{owner} |
4180 | && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, |
4258 | && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, |
4181 | values %{ $self->{item} }; |
4259 | values %{ $self->{item} }; |
4182 | |
4260 | |
4183 | while (my ($k, $v) = each %{ $self->{item} }) { |
4261 | while (my ($k, $v) = each %{ $self->{item} }) { |
4184 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4262 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4185 | } |
4263 | } |
… | |
… | |
4206 | for ($short) { |
4284 | for ($short) { |
4207 | s/^\s+//; |
4285 | s/^\s+//; |
4208 | s/\s+/ /g; |
4286 | s/\s+/ /g; |
4209 | } |
4287 | } |
4210 | |
4288 | |
4211 | new CFPlus::UI::Label |
4289 | new DC::UI::Label |
4212 | markup => $short, |
4290 | markup => $short, |
4213 | tooltip => $item->{tooltip}, |
4291 | tooltip => $item->{tooltip}, |
4214 | tooltip_font => $::FONT_PROP, |
4292 | tooltip_font => $::FONT_PROP, |
4215 | tooltip_width => 0.67, |
4293 | tooltip_width => 0.67, |
4216 | fontsize => $item->{fontsize} || $self->{fontsize}, |
4294 | fontsize => $item->{fontsize} || $self->{fontsize}, |
4217 | max_w => $::WIDTH * 0.44, |
4295 | max_w => $::WIDTH * 0.44, |
|
|
4296 | align => 0, |
4218 | fg => [@{ $item->{fg} }], |
4297 | fg => [@{ $item->{fg} }], |
4219 | can_events => 1, |
4298 | can_events => 1, |
4220 | can_hover => 1 |
4299 | can_hover => 1 |
4221 | }; |
4300 | }; |
4222 | |
4301 | |
… | |
… | |
4230 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4309 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4231 | } |
4310 | } |
4232 | |
4311 | |
4233 | push @widgets, $label; |
4312 | push @widgets, $label; |
4234 | } |
4313 | } |
|
|
4314 | |
|
|
4315 | my $hash = join ",", @widgets; |
|
|
4316 | return if $hash eq $self->{last_widget_hash}; |
|
|
4317 | $self->{last_widget_hash} = $hash; |
4235 | |
4318 | |
4236 | $self->clear; |
4319 | $self->clear; |
4237 | $self->SUPER::add (reverse @widgets); |
4320 | $self->SUPER::add (reverse @widgets); |
4238 | } |
4321 | } |
4239 | |
4322 | |
… | |
… | |
4295 | $self->SUPER::destroy; |
4378 | $self->SUPER::destroy; |
4296 | } |
4379 | } |
4297 | |
4380 | |
4298 | ############################################################################# |
4381 | ############################################################################# |
4299 | |
4382 | |
4300 | package CFPlus::UI::Root; |
4383 | package DC::UI::Root; |
4301 | |
4384 | |
4302 | our @ISA = CFPlus::UI::Container::; |
4385 | our @ISA = DC::UI::Container::; |
4303 | |
4386 | |
4304 | use List::Util qw(min max); |
4387 | use List::Util qw(min max); |
4305 | |
4388 | |
4306 | use CFPlus::OpenGL; |
4389 | use DC::OpenGL; |
4307 | |
4390 | |
4308 | sub new { |
4391 | sub new { |
4309 | my $class = shift; |
4392 | my $class = shift; |
4310 | |
4393 | |
4311 | my $self = $class->SUPER::new ( |
4394 | my $self = $class->SUPER::new ( |
4312 | visible => 1, |
4395 | visible => 1, |
4313 | @_, |
4396 | @_, |
4314 | ); |
4397 | ); |
4315 | |
4398 | |
4316 | CFPlus::weaken ($self->{root} = $self); |
4399 | DC::weaken ($self->{root} = $self); |
4317 | |
4400 | |
4318 | $self |
4401 | $self |
4319 | } |
4402 | } |
4320 | |
4403 | |
4321 | sub size_request { |
4404 | sub size_request { |
… | |
… | |
4369 | } |
4452 | } |
4370 | |
4453 | |
4371 | sub update { |
4454 | sub update { |
4372 | my ($self) = @_; |
4455 | my ($self) = @_; |
4373 | |
4456 | |
4374 | $::WANT_REFRESH->start; |
4457 | $::WANT_REFRESH = 1; |
4375 | } |
4458 | } |
4376 | |
4459 | |
4377 | sub add { |
4460 | sub add { |
4378 | my ($self, @children) = @_; |
4461 | my ($self, @children) = @_; |
4379 | |
4462 | |
… | |
… | |
4446 | |
4529 | |
4447 | delete $queue{$widget+0}; |
4530 | delete $queue{$widget+0}; |
4448 | |
4531 | |
4449 | my ($w, $h) = $widget->size_request; |
4532 | my ($w, $h) = $widget->size_request; |
4450 | |
4533 | |
4451 | $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; |
4534 | $w += $widget->{padding_x} * 2; |
4452 | $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; |
4535 | $h += $widget->{padding_y} * 2; |
|
|
4536 | |
|
|
4537 | $w = max $widget->{min_w}, $w; |
|
|
4538 | $h = max $widget->{min_h}, $h; |
4453 | |
4539 | |
4454 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
4540 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
4455 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
4541 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
4456 | |
4542 | |
4457 | $w = $widget->{force_w} if exists $widget->{force_w}; |
4543 | $w = $widget->{force_w} if exists $widget->{force_w}; |
… | |
… | |
4524 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4610 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4525 | glMatrixMode GL_MODELVIEW; |
4611 | glMatrixMode GL_MODELVIEW; |
4526 | glLoadIdentity; |
4612 | glLoadIdentity; |
4527 | |
4613 | |
4528 | { |
4614 | { |
4529 | package CFPlus::UI::Base; |
4615 | package DC::UI::Base; |
4530 | |
4616 | |
4531 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4617 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4532 | (0, 0, $self->{w}, $self->{h}); |
4618 | (0, 0, $self->{w}, $self->{h}); |
4533 | |
4619 | |
4534 | $self->_draw; |
4620 | $self->_draw; |
4535 | } |
4621 | } |
4536 | } |
4622 | } |
4537 | |
4623 | |
4538 | ############################################################################# |
4624 | ############################################################################# |
4539 | |
4625 | |
4540 | package CFPlus::UI; |
4626 | package DC::UI; |
4541 | |
4627 | |
4542 | $ROOT = new CFPlus::UI::Root; |
4628 | $ROOT = new DC::UI::Root; |
4543 | $TOOLTIP = new CFPlus::UI::Tooltip z => 900; |
4629 | $TOOLTIP = new DC::UI::Tooltip z => 900; |
4544 | |
4630 | |
4545 | 1 |
4631 | 1 |
4546 | |
|
|