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.361 by root, Sat Dec 9 02:21:25 2006 UTC vs.
Revision 1.439 by root, Sun Sep 16 13:04:36 2007 UTC

1package CFPlus::UI; 1package CFPlus::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util ();
7use List::Util (); 6use List::Util ();
8use Event; 7use Event;
9 8
10use CFPlus; 9use CFPlus;
11use CFPlus::Pod; 10use CFPlus::Pod;
23our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
24 if (!$GRAB) { 23 if (!$GRAB) {
25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
26 if (length $widget->{tooltip}) { 25 if (length $widget->{tooltip}) {
27 if ($TOOLTIP->{owner} != $widget) { 26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
28 $TOOLTIP->hide; 28 $TOOLTIP->hide;
29 29
30 $TOOLTIP->{owner} = $widget; 30 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
31 32
32 return if $ENV{CFPLUS_DEBUG} & 8; 33 return if $ENV{CFPLUS_DEBUG} & 8;
33 34
34 my $tip = $widget->{tooltip};
35
36 $tip = $tip->($widget) if CODE:: eq ref $tip;
37
38 $TOOLTIP->set_tooltip_from ($widget); 35 $TOOLTIP->set_tooltip_from ($widget);
39 $TOOLTIP->show; 36 $TOOLTIP->show;
40 } 37 }
41 38
42 return; 39 return;
43 } 40 }
44 } 41 }
45 } 42 }
46 43
47 $TOOLTIP->hide; 44 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
48 delete $TOOLTIP->{owner}; 46 delete $TOOLTIP->{owner};
49}); 47});
50 48
51sub get_layout { 49sub get_layout {
52 my $layout; 50 my $layout;
111 } 109 }
112 110
113 if ($GRAB) { 111 if ($GRAB) {
114 if ($ev->{button} == 4 || $ev->{button} == 5) { 112 if ($ev->{button} == 4 || $ev->{button} == 5) {
115 # mousewheel 113 # mousewheel
116 $ev->{dx} = 0;
117 $ev->{dy} = $ev->{button} * 2 - 9; 114 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT;
116
117 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta;
119
118 $GRAB->emit (mouse_wheel => $ev); 120 $GRAB->emit (mouse_wheel => $ev);
119 } else { 121 } else {
120 $GRAB->emit (button_down => $ev) 122 $GRAB->emit (button_down => $ev)
121 } 123 }
122 } 124 }
235 h => undef, 237 h => undef,
236 can_events => 1, 238 can_events => 1,
237 @_ 239 @_
238 }, $class; 240 }, $class;
239 241
240 Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
241 243
242 for (keys %$self) { 244 for (keys %$self) {
243 if (/^on_(.*)$/) { 245 if (/^on_(.*)$/) {
244 $self->connect ($1 => delete $self->{$_}); 246 $self->connect ($1 => delete $self->{$_});
245 } 247 }
266 $self->hide; 268 $self->hide;
267 $self->emit ("destroy"); 269 $self->emit ("destroy");
268 %$self = (); 270 %$self = ();
269} 271}
270 272
273sub TO_JSON {
274 { "\fw" => $_[0]{s_id} }
275}
276
271sub show { 277sub show {
272 my ($self) = @_; 278 my ($self) = @_;
273 279
274 return if $self->{parent}; 280 return if $self->{parent};
275 281
314sub set_visibility { 320sub set_visibility {
315 my ($self, $visible) = @_; 321 my ($self, $visible) = @_;
316 322
317 return if $self->{visible} == $visible; 323 return if $self->{visible} == $visible;
318 324
319 $visible ? $self->hide 325 $visible ? $self->show
320 : $self->show; 326 : $self->hide;
321} 327}
322 328
323sub toggle_visibility { 329sub toggle_visibility {
324 my ($self) = @_; 330 my ($self) = @_;
325 331
352 358
353 $self->{force_w} = $w; 359 $self->{force_w} = $w;
354 $self->{force_h} = $h; 360 $self->{force_h} = $h;
355 361
356 $self->realloc; 362 $self->realloc;
363}
364
365# travsle the widget chain up to find the maximum "physical" size constraints
366sub get_max_wh {
367 my ($self) = @_;
368
369 return ($self->{alloc_w}, $self->{alloc_h})
370 if $self->{alloc_w} && !$self->{root}{realloc}{$self+0};
371
372 return $self->{parent}->get_max_wh
373 if $self->{parent};
374
375 ($::WIDTH, $::HEIGHT)
357} 376}
358 377
359sub size_request { 378sub size_request {
360 require Carp; 379 require Carp;
361 Carp::confess "size_request is abstract"; 380 Carp::confess "size_request is abstract";
530 ? $args[0]->xy ($self) 549 ? $args[0]->xy ($self)
531 : (); 550 : ();
532 551
533 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 552 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
534 553
535 #d##TODO# stop propagating at first true, do not use sum 554 for my $cb (
536 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 555 @{$self->{signal_cb}{$signal} || []}, # before
537 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 556 ($self->can ("invoke_$signal") || sub { 1 }), # closure
557 ) {
558 return $cb->($self, @args, @append) || next;
559 }
560
561 # parent
538 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 562 $self->{parent} && $self->{parent}->emit ($signal, @args)
539} 563}
540 564
541sub find_widget { 565#sub find_widget {
542 my ($self, $x, $y) = @_; 566# in .xs
543
544 return () unless $self->{can_events};
545
546 return $self
547 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
548 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
549
550 ()
551}
552 567
553sub set_parent { 568sub set_parent {
554 my ($self, $parent) = @_; 569 my ($self, $parent) = @_;
555 570
556 Scalar::Util::weaken ($self->{parent} = $parent); 571 CFPlus::weaken ($self->{parent} = $parent);
557 $self->set_visible if $parent->{visible}; 572 $self->set_visible if $parent->{visible};
558} 573}
559 574
560sub realloc { 575sub realloc {
561 my ($self) = @_; 576 my ($self) = @_;
587 602
588# using global variables seems a bit hacky, but passing through all drawing 603# using global variables seems a bit hacky, but passing through all drawing
589# functions seems pointless. 604# functions seems pointless.
590our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 605our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
591 606
592sub draw { 607#sub draw {
593 my ($self) = @_; 608#CFPlus.xs
594
595 return unless $self->{h} && $self->{w};
596
597 # update screen rectangle
598 local $draw_x = $draw_x + $self->{x};
599 local $draw_y = $draw_y + $self->{y};
600
601 # skip widgets that are entirely outside the drawing area
602 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
603 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
604
605 glPushMatrix;
606 glTranslate $self->{x}, $self->{y}, 0;
607
608 if ($self == $HOVER && $self->{can_hover}) {
609 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
610 glEnable GL_BLEND;
611 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
612 glBegin GL_QUADS;
613 glVertex 0 , 0;
614 glVertex $self->{w}, 0;
615 glVertex $self->{w}, $self->{h};
616 glVertex 0 , $self->{h};
617 glEnd;
618 glDisable GL_BLEND;
619 }
620
621 if ($ENV{CFPLUS_DEBUG} & 1) {
622 glPushMatrix;
623 glColor 1, 1, 0, 1;
624 glTranslate 0.375, 0.375;
625 glBegin GL_LINE_LOOP;
626 glVertex 0 , 0;
627 glVertex $self->{w} - 1, 0;
628 glVertex $self->{w} - 1, $self->{h} - 1;
629 glVertex 0 , $self->{h} - 1;
630 glEnd;
631 glPopMatrix;
632 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
633 }
634
635 $self->_draw;
636 glPopMatrix;
637}
638 609
639sub _draw { 610sub _draw {
640 my ($self) = @_; 611 my ($self) = @_;
641 612
642 warn "no draw defined for $self\n"; 613 warn "no draw defined for $self\n";
645sub DESTROY { 616sub DESTROY {
646 my ($self) = @_; 617 my ($self) = @_;
647 618
648 return if CFPlus::in_destruct; 619 return if CFPlus::in_destruct;
649 620
621 local $@;
650 eval { $self->destroy }; 622 eval { $self->destroy };
651 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 623 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
652 624
653 delete $WIDGET{$self+0}; 625 delete $WIDGET{$self+0};
654} 626}
662use strict; 634use strict;
663use CFPlus::OpenGL; 635use CFPlus::OpenGL;
664 636
665sub new { 637sub new {
666 my $class = shift; 638 my $class = shift;
667
668 # range [value, low, high, page]
669 639
670 $class->SUPER::new ( 640 $class->SUPER::new (
671 #bg => [0, 0, 0, 0.2], 641 #bg => [0, 0, 0, 0.2],
672 #active_bg => [1, 1, 1, 0.5], 642 #active_bg => [1, 1, 1, 0.5],
673 @_ 643 @_
685 my ($w, $h) = @$self{qw(w h)}; 655 my ($w, $h) = @$self{qw(w h)};
686 656
687 glEnable GL_BLEND; 657 glEnable GL_BLEND;
688 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 658 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
689 glColor_premultiply @$color; 659 glColor_premultiply @$color;
690
691 glBegin GL_QUADS;
692 glVertex 0 , 0;
693 glVertex 0 , $h;
694 glVertex $w, $h; 660 glRect 0, 0, $w, $h;
695 glVertex $w, 0;
696 glEnd;
697
698 glDisable GL_BLEND; 661 glDisable GL_BLEND;
699 } 662 }
700} 663}
701 664
702############################################################################# 665#############################################################################
734 can_events => 0, 697 can_events => 0,
735 %arg, 698 %arg,
736 ); 699 );
737 700
738 $self->add (@$children) 701 $self->add (@$children)
739 if $children; 702 if $children && @$children;
740 703
741 $self 704 $self
742} 705}
743 706
744sub realloc { 707sub realloc {
753 my ($self, @widgets) = @_; 716 my ($self, @widgets) = @_;
754 717
755 $_->set_parent ($self) 718 $_->set_parent ($self)
756 for @widgets; 719 for @widgets;
757 720
721 # TODO: only do this in widgets that need it, e.g. root, fixed
758 use sort 'stable'; 722 use sort 'stable';
759 723
760 $self->{children} = [ 724 $self->{children} = [
761 sort { $a->{z} <=> $b->{z} } 725 sort { $a->{z} <=> $b->{z} }
762 @{$self->{children}}, @widgets 726 @{$self->{children}}, @widgets
763 ]; 727 ];
764 728
765 $self->realloc; 729 $self->realloc;
730
731 $self->emit (c_add => \@widgets);
732
733 map $_+0, @widgets
766} 734}
767 735
768sub children { 736sub children {
769 @{ $_[0]{children} } 737 @{ $_[0]{children} }
770} 738}
771 739
772sub remove { 740sub remove {
773 my ($self, $child) = @_; 741 my ($self, @widgets) = @_;
774 742
743 $self->emit (c_remove => \@widgets);
744
745 for my $child (@widgets) {
775 delete $child->{parent}; 746 delete $child->{parent};
776 $child->hide; 747 $child->hide;
777
778 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 748 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
749 }
779 750
780 $self->realloc; 751 $self->realloc;
781} 752}
782 753
783sub clear { 754sub clear {
784 my ($self) = @_; 755 my ($self) = @_;
785 756
786 my $children = delete $self->{children}; 757 my $children = $self->{children};
787 $self->{children} = []; 758 $self->{children} = [];
788 759
789 for (@$children) { 760 for (@$children) {
790 delete $_->{parent}; 761 delete $_->{parent};
791 $_->hide; 762 $_->hide;
811} 782}
812 783
813sub _draw { 784sub _draw {
814 my ($self) = @_; 785 my ($self) = @_;
815 786
816 $_->draw for @{$self->{children}}; 787 $_->draw for $self->visible_children;
817} 788}
818 789
819############################################################################# 790#############################################################################
820 791
821package CFPlus::UI::Bin; 792package CFPlus::UI::Bin;
831} 802}
832 803
833sub add { 804sub add {
834 my ($self, $child) = @_; 805 my ($self, $child) = @_;
835 806
836 $self->SUPER::remove ($_) for @{ $self->{children} }; 807 $self->clear;
837 $self->SUPER::add ($child); 808 $self->SUPER::add ($child);
838} 809}
839 810
840sub remove { 811sub remove {
841 my ($self, $widget) = @_; 812 my ($self, $widget) = @_;
859 830
860 1 831 1
861} 832}
862 833
863############################################################################# 834#############################################################################
864
865# back-buffered drawing area 835# back-buffered drawing area
866 836
867package CFPlus::UI::Window; 837package CFPlus::UI::Window;
868 838
869our @ISA = CFPlus::UI::Bin::; 839our @ISA = CFPlus::UI::Bin::;
931} 901}
932 902
933############################################################################# 903#############################################################################
934 904
935package CFPlus::UI::ViewPort; 905package CFPlus::UI::ViewPort;
906
907use List::Util qw(min max);
936 908
937our @ISA = CFPlus::UI::Window::; 909our @ISA = CFPlus::UI::Window::;
938 910
939sub new { 911sub new {
940 my $class = shift; 912 my $class = shift;
949sub size_request { 921sub size_request {
950 my ($self) = @_; 922 my ($self) = @_;
951 923
952 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 924 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
953 925
954 $w = 10 if $self->{scroll_x}; 926 $w = 1 if $self->{scroll_x};
955 $h = 10 if $self->{scroll_y}; 927 $h = 1 if $self->{scroll_y};
956 928
957 ($w, $h) 929 ($w, $h)
958} 930}
959 931
960sub invoke_size_allocate { 932sub invoke_size_allocate {
972} 944}
973 945
974sub set_offset { 946sub set_offset {
975 my ($self, $x, $y) = @_; 947 my ($self, $x, $y) = @_;
976 948
949 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
950 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
951
952 if ($x != $self->{view_x} or $y != $self->{view_y}) {
977 $self->{view_x} = int $x; 953 $self->{view_x} = $x;
978 $self->{view_y} = int $y; 954 $self->{view_y} = $y;
979 955
956 $self->emit (changed => $x, $y);
980 $self->update; 957 $self->update;
958 }
959}
960
961sub set_center {
962 my ($self, $x, $y) = @_;
963
964 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
965}
966
967sub make_visible {
968 my ($self, $x, $y, $border) = @_;
969
970 if ( $x < $self->{view_x} + $self->{w} * $border
971 || $x > $self->{view_x} + $self->{w} * (1 - $border)
972 || $y < $self->{view_y} + $self->{h} * $border
973 || $y > $self->{view_y} + $self->{h} * (1 - $border)
974 ) {
975 $self->set_center ($x, $y);
976 }
981} 977}
982 978
983# hmm, this does not work for topleft of $self... but we should not ask for that 979# hmm, this does not work for topleft of $self... but we should not ask for that
984sub coord2local { 980sub coord2local {
985 my ($self, $x, $y) = @_; 981 my ($self, $x, $y) = @_;
1000 my ($self, $x, $y) = @_; 996 my ($self, $x, $y) = @_;
1001 997
1002 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1003 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 999 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1004 ) { 1000 ) {
1005 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1001 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1006 } else { 1002 } else {
1007 $self->CFPlus::UI::Base::find_widget ($x, $y) 1003 $self->CFPlus::UI::Base::find_widget ($x, $y)
1008 } 1004 }
1009} 1005}
1010 1006
1021 1017
1022############################################################################# 1018#############################################################################
1023 1019
1024package CFPlus::UI::ScrolledWindow; 1020package CFPlus::UI::ScrolledWindow;
1025 1021
1026our @ISA = CFPlus::UI::HBox::; 1022our @ISA = CFPlus::UI::Table::;
1027 1023
1028sub new { 1024sub new {
1029 my ($class, %arg) = @_; 1025 my ($class, %arg) = @_;
1030 1026
1031 my $child = delete $arg{child}; 1027 my $child = delete $arg{child};
1032 1028
1033 my $self; 1029 my $self;
1034 1030
1035 my $slider = new CFPlus::UI::Slider 1031 my $hslider = new CFPlus::UI::Slider
1032 c_col => 0,
1033 c_row => 1,
1034 vertical => 0,
1035 range => [0, 0, 1, 0.01], # HACK fix
1036 on_changed => sub {
1037 $self->{hpos} = $_[1];
1038 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1039 },
1040 ;
1041
1042 my $vslider = new CFPlus::UI::Slider
1043 c_col => 1,
1044 c_row => 0,
1036 vertical => 1, 1045 vertical => 1,
1037 range => [0, 0, 1, 0.01], # HACK fix 1046 range => [0, 0, 1, 0.01], # HACK fix
1038 on_changed => sub { 1047 on_changed => sub {
1039 $self->{vp}->set_offset (0, $_[1]); 1048 $self->{vpos} = $_[1];
1049 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1040 }, 1050 },
1041 ; 1051 ;
1042 1052
1043 $self = $class->SUPER::new ( 1053 $self = $class->SUPER::new (
1044 vp => (new CFPlus::UI::ViewPort expand => 1), 1054 scroll_x => 0,
1055 scroll_y => 1,
1045 can_events => 1, 1056 can_events => 1,
1046 slider => $slider, 1057 hslider => $hslider,
1058 vslider => $vslider,
1059 col_expand => [1, 0],
1060 row_expand => [1, 0],
1047 %arg, 1061 %arg,
1048 ); 1062 );
1049 1063
1064 $self->{vp} = new CFPlus::UI::ViewPort
1065 c_col => 0,
1066 c_row => 0,
1067 expand => 1,
1068 scroll_x => $self->{scroll_x},
1069 scroll_y => $self->{scroll_y},
1070 on_changed => sub {
1071 my ($vp, $x, $y) = @_;
1072
1073 $vp->{parent}{hslider}->set_value ($x);
1074 $vp->{parent}{vslider}->set_value ($y);
1075
1076 0
1077 },
1078 on_size_allocate => sub {
1079 my ($vp, $w, $h) = @_;
1080 $vp->{parent}->update_slider;
1081 0
1082 },
1083 ;
1084
1050 $self->SUPER::add ($self->{vp}, $self->{slider}); 1085 $self->SUPER::add ($self->{vp});
1086
1051 $self->add ($child) if $child; 1087 $self->add ($child) if $child;
1052 1088
1053 $self 1089 $self
1054} 1090}
1055
1056#TODO# update range on size_allocate depending on child
1057 1091
1058sub add { 1092sub add {
1059 my ($self, $widget) = @_; 1093 my ($self, $widget) = @_;
1060 1094
1061 $self->{vp}->add ($self->{child} = $widget); 1095 $self->{vp}->add ($self->{child} = $widget);
1062} 1096}
1063 1097
1098sub set_offset { shift->{vp}->set_offset (@_) }
1099sub set_center { shift->{vp}->set_center (@_) }
1100sub make_visible { shift->{vp}->make_visible (@_) }
1101
1102sub update_slider {
1103 my ($self) = @_;
1104
1105 my $child = ($self->{vp} or return)->child;
1106
1107 if ($self->{scroll_x}) {
1108 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1109 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1110
1111 my $visible = $w1 > $w2;
1112 if ($visible != $self->{hslider_visible}) {
1113 $self->{hslider_visible} = $visible;
1114 $visible ? $self->SUPER::add ($self->{hslider})
1115 : $self->SUPER::remove ($self->{hslider});
1116 }
1117 }
1118
1119 if ($self->{scroll_y}) {
1120 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1121 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1122
1123 my $visible = $h1 > $h2;
1124 if ($visible != $self->{vslider_visible}) {
1125 $self->{vslider_visible} = $visible;
1126 $visible ? $self->SUPER::add ($self->{vslider})
1127 : $self->SUPER::remove ($self->{vslider});
1128 }
1129 }
1130}
1131
1132sub start_dragging {
1133 my ($self, $ev) = @_;
1134
1135 $self->grab_focus;
1136
1137 my $ox = $self->{vp}{view_x};
1138 my $oy = $self->{vp}{view_y};
1139
1140 $self->{motion} = sub {
1141 my ($ev, $x, $y) = @_;
1142
1143 $ox -= $ev->{xrel};
1144 $oy -= $ev->{yrel};
1145
1146 $self->{vp}->set_offset ($ox, $oy);
1147 };
1148}
1149
1064sub invoke_mouse_wheel { 1150sub invoke_mouse_wheel {
1065 my ($self, $ev) = @_; 1151 my ($self, $ev) = @_;
1066 1152
1067 return 0 unless $ev->{dy}; # only vertical movements
1068
1069 $self->{slider}->emit (mouse_wheel => $ev); 1153 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1154 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1070 1155
1071 1 1156 1
1072} 1157}
1073 1158
1074sub update_slider { 1159sub invoke_button_down {
1075 my ($self) = @_; 1160 my ($self, $ev, $x, $y) = @_;
1076 1161
1077 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1162 if ($ev->{button} == 2) {
1078} 1163 $self->start_dragging ($ev);
1164 return 1;
1165 }
1079 1166
1080sub update { 1167 0
1081 my ($self) = @_; 1168}
1082 1169
1083 $self->SUPER::update; 1170sub invoke_button_up {
1171 my ($self, $ev, $x, $y) = @_;
1084 1172
1085 $self->update_slider; 1173 if (delete $self->{motion}) {
1174 return 1;
1175 }
1176
1177 0
1178}
1179
1180sub invoke_mouse_motion {
1181 my ($self, $ev, $x, $y) = @_;
1182
1183 if ($self->{motion}) {
1184 $self->{motion}->($ev, $x, $y);
1185 return 1;
1186 }
1187
1188 0
1086} 1189}
1087 1190
1088sub invoke_size_allocate { 1191sub invoke_size_allocate {
1089 my ($self, $w, $h) = @_; 1192 my ($self, $w, $h) = @_;
1090 1193
1091 $self->update_slider; 1194 $self->update_slider;
1092
1093 $self->SUPER::invoke_size_allocate ($w, $h) 1195 $self->SUPER::invoke_size_allocate ($w, $h)
1094} 1196}
1095 1197
1096############################################################################# 1198#############################################################################
1097 1199
1117 my ($w, $h) = @$self{qw(w h)}; 1219 my ($w, $h) = @$self{qw(w h)};
1118 1220
1119 glEnable GL_BLEND; 1221 glEnable GL_BLEND;
1120 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1121 glColor_premultiply @{ $self->{bg} }; 1223 glColor_premultiply @{ $self->{bg} };
1122
1123 glBegin GL_QUADS;
1124 glVertex 0 , 0;
1125 glVertex 0 , $h;
1126 glVertex $w, $h; 1224 glRect 0, 0, $w, $h;
1127 glVertex $w, 0;
1128 glEnd;
1129
1130 glDisable GL_BLEND; 1225 glDisable GL_BLEND;
1131 } 1226 }
1132 1227
1133 $self->SUPER::_draw; 1228 $self->SUPER::_draw;
1134} 1229}
1247 1342
1248my @border = 1343my @border =
1249 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1344 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1250 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1345 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1251 1346
1347my @icon =
1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1349 qw(x1_move.png x1_resize.png);
1350
1252sub new { 1351sub new {
1253 my ($class, %arg) = @_; 1352 my ($class, %arg) = @_;
1254 1353
1255 my $self = $class->SUPER::new ( 1354 my $self = $class->SUPER::new (
1256 bg => [1, 1, 1, 1], 1355 bg => [1, 1, 1, 1],
1424 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1523 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1425 1524
1426 my $border = $self->border; 1525 my $border = $self->border;
1427 1526
1428 glColor @{ $self->{border_bg} }; 1527 glColor @{ $self->{border_bg} };
1429 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1528 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1430 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1529 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1431 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1530 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1432 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1531 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1532
1533 # move
1534 my $w2 = ($w - $border) * .5;
1535 my $h2 = ($h - $border) * .5;
1536 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1537 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1538 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1539
1540 # resize
1541 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1542 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1543 unless $self->{has_close_button};
1544 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1545 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1433 1546
1434 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1547 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1435 glColor @{ $self->{bg} }; 1548 glColor @{ $self->{bg} };
1436 1549
1437 # TODO: repeat texture not scale 1550 # TODO: repeat texture not scale
1458 1571
1459############################################################################# 1572#############################################################################
1460 1573
1461package CFPlus::UI::Table; 1574package CFPlus::UI::Table;
1462 1575
1463our @ISA = CFPlus::UI::Base::; 1576our @ISA = CFPlus::UI::Container::;
1464 1577
1465use List::Util qw(max sum); 1578use List::Util qw(max sum);
1466 1579
1467use CFPlus::OpenGL; 1580use CFPlus::OpenGL;
1468 1581
1469sub new { 1582sub new {
1470 my $class = shift; 1583 my $class = shift;
1471 1584
1472 $class->SUPER::new ( 1585 $class->SUPER::new (
1473 col_expand => [], 1586 col_expand => [],
1587 row_expand => [],
1474 @_, 1588 @_,
1475 ) 1589 )
1476} 1590}
1477 1591
1478sub children {
1479 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1480}
1481
1482sub add { 1592sub add {
1593 my ($self, @widgets) = @_;
1594
1595 for my $child (@widgets) {
1596 $child->{c_rowspan} ||= 1;
1597 $child->{c_colspan} ||= 1;
1598 }
1599
1600 $self->SUPER::add (@widgets);
1601}
1602
1603sub add_at {
1483 my ($self) = shift; 1604 my $self = shift;
1605
1606 my @widgets;
1484 1607
1485 while (@_) { 1608 while (@_) {
1486 my ($x, $y, $child) = splice @_, 0, 3, (); 1609 my ($col, $row, $child) = splice @_, 0, 3, ();
1487 $child->set_parent ($self);
1488 $self->{children}[$y][$x] = $child;
1489 }
1490 1610
1491 $self->{force_realloc} = 1; 1611 $child->{c_row} = $row;
1492 $self->{force_size_alloc} = 1; 1612 $child->{c_col} = $col;
1493 $self->realloc;
1494}
1495 1613
1496sub remove { 1614 push @widgets, $child;
1615 }
1616
1617 $self->add (@widgets);
1618}
1619
1620sub get_wh {
1497 my ($self, $child) = @_; 1621 my ($self) = @_;
1498 1622
1499 # TODO: not yet implemented 1623 my (@w, @h);
1500}
1501
1502# TODO: move to container class maybe? send children a signal on removal?
1503sub clear {
1504 my ($self) = @_;
1505 1624
1506 my @children = $self->children; 1625 my @children = $self->children;
1507 delete $self->{children}; 1626
1627 # first pass, columns
1628 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1629 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1630
1631 my $sw = sum @w[$c .. $c + $cs - 1];
1632
1633 if ($w > $sw) {
1634 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1635 }
1508 1636 }
1509 for (@children) {
1510 delete $_->{parent};
1511 $_->hide;
1512 }
1513 1637
1514 $self->realloc; 1638 # second pass, rows
1515} 1639 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1516
1517sub get_wh {
1518 my ($self) = @_;
1519
1520 my (@w, @h);
1521
1522 for my $y (0 .. $#{$self->{children}}) {
1523 my $row = $self->{children}[$y]
1524 or next;
1525
1526 for my $x (0 .. $#$row) {
1527 my $widget = $row->[$x]
1528 or next;
1529 my ($w, $h) = @$widget{qw(req_w req_h)}; 1640 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1530 1641
1531 $w[$x] = max $w[$x], $w; 1642 my $sh = sum @h[$r .. $r + $rs - 1];
1532 $h[$y] = max $h[$y], $h; 1643
1644 if ($h > $sh) {
1645 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1533 } 1646 }
1534 } 1647 }
1535 1648
1536 (\@w, \@h) 1649 (\@w, \@h)
1537} 1650}
1553 my ($ws, $hs) = $self->get_wh; 1666 my ($ws, $hs) = $self->get_wh;
1554 1667
1555 my $req_w = (sum @$ws) || 1; 1668 my $req_w = (sum @$ws) || 1;
1556 my $req_h = (sum @$hs) || 1; 1669 my $req_h = (sum @$hs) || 1;
1557 1670
1558 # TODO: nicer code && do row_expand 1671 # now linearly scale the rows/columns to the allocated size
1559 my @col_expand = @{$self->{col_expand}}; 1672 my @col_expand = @{$self->{col_expand}};
1560 @col_expand = (1) x @$ws unless @col_expand; 1673 @col_expand = (1) x @$ws unless @col_expand;
1561 my $col_expand = (sum @col_expand) || 1; 1674 my $col_expand = (sum @col_expand) || 1;
1562 1675
1563 # linearly scale sizes
1564 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1676 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1565 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1566 1677
1567 CFPlus::UI::harmonize $ws; 1678 CFPlus::UI::harmonize $ws;
1679
1680 my @row_expand = @{$self->{row_expand}};
1681 @row_expand = (1) x @$ws unless @row_expand;
1682 my $row_expand = (sum @row_expand) || 1;
1683
1684 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1685
1568 CFPlus::UI::harmonize $hs; 1686 CFPlus::UI::harmonize $hs;
1569 1687
1570 my $y; 1688 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1689 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1571 1690
1572 for my $r (0 .. $#{$self->{children}}) { 1691 for my $widget ($self->children) {
1573 my $row = $self->{children}[$r] 1692 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1574 or next;
1575 1693
1576 my $x = 0; 1694 $widget->configure (
1577 my $row_h = $hs->[$r]; 1695 $x[$c], $y[$r],
1696 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1578 1697 );
1579 for my $c (0 .. $#$row) { 1698 }
1580 my $col_w = $ws->[$c];
1581 1699
1582 if (my $widget = $row->[$c]) { 1700 1
1583 $widget->configure ($x, $y, $col_w, $row_h); 1701}
1584 }
1585 1702
1586 $x += $col_w; 1703#############################################################################
1704
1705package CFPlus::UI::Fixed;
1706
1707use List::Util qw(min max);
1708
1709our @ISA = CFPlus::UI::Container::;
1710
1711sub _scale($$$) {
1712 my ($rel, $val, $max) = @_;
1713
1714 $rel ? $val * $max : $val
1715}
1716
1717sub size_request {
1718 my ($self) = @_;
1719
1720 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1721
1722 # determine overall size by querying abs widgets
1723 for my $child ($self->visible_children) {
1724 unless ($child->{c_rel}) {
1725 my $x = $child->{c_x};
1726 my $y = $child->{c_y};
1727
1728 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1729 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1587 } 1730 }
1731 }
1588 1732
1589 $y += $row_h; 1733 my $W = $x2 - $x1;
1734 my $H = $y2 - $y1;
1735
1736 # now layout remaining widgets
1737 for my $child ($self->visible_children) {
1738 if ($child->{c_rel}) {
1739 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1740 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1741
1742 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1743 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1744 }
1745 }
1746
1747 my $W = $x2 - $x1;
1748 my $H = $y2 - $y1;
1749
1750 ($W, $H)
1751}
1752
1753sub invoke_size_allocate {
1754 my ($self, $W, $H) = @_;
1755
1756 for my $child ($self->visible_children) {
1757 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1758 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1759
1760 $x += $child->{c_halign} * $child->{req_w};
1761 $y += $child->{c_valign} * $child->{req_h};
1762
1763 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1590 } 1764 }
1591 1765
1592 1 1766 1
1593} 1767}
1594 1768
1595sub find_widget {
1596 my ($self, $x, $y) = @_;
1597
1598 $x -= $self->{x};
1599 $y -= $self->{y};
1600
1601 my $res;
1602
1603 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1604 $res = $_->find_widget ($x, $y)
1605 and return $res;
1606 }
1607
1608 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1609}
1610
1611sub _draw {
1612 my ($self) = @_;
1613
1614 for (grep $_, @{$self->{children}}) {
1615 $_->draw for grep $_, @$_;
1616 }
1617}
1618
1619############################################################################# 1769#############################################################################
1620 1770
1621package CFPlus::UI::Box; 1771package CFPlus::UI::Box;
1622 1772
1623our @ISA = CFPlus::UI::Container::; 1773our @ISA = CFPlus::UI::Container::;
1624 1774
1625sub size_request { 1775sub size_request {
1626 my ($self) = @_; 1776 my ($self) = @_;
1777
1778 my @children = $self->visible_children;
1627 1779
1628 $self->{vertical} 1780 $self->{vertical}
1629 ? ( 1781 ? (
1630 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1782 (List::Util::max map $_->{req_w}, @children),
1631 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1783 (List::Util::sum map $_->{req_h}, @children),
1632 ) 1784 )
1633 : ( 1785 : (
1634 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1786 (List::Util::sum map $_->{req_w}, @children),
1635 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1787 (List::Util::max map $_->{req_h}, @children),
1636 ) 1788 )
1637} 1789}
1638 1790
1639sub invoke_size_allocate { 1791sub invoke_size_allocate {
1640 my ($self, $w, $h) = @_; 1792 my ($self, $w, $h) = @_;
1764 1916
1765 delete $self->{ox}; 1917 delete $self->{ox};
1766 $self->SUPER::realloc; 1918 $self->SUPER::realloc;
1767} 1919}
1768 1920
1921sub clear {
1922 my ($self) = @_;
1923
1924 $self->set_text ("");
1925}
1926
1769sub set_text { 1927sub set_text {
1770 my ($self, $text) = @_; 1928 my ($self, $text) = @_;
1771 1929
1772 return if $self->{text} eq "T$text"; 1930 return if $self->{text} eq "T$text";
1773 $self->{text} = "T$text"; 1931 $self->{text} = "T$text";
1796 1954
1797sub size_request { 1955sub size_request {
1798 my ($self) = @_; 1956 my ($self) = @_;
1799 1957
1800 $self->{size_req} ||= do { 1958 $self->{size_req} ||= do {
1959 my ($max_w, $max_h) = $self->get_max_wh;
1960
1801 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1961 $self->{layout}->set_font ($self->{font}) if $self->{font};
1802 $self->{layout}->set_width ($self->{max_w} || -1); 1962 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1803 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1963 $self->{layout}->set_ellipsise ($self->{ellipsise});
1804 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1964 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1805 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1965 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1806 1966
1807 my ($w, $h) = $self->{layout}->size; 1967 my ($w, $h) = $self->{layout}->size;
1879 : ($self->{w} - $size->[0]) * 0.5); 2039 : ($self->{w} - $size->[0]) * 0.5);
1880 2040
1881 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2041 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1882 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2042 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1883 : ($self->{h} - $size->[1]) * 0.5); 2043 : ($self->{h} - $size->[1]) * 0.5);
2044
2045 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1884 }; 2046 };
1885 2047
1886 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2048# unless ($self->{list}) {
1887 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2049# $self->{list} = CFPlus::OpenGL::glGenList;
1888 2050# CFPlus::OpenGL::glNewList $self->{list};
1889 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2051# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2052# CFPlus::OpenGL::glEndList;
2053# }
2054#
2055# CFPlus::OpenGL::glCallList $self->{list};
2056
2057 $self->{layout}->draw;
1890} 2058}
2059
2060#sub destroy {
2061# my ($self) = @_;
2062#
2063# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2064#
2065# $self->SUPER::destroy;
2066#}
1891 2067
1892############################################################################# 2068#############################################################################
1893 2069
1894package CFPlus::UI::EntryBase; 2070package CFPlus::UI::EntryBase;
1895 2071
1901 my $class = shift; 2077 my $class = shift;
1902 2078
1903 $class->SUPER::new ( 2079 $class->SUPER::new (
1904 fg => [1, 1, 1], 2080 fg => [1, 1, 1],
1905 bg => [0, 0, 0, 0.2], 2081 bg => [0, 0, 0, 0.2],
2082 outline => [0.6, 0.3, 0.1],
1906 active_bg => [1, 1, 1, 0.5], 2083 active_bg => [0, 0, 1, .2],
1907 active_fg => [0, 0, 0], 2084 active_fg => [1, 1, 1],
2085 active_outline => [1, 1, 0],
1908 can_hover => 1, 2086 can_hover => 1,
1909 can_focus => 1, 2087 can_focus => 1,
1910 valign => 0, 2088 valign => 0,
1911 can_events => 1, 2089 can_events => 1,
1912 ellipsise => 0, 2090 ellipsise => 0,
1962 my $sym = $ev->{sym}; 2140 my $sym = $ev->{sym};
1963 my $uni = $ev->{unicode}; 2141 my $uni = $ev->{unicode};
1964 2142
1965 my $text = $self->get_text; 2143 my $text = $self->get_text;
1966 2144
2145 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2146
1967 if ($uni == 8) { 2147 if ($uni == 8) {
1968 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1969 } elsif ($uni == 127) { 2149 } elsif ($uni == 127) {
1970 substr $text, $self->{cursor}, 1, ""; 2150 substr $text, $self->{cursor}, 1, "";
1971 } elsif ($sym == CFPlus::SDLK_LEFT) { 2151 } elsif ($sym == CFPlus::SDLK_LEFT) {
1972 --$self->{cursor} if $self->{cursor}; 2152 --$self->{cursor} if $self->{cursor};
1973 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2153 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1974 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2154 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1975 } elsif ($sym == CFPlus::SDLK_HOME) { 2155 } elsif ($sym == CFPlus::SDLK_HOME) {
2156 # what a hack
1976 $self->{cursor} = 0; 2157 $self->{cursor} =
2158 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2159 ? length $1
2160 : 0;
1977 } elsif ($sym == CFPlus::SDLK_END) { 2161 } elsif ($sym == CFPlus::SDLK_END) {
2162 # uh, again
2163 $self->{cursor} =
2164 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
1978 $self->{cursor} = length $text; 2165 ? $self->{cursor} + length $1
2166 : length $self->{text};
1979 } elsif ($uni == 21) { # ctrl-u 2167 } elsif ($uni == 21) { # ctrl-u
1980 $text = ""; 2168 $text = "";
1981 $self->{cursor} = 0; 2169 $self->{cursor} = 0;
1982 } elsif ($uni == 27) { 2170 } elsif ($uni == 27) {
1983 $self->emit ('escape'); 2171 $self->emit ('escape');
1984 } elsif ($uni >= 0x20 || $uni == 0x0d) { 2172 } elsif ($uni == 0x0d) {
2173 substr $text, $self->{cursor}++, 0, "\012";
2174 } elsif ($uni >= 0x20) {
1985 substr $text, $self->{cursor}++, 0, chr $uni; 2175 substr $text, $self->{cursor}++, 0, chr $uni;
1986 } else { 2176 } else {
1987 return 0; 2177 return 0;
1988 } 2178 }
1989 2179
2040 glColor_premultiply @{$self->{bg}}; 2230 glColor_premultiply @{$self->{bg}};
2041 } 2231 }
2042 2232
2043 glEnable GL_BLEND; 2233 glEnable GL_BLEND;
2044 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2234 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2045 glBegin GL_QUADS;
2046 glVertex 0 , 0;
2047 glVertex 0 , $self->{h};
2048 glVertex $self->{w}, $self->{h}; 2235 glRect 0, 0, $self->{w}, $self->{h};
2049 glVertex $self->{w}, 0;
2050 glEnd;
2051 glDisable GL_BLEND; 2236 glDisable GL_BLEND;
2052 2237
2053 $self->SUPER::_draw; 2238 $self->SUPER::_draw;
2054 2239
2055 #TODO: force update every cursor change :( 2240 #TODO: force update every cursor change :(
2057 2242
2058 unless (exists $self->{cur_h}) { 2243 unless (exists $self->{cur_h}) {
2059 my $text = substr $self->{text}, 0, $self->{cursor}; 2244 my $text = substr $self->{text}, 0, $self->{cursor};
2060 utf8::encode $text; 2245 utf8::encode $text;
2061 2246
2062 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2247 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2063 } 2248 }
2064 2249
2250 glColor_premultiply @{$self->{active_fg}};
2065 glBegin GL_LINES; 2251 glBegin GL_LINES;
2066 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2252 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2067 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2253 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2254 glEnd;
2255
2256 glLineWidth 3;
2257 glColor @{$self->{active_outline}};
2258 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2259 glLineWidth 1;
2260
2261 } else {
2262 glColor @{$self->{outline}};
2263 glBegin GL_LINE_STRIP;
2264 glVertex .5, $self->{h} * .5;
2265 glVertex .5, $self->{h} - 2.5;
2266 glVertex $self->{w} - .5, $self->{h} - 2.5;
2267 glVertex $self->{w} - .5, $self->{h} * .5;
2068 glEnd; 2268 glEnd;
2069 } 2269 }
2070} 2270}
2071 2271
2072############################################################################# 2272#############################################################################
2159 1 2359 1
2160} 2360}
2161 2361
2162############################################################################# 2362#############################################################################
2163 2363
2164package CFPlus::UI::Button; 2364package CFPlus::UI::ButtonBin;
2165 2365
2166our @ISA = CFPlus::UI::Label::; 2366our @ISA = CFPlus::UI::Bin::;
2167 2367
2168use CFPlus::OpenGL; 2368use CFPlus::OpenGL;
2169 2369
2170my @tex = 2370my @tex =
2171 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2371 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2172 qw(b1_button_active.png); 2372 qw(b1_button_inactive.png b1_button_active.png);
2173 2373
2174sub new { 2374sub new {
2175 my $class = shift; 2375 my $class = shift;
2176 2376
2177 $class->SUPER::new ( 2377 $class->SUPER::new (
2178 padding_x => 4,
2179 padding_y => 4,
2180 fg => [1, 1, 1],
2181 active_fg => [0, 0, 1],
2182 can_hover => 1, 2378 can_hover => 1,
2183 align => 0, 2379 align => 0,
2184 valign => 0, 2380 valign => 0,
2185 can_events => 1, 2381 can_events => 1,
2186 @_ 2382 @_
2198} 2394}
2199 2395
2200sub _draw { 2396sub _draw {
2201 my ($self) = @_; 2397 my ($self) = @_;
2202 2398
2203 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2204
2205 glEnable GL_TEXTURE_2D; 2399 glEnable GL_TEXTURE_2D;
2206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2400 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2207 glColor 0, 0, 0, 1; 2401 glColor 0, 0, 0, 1;
2208 2402
2403 my $tex = $tex[$GRAB == $self];
2209 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2404 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2405
2406 glDisable GL_TEXTURE_2D;
2407
2408 $self->SUPER::_draw;
2409}
2410
2411#############################################################################
2412
2413package CFPlus::UI::Button;
2414
2415our @ISA = CFPlus::UI::Label::;
2416
2417use CFPlus::OpenGL;
2418
2419my @tex =
2420 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2421 qw(b1_button_inactive.png b1_button_active.png);
2422
2423sub new {
2424 my $class = shift;
2425
2426 $class->SUPER::new (
2427 padding_x => 4,
2428 padding_y => 4,
2429 fg => [1.0, 1.0, 1.0],
2430 active_fg => [0.8, 0.8, 0.8],
2431 can_hover => 1,
2432 align => 0,
2433 valign => 0,
2434 can_events => 1,
2435 @_
2436 )
2437}
2438
2439sub invoke_button_up {
2440 my ($self, $ev, $x, $y) = @_;
2441
2442 $self->emit ("activate")
2443 if $x >= 0 && $x < $self->{w}
2444 && $y >= 0 && $y < $self->{h};
2445
2446 1
2447}
2448
2449sub _draw {
2450 my ($self) = @_;
2451
2452 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2453
2454 glEnable GL_TEXTURE_2D;
2455 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2456 glColor 0, 0, 0, 1;
2457
2458 my $tex = $tex[$GRAB == $self];
2459 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2210 2460
2211 glDisable GL_TEXTURE_2D; 2461 glDisable GL_TEXTURE_2D;
2212 2462
2213 $self->SUPER::_draw; 2463 $self->SUPER::_draw;
2214} 2464}
2271sub _draw { 2521sub _draw {
2272 my ($self) = @_; 2522 my ($self) = @_;
2273 2523
2274 $self->SUPER::_draw; 2524 $self->SUPER::_draw;
2275 2525
2276 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2526 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2277 2527
2278 my ($w, $h) = @$self{qw(w h)}; 2528 my ($w, $h) = @$self{qw(w h)};
2279 2529
2280 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2530 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2281 2531
2301sub new { 2551sub new {
2302 my $class = shift; 2552 my $class = shift;
2303 2553
2304 my $self = $class->SUPER::new ( 2554 my $self = $class->SUPER::new (
2305 can_events => 0, 2555 can_events => 0,
2556 scale => 1,
2306 @_, 2557 @_,
2307 ); 2558 );
2308 2559
2309 $self->{path} || $self->{tex} 2560 $self->{path} || $self->{tex}
2310 or Carp::croak "'path' or 'tex' attributes required"; 2561 or Carp::croak "'path' or 'tex' attributes required";
2311 2562
2312 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2563 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2313 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2564 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2314 2565
2315 Scalar::Util::weaken $texture_cache{$self->{path}}; 2566 CFPlus::weaken $texture_cache{$self->{path}};
2316 2567
2317 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2568 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2318 2569
2319 $self 2570 $self
2320} 2571}
2335} 2586}
2336 2587
2337sub size_request { 2588sub size_request {
2338 my ($self) = @_; 2589 my ($self) = @_;
2339 2590
2340 ($self->{tex}{w}, $self->{tex}{h}) 2591 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2341} 2592}
2342 2593
2343sub _draw { 2594sub _draw {
2344 my ($self) = @_; 2595 my ($self) = @_;
2345 2596
2355 } 2606 }
2356 2607
2357 glEnable GL_TEXTURE_2D; 2608 glEnable GL_TEXTURE_2D;
2358 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2609 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2359 2610
2360 $tex->draw_quad (0, 0, $w, $h); 2611 $tex->draw_quad_alpha (0, 0, $w, $h);
2361 2612
2362 glDisable GL_TEXTURE_2D; 2613 glDisable GL_TEXTURE_2D;
2363} 2614}
2364 2615
2365############################################################################# 2616#############################################################################
2384 align => 0, 2635 align => 0,
2385 valign => 0, 2636 valign => 0,
2386 can_events => 1, 2637 can_events => 1,
2387 @_ 2638 @_
2388 ); 2639 );
2640}
2641
2642sub invoke_button_down {
2643 my ($self, $ev, $x, $y) = @_;
2644
2645 1
2389} 2646}
2390 2647
2391sub invoke_button_up { 2648sub invoke_button_up {
2392 my ($self, $ev, $x, $y) = @_; 2649 my ($self, $ev, $x, $y) = @_;
2393 2650
2533 glDisable GL_TEXTURE_2D; 2790 glDisable GL_TEXTURE_2D;
2534} 2791}
2535 2792
2536############################################################################# 2793#############################################################################
2537 2794
2795package CFPlus::UI::Progress;
2796
2797our @ISA = CFPlus::UI::Label::;
2798
2799use CFPlus::OpenGL;
2800
2801sub new {
2802 my ($class, %arg) = @_;
2803
2804 my $self = $class->SUPER::new (
2805 fg => [1, 1, 1],
2806 bg => [0, 0, 1, 0.2],
2807 bar => [0.7, 0.5, 0.1, 0.8],
2808 outline => [0.4, 0.3, 0],
2809 fontsize => 0.9,
2810 valign => 0,
2811 align => 0,
2812 can_events => 1,
2813 ellipsise => 1,
2814 label => "%d%%",
2815 %arg,
2816 );
2817
2818 $self->set_value ($arg{value} || -1);
2819
2820 $self
2821}
2822
2823sub set_label {
2824 my ($self, $label) = @_;
2825
2826 return if $self->{label} eq $label;
2827 $self->{label} = $label;
2828
2829 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2830}
2831
2832sub set_value {
2833 my ($self, $value) = @_;
2834
2835 if ($self->{value} ne $value) {
2836 $self->{value} = $value;
2837
2838 if ($value < 0) {
2839 $self->set_text ("-");
2840 } else {
2841 $self->set_text (sprintf $self->{label}, $value * 100);
2842 }
2843
2844 $self->update;
2845 }
2846}
2847
2848sub _draw {
2849 my ($self) = @_;
2850
2851 glEnable GL_BLEND;
2852 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2853
2854 if ($self->{value} >= 0) {
2855 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2856
2857 glColor_premultiply @{$self->{bar}};
2858 glRect 2, 2, $s, $self->{h} - 2;
2859 glColor_premultiply @{$self->{bg}};
2860 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2861 }
2862
2863 glColor_premultiply @{$self->{outline}};
2864 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2865
2866 glDisable GL_BLEND;
2867
2868 {
2869 local $self->{bg}; # do not draw background
2870 $self->SUPER::_draw;
2871 }
2872}
2873
2874#############################################################################
2875
2876package CFPlus::UI::ExperienceProgress;
2877
2878our @ISA = CFPlus::UI::Progress::;
2879
2880sub new {
2881 my ($class, %arg) = @_;
2882
2883 my $self = $class->SUPER::new (
2884 tooltip => sub {
2885 my ($self) = @_;
2886
2887 sprintf "level %d\n%s points\n%s next level\n%s to go",
2888 $self->{lvl},
2889 ::formsep ($self->{exp}),
2890 ::formsep ($self->{nxt}),
2891 ::formsep ($self->{nxt} - $self->{exp}),
2892 },
2893 %arg
2894 );
2895
2896 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2897 if $::CONN;
2898
2899 $self
2900}
2901
2902sub DESTROY {
2903 my ($self) = @_;
2904
2905 delete $::CONN->{on_exp_update}{$self+0}
2906 if $::CONN;
2907
2908 $self->SUPER::DESTROY;
2909}
2910
2911sub set_value {
2912 my ($self, $lvl, $exp) = @_;
2913
2914 $self->{lvl} = $lvl;
2915 $self->{exp} = $exp;
2916
2917 my $v = -1;
2918
2919 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2920 my $l0 = $table->[$lvl - 1];
2921 my $l1 = $table->[$lvl];
2922
2923 $self->{nxt} = $l1;
2924
2925 $v = ($exp - $l0) / ($l1 - $l0);
2926 }
2927
2928 $self->SUPER::set_value ($v);
2929}
2930
2931#############################################################################
2932
2538package CFPlus::UI::Gauge; 2933package CFPlus::UI::Gauge;
2539 2934
2540our @ISA = CFPlus::UI::VBox::; 2935our @ISA = CFPlus::UI::VBox::;
2541 2936
2542sub new { 2937sub new {
2667 3062
2668 $self->SUPER::invoke_button_down ($ev, $x, $y); 3063 $self->SUPER::invoke_button_down ($ev, $x, $y);
2669 3064
2670 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3065 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2671 3066
2672 $self->invoke_mouse_motion ($ev, $x, $y) 3067 $self->invoke_mouse_motion ($ev, $x, $y);
3068
3069 1
2673} 3070}
2674 3071
2675sub invoke_mouse_motion { 3072sub invoke_mouse_motion {
2676 my ($self, $ev, $x, $y) = @_; 3073 my ($self, $ev, $x, $y) = @_;
2677 3074
2693sub invoke_mouse_wheel { 3090sub invoke_mouse_wheel {
2694 my ($self, $ev) = @_; 3091 my ($self, $ev) = @_;
2695 3092
2696 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3093 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2697 3094
3095 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
3096
2698 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 3097 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2699 3098
2700 ! ! $delta 3099 1
2701} 3100}
2702 3101
2703sub update { 3102sub update {
2704 my ($self) = @_; 3103 my ($self) = @_;
2705 3104
2813 #font => default_font 3212 #font => default_font
2814 @_, 3213 @_,
2815 3214
2816 layout => (new CFPlus::Layout), 3215 layout => (new CFPlus::Layout),
2817 par => [], 3216 par => [],
3217 max_par => 0,
2818 height => 0, 3218 height => 0,
2819 children => [ 3219 children => [
2820 (new CFPlus::UI::Empty expand => 1), 3220 (new CFPlus::UI::Empty expand => 1),
2821 (new CFPlus::UI::Slider vertical => 1), 3221 (new CFPlus::UI::Slider vertical => 1),
2822 ], 3222 ],
2835} 3235}
2836 3236
2837sub size_request { 3237sub size_request {
2838 my ($self) = @_; 3238 my ($self) = @_;
2839 3239
2840 my ($empty, $slider) = @{ $self->{children} }; 3240 my ($empty, $slider) = $self->visible_children;
2841 3241
2842 local $self->{children} = [$empty, $slider]; 3242 local $self->{children} = [$empty, $slider];
2843 $self->SUPER::size_request 3243 $self->SUPER::size_request
2844} 3244}
2845 3245
2947 3347
2948 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 3348 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2949 push @{$self->{par}}, $para; 3349 push @{$self->{par}}, $para;
2950 } 3350 }
2951 3351
3352 if (my $max = $self->{max_par}) {
3353 shift @{$self->{par}} while @{$self->{par}} > $max;
3354 }
3355
2952 $self->{need_reflow}++; 3356 $self->{need_reflow}++;
2953 $self->update; 3357 $self->update;
2954} 3358}
2955 3359
2956sub scroll_to_bottom { 3360sub scroll_to_bottom {
3030 3434
3031 if ($y0 < $y + $h && $y < $y1) { 3435 if ($y0 < $y + $h && $y < $y1) {
3032 my $layout = $self->get_layout ($para); 3436 my $layout = $self->get_layout ($para);
3033 3437
3034 $layout->render ($para->{indent}, $y - $y0); 3438 $layout->render ($para->{indent}, $y - $y0);
3439 $layout->draw;
3035 3440
3036 if (my @w = @{ $para->{widget} }) { 3441 if (my @w = @{ $para->{widget} }) {
3037 my @s = $layout->get_shapes; 3442 my @s = $layout->get_shapes;
3038 3443
3039 for (@w) { 3444 for (@w) {
3161} 3566}
3162 3567
3163sub set_tooltip_from { 3568sub set_tooltip_from {
3164 my ($self, $widget) = @_; 3569 my ($self, $widget) = @_;
3165 3570
3166 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3167 if $widget->{tooltip} =~ /^#(.*)$/;
3168
3169 my $tooltip = $widget->{tooltip}; 3571 my $tip = $widget->{tooltip};
3572 $tip = $tip->($widget) if "CODE" eq ref $tip;
3573
3574 $tip = CFPlus::Pod::section_label tooltip => $1
3575 if $tip =~ /^#(.*)$/;
3170 3576
3171 if ($ENV{CFPLUS_DEBUG} & 2) { 3577 if ($ENV{CFPLUS_DEBUG} & 2) {
3172 $tooltip .= "\n\n" . (ref $widget) . "\n" 3578 $tip .= "\n\n" . (ref $widget) . "\n"
3173 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3579 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3174 . "req $widget->{req_w} $widget->{req_h}\n" 3580 . "req $widget->{req_w} $widget->{req_h}\n"
3175 . "visible $widget->{visible}"; 3581 . "visible $widget->{visible}";
3176 } 3582 }
3177 3583
3178 $tooltip =~ s/^\n+//; 3584 $tip =~ s/^\n+//;
3179 $tooltip =~ s/\n+$//; 3585 $tip =~ s/\n+$//;
3180 3586
3181 $self->add (new CFPlus::UI::Label 3587 $self->add (new CFPlus::UI::Label
3182 markup => $tooltip, 3588 markup => $tip,
3183 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3589 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3184 fontsize => 0.8, 3590 fontsize => 0.8,
3185 style => 1, # FLAG_INVERSE 3591 style => 1, # FLAG_INVERSE
3186 ellipsise => 0, 3592 ellipsise => 0,
3187 font => ($widget->{tooltip_font} || $::FONT_PROP), 3593 font => ($widget->{tooltip_font} || $::FONT_PROP),
3225} 3631}
3226 3632
3227sub _draw { 3633sub _draw {
3228 my ($self) = @_; 3634 my ($self) = @_;
3229 3635
3230 glTranslate 0.375, 0.375;
3231
3232 my ($w, $h) = @$self{qw(w h)}; 3636 my ($w, $h) = @$self{qw(w h)};
3233 3637
3234 glColor 1, 0.8, 0.4; 3638 glColor 1, 0.8, 0.4;
3235 glBegin GL_QUADS; 3639 glRect 0, 0, $w, $h;
3236 glVertex 0 , 0;
3237 glVertex 0 , $h;
3238 glVertex $w, $h;
3239 glVertex $w, 0;
3240 glEnd;
3241 3640
3242 glColor 0, 0, 0; 3641 glColor 0, 0, 0;
3243 glBegin GL_LINE_LOOP; 3642 glRect_lineloop .5, .5, $w + .5, $h + .5;
3244 glVertex 0 , 0;
3245 glVertex 0 , $h;
3246 glVertex $w, $h;
3247 glVertex $w, 0;
3248 glEnd;
3249 3643
3250 glTranslate 2 - 0.375, 2 - 0.375; 3644 glTranslate 2, 2;
3251 3645
3252 $self->SUPER::_draw; 3646 $self->SUPER::_draw;
3253} 3647}
3254 3648
3255############################################################################# 3649#############################################################################
3262 3656
3263sub new { 3657sub new {
3264 my $class = shift; 3658 my $class = shift;
3265 3659
3266 my $self = $class->SUPER::new ( 3660 my $self = $class->SUPER::new (
3661 size_w => 32,
3662 size_h => 8,
3267 aspect => 1, 3663 aspect => 1,
3268 can_events => 0, 3664 can_events => 0,
3269 @_, 3665 @_,
3270 ); 3666 );
3271 3667
3272 if ($self->{anim} && $self->{animspeed}) { 3668 if ($self->{anim} && $self->{animspeed}) {
3273 Scalar::Util::weaken (my $widget = $self); 3669 CFPlus::weaken (my $widget = $self);
3274 3670
3671 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3275 $self->{timer} = Event->timer ( 3673 $self->{timer} = Event->timer (
3276 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3277 hard => 1, 3674 parked => 1,
3278 interval => $self->{animspeed},
3279 cb => sub { 3675 cb => sub {
3676 return unless $::CONN;
3677
3678 my $w = $widget
3679 or return;
3680
3280 ++$widget->{frame}; 3681 ++$w->{frame};
3682 $w->update_face;
3683
3684 # somehow, $widget can go away
3281 $widget->update; 3685 $w->update;
3686 $w->update_timer;
3282 }, 3687 },
3283 ); 3688 );
3689
3690 $self->update_face;
3691 $self->update_timer;
3284 } 3692 }
3285 3693
3286 $self 3694 $self
3287} 3695}
3288 3696
3697sub update_timer {
3698 my ($self) = @_;
3699
3700 return unless $self->{timer};
3701
3702 if ($self->{visible}) {
3703 $self->{timer}->at (
3704 $self->{anim_start}
3705 + $self->{animspeed}
3706 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3707 );
3708 $self->{timer}->start;
3709 } else {
3710 $self->{timer}->stop;
3711 }
3712}
3713
3714sub update_face {
3715 my ($self) = @_;
3716
3717 if ($::CONN) {
3718 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3719 if ($anim && @$anim) {
3720 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3721 delete $self->{face_change_cb};
3722
3723 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3724 unless ($tex->{name} || $tex->{loading}) {
3725 $tex->upload (sub { $self->reconfigure });
3726 }
3727 }
3728 }
3729 }
3730 }
3731}
3732
3289sub size_request { 3733sub size_request {
3290 (32, 8) 3734 my ($self) = @_;
3735
3736 if ($::CONN) {
3737 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3738 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3739 if ($tex->{name}) {
3740 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3741 } elsif (!$tex->{loading}) {
3742 $tex->upload (sub { $self->reconfigure });
3743 }
3744 }
3745
3746 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3747 }
3748 }
3749
3750 ($self->{size_w} || 8, $self->{size_h} || 8)
3291} 3751}
3292 3752
3293sub update { 3753sub update {
3294 my ($self) = @_; 3754 my ($self) = @_;
3295 3755
3296 return unless $self->{visible}; 3756 return unless $self->{visible};
3297 3757
3298 $self->SUPER::update; 3758 $self->SUPER::update;
3299} 3759}
3300 3760
3761sub invoke_visibility_change {
3762 my ($self) = @_;
3763
3764 $self->update_timer;
3765
3766 0
3767}
3768
3301sub _draw { 3769sub _draw {
3302 my ($self) = @_; 3770 my ($self) = @_;
3303 3771
3304 return unless $::CONN;
3305
3306 $self->SUPER::_draw; 3772 $self->SUPER::_draw;
3307 3773
3308 my $face; 3774 if (my $tex = $self->{tex}) {
3309
3310 if ($self->{frame}) {
3311 my $anim = $::CONN->{anim}[$self->{anim}];
3312
3313 $face = $anim->[ $self->{frame} % @$anim ]
3314 if $anim && @$anim;
3315 }
3316
3317 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3318
3319 if ($tex) {
3320 glEnable GL_TEXTURE_2D; 3775 glEnable GL_TEXTURE_2D;
3321 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3776 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3322 glColor 0, 0, 0, 1; 3777 glColor 0, 0, 0, 1;
3323 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3778 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3324 glDisable GL_TEXTURE_2D; 3779 glDisable GL_TEXTURE_2D;
3326} 3781}
3327 3782
3328sub destroy { 3783sub destroy {
3329 my ($self) = @_; 3784 my ($self) = @_;
3330 3785
3331 $self->{timer}->cancel 3786 (delete $self->{timer})->cancel
3332 if $self->{timer}; 3787 if $self->{timer};
3333 3788
3334 $self->SUPER::destroy; 3789 $self->SUPER::destroy;
3335} 3790}
3336 3791
3376 children => [ 3831 children => [
3377 (new CFPlus::UI::Label markup => $left, expand => 1), 3832 (new CFPlus::UI::Label markup => $left, expand => 1),
3378 (new CFPlus::UI::Label markup => $right, align => +1), 3833 (new CFPlus::UI::Label markup => $right, align => +1),
3379 ], 3834 ],
3380 ; 3835 ;
3381 3836
3382 } else { 3837 } else {
3383 $widget = new CFPlus::UI::Label 3838 $widget = new CFPlus::UI::Label
3384 can_hover => 1, 3839 can_hover => 1,
3385 can_events => 1, 3840 can_events => 1,
3386 markup => $widget, 3841 markup => $widget,
3511 3966
3512############################################################################# 3967#############################################################################
3513 3968
3514package CFPlus::UI::Notebook; 3969package CFPlus::UI::Notebook;
3515 3970
3971use CFPlus::OpenGL;
3972
3516our @ISA = CFPlus::UI::VBox::; 3973our @ISA = CFPlus::UI::VBox::;
3517 3974
3518sub new { 3975sub new {
3519 my $class = shift; 3976 my $class = shift;
3520 3977
3521 my $self = $class->SUPER::new ( 3978 my $self = $class->SUPER::new (
3522 buttonbar => (new CFPlus::UI::Buttonbar), 3979 buttonbar => (new CFPlus::UI::Buttonbar),
3523 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3980 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3981 active_outline => [.7, .7, 0.2],
3524 # filter => # will be put between multiplexer and $self 3982 # filter => # will be put between multiplexer and $self
3525 @_, 3983 @_,
3526 ); 3984 );
3527 3985
3528 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3986 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3529 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3987 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3530 3988
3989 {
3990 Scalar::Util::weaken (my $wself = $self);
3991
3992 $self->{multiplexer}->connect (c_add => sub {
3993 my ($mplex, $widgets) = @_;
3994
3995 for my $child (@$widgets) {
3996 Scalar::Util::weaken $child;
3997 $child->{c_tab_} ||= do {
3998 my $tab =
3999 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
4000 ? $child->{c_tab}
4001 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4002
4003 $tab->connect (activate => sub {
4004 $wself->set_current_page ($child);
4005 });
4006
4007 $tab
4008 };
4009
4010 $self->{buttonbar}->add ($child->{c_tab_});
4011 }
4012 });
4013
4014 $self->{multiplexer}->connect (c_remove => sub {
4015 my ($mplex, $widgets) = @_;
4016
4017 for my $child (@$widgets) {
4018 $wself->{buttonbar}->remove ($child->{c_tab_});
4019 }
4020 });
4021 }
4022
3531 $self 4023 $self
3532} 4024}
3533 4025
3534sub add { 4026sub add {
4027 my ($self, @widgets) = @_;
4028
4029 $self->{multiplexer}->add (@widgets)
4030}
4031
4032sub remove {
4033 my ($self, @widgets) = @_;
4034
4035 $self->{multiplexer}->remove (@widgets)
4036}
4037
4038sub pages {
4039 my ($self) = @_;
4040 $self->{multiplexer}->children
4041}
4042
4043sub add_tab {
3535 my ($self, $title, $widget, $tooltip) = @_; 4044 my ($self, $title, $widget, $tooltip) = @_;
3536 4045
3537 Scalar::Util::weaken $self; 4046 $title = [$title, $tooltip] unless ref $title;
4047 $widget->{c_tab} = $title;
3538 4048
3539 $self->{buttonbar}->add (new CFPlus::UI::Button
3540 markup => $title,
3541 tooltip => $tooltip,
3542 on_activate => sub { $self->set_current_page ($widget) },
3543 );
3544
3545 $self->{multiplexer}->add ($widget); 4049 $self->add ($widget);
3546} 4050}
3547 4051
3548sub get_current_page { 4052sub get_current_page {
3549 my ($self) = @_; 4053 my ($self) = @_;
3550 4054
3554sub set_current_page { 4058sub set_current_page {
3555 my ($self, $page) = @_; 4059 my ($self, $page) = @_;
3556 4060
3557 $self->{multiplexer}->set_current_page ($page); 4061 $self->{multiplexer}->set_current_page ($page);
3558 $self->emit (page_changed => $self->{multiplexer}{current}); 4062 $self->emit (page_changed => $self->{multiplexer}{current});
4063}
4064
4065sub _draw {
4066 my ($self) = @_;
4067
4068 $self->SUPER::_draw ();
4069
4070 if (my $cur = $self->{multiplexer}{current}) {
4071 if ($cur = $cur->{c_tab_}) {
4072 glTranslate $self->{buttonbar}{x} + $cur->{x},
4073 $self->{buttonbar}{y} + $cur->{y};
4074 glLineWidth 3;
4075 #glEnable GL_BLEND;
4076 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4077 glColor @{$self->{active_outline}};
4078 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4079 glLineWidth 1;
4080 #glDisable GL_BLEND;
4081 }
4082 }
3559} 4083}
3560 4084
3561############################################################################# 4085#############################################################################
3562 4086
3563package CFPlus::UI::Selector; 4087package CFPlus::UI::Selector;
3626 my $self = $class->SUPER::new ( 4150 my $self = $class->SUPER::new (
3627 fontsize => 0.8, 4151 fontsize => 0.8,
3628 @_, 4152 @_,
3629 ); 4153 );
3630 4154
3631 Scalar::Util::weaken (my $this = $self); 4155 CFPlus::weaken (my $this = $self);
3632 4156
3633 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4157 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3634 4158
3635 $self 4159 $self
3636} 4160}
3775 my $self = $class->SUPER::new ( 4299 my $self = $class->SUPER::new (
3776 visible => 1, 4300 visible => 1,
3777 @_, 4301 @_,
3778 ); 4302 );
3779 4303
3780 Scalar::Util::weaken ($self->{root} = $self); 4304 CFPlus::weaken ($self->{root} = $self);
3781 4305
3782 $self 4306 $self
3783} 4307}
3784 4308
3785sub size_request { 4309sub size_request {
3833} 4357}
3834 4358
3835sub update { 4359sub update {
3836 my ($self) = @_; 4360 my ($self) = @_;
3837 4361
3838 $::WANT_REFRESH++; 4362 $::WANT_REFRESH->start;
3839} 4363}
3840 4364
3841sub add { 4365sub add {
3842 my ($self, @children) = @_; 4366 my ($self, @children) = @_;
3843 4367
3880 while ($self->{refresh_hook}) { 4404 while ($self->{refresh_hook}) {
3881 $_->() 4405 $_->()
3882 for values %{delete $self->{refresh_hook}}; 4406 for values %{delete $self->{refresh_hook}};
3883 } 4407 }
3884 4408
3885 if ($self->{realloc}) { 4409 while ($self->{realloc}) {
3886 my %queue; 4410 my %queue;
3887 my @queue; 4411 my @queue;
3888 my $widget; 4412 my $widget;
3889 4413
3890 outer: 4414 outer:
3937 } 4461 }
3938 } 4462 }
3939 4463
3940 delete $self->{realloc}{$widget+0}; 4464 delete $self->{realloc}{$widget+0};
3941 } 4465 }
3942 }
3943 4466
3944 while (my $size_alloc = delete $self->{size_alloc}) { 4467 while (my $size_alloc = delete $self->{size_alloc}) {
3945 my @queue = sort { $b->{visible} <=> $a->{visible} } 4468 my @queue = sort { $a->{visible} <=> $b->{visible} }
3946 values %$size_alloc; 4469 values %$size_alloc;
3947 4470
3948 while () { 4471 while () {
3949 my $widget = pop @queue || last; 4472 my $widget = pop @queue || last;
3950 4473
3951 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4474 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3952 4475
3953 $w = 0 if $w < 0;
3954 $h = 0 if $h < 0;
3955
3956 $w = max $widget->{min_w}, $w; 4476 $w = max $widget->{min_w}, $w;
3957 $h = max $widget->{min_h}, $h; 4477 $h = max $widget->{min_h}, $h;
3958 4478
3959# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4479# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
3960# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4480# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
3961 4481
3962 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4482 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3963 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4483 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3964 4484
3965 $w = int $w + 0.5; 4485 $w = int $w + 0.5;
3966 $h = int $h + 0.5; 4486 $h = int $h + 0.5;
3967 4487
3968 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4488 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3969 $widget->{old_w} = $widget->{w}; 4489 $widget->{old_w} = $widget->{w};
3970 $widget->{old_h} = $widget->{h}; 4490 $widget->{old_h} = $widget->{h};
3971 4491
3972 $widget->{w} = $w; 4492 $widget->{w} = $w;
3973 $widget->{h} = $h; 4493 $widget->{h} = $h;
3974 4494
3975 $widget->emit (size_allocate => $w, $h); 4495 $widget->emit (size_allocate => $w, $h);
4496 }
3976 } 4497 }
3977 } 4498 }
3978 } 4499 }
3979 4500
3980 while ($self->{post_alloc_hook}) { 4501 while ($self->{post_alloc_hook}) {
3981 $_->() 4502 $_->()
3982 for values %{delete $self->{post_alloc_hook}}; 4503 for values %{delete $self->{post_alloc_hook}};
3983 } 4504 }
3984
3985 4505
3986 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4506 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3987 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4507 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3988 glClear GL_COLOR_BUFFER_BIT; 4508 glClear GL_COLOR_BUFFER_BIT;
3989 4509

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines