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.341 by root, Sun Aug 13 03:20:56 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 }
262 264
263sub destroy { 265sub destroy {
264 my ($self) = @_; 266 my ($self) = @_;
265 267
266 $self->hide; 268 $self->hide;
269 $self->emit ("destroy");
267 %$self = (); 270 %$self = ();
271}
272
273sub TO_JSON {
274 { "\fw" => $_[0]{s_id} }
268} 275}
269 276
270sub show { 277sub show {
271 my ($self) = @_; 278 my ($self) = @_;
272 279
313sub set_visibility { 320sub set_visibility {
314 my ($self, $visible) = @_; 321 my ($self, $visible) = @_;
315 322
316 return if $self->{visible} == $visible; 323 return if $self->{visible} == $visible;
317 324
318 $visible ? $self->hide 325 $visible ? $self->show
319 : $self->show; 326 : $self->hide;
320} 327}
321 328
322sub toggle_visibility { 329sub toggle_visibility {
323 my ($self) = @_; 330 my ($self) = @_;
324 331
351 358
352 $self->{force_w} = $w; 359 $self->{force_w} = $w;
353 $self->{force_h} = $h; 360 $self->{force_h} = $h;
354 361
355 $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)
356} 376}
357 377
358sub size_request { 378sub size_request {
359 require Carp; 379 require Carp;
360 Carp::confess "size_request is abstract"; 380 Carp::confess "size_request is abstract";
502 522
503 defined wantarray and CFPlus::guard { 523 defined wantarray and CFPlus::guard {
504 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 524 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
505 @{ $self->{signal_cb}{$signal} }; 525 @{ $self->{signal_cb}{$signal} };
506 } 526 }
527}
528
529sub disconnect_all {
530 my ($self, $signal) = @_;
531
532 delete $self->{signal_cb}{$signal};
507} 533}
508 534
509my %has_coords = ( 535my %has_coords = (
510 button_down => 1, 536 button_down => 1,
511 button_up => 1, 537 button_up => 1,
514); 540);
515 541
516sub emit { 542sub emit {
517 my ($self, $signal, @args) = @_; 543 my ($self, $signal, @args) = @_;
518 544
519 # I do not really like this solution, but I dislike duplication 545 # I do not really like this solution, but I do not like duplication
520 # and needlessly verbose code, too. 546 # and needlessly verbose code, either.
521 my @append 547 my @append
522 = $has_coords{$signal} 548 = $has_coords{$signal}
523 ? $args[0]->xy ($self) 549 ? $args[0]->xy ($self)
524 : (); 550 : ();
525 551
526 #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#
527 553
528 #d##TODO# stop propagating at first true, do not use sum 554 for my $cb (
529 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 555 @{$self->{signal_cb}{$signal} || []}, # before
530 || ($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
531 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 562 $self->{parent} && $self->{parent}->emit ($signal, @args)
532} 563}
533 564
534sub find_widget { 565#sub find_widget {
535 my ($self, $x, $y) = @_; 566# in .xs
536
537 return () unless $self->{can_events};
538
539 return $self
540 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
541 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
542
543 ()
544}
545 567
546sub set_parent { 568sub set_parent {
547 my ($self, $parent) = @_; 569 my ($self, $parent) = @_;
548 570
549 Scalar::Util::weaken ($self->{parent} = $parent); 571 CFPlus::weaken ($self->{parent} = $parent);
550 $self->set_visible if $parent->{visible}; 572 $self->set_visible if $parent->{visible};
551} 573}
552 574
553sub realloc { 575sub realloc {
554 my ($self) = @_; 576 my ($self) = @_;
580 602
581# 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
582# functions seems pointless. 604# functions seems pointless.
583our ($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
584 606
585sub draw { 607#sub draw {
586 my ($self) = @_; 608#CFPlus.xs
587
588 return unless $self->{h} && $self->{w};
589
590 # update screen rectangle
591 local $draw_x = $draw_x + $self->{x};
592 local $draw_y = $draw_y + $self->{y};
593
594 # skip widgets that are entirely outside the drawing area
595 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
596 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
597
598 glPushMatrix;
599 glTranslate $self->{x}, $self->{y}, 0;
600
601 if ($self == $HOVER && $self->{can_hover}) {
602 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
603 glEnable GL_BLEND;
604 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
605 glBegin GL_QUADS;
606 glVertex 0 , 0;
607 glVertex $self->{w}, 0;
608 glVertex $self->{w}, $self->{h};
609 glVertex 0 , $self->{h};
610 glEnd;
611 glDisable GL_BLEND;
612 }
613
614 if ($ENV{CFPLUS_DEBUG} & 1) {
615 glPushMatrix;
616 glColor 1, 1, 0, 1;
617 glTranslate 0.375, 0.375;
618 glBegin GL_LINE_LOOP;
619 glVertex 0 , 0;
620 glVertex $self->{w} - 1, 0;
621 glVertex $self->{w} - 1, $self->{h} - 1;
622 glVertex 0 , $self->{h} - 1;
623 glEnd;
624 glPopMatrix;
625 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
626 }
627
628 $self->_draw;
629 glPopMatrix;
630}
631 609
632sub _draw { 610sub _draw {
633 my ($self) = @_; 611 my ($self) = @_;
634 612
635 warn "no draw defined for $self\n"; 613 warn "no draw defined for $self\n";
638sub DESTROY { 616sub DESTROY {
639 my ($self) = @_; 617 my ($self) = @_;
640 618
641 return if CFPlus::in_destruct; 619 return if CFPlus::in_destruct;
642 620
643 delete $WIDGET{$self+0}; 621 local $@;
644
645 eval { $self->destroy }; 622 eval { $self->destroy };
646 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 623 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
624
625 delete $WIDGET{$self+0};
647} 626}
648 627
649############################################################################# 628#############################################################################
650 629
651package CFPlus::UI::DrawBG; 630package CFPlus::UI::DrawBG;
655use strict; 634use strict;
656use CFPlus::OpenGL; 635use CFPlus::OpenGL;
657 636
658sub new { 637sub new {
659 my $class = shift; 638 my $class = shift;
660
661 # range [value, low, high, page]
662 639
663 $class->SUPER::new ( 640 $class->SUPER::new (
664 #bg => [0, 0, 0, 0.2], 641 #bg => [0, 0, 0, 0.2],
665 #active_bg => [1, 1, 1, 0.5], 642 #active_bg => [1, 1, 1, 0.5],
666 @_ 643 @_
678 my ($w, $h) = @$self{qw(w h)}; 655 my ($w, $h) = @$self{qw(w h)};
679 656
680 glEnable GL_BLEND; 657 glEnable GL_BLEND;
681 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 658 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
682 glColor_premultiply @$color; 659 glColor_premultiply @$color;
683
684 glBegin GL_QUADS;
685 glVertex 0 , 0;
686 glVertex 0 , $h;
687 glVertex $w, $h; 660 glRect 0, 0, $w, $h;
688 glVertex $w, 0;
689 glEnd;
690
691 glDisable GL_BLEND; 661 glDisable GL_BLEND;
692 } 662 }
693} 663}
694 664
695############################################################################# 665#############################################################################
727 can_events => 0, 697 can_events => 0,
728 %arg, 698 %arg,
729 ); 699 );
730 700
731 $self->add (@$children) 701 $self->add (@$children)
732 if $children; 702 if $children && @$children;
733 703
734 $self 704 $self
735} 705}
736 706
737sub realloc { 707sub realloc {
746 my ($self, @widgets) = @_; 716 my ($self, @widgets) = @_;
747 717
748 $_->set_parent ($self) 718 $_->set_parent ($self)
749 for @widgets; 719 for @widgets;
750 720
721 # TODO: only do this in widgets that need it, e.g. root, fixed
751 use sort 'stable'; 722 use sort 'stable';
752 723
753 $self->{children} = [ 724 $self->{children} = [
754 sort { $a->{z} <=> $b->{z} } 725 sort { $a->{z} <=> $b->{z} }
755 @{$self->{children}}, @widgets 726 @{$self->{children}}, @widgets
756 ]; 727 ];
757 728
758 $self->realloc; 729 $self->realloc;
730
731 $self->emit (c_add => \@widgets);
732
733 map $_+0, @widgets
759} 734}
760 735
761sub children { 736sub children {
762 @{ $_[0]{children} } 737 @{ $_[0]{children} }
763} 738}
764 739
765sub remove { 740sub remove {
766 my ($self, $child) = @_; 741 my ($self, @widgets) = @_;
767 742
743 $self->emit (c_remove => \@widgets);
744
745 for my $child (@widgets) {
768 delete $child->{parent}; 746 delete $child->{parent};
769 $child->hide; 747 $child->hide;
770
771 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 748 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
749 }
772 750
773 $self->realloc; 751 $self->realloc;
774} 752}
775 753
776sub clear { 754sub clear {
777 my ($self) = @_; 755 my ($self) = @_;
778 756
779 my $children = delete $self->{children}; 757 my $children = $self->{children};
780 $self->{children} = []; 758 $self->{children} = [];
781 759
782 for (@$children) { 760 for (@$children) {
783 delete $_->{parent}; 761 delete $_->{parent};
784 $_->hide; 762 $_->hide;
804} 782}
805 783
806sub _draw { 784sub _draw {
807 my ($self) = @_; 785 my ($self) = @_;
808 786
809 $_->draw for @{$self->{children}}; 787 $_->draw for $self->visible_children;
810} 788}
811 789
812############################################################################# 790#############################################################################
813 791
814package CFPlus::UI::Bin; 792package CFPlus::UI::Bin;
824} 802}
825 803
826sub add { 804sub add {
827 my ($self, $child) = @_; 805 my ($self, $child) = @_;
828 806
829 $self->SUPER::remove ($_) for @{ $self->{children} }; 807 $self->clear;
830 $self->SUPER::add ($child); 808 $self->SUPER::add ($child);
831} 809}
832 810
833sub remove { 811sub remove {
834 my ($self, $widget) = @_; 812 my ($self, $widget) = @_;
852 830
853 1 831 1
854} 832}
855 833
856############################################################################# 834#############################################################################
857
858# back-buffered drawing area 835# back-buffered drawing area
859 836
860package CFPlus::UI::Window; 837package CFPlus::UI::Window;
861 838
862our @ISA = CFPlus::UI::Bin::; 839our @ISA = CFPlus::UI::Bin::;
898 glClear GL_COLOR_BUFFER_BIT; 875 glClear GL_COLOR_BUFFER_BIT;
899 876
900 { 877 {
901 package CFPlus::UI::Base; 878 package CFPlus::UI::Base;
902 879
903 ($draw_x, $draw_y, $draw_w, $draw_h) = 880 local ($draw_x, $draw_y, $draw_w, $draw_h) =
904 (0, 0, $self->{w}, $self->{h}); 881 (0, 0, $self->{w}, $self->{h});
882
883 $self->_render;
905 } 884 }
906
907 $self->_render;
908 }; 885 };
909} 886}
910 887
911sub _draw { 888sub _draw {
912 my ($self) = @_; 889 my ($self) = @_;
913
914 my ($w, $h) = @$self{qw(w h)};
915 890
916 my $tex = $self->{texture} 891 my $tex = $self->{texture}
917 or return; 892 or return;
918 893
919 glEnable GL_TEXTURE_2D; 894 glEnable GL_TEXTURE_2D;
920 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 895 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
921 glColor 0, 0, 0, 1; 896 glColor 0, 0, 0, 1;
922 897
923 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 898 $tex->draw_quad_alpha_premultiplied (0, 0);
924 899
925 glDisable GL_TEXTURE_2D; 900 glDisable GL_TEXTURE_2D;
926} 901}
927 902
928############################################################################# 903#############################################################################
929 904
930package CFPlus::UI::ViewPort; 905package CFPlus::UI::ViewPort;
906
907use List::Util qw(min max);
931 908
932our @ISA = CFPlus::UI::Window::; 909our @ISA = CFPlus::UI::Window::;
933 910
934sub new { 911sub new {
935 my $class = shift; 912 my $class = shift;
944sub size_request { 921sub size_request {
945 my ($self) = @_; 922 my ($self) = @_;
946 923
947 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 924 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
948 925
949 $w = 10 if $self->{scroll_x}; 926 $w = 1 if $self->{scroll_x};
950 $h = 10 if $self->{scroll_y}; 927 $h = 1 if $self->{scroll_y};
951 928
952 ($w, $h) 929 ($w, $h)
953} 930}
954 931
955sub invoke_size_allocate { 932sub invoke_size_allocate {
967} 944}
968 945
969sub set_offset { 946sub set_offset {
970 my ($self, $x, $y) = @_; 947 my ($self, $x, $y) = @_;
971 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}) {
972 $self->{view_x} = int $x; 953 $self->{view_x} = $x;
973 $self->{view_y} = int $y; 954 $self->{view_y} = $y;
974 955
956 $self->emit (changed => $x, $y);
975 $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 }
976} 977}
977 978
978# 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
979sub coord2local { 980sub coord2local {
980 my ($self, $x, $y) = @_; 981 my ($self, $x, $y) = @_;
995 my ($self, $x, $y) = @_; 996 my ($self, $x, $y) = @_;
996 997
997 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
998 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 999 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
999 ) { 1000 ) {
1000 $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})
1001 } else { 1002 } else {
1002 $self->CFPlus::UI::Base::find_widget ($x, $y) 1003 $self->CFPlus::UI::Base::find_widget ($x, $y)
1003 } 1004 }
1004} 1005}
1005 1006
1016 1017
1017############################################################################# 1018#############################################################################
1018 1019
1019package CFPlus::UI::ScrolledWindow; 1020package CFPlus::UI::ScrolledWindow;
1020 1021
1021our @ISA = CFPlus::UI::HBox::; 1022our @ISA = CFPlus::UI::Table::;
1022 1023
1023sub new { 1024sub new {
1024 my ($class, %arg) = @_; 1025 my ($class, %arg) = @_;
1025 1026
1026 my $child = delete $arg{child}; 1027 my $child = delete $arg{child};
1027 1028
1028 my $self; 1029 my $self;
1029 1030
1030 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,
1031 vertical => 1, 1045 vertical => 1,
1032 range => [0, 0, 1, 0.01], # HACK fix 1046 range => [0, 0, 1, 0.01], # HACK fix
1033 on_changed => sub { 1047 on_changed => sub {
1034 $self->{vp}->set_offset (0, $_[1]); 1048 $self->{vpos} = $_[1];
1049 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1035 }, 1050 },
1036 ; 1051 ;
1037 1052
1038 $self = $class->SUPER::new ( 1053 $self = $class->SUPER::new (
1039 vp => (new CFPlus::UI::ViewPort expand => 1), 1054 scroll_x => 0,
1055 scroll_y => 1,
1040 can_events => 1, 1056 can_events => 1,
1041 slider => $slider, 1057 hslider => $hslider,
1058 vslider => $vslider,
1059 col_expand => [1, 0],
1060 row_expand => [1, 0],
1042 %arg, 1061 %arg,
1043 ); 1062 );
1044 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
1045 $self->SUPER::add ($self->{vp}, $self->{slider}); 1085 $self->SUPER::add ($self->{vp});
1086
1046 $self->add ($child) if $child; 1087 $self->add ($child) if $child;
1047 1088
1048 $self 1089 $self
1049} 1090}
1050
1051#TODO# update range on size_allocate depending on child
1052 1091
1053sub add { 1092sub add {
1054 my ($self, $widget) = @_; 1093 my ($self, $widget) = @_;
1055 1094
1056 $self->{vp}->add ($self->{child} = $widget); 1095 $self->{vp}->add ($self->{child} = $widget);
1057} 1096}
1058 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
1059sub invoke_mouse_wheel { 1150sub invoke_mouse_wheel {
1060 my ($self, $ev) = @_; 1151 my ($self, $ev) = @_;
1061 1152
1062 return 0 unless $ev->{dy}; # only vertical movements
1063
1064 $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};
1065 1155
1066 1 1156 1
1067} 1157}
1068 1158
1069sub update_slider { 1159sub invoke_button_down {
1070 my ($self) = @_; 1160 my ($self, $ev, $x, $y) = @_;
1071 1161
1072 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1162 if ($ev->{button} == 2) {
1073} 1163 $self->start_dragging ($ev);
1164 return 1;
1165 }
1074 1166
1075sub update { 1167 0
1076 my ($self) = @_; 1168}
1077 1169
1078 $self->SUPER::update; 1170sub invoke_button_up {
1171 my ($self, $ev, $x, $y) = @_;
1079 1172
1080 $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
1081} 1189}
1082 1190
1083sub invoke_size_allocate { 1191sub invoke_size_allocate {
1084 my ($self, $w, $h) = @_; 1192 my ($self, $w, $h) = @_;
1085 1193
1086 $self->update_slider; 1194 $self->update_slider;
1087
1088 $self->SUPER::invoke_size_allocate ($w, $h) 1195 $self->SUPER::invoke_size_allocate ($w, $h)
1089} 1196}
1090 1197
1091############################################################################# 1198#############################################################################
1092 1199
1112 my ($w, $h) = @$self{qw(w h)}; 1219 my ($w, $h) = @$self{qw(w h)};
1113 1220
1114 glEnable GL_BLEND; 1221 glEnable GL_BLEND;
1115 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1116 glColor_premultiply @{ $self->{bg} }; 1223 glColor_premultiply @{ $self->{bg} };
1117
1118 glBegin GL_QUADS;
1119 glVertex 0 , 0;
1120 glVertex 0 , $h;
1121 glVertex $w, $h; 1224 glRect 0, 0, $w, $h;
1122 glVertex $w, 0;
1123 glEnd;
1124
1125 glDisable GL_BLEND; 1225 glDisable GL_BLEND;
1126 } 1226 }
1127 1227
1128 $self->SUPER::_draw; 1228 $self->SUPER::_draw;
1129} 1229}
1130 1230
1131############################################################################# 1231#############################################################################
1132 1232
1133package CFPlus::UI::FancyFrame; 1233package CFPlus::UI::FancyFrame;
1234
1235our @ISA = CFPlus::UI::Bin::;
1236
1237use CFPlus::OpenGL;
1238
1239sub new {
1240 my ($class, %arg) = @_;
1241
1242 if ((exists $arg{label}) && !ref $arg{label}) {
1243 $arg{label} = new CFPlus::UI::Label
1244 align => 1,
1245 valign => 0,
1246 text => $arg{label},
1247 fontsize => ($arg{border} || 0.8) * 0.75;
1248 }
1249
1250 my $self = $class->SUPER::new (
1251 # label => "",
1252 fg => [0.6, 0.3, 0.1],
1253 border => 0.8,
1254 style => 'single',
1255 %arg,
1256 );
1257
1258 $self
1259}
1260
1261sub add {
1262 my ($self, @widgets) = @_;
1263
1264 $self->SUPER::add (@widgets);
1265 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1266}
1267
1268sub border {
1269 int $_[0]{border} * $::FONTSIZE
1270}
1271
1272sub size_request {
1273 my ($self) = @_;
1274
1275 ($self->{label_w}, undef) = $self->{label}->size_request
1276 if $self->{label};
1277
1278 my ($w, $h) = $self->SUPER::size_request;
1279
1280 (
1281 $w + $self->border * 2,
1282 $h + $self->border * 2,
1283 )
1284}
1285
1286sub invoke_size_allocate {
1287 my ($self, $w, $h) = @_;
1288
1289 my $border = $self->border;
1290
1291 $w -= List::Util::max 0, $border * 2;
1292 $h -= List::Util::max 0, $border * 2;
1293
1294 if (my $label = $self->{label}) {
1295 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1296 $label->{h} = List::Util::min $h, $border;
1297 $label->invoke_size_allocate ($label->{w}, $label->{h});
1298 }
1299
1300 $self->child->configure ($border, $border, $w, $h);
1301
1302 1
1303}
1304
1305sub _draw {
1306 my ($self) = @_;
1307
1308 my $child = $self->{children}[0];
1309
1310 my $border = $self->border;
1311 my ($w, $h) = ($self->{w}, $self->{h});
1312
1313 $child->draw;
1314
1315 glColor @{$self->{fg}};
1316 glBegin GL_LINE_STRIP;
1317 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1318 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1320 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1321 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1322 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1323 glEnd;
1324
1325 if ($self->{label}) {
1326 glTranslate $border * 2, 0;
1327 $self->{label}->_draw;
1328 }
1329}
1330
1331#############################################################################
1332
1333package CFPlus::UI::Toplevel;
1134 1334
1135our @ISA = CFPlus::UI::Bin::; 1335our @ISA = CFPlus::UI::Bin::;
1136 1336
1137use CFPlus::OpenGL; 1337use CFPlus::OpenGL;
1138 1338
1141 mipmap => 1, wrap => 1; 1341 mipmap => 1, wrap => 1;
1142 1342
1143my @border = 1343my @border =
1144 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1344 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1145 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);
1346
1347my @icon =
1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1349 qw(x1_move.png x1_resize.png);
1146 1350
1147sub new { 1351sub new {
1148 my ($class, %arg) = @_; 1352 my ($class, %arg) = @_;
1149 1353
1150 my $self = $class->SUPER::new ( 1354 my $self = $class->SUPER::new (
1319 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1523 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1320 1524
1321 my $border = $self->border; 1525 my $border = $self->border;
1322 1526
1323 glColor @{ $self->{border_bg} }; 1527 glColor @{ $self->{border_bg} };
1324 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1528 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1325 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1529 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1326 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1530 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1327 $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);
1328 1546
1329 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1547 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1330 glColor @{ $self->{bg} }; 1548 glColor @{ $self->{bg} };
1331 1549
1332 # TODO: repeat texture not scale 1550 # TODO: repeat texture not scale
1353 1571
1354############################################################################# 1572#############################################################################
1355 1573
1356package CFPlus::UI::Table; 1574package CFPlus::UI::Table;
1357 1575
1358our @ISA = CFPlus::UI::Base::; 1576our @ISA = CFPlus::UI::Container::;
1359 1577
1360use List::Util qw(max sum); 1578use List::Util qw(max sum);
1361 1579
1362use CFPlus::OpenGL; 1580use CFPlus::OpenGL;
1363 1581
1364sub new { 1582sub new {
1365 my $class = shift; 1583 my $class = shift;
1366 1584
1367 $class->SUPER::new ( 1585 $class->SUPER::new (
1368 col_expand => [], 1586 col_expand => [],
1587 row_expand => [],
1369 @_, 1588 @_,
1370 ) 1589 )
1371} 1590}
1372 1591
1373sub children {
1374 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1375}
1376
1377sub 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 {
1378 my ($self) = shift; 1604 my $self = shift;
1605
1606 my @widgets;
1379 1607
1380 while (@_) { 1608 while (@_) {
1381 my ($x, $y, $child) = splice @_, 0, 3, (); 1609 my ($col, $row, $child) = splice @_, 0, 3, ();
1382 $child->set_parent ($self);
1383 $self->{children}[$y][$x] = $child;
1384 }
1385 1610
1386 $self->{force_realloc} = 1; 1611 $child->{c_row} = $row;
1387 $self->{force_size_alloc} = 1; 1612 $child->{c_col} = $col;
1388 $self->realloc;
1389}
1390 1613
1391sub remove { 1614 push @widgets, $child;
1615 }
1616
1617 $self->add (@widgets);
1618}
1619
1620sub get_wh {
1392 my ($self, $child) = @_; 1621 my ($self) = @_;
1393 1622
1394 # TODO: not yet implemented 1623 my (@w, @h);
1395}
1396
1397# TODO: move to container class maybe? send children a signal on removal?
1398sub clear {
1399 my ($self) = @_;
1400 1624
1401 my @children = $self->children; 1625 my @children = $self->children;
1402 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 }
1403 1636 }
1404 for (@children) {
1405 delete $_->{parent};
1406 $_->hide;
1407 }
1408 1637
1409 $self->realloc; 1638 # second pass, rows
1410} 1639 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1411
1412sub get_wh {
1413 my ($self) = @_;
1414
1415 my (@w, @h);
1416
1417 for my $y (0 .. $#{$self->{children}}) {
1418 my $row = $self->{children}[$y]
1419 or next;
1420
1421 for my $x (0 .. $#$row) {
1422 my $widget = $row->[$x]
1423 or next;
1424 my ($w, $h) = @$widget{qw(req_w req_h)}; 1640 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1425 1641
1426 $w[$x] = max $w[$x], $w; 1642 my $sh = sum @h[$r .. $r + $rs - 1];
1427 $h[$y] = max $h[$y], $h; 1643
1644 if ($h > $sh) {
1645 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1428 } 1646 }
1429 } 1647 }
1430 1648
1431 (\@w, \@h) 1649 (\@w, \@h)
1432} 1650}
1448 my ($ws, $hs) = $self->get_wh; 1666 my ($ws, $hs) = $self->get_wh;
1449 1667
1450 my $req_w = (sum @$ws) || 1; 1668 my $req_w = (sum @$ws) || 1;
1451 my $req_h = (sum @$hs) || 1; 1669 my $req_h = (sum @$hs) || 1;
1452 1670
1453 # TODO: nicer code && do row_expand 1671 # now linearly scale the rows/columns to the allocated size
1454 my @col_expand = @{$self->{col_expand}}; 1672 my @col_expand = @{$self->{col_expand}};
1455 @col_expand = (1) x @$ws unless @col_expand; 1673 @col_expand = (1) x @$ws unless @col_expand;
1456 my $col_expand = (sum @col_expand) || 1; 1674 my $col_expand = (sum @col_expand) || 1;
1457 1675
1458 # linearly scale sizes
1459 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1676 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1460 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1461 1677
1462 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
1463 CFPlus::UI::harmonize $hs; 1686 CFPlus::UI::harmonize $hs;
1464 1687
1465 my $y; 1688 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1689 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1466 1690
1467 for my $r (0 .. $#{$self->{children}}) { 1691 for my $widget ($self->children) {
1468 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)};
1469 or next;
1470 1693
1471 my $x = 0; 1694 $widget->configure (
1472 my $row_h = $hs->[$r]; 1695 $x[$c], $y[$r],
1696 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1473 1697 );
1474 for my $c (0 .. $#$row) { 1698 }
1475 my $col_w = $ws->[$c];
1476 1699
1477 if (my $widget = $row->[$c]) { 1700 1
1478 $widget->configure ($x, $y, $col_w, $row_h); 1701}
1479 }
1480 1702
1481 $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};
1482 } 1730 }
1731 }
1483 1732
1484 $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});
1485 } 1764 }
1486 1765
1487 1 1766 1
1488} 1767}
1489 1768
1490sub find_widget {
1491 my ($self, $x, $y) = @_;
1492
1493 $x -= $self->{x};
1494 $y -= $self->{y};
1495
1496 my $res;
1497
1498 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1499 $res = $_->find_widget ($x, $y)
1500 and return $res;
1501 }
1502
1503 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1504}
1505
1506sub _draw {
1507 my ($self) = @_;
1508
1509 for (grep $_, @{$self->{children}}) {
1510 $_->draw for grep $_, @$_;
1511 }
1512}
1513
1514############################################################################# 1769#############################################################################
1515 1770
1516package CFPlus::UI::Box; 1771package CFPlus::UI::Box;
1517 1772
1518our @ISA = CFPlus::UI::Container::; 1773our @ISA = CFPlus::UI::Container::;
1519 1774
1520sub size_request { 1775sub size_request {
1521 my ($self) = @_; 1776 my ($self) = @_;
1777
1778 my @children = $self->visible_children;
1522 1779
1523 $self->{vertical} 1780 $self->{vertical}
1524 ? ( 1781 ? (
1525 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1782 (List::Util::max map $_->{req_w}, @children),
1526 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1783 (List::Util::sum map $_->{req_h}, @children),
1527 ) 1784 )
1528 : ( 1785 : (
1529 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1786 (List::Util::sum map $_->{req_w}, @children),
1530 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1787 (List::Util::max map $_->{req_h}, @children),
1531 ) 1788 )
1532} 1789}
1533 1790
1534sub invoke_size_allocate { 1791sub invoke_size_allocate {
1535 my ($self, $w, $h) = @_; 1792 my ($self, $w, $h) = @_;
1618 #active_bg => none 1875 #active_bg => none
1619 #font => default_font 1876 #font => default_font
1620 #text => initial text 1877 #text => initial text
1621 #markup => initial narkup 1878 #markup => initial narkup
1622 #max_w => maximum pixel width 1879 #max_w => maximum pixel width
1880 #style => 0, # render flags
1623 ellipsise => 3, # end 1881 ellipsise => 3, # end
1624 layout => (new CFPlus::Layout), 1882 layout => (new CFPlus::Layout),
1625 fontsize => 1, 1883 fontsize => 1,
1626 align => -1, 1884 align => -1,
1627 valign => -1, 1885 valign => -1,
1658 1916
1659 delete $self->{ox}; 1917 delete $self->{ox};
1660 $self->SUPER::realloc; 1918 $self->SUPER::realloc;
1661} 1919}
1662 1920
1921sub clear {
1922 my ($self) = @_;
1923
1924 $self->set_text ("");
1925}
1926
1663sub set_text { 1927sub set_text {
1664 my ($self, $text) = @_; 1928 my ($self, $text) = @_;
1665 1929
1666 return if $self->{text} eq "T$text"; 1930 return if $self->{text} eq "T$text";
1667 $self->{text} = "T$text"; 1931 $self->{text} = "T$text";
1690 1954
1691sub size_request { 1955sub size_request {
1692 my ($self) = @_; 1956 my ($self) = @_;
1693 1957
1694 $self->{size_req} ||= do { 1958 $self->{size_req} ||= do {
1959 my ($max_w, $max_h) = $self->get_max_wh;
1960
1695 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1961 $self->{layout}->set_font ($self->{font}) if $self->{font};
1696 $self->{layout}->set_width ($self->{max_w} || -1); 1962 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1697 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1963 $self->{layout}->set_ellipsise ($self->{ellipsise});
1698 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1964 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1699 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1965 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1700 1966
1701 my ($w, $h) = $self->{layout}->size; 1967 my ($w, $h) = $self->{layout}->size;
1773 : ($self->{w} - $size->[0]) * 0.5); 2039 : ($self->{w} - $size->[0]) * 0.5);
1774 2040
1775 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2041 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1776 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2042 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1777 : ($self->{h} - $size->[1]) * 0.5); 2043 : ($self->{h} - $size->[1]) * 0.5);
2044
2045 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1778 }; 2046 };
1779 2047
1780 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2048# unless ($self->{list}) {
1781 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2049# $self->{list} = CFPlus::OpenGL::glGenList;
1782 2050# CFPlus::OpenGL::glNewList $self->{list};
1783 $self->{layout}->render ($self->{ox}, $self->{oy}); 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;
1784} 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#}
1785 2067
1786############################################################################# 2068#############################################################################
1787 2069
1788package CFPlus::UI::EntryBase; 2070package CFPlus::UI::EntryBase;
1789 2071
1795 my $class = shift; 2077 my $class = shift;
1796 2078
1797 $class->SUPER::new ( 2079 $class->SUPER::new (
1798 fg => [1, 1, 1], 2080 fg => [1, 1, 1],
1799 bg => [0, 0, 0, 0.2], 2081 bg => [0, 0, 0, 0.2],
2082 outline => [0.6, 0.3, 0.1],
1800 active_bg => [1, 1, 1, 0.5], 2083 active_bg => [0, 0, 1, .2],
1801 active_fg => [0, 0, 0], 2084 active_fg => [1, 1, 1],
2085 active_outline => [1, 1, 0],
1802 can_hover => 1, 2086 can_hover => 1,
1803 can_focus => 1, 2087 can_focus => 1,
1804 valign => 0, 2088 valign => 0,
1805 can_events => 1, 2089 can_events => 1,
2090 ellipsise => 0,
1806 #text => ... 2091 #text => ...
1807 #hidden => "*", 2092 #hidden => "*",
1808 @_ 2093 @_
1809 ) 2094 )
1810} 2095}
1854 my $mod = $ev->{mod}; 2139 my $mod = $ev->{mod};
1855 my $sym = $ev->{sym}; 2140 my $sym = $ev->{sym};
1856 my $uni = $ev->{unicode}; 2141 my $uni = $ev->{unicode};
1857 2142
1858 my $text = $self->get_text; 2143 my $text = $self->get_text;
2144
2145 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
1859 2146
1860 if ($uni == 8) { 2147 if ($uni == 8) {
1861 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1862 } elsif ($uni == 127) { 2149 } elsif ($uni == 127) {
1863 substr $text, $self->{cursor}, 1, ""; 2150 substr $text, $self->{cursor}, 1, "";
1864 } elsif ($sym == CFPlus::SDLK_LEFT) { 2151 } elsif ($sym == CFPlus::SDLK_LEFT) {
1865 --$self->{cursor} if $self->{cursor}; 2152 --$self->{cursor} if $self->{cursor};
1866 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2153 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1867 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2154 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1868 } elsif ($sym == CFPlus::SDLK_HOME) { 2155 } elsif ($sym == CFPlus::SDLK_HOME) {
2156 # what a hack
2157 $self->{cursor} =
2158 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2159 ? length $1
2160 : 0;
2161 } elsif ($sym == CFPlus::SDLK_END) {
2162 # uh, again
2163 $self->{cursor} =
2164 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2165 ? $self->{cursor} + length $1
2166 : length $self->{text};
2167 } elsif ($uni == 21) { # ctrl-u
2168 $text = "";
1869 $self->{cursor} = 0; 2169 $self->{cursor} = 0;
1870 } elsif ($sym == CFPlus::SDLK_END) {
1871 $self->{cursor} = length $text;
1872 } elsif ($uni == 27) { 2170 } elsif ($uni == 27) {
1873 $self->emit ('escape'); 2171 $self->emit ('escape');
1874 } elsif ($uni) { 2172 } elsif ($uni == 0x0d) {
2173 substr $text, $self->{cursor}++, 0, "\012";
2174 } elsif ($uni >= 0x20) {
1875 substr $text, $self->{cursor}++, 0, chr $uni; 2175 substr $text, $self->{cursor}++, 0, chr $uni;
1876 } else { 2176 } else {
1877 return 0; 2177 return 0;
1878 } 2178 }
1879 2179
1880 $self->_set_text ($text); 2180 $self->_set_text ($text);
1881 2181
1882 $self->realloc; 2182 $self->realloc;
2183 $self->update;
1883 2184
1884 1 2185 1
1885} 2186}
1886 2187
1887sub invoke_focus_in { 2188sub invoke_focus_in {
1929 glColor_premultiply @{$self->{bg}}; 2230 glColor_premultiply @{$self->{bg}};
1930 } 2231 }
1931 2232
1932 glEnable GL_BLEND; 2233 glEnable GL_BLEND;
1933 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2234 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1934 glBegin GL_QUADS;
1935 glVertex 0 , 0;
1936 glVertex 0 , $self->{h};
1937 glVertex $self->{w}, $self->{h}; 2235 glRect 0, 0, $self->{w}, $self->{h};
1938 glVertex $self->{w}, 0;
1939 glEnd;
1940 glDisable GL_BLEND; 2236 glDisable GL_BLEND;
1941 2237
1942 $self->SUPER::_draw; 2238 $self->SUPER::_draw;
1943 2239
1944 #TODO: force update every cursor change :( 2240 #TODO: force update every cursor change :(
1946 2242
1947 unless (exists $self->{cur_h}) { 2243 unless (exists $self->{cur_h}) {
1948 my $text = substr $self->{text}, 0, $self->{cursor}; 2244 my $text = substr $self->{text}, 0, $self->{cursor};
1949 utf8::encode $text; 2245 utf8::encode $text;
1950 2246
1951 @$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);
1952 } 2248 }
1953 2249
1954 glColor @{$self->{fg}}; 2250 glColor_premultiply @{$self->{active_fg}};
1955 glBegin GL_LINES; 2251 glBegin GL_LINES;
1956 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2252 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
1957 glVertex $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};
1958 glEnd; 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;
2268 glEnd;
1959 } 2269 }
1960} 2270}
2271
2272#############################################################################
1961 2273
1962package CFPlus::UI::Entry; 2274package CFPlus::UI::Entry;
1963 2275
1964our @ISA = CFPlus::UI::EntryBase::; 2276our @ISA = CFPlus::UI::EntryBase::;
1965 2277
1968sub invoke_key_down { 2280sub invoke_key_down {
1969 my ($self, $ev) = @_; 2281 my ($self, $ev) = @_;
1970 2282
1971 my $sym = $ev->{sym}; 2283 my $sym = $ev->{sym};
1972 2284
1973 if ($sym == 13) { 2285 if ($ev->{uni} == 0x0d || $sym == 13) {
1974 unshift @{$self->{history}}, 2286 unshift @{$self->{history}},
1975 my $txt = $self->get_text; 2287 my $txt = $self->get_text;
1976 2288
1977 $self->{history_pointer} = -1; 2289 $self->{history_pointer} = -1;
1978 $self->{history_saveback} = ''; 2290 $self->{history_saveback} = '';
2008 1 2320 1
2009} 2321}
2010 2322
2011############################################################################# 2323#############################################################################
2012 2324
2325package CFPlus::UI::TextEdit;
2326
2327our @ISA = CFPlus::UI::EntryBase::;
2328
2329use CFPlus::OpenGL;
2330
2331sub move_cursor_ver {
2332 my ($self, $dy) = @_;
2333
2334 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2335
2336 $y += $dy;
2337
2338 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2339 $self->{cursor} = $index;
2340 delete $self->{cur_h};
2341 $self->update;
2342 return;
2343 }
2344}
2345
2346sub invoke_key_down {
2347 my ($self, $ev) = @_;
2348
2349 my $sym = $ev->{sym};
2350
2351 if ($sym == CFPlus::SDLK_UP) {
2352 $self->move_cursor_ver (-1);
2353 } elsif ($sym == CFPlus::SDLK_DOWN) {
2354 $self->move_cursor_ver (+1);
2355 } else {
2356 return $self->SUPER::invoke_key_down ($ev)
2357 }
2358
2359 1
2360}
2361
2362#############################################################################
2363
2013package CFPlus::UI::Button; 2364package CFPlus::UI::ButtonBin;
2014 2365
2015our @ISA = CFPlus::UI::Label::; 2366our @ISA = CFPlus::UI::Bin::;
2016 2367
2017use CFPlus::OpenGL; 2368use CFPlus::OpenGL;
2018 2369
2019my @tex = 2370my @tex =
2020 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2371 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2021 qw(b1_button_active.png); 2372 qw(b1_button_inactive.png b1_button_active.png);
2022 2373
2023sub new { 2374sub new {
2024 my $class = shift; 2375 my $class = shift;
2025 2376
2026 $class->SUPER::new ( 2377 $class->SUPER::new (
2027 padding_x => 4,
2028 padding_y => 4,
2029 fg => [1, 1, 1],
2030 active_fg => [0, 0, 1],
2031 can_hover => 1, 2378 can_hover => 1,
2032 align => 0, 2379 align => 0,
2033 valign => 0, 2380 valign => 0,
2034 can_events => 1, 2381 can_events => 1,
2035 @_ 2382 @_
2047} 2394}
2048 2395
2049sub _draw { 2396sub _draw {
2050 my ($self) = @_; 2397 my ($self) = @_;
2051 2398
2052 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2053
2054 glEnable GL_TEXTURE_2D; 2399 glEnable GL_TEXTURE_2D;
2055 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2400 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2056 glColor 0, 0, 0, 1; 2401 glColor 0, 0, 0, 1;
2057 2402
2403 my $tex = $tex[$GRAB == $self];
2058 $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});
2059 2460
2060 glDisable GL_TEXTURE_2D; 2461 glDisable GL_TEXTURE_2D;
2061 2462
2062 $self->SUPER::_draw; 2463 $self->SUPER::_draw;
2063} 2464}
2120sub _draw { 2521sub _draw {
2121 my ($self) = @_; 2522 my ($self) = @_;
2122 2523
2123 $self->SUPER::_draw; 2524 $self->SUPER::_draw;
2124 2525
2125 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2526 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2126 2527
2127 my ($w, $h) = @$self{qw(w h)}; 2528 my ($w, $h) = @$self{qw(w h)};
2128 2529
2129 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;
2130 2531
2150sub new { 2551sub new {
2151 my $class = shift; 2552 my $class = shift;
2152 2553
2153 my $self = $class->SUPER::new ( 2554 my $self = $class->SUPER::new (
2154 can_events => 0, 2555 can_events => 0,
2556 scale => 1,
2155 @_, 2557 @_,
2156 ); 2558 );
2157 2559
2158 $self->{path} || $self->{tex} 2560 $self->{path} || $self->{tex}
2159 or Carp::croak "'path' or 'tex' attributes required"; 2561 or Carp::croak "'path' or 'tex' attributes required";
2160 2562
2161 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2563 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2162 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;
2163 2565
2164 Scalar::Util::weaken $texture_cache{$self->{path}}; 2566 CFPlus::weaken $texture_cache{$self->{path}};
2165 2567
2166 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2568 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2167 2569
2168 $self 2570 $self
2169} 2571}
2184} 2586}
2185 2587
2186sub size_request { 2588sub size_request {
2187 my ($self) = @_; 2589 my ($self) = @_;
2188 2590
2189 ($self->{tex}{w}, $self->{tex}{h}) 2591 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2190} 2592}
2191 2593
2192sub _draw { 2594sub _draw {
2193 my ($self) = @_; 2595 my ($self) = @_;
2194 2596
2204 } 2606 }
2205 2607
2206 glEnable GL_TEXTURE_2D; 2608 glEnable GL_TEXTURE_2D;
2207 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2609 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2208 2610
2209 $tex->draw_quad (0, 0, $w, $h); 2611 $tex->draw_quad_alpha (0, 0, $w, $h);
2210 2612
2211 glDisable GL_TEXTURE_2D; 2613 glDisable GL_TEXTURE_2D;
2212} 2614}
2213 2615
2214############################################################################# 2616#############################################################################
2233 align => 0, 2635 align => 0,
2234 valign => 0, 2636 valign => 0,
2235 can_events => 1, 2637 can_events => 1,
2236 @_ 2638 @_
2237 ); 2639 );
2640}
2641
2642sub invoke_button_down {
2643 my ($self, $ev, $x, $y) = @_;
2644
2645 1
2238} 2646}
2239 2647
2240sub invoke_button_up { 2648sub invoke_button_up {
2241 my ($self, $ev, $x, $y) = @_; 2649 my ($self, $ev, $x, $y) = @_;
2242 2650
2382 glDisable GL_TEXTURE_2D; 2790 glDisable GL_TEXTURE_2D;
2383} 2791}
2384 2792
2385############################################################################# 2793#############################################################################
2386 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
2387package CFPlus::UI::Gauge; 2933package CFPlus::UI::Gauge;
2388 2934
2389our @ISA = CFPlus::UI::VBox::; 2935our @ISA = CFPlus::UI::VBox::;
2390 2936
2391sub new { 2937sub new {
2516 3062
2517 $self->SUPER::invoke_button_down ($ev, $x, $y); 3063 $self->SUPER::invoke_button_down ($ev, $x, $y);
2518 3064
2519 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3065 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2520 3066
2521 $self->invoke_mouse_motion ($ev, $x, $y) 3067 $self->invoke_mouse_motion ($ev, $x, $y);
3068
3069 1
2522} 3070}
2523 3071
2524sub invoke_mouse_motion { 3072sub invoke_mouse_motion {
2525 my ($self, $ev, $x, $y) = @_; 3073 my ($self, $ev, $x, $y) = @_;
2526 3074
2542sub invoke_mouse_wheel { 3090sub invoke_mouse_wheel {
2543 my ($self, $ev) = @_; 3091 my ($self, $ev) = @_;
2544 3092
2545 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3093 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2546 3094
3095 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
3096
2547 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 3097 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2548 3098
2549 ! ! $delta 3099 1
2550} 3100}
2551 3101
2552sub update { 3102sub update {
2553 my ($self) = @_; 3103 my ($self) = @_;
2554 3104
2662 #font => default_font 3212 #font => default_font
2663 @_, 3213 @_,
2664 3214
2665 layout => (new CFPlus::Layout), 3215 layout => (new CFPlus::Layout),
2666 par => [], 3216 par => [],
3217 max_par => 0,
2667 height => 0, 3218 height => 0,
2668 children => [ 3219 children => [
2669 (new CFPlus::UI::Empty expand => 1), 3220 (new CFPlus::UI::Empty expand => 1),
2670 (new CFPlus::UI::Slider vertical => 1), 3221 (new CFPlus::UI::Slider vertical => 1),
2671 ], 3222 ],
2684} 3235}
2685 3236
2686sub size_request { 3237sub size_request {
2687 my ($self) = @_; 3238 my ($self) = @_;
2688 3239
2689 my ($empty, $slider) = @{ $self->{children} }; 3240 my ($empty, $slider) = $self->visible_children;
2690 3241
2691 local $self->{children} = [$empty, $slider]; 3242 local $self->{children} = [$empty, $slider];
2692 $self->SUPER::size_request 3243 $self->SUPER::size_request
2693} 3244}
2694 3245
2750sub set_offset { 3301sub set_offset {
2751 my ($self, $offset) = @_; 3302 my ($self, $offset) = @_;
2752 3303
2753 # todo: base offset on lines or so, not on pixels 3304 # todo: base offset on lines or so, not on pixels
2754 $self->{children}[1]->set_value ($offset); 3305 $self->{children}[1]->set_value ($offset);
3306}
3307
3308sub current_paragraph {
3309 my ($self) = @_;
3310
3311 $self->{top_paragraph} - 1
3312}
3313
3314sub scroll_to {
3315 my ($self, $para) = @_;
3316
3317 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
3318
3319 $self->{scroll_to} = $para;
3320 $self->update;
2755} 3321}
2756 3322
2757sub clear { 3323sub clear {
2758 my ($self) = @_; 3324 my ($self) = @_;
2759 3325
2781 3347
2782 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 3348 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2783 push @{$self->{par}}, $para; 3349 push @{$self->{par}}, $para;
2784 } 3350 }
2785 3351
3352 if (my $max = $self->{max_par}) {
3353 shift @{$self->{par}} while @{$self->{par}} > $max;
3354 }
3355
2786 $self->{need_reflow}++; 3356 $self->{need_reflow}++;
2787 $self->update; 3357 $self->update;
2788} 3358}
2789 3359
2790sub scroll_to_bottom { 3360sub scroll_to_bottom {
2791 my ($self) = @_; 3361 my ($self) = @_;
2792 3362
2793 $self->{scroll_to_bottom} = 1; 3363 $self->{scroll_to} = $#{$self->{par}};
2794 $self->update; 3364 $self->update;
2795} 3365}
2796 3366
3367sub force_uptodate {
3368 my ($self) = @_;
3369
3370 if (delete $self->{need_reflow}) {
3371 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3372
3373 my $height = 0;
3374
3375 for my $para (@{$self->{par}}) {
3376 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3377 my $layout = $self->get_layout ($para);
3378 my ($w, $h) = $layout->size;
3379
3380 $para->{w} = $w + $para->{indent};
3381 $para->{h} = $h;
3382 $para->{wrapped} = $layout->has_wrapped;
3383 }
3384
3385 $para->{y} = $height;
3386 $height += $para->{h};
3387 }
3388
3389 $self->{height} = $height;
3390 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3391
3392 delete $self->{texture};
3393 }
3394
3395 if (my $paridx = delete $self->{scroll_to}) {
3396 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3397 }
3398}
3399
2797sub update { 3400sub update {
2798 my ($self) = @_; 3401 my ($self) = @_;
2799 3402
2800 $self->SUPER::update; 3403 $self->SUPER::update;
2801 3404
2802 return unless $self->{h} > 0; 3405 return unless $self->{h} > 0;
2803 3406
2804 delete $self->{texture}; 3407 delete $self->{texture};
2805 3408
2806 $ROOT->on_post_alloc ($self => sub { 3409 $ROOT->on_post_alloc ($self => sub {
3410 $self->force_uptodate;
3411
2807 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3412 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2808
2809 if (delete $self->{need_reflow}) {
2810 my $height = 0;
2811
2812 for my $para (@{$self->{par}}) {
2813 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2814 my $layout = $self->get_layout ($para);
2815 my ($w, $h) = $layout->size;
2816
2817 $para->{w} = $w + $para->{indent};
2818 $para->{h} = $h;
2819 $para->{wrapped} = $layout->has_wrapped;
2820 }
2821
2822 $height += $para->{h};
2823 }
2824
2825 $self->{height} = $height;
2826
2827 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2828
2829 delete $self->{texture};
2830 }
2831
2832 if (delete $self->{scroll_to_bottom}) {
2833 $self->{children}[1]->set_value (1e10);
2834 }
2835 3413
2836 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3414 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2837 glClearColor 0, 0, 0, 0; 3415 glClearColor 0, 0, 0, 0;
2838 glClear GL_COLOR_BUFFER_BIT; 3416 glClear GL_COLOR_BUFFER_BIT;
2839 3417
3418 package CFPlus::UI::Base;
3419 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3420 (0, 0, $self->{w}, $self->{h});
3421
3422 my $top = int $self->{children}[1]{range}[0];
3423
3424 my $paridx = 0;
3425 my $top_paragraph;
2840 my $top = int $self->{children}[1]{range}[0]; 3426 my $top = int $self->{children}[1]{range}[0];
2841 3427
2842 my $y0 = $top; 3428 my $y0 = $top;
2843 my $y1 = $top + $H; 3429 my $y1 = $top + $H;
2844 3430
2845 my $y = 0;
2846
2847 for my $para (@{$self->{par}}) { 3431 for my $para (@{$self->{par}}) {
2848 my $h = $para->{h}; 3432 my $h = $para->{h};
3433 my $y = $para->{y};
2849 3434
2850 if ($y0 < $y + $h && $y < $y1) { 3435 if ($y0 < $y + $h && $y < $y1) {
2851
2852 my $layout = $self->get_layout ($para); 3436 my $layout = $self->get_layout ($para);
2853 3437
2854 $layout->render ($para->{indent}, $y - $y0); 3438 $layout->render ($para->{indent}, $y - $y0);
3439 $layout->draw;
2855 3440
2856 if (my @w = @{ $para->{widget} }) { 3441 if (my @w = @{ $para->{widget} }) {
2857 my @s = $layout->get_shapes; 3442 my @s = $layout->get_shapes;
2858 3443
2859 for (@w) { 3444 for (@w) {
2865 $_->draw; 3450 $_->draw;
2866 } 3451 }
2867 } 3452 }
2868 } 3453 }
2869 3454
2870 $y += $h; 3455 $paridx++;
3456 $top_paragraph ||= $paridx if $y >= $top;
2871 } 3457 }
3458
3459 $self->{top_paragraph} = $top_paragraph;
2872 }; 3460 };
2873 }); 3461 });
2874} 3462}
2875 3463
2876sub reconfigure { 3464sub reconfigure {
2978} 3566}
2979 3567
2980sub set_tooltip_from { 3568sub set_tooltip_from {
2981 my ($self, $widget) = @_; 3569 my ($self, $widget) = @_;
2982 3570
2983 $widget->{tooltip} = CFPlus::Pod::as_label CFPlus::Pod::section tooltip => $1
2984 if $widget->{tooltip} =~ /^#(.*)$/;
2985
2986 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 =~ /^#(.*)$/;
2987 3576
2988 if ($ENV{CFPLUS_DEBUG} & 2) { 3577 if ($ENV{CFPLUS_DEBUG} & 2) {
2989 $tooltip .= "\n\n" . (ref $widget) . "\n" 3578 $tip .= "\n\n" . (ref $widget) . "\n"
2990 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3579 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2991 . "req $widget->{req_w} $widget->{req_h}\n" 3580 . "req $widget->{req_w} $widget->{req_h}\n"
2992 . "visible $widget->{visible}"; 3581 . "visible $widget->{visible}";
2993 } 3582 }
2994 3583
2995 $tooltip =~ s/^\n+//; 3584 $tip =~ s/^\n+//;
2996 $tooltip =~ s/\n+$//; 3585 $tip =~ s/\n+$//;
2997 3586
2998 $self->add (new CFPlus::UI::Label 3587 $self->add (new CFPlus::UI::Label
2999 markup => $tooltip, 3588 markup => $tip,
3000 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3589 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3001 fontsize => 0.8, 3590 fontsize => 0.8,
3002 fg => [0, 0, 0, 1], 3591 style => 1, # FLAG_INVERSE
3003 ellipsise => 0, 3592 ellipsise => 0,
3004 font => ($widget->{tooltip_font} || $::FONT_PROP), 3593 font => ($widget->{tooltip_font} || $::FONT_PROP),
3005 ); 3594 );
3006} 3595}
3007 3596
3042} 3631}
3043 3632
3044sub _draw { 3633sub _draw {
3045 my ($self) = @_; 3634 my ($self) = @_;
3046 3635
3047 glTranslate 0.375, 0.375;
3048
3049 my ($w, $h) = @$self{qw(w h)}; 3636 my ($w, $h) = @$self{qw(w h)};
3050 3637
3051 glColor 1, 0.8, 0.4; 3638 glColor 1, 0.8, 0.4;
3052 glBegin GL_QUADS; 3639 glRect 0, 0, $w, $h;
3053 glVertex 0 , 0;
3054 glVertex 0 , $h;
3055 glVertex $w, $h;
3056 glVertex $w, 0;
3057 glEnd;
3058 3640
3059 glColor 0, 0, 0; 3641 glColor 0, 0, 0;
3060 glBegin GL_LINE_LOOP; 3642 glRect_lineloop .5, .5, $w + .5, $h + .5;
3061 glVertex 0 , 0;
3062 glVertex 0 , $h;
3063 glVertex $w, $h;
3064 glVertex $w, 0;
3065 glEnd;
3066 3643
3067 glTranslate 2 - 0.375, 2 - 0.375; 3644 glTranslate 2, 2;
3068 3645
3069 $self->SUPER::_draw; 3646 $self->SUPER::_draw;
3070} 3647}
3071 3648
3072############################################################################# 3649#############################################################################
3079 3656
3080sub new { 3657sub new {
3081 my $class = shift; 3658 my $class = shift;
3082 3659
3083 my $self = $class->SUPER::new ( 3660 my $self = $class->SUPER::new (
3661 size_w => 32,
3662 size_h => 8,
3084 aspect => 1, 3663 aspect => 1,
3085 can_events => 0, 3664 can_events => 0,
3086 @_, 3665 @_,
3087 ); 3666 );
3088 3667
3089 if ($self->{anim} && $self->{animspeed}) { 3668 if ($self->{anim} && $self->{animspeed}) {
3090 Scalar::Util::weaken (my $widget = $self); 3669 CFPlus::weaken (my $widget = $self);
3091 3670
3671 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3092 $self->{timer} = Event->timer ( 3673 $self->{timer} = Event->timer (
3093 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3094 hard => 1, 3674 parked => 1,
3095 interval => $self->{animspeed},
3096 cb => sub { 3675 cb => sub {
3676 return unless $::CONN;
3677
3678 my $w = $widget
3679 or return;
3680
3097 ++$widget->{frame}; 3681 ++$w->{frame};
3682 $w->update_face;
3683
3684 # somehow, $widget can go away
3098 $widget->update; 3685 $w->update;
3686 $w->update_timer;
3099 }, 3687 },
3100 ); 3688 );
3689
3690 $self->update_face;
3691 $self->update_timer;
3101 } 3692 }
3102 3693
3103 $self 3694 $self
3104} 3695}
3105 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
3106sub size_request { 3733sub size_request {
3107 (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)
3108} 3751}
3109 3752
3110sub update { 3753sub update {
3111 my ($self) = @_; 3754 my ($self) = @_;
3112 3755
3113 return unless $self->{visible}; 3756 return unless $self->{visible};
3114 3757
3115 $self->SUPER::update; 3758 $self->SUPER::update;
3116} 3759}
3117 3760
3761sub invoke_visibility_change {
3762 my ($self) = @_;
3763
3764 $self->update_timer;
3765
3766 0
3767}
3768
3118sub _draw { 3769sub _draw {
3119 my ($self) = @_; 3770 my ($self) = @_;
3120 3771
3121 return unless $::CONN;
3122
3123 $self->SUPER::_draw; 3772 $self->SUPER::_draw;
3124 3773
3125 my $face; 3774 if (my $tex = $self->{tex}) {
3126
3127 if ($self->{frame}) {
3128 my $anim = $::CONN->{anim}[$self->{anim}];
3129
3130 $face = $anim->[ $self->{frame} % @$anim ]
3131 if $anim && @$anim;
3132 }
3133
3134 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3135
3136 if ($tex) {
3137 glEnable GL_TEXTURE_2D; 3775 glEnable GL_TEXTURE_2D;
3138 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3776 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3139 glColor 0, 0, 0, 1; 3777 glColor 0, 0, 0, 1;
3140 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3778 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3141 glDisable GL_TEXTURE_2D; 3779 glDisable GL_TEXTURE_2D;
3143} 3781}
3144 3782
3145sub destroy { 3783sub destroy {
3146 my ($self) = @_; 3784 my ($self) = @_;
3147 3785
3148 $self->{timer}->cancel 3786 (delete $self->{timer})->cancel
3149 if $self->{timer}; 3787 if $self->{timer};
3150 3788
3151 $self->SUPER::destroy; 3789 $self->SUPER::destroy;
3152} 3790}
3153 3791
3155 3793
3156package CFPlus::UI::Buttonbar; 3794package CFPlus::UI::Buttonbar;
3157 3795
3158our @ISA = CFPlus::UI::HBox::; 3796our @ISA = CFPlus::UI::HBox::;
3159 3797
3160# TODO: should actualyl wrap buttons and other goodies. 3798# TODO: should actually wrap buttons and other goodies.
3161 3799
3162############################################################################# 3800#############################################################################
3163 3801
3164package CFPlus::UI::Menu; 3802package CFPlus::UI::Menu;
3165 3803
3166our @ISA = CFPlus::UI::FancyFrame::; 3804our @ISA = CFPlus::UI::Toplevel::;
3167 3805
3168use CFPlus::OpenGL; 3806use CFPlus::OpenGL;
3169 3807
3170sub new { 3808sub new {
3171 my $class = shift; 3809 my $class = shift;
3193 children => [ 3831 children => [
3194 (new CFPlus::UI::Label markup => $left, expand => 1), 3832 (new CFPlus::UI::Label markup => $left, expand => 1),
3195 (new CFPlus::UI::Label markup => $right, align => +1), 3833 (new CFPlus::UI::Label markup => $right, align => +1),
3196 ], 3834 ],
3197 ; 3835 ;
3198 3836
3199 } else { 3837 } else {
3200 $widget = new CFPlus::UI::Label 3838 $widget = new CFPlus::UI::Label
3201 can_hover => 1, 3839 can_hover => 1,
3202 can_events => 1, 3840 can_events => 1,
3203 markup => $widget, 3841 markup => $widget,
3328 3966
3329############################################################################# 3967#############################################################################
3330 3968
3331package CFPlus::UI::Notebook; 3969package CFPlus::UI::Notebook;
3332 3970
3971use CFPlus::OpenGL;
3972
3333our @ISA = CFPlus::UI::VBox::; 3973our @ISA = CFPlus::UI::VBox::;
3334 3974
3335sub new { 3975sub new {
3336 my $class = shift; 3976 my $class = shift;
3337 3977
3338 my $self = $class->SUPER::new ( 3978 my $self = $class->SUPER::new (
3339 buttonbar => (new CFPlus::UI::Buttonbar), 3979 buttonbar => (new CFPlus::UI::Buttonbar),
3340 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3980 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3981 active_outline => [.7, .7, 0.2],
3341 # filter => # will be put between multiplexer and $self 3982 # filter => # will be put between multiplexer and $self
3342 @_, 3983 @_,
3343 ); 3984 );
3344 3985
3345 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3986 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3346 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3987 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3347 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
3348 $self 4023 $self
3349} 4024}
3350 4025
3351sub 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 {
3352 my ($self, $title, $widget, $tooltip) = @_; 4044 my ($self, $title, $widget, $tooltip) = @_;
3353 4045
3354 Scalar::Util::weaken $self; 4046 $title = [$title, $tooltip] unless ref $title;
4047 $widget->{c_tab} = $title;
3355 4048
3356 $self->{buttonbar}->add (new CFPlus::UI::Button
3357 markup => $title,
3358 tooltip => $tooltip,
3359 on_activate => sub { $self->set_current_page ($widget) },
3360 );
3361
3362 $self->{multiplexer}->add ($widget); 4049 $self->add ($widget);
3363} 4050}
3364 4051
3365sub get_current_page { 4052sub get_current_page {
3366 my ($self) = @_; 4053 my ($self) = @_;
3367 4054
3371sub set_current_page { 4058sub set_current_page {
3372 my ($self, $page) = @_; 4059 my ($self, $page) = @_;
3373 4060
3374 $self->{multiplexer}->set_current_page ($page); 4061 $self->{multiplexer}->set_current_page ($page);
3375 $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 }
3376} 4083}
3377 4084
3378############################################################################# 4085#############################################################################
3379 4086
3380package CFPlus::UI::Selector; 4087package CFPlus::UI::Selector;
3443 my $self = $class->SUPER::new ( 4150 my $self = $class->SUPER::new (
3444 fontsize => 0.8, 4151 fontsize => 0.8,
3445 @_, 4152 @_,
3446 ); 4153 );
3447 4154
3448 Scalar::Util::weaken (my $this = $self); 4155 CFPlus::weaken (my $this = $self);
3449 4156
3450 $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 });
3451 4158
3452 $self 4159 $self
3453} 4160}
3576 $self->SUPER::destroy; 4283 $self->SUPER::destroy;
3577} 4284}
3578 4285
3579############################################################################# 4286#############################################################################
3580 4287
3581package CFPlus::UI::Inventory;
3582
3583our @ISA = CFPlus::UI::Table::;
3584
3585sub new {
3586 my $class = shift;
3587
3588 my $self = $class->SUPER::new (
3589 col_expand => [0, 1, 0],
3590 items => [],
3591 @_,
3592 );
3593
3594 $self->set_sort_order (undef);
3595
3596 $self
3597}
3598
3599sub update_items {
3600 my ($self) = @_;
3601
3602 $self->clear;
3603
3604 my @item = $self->{sort}->(@{ $self->{items} });
3605
3606 my @adds;
3607 my $row = 0;
3608 for my $item ($self->{sort}->(@{ $self->{items} })) {
3609 CFPlus::Item::update_widgets $item;
3610
3611 push @adds, 0, $row, $item->{face_widget};
3612 push @adds, 1, $row, $item->{desc_widget};
3613 push @adds, 2, $row, $item->{weight_widget};
3614
3615 $row++;
3616 }
3617
3618 $self->add (@adds);
3619}
3620
3621sub set_sort_order {
3622 my ($self, $order) = @_;
3623
3624 $self->{sort} = $order ||= sub {
3625 sort {
3626 $a->{type} <=> $b->{type}
3627 or $a->{name} cmp $b->{name}
3628 } @_
3629 };
3630
3631 $self->update_items;
3632}
3633
3634sub set_items {
3635 my ($self, $items) = @_;
3636
3637 $self->{items} = [$items ? values %$items : ()];
3638 $self->update_items;
3639}
3640
3641#############################################################################
3642
3643package CFPlus::UI::SpellList;
3644
3645our @ISA = CFPlus::UI::Table::;
3646
3647sub new {
3648 my $class = shift;
3649
3650 my $self = $class->SUPER::new (
3651 binding => [],
3652 commands => [],
3653 @_,
3654 )
3655}
3656
3657my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3658
3659my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3660 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3661my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3662 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3663my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3664 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3665my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3666 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3667my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3668 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3669
3670sub rebuild_spell_list {
3671 my ($self) = @_;
3672
3673 $CFPlus::UI::ROOT->on_refresh ($self => sub {
3674 $self->clear;
3675
3676 return unless $::CONN;
3677
3678 $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3679 $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL);
3680 $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3681 $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3682 $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3683
3684 my $row = 0;
3685
3686 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3687 my $spell = $self->{spell}{$_};
3688
3689 $row++;
3690
3691 my $spell_cb = sub {
3692 my ($widget, $ev) = @_;
3693
3694 if ($ev->{button} == 1) {
3695 $::CONN->user_send ("cast $spell->{name}");
3696 } elsif ($ev->{button} == 2) {
3697 $::CONN->user_send ("invoke $spell->{name}");
3698 } elsif ($ev->{button} == 3) {
3699 (new CFPlus::UI::Menu
3700 items => [
3701 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3702 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3703 ],
3704 )->popup ($ev);
3705 } else {
3706 return 0;
3707 }
3708
3709 1
3710 };
3711
3712 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3713
3714 #TODO: add path info to tooltip
3715 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3716
3717 $self->add (0, $row, new CFPlus::UI::Face
3718 face => $spell->{face},
3719 can_hover => 1,
3720 can_events => 1,
3721 tooltip => $tooltip,
3722 on_button_down => $spell_cb,
3723 );
3724
3725 $self->add (1, $row, new CFPlus::UI::Label
3726 expand => 1,
3727 text => $spell->{name},
3728 can_hover => 1,
3729 can_events => 1,
3730 tooltip => $tooltip,
3731 on_button_down => $spell_cb,
3732 );
3733
3734 $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3735 $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3736 $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3737 $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3738 }
3739 });
3740}
3741
3742sub add_spell {
3743 my ($self, $spell) = @_;
3744
3745 $self->{spell}->{$spell->{name}} = $spell;
3746 $self->rebuild_spell_list;
3747}
3748
3749sub remove_spell {
3750 my ($self, $spell) = @_;
3751
3752 delete $self->{spell}->{$spell->{name}};
3753 $self->rebuild_spell_list;
3754}
3755
3756sub clear_spells {
3757 my ($self) = @_;
3758
3759 $self->{spell} = {};
3760 $self->rebuild_spell_list;
3761}
3762
3763#############################################################################
3764
3765package CFPlus::UI::Root; 4288package CFPlus::UI::Root;
3766 4289
3767our @ISA = CFPlus::UI::Container::; 4290our @ISA = CFPlus::UI::Container::;
3768 4291
3769use List::Util qw(min max); 4292use List::Util qw(min max);
3776 my $self = $class->SUPER::new ( 4299 my $self = $class->SUPER::new (
3777 visible => 1, 4300 visible => 1,
3778 @_, 4301 @_,
3779 ); 4302 );
3780 4303
3781 Scalar::Util::weaken ($self->{root} = $self); 4304 CFPlus::weaken ($self->{root} = $self);
3782 4305
3783 $self 4306 $self
3784} 4307}
3785 4308
3786sub size_request { 4309sub size_request {
3834} 4357}
3835 4358
3836sub update { 4359sub update {
3837 my ($self) = @_; 4360 my ($self) = @_;
3838 4361
3839 $::WANT_REFRESH++; 4362 $::WANT_REFRESH->start;
3840} 4363}
3841 4364
3842sub add { 4365sub add {
3843 my ($self, @children) = @_; 4366 my ($self, @children) = @_;
3844 4367
3881 while ($self->{refresh_hook}) { 4404 while ($self->{refresh_hook}) {
3882 $_->() 4405 $_->()
3883 for values %{delete $self->{refresh_hook}}; 4406 for values %{delete $self->{refresh_hook}};
3884 } 4407 }
3885 4408
3886 if ($self->{realloc}) { 4409 while ($self->{realloc}) {
3887 my %queue; 4410 my %queue;
3888 my @queue; 4411 my @queue;
3889 my $widget; 4412 my $widget;
3890 4413
3891 outer: 4414 outer:
3938 } 4461 }
3939 } 4462 }
3940 4463
3941 delete $self->{realloc}{$widget+0}; 4464 delete $self->{realloc}{$widget+0};
3942 } 4465 }
3943 }
3944 4466
3945 while (my $size_alloc = delete $self->{size_alloc}) { 4467 while (my $size_alloc = delete $self->{size_alloc}) {
3946 my @queue = sort { $b->{visible} <=> $a->{visible} } 4468 my @queue = sort { $a->{visible} <=> $b->{visible} }
3947 values %$size_alloc; 4469 values %$size_alloc;
3948 4470
3949 while () { 4471 while () {
3950 my $widget = pop @queue || last; 4472 my $widget = pop @queue || last;
3951 4473
3952 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4474 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3953 4475
3954 $w = 0 if $w < 0;
3955 $h = 0 if $h < 0;
3956
3957 $w = max $widget->{min_w}, $w; 4476 $w = max $widget->{min_w}, $w;
3958 $h = max $widget->{min_h}, $h; 4477 $h = max $widget->{min_h}, $h;
3959 4478
3960# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4479# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
3961# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4480# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
3962 4481
3963 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4482 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3964 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4483 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3965 4484
3966 $w = int $w + 0.5; 4485 $w = int $w + 0.5;
3967 $h = int $h + 0.5; 4486 $h = int $h + 0.5;
3968 4487
3969 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}) {
3970 $widget->{old_w} = $widget->{w}; 4489 $widget->{old_w} = $widget->{w};
3971 $widget->{old_h} = $widget->{h}; 4490 $widget->{old_h} = $widget->{h};
3972 4491
3973 $widget->{w} = $w; 4492 $widget->{w} = $w;
3974 $widget->{h} = $h; 4493 $widget->{h} = $h;
3975 4494
3976 $widget->emit (size_allocate => $w, $h); 4495 $widget->emit (size_allocate => $w, $h);
4496 }
3977 } 4497 }
3978 } 4498 }
3979 } 4499 }
3980 4500
3981 while ($self->{post_alloc_hook}) { 4501 while ($self->{post_alloc_hook}) {
3982 $_->() 4502 $_->()
3983 for values %{delete $self->{post_alloc_hook}}; 4503 for values %{delete $self->{post_alloc_hook}};
3984 } 4504 }
3985
3986 4505
3987 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4506 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3988 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4507 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3989 glClear GL_COLOR_BUFFER_BIT; 4508 glClear GL_COLOR_BUFFER_BIT;
3990 4509
3995 glLoadIdentity; 4514 glLoadIdentity;
3996 4515
3997 { 4516 {
3998 package CFPlus::UI::Base; 4517 package CFPlus::UI::Base;
3999 4518
4000 ($draw_x, $draw_y, $draw_w, $draw_h) = 4519 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4001 (0, 0, $self->{w}, $self->{h}); 4520 (0, 0, $self->{w}, $self->{h});
4002 }
4003 4521
4004 $self->_draw; 4522 $self->_draw;
4523 }
4005} 4524}
4006 4525
4007############################################################################# 4526#############################################################################
4008 4527
4009package CFPlus::UI; 4528package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines