ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.392 by root, Sat Jul 21 13:37:09 2007 UTC vs.
Revision 1.457 by root, Fri Dec 28 15:05:20 2007 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
30 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; 32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32 33
33 return if $ENV{CFPLUS_DEBUG} & 8; 34 return if $ENV{CFPLUS_DEBUG} & 8;
34 35
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show; 37 $TOOLTIP->show;
41 } 38 }
42 39
43 return; 40 return;
46 } 43 }
47 44
48 $TOOLTIP->hide; 45 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
51}); 48};
52 49
53sub get_layout { 50sub get_layout {
54 my $layout; 51 my $layout;
55 52
56 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
91 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
92 89
93 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
95 92
96 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
97 } 94 }
98} 95}
99 96
100sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
101 my ($ev) = @_; 98 my ($ev) = @_;
107 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
108 105
109 $GRAB = $widget; 106 $GRAB = $widget;
110 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
111 108
112 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
113 } 110 }
114 111
115 if ($GRAB) { 112 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel 114 # mousewheel
118 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
120 117
121 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
123 120
124 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
142 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
143 $grab->update if $grab; 140 $grab->update if $grab;
144 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
145 142
146 check_hover $widget; 143 check_hover $widget;
147 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
148 } 145 }
149} 146}
150 147
151sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
152 my ($ev) = @_; 149 my ($ev) = @_;
214 reconfigure_widgets; 211 reconfigure_widgets;
215} 212}
216 213
217############################################################################# 214#############################################################################
218 215
219package CFPlus::UI::Event; 216package DC::UI::Event;
220 217
221sub xy { 218sub xy {
222 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
223} 220}
224 221
225############################################################################# 222#############################################################################
226 223
227package CFPlus::UI::Base; 224package DC::UI::Base;
228 225
229use strict; 226use strict;
230 227
231use CFPlus::OpenGL; 228use DC::OpenGL;
232 229
233sub new { 230sub new {
234 my $class = shift; 231 my $class = shift;
235 232
236 my $self = bless { 233 my $self = bless {
241 h => undef, 238 h => undef,
242 can_events => 1, 239 can_events => 1,
243 @_ 240 @_
244 }, $class; 241 }, $class;
245 242
246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
247 244
248 for (keys %$self) { 245 for (keys %$self) {
249 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
250 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
251 } 248 }
252 } 249 }
253 250
254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
255 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
256 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 253 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
257 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 254 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
258 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 255 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
259 256
260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
262 259
263 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
273 $self->emit ("destroy"); 270 $self->emit ("destroy");
274 %$self = (); 271 %$self = ();
275} 272}
276 273
277sub TO_JSON { 274sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
279} 276}
280 277
281sub show { 278sub show {
282 my ($self) = @_; 279 my ($self) = @_;
283 280
284 return if $self->{parent}; 281 return if $self->{parent};
285 282
286 $CFPlus::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
287} 284}
288 285
289sub set_visible { 286sub set_visible {
290 my ($self) = @_; 287 my ($self) = @_;
291 288
312 delete $self->{root}; 309 delete $self->{root};
313 310
314 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
315 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
316 313
317 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $DC::UI::TOOLTIP_WATCHER->invoke
318 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
319 316
320 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
321 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
322} 319}
341sub hide { 338sub hide {
342 my ($self) = @_; 339 my ($self) = @_;
343 340
344 $self->set_invisible; 341 $self->set_invisible;
345 342
343 # extra $parent copy for 5.8.8+ bug workaround
344 # (otherwise $_[0] in remove gets freed
345 if (my $parent = $self->{parent}) {
346 $self->{parent}->remove ($self) 346 $parent->remove ($self);
347 if $self->{parent}; 347 }
348} 348}
349 349
350sub move_abs { 350sub move_abs {
351 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
352 352
364 $self->{force_h} = $h; 364 $self->{force_h} = $h;
365 365
366 $self->realloc; 366 $self->realloc;
367} 367}
368 368
369# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh {
371 my ($self) = @_;
372
373 return $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT)
377}
378
369sub size_request { 379sub size_request {
370 require Carp; 380 require Carp;
371 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
372} 382}
373 383
379 my ($self, $x, $y, $w, $h) = @_; 389 my ($self, $x, $y, $w, $h) = @_;
380 390
381 if ($self->{aspect}) { 391 if ($self->{aspect}) {
382 my ($ow, $oh) = ($w, $h); 392 my ($ow, $oh) = ($w, $h);
383 393
384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 394 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 395 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
386 396
387 # use alignment to adjust x, y 397 # use alignment to adjust x, y
388 398
389 $x += int 0.5 * ($ow - $w); 399 $x += int 0.5 * ($ow - $w);
390 $y += int 0.5 * ($oh - $h); 400 $y += int 0.5 * ($oh - $h);
431 441
432 return if $self->{tooltip} eq $tooltip; 442 return if $self->{tooltip} eq $tooltip;
433 443
434 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
435 445
436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($DC::UI::TOOLTIP->{owner} == $self) {
437 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $DC::UI::TOOLTIP->{owner};
438 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $DC::UI::TOOLTIP_WATCHER->invoke;
439 } 449 }
440} 450}
441 451
442# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
443sub coord2local { 453sub coord2local {
444 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
445 455
446 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
447 457
448 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
449} 459}
450 460
451# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
452sub coord2global { 462sub coord2global {
453 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
454 464
455 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
456 466
457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
458} 468}
459 469
460sub invoke_focus_in { 470sub invoke_focus_in {
509sub connect { 519sub connect {
510 my ($self, $signal, $cb) = @_; 520 my ($self, $signal, $cb) = @_;
511 521
512 push @{ $self->{signal_cb}{$signal} }, $cb; 522 push @{ $self->{signal_cb}{$signal} }, $cb;
513 523
514 defined wantarray and CFPlus::guard { 524 defined wantarray and DC::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} }; 526 @{ $self->{signal_cb}{$signal} };
517 } 527 }
518} 528}
519 529
557# in .xs 567# in .xs
558 568
559sub set_parent { 569sub set_parent {
560 my ($self, $parent) = @_; 570 my ($self, $parent) = @_;
561 571
562 CFPlus::weaken ($self->{parent} = $parent); 572 DC::weaken ($self->{parent} = $parent);
563 $self->set_visible if $parent->{visible}; 573 $self->set_visible if $parent->{visible};
564} 574}
565 575
566sub realloc { 576sub realloc {
567 my ($self) = @_; 577 my ($self) = @_;
593 603
594# using global variables seems a bit hacky, but passing through all drawing 604# using global variables seems a bit hacky, but passing through all drawing
595# functions seems pointless. 605# functions seems pointless.
596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 606our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
597 607
598sub draw { 608#sub draw {
599 my ($self) = @_; 609#CFPlus.xs
600
601 return unless $self->{h} && $self->{w};
602
603 # update screen rectangle
604 local $draw_x = $draw_x + $self->{x};
605 local $draw_y = $draw_y + $self->{y};
606
607 # skip widgets that are entirely outside the drawing area
608 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
609 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
610
611 glPushMatrix;
612 glTranslate $self->{x}, $self->{y}, 0;
613
614 if ($self == $HOVER && $self->{can_hover}) {
615 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
616 glEnable GL_BLEND;
617 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
618 glBegin GL_QUADS;
619 glVertex 0 , 0;
620 glVertex $self->{w}, 0;
621 glVertex $self->{w}, $self->{h};
622 glVertex 0 , $self->{h};
623 glEnd;
624 glDisable GL_BLEND;
625 }
626
627 if ($ENV{CFPLUS_DEBUG} & 1) {
628 glPushMatrix;
629 glColor 1, 1, 0, 1;
630 glTranslate 0.375, 0.375;
631 glBegin GL_LINE_LOOP;
632 glVertex 0 , 0;
633 glVertex $self->{w} - 1, 0;
634 glVertex $self->{w} - 1, $self->{h} - 1;
635 glVertex 0 , $self->{h} - 1;
636 glEnd;
637 glPopMatrix;
638 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
639 }
640
641 $self->_draw;
642 glPopMatrix;
643}
644 610
645sub _draw { 611sub _draw {
646 my ($self) = @_; 612 my ($self) = @_;
647 613
648 warn "no draw defined for $self\n"; 614 warn "no draw defined for $self\n";
649} 615}
650 616
651my $cntx;#d#
652sub DESTROY { 617sub DESTROY {
653 my ($self) = @_; 618 my ($self) = @_;
654 619
655 return if CFPlus::in_destruct; 620 return if DC::in_destruct;
656 621
622 local $@;
657 eval { $self->destroy }; 623 eval { $self->destroy };
658 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
659 625
660 delete $WIDGET{$self+0}; 626 delete $WIDGET{$self+0};
661} 627}
662 628
663############################################################################# 629#############################################################################
664 630
665package CFPlus::UI::DrawBG; 631package DC::UI::DrawBG;
666 632
667our @ISA = CFPlus::UI::Base::; 633our @ISA = DC::UI::Base::;
668 634
669use strict; 635use strict;
670use CFPlus::OpenGL; 636use DC::OpenGL;
671 637
672sub new { 638sub new {
673 my $class = shift; 639 my $class = shift;
674
675 # range [value, low, high, page]
676 640
677 $class->SUPER::new ( 641 $class->SUPER::new (
678 #bg => [0, 0, 0, 0.2], 642 #bg => [0, 0, 0, 0.2],
679 #active_bg => [1, 1, 1, 0.5], 643 #active_bg => [1, 1, 1, 0.5],
680 @_ 644 @_
692 my ($w, $h) = @$self{qw(w h)}; 656 my ($w, $h) = @$self{qw(w h)};
693 657
694 glEnable GL_BLEND; 658 glEnable GL_BLEND;
695 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 659 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
696 glColor_premultiply @$color; 660 glColor_premultiply @$color;
697
698 glBegin GL_QUADS;
699 glVertex 0 , 0;
700 glVertex 0 , $h;
701 glVertex $w, $h; 661 glRect 0, 0, $w, $h;
702 glVertex $w, 0;
703 glEnd;
704
705 glDisable GL_BLEND; 662 glDisable GL_BLEND;
706 } 663 }
707} 664}
708 665
709############################################################################# 666#############################################################################
710 667
711package CFPlus::UI::Empty; 668package DC::UI::Empty;
712 669
713our @ISA = CFPlus::UI::Base::; 670our @ISA = DC::UI::Base::;
714 671
715sub new { 672sub new {
716 my ($class, %arg) = @_; 673 my ($class, %arg) = @_;
717 $class->SUPER::new (can_events => 0, %arg); 674 $class->SUPER::new (can_events => 0, %arg);
718} 675}
725 682
726sub draw { } 683sub draw { }
727 684
728############################################################################# 685#############################################################################
729 686
730package CFPlus::UI::Container; 687package DC::UI::Container;
731 688
732our @ISA = CFPlus::UI::Base::; 689our @ISA = DC::UI::Base::;
733 690
734sub new { 691sub new {
735 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
736 693
737 my $children = delete $arg{children}; 694 my $children = delete $arg{children};
769 sort { $a->{z} <=> $b->{z} } 726 sort { $a->{z} <=> $b->{z} }
770 @{$self->{children}}, @widgets 727 @{$self->{children}}, @widgets
771 ]; 728 ];
772 729
773 $self->realloc; 730 $self->realloc;
731
732 $self->emit (c_add => \@widgets);
733
734 map $_+0, @widgets
774} 735}
775 736
776sub children { 737sub children {
777 @{ $_[0]{children} } 738 @{ $_[0]{children} }
778} 739}
779 740
780sub remove { 741sub remove {
781 my ($self, $child) = @_; 742 my ($self, @widgets) = @_;
782 743
744 $self->emit (c_remove => \@widgets);
745
746 for my $child (@widgets) {
783 delete $child->{parent}; 747 delete $child->{parent};
784 $child->hide; 748 $child->hide;
785
786 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 749 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
750 }
787 751
788 $self->realloc; 752 $self->realloc;
789} 753}
790 754
791sub clear { 755sub clear {
792 my ($self) = @_; 756 my ($self) = @_;
793 757
794 my $children = delete $self->{children}; 758 my $children = $self->{children};
795 $self->{children} = []; 759 $self->{children} = [];
796 760
797 for (@$children) { 761 for (@$children) {
798 delete $_->{parent}; 762 delete $_->{parent};
799 $_->hide; 763 $_->hide;
819} 783}
820 784
821sub _draw { 785sub _draw {
822 my ($self) = @_; 786 my ($self) = @_;
823 787
824 $_->draw for @{$self->{children}}; 788 $_->draw for $self->visible_children;
825} 789}
826 790
827############################################################################# 791#############################################################################
828 792
829package CFPlus::UI::Bin; 793package DC::UI::Bin;
830 794
831our @ISA = CFPlus::UI::Container::; 795our @ISA = DC::UI::Container::;
832 796
833sub new { 797sub new {
834 my ($class, %arg) = @_; 798 my ($class, %arg) = @_;
835 799
836 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 800 my $child = (delete $arg{child}) || new DC::UI::Empty::;
837 801
838 $class->SUPER::new (children => [$child], %arg) 802 $class->SUPER::new (children => [$child], %arg)
839} 803}
840 804
841sub add { 805sub add {
842 my ($self, $child) = @_; 806 my ($self, $child) = @_;
843 807
844 $self->SUPER::remove ($_) for @{ $self->{children} }; 808 $self->clear;
845 $self->SUPER::add ($child); 809 $self->SUPER::add ($child);
846} 810}
847 811
848sub remove { 812sub remove {
849 my ($self, $widget) = @_; 813 my ($self, $widget) = @_;
850 814
851 $self->SUPER::remove ($widget); 815 $self->SUPER::remove ($widget);
852 816
853 $self->{children} = [new CFPlus::UI::Empty] 817 $self->{children} = [new DC::UI::Empty]
854 unless @{$self->{children}}; 818 unless @{$self->{children}};
855} 819}
856 820
857sub child { $_[0]->{children}[0] } 821sub child { $_[0]->{children}[0] }
858 822
867 831
868 1 832 1
869} 833}
870 834
871############################################################################# 835#############################################################################
872
873# back-buffered drawing area 836# back-buffered drawing area
874 837
875package CFPlus::UI::Window; 838package DC::UI::Window;
876 839
877our @ISA = CFPlus::UI::Bin::; 840our @ISA = DC::UI::Bin::;
878 841
879use CFPlus::OpenGL; 842use DC::OpenGL;
880 843
881sub new { 844sub new {
882 my ($class, %arg) = @_; 845 my ($class, %arg) = @_;
883 846
884 my $self = $class->SUPER::new (%arg); 847 my $self = $class->SUPER::new (%arg);
906} 869}
907 870
908sub render_child { 871sub render_child {
909 my ($self) = @_; 872 my ($self) = @_;
910 873
911 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 874 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
912 glClearColor 0, 0, 0, 0; 875 glClearColor 0, 0, 0, 0;
913 glClear GL_COLOR_BUFFER_BIT; 876 glClear GL_COLOR_BUFFER_BIT;
914 877
915 { 878 {
916 package CFPlus::UI::Base; 879 package DC::UI::Base;
917 880
918 local ($draw_x, $draw_y, $draw_w, $draw_h) = 881 local ($draw_x, $draw_y, $draw_w, $draw_h) =
919 (0, 0, $self->{w}, $self->{h}); 882 (0, 0, $self->{w}, $self->{h});
920 883
921 $self->_render; 884 $self->_render;
938 glDisable GL_TEXTURE_2D; 901 glDisable GL_TEXTURE_2D;
939} 902}
940 903
941############################################################################# 904#############################################################################
942 905
943package CFPlus::UI::ViewPort; 906package DC::UI::ViewPort;
944 907
945use List::Util qw(min max); 908use List::Util qw(min max);
946 909
947our @ISA = CFPlus::UI::Window::; 910our @ISA = DC::UI::Window::;
948 911
949sub new { 912sub new {
950 my $class = shift; 913 my $class = shift;
951 914
952 $class->SUPER::new ( 915 $class->SUPER::new (
994 $self->emit (changed => $x, $y); 957 $self->emit (changed => $x, $y);
995 $self->update; 958 $self->update;
996 } 959 }
997} 960}
998 961
962sub set_center {
963 my ($self, $x, $y) = @_;
964
965 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
966}
967
968sub make_visible {
969 my ($self, $x, $y, $border) = @_;
970
971 if ( $x < $self->{view_x} + $self->{w} * $border
972 || $x > $self->{view_x} + $self->{w} * (1 - $border)
973 || $y < $self->{view_y} + $self->{h} * $border
974 || $y > $self->{view_y} + $self->{h} * (1 - $border)
975 ) {
976 $self->set_center ($x, $y);
977 }
978}
979
999# hmm, this does not work for topleft of $self... but we should not ask for that 980# hmm, this does not work for topleft of $self... but we should not ask for that
1000sub coord2local { 981sub coord2local {
1001 my ($self, $x, $y) = @_; 982 my ($self, $x, $y) = @_;
1002 983
1003 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) 984 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1020 ) { 1001 ) {
1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1022 } else { 1003 } else {
1023 $self->CFPlus::UI::Base::find_widget ($x, $y) 1004 $self->DC::UI::Base::find_widget ($x, $y)
1024 } 1005 }
1025} 1006}
1026 1007
1027sub _render { 1008sub _render {
1028 my ($self) = @_; 1009 my ($self) = @_;
1029 1010
1030 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1011 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1031 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1012 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1032 1013
1033 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1014 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1034 1015
1035 $self->SUPER::_render; 1016 $self->SUPER::_render;
1036} 1017}
1037 1018
1038############################################################################# 1019#############################################################################
1039 1020
1040package CFPlus::UI::ScrolledWindow; 1021package DC::UI::ScrolledWindow;
1041 1022
1042our @ISA = CFPlus::UI::Table::; 1023our @ISA = DC::UI::Table::;
1043 1024
1044sub new { 1025sub new {
1045 my ($class, %arg) = @_; 1026 my ($class, %arg) = @_;
1046 1027
1047 my $child = delete $arg{child}; 1028 my $child = delete $arg{child};
1048 1029
1049 my $self; 1030 my $self;
1050 1031
1051 my $hslider = new CFPlus::UI::Slider 1032 my $hslider = new DC::UI::Slider
1052 col => 0, 1033 c_col => 0,
1053 row => 1, 1034 c_row => 1,
1054 vertical => 0, 1035 vertical => 0,
1055 range => [0, 0, 1, 0.01], # HACK fix 1036 range => [0, 0, 1, 0.01], # HACK fix
1056 on_changed => sub { 1037 on_changed => sub {
1057 $self->{hpos} = $_[1]; 1038 $self->{hpos} = $_[1];
1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1059 }, 1040 },
1060 ; 1041 ;
1061 1042
1062 my $vslider = new CFPlus::UI::Slider 1043 my $vslider = new DC::UI::Slider
1063 col => 1, 1044 c_col => 1,
1064 row => 0, 1045 c_row => 0,
1065 vertical => 1, 1046 vertical => 1,
1066 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1067 on_changed => sub { 1048 on_changed => sub {
1068 $self->{vpos} = $_[1]; 1049 $self->{vpos} = $_[1];
1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1079 col_expand => [1, 0], 1060 col_expand => [1, 0],
1080 row_expand => [1, 0], 1061 row_expand => [1, 0],
1081 %arg, 1062 %arg,
1082 ); 1063 );
1083 1064
1084 $self->{vp} = new CFPlus::UI::ViewPort 1065 $self->{vp} = new DC::UI::ViewPort
1085 col => 0, 1066 c_col => 0,
1086 row => 0, 1067 c_row => 0,
1087 expand => 1, 1068 expand => 1,
1088 scroll_x => $self->{scroll_x}, 1069 scroll_x => $self->{scroll_x},
1089 scroll_y => $self->{scroll_y}, 1070 scroll_y => $self->{scroll_y},
1090 on_changed => sub { 1071 on_changed => sub {
1091 my ($vp, $x, $y) = @_; 1072 my ($vp, $x, $y) = @_;
1093 $vp->{parent}{hslider}->set_value ($x); 1074 $vp->{parent}{hslider}->set_value ($x);
1094 $vp->{parent}{vslider}->set_value ($y); 1075 $vp->{parent}{vslider}->set_value ($y);
1095 1076
1096 0 1077 0
1097 }, 1078 },
1079 on_size_allocate => sub {
1080 my ($vp, $w, $h) = @_;
1081 $vp->{parent}->update_slider;
1082 0
1083 },
1098 ; 1084 ;
1099 1085
1100 $self->SUPER::add ($self->{vp}); 1086 $self->SUPER::add ($self->{vp});
1101 1087
1102 $self->add ($child) if $child; 1088 $self->add ($child) if $child;
1103 1089
1104 $self 1090 $self
1105} 1091}
1106
1107#TODO# update range on size_allocate depending on child?
1108 1092
1109sub add { 1093sub add {
1110 my ($self, $widget) = @_; 1094 my ($self, $widget) = @_;
1111 1095
1112 $self->{vp}->add ($self->{child} = $widget); 1096 $self->{vp}->add ($self->{child} = $widget);
1113} 1097}
1114 1098
1099sub set_offset { shift->{vp}->set_offset (@_) }
1100sub set_center { shift->{vp}->set_center (@_) }
1101sub make_visible { shift->{vp}->make_visible (@_) }
1102
1115sub update_slider { 1103sub update_slider {
1116 my ($self) = @_; 1104 my ($self) = @_;
1117 1105
1118 my $child = ($self->{vp} or return)->child; 1106 my $child = ($self->{vp} or return)->child;
1119 1107
1108 if ($self->{scroll_x}) {
1120 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1109 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1121 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1110 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1122 1111
1123 my $visible = $w1 > $w2; 1112 my $visible = $w1 > $w2;
1124 if ($visible != $self->{hslider}{visible}) { 1113 if ($visible != $self->{hslider_visible}) {
1114 $self->{hslider_visible} = $visible;
1125 $visible ? $self->SUPER::add ($self->{hslider}) 1115 $visible ? $self->SUPER::add ($self->{hslider})
1126 : $self->SUPER::remove ($self->{hslider}); 1116 : $self->SUPER::remove ($self->{hslider});
1117 }
1127 } 1118 }
1128 1119
1120 if ($self->{scroll_y}) {
1129 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1121 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1130 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1122 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1131 1123
1132 my $visible = $h1 > $h2; 1124 my $visible = $h1 > $h2;
1133 if ($visible != $self->{vslider}{visible}) { 1125 if ($visible != $self->{vslider_visible}) {
1126 $self->{vslider_visible} = $visible;
1134 $visible ? $self->SUPER::add ($self->{vslider}) 1127 $visible ? $self->SUPER::add ($self->{vslider})
1135 : $self->SUPER::remove ($self->{vslider}); 1128 : $self->SUPER::remove ($self->{vslider});
1129 }
1130 }
1131}
1132
1133sub start_dragging {
1134 my ($self, $ev) = @_;
1135
1136 $self->grab_focus;
1137
1138 my $ox = $self->{vp}{view_x};
1139 my $oy = $self->{vp}{view_y};
1140
1141 $self->{motion} = sub {
1142 my ($ev, $x, $y) = @_;
1143
1144 $ox -= $ev->{xrel};
1145 $oy -= $ev->{yrel};
1146
1147 $self->{vp}->set_offset ($ox, $oy);
1136 } 1148 };
1137}
1138
1139sub update {
1140 my ($self) = @_;
1141
1142 $self->update_slider;
1143 $self->SUPER::update;
1144} 1149}
1145 1150
1146sub invoke_mouse_wheel { 1151sub invoke_mouse_wheel {
1147 my ($self, $ev) = @_; 1152 my ($self, $ev) = @_;
1148 1153
1149 $self->{vslider}->emit (mouse_wheel => $ev) 1154 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1150 if $ev->{dy};
1151
1152 $self->{hslider}->emit (mouse_wheel => $ev) 1155 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1153 if $ev->{dx};
1154 1156
1155 1 1157 1
1156} 1158}
1157 1159
1158sub invoke_button_down { 1160sub invoke_button_down {
1159 my ($self, $ev, $x, $y) = @_; 1161 my ($self, $ev, $x, $y) = @_;
1160 1162
1161 if ($ev->{button} == 2) { 1163 if ($ev->{button} == 2) {
1162 $self->grab_focus; 1164 $self->start_dragging ($ev);
1163
1164 my $ox = $self->{vp}{view_x} + $ev->{x};
1165 my $oy = $self->{vp}{view_y} + $ev->{y};
1166
1167 $self->{motion} = sub {
1168 my ($ev, $x, $y) = @_;
1169
1170 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1171 $self->update;
1172 };
1173
1174 return 1; 1165 return 1;
1175 } 1166 }
1176 1167
1177 0 1168 0
1178} 1169}
1205 $self->SUPER::invoke_size_allocate ($w, $h) 1196 $self->SUPER::invoke_size_allocate ($w, $h)
1206} 1197}
1207 1198
1208############################################################################# 1199#############################################################################
1209 1200
1210package CFPlus::UI::Frame; 1201package DC::UI::Frame;
1211 1202
1212our @ISA = CFPlus::UI::Bin::; 1203our @ISA = DC::UI::Bin::;
1213 1204
1214use CFPlus::OpenGL; 1205use DC::OpenGL;
1215 1206
1216sub new { 1207sub new {
1217 my $class = shift; 1208 my $class = shift;
1218 1209
1219 $class->SUPER::new ( 1210 $class->SUPER::new (
1229 my ($w, $h) = @$self{qw(w h)}; 1220 my ($w, $h) = @$self{qw(w h)};
1230 1221
1231 glEnable GL_BLEND; 1222 glEnable GL_BLEND;
1232 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1223 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1233 glColor_premultiply @{ $self->{bg} }; 1224 glColor_premultiply @{ $self->{bg} };
1234
1235 glBegin GL_QUADS;
1236 glVertex 0 , 0;
1237 glVertex 0 , $h;
1238 glVertex $w, $h; 1225 glRect 0, 0, $w, $h;
1239 glVertex $w, 0;
1240 glEnd;
1241
1242 glDisable GL_BLEND; 1226 glDisable GL_BLEND;
1243 } 1227 }
1244 1228
1245 $self->SUPER::_draw; 1229 $self->SUPER::_draw;
1246} 1230}
1247 1231
1248############################################################################# 1232#############################################################################
1249 1233
1250package CFPlus::UI::FancyFrame; 1234package DC::UI::FancyFrame;
1251 1235
1252our @ISA = CFPlus::UI::Bin::; 1236our @ISA = DC::UI::Bin::;
1253 1237
1254use CFPlus::OpenGL; 1238use DC::OpenGL;
1255 1239
1256sub new { 1240sub new {
1257 my ($class, %arg) = @_; 1241 my ($class, %arg) = @_;
1258 1242
1259 if ((exists $arg{label}) && !ref $arg{label}) { 1243 if ((exists $arg{label}) && !ref $arg{label}) {
1260 $arg{label} = new CFPlus::UI::Label 1244 $arg{label} = new DC::UI::Label
1261 align => 1, 1245 align => 1,
1262 valign => 0, 1246 valign => 0,
1263 text => $arg{label}, 1247 text => $arg{label},
1264 fontsize => ($arg{border} || 0.8) * 0.75; 1248 fontsize => ($arg{border} || 0.8) * 0.75;
1265 } 1249 }
1277 1261
1278sub add { 1262sub add {
1279 my ($self, @widgets) = @_; 1263 my ($self, @widgets) = @_;
1280 1264
1281 $self->SUPER::add (@widgets); 1265 $self->SUPER::add (@widgets);
1282 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1266 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1283} 1267}
1284 1268
1285sub border { 1269sub border {
1286 int $_[0]{border} * $::FONTSIZE 1270 int $_[0]{border} * $::FONTSIZE
1287} 1271}
1345 } 1329 }
1346} 1330}
1347 1331
1348############################################################################# 1332#############################################################################
1349 1333
1350package CFPlus::UI::Toplevel; 1334package DC::UI::Toplevel;
1351 1335
1352our @ISA = CFPlus::UI::Bin::; 1336our @ISA = DC::UI::Bin::;
1353 1337
1354use CFPlus::OpenGL; 1338use DC::OpenGL;
1355 1339
1356my $bg = 1340my $bg =
1357 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1341 new_from_file DC::Texture DC::find_rcfile "d1_bg.png",
1358 mipmap => 1, wrap => 1; 1342 mipmap => 1, wrap => 1;
1359 1343
1360my @border = 1344my @border =
1361 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1345 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1362 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1346 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1363 1347
1364my @icon = 1348my @icon =
1365 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1349 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
1366 qw(x1_move.png x1_resize.png); 1350 qw(x1_move.png x1_resize.png);
1367 1351
1368sub new { 1352sub new {
1369 my ($class, %arg) = @_; 1353 my ($class, %arg) = @_;
1370 1354
1376 min_w => 64, 1360 min_w => 64,
1377 min_h => 32, 1361 min_h => 32,
1378 %arg, 1362 %arg,
1379 ); 1363 );
1380 1364
1381 $self->{title_widget} = new CFPlus::UI::Label 1365 $self->{title_widget} = new DC::UI::Label
1382 align => 0, 1366 align => 0,
1383 valign => 1, 1367 valign => 1,
1384 text => $self->{title}, 1368 text => $self->{title},
1385 fontsize => $self->{border}, 1369 fontsize => $self->{border},
1386 if exists $self->{title}; 1370 if exists $self->{title};
1387 1371
1388 if ($self->{has_close_button}) { 1372 if ($self->{has_close_button}) {
1389 $self->{close_button} = 1373 $self->{close_button} =
1390 new CFPlus::UI::ImageButton 1374 new DC::UI::ImageButton
1391 path => 'x1_close.png', 1375 path => 'x1_close.png',
1392 on_activate => sub { $self->emit ("delete") }; 1376 on_activate => sub { $self->emit ("delete") };
1393 1377
1394 $self->CFPlus::UI::Container::add ($self->{close_button}); 1378 $self->DC::UI::Container::add ($self->{close_button});
1395 } 1379 }
1396 1380
1397 $self 1381 $self
1398} 1382}
1399 1383
1400sub add { 1384sub add {
1401 my ($self, @widgets) = @_; 1385 my ($self, @widgets) = @_;
1402 1386
1403 $self->SUPER::add (@widgets); 1387 $self->SUPER::add (@widgets);
1404 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1388 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1405 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1389 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1406} 1390}
1407 1391
1408sub border { 1392sub border {
1409 int $_[0]{border} * $::FONTSIZE 1393 int $_[0]{border} * $::FONTSIZE
1394}
1395
1396sub get_max_wh {
1397 my ($self) = @_;
1398
1399 return ($self->{w}, $self->{h})
1400 if $self->{visible} && $self->{w};
1401
1402 $self->SUPER::get_max_wh
1410} 1403}
1411 1404
1412sub size_request { 1405sub size_request {
1413 my ($self) = @_; 1406 my ($self) = @_;
1414 1407
1586 if $self->{close_button}; 1579 if $self->{close_button};
1587} 1580}
1588 1581
1589############################################################################# 1582#############################################################################
1590 1583
1591package CFPlus::UI::Table; 1584package DC::UI::Table;
1592 1585
1593our @ISA = CFPlus::UI::Container::; 1586our @ISA = DC::UI::Container::;
1594 1587
1595use List::Util qw(max sum); 1588use List::Util qw(max sum);
1596 1589
1597use CFPlus::OpenGL; 1590use DC::OpenGL;
1598 1591
1599sub new { 1592sub new {
1600 my $class = shift; 1593 my $class = shift;
1601 1594
1602 $class->SUPER::new ( 1595 $class->SUPER::new (
1608 1601
1609sub add { 1602sub add {
1610 my ($self, @widgets) = @_; 1603 my ($self, @widgets) = @_;
1611 1604
1612 for my $child (@widgets) { 1605 for my $child (@widgets) {
1613 $child->{rowspan} ||= 1; 1606 $child->{c_rowspan} ||= 1;
1614 $child->{colspan} ||= 1; 1607 $child->{c_colspan} ||= 1;
1615 } 1608 }
1616 1609
1617 $self->SUPER::add (@widgets); 1610 $self->SUPER::add (@widgets);
1618} 1611}
1619 1612
1623 my @widgets; 1616 my @widgets;
1624 1617
1625 while (@_) { 1618 while (@_) {
1626 my ($col, $row, $child) = splice @_, 0, 3, (); 1619 my ($col, $row, $child) = splice @_, 0, 3, ();
1627 1620
1628 $child->{row} = $row; 1621 $child->{c_row} = $row;
1629 $child->{col} = $col; 1622 $child->{c_col} = $col;
1630 1623
1631 push @widgets, $child; 1624 push @widgets, $child;
1632 } 1625 }
1633 1626
1634 $self->add (@widgets); 1627 $self->add (@widgets);
1640 my (@w, @h); 1633 my (@w, @h);
1641 1634
1642 my @children = $self->children; 1635 my @children = $self->children;
1643 1636
1644 # first pass, columns 1637 # first pass, columns
1645 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { 1638 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1646 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; 1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1647 1640
1648 my $sw = sum @w[$c .. $c + $cs - 1]; 1641 my $sw = sum @w[$c .. $c + $cs - 1];
1649 1642
1650 if ($w > $sw) { 1643 if ($w > $sw) {
1651 $_ += ($w - $sw) / $cs for @w[$c .. $c + $cs - 1]; 1644 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1652 } 1645 }
1653 } 1646 }
1654 1647
1655 # second pass, rows 1648 # second pass, rows
1656 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { 1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1657 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; 1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1658 1651
1659 my $sh = sum @h[$r .. $r + $rs - 1]; 1652 my $sh = sum @h[$r .. $r + $rs - 1];
1660 1653
1661 if ($h > $sh) { 1654 if ($h > $sh) {
1662 $_ += ($h - $sh) / $rs for @h[$r .. $r + $rs - 1]; 1655 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1663 } 1656 }
1664 } 1657 }
1665 1658
1666 (\@w, \@h) 1659 (\@w, \@h)
1667} 1660}
1690 @col_expand = (1) x @$ws unless @col_expand; 1683 @col_expand = (1) x @$ws unless @col_expand;
1691 my $col_expand = (sum @col_expand) || 1; 1684 my $col_expand = (sum @col_expand) || 1;
1692 1685
1693 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1694 1687
1695 CFPlus::UI::harmonize $ws; 1688 DC::UI::harmonize $ws;
1696 1689
1697 my @row_expand = @{$self->{row_expand}}; 1690 my @row_expand = @{$self->{row_expand}};
1698 @row_expand = (1) x @$ws unless @row_expand; 1691 @row_expand = (1) x @$ws unless @row_expand;
1699 my $row_expand = (sum @row_expand) || 1; 1692 my $row_expand = (sum @row_expand) || 1;
1700 1693
1701 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1702 1695
1703 CFPlus::UI::harmonize $hs; 1696 DC::UI::harmonize $hs;
1704 1697
1705 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1706 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1707 1700
1708 for my $widget ($self->children) { 1701 for my $widget ($self->children) {
1709 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; 1702 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1710 1703
1711 $widget->configure ( 1704 $widget->configure (
1712 $x[$c], $y[$r], 1705 $x[$c], $y[$r],
1713 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], 1706 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1714 ); 1707 );
1717 1 1710 1
1718} 1711}
1719 1712
1720############################################################################# 1713#############################################################################
1721 1714
1722package CFPlus::UI::Fixed; 1715package DC::UI::Fixed;
1723 1716
1724use List::Util qw(min max); 1717use List::Util qw(min max);
1725 1718
1726our @ISA = CFPlus::UI::Container::; 1719our @ISA = DC::UI::Container::;
1727
1728sub add {
1729 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1730
1731 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1732 $self->SUPER::add ($child);
1733}
1734 1720
1735sub _scale($$$) { 1721sub _scale($$$) {
1736 my ($mode, $val, $max) = @_; 1722 my ($rel, $val, $max) = @_;
1737 1723
1738 $mode eq "abs" ? $val 1724 $rel ? $val * $max : $val
1739 : $mode eq "rel" ? $val * $max
1740 : 0
1741} 1725}
1742 1726
1743sub size_request { 1727sub size_request {
1744 my ($self) = @_; 1728 my ($self) = @_;
1745 1729
1746 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); 1730 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1747 1731
1748 # determine overall size by querying abs widgets 1732 # determine overall size by querying abs widgets
1749 for my $child ($self->visible_children) { 1733 for my $child ($self->visible_children) {
1750 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1734 unless ($child->{c_rel}) {
1735 my $x = $child->{c_x};
1736 my $y = $child->{c_y};
1751 1737
1752 if ($pos eq "abs") {
1753 $w = _scale $size, $w, $child->{req_w};
1754 $h = _scale $size, $h, $child->{req_h};
1755
1756 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1738 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1757 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1739 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1758 } 1740 }
1759 } 1741 }
1760 1742
1761 my $W = $x2 - $x1; 1743 my $W = $x2 - $x1;
1762 my $H = $y2 - $y1; 1744 my $H = $y2 - $y1;
1763 1745
1764 # now layout remaining widgets 1746 # now layout remaining widgets
1765 for my $child ($self->visible_children) { 1747 for my $child ($self->visible_children) {
1766 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1748 if ($child->{c_rel}) {
1749 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1750 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1767 1751
1768 if ($pos ne "abs") {
1769 $x = _scale $pos, $x, $W;
1770 $y = _scale $pos, $x, $H;
1771 $w = _scale $size, $w, $child->{req_w};
1772 $h = _scale $size, $h, $child->{req_h};
1773
1774 $x1 = min $x1, $x; $x2 = max $x2, $x + $w; 1752 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1775 $y1 = min $y1, $y; $y2 = max $y2, $y + $h; 1753 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1776 } 1754 }
1777 } 1755 }
1778 1756
1779 my $W = $x2 - $x1; 1757 my $W = $x2 - $x1;
1780 my $H = $y2 - $y1; 1758 my $H = $y2 - $y1;
1784 1762
1785sub invoke_size_allocate { 1763sub invoke_size_allocate {
1786 my ($self, $W, $H) = @_; 1764 my ($self, $W, $H) = @_;
1787 1765
1788 for my $child ($self->visible_children) { 1766 for my $child ($self->visible_children) {
1789 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; 1767 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1768 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1790 1769
1791 $x = _scale $pos, $x, $W; 1770 $x += $child->{c_halign} * $child->{req_w};
1792 $y = _scale $pos, $x, $H; 1771 $y += $child->{c_valign} * $child->{req_h};
1793 $w = _scale $size, $w, $child->{req_w};
1794 $h = _scale $size, $h, $child->{req_h};
1795 1772
1796 $child->configure ($x, $y, $w, $h); 1773 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1797 } 1774 }
1798 1775
1799 1 1776 1
1800} 1777}
1801 1778
1802############################################################################# 1779#############################################################################
1803 1780
1804package CFPlus::UI::Box; 1781package DC::UI::Box;
1805 1782
1806our @ISA = CFPlus::UI::Container::; 1783our @ISA = DC::UI::Container::;
1807 1784
1808sub size_request { 1785sub size_request {
1809 my ($self) = @_; 1786 my ($self) = @_;
1787
1788 my @children = $self->visible_children;
1810 1789
1811 $self->{vertical} 1790 $self->{vertical}
1812 ? ( 1791 ? (
1813 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1792 (List::Util::max map $_->{req_w}, @children),
1814 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_h}, @children),
1815 ) 1794 )
1816 : ( 1795 : (
1817 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1796 (List::Util::sum map $_->{req_w}, @children),
1818 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1797 (List::Util::max map $_->{req_h}, @children),
1819 ) 1798 )
1820} 1799}
1821 1800
1822sub invoke_size_allocate { 1801sub invoke_size_allocate {
1823 my ($self, $w, $h) = @_; 1802 my ($self, $w, $h) = @_;
1844 $req[$_] += $space * $children[$_]{expand} 1823 $req[$_] += $space * $children[$_]{expand}
1845 for 0 .. $#children; 1824 for 0 .. $#children;
1846 } 1825 }
1847 } 1826 }
1848 1827
1849 CFPlus::UI::harmonize \@req; 1828 DC::UI::harmonize \@req;
1850 1829
1851 my $pos = 0; 1830 my $pos = 0;
1852 for (0 .. $#children) { 1831 for (0 .. $#children) {
1853 my $alloc = $req[$_]; 1832 my $alloc = $req[$_];
1854 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1859 1 1838 1
1860} 1839}
1861 1840
1862############################################################################# 1841#############################################################################
1863 1842
1864package CFPlus::UI::HBox; 1843package DC::UI::HBox;
1865 1844
1866our @ISA = CFPlus::UI::Box::; 1845our @ISA = DC::UI::Box::;
1867 1846
1868sub new { 1847sub new {
1869 my $class = shift; 1848 my $class = shift;
1870 1849
1871 $class->SUPER::new ( 1850 $class->SUPER::new (
1874 ) 1853 )
1875} 1854}
1876 1855
1877############################################################################# 1856#############################################################################
1878 1857
1879package CFPlus::UI::VBox; 1858package DC::UI::VBox;
1880 1859
1881our @ISA = CFPlus::UI::Box::; 1860our @ISA = DC::UI::Box::;
1882 1861
1883sub new { 1862sub new {
1884 my $class = shift; 1863 my $class = shift;
1885 1864
1886 $class->SUPER::new ( 1865 $class->SUPER::new (
1889 ) 1868 )
1890} 1869}
1891 1870
1892############################################################################# 1871#############################################################################
1893 1872
1894package CFPlus::UI::Label; 1873package DC::UI::Label;
1895 1874
1896our @ISA = CFPlus::UI::DrawBG::; 1875our @ISA = DC::UI::DrawBG::;
1897 1876
1898use CFPlus::OpenGL; 1877use DC::OpenGL;
1899 1878
1900sub new { 1879sub new {
1901 my ($class, %arg) = @_; 1880 my ($class, %arg) = @_;
1902 1881
1903 my $self = $class->SUPER::new ( 1882 my $self = $class->SUPER::new (
1908 #text => initial text 1887 #text => initial text
1909 #markup => initial narkup 1888 #markup => initial narkup
1910 #max_w => maximum pixel width 1889 #max_w => maximum pixel width
1911 #style => 0, # render flags 1890 #style => 0, # render flags
1912 ellipsise => 3, # end 1891 ellipsise => 3, # end
1913 layout => (new CFPlus::Layout), 1892 layout => (new DC::Layout),
1914 fontsize => 1, 1893 fontsize => 1,
1915 align => -1, 1894 align => -1,
1916 valign => -1, 1895 valign => -1,
1917 padding_x => 2, 1896 padding_x => 2,
1918 padding_y => 2, 1897 padding_y => 2,
1919 can_events => 0, 1898 can_events => 0,
1920 %arg 1899 %arg
1921 ); 1900 );
1922 1901
1923 if (exists $self->{template}) { 1902 if (exists $self->{template}) {
1924 my $layout = new CFPlus::Layout; 1903 my $layout = new DC::Layout;
1925 $layout->set_text (delete $self->{template}); 1904 $layout->set_text (delete $self->{template});
1926 $self->{template} = $layout; 1905 $self->{template} = $layout;
1927 } 1906 }
1928 1907
1929 if (exists $self->{markup}) { 1908 if (exists $self->{markup}) {
1947 1926
1948 delete $self->{ox}; 1927 delete $self->{ox};
1949 $self->SUPER::realloc; 1928 $self->SUPER::realloc;
1950} 1929}
1951 1930
1931sub clear {
1932 my ($self) = @_;
1933
1934 $self->set_text ("");
1935}
1936
1952sub set_text { 1937sub set_text {
1953 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1954 1939
1955 return if $self->{text} eq "T$text"; 1940 return if $self->{text} eq "T$text";
1956 $self->{text} = "T$text"; 1941 $self->{text} = "T$text";
1979 1964
1980sub size_request { 1965sub size_request {
1981 my ($self) = @_; 1966 my ($self) = @_;
1982 1967
1983 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1984 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1985 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1986 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1989 1976
1990 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
2062 : ($self->{w} - $size->[0]) * 0.5); 2049 : ($self->{w} - $size->[0]) * 0.5);
2063 2050
2064 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2065 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2066 : ($self->{h} - $size->[1]) * 0.5); 2053 : ($self->{h} - $size->[1]) * 0.5);
2054
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2067 }; 2056 };
2068 2057
2069 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2058# unless ($self->{list}) {
2070 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2059# $self->{list} = DC::OpenGL::glGenList;
2071 2060# DC::OpenGL::glNewList $self->{list};
2072 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062# DC::OpenGL::glEndList;
2063# }
2064#
2065# DC::OpenGL::glCallList $self->{list};
2066
2067 $self->{layout}->draw;
2073} 2068}
2069
2070#sub destroy {
2071# my ($self) = @_;
2072#
2073# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2074#
2075# $self->SUPER::destroy;
2076#}
2074 2077
2075############################################################################# 2078#############################################################################
2076 2079
2077package CFPlus::UI::EntryBase; 2080package DC::UI::EntryBase;
2078 2081
2079our @ISA = CFPlus::UI::Label::; 2082our @ISA = DC::UI::Label::;
2080 2083
2081use CFPlus::OpenGL; 2084use DC::OpenGL;
2082 2085
2083sub new { 2086sub new {
2084 my $class = shift; 2087 my $class = shift;
2085 2088
2086 $class->SUPER::new ( 2089 $class->SUPER::new (
2087 fg => [1, 1, 1], 2090 fg => [1, 1, 1],
2088 bg => [0, 0, 0, 0.2], 2091 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1],
2089 active_bg => [1, 1, 1, 0.5], 2093 active_bg => [0, 0, 1, .2],
2090 active_fg => [0, 0, 0], 2094 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0],
2091 can_hover => 1, 2096 can_hover => 1,
2092 can_focus => 1, 2097 can_focus => 1,
2093 valign => 0, 2098 valign => 0,
2094 can_events => 1, 2099 can_events => 1,
2095 ellipsise => 0, 2100 ellipsise => 0,
2101 padding_x => 4,
2102 padding_y => 2,
2096 #text => ... 2103 #text => ...
2097 #hidden => "*", 2104 #hidden => "*",
2098 @_ 2105 @_
2099 ) 2106 )
2100} 2107}
2151 2158
2152 if ($uni == 8) { 2159 if ($uni == 8) {
2153 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2160 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2154 } elsif ($uni == 127) { 2161 } elsif ($uni == 127) {
2155 substr $text, $self->{cursor}, 1, ""; 2162 substr $text, $self->{cursor}, 1, "";
2156 } elsif ($sym == CFPlus::SDLK_LEFT) { 2163 } elsif ($sym == DC::SDLK_LEFT) {
2157 --$self->{cursor} if $self->{cursor}; 2164 --$self->{cursor} if $self->{cursor};
2158 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2165 } elsif ($sym == DC::SDLK_RIGHT) {
2159 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2166 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2160 } elsif ($sym == CFPlus::SDLK_HOME) { 2167 } elsif ($sym == DC::SDLK_HOME) {
2161 # what a hack 2168 # what a hack
2162 $self->{cursor} = 2169 $self->{cursor} =
2163 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2170 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2164 ? length $1 2171 ? length $1
2165 : 0; 2172 : 0;
2166 } elsif ($sym == CFPlus::SDLK_END) { 2173 } elsif ($sym == DC::SDLK_END) {
2167 # uh, again 2174 # uh, again
2168 $self->{cursor} = 2175 $self->{cursor} =
2169 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2176 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2170 ? $self->{cursor} + length $1 2177 ? $self->{cursor} + length $1
2171 : length $self->{text}; 2178 : length $self->{text};
2235 glColor_premultiply @{$self->{bg}}; 2242 glColor_premultiply @{$self->{bg}};
2236 } 2243 }
2237 2244
2238 glEnable GL_BLEND; 2245 glEnable GL_BLEND;
2239 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2246 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2240 glBegin GL_QUADS;
2241 glVertex 0 , 0;
2242 glVertex 0 , $self->{h};
2243 glVertex $self->{w}, $self->{h}; 2247 glRect 0, 0, $self->{w}, $self->{h};
2244 glVertex $self->{w}, 0;
2245 glEnd;
2246 glDisable GL_BLEND; 2248 glDisable GL_BLEND;
2247 2249
2248 $self->SUPER::_draw; 2250 $self->SUPER::_draw;
2249 2251
2250 #TODO: force update every cursor change :( 2252 #TODO: force update every cursor change :(
2252 2254
2253 unless (exists $self->{cur_h}) { 2255 unless (exists $self->{cur_h}) {
2254 my $text = substr $self->{text}, 0, $self->{cursor}; 2256 my $text = substr $self->{text}, 0, $self->{cursor};
2255 utf8::encode $text; 2257 utf8::encode $text;
2256 2258
2257 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2259 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2258 } 2260 }
2259 2261
2262 glColor_premultiply @{$self->{active_fg}};
2260 glBegin GL_LINES; 2263 glBegin GL_LINES;
2261 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2264 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2262 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2265 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2263 glEnd; 2266 glEnd;
2267
2268 glLineWidth 3;
2269 glColor @{$self->{active_outline}};
2270 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2271 glLineWidth 1;
2272
2273 } else {
2274 glColor @{$self->{outline}};
2275 glBegin GL_LINE_STRIP;
2276 glVertex .5, $self->{h} * .5;
2277 glVertex .5, $self->{h} - 2.5;
2278 glVertex $self->{w} - .5, $self->{h} - 2.5;
2279 glVertex $self->{w} - .5, $self->{h} * .5;
2280 glEnd;
2264 } 2281 }
2265} 2282}
2266 2283
2267############################################################################# 2284#############################################################################
2268 2285
2269package CFPlus::UI::Entry; 2286package DC::UI::Entry;
2270 2287
2271our @ISA = CFPlus::UI::EntryBase::; 2288our @ISA = DC::UI::EntryBase::;
2272 2289
2273use CFPlus::OpenGL; 2290use DC::OpenGL;
2274 2291
2275sub invoke_key_down { 2292sub invoke_key_down {
2276 my ($self, $ev) = @_; 2293 my ($self, $ev) = @_;
2277 2294
2278 my $sym = $ev->{sym}; 2295 my $sym = $ev->{sym};
2284 $self->{history_pointer} = -1; 2301 $self->{history_pointer} = -1;
2285 $self->{history_saveback} = ''; 2302 $self->{history_saveback} = '';
2286 $self->emit (activate => $txt); 2303 $self->emit (activate => $txt);
2287 $self->update; 2304 $self->update;
2288 2305
2289 } elsif ($sym == CFPlus::SDLK_UP) { 2306 } elsif ($sym == DC::SDLK_UP) {
2290 if ($self->{history_pointer} < 0) { 2307 if ($self->{history_pointer} < 0) {
2291 $self->{history_saveback} = $self->get_text; 2308 $self->{history_saveback} = $self->get_text;
2292 } 2309 }
2293 if (@{$self->{history} || []} > 0) { 2310 if (@{$self->{history} || []} > 0) {
2294 $self->{history_pointer}++; 2311 $self->{history_pointer}++;
2296 $self->{history_pointer} = @{$self->{history} || []} - 1; 2313 $self->{history_pointer} = @{$self->{history} || []} - 1;
2297 } 2314 }
2298 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2315 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2299 } 2316 }
2300 2317
2301 } elsif ($sym == CFPlus::SDLK_DOWN) { 2318 } elsif ($sym == DC::SDLK_DOWN) {
2302 $self->{history_pointer}--; 2319 $self->{history_pointer}--;
2303 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2320 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2304 2321
2305 if ($self->{history_pointer} >= 0) { 2322 if ($self->{history_pointer} >= 0) {
2306 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2323 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2315 1 2332 1
2316} 2333}
2317 2334
2318############################################################################# 2335#############################################################################
2319 2336
2320package CFPlus::UI::TextEdit; 2337package DC::UI::TextEdit;
2321 2338
2322our @ISA = CFPlus::UI::EntryBase::; 2339our @ISA = DC::UI::EntryBase::;
2323 2340
2324use CFPlus::OpenGL; 2341use DC::OpenGL;
2342
2343sub new {
2344 my $class = shift;
2345
2346 $class->SUPER::new (
2347 padding_y => 4,
2348
2349 @_
2350 )
2351}
2325 2352
2326sub move_cursor_ver { 2353sub move_cursor_ver {
2327 my ($self, $dy) = @_; 2354 my ($self, $dy) = @_;
2328 2355
2329 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2356 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2357 warn "cursor $self->{cursor} => $x $line\n";#d#
2330 2358
2331 $y += $dy; 2359 $line += $dy;
2332 2360
2333 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2361 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2362 warn "index $x $line => $index\n";#d#
2334 $self->{cursor} = $index; 2363 $self->{cursor} = $index;
2335 delete $self->{cur_h}; 2364 delete $self->{cur_h};
2336 $self->update; 2365 $self->update;
2337 return; 2366 return;
2338 } 2367 }
2341sub invoke_key_down { 2370sub invoke_key_down {
2342 my ($self, $ev) = @_; 2371 my ($self, $ev) = @_;
2343 2372
2344 my $sym = $ev->{sym}; 2373 my $sym = $ev->{sym};
2345 2374
2346 if ($sym == CFPlus::SDLK_UP) { 2375 if ($sym == DC::SDLK_UP) {
2347 $self->move_cursor_ver (-1); 2376 $self->move_cursor_ver (-1);
2348 } elsif ($sym == CFPlus::SDLK_DOWN) { 2377 } elsif ($sym == DC::SDLK_DOWN) {
2349 $self->move_cursor_ver (+1); 2378 $self->move_cursor_ver (+1);
2350 } else { 2379 } else {
2351 return $self->SUPER::invoke_key_down ($ev) 2380 return $self->SUPER::invoke_key_down ($ev)
2352 } 2381 }
2353 2382
2354 1 2383 1
2355} 2384}
2356 2385
2357############################################################################# 2386#############################################################################
2358 2387
2359package CFPlus::UI::Button; 2388package DC::UI::ButtonBin;
2360 2389
2361our @ISA = CFPlus::UI::Label::; 2390our @ISA = DC::UI::Bin::;
2362 2391
2363use CFPlus::OpenGL; 2392use DC::OpenGL;
2364 2393
2365my @tex = 2394my @tex =
2366 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2395 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2367 qw(b1_button_inactive.png b1_button_active.png); 2396 qw(b1_button_inactive.png b1_button_active.png);
2368 2397
2369sub new { 2398sub new {
2370 my $class = shift; 2399 my $class = shift;
2371 2400
2372 $class->SUPER::new ( 2401 $class->SUPER::new (
2402 can_hover => 1,
2403 align => 0,
2404 valign => 0,
2405 can_events => 1,
2406 @_
2407 )
2408}
2409
2410sub invoke_button_up {
2411 my ($self, $ev, $x, $y) = @_;
2412
2413 $self->emit ("activate")
2414 if $x >= 0 && $x < $self->{w}
2415 && $y >= 0 && $y < $self->{h};
2416
2417 1
2418}
2419
2420sub _draw {
2421 my ($self) = @_;
2422
2423 glEnable GL_TEXTURE_2D;
2424 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2425 glColor 0, 0, 0, 1;
2426
2427 my $tex = $tex[$GRAB == $self];
2428 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2429
2430 glDisable GL_TEXTURE_2D;
2431
2432 $self->SUPER::_draw;
2433}
2434
2435#############################################################################
2436
2437package DC::UI::Button;
2438
2439our @ISA = DC::UI::Label::;
2440
2441use DC::OpenGL;
2442
2443my @tex =
2444 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2445 qw(b1_button_inactive.png b1_button_active.png);
2446
2447sub new {
2448 my $class = shift;
2449
2450 $class->SUPER::new (
2373 padding_x => 4, 2451 padding_x => 8,
2374 padding_y => 4, 2452 padding_y => 4,
2375 fg => [1.0, 1.0, 1.0], 2453 fg => [1.0, 1.0, 1.0],
2376 active_fg => [0.8, 0.8, 0.8], 2454 active_fg => [0.8, 0.8, 0.8],
2377 can_hover => 1, 2455 can_hover => 1,
2378 align => 0, 2456 align => 0,
2409 $self->SUPER::_draw; 2487 $self->SUPER::_draw;
2410} 2488}
2411 2489
2412############################################################################# 2490#############################################################################
2413 2491
2414package CFPlus::UI::CheckBox; 2492package DC::UI::CheckBox;
2415 2493
2416our @ISA = CFPlus::UI::DrawBG::; 2494our @ISA = DC::UI::DrawBG::;
2417 2495
2418my @tex = 2496my @tex =
2419 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2497 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2420 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2498 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2421 2499
2422use CFPlus::OpenGL; 2500use DC::OpenGL;
2423 2501
2424sub new { 2502sub new {
2425 my $class = shift; 2503 my $class = shift;
2426 2504
2427 $class->SUPER::new ( 2505 $class->SUPER::new (
2467sub _draw { 2545sub _draw {
2468 my ($self) = @_; 2546 my ($self) = @_;
2469 2547
2470 $self->SUPER::_draw; 2548 $self->SUPER::_draw;
2471 2549
2472 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2550 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2473 2551
2474 my ($w, $h) = @$self{qw(w h)}; 2552 my ($w, $h) = @$self{qw(w h)};
2475 2553
2476 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2554 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2477 2555
2484 glDisable GL_TEXTURE_2D; 2562 glDisable GL_TEXTURE_2D;
2485} 2563}
2486 2564
2487############################################################################# 2565#############################################################################
2488 2566
2489package CFPlus::UI::Image; 2567package DC::UI::Image;
2490 2568
2491our @ISA = CFPlus::UI::Base::; 2569our @ISA = DC::UI::Base::;
2492 2570
2493use CFPlus::OpenGL; 2571use DC::OpenGL;
2494 2572
2495our %texture_cache; 2573our %texture_cache;
2496 2574
2497sub new { 2575sub new {
2498 my $class = shift; 2576 my $class = shift;
2499 2577
2500 my $self = $class->SUPER::new ( 2578 my $self = $class->SUPER::new (
2501 can_events => 0, 2579 can_events => 0,
2580 scale => 1,
2502 @_, 2581 @_,
2503 ); 2582 );
2504 2583
2505 $self->{path} || $self->{tex} 2584 $self->{path} || $self->{tex}
2506 or Carp::croak "'path' or 'tex' attributes required"; 2585 or Carp::croak "'path' or 'tex' attributes required";
2507 2586
2508 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2587 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2509 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2588 new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1;
2510 2589
2511 CFPlus::weaken $texture_cache{$self->{path}}; 2590 DC::weaken $texture_cache{$self->{path}};
2512 2591
2513 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2592 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2514 2593
2515 $self 2594 $self
2516} 2595}
2517 2596
2518sub STORABLE_freeze { 2597sub STORABLE_freeze {
2519 my ($self, $cloning) = @_; 2598 my ($self, $cloning) = @_;
2520 2599
2521 $self->{path} 2600 $self->{path}
2522 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2601 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2523 2602
2524 $self->{path} 2603 $self->{path}
2525} 2604}
2526 2605
2527sub STORABLE_attach { 2606sub STORABLE_attach {
2531} 2610}
2532 2611
2533sub size_request { 2612sub size_request {
2534 my ($self) = @_; 2613 my ($self) = @_;
2535 2614
2536 ($self->{tex}{w}, $self->{tex}{h}) 2615 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2537} 2616}
2538 2617
2539sub _draw { 2618sub _draw {
2540 my ($self) = @_; 2619 my ($self) = @_;
2541 2620
2551 } 2630 }
2552 2631
2553 glEnable GL_TEXTURE_2D; 2632 glEnable GL_TEXTURE_2D;
2554 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2633 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2555 2634
2556 $tex->draw_quad (0, 0, $w, $h); 2635 $tex->draw_quad_alpha (0, 0, $w, $h);
2557 2636
2558 glDisable GL_TEXTURE_2D; 2637 glDisable GL_TEXTURE_2D;
2559} 2638}
2560 2639
2561############################################################################# 2640#############################################################################
2562 2641
2563package CFPlus::UI::ImageButton; 2642package DC::UI::ImageButton;
2564 2643
2565our @ISA = CFPlus::UI::Image::; 2644our @ISA = DC::UI::Image::;
2566 2645
2567use CFPlus::OpenGL; 2646use DC::OpenGL;
2568 2647
2569my %textures; 2648my %textures;
2570 2649
2571sub new { 2650sub new {
2572 my $class = shift; 2651 my $class = shift;
2582 can_events => 1, 2661 can_events => 1,
2583 @_ 2662 @_
2584 ); 2663 );
2585} 2664}
2586 2665
2666sub invoke_button_down {
2667 my ($self, $ev, $x, $y) = @_;
2668
2669 1
2670}
2671
2587sub invoke_button_up { 2672sub invoke_button_up {
2588 my ($self, $ev, $x, $y) = @_; 2673 my ($self, $ev, $x, $y) = @_;
2589 2674
2590 $self->emit ("activate") 2675 $self->emit ("activate")
2591 if $x >= 0 && $x < $self->{w} 2676 if $x >= 0 && $x < $self->{w}
2594 1 2679 1
2595} 2680}
2596 2681
2597############################################################################# 2682#############################################################################
2598 2683
2599package CFPlus::UI::VGauge; 2684package DC::UI::VGauge;
2600 2685
2601our @ISA = CFPlus::UI::Base::; 2686our @ISA = DC::UI::Base::;
2602 2687
2603use List::Util qw(min max); 2688use List::Util qw(min max);
2604 2689
2605use CFPlus::OpenGL; 2690use DC::OpenGL;
2606 2691
2607my %tex = ( 2692my %tex = (
2608 food => [ 2693 food => [
2609 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2694 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2610 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2695 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2611 ], 2696 ],
2612 grace => [ 2697 grace => [
2613 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2698 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2614 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2699 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2615 ], 2700 ],
2616 hp => [ 2701 hp => [
2617 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2702 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2618 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2703 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2619 ], 2704 ],
2620 mana => [ 2705 mana => [
2621 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2706 map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 }
2622 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2707 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2623 ], 2708 ],
2624); 2709);
2625 2710
2626# eg. VGauge->new (gauge => 'food'), default gauge: food 2711# eg. VGauge->new (gauge => 'food'), default gauge: food
2729 glDisable GL_TEXTURE_2D; 2814 glDisable GL_TEXTURE_2D;
2730} 2815}
2731 2816
2732############################################################################# 2817#############################################################################
2733 2818
2819package DC::UI::Progress;
2820
2821our @ISA = DC::UI::Label::;
2822
2823use DC::OpenGL;
2824
2825sub new {
2826 my ($class, %arg) = @_;
2827
2828 my $self = $class->SUPER::new (
2829 fg => [1, 1, 1],
2830 bg => [0, 0, 1, 0.2],
2831 bar => [0.7, 0.5, 0.1, 0.8],
2832 outline => [0.4, 0.3, 0],
2833 fontsize => 0.9,
2834 valign => 0,
2835 align => 0,
2836 can_events => 1,
2837 ellipsise => 1,
2838 label => "%d%%",
2839 %arg,
2840 );
2841
2842 $self->set_value ($arg{value} || -1);
2843
2844 $self
2845}
2846
2847sub set_label {
2848 my ($self, $label) = @_;
2849
2850 return if $self->{label} eq $label;
2851 $self->{label} = $label;
2852
2853 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2854}
2855
2856sub set_value {
2857 my ($self, $value) = @_;
2858
2859 if ($self->{value} ne $value) {
2860 $self->{value} = $value;
2861
2862 if ($value < 0) {
2863 $self->set_text ("-");
2864 } else {
2865 $self->set_text (sprintf $self->{label}, $value * 100);
2866 }
2867
2868 $self->update;
2869 }
2870}
2871
2872sub _draw {
2873 my ($self) = @_;
2874
2875 glEnable GL_BLEND;
2876 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2877
2878 if ($self->{value} >= 0) {
2879 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2880
2881 glColor_premultiply @{$self->{bar}};
2882 glRect 2, 2, $s, $self->{h} - 2;
2883 glColor_premultiply @{$self->{bg}};
2884 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2885 }
2886
2887 glColor_premultiply @{$self->{outline}};
2888 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2889
2890 glDisable GL_BLEND;
2891
2892 {
2893 local $self->{bg}; # do not draw background
2894 $self->SUPER::_draw;
2895 }
2896}
2897
2898#############################################################################
2899
2900package DC::UI::ExperienceProgress;
2901
2902our @ISA = DC::UI::Progress::;
2903
2904sub new {
2905 my ($class, %arg) = @_;
2906
2907 my $self = $class->SUPER::new (
2908 tooltip => sub {
2909 my ($self) = @_;
2910
2911 sprintf "level %d\n%s points\n%s next level\n%s to go",
2912 $self->{lvl},
2913 ::formsep ($self->{exp}),
2914 ::formsep ($self->{nxt}),
2915 ::formsep ($self->{nxt} - $self->{exp}),
2916 },
2917 %arg
2918 );
2919
2920 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2921 if $::CONN;
2922
2923 $self
2924}
2925
2926sub DESTROY {
2927 my ($self) = @_;
2928
2929 delete $::CONN->{on_exp_update}{$self+0}
2930 if $::CONN;
2931
2932 $self->SUPER::DESTROY;
2933}
2934
2935sub set_value {
2936 my ($self, $lvl, $exp) = @_;
2937
2938 $self->{lvl} = $lvl;
2939 $self->{exp} = $exp;
2940
2941 my $v = -1;
2942
2943 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2944 my $l0 = $table->[$lvl - 1];
2945 my $l1 = $table->[$lvl];
2946
2947 $self->{nxt} = $l1;
2948
2949 $v = ($exp - $l0) / ($l1 - $l0);
2950 }
2951
2952 $self->SUPER::set_value ($v);
2953}
2954
2955#############################################################################
2956
2734package CFPlus::UI::Gauge; 2957package DC::UI::Gauge;
2735 2958
2736our @ISA = CFPlus::UI::VBox::; 2959our @ISA = DC::UI::VBox::;
2737 2960
2738sub new { 2961sub new {
2739 my ($class, %arg) = @_; 2962 my ($class, %arg) = @_;
2740 2963
2741 my $self = $class->SUPER::new ( 2964 my $self = $class->SUPER::new (
2743 can_hover => 1, 2966 can_hover => 1,
2744 can_events => 1, 2967 can_events => 1,
2745 %arg, 2968 %arg,
2746 ); 2969 );
2747 2970
2748 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 2971 $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999");
2749 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2972 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2750 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 2973 $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999");
2751 2974
2752 $self 2975 $self
2753} 2976}
2754 2977
2755sub set_fontsize { 2978sub set_fontsize {
2776 $self->{value}->set_text ($val); 2999 $self->{value}->set_text ($val);
2777} 3000}
2778 3001
2779############################################################################# 3002#############################################################################
2780 3003
2781package CFPlus::UI::Slider; 3004package DC::UI::Slider;
2782 3005
2783use strict; 3006use strict;
2784 3007
2785use CFPlus::OpenGL; 3008use DC::OpenGL;
2786 3009
2787our @ISA = CFPlus::UI::DrawBG::; 3010our @ISA = DC::UI::DrawBG::;
2788 3011
2789my @tex = 3012my @tex =
2790 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3013 map { new_from_file DC::Texture DC::find_rcfile $_ }
2791 qw(s1_slider.png s1_slider_bg.png); 3014 qw(s1_slider.png s1_slider_bg.png);
2792 3015
2793sub new { 3016sub new {
2794 my $class = shift; 3017 my $class = shift;
2795 3018
2863 3086
2864 $self->SUPER::invoke_button_down ($ev, $x, $y); 3087 $self->SUPER::invoke_button_down ($ev, $x, $y);
2865 3088
2866 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3089 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2867 3090
2868 $self->invoke_mouse_motion ($ev, $x, $y) 3091 $self->invoke_mouse_motion ($ev, $x, $y);
3092
3093 1
2869} 3094}
2870 3095
2871sub invoke_mouse_motion { 3096sub invoke_mouse_motion {
2872 my ($self, $ev, $x, $y) = @_; 3097 my ($self, $ev, $x, $y) = @_;
2873 3098
2889sub invoke_mouse_wheel { 3114sub invoke_mouse_wheel {
2890 my ($self, $ev) = @_; 3115 my ($self, $ev) = @_;
2891 3116
2892 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3117 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2893 3118
2894 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3119 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
2895 3120
2896 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3121 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2897 3122
2898 ! ! $delta 3123 1
2899} 3124}
2900 3125
2901sub update { 3126sub update {
2902 my ($self) = @_; 3127 my ($self) = @_;
2903 3128
2952 glDisable GL_TEXTURE_2D; 3177 glDisable GL_TEXTURE_2D;
2953} 3178}
2954 3179
2955############################################################################# 3180#############################################################################
2956 3181
2957package CFPlus::UI::ValSlider; 3182package DC::UI::ValSlider;
2958 3183
2959our @ISA = CFPlus::UI::HBox::; 3184our @ISA = DC::UI::HBox::;
2960 3185
2961sub new { 3186sub new {
2962 my ($class, %arg) = @_; 3187 my ($class, %arg) = @_;
2963 3188
2964 my $range = delete $arg{range}; 3189 my $range = delete $arg{range};
2965 3190
2966 my $self = $class->SUPER::new ( 3191 my $self = $class->SUPER::new (
2967 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3192 slider => (new DC::UI::Slider expand => 1, range => $range),
2968 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3193 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
2969 to_value => sub { shift }, 3194 to_value => sub { shift },
2970 from_value => sub { shift }, 3195 from_value => sub { shift },
2971 %arg, 3196 %arg,
2972 ); 3197 );
2973 3198
2993sub set_range { shift->{slider}->set_range (@_) } 3218sub set_range { shift->{slider}->set_range (@_) }
2994sub set_value { shift->{slider}->set_value (@_) } 3219sub set_value { shift->{slider}->set_value (@_) }
2995 3220
2996############################################################################# 3221#############################################################################
2997 3222
2998package CFPlus::UI::TextScroller; 3223package DC::UI::TextScroller;
2999 3224
3000our @ISA = CFPlus::UI::HBox::; 3225our @ISA = DC::UI::HBox::;
3001 3226
3002use CFPlus::OpenGL; 3227use DC::OpenGL;
3003 3228
3004sub new { 3229sub new {
3005 my $class = shift; 3230 my $class = shift;
3006 3231
3007 my $self = $class->SUPER::new ( 3232 my $self = $class->SUPER::new (
3009 can_events => 1, 3234 can_events => 1,
3010 indent => 0, 3235 indent => 0,
3011 #font => default_font 3236 #font => default_font
3012 @_, 3237 @_,
3013 3238
3014 layout => (new CFPlus::Layout), 3239 layout => (new DC::Layout),
3015 par => [], 3240 par => [],
3016 max_par => 0, 3241 max_par => 0,
3017 height => 0, 3242 height => 0,
3018 children => [ 3243 children => [
3019 (new CFPlus::UI::Empty expand => 1), 3244 (new DC::UI::Empty expand => 1),
3020 (new CFPlus::UI::Slider vertical => 1), 3245 (new DC::UI::Slider vertical => 1),
3021 ], 3246 ],
3022 ); 3247 );
3023 3248
3024 $self->{children}[1]->connect (changed => sub { $self->update }); 3249 $self->{children}[1]->connect (changed => sub { $self->update });
3025 3250
3034} 3259}
3035 3260
3036sub size_request { 3261sub size_request {
3037 my ($self) = @_; 3262 my ($self) = @_;
3038 3263
3039 my ($empty, $slider) = @{ $self->{children} }; 3264 my ($empty, $slider) = $self->visible_children;
3040 3265
3041 local $self->{children} = [$empty, $slider]; 3266 local $self->{children} = [$empty, $slider];
3042 $self->SUPER::size_request 3267 $self->SUPER::size_request
3043} 3268}
3044 3269
3208 $ROOT->on_post_alloc ($self => sub { 3433 $ROOT->on_post_alloc ($self => sub {
3209 $self->force_uptodate; 3434 $self->force_uptodate;
3210 3435
3211 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3436 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3212 3437
3213 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3438 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3214 glClearColor 0, 0, 0, 0; 3439 glClearColor 0, 0, 0, 0;
3215 glClear GL_COLOR_BUFFER_BIT; 3440 glClear GL_COLOR_BUFFER_BIT;
3216 3441
3217 package CFPlus::UI::Base; 3442 package DC::UI::Base;
3218 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3443 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3219 (0, 0, $self->{w}, $self->{h}); 3444 (0, 0, $self->{w}, $self->{h});
3220 3445
3221 my $top = int $self->{children}[1]{range}[0]; 3446 my $top = int $self->{children}[1]{range}[0];
3222 3447
3233 3458
3234 if ($y0 < $y + $h && $y < $y1) { 3459 if ($y0 < $y + $h && $y < $y1) {
3235 my $layout = $self->get_layout ($para); 3460 my $layout = $self->get_layout ($para);
3236 3461
3237 $layout->render ($para->{indent}, $y - $y0); 3462 $layout->render ($para->{indent}, $y - $y0);
3463 $layout->draw;
3238 3464
3239 if (my @w = @{ $para->{widget} }) { 3465 if (my @w = @{ $para->{widget} }) {
3240 my @s = $layout->get_shapes; 3466 my @s = $layout->get_shapes;
3241 3467
3242 for (@w) { 3468 for (@w) {
3280 $self->{children}[1]->draw; 3506 $self->{children}[1]->draw;
3281} 3507}
3282 3508
3283############################################################################# 3509#############################################################################
3284 3510
3285package CFPlus::UI::Animator; 3511package DC::UI::Animator;
3286 3512
3287use CFPlus::OpenGL; 3513use DC::OpenGL;
3288 3514
3289our @ISA = CFPlus::UI::Bin::; 3515our @ISA = DC::UI::Bin::;
3290 3516
3291sub moveto { 3517sub moveto {
3292 my ($self, $x, $y) = @_; 3518 my ($self, $x, $y) = @_;
3293 3519
3294 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3520 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3322 glPopMatrix; 3548 glPopMatrix;
3323} 3549}
3324 3550
3325############################################################################# 3551#############################################################################
3326 3552
3327package CFPlus::UI::Flopper; 3553package DC::UI::Flopper;
3328 3554
3329our @ISA = CFPlus::UI::Button::; 3555our @ISA = DC::UI::Button::;
3330 3556
3331sub new { 3557sub new {
3332 my $class = shift; 3558 my $class = shift;
3333 3559
3334 my $self = $class->SUPER::new ( 3560 my $self = $class->SUPER::new (
3346 $self->{other}->toggle_visibility; 3572 $self->{other}->toggle_visibility;
3347} 3573}
3348 3574
3349############################################################################# 3575#############################################################################
3350 3576
3351package CFPlus::UI::Tooltip; 3577package DC::UI::Tooltip;
3352 3578
3353our @ISA = CFPlus::UI::Bin::; 3579our @ISA = DC::UI::Bin::;
3354 3580
3355use CFPlus::OpenGL; 3581use DC::OpenGL;
3356 3582
3357sub new { 3583sub new {
3358 my $class = shift; 3584 my $class = shift;
3359 3585
3360 $class->SUPER::new ( 3586 $class->SUPER::new (
3364} 3590}
3365 3591
3366sub set_tooltip_from { 3592sub set_tooltip_from {
3367 my ($self, $widget) = @_; 3593 my ($self, $widget) = @_;
3368 3594
3369 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3370 if $widget->{tooltip} =~ /^#(.*)$/;
3371
3372 my $tooltip = $widget->{tooltip}; 3595 my $tip = $widget->{tooltip};
3596 $tip = $tip->($widget) if "CODE" eq ref $tip;
3597
3598 $tip = DC::Pod::section_label tooltip => $1
3599 if $tip =~ /^#(.*)$/;
3373 3600
3374 if ($ENV{CFPLUS_DEBUG} & 2) { 3601 if ($ENV{CFPLUS_DEBUG} & 2) {
3375 $tooltip .= "\n\n" . (ref $widget) . "\n" 3602 $tip .= "\n\n" . (ref $widget) . "\n"
3376 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3603 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3377 . "req $widget->{req_w} $widget->{req_h}\n" 3604 . "req $widget->{req_w} $widget->{req_h}\n"
3378 . "visible $widget->{visible}"; 3605 . "visible $widget->{visible}";
3379 } 3606 }
3380 3607
3381 $tooltip =~ s/^\n+//; 3608 $tip =~ s/^\n+//;
3382 $tooltip =~ s/\n+$//; 3609 $tip =~ s/\n+$//;
3383 3610
3384 $self->add (new CFPlus::UI::Label 3611 $self->add (new DC::UI::Label
3385 markup => $tooltip, 3612 markup => $tip,
3386 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3613 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3387 fontsize => 0.8, 3614 fontsize => 0.8,
3388 style => 1, # FLAG_INVERSE 3615 style => 1, # FLAG_INVERSE
3389 ellipsise => 0, 3616 ellipsise => 0,
3390 font => ($widget->{tooltip_font} || $::FONT_PROP), 3617 font => ($widget->{tooltip_font} || $::FONT_PROP),
3428} 3655}
3429 3656
3430sub _draw { 3657sub _draw {
3431 my ($self) = @_; 3658 my ($self) = @_;
3432 3659
3433 glTranslate 0.375, 0.375;
3434
3435 my ($w, $h) = @$self{qw(w h)}; 3660 my ($w, $h) = @$self{qw(w h)};
3436 3661
3437 glColor 1, 0.8, 0.4; 3662 glColor 1, 0.8, 0.4;
3438 glBegin GL_QUADS; 3663 glRect 0, 0, $w, $h;
3439 glVertex 0 , 0;
3440 glVertex 0 , $h;
3441 glVertex $w, $h;
3442 glVertex $w, 0;
3443 glEnd;
3444 3664
3445 glColor 0, 0, 0; 3665 glColor 0, 0, 0;
3446 glBegin GL_LINE_LOOP; 3666 glRect_lineloop .5, .5, $w + .5, $h + .5;
3447 glVertex 0 , 0;
3448 glVertex 0 , $h;
3449 glVertex $w, $h;
3450 glVertex $w, 0;
3451 glEnd;
3452 3667
3453 glTranslate 2 - 0.375, 2 - 0.375; 3668 glTranslate 2, 2;
3454 3669
3455 $self->SUPER::_draw; 3670 $self->SUPER::_draw;
3456} 3671}
3457 3672
3458############################################################################# 3673#############################################################################
3459 3674
3460package CFPlus::UI::Face; 3675package DC::UI::Face;
3461 3676
3462our @ISA = CFPlus::UI::DrawBG::; 3677our @ISA = DC::UI::DrawBG::;
3463 3678
3464use CFPlus::OpenGL; 3679use DC::OpenGL;
3465 3680
3466sub new { 3681sub new {
3467 my $class = shift; 3682 my $class = shift;
3468 3683
3469 my $self = $class->SUPER::new ( 3684 my $self = $class->SUPER::new (
3473 can_events => 0, 3688 can_events => 0,
3474 @_, 3689 @_,
3475 ); 3690 );
3476 3691
3477 if ($self->{anim} && $self->{animspeed}) { 3692 if ($self->{anim} && $self->{animspeed}) {
3478 CFPlus::weaken (my $widget = $self); 3693 DC::weaken (my $widget = $self);
3479 3694
3480 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3695 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3481 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; 3696 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3482 $self->{timer} = Event->timer (
3483 parked => 1,
3484 cb => sub {
3485 return unless $::CONN && $widget; 3697 return unless $::CONN;
3486 3698
3699 my $w = $widget
3700 or return;
3701
3487 ++$widget->{frame}; 3702 ++$w->{frame};
3488 $widget->update_face; 3703 $w->update_face;
3704
3705 # somehow, $widget can go away
3489 $widget->update; 3706 $w->update;
3490
3491 $widget->update_timer; 3707 $w->update_timer;
3492 },
3493 ); 3708 };
3494 3709
3495 $self->update_face; 3710 $self->update_face;
3496 $self->update_timer; 3711 $self->update_timer;
3497 } 3712 }
3498 3713
3503 my ($self) = @_; 3718 my ($self) = @_;
3504 3719
3505 return unless $self->{timer}; 3720 return unless $self->{timer};
3506 3721
3507 if ($self->{visible}) { 3722 if ($self->{visible}) {
3508 $self->{timer}->at (
3509 $self->{anim_start}
3510 + $self->{animspeed}
3511 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3512 );
3513 $self->{timer}->start; 3723 $self->{timer}->start;
3514 } else { 3724 } else {
3515 $self->{timer}->stop; 3725 $self->{timer}->stop;
3516 } 3726 }
3517} 3727}
3518 3728
3519sub update_face { 3729sub update_face {
3520 my ($self) = @_; 3730 my ($self) = @_;
3521 3731
3522 return unless $::CONN;
3523
3524 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3525 if ($anim && @$anim) {
3526 delete $self->{wait_face};
3527 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3528 }
3529 }
3530}
3531
3532sub size_request {
3533 my ($self) = @_;
3534
3535 if ($::CONN) { 3732 if ($::CONN) {
3536 if (my $faceid = $::CONN->{faceid}[$self->{face}]) { 3733 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3537 if (my $tex = $::CONN->{texture}[$faceid]) { 3734 if ($anim && @$anim) {
3538 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); 3735 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3539 } else { 3736 delete $self->{face_change_cb};
3540 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { 3737
3541 $self->realloc; 3738 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3739 unless ($tex->{name} || $tex->{loading}) {
3740 $tex->upload (sub { $self->reconfigure });
3741 }
3542 }); 3742 }
3543 } 3743 }
3544 } 3744 }
3545 } 3745 }
3746}
3747
3748sub size_request {
3749 my ($self) = @_;
3750
3751 if ($::CONN) {
3752 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3753 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3754 if ($tex->{name}) {
3755 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3756 } elsif (!$tex->{loading}) {
3757 $tex->upload (sub { $self->reconfigure });
3758 }
3759 }
3760
3761 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3762 }
3763 }
3546 3764
3547 ($self->{size_w} || 8, $self->{size_h} || 8) 3765 ($self->{size_w} || 8, $self->{size_h} || 8)
3548} 3766}
3549 3767
3550sub update { 3768sub update {
3564} 3782}
3565 3783
3566sub _draw { 3784sub _draw {
3567 my ($self) = @_; 3785 my ($self) = @_;
3568 3786
3569 return unless $::CONN;
3570
3571 $self->SUPER::_draw; 3787 $self->SUPER::_draw;
3572 3788
3573 my $faceid = $::CONN->{faceid}[$self->{face}] 3789 if (my $tex = $self->{tex}) {
3574 or return;
3575
3576 my $tex = $::CONN->{texture}[$faceid];
3577
3578 if ($tex) {
3579 glEnable GL_TEXTURE_2D; 3790 glEnable GL_TEXTURE_2D;
3580 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3791 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3581 glColor 0, 0, 0, 1; 3792 glColor 0, 0, 0, 1;
3582 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3793 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3583 glDisable GL_TEXTURE_2D; 3794 glDisable GL_TEXTURE_2D;
3593 $self->SUPER::destroy; 3804 $self->SUPER::destroy;
3594} 3805}
3595 3806
3596############################################################################# 3807#############################################################################
3597 3808
3598package CFPlus::UI::Buttonbar; 3809package DC::UI::Buttonbar;
3599 3810
3600our @ISA = CFPlus::UI::HBox::; 3811our @ISA = DC::UI::HBox::;
3601 3812
3602# TODO: should actually wrap buttons and other goodies. 3813# TODO: should actually wrap buttons and other goodies.
3603 3814
3604############################################################################# 3815#############################################################################
3605 3816
3606package CFPlus::UI::Menu; 3817package DC::UI::Menu;
3607 3818
3608our @ISA = CFPlus::UI::Toplevel::; 3819our @ISA = DC::UI::Toplevel::;
3609 3820
3610use CFPlus::OpenGL; 3821use DC::OpenGL;
3611 3822
3612sub new { 3823sub new {
3613 my $class = shift; 3824 my $class = shift;
3614 3825
3615 my $self = $class->SUPER::new ( 3826 my $self = $class->SUPER::new (
3616 items => [], 3827 items => [],
3617 z => 100, 3828 z => 100,
3618 @_, 3829 @_,
3619 ); 3830 );
3620 3831
3621 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3832 $self->add ($self->{vbox} = new DC::UI::VBox);
3622 3833
3623 for my $item (@{ $self->{items} }) { 3834 for my $item (@{ $self->{items} }) {
3624 my ($widget, $cb, $tooltip) = @$item; 3835 my ($widget, $cb, $tooltip) = @$item;
3625 3836
3626 # handle various types of items, only text for now 3837 # handle various types of items, only text for now
3627 if (!ref $widget) { 3838 if (!ref $widget) {
3628 if ($widget =~ /\t/) { 3839 if ($widget =~ /\t/) {
3629 my ($left, $right) = split /\t/, $widget, 2; 3840 my ($left, $right) = split /\t/, $widget, 2;
3630 3841
3631 $widget = new CFPlus::UI::HBox 3842 $widget = new DC::UI::HBox
3632 can_hover => 1, 3843 can_hover => 1,
3633 can_events => 1, 3844 can_events => 1,
3634 tooltip => $tooltip, 3845 tooltip => $tooltip,
3635 children => [ 3846 children => [
3636 (new CFPlus::UI::Label markup => $left, expand => 1), 3847 (new DC::UI::Label markup => $left, expand => 1),
3637 (new CFPlus::UI::Label markup => $right, align => +1), 3848 (new DC::UI::Label markup => $right, align => +1),
3638 ], 3849 ],
3639 ; 3850 ;
3640 3851
3641 } else { 3852 } else {
3642 $widget = new CFPlus::UI::Label 3853 $widget = new DC::UI::Label
3643 can_hover => 1, 3854 can_hover => 1,
3644 can_events => 1, 3855 can_events => 1,
3645 markup => $widget, 3856 markup => $widget,
3646 tooltip => $tooltip; 3857 tooltip => $tooltip;
3647 } 3858 }
3695 1 3906 1
3696} 3907}
3697 3908
3698############################################################################# 3909#############################################################################
3699 3910
3700package CFPlus::UI::Multiplexer; 3911package DC::UI::Multiplexer;
3701 3912
3702our @ISA = CFPlus::UI::Container::; 3913our @ISA = DC::UI::Container::;
3703 3914
3704sub new { 3915sub new {
3705 my $class = shift; 3916 my $class = shift;
3706 3917
3707 my $self = $class->SUPER::new ( 3918 my $self = $class->SUPER::new (
3768 $self->{current}->draw; 3979 $self->{current}->draw;
3769} 3980}
3770 3981
3771############################################################################# 3982#############################################################################
3772 3983
3773package CFPlus::UI::Notebook; 3984package DC::UI::Notebook;
3774 3985
3986use DC::OpenGL;
3987
3775our @ISA = CFPlus::UI::VBox::; 3988our @ISA = DC::UI::VBox::;
3776 3989
3777sub new { 3990sub new {
3778 my $class = shift; 3991 my $class = shift;
3779 3992
3780 my $self = $class->SUPER::new ( 3993 my $self = $class->SUPER::new (
3781 buttonbar => (new CFPlus::UI::Buttonbar), 3994 buttonbar => (new DC::UI::Buttonbar),
3782 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3995 multiplexer => (new DC::UI::Multiplexer expand => 1),
3996 active_outline => [.7, .7, 0.2],
3783 # filter => # will be put between multiplexer and $self 3997 # filter => # will be put between multiplexer and $self
3784 @_, 3998 @_,
3785 ); 3999 );
3786 4000
3787 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4001 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3788 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 4002 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3789 4003
4004 {
4005 Scalar::Util::weaken (my $wself = $self);
4006
4007 $self->{multiplexer}->connect (c_add => sub {
4008 my ($mplex, $widgets) = @_;
4009
4010 for my $child (@$widgets) {
4011 Scalar::Util::weaken $child;
4012 $child->{c_tab_} ||= do {
4013 my $tab =
4014 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4015 ? $child->{c_tab}
4016 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4017
4018 $tab->connect (activate => sub {
4019 $wself->set_current_page ($child);
4020 });
4021
4022 $tab
4023 };
4024
4025 $self->{buttonbar}->add ($child->{c_tab_});
4026 }
4027 });
4028
4029 $self->{multiplexer}->connect (c_remove => sub {
4030 my ($mplex, $widgets) = @_;
4031
4032 for my $child (@$widgets) {
4033 $wself->{buttonbar}->remove ($child->{c_tab_});
4034 }
4035 });
4036 }
4037
3790 $self 4038 $self
3791} 4039}
3792 4040
3793sub add { 4041sub add {
4042 my ($self, @widgets) = @_;
4043
4044 $self->{multiplexer}->add (@widgets)
4045}
4046
4047sub remove {
4048 my ($self, @widgets) = @_;
4049
4050 $self->{multiplexer}->remove (@widgets)
4051}
4052
4053sub pages {
4054 my ($self) = @_;
4055 $self->{multiplexer}->children
4056}
4057
4058sub add_tab {
3794 my ($self, $title, $widget, $tooltip) = @_; 4059 my ($self, $title, $widget, $tooltip) = @_;
3795 4060
3796 CFPlus::weaken $self; 4061 $title = [$title, $tooltip] unless ref $title;
4062 $widget->{c_tab} = $title;
3797 4063
3798 $self->{buttonbar}->add (new CFPlus::UI::Button
3799 markup => $title,
3800 tooltip => $tooltip,
3801 on_activate => sub { $self->set_current_page ($widget) },
3802 );
3803
3804 $self->{multiplexer}->add ($widget); 4064 $self->add ($widget);
3805} 4065}
3806 4066
3807sub get_current_page { 4067sub get_current_page {
3808 my ($self) = @_; 4068 my ($self) = @_;
3809 4069
3815 4075
3816 $self->{multiplexer}->set_current_page ($page); 4076 $self->{multiplexer}->set_current_page ($page);
3817 $self->emit (page_changed => $self->{multiplexer}{current}); 4077 $self->emit (page_changed => $self->{multiplexer}{current});
3818} 4078}
3819 4079
4080sub _draw {
4081 my ($self) = @_;
4082
4083 $self->SUPER::_draw ();
4084
4085 if (my $cur = $self->{multiplexer}{current}) {
4086 if ($cur = $cur->{c_tab_}) {
4087 glTranslate $self->{buttonbar}{x} + $cur->{x},
4088 $self->{buttonbar}{y} + $cur->{y};
4089 glLineWidth 3;
4090 #glEnable GL_BLEND;
4091 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4092 glColor @{$self->{active_outline}};
4093 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4094 glLineWidth 1;
4095 #glDisable GL_BLEND;
4096 }
4097 }
4098}
4099
3820############################################################################# 4100#############################################################################
3821 4101
3822package CFPlus::UI::Selector; 4102package DC::UI::Selector;
3823 4103
3824use utf8; 4104use utf8;
3825 4105
3826our @ISA = CFPlus::UI::Button::; 4106our @ISA = DC::UI::Button::;
3827 4107
3828sub new { 4108sub new {
3829 my $class = shift; 4109 my $class = shift;
3830 4110
3831 my $self = $class->SUPER::new ( 4111 my $self = $class->SUPER::new (
3848 my ($value, $title, $tooltip) = @$_; 4128 my ($value, $title, $tooltip) = @$_;
3849 4129
3850 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4130 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3851 } 4131 }
3852 4132
3853 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4133 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
3854} 4134}
3855 4135
3856sub _set_value { 4136sub _set_value {
3857 my ($self, $value) = @_; 4137 my ($self, $value) = @_;
3858 4138
3859 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4139 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4140 $item ||= $self->{options}[0]
3860 or return; 4141 or return;
3861 4142
3862 $self->{value} = $item->[0]; 4143 $self->{value} = $item->[0];
3863 $self->set_markup ("$item->[1] ⇓"); 4144 $self->set_markup ("$item->[1] ⇓");
3864 $self->set_tooltip ($item->[2]); 4145# $self->set_tooltip ($item->[2]);
3865} 4146}
3866 4147
3867sub set_value { 4148sub set_value {
3868 my ($self, $value) = @_; 4149 my ($self, $value) = @_;
3869 4150
3871 4152
3872 $self->_set_value ($value); 4153 $self->_set_value ($value);
3873 $self->emit (changed => $value); 4154 $self->emit (changed => $value);
3874} 4155}
3875 4156
4157sub set_options {
4158 my ($self, $options) = @_;
4159
4160 $self->{options} = $options;
4161 $self->_set_value ($self->{value});
4162}
4163
3876############################################################################# 4164#############################################################################
3877 4165
3878package CFPlus::UI::Statusbox; 4166package DC::UI::Statusbox;
3879 4167
3880our @ISA = CFPlus::UI::VBox::; 4168our @ISA = DC::UI::VBox::;
3881 4169
3882sub new { 4170sub new {
3883 my $class = shift; 4171 my $class = shift;
3884 4172
3885 my $self = $class->SUPER::new ( 4173 my $self = $class->SUPER::new (
3886 fontsize => 0.8, 4174 fontsize => 0.8,
3887 @_, 4175 @_,
3888 ); 4176 );
3889 4177
3890 CFPlus::weaken (my $this = $self); 4178 DC::weaken (my $this = $self);
3891 4179
3892 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4180 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3893 4181
3894 $self 4182 $self
3895} 4183}
3896 4184
3897sub reorder { 4185sub reorder {
3898 my ($self) = @_; 4186 my ($self) = @_;
3899 my $NOW = Time::HiRes::time; 4187 my $NOW = Time::HiRes::time;
3900 4188
3901 # freeze display when hovering over any label 4189 # freeze display when hovering over any label
3902 return if $CFPlus::UI::TOOLTIP->{owner} 4190 return if $DC::UI::TOOLTIP->{owner}
3903 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4191 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
3904 values %{ $self->{item} }; 4192 values %{ $self->{item} };
3905 4193
3906 while (my ($k, $v) = each %{ $self->{item} }) { 4194 while (my ($k, $v) = each %{ $self->{item} }) {
3907 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4195 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3908 } 4196 }
4197
4198 $self->{timer}->set (1, 1);
3909 4199
3910 my @widgets; 4200 my @widgets;
3911 4201
3912 my @items = sort { 4202 my @items = sort {
3913 $a->{pri} <=> $b->{pri} 4203 $a->{pri} <=> $b->{pri}
3914 or $b->{id} <=> $a->{id} 4204 or $b->{id} <=> $a->{id}
3915 } values %{ $self->{item} }; 4205 } values %{ $self->{item} };
3916
3917 $self->{timer}->interval (1);
3918 4206
3919 my $count = 10 + 1; 4207 my $count = 10 + 1;
3920 for my $item (@items) { 4208 for my $item (@items) {
3921 last unless --$count; 4209 last unless --$count;
3922 4210
3929 for ($short) { 4217 for ($short) {
3930 s/^\s+//; 4218 s/^\s+//;
3931 s/\s+/ /g; 4219 s/\s+/ /g;
3932 } 4220 }
3933 4221
3934 new CFPlus::UI::Label 4222 new DC::UI::Label
3935 markup => $short, 4223 markup => $short,
3936 tooltip => $item->{tooltip}, 4224 tooltip => $item->{tooltip},
3937 tooltip_font => $::FONT_PROP, 4225 tooltip_font => $::FONT_PROP,
3938 tooltip_width => 0.67, 4226 tooltip_width => 0.67,
3939 fontsize => $item->{fontsize} || $self->{fontsize}, 4227 fontsize => $item->{fontsize} || $self->{fontsize},
3946 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4234 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3947 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4235 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3948 $label->update; 4236 $label->update;
3949 $label->set_max_size (undef, $label->{req_h} * $diff) 4237 $label->set_max_size (undef, $label->{req_h} * $diff)
3950 if $diff < 1; 4238 if $diff < 1;
3951 $self->{timer}->interval (1/30); 4239 $self->{timer}->set (1/30, 1/30);
3952 } else { 4240 } else {
3953 $label->{fg}[3] = $item->{fg}[3] || 1; 4241 $label->{fg}[3] = $item->{fg}[3] || 1;
3954 } 4242 }
3955 4243
3956 push @widgets, $label; 4244 push @widgets, $label;
4018 $self->SUPER::destroy; 4306 $self->SUPER::destroy;
4019} 4307}
4020 4308
4021############################################################################# 4309#############################################################################
4022 4310
4023package CFPlus::UI::Root; 4311package DC::UI::Root;
4024 4312
4025our @ISA = CFPlus::UI::Container::; 4313our @ISA = DC::UI::Container::;
4026 4314
4027use List::Util qw(min max); 4315use List::Util qw(min max);
4028 4316
4029use CFPlus::OpenGL; 4317use DC::OpenGL;
4030 4318
4031sub new { 4319sub new {
4032 my $class = shift; 4320 my $class = shift;
4033 4321
4034 my $self = $class->SUPER::new ( 4322 my $self = $class->SUPER::new (
4035 visible => 1, 4323 visible => 1,
4036 @_, 4324 @_,
4037 ); 4325 );
4038 4326
4039 CFPlus::weaken ($self->{root} = $self); 4327 DC::weaken ($self->{root} = $self);
4040 4328
4041 $self 4329 $self
4042} 4330}
4043 4331
4044sub size_request { 4332sub size_request {
4092} 4380}
4093 4381
4094sub update { 4382sub update {
4095 my ($self) = @_; 4383 my ($self) = @_;
4096 4384
4097 $::WANT_REFRESH++; 4385 $::WANT_REFRESH = 1;
4098} 4386}
4099 4387
4100sub add { 4388sub add {
4101 my ($self, @children) = @_; 4389 my ($self, @children) = @_;
4102 4390
4139 while ($self->{refresh_hook}) { 4427 while ($self->{refresh_hook}) {
4140 $_->() 4428 $_->()
4141 for values %{delete $self->{refresh_hook}}; 4429 for values %{delete $self->{refresh_hook}};
4142 } 4430 }
4143 4431
4144 if ($self->{realloc}) { 4432 while ($self->{realloc}) {
4145 my %queue; 4433 my %queue;
4146 my @queue; 4434 my @queue;
4147 my $widget; 4435 my $widget;
4148 4436
4149 outer: 4437 outer:
4196 } 4484 }
4197 } 4485 }
4198 4486
4199 delete $self->{realloc}{$widget+0}; 4487 delete $self->{realloc}{$widget+0};
4200 } 4488 }
4201 }
4202 4489
4203 while (my $size_alloc = delete $self->{size_alloc}) { 4490 while (my $size_alloc = delete $self->{size_alloc}) {
4204 my @queue = sort { $b->{visible} <=> $a->{visible} } 4491 my @queue = sort { $a->{visible} <=> $b->{visible} }
4205 values %$size_alloc; 4492 values %$size_alloc;
4206 4493
4207 while () { 4494 while () {
4208 my $widget = pop @queue || last; 4495 my $widget = pop @queue || last;
4209 4496
4210 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4497 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4211 4498
4212 $w = 0 if $w < 0;
4213 $h = 0 if $h < 0;
4214
4215 $w = max $widget->{min_w}, $w; 4499 $w = max $widget->{min_w}, $w;
4216 $h = max $widget->{min_h}, $h; 4500 $h = max $widget->{min_h}, $h;
4217 4501
4218# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4502# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4219# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4503# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4220 4504
4221 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4505 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4222 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4506 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4223 4507
4224 $w = int $w + 0.5; 4508 $w = int $w + 0.5;
4225 $h = int $h + 0.5; 4509 $h = int $h + 0.5;
4226 4510
4227 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4511 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4228 $widget->{old_w} = $widget->{w}; 4512 $widget->{old_w} = $widget->{w};
4229 $widget->{old_h} = $widget->{h}; 4513 $widget->{old_h} = $widget->{h};
4230 4514
4231 $widget->{w} = $w; 4515 $widget->{w} = $w;
4232 $widget->{h} = $h; 4516 $widget->{h} = $h;
4233 4517
4234 $widget->emit (size_allocate => $w, $h); 4518 $widget->emit (size_allocate => $w, $h);
4519 }
4235 } 4520 }
4236 } 4521 }
4237 } 4522 }
4238 4523
4239 while ($self->{post_alloc_hook}) { 4524 while ($self->{post_alloc_hook}) {
4250 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4535 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4251 glMatrixMode GL_MODELVIEW; 4536 glMatrixMode GL_MODELVIEW;
4252 glLoadIdentity; 4537 glLoadIdentity;
4253 4538
4254 { 4539 {
4255 package CFPlus::UI::Base; 4540 package DC::UI::Base;
4256 4541
4257 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4542 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4258 (0, 0, $self->{w}, $self->{h}); 4543 (0, 0, $self->{w}, $self->{h});
4259 4544
4260 $self->_draw; 4545 $self->_draw;
4261 } 4546 }
4262} 4547}
4263 4548
4264############################################################################# 4549#############################################################################
4265 4550
4266package CFPlus::UI; 4551package DC::UI;
4267 4552
4268$ROOT = new CFPlus::UI::Root; 4553$ROOT = new DC::UI::Root;
4269$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4554$TOOLTIP = new DC::UI::Tooltip z => 900;
4270 4555
42711 45561
4272 4557

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines