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.346 by root, Mon Aug 14 01:21:02 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) = @_;
924} 901}
925 902
926############################################################################# 903#############################################################################
927 904
928package CFPlus::UI::ViewPort; 905package CFPlus::UI::ViewPort;
906
907use List::Util qw(min max);
929 908
930our @ISA = CFPlus::UI::Window::; 909our @ISA = CFPlus::UI::Window::;
931 910
932sub new { 911sub new {
933 my $class = shift; 912 my $class = shift;
942sub size_request { 921sub size_request {
943 my ($self) = @_; 922 my ($self) = @_;
944 923
945 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 924 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
946 925
947 $w = 10 if $self->{scroll_x}; 926 $w = 1 if $self->{scroll_x};
948 $h = 10 if $self->{scroll_y}; 927 $h = 1 if $self->{scroll_y};
949 928
950 ($w, $h) 929 ($w, $h)
951} 930}
952 931
953sub invoke_size_allocate { 932sub invoke_size_allocate {
965} 944}
966 945
967sub set_offset { 946sub set_offset {
968 my ($self, $x, $y) = @_; 947 my ($self, $x, $y) = @_;
969 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}) {
970 $self->{view_x} = int $x; 953 $self->{view_x} = $x;
971 $self->{view_y} = int $y; 954 $self->{view_y} = $y;
972 955
956 $self->emit (changed => $x, $y);
973 $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 }
974} 977}
975 978
976# 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
977sub coord2local { 980sub coord2local {
978 my ($self, $x, $y) = @_; 981 my ($self, $x, $y) = @_;
993 my ($self, $x, $y) = @_; 996 my ($self, $x, $y) = @_;
994 997
995 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
996 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 999 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
997 ) { 1000 ) {
998 $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})
999 } else { 1002 } else {
1000 $self->CFPlus::UI::Base::find_widget ($x, $y) 1003 $self->CFPlus::UI::Base::find_widget ($x, $y)
1001 } 1004 }
1002} 1005}
1003 1006
1014 1017
1015############################################################################# 1018#############################################################################
1016 1019
1017package CFPlus::UI::ScrolledWindow; 1020package CFPlus::UI::ScrolledWindow;
1018 1021
1019our @ISA = CFPlus::UI::HBox::; 1022our @ISA = CFPlus::UI::Table::;
1020 1023
1021sub new { 1024sub new {
1022 my ($class, %arg) = @_; 1025 my ($class, %arg) = @_;
1023 1026
1024 my $child = delete $arg{child}; 1027 my $child = delete $arg{child};
1025 1028
1026 my $self; 1029 my $self;
1027 1030
1028 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,
1029 vertical => 1, 1045 vertical => 1,
1030 range => [0, 0, 1, 0.01], # HACK fix 1046 range => [0, 0, 1, 0.01], # HACK fix
1031 on_changed => sub { 1047 on_changed => sub {
1032 $self->{vp}->set_offset (0, $_[1]); 1048 $self->{vpos} = $_[1];
1049 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1033 }, 1050 },
1034 ; 1051 ;
1035 1052
1036 $self = $class->SUPER::new ( 1053 $self = $class->SUPER::new (
1037 vp => (new CFPlus::UI::ViewPort expand => 1), 1054 scroll_x => 0,
1055 scroll_y => 1,
1038 can_events => 1, 1056 can_events => 1,
1039 slider => $slider, 1057 hslider => $hslider,
1058 vslider => $vslider,
1059 col_expand => [1, 0],
1060 row_expand => [1, 0],
1040 %arg, 1061 %arg,
1041 ); 1062 );
1042 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
1043 $self->SUPER::add ($self->{vp}, $self->{slider}); 1085 $self->SUPER::add ($self->{vp});
1086
1044 $self->add ($child) if $child; 1087 $self->add ($child) if $child;
1045 1088
1046 $self 1089 $self
1047} 1090}
1048
1049#TODO# update range on size_allocate depending on child
1050 1091
1051sub add { 1092sub add {
1052 my ($self, $widget) = @_; 1093 my ($self, $widget) = @_;
1053 1094
1054 $self->{vp}->add ($self->{child} = $widget); 1095 $self->{vp}->add ($self->{child} = $widget);
1055} 1096}
1056 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
1057sub invoke_mouse_wheel { 1150sub invoke_mouse_wheel {
1058 my ($self, $ev) = @_; 1151 my ($self, $ev) = @_;
1059 1152
1060 return 0 unless $ev->{dy}; # only vertical movements
1061
1062 $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};
1063 1155
1064 1 1156 1
1065} 1157}
1066 1158
1067sub update_slider { 1159sub invoke_button_down {
1068 my ($self) = @_; 1160 my ($self, $ev, $x, $y) = @_;
1069 1161
1070 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1162 if ($ev->{button} == 2) {
1071} 1163 $self->start_dragging ($ev);
1164 return 1;
1165 }
1072 1166
1073sub update { 1167 0
1074 my ($self) = @_; 1168}
1075 1169
1076 $self->SUPER::update; 1170sub invoke_button_up {
1171 my ($self, $ev, $x, $y) = @_;
1077 1172
1078 $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
1079} 1189}
1080 1190
1081sub invoke_size_allocate { 1191sub invoke_size_allocate {
1082 my ($self, $w, $h) = @_; 1192 my ($self, $w, $h) = @_;
1083 1193
1084 $self->update_slider; 1194 $self->update_slider;
1085
1086 $self->SUPER::invoke_size_allocate ($w, $h) 1195 $self->SUPER::invoke_size_allocate ($w, $h)
1087} 1196}
1088 1197
1089############################################################################# 1198#############################################################################
1090 1199
1110 my ($w, $h) = @$self{qw(w h)}; 1219 my ($w, $h) = @$self{qw(w h)};
1111 1220
1112 glEnable GL_BLEND; 1221 glEnable GL_BLEND;
1113 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1114 glColor_premultiply @{ $self->{bg} }; 1223 glColor_premultiply @{ $self->{bg} };
1115
1116 glBegin GL_QUADS;
1117 glVertex 0 , 0;
1118 glVertex 0 , $h;
1119 glVertex $w, $h; 1224 glRect 0, 0, $w, $h;
1120 glVertex $w, 0;
1121 glEnd;
1122
1123 glDisable GL_BLEND; 1225 glDisable GL_BLEND;
1124 } 1226 }
1125 1227
1126 $self->SUPER::_draw; 1228 $self->SUPER::_draw;
1127} 1229}
1138 my ($class, %arg) = @_; 1240 my ($class, %arg) = @_;
1139 1241
1140 if ((exists $arg{label}) && !ref $arg{label}) { 1242 if ((exists $arg{label}) && !ref $arg{label}) {
1141 $arg{label} = new CFPlus::UI::Label 1243 $arg{label} = new CFPlus::UI::Label
1142 align => 1, 1244 align => 1,
1143 valign => 1, 1245 valign => 0,
1144 text => $arg{label}, 1246 text => $arg{label},
1145 fontsize => ($arg{border} || 0.8) * 0.75; 1247 fontsize => ($arg{border} || 0.8) * 0.75;
1146 } 1248 }
1147 1249
1148 my $self = $class->SUPER::new ( 1250 my $self = $class->SUPER::new (
1240 1342
1241my @border = 1343my @border =
1242 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1344 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1243 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);
1244 1346
1347my @icon =
1348 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1349 qw(x1_move.png x1_resize.png);
1350
1245sub new { 1351sub new {
1246 my ($class, %arg) = @_; 1352 my ($class, %arg) = @_;
1247 1353
1248 my $self = $class->SUPER::new ( 1354 my $self = $class->SUPER::new (
1249 bg => [1, 1, 1, 1], 1355 bg => [1, 1, 1, 1],
1417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1523 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1418 1524
1419 my $border = $self->border; 1525 my $border = $self->border;
1420 1526
1421 glColor @{ $self->{border_bg} }; 1527 glColor @{ $self->{border_bg} };
1422 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1528 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1423 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1529 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1424 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1530 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1425 $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);
1426 1546
1427 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1547 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1428 glColor @{ $self->{bg} }; 1548 glColor @{ $self->{bg} };
1429 1549
1430 # TODO: repeat texture not scale 1550 # TODO: repeat texture not scale
1451 1571
1452############################################################################# 1572#############################################################################
1453 1573
1454package CFPlus::UI::Table; 1574package CFPlus::UI::Table;
1455 1575
1456our @ISA = CFPlus::UI::Base::; 1576our @ISA = CFPlus::UI::Container::;
1457 1577
1458use List::Util qw(max sum); 1578use List::Util qw(max sum);
1459 1579
1460use CFPlus::OpenGL; 1580use CFPlus::OpenGL;
1461 1581
1462sub new { 1582sub new {
1463 my $class = shift; 1583 my $class = shift;
1464 1584
1465 $class->SUPER::new ( 1585 $class->SUPER::new (
1466 col_expand => [], 1586 col_expand => [],
1587 row_expand => [],
1467 @_, 1588 @_,
1468 ) 1589 )
1469} 1590}
1470 1591
1471sub children {
1472 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1473}
1474
1475sub 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 {
1476 my ($self) = shift; 1604 my $self = shift;
1605
1606 my @widgets;
1477 1607
1478 while (@_) { 1608 while (@_) {
1479 my ($x, $y, $child) = splice @_, 0, 3, (); 1609 my ($col, $row, $child) = splice @_, 0, 3, ();
1480 $child->set_parent ($self);
1481 $self->{children}[$y][$x] = $child;
1482 }
1483 1610
1484 $self->{force_realloc} = 1; 1611 $child->{c_row} = $row;
1485 $self->{force_size_alloc} = 1; 1612 $child->{c_col} = $col;
1486 $self->realloc;
1487}
1488 1613
1489sub remove { 1614 push @widgets, $child;
1615 }
1616
1617 $self->add (@widgets);
1618}
1619
1620sub get_wh {
1490 my ($self, $child) = @_; 1621 my ($self) = @_;
1491 1622
1492 # TODO: not yet implemented 1623 my (@w, @h);
1493}
1494
1495# TODO: move to container class maybe? send children a signal on removal?
1496sub clear {
1497 my ($self) = @_;
1498 1624
1499 my @children = $self->children; 1625 my @children = $self->children;
1500 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 }
1501 1636 }
1502 for (@children) {
1503 delete $_->{parent};
1504 $_->hide;
1505 }
1506 1637
1507 $self->realloc; 1638 # second pass, rows
1508} 1639 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1509
1510sub get_wh {
1511 my ($self) = @_;
1512
1513 my (@w, @h);
1514
1515 for my $y (0 .. $#{$self->{children}}) {
1516 my $row = $self->{children}[$y]
1517 or next;
1518
1519 for my $x (0 .. $#$row) {
1520 my $widget = $row->[$x]
1521 or next;
1522 my ($w, $h) = @$widget{qw(req_w req_h)}; 1640 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1523 1641
1524 $w[$x] = max $w[$x], $w; 1642 my $sh = sum @h[$r .. $r + $rs - 1];
1525 $h[$y] = max $h[$y], $h; 1643
1644 if ($h > $sh) {
1645 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1526 } 1646 }
1527 } 1647 }
1528 1648
1529 (\@w, \@h) 1649 (\@w, \@h)
1530} 1650}
1546 my ($ws, $hs) = $self->get_wh; 1666 my ($ws, $hs) = $self->get_wh;
1547 1667
1548 my $req_w = (sum @$ws) || 1; 1668 my $req_w = (sum @$ws) || 1;
1549 my $req_h = (sum @$hs) || 1; 1669 my $req_h = (sum @$hs) || 1;
1550 1670
1551 # TODO: nicer code && do row_expand 1671 # now linearly scale the rows/columns to the allocated size
1552 my @col_expand = @{$self->{col_expand}}; 1672 my @col_expand = @{$self->{col_expand}};
1553 @col_expand = (1) x @$ws unless @col_expand; 1673 @col_expand = (1) x @$ws unless @col_expand;
1554 my $col_expand = (sum @col_expand) || 1; 1674 my $col_expand = (sum @col_expand) || 1;
1555 1675
1556 # linearly scale sizes
1557 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1676 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1558 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1559 1677
1560 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
1561 CFPlus::UI::harmonize $hs; 1686 CFPlus::UI::harmonize $hs;
1562 1687
1563 my $y; 1688 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1689 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1564 1690
1565 for my $r (0 .. $#{$self->{children}}) { 1691 for my $widget ($self->children) {
1566 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)};
1567 or next;
1568 1693
1569 my $x = 0; 1694 $widget->configure (
1570 my $row_h = $hs->[$r]; 1695 $x[$c], $y[$r],
1696 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1571 1697 );
1572 for my $c (0 .. $#$row) { 1698 }
1573 my $col_w = $ws->[$c];
1574 1699
1575 if (my $widget = $row->[$c]) { 1700 1
1576 $widget->configure ($x, $y, $col_w, $row_h); 1701}
1577 }
1578 1702
1579 $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};
1580 } 1730 }
1731 }
1581 1732
1582 $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});
1583 } 1764 }
1584 1765
1585 1 1766 1
1586} 1767}
1587 1768
1588sub find_widget {
1589 my ($self, $x, $y) = @_;
1590
1591 $x -= $self->{x};
1592 $y -= $self->{y};
1593
1594 my $res;
1595
1596 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1597 $res = $_->find_widget ($x, $y)
1598 and return $res;
1599 }
1600
1601 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1602}
1603
1604sub _draw {
1605 my ($self) = @_;
1606
1607 for (grep $_, @{$self->{children}}) {
1608 $_->draw for grep $_, @$_;
1609 }
1610}
1611
1612############################################################################# 1769#############################################################################
1613 1770
1614package CFPlus::UI::Box; 1771package CFPlus::UI::Box;
1615 1772
1616our @ISA = CFPlus::UI::Container::; 1773our @ISA = CFPlus::UI::Container::;
1617 1774
1618sub size_request { 1775sub size_request {
1619 my ($self) = @_; 1776 my ($self) = @_;
1777
1778 my @children = $self->visible_children;
1620 1779
1621 $self->{vertical} 1780 $self->{vertical}
1622 ? ( 1781 ? (
1623 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1782 (List::Util::max map $_->{req_w}, @children),
1624 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1783 (List::Util::sum map $_->{req_h}, @children),
1625 ) 1784 )
1626 : ( 1785 : (
1627 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1786 (List::Util::sum map $_->{req_w}, @children),
1628 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1787 (List::Util::max map $_->{req_h}, @children),
1629 ) 1788 )
1630} 1789}
1631 1790
1632sub invoke_size_allocate { 1791sub invoke_size_allocate {
1633 my ($self, $w, $h) = @_; 1792 my ($self, $w, $h) = @_;
1757 1916
1758 delete $self->{ox}; 1917 delete $self->{ox};
1759 $self->SUPER::realloc; 1918 $self->SUPER::realloc;
1760} 1919}
1761 1920
1921sub clear {
1922 my ($self) = @_;
1923
1924 $self->set_text ("");
1925}
1926
1762sub set_text { 1927sub set_text {
1763 my ($self, $text) = @_; 1928 my ($self, $text) = @_;
1764 1929
1765 return if $self->{text} eq "T$text"; 1930 return if $self->{text} eq "T$text";
1766 $self->{text} = "T$text"; 1931 $self->{text} = "T$text";
1789 1954
1790sub size_request { 1955sub size_request {
1791 my ($self) = @_; 1956 my ($self) = @_;
1792 1957
1793 $self->{size_req} ||= do { 1958 $self->{size_req} ||= do {
1959 my ($max_w, $max_h) = $self->get_max_wh;
1960
1794 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1961 $self->{layout}->set_font ($self->{font}) if $self->{font};
1795 $self->{layout}->set_width ($self->{max_w} || -1); 1962 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1796 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1963 $self->{layout}->set_ellipsise ($self->{ellipsise});
1797 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1964 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1798 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1965 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1799 1966
1800 my ($w, $h) = $self->{layout}->size; 1967 my ($w, $h) = $self->{layout}->size;
1872 : ($self->{w} - $size->[0]) * 0.5); 2039 : ($self->{w} - $size->[0]) * 0.5);
1873 2040
1874 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2041 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1875 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2042 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1876 : ($self->{h} - $size->[1]) * 0.5); 2043 : ($self->{h} - $size->[1]) * 0.5);
2044
2045 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1877 }; 2046 };
1878 2047
1879 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2048# unless ($self->{list}) {
1880 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2049# $self->{list} = CFPlus::OpenGL::glGenList;
1881 2050# CFPlus::OpenGL::glNewList $self->{list};
1882 $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;
1883} 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#}
1884 2067
1885############################################################################# 2068#############################################################################
1886 2069
1887package CFPlus::UI::EntryBase; 2070package CFPlus::UI::EntryBase;
1888 2071
1894 my $class = shift; 2077 my $class = shift;
1895 2078
1896 $class->SUPER::new ( 2079 $class->SUPER::new (
1897 fg => [1, 1, 1], 2080 fg => [1, 1, 1],
1898 bg => [0, 0, 0, 0.2], 2081 bg => [0, 0, 0, 0.2],
2082 outline => [0.6, 0.3, 0.1],
1899 active_bg => [1, 1, 1, 0.5], 2083 active_bg => [0, 0, 1, .2],
1900 active_fg => [0, 0, 0], 2084 active_fg => [1, 1, 1],
2085 active_outline => [1, 1, 0],
1901 can_hover => 1, 2086 can_hover => 1,
1902 can_focus => 1, 2087 can_focus => 1,
1903 valign => 0, 2088 valign => 0,
1904 can_events => 1, 2089 can_events => 1,
2090 ellipsise => 0,
1905 #text => ... 2091 #text => ...
1906 #hidden => "*", 2092 #hidden => "*",
1907 @_ 2093 @_
1908 ) 2094 )
1909} 2095}
1953 my $mod = $ev->{mod}; 2139 my $mod = $ev->{mod};
1954 my $sym = $ev->{sym}; 2140 my $sym = $ev->{sym};
1955 my $uni = $ev->{unicode}; 2141 my $uni = $ev->{unicode};
1956 2142
1957 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;
1958 2146
1959 if ($uni == 8) { 2147 if ($uni == 8) {
1960 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2148 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1961 } elsif ($uni == 127) { 2149 } elsif ($uni == 127) {
1962 substr $text, $self->{cursor}, 1, ""; 2150 substr $text, $self->{cursor}, 1, "";
1963 } elsif ($sym == CFPlus::SDLK_LEFT) { 2151 } elsif ($sym == CFPlus::SDLK_LEFT) {
1964 --$self->{cursor} if $self->{cursor}; 2152 --$self->{cursor} if $self->{cursor};
1965 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2153 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1966 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2154 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1967 } 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 = "";
1968 $self->{cursor} = 0; 2169 $self->{cursor} = 0;
1969 } elsif ($sym == CFPlus::SDLK_END) {
1970 $self->{cursor} = length $text;
1971 } elsif ($uni == 27) { 2170 } elsif ($uni == 27) {
1972 $self->emit ('escape'); 2171 $self->emit ('escape');
1973 } elsif ($uni) { 2172 } elsif ($uni == 0x0d) {
2173 substr $text, $self->{cursor}++, 0, "\012";
2174 } elsif ($uni >= 0x20) {
1974 substr $text, $self->{cursor}++, 0, chr $uni; 2175 substr $text, $self->{cursor}++, 0, chr $uni;
1975 } else { 2176 } else {
1976 return 0; 2177 return 0;
1977 } 2178 }
1978 2179
1979 $self->_set_text ($text); 2180 $self->_set_text ($text);
1980 2181
1981 $self->realloc; 2182 $self->realloc;
2183 $self->update;
1982 2184
1983 1 2185 1
1984} 2186}
1985 2187
1986sub invoke_focus_in { 2188sub invoke_focus_in {
2028 glColor_premultiply @{$self->{bg}}; 2230 glColor_premultiply @{$self->{bg}};
2029 } 2231 }
2030 2232
2031 glEnable GL_BLEND; 2233 glEnable GL_BLEND;
2032 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2234 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2033 glBegin GL_QUADS;
2034 glVertex 0 , 0;
2035 glVertex 0 , $self->{h};
2036 glVertex $self->{w}, $self->{h}; 2235 glRect 0, 0, $self->{w}, $self->{h};
2037 glVertex $self->{w}, 0;
2038 glEnd;
2039 glDisable GL_BLEND; 2236 glDisable GL_BLEND;
2040 2237
2041 $self->SUPER::_draw; 2238 $self->SUPER::_draw;
2042 2239
2043 #TODO: force update every cursor change :( 2240 #TODO: force update every cursor change :(
2045 2242
2046 unless (exists $self->{cur_h}) { 2243 unless (exists $self->{cur_h}) {
2047 my $text = substr $self->{text}, 0, $self->{cursor}; 2244 my $text = substr $self->{text}, 0, $self->{cursor};
2048 utf8::encode $text; 2245 utf8::encode $text;
2049 2246
2050 @$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);
2051 } 2248 }
2052 2249
2250 glColor_premultiply @{$self->{active_fg}};
2053 glBegin GL_LINES; 2251 glBegin GL_LINES;
2054 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};
2055 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};
2056 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;
2057 } 2269 }
2058} 2270}
2271
2272#############################################################################
2059 2273
2060package CFPlus::UI::Entry; 2274package CFPlus::UI::Entry;
2061 2275
2062our @ISA = CFPlus::UI::EntryBase::; 2276our @ISA = CFPlus::UI::EntryBase::;
2063 2277
2066sub invoke_key_down { 2280sub invoke_key_down {
2067 my ($self, $ev) = @_; 2281 my ($self, $ev) = @_;
2068 2282
2069 my $sym = $ev->{sym}; 2283 my $sym = $ev->{sym};
2070 2284
2071 if ($sym == 13) { 2285 if ($ev->{uni} == 0x0d || $sym == 13) {
2072 unshift @{$self->{history}}, 2286 unshift @{$self->{history}},
2073 my $txt = $self->get_text; 2287 my $txt = $self->get_text;
2074 2288
2075 $self->{history_pointer} = -1; 2289 $self->{history_pointer} = -1;
2076 $self->{history_saveback} = ''; 2290 $self->{history_saveback} = '';
2106 1 2320 1
2107} 2321}
2108 2322
2109############################################################################# 2323#############################################################################
2110 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
2111package CFPlus::UI::Button; 2364package CFPlus::UI::ButtonBin;
2112 2365
2113our @ISA = CFPlus::UI::Label::; 2366our @ISA = CFPlus::UI::Bin::;
2114 2367
2115use CFPlus::OpenGL; 2368use CFPlus::OpenGL;
2116 2369
2117my @tex = 2370my @tex =
2118 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2371 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2119 qw(b1_button_active.png); 2372 qw(b1_button_inactive.png b1_button_active.png);
2120 2373
2121sub new { 2374sub new {
2122 my $class = shift; 2375 my $class = shift;
2123 2376
2124 $class->SUPER::new ( 2377 $class->SUPER::new (
2125 padding_x => 4,
2126 padding_y => 4,
2127 fg => [1, 1, 1],
2128 active_fg => [0, 0, 1],
2129 can_hover => 1, 2378 can_hover => 1,
2130 align => 0, 2379 align => 0,
2131 valign => 0, 2380 valign => 0,
2132 can_events => 1, 2381 can_events => 1,
2133 @_ 2382 @_
2145} 2394}
2146 2395
2147sub _draw { 2396sub _draw {
2148 my ($self) = @_; 2397 my ($self) = @_;
2149 2398
2150 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2151
2152 glEnable GL_TEXTURE_2D; 2399 glEnable GL_TEXTURE_2D;
2153 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2400 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2154 glColor 0, 0, 0, 1; 2401 glColor 0, 0, 0, 1;
2155 2402
2403 my $tex = $tex[$GRAB == $self];
2156 $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});
2157 2460
2158 glDisable GL_TEXTURE_2D; 2461 glDisable GL_TEXTURE_2D;
2159 2462
2160 $self->SUPER::_draw; 2463 $self->SUPER::_draw;
2161} 2464}
2218sub _draw { 2521sub _draw {
2219 my ($self) = @_; 2522 my ($self) = @_;
2220 2523
2221 $self->SUPER::_draw; 2524 $self->SUPER::_draw;
2222 2525
2223 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2526 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2224 2527
2225 my ($w, $h) = @$self{qw(w h)}; 2528 my ($w, $h) = @$self{qw(w h)};
2226 2529
2227 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;
2228 2531
2248sub new { 2551sub new {
2249 my $class = shift; 2552 my $class = shift;
2250 2553
2251 my $self = $class->SUPER::new ( 2554 my $self = $class->SUPER::new (
2252 can_events => 0, 2555 can_events => 0,
2556 scale => 1,
2253 @_, 2557 @_,
2254 ); 2558 );
2255 2559
2256 $self->{path} || $self->{tex} 2560 $self->{path} || $self->{tex}
2257 or Carp::croak "'path' or 'tex' attributes required"; 2561 or Carp::croak "'path' or 'tex' attributes required";
2258 2562
2259 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2563 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2260 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;
2261 2565
2262 Scalar::Util::weaken $texture_cache{$self->{path}}; 2566 CFPlus::weaken $texture_cache{$self->{path}};
2263 2567
2264 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2568 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2265 2569
2266 $self 2570 $self
2267} 2571}
2282} 2586}
2283 2587
2284sub size_request { 2588sub size_request {
2285 my ($self) = @_; 2589 my ($self) = @_;
2286 2590
2287 ($self->{tex}{w}, $self->{tex}{h}) 2591 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2288} 2592}
2289 2593
2290sub _draw { 2594sub _draw {
2291 my ($self) = @_; 2595 my ($self) = @_;
2292 2596
2302 } 2606 }
2303 2607
2304 glEnable GL_TEXTURE_2D; 2608 glEnable GL_TEXTURE_2D;
2305 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2609 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2306 2610
2307 $tex->draw_quad (0, 0, $w, $h); 2611 $tex->draw_quad_alpha (0, 0, $w, $h);
2308 2612
2309 glDisable GL_TEXTURE_2D; 2613 glDisable GL_TEXTURE_2D;
2310} 2614}
2311 2615
2312############################################################################# 2616#############################################################################
2331 align => 0, 2635 align => 0,
2332 valign => 0, 2636 valign => 0,
2333 can_events => 1, 2637 can_events => 1,
2334 @_ 2638 @_
2335 ); 2639 );
2640}
2641
2642sub invoke_button_down {
2643 my ($self, $ev, $x, $y) = @_;
2644
2645 1
2336} 2646}
2337 2647
2338sub invoke_button_up { 2648sub invoke_button_up {
2339 my ($self, $ev, $x, $y) = @_; 2649 my ($self, $ev, $x, $y) = @_;
2340 2650
2480 glDisable GL_TEXTURE_2D; 2790 glDisable GL_TEXTURE_2D;
2481} 2791}
2482 2792
2483############################################################################# 2793#############################################################################
2484 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
2485package CFPlus::UI::Gauge; 2933package CFPlus::UI::Gauge;
2486 2934
2487our @ISA = CFPlus::UI::VBox::; 2935our @ISA = CFPlus::UI::VBox::;
2488 2936
2489sub new { 2937sub new {
2614 3062
2615 $self->SUPER::invoke_button_down ($ev, $x, $y); 3063 $self->SUPER::invoke_button_down ($ev, $x, $y);
2616 3064
2617 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3065 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2618 3066
2619 $self->invoke_mouse_motion ($ev, $x, $y) 3067 $self->invoke_mouse_motion ($ev, $x, $y);
3068
3069 1
2620} 3070}
2621 3071
2622sub invoke_mouse_motion { 3072sub invoke_mouse_motion {
2623 my ($self, $ev, $x, $y) = @_; 3073 my ($self, $ev, $x, $y) = @_;
2624 3074
2640sub invoke_mouse_wheel { 3090sub invoke_mouse_wheel {
2641 my ($self, $ev) = @_; 3091 my ($self, $ev) = @_;
2642 3092
2643 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3093 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2644 3094
3095 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
3096
2645 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 3097 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2646 3098
2647 ! ! $delta 3099 1
2648} 3100}
2649 3101
2650sub update { 3102sub update {
2651 my ($self) = @_; 3103 my ($self) = @_;
2652 3104
2760 #font => default_font 3212 #font => default_font
2761 @_, 3213 @_,
2762 3214
2763 layout => (new CFPlus::Layout), 3215 layout => (new CFPlus::Layout),
2764 par => [], 3216 par => [],
3217 max_par => 0,
2765 height => 0, 3218 height => 0,
2766 children => [ 3219 children => [
2767 (new CFPlus::UI::Empty expand => 1), 3220 (new CFPlus::UI::Empty expand => 1),
2768 (new CFPlus::UI::Slider vertical => 1), 3221 (new CFPlus::UI::Slider vertical => 1),
2769 ], 3222 ],
2782} 3235}
2783 3236
2784sub size_request { 3237sub size_request {
2785 my ($self) = @_; 3238 my ($self) = @_;
2786 3239
2787 my ($empty, $slider) = @{ $self->{children} }; 3240 my ($empty, $slider) = $self->visible_children;
2788 3241
2789 local $self->{children} = [$empty, $slider]; 3242 local $self->{children} = [$empty, $slider];
2790 $self->SUPER::size_request 3243 $self->SUPER::size_request
2791} 3244}
2792 3245
2853} 3306}
2854 3307
2855sub current_paragraph { 3308sub current_paragraph {
2856 my ($self) = @_; 3309 my ($self) = @_;
2857 3310
2858 $self->force_uptodate;
2859 $self->{top_paragraph} - 1 3311 $self->{top_paragraph} - 1
2860} 3312}
2861 3313
2862sub scroll_to { 3314sub scroll_to {
2863 my ($self, $para) = @_; 3315 my ($self, $para) = @_;
2864 3316
2865 $self->force_uptodate;
2866
2867 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para; 3317 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2868 3318
2869 $self->{children}[1]->set_value ($self->{par}[$para]{y}); 3319 $self->{scroll_to} = $para;
3320 $self->update;
2870} 3321}
2871 3322
2872sub clear { 3323sub clear {
2873 my ($self) = @_; 3324 my ($self) = @_;
2874 3325
2896 3347
2897 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 3348 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2898 push @{$self->{par}}, $para; 3349 push @{$self->{par}}, $para;
2899 } 3350 }
2900 3351
3352 if (my $max = $self->{max_par}) {
3353 shift @{$self->{par}} while @{$self->{par}} > $max;
3354 }
3355
2901 $self->{need_reflow}++; 3356 $self->{need_reflow}++;
2902 $self->update; 3357 $self->update;
2903} 3358}
2904 3359
2905sub scroll_to_bottom { 3360sub scroll_to_bottom {
2906 my ($self) = @_; 3361 my ($self) = @_;
2907 3362
2908 $self->{scroll_to_bottom} = 1; 3363 $self->{scroll_to} = $#{$self->{par}};
2909 $self->update; 3364 $self->update;
2910} 3365}
2911 3366
2912sub force_uptodate { 3367sub force_uptodate {
2913 my ($self) = @_; 3368 my ($self) = @_;
2914 3369
2915 if (delete $self->{need_reflow}) { 3370 if (delete $self->{need_reflow}) {
2916 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3371 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2917 3372
2918 my $height = 0; 3373 my $height = 0;
2919 my $paridx;
2920 my $top_paragraph;
2921 my $top = int $self->{children}[1]{range}[0];
2922 3374
2923 for my $para (@{$self->{par}}) { 3375 for my $para (@{$self->{par}}) {
2924 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { 3376 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2925 my $layout = $self->get_layout ($para); 3377 my $layout = $self->get_layout ($para);
2926 my ($w, $h) = $layout->size; 3378 my ($w, $h) = $layout->size;
2929 $para->{h} = $h; 3381 $para->{h} = $h;
2930 $para->{wrapped} = $layout->has_wrapped; 3382 $para->{wrapped} = $layout->has_wrapped;
2931 } 3383 }
2932 3384
2933 $para->{y} = $height; 3385 $para->{y} = $height;
2934
2935 $paridx++;
2936 $top_paragraph ||= $paridx if $height >= $top;
2937
2938 $height += $para->{h}; 3386 $height += $para->{h};
2939 } 3387 }
2940 3388
2941 $self->{top_paragraph} = $top_paragraph;
2942 $self->{height} = $height; 3389 $self->{height} = $height;
2943
2944 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); 3390 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2945 3391
2946 delete $self->{texture}; 3392 delete $self->{texture};
2947 } 3393 }
2948 3394
2949 if (delete $self->{scroll_to_bottom}) { 3395 if (my $paridx = delete $self->{scroll_to}) {
2950 $self->{children}[1]->set_value (1e10); 3396 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2951 } 3397 }
2952} 3398}
2953 3399
2954sub update { 3400sub update {
2955 my ($self) = @_; 3401 my ($self) = @_;
2967 3413
2968 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3414 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2969 glClearColor 0, 0, 0, 0; 3415 glClearColor 0, 0, 0, 0;
2970 glClear GL_COLOR_BUFFER_BIT; 3416 glClear GL_COLOR_BUFFER_BIT;
2971 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;
2972 my $top = int $self->{children}[1]{range}[0]; 3426 my $top = int $self->{children}[1]{range}[0];
2973 3427
2974 my $y0 = $top; 3428 my $y0 = $top;
2975 my $y1 = $top + $H; 3429 my $y1 = $top + $H;
2976 3430
2980 3434
2981 if ($y0 < $y + $h && $y < $y1) { 3435 if ($y0 < $y + $h && $y < $y1) {
2982 my $layout = $self->get_layout ($para); 3436 my $layout = $self->get_layout ($para);
2983 3437
2984 $layout->render ($para->{indent}, $y - $y0); 3438 $layout->render ($para->{indent}, $y - $y0);
3439 $layout->draw;
2985 3440
2986 if (my @w = @{ $para->{widget} }) { 3441 if (my @w = @{ $para->{widget} }) {
2987 my @s = $layout->get_shapes; 3442 my @s = $layout->get_shapes;
2988 3443
2989 for (@w) { 3444 for (@w) {
2994 3449
2995 $_->draw; 3450 $_->draw;
2996 } 3451 }
2997 } 3452 }
2998 } 3453 }
3454
3455 $paridx++;
3456 $top_paragraph ||= $paridx if $y >= $top;
2999 } 3457 }
3458
3459 $self->{top_paragraph} = $top_paragraph;
3000 }; 3460 };
3001 }); 3461 });
3002} 3462}
3003 3463
3004sub reconfigure { 3464sub reconfigure {
3106} 3566}
3107 3567
3108sub set_tooltip_from { 3568sub set_tooltip_from {
3109 my ($self, $widget) = @_; 3569 my ($self, $widget) = @_;
3110 3570
3111 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3112 if $widget->{tooltip} =~ /^#(.*)$/;
3113
3114 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 =~ /^#(.*)$/;
3115 3576
3116 if ($ENV{CFPLUS_DEBUG} & 2) { 3577 if ($ENV{CFPLUS_DEBUG} & 2) {
3117 $tooltip .= "\n\n" . (ref $widget) . "\n" 3578 $tip .= "\n\n" . (ref $widget) . "\n"
3118 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3579 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3119 . "req $widget->{req_w} $widget->{req_h}\n" 3580 . "req $widget->{req_w} $widget->{req_h}\n"
3120 . "visible $widget->{visible}"; 3581 . "visible $widget->{visible}";
3121 } 3582 }
3122 3583
3123 $tooltip =~ s/^\n+//; 3584 $tip =~ s/^\n+//;
3124 $tooltip =~ s/\n+$//; 3585 $tip =~ s/\n+$//;
3125 3586
3126 $self->add (new CFPlus::UI::Label 3587 $self->add (new CFPlus::UI::Label
3127 markup => $tooltip, 3588 markup => $tip,
3128 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3589 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3129 fontsize => 0.8, 3590 fontsize => 0.8,
3130 style => 1, # FLAG_INVERSE 3591 style => 1, # FLAG_INVERSE
3131 ellipsise => 0, 3592 ellipsise => 0,
3132 font => ($widget->{tooltip_font} || $::FONT_PROP), 3593 font => ($widget->{tooltip_font} || $::FONT_PROP),
3170} 3631}
3171 3632
3172sub _draw { 3633sub _draw {
3173 my ($self) = @_; 3634 my ($self) = @_;
3174 3635
3175 glTranslate 0.375, 0.375;
3176
3177 my ($w, $h) = @$self{qw(w h)}; 3636 my ($w, $h) = @$self{qw(w h)};
3178 3637
3179 glColor 1, 0.8, 0.4; 3638 glColor 1, 0.8, 0.4;
3180 glBegin GL_QUADS; 3639 glRect 0, 0, $w, $h;
3181 glVertex 0 , 0;
3182 glVertex 0 , $h;
3183 glVertex $w, $h;
3184 glVertex $w, 0;
3185 glEnd;
3186 3640
3187 glColor 0, 0, 0; 3641 glColor 0, 0, 0;
3188 glBegin GL_LINE_LOOP; 3642 glRect_lineloop .5, .5, $w + .5, $h + .5;
3189 glVertex 0 , 0;
3190 glVertex 0 , $h;
3191 glVertex $w, $h;
3192 glVertex $w, 0;
3193 glEnd;
3194 3643
3195 glTranslate 2 - 0.375, 2 - 0.375; 3644 glTranslate 2, 2;
3196 3645
3197 $self->SUPER::_draw; 3646 $self->SUPER::_draw;
3198} 3647}
3199 3648
3200############################################################################# 3649#############################################################################
3207 3656
3208sub new { 3657sub new {
3209 my $class = shift; 3658 my $class = shift;
3210 3659
3211 my $self = $class->SUPER::new ( 3660 my $self = $class->SUPER::new (
3661 size_w => 32,
3662 size_h => 8,
3212 aspect => 1, 3663 aspect => 1,
3213 can_events => 0, 3664 can_events => 0,
3214 @_, 3665 @_,
3215 ); 3666 );
3216 3667
3217 if ($self->{anim} && $self->{animspeed}) { 3668 if ($self->{anim} && $self->{animspeed}) {
3218 Scalar::Util::weaken (my $widget = $self); 3669 CFPlus::weaken (my $widget = $self);
3219 3670
3671 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3220 $self->{timer} = Event->timer ( 3673 $self->{timer} = Event->timer (
3221 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3222 hard => 1, 3674 parked => 1,
3223 interval => $self->{animspeed},
3224 cb => sub { 3675 cb => sub {
3676 return unless $::CONN;
3677
3678 my $w = $widget
3679 or return;
3680
3225 ++$widget->{frame}; 3681 ++$w->{frame};
3682 $w->update_face;
3683
3684 # somehow, $widget can go away
3226 $widget->update; 3685 $w->update;
3686 $w->update_timer;
3227 }, 3687 },
3228 ); 3688 );
3689
3690 $self->update_face;
3691 $self->update_timer;
3229 } 3692 }
3230 3693
3231 $self 3694 $self
3232} 3695}
3233 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
3234sub size_request { 3733sub size_request {
3235 (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)
3236} 3751}
3237 3752
3238sub update { 3753sub update {
3239 my ($self) = @_; 3754 my ($self) = @_;
3240 3755
3241 return unless $self->{visible}; 3756 return unless $self->{visible};
3242 3757
3243 $self->SUPER::update; 3758 $self->SUPER::update;
3244} 3759}
3245 3760
3761sub invoke_visibility_change {
3762 my ($self) = @_;
3763
3764 $self->update_timer;
3765
3766 0
3767}
3768
3246sub _draw { 3769sub _draw {
3247 my ($self) = @_; 3770 my ($self) = @_;
3248 3771
3249 return unless $::CONN;
3250
3251 $self->SUPER::_draw; 3772 $self->SUPER::_draw;
3252 3773
3253 my $face; 3774 if (my $tex = $self->{tex}) {
3254
3255 if ($self->{frame}) {
3256 my $anim = $::CONN->{anim}[$self->{anim}];
3257
3258 $face = $anim->[ $self->{frame} % @$anim ]
3259 if $anim && @$anim;
3260 }
3261
3262 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3263
3264 if ($tex) {
3265 glEnable GL_TEXTURE_2D; 3775 glEnable GL_TEXTURE_2D;
3266 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3776 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3267 glColor 0, 0, 0, 1; 3777 glColor 0, 0, 0, 1;
3268 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3778 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3269 glDisable GL_TEXTURE_2D; 3779 glDisable GL_TEXTURE_2D;
3271} 3781}
3272 3782
3273sub destroy { 3783sub destroy {
3274 my ($self) = @_; 3784 my ($self) = @_;
3275 3785
3276 $self->{timer}->cancel 3786 (delete $self->{timer})->cancel
3277 if $self->{timer}; 3787 if $self->{timer};
3278 3788
3279 $self->SUPER::destroy; 3789 $self->SUPER::destroy;
3280} 3790}
3281 3791
3283 3793
3284package CFPlus::UI::Buttonbar; 3794package CFPlus::UI::Buttonbar;
3285 3795
3286our @ISA = CFPlus::UI::HBox::; 3796our @ISA = CFPlus::UI::HBox::;
3287 3797
3288# TODO: should actualyl wrap buttons and other goodies. 3798# TODO: should actually wrap buttons and other goodies.
3289 3799
3290############################################################################# 3800#############################################################################
3291 3801
3292package CFPlus::UI::Menu; 3802package CFPlus::UI::Menu;
3293 3803
3321 children => [ 3831 children => [
3322 (new CFPlus::UI::Label markup => $left, expand => 1), 3832 (new CFPlus::UI::Label markup => $left, expand => 1),
3323 (new CFPlus::UI::Label markup => $right, align => +1), 3833 (new CFPlus::UI::Label markup => $right, align => +1),
3324 ], 3834 ],
3325 ; 3835 ;
3326 3836
3327 } else { 3837 } else {
3328 $widget = new CFPlus::UI::Label 3838 $widget = new CFPlus::UI::Label
3329 can_hover => 1, 3839 can_hover => 1,
3330 can_events => 1, 3840 can_events => 1,
3331 markup => $widget, 3841 markup => $widget,
3456 3966
3457############################################################################# 3967#############################################################################
3458 3968
3459package CFPlus::UI::Notebook; 3969package CFPlus::UI::Notebook;
3460 3970
3971use CFPlus::OpenGL;
3972
3461our @ISA = CFPlus::UI::VBox::; 3973our @ISA = CFPlus::UI::VBox::;
3462 3974
3463sub new { 3975sub new {
3464 my $class = shift; 3976 my $class = shift;
3465 3977
3466 my $self = $class->SUPER::new ( 3978 my $self = $class->SUPER::new (
3467 buttonbar => (new CFPlus::UI::Buttonbar), 3979 buttonbar => (new CFPlus::UI::Buttonbar),
3468 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3980 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3981 active_outline => [.7, .7, 0.2],
3469 # filter => # will be put between multiplexer and $self 3982 # filter => # will be put between multiplexer and $self
3470 @_, 3983 @_,
3471 ); 3984 );
3472 3985
3473 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3986 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3474 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3987 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3475 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
3476 $self 4023 $self
3477} 4024}
3478 4025
3479sub 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 {
3480 my ($self, $title, $widget, $tooltip) = @_; 4044 my ($self, $title, $widget, $tooltip) = @_;
3481 4045
3482 Scalar::Util::weaken $self; 4046 $title = [$title, $tooltip] unless ref $title;
4047 $widget->{c_tab} = $title;
3483 4048
3484 $self->{buttonbar}->add (new CFPlus::UI::Button
3485 markup => $title,
3486 tooltip => $tooltip,
3487 on_activate => sub { $self->set_current_page ($widget) },
3488 );
3489
3490 $self->{multiplexer}->add ($widget); 4049 $self->add ($widget);
3491} 4050}
3492 4051
3493sub get_current_page { 4052sub get_current_page {
3494 my ($self) = @_; 4053 my ($self) = @_;
3495 4054
3499sub set_current_page { 4058sub set_current_page {
3500 my ($self, $page) = @_; 4059 my ($self, $page) = @_;
3501 4060
3502 $self->{multiplexer}->set_current_page ($page); 4061 $self->{multiplexer}->set_current_page ($page);
3503 $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 }
3504} 4083}
3505 4084
3506############################################################################# 4085#############################################################################
3507 4086
3508package CFPlus::UI::Selector; 4087package CFPlus::UI::Selector;
3571 my $self = $class->SUPER::new ( 4150 my $self = $class->SUPER::new (
3572 fontsize => 0.8, 4151 fontsize => 0.8,
3573 @_, 4152 @_,
3574 ); 4153 );
3575 4154
3576 Scalar::Util::weaken (my $this = $self); 4155 CFPlus::weaken (my $this = $self);
3577 4156
3578 $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 });
3579 4158
3580 $self 4159 $self
3581} 4160}
3704 $self->SUPER::destroy; 4283 $self->SUPER::destroy;
3705} 4284}
3706 4285
3707############################################################################# 4286#############################################################################
3708 4287
3709package CFPlus::UI::Inventory;
3710
3711our @ISA = CFPlus::UI::Table::;
3712
3713sub new {
3714 my $class = shift;
3715
3716 my $self = $class->SUPER::new (
3717 col_expand => [0, 1, 0],
3718 items => [],
3719 @_,
3720 );
3721
3722 $self->set_sort_order (undef);
3723
3724 $self
3725}
3726
3727sub update_items {
3728 my ($self) = @_;
3729
3730 $self->clear;
3731
3732 my @item = $self->{sort}->(@{ $self->{items} });
3733
3734 my @adds;
3735 my $row = 0;
3736 for my $item ($self->{sort}->(@{ $self->{items} })) {
3737 CFPlus::Item::update_widgets $item;
3738
3739 push @adds, 0, $row, $item->{face_widget};
3740 push @adds, 1, $row, $item->{desc_widget};
3741 push @adds, 2, $row, $item->{weight_widget};
3742
3743 $row++;
3744 }
3745
3746 $self->add (@adds);
3747}
3748
3749sub set_sort_order {
3750 my ($self, $order) = @_;
3751
3752 $self->{sort} = $order ||= sub {
3753 sort {
3754 $a->{type} <=> $b->{type}
3755 or $a->{name} cmp $b->{name}
3756 } @_
3757 };
3758
3759 $self->update_items;
3760}
3761
3762sub set_items {
3763 my ($self, $items) = @_;
3764
3765 $self->{items} = [$items ? values %$items : ()];
3766 $self->update_items;
3767}
3768
3769#############################################################################
3770
3771package CFPlus::UI::SpellList;
3772
3773our @ISA = CFPlus::UI::Table::;
3774
3775sub new {
3776 my $class = shift;
3777
3778 my $self = $class->SUPER::new (
3779 binding => [],
3780 commands => [],
3781 @_,
3782 )
3783}
3784
3785my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3786
3787my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3788 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3789my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3790 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3791my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3792 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3793my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3794 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3795my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3796 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3797
3798sub rebuild_spell_list {
3799 my ($self) = @_;
3800
3801 $CFPlus::UI::ROOT->on_refresh ($self => sub {
3802 $self->clear;
3803
3804 return unless $::CONN;
3805
3806 $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3807 $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL);
3808 $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3809 $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3810 $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3811
3812 my $row = 0;
3813
3814 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3815 my $spell = $self->{spell}{$_};
3816
3817 $row++;
3818
3819 my $spell_cb = sub {
3820 my ($widget, $ev) = @_;
3821
3822 if ($ev->{button} == 1) {
3823 $::CONN->user_send ("cast $spell->{name}");
3824 } elsif ($ev->{button} == 2) {
3825 $::CONN->user_send ("invoke $spell->{name}");
3826 } elsif ($ev->{button} == 3) {
3827 (new CFPlus::UI::Menu
3828 items => [
3829 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3830 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3831 ],
3832 )->popup ($ev);
3833 } else {
3834 return 0;
3835 }
3836
3837 1
3838 };
3839
3840 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3841
3842 #TODO: add path info to tooltip
3843 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3844
3845 $self->add (0, $row, new CFPlus::UI::Face
3846 face => $spell->{face},
3847 can_hover => 1,
3848 can_events => 1,
3849 tooltip => $tooltip,
3850 on_button_down => $spell_cb,
3851 );
3852
3853 $self->add (1, $row, new CFPlus::UI::Label
3854 expand => 1,
3855 text => $spell->{name},
3856 can_hover => 1,
3857 can_events => 1,
3858 tooltip => $tooltip,
3859 on_button_down => $spell_cb,
3860 );
3861
3862 $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3863 $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3864 $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3865 $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3866 }
3867 });
3868}
3869
3870sub add_spell {
3871 my ($self, $spell) = @_;
3872
3873 $self->{spell}->{$spell->{name}} = $spell;
3874 $self->rebuild_spell_list;
3875}
3876
3877sub remove_spell {
3878 my ($self, $spell) = @_;
3879
3880 delete $self->{spell}->{$spell->{name}};
3881 $self->rebuild_spell_list;
3882}
3883
3884sub clear_spells {
3885 my ($self) = @_;
3886
3887 $self->{spell} = {};
3888 $self->rebuild_spell_list;
3889}
3890
3891#############################################################################
3892
3893package CFPlus::UI::Root; 4288package CFPlus::UI::Root;
3894 4289
3895our @ISA = CFPlus::UI::Container::; 4290our @ISA = CFPlus::UI::Container::;
3896 4291
3897use List::Util qw(min max); 4292use List::Util qw(min max);
3904 my $self = $class->SUPER::new ( 4299 my $self = $class->SUPER::new (
3905 visible => 1, 4300 visible => 1,
3906 @_, 4301 @_,
3907 ); 4302 );
3908 4303
3909 Scalar::Util::weaken ($self->{root} = $self); 4304 CFPlus::weaken ($self->{root} = $self);
3910 4305
3911 $self 4306 $self
3912} 4307}
3913 4308
3914sub size_request { 4309sub size_request {
3962} 4357}
3963 4358
3964sub update { 4359sub update {
3965 my ($self) = @_; 4360 my ($self) = @_;
3966 4361
3967 $::WANT_REFRESH++; 4362 $::WANT_REFRESH->start;
3968} 4363}
3969 4364
3970sub add { 4365sub add {
3971 my ($self, @children) = @_; 4366 my ($self, @children) = @_;
3972 4367
4009 while ($self->{refresh_hook}) { 4404 while ($self->{refresh_hook}) {
4010 $_->() 4405 $_->()
4011 for values %{delete $self->{refresh_hook}}; 4406 for values %{delete $self->{refresh_hook}};
4012 } 4407 }
4013 4408
4014 if ($self->{realloc}) { 4409 while ($self->{realloc}) {
4015 my %queue; 4410 my %queue;
4016 my @queue; 4411 my @queue;
4017 my $widget; 4412 my $widget;
4018 4413
4019 outer: 4414 outer:
4066 } 4461 }
4067 } 4462 }
4068 4463
4069 delete $self->{realloc}{$widget+0}; 4464 delete $self->{realloc}{$widget+0};
4070 } 4465 }
4071 }
4072 4466
4073 while (my $size_alloc = delete $self->{size_alloc}) { 4467 while (my $size_alloc = delete $self->{size_alloc}) {
4074 my @queue = sort { $b->{visible} <=> $a->{visible} } 4468 my @queue = sort { $a->{visible} <=> $b->{visible} }
4075 values %$size_alloc; 4469 values %$size_alloc;
4076 4470
4077 while () { 4471 while () {
4078 my $widget = pop @queue || last; 4472 my $widget = pop @queue || last;
4079 4473
4080 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4474 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4081 4475
4082 $w = 0 if $w < 0;
4083 $h = 0 if $h < 0;
4084
4085 $w = max $widget->{min_w}, $w; 4476 $w = max $widget->{min_w}, $w;
4086 $h = max $widget->{min_h}, $h; 4477 $h = max $widget->{min_h}, $h;
4087 4478
4088# $w = min $self->{w} - $widget->{x}, $w if $self->{w}; 4479# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4089# $h = min $self->{h} - $widget->{y}, $h if $self->{h}; 4480# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4090 4481
4091 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4482 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4092 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4483 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4093 4484
4094 $w = int $w + 0.5; 4485 $w = int $w + 0.5;
4095 $h = int $h + 0.5; 4486 $h = int $h + 0.5;
4096 4487
4097 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}) {
4098 $widget->{old_w} = $widget->{w}; 4489 $widget->{old_w} = $widget->{w};
4099 $widget->{old_h} = $widget->{h}; 4490 $widget->{old_h} = $widget->{h};
4100 4491
4101 $widget->{w} = $w; 4492 $widget->{w} = $w;
4102 $widget->{h} = $h; 4493 $widget->{h} = $h;
4103 4494
4104 $widget->emit (size_allocate => $w, $h); 4495 $widget->emit (size_allocate => $w, $h);
4496 }
4105 } 4497 }
4106 } 4498 }
4107 } 4499 }
4108 4500
4109 while ($self->{post_alloc_hook}) { 4501 while ($self->{post_alloc_hook}) {
4110 $_->() 4502 $_->()
4111 for values %{delete $self->{post_alloc_hook}}; 4503 for values %{delete $self->{post_alloc_hook}};
4112 } 4504 }
4113
4114 4505
4115 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4506 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4116 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4507 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4117 glClear GL_COLOR_BUFFER_BIT; 4508 glClear GL_COLOR_BUFFER_BIT;
4118 4509
4123 glLoadIdentity; 4514 glLoadIdentity;
4124 4515
4125 { 4516 {
4126 package CFPlus::UI::Base; 4517 package CFPlus::UI::Base;
4127 4518
4128 ($draw_x, $draw_y, $draw_w, $draw_h) = 4519 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4129 (0, 0, $self->{w}, $self->{h}); 4520 (0, 0, $self->{w}, $self->{h});
4130 }
4131 4521
4132 $self->_draw; 4522 $self->_draw;
4523 }
4133} 4524}
4134 4525
4135############################################################################# 4526#############################################################################
4136 4527
4137package CFPlus::UI; 4528package CFPlus::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines