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.259 by root, Tue May 30 07:13:09 2006 UTC vs.
Revision 1.278 by root, Sun Jun 4 23:05:06 2006 UTC

40 40
41 $LAYOUT = $layout; 41 $LAYOUT = $layout;
42} 42}
43 43
44sub check_tooltip { 44sub check_tooltip {
45 return if $ENV{CFPLUS_DEBUG} & 8;
46
45 if (!$GRAB) { 47 if (!$GRAB) {
46 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 48 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
47 if (length $widget->{tooltip}) { 49 if (length $widget->{tooltip}) {
48
49 if ($TOOLTIP->{owner} != $widget) { 50 if ($TOOLTIP->{owner} != $widget) {
50 $TOOLTIP->hide; 51 $TOOLTIP->hide;
51 52
52 $TOOLTIP->{owner} = $widget; 53 $TOOLTIP->{owner} = $widget;
53 54
170sub rescale_widgets { 171sub rescale_widgets {
171 my ($sx, $sy) = @_; 172 my ($sx, $sy) = @_;
172 173
173 for my $widget (values %WIDGET) { 174 for my $widget (values %WIDGET) {
174 if ($widget->{is_toplevel}) { 175 if ($widget->{is_toplevel}) {
175 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 176 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
176 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 177 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
177 178
178 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 179 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
179 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 180 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
180 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w}; 181 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
181 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/; 182 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 183 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
183 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h}; 184 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
184 185
185 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 186 $widget->{x} -= int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
186 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 187 $widget->{y} -= int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
187 188
188 } 189 }
189 } 190 }
190 191
191 reconfigure_widgets; 192 reconfigure_widgets;
370sub size_allocate { 371sub size_allocate {
371 # nothing to be done 372 # nothing to be done
372} 373}
373 374
374sub children { 375sub children {
376 # nop
377}
378
379sub visible_children {
380 $_[0]->children
375} 381}
376 382
377sub set_max_size { 383sub set_max_size {
378 my ($self, $w, $h) = @_; 384 my ($self, $w, $h) = @_;
379 385
438 444
439 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus 445 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
440 unless $FOCUS; 446 unless $FOCUS;
441} 447}
442 448
443sub mouse_motion { } 449sub mouse_motion { 0 }
444sub button_up { } 450sub button_up { 0 }
445sub key_down { } 451sub key_down { 0 }
446sub key_up { } 452sub key_up { 0 }
447 453
448sub button_down { 454sub button_down {
449 my ($self, $ev, $x, $y) = @_; 455 my ($self, $ev, $x, $y) = @_;
450 456
451 $self->focus_in; 457 $self->focus_in;
452}
453 458
454sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 459 0
455sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 460}
456sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
457sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
458sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
459 461
460sub find_widget { 462sub find_widget {
461 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
462 464
463 return () unless $self->{can_events}; 465 return () unless $self->{can_events};
525 527
526 $self->realloc; 528 $self->realloc;
527 $self->update; 529 $self->update;
528} 530}
529 531
532# using global variables seems a bit hacky, but passing through all drawing
533# functions seems pointless.
534our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
535
530sub draw { 536sub draw {
531 my ($self) = @_; 537 my ($self) = @_;
532 538
533 return unless $self->{h} && $self->{w}; 539 return unless $self->{h} && $self->{w};
540
541 # update screen rectangle
542 local $draw_x = $draw_x + $self->{x};
543 local $draw_y = $draw_y + $self->{y};
544 local $draw_w = $draw_x + $self->{w};
545 local $draw_h = $draw_y + $self->{h};
546
547 # skip widgets that are entirely outside the drawing area
548 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
549 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
534 550
535 glPushMatrix; 551 glPushMatrix;
536 glTranslate $self->{x}, $self->{y}, 0; 552 glTranslate $self->{x}, $self->{y}, 0;
537 $self->_draw;
538 glPopMatrix;
539 553
540 if ($self == $HOVER && $self->{can_hover}) { 554 if ($self == $HOVER && $self->{can_hover}) {
541 my ($x, $y) = @$self{qw(x y)};
542
543 glColor 1, 0.8, 0.5, 0.2; 555 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
544 glEnable GL_BLEND; 556 glEnable GL_BLEND;
545 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 557 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
546 glBegin GL_QUADS; 558 glBegin GL_QUADS;
547 glVertex $x , $y; 559 glVertex 0 , 0;
548 glVertex $x + $self->{w}, $y; 560 glVertex $self->{w}, 0;
549 glVertex $x + $self->{w}, $y + $self->{h}; 561 glVertex $self->{w}, $self->{h};
550 glVertex $x , $y + $self->{h}; 562 glVertex 0 , $self->{h};
551 glEnd; 563 glEnd;
552 glDisable GL_BLEND; 564 glDisable GL_BLEND;
553 } 565 }
554 566
555 if ($ENV{CFPLUS_DEBUG} & 1) { 567 if ($ENV{CFPLUS_DEBUG} & 1) {
556 glPushMatrix; 568 glPushMatrix;
557 glColor 1, 1, 0, 1; 569 glColor 1, 1, 0, 1;
558 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 570 glTranslate 0.375, 0.375;
559 glBegin GL_LINE_LOOP; 571 glBegin GL_LINE_LOOP;
560 glVertex 0 , 0; 572 glVertex 0 , 0;
561 glVertex $self->{w} - 1, 0; 573 glVertex $self->{w} - 1, 0;
562 glVertex $self->{w} - 1, $self->{h} - 1; 574 glVertex $self->{w} - 1, $self->{h} - 1;
563 glVertex 0 , $self->{h} - 1; 575 glVertex 0 , $self->{h} - 1;
564 glEnd; 576 glEnd;
565 glPopMatrix; 577 glPopMatrix;
566 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 578 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
567 } 579 }
580
581 $self->_draw;
582 glPopMatrix;
568} 583}
569 584
570sub _draw { 585sub _draw {
571 my ($self) = @_; 586 my ($self) = @_;
572 587
610 625
611 if ($color && (@$color < 4 || $color->[3])) { 626 if ($color && (@$color < 4 || $color->[3])) {
612 my ($w, $h) = @$self{qw(w h)}; 627 my ($w, $h) = @$self{qw(w h)};
613 628
614 glEnable GL_BLEND; 629 glEnable GL_BLEND;
615 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 630 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
616 glColor @$color; 631 glColor_premultiply @$color;
617 632
618 glBegin GL_QUADS; 633 glBegin GL_QUADS;
619 glVertex 0 , 0; 634 glVertex 0 , 0;
620 glVertex 0 , $h; 635 glVertex 0 , $h;
621 glVertex $w, $h; 636 glVertex $w, $h;
652our @ISA = CFClient::UI::Base::; 667our @ISA = CFClient::UI::Base::;
653 668
654sub new { 669sub new {
655 my ($class, %arg) = @_; 670 my ($class, %arg) = @_;
656 671
657 my $children = delete $arg{children} || []; 672 my $children = delete $arg{children};
658 673
659 my $self = $class->SUPER::new ( 674 my $self = $class->SUPER::new (
660 children => [], 675 children => [],
661 can_events => 0, 676 can_events => 0,
662 %arg, 677 %arg,
663 ); 678 );
679
664 $self->add ($_) for @$children; 680 $self->add (@$children)
681 if $children;
665 682
666 $self 683 $self
667} 684}
668 685
669sub add { 686sub add {
717 $x -= $self->{x}; 734 $x -= $self->{x};
718 $y -= $self->{y}; 735 $y -= $self->{y};
719 736
720 my $res; 737 my $res;
721 738
722 for (reverse @{ $self->{children} }) { 739 for (reverse $self->visible_children) {
723 $res = $_->find_widget ($x, $y) 740 $res = $_->find_widget ($x, $y)
724 and return $res; 741 and return $res;
725 } 742 }
726 743
727 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) 744 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
776 $self->{children}[0]->configure (0, 0, $w, $h); 793 $self->{children}[0]->configure (0, 0, $w, $h);
777} 794}
778 795
779############################################################################# 796#############################################################################
780 797
798# back-buffered drawing area
799
781package CFClient::UI::Window; 800package CFClient::UI::Window;
782 801
783our @ISA = CFClient::UI::Bin::; 802our @ISA = CFClient::UI::Bin::;
784 803
785use CFClient::OpenGL; 804use CFClient::OpenGL;
803 $self->SUPER::size_allocate ($w, $h); 822 $self->SUPER::size_allocate ($w, $h);
804 $self->update; 823 $self->update;
805} 824}
806 825
807sub _render { 826sub _render {
827 my ($self) = @_;
828
808 $_[0]{children}[0]->draw; 829 $self->{children}[0]->draw;
809} 830}
810 831
811sub render_child { 832sub render_child {
812 my ($self) = @_; 833 my ($self) = @_;
813 834
814 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 835 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
815 glClearColor 0, 0, 0, 0; 836 glClearColor 0, 0, 0, 0;
816 glClear GL_COLOR_BUFFER_BIT; 837 glClear GL_COLOR_BUFFER_BIT;
817 838
839 {
840 package CFClient::UI::Base;
841
842 ($draw_x, $draw_y, $draw_w, $draw_h) =
843 (0, 0, $self->{w}, $self->{h});
844 }
845
818 $self->_render; 846 $self->_render;
819 }; 847 };
820} 848}
821 849
822sub _draw { 850sub _draw {
823 my ($self) = @_; 851 my ($self) = @_;
824 852
825 my ($w, $h) = ($self->w, $self->h); 853 my ($w, $h) = @$self{qw(w h)};
826 854
827 my $tex = $self->{texture} 855 my $tex = $self->{texture}
828 or return; 856 or return;
829 857
830 glEnable GL_TEXTURE_2D; 858 glEnable GL_TEXTURE_2D;
831 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
860 glEnable GL_BLEND;
861 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
832 glColor 1, 1, 1, 1; 862 glColor 0, 0, 0, 1;
833 863
834 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 864 $tex->draw_quad (0, 0, $w, $h);
835 865
866 glDisable GL_BLEND;
836 glDisable GL_TEXTURE_2D; 867 glDisable GL_TEXTURE_2D;
837} 868}
838 869
839############################################################################# 870#############################################################################
840 871
913} 944}
914 945
915sub _render { 946sub _render {
916 my ($self) = @_; 947 my ($self) = @_;
917 948
949 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x};
950 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y};
951
918 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 952 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
919 953
920 $self->SUPER::_render; 954 $self->SUPER::_render;
921} 955}
922 956
925package CFClient::UI::ScrolledWindow; 959package CFClient::UI::ScrolledWindow;
926 960
927our @ISA = CFClient::UI::HBox::; 961our @ISA = CFClient::UI::HBox::;
928 962
929sub new { 963sub new {
930 my $class = shift; 964 my ($class, %arg) = @_;
965
966 my $child = delete $arg{child};
931 967
932 my $self; 968 my $self;
933 969
934 my $slider = new CFClient::UI::Slider 970 my $slider = new CFClient::UI::Slider
935 vertical => 1, 971 vertical => 1,
940 ; 976 ;
941 977
942 $self = $class->SUPER::new ( 978 $self = $class->SUPER::new (
943 vp => (new CFClient::UI::ViewPort expand => 1), 979 vp => (new CFClient::UI::ViewPort expand => 1),
944 slider => $slider, 980 slider => $slider,
945 @_, 981 %arg,
946 ); 982 );
947 983
948 $self->{vp}->add ($self->{scrolled});
949 $self->add ($self->{vp});
950 $self->add ($self->{slider}); 984 $self->SUPER::add ($self->{vp}, $self->{slider});
985 $self->add ($child) if $child;
951 986
952 $self 987 $self
988}
989
990sub add {
991 my ($self, $widget) = @_;
992
993 $self->{vp}->add ($self->{child} = $widget);
953} 994}
954 995
955sub update { 996sub update {
956 my ($self) = @_; 997 my ($self) = @_;
957 998
996 1037
997 if ($self->{bg}) { 1038 if ($self->{bg}) {
998 my ($w, $h) = @$self{qw(w h)}; 1039 my ($w, $h) = @$self{qw(w h)};
999 1040
1000 glEnable GL_BLEND; 1041 glEnable GL_BLEND;
1001 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1042 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1002 glColor @{ $self->{bg} }; 1043 glColor_premultiply @{ $self->{bg} };
1003 1044
1004 glBegin GL_QUADS; 1045 glBegin GL_QUADS;
1005 glVertex 0 , 0; 1046 glVertex 0 , 0;
1006 glVertex 0 , $h; 1047 glVertex 0 , $h;
1007 glVertex $w, $h; 1048 glVertex $w, $h;
1029my @border = 1070my @border =
1030 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1071 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1031 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1072 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1032 1073
1033sub new { 1074sub new {
1034 my $class = shift; 1075 my ($class, %arg) = @_;
1076
1077 my $title = delete $arg{title};
1035 1078
1036 my $self = $class->SUPER::new ( 1079 my $self = $class->SUPER::new (
1037 bg => [1, 1, 1, 1], 1080 bg => [1, 1, 1, 1],
1038 border_bg => [1, 1, 1, 1], 1081 border_bg => [1, 1, 1, 1],
1039 border => 0.6, 1082 border => 0.6,
1040 can_events => 1, 1083 can_events => 1,
1041 min_w => 16, 1084 min_w => 16,
1042 min_h => 16, 1085 min_h => 16,
1043 @_ 1086 %arg,
1044 ); 1087 );
1045 1088
1046 $self->{title} &&= new CFClient::UI::Label 1089 $self->{title} = new CFClient::UI::Label
1047 align => 0, 1090 align => 0,
1048 valign => 1, 1091 valign => 1,
1049 text => $self->{title}, 1092 text => $title,
1050 fontsize => $self->{border}; 1093 fontsize => $self->{border}
1094 if defined $title;
1051 1095
1052 $self 1096 $self
1097}
1098
1099sub add {
1100 my ($self, @widgets) = @_;
1101
1102 $self->SUPER::add (@widgets);
1103 $self->CFClient::UI::Container::add ($self->{title}) if $self->{title};
1053} 1104}
1054 1105
1055sub border { 1106sub border {
1056 int $_[0]{border} * $::FONTSIZE 1107 int $_[0]{border} * $::FONTSIZE
1057} 1108}
1058 1109
1059sub size_request { 1110sub size_request {
1060 my ($self) = @_; 1111 my ($self) = @_;
1112
1113 $self->{title}->size_request
1114 if $self->{title};
1061 1115
1062 my ($w, $h) = $self->SUPER::size_request; 1116 my ($w, $h) = $self->SUPER::size_request;
1063 1117
1064 ( 1118 (
1065 $w + $self->border * 2, 1119 $w + $self->border * 2,
1068} 1122}
1069 1123
1070sub size_allocate { 1124sub size_allocate {
1071 my ($self, $w, $h) = @_; 1125 my ($self, $w, $h) = @_;
1072 1126
1127 if ($self->{title}) {
1128 $self->{title}{w} = $w;
1129 $self->{title}{h} = $h;
1130 $self->{title}->size_allocate ($w, $h);
1131 }
1132
1133 my $border = $self->border;
1134
1073 $h -= List::Util::max 0, $self->border * 2; 1135 $h -= List::Util::max 0, $border * 2;
1074 $w -= List::Util::max 0, $self->border * 2; 1136 $w -= List::Util::max 0, $border * 2;
1075 1137
1076 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1077 if $self->{title};
1078
1079 $self->child->configure ($self->border, $self->border, $w, $h); 1138 $self->child->configure ($border, $border, $w, $h);
1080} 1139}
1081 1140
1082sub button_down { 1141sub button_down {
1083 my ($self, $ev, $x, $y) = @_; 1142 my ($self, $ev, $x, $y) = @_;
1084 1143
1103 my $dy = $ev->{y} - $oy; 1162 my $dy = $ev->{y} - $oy;
1104 1163
1105 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1164 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1106 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1165 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1107 1166
1167 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1108 $self->realloc; 1168 $self->realloc;
1109 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1110 }; 1169 };
1111 1170
1112 } elsif ($lr ^ $td) { 1171 } elsif ($lr ^ $td) {
1113 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1172 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1114 my ($bx, $by) = ($self->{x}, $self->{y}); 1173 my ($bx, $by) = ($self->{x}, $self->{y});
1117 my ($ev, $x, $y) = @_; 1176 my ($ev, $x, $y) = @_;
1118 1177
1119 ($x, $y) = ($ev->{x}, $ev->{y}); 1178 ($x, $y) = ($ev->{x}, $ev->{y});
1120 1179
1121 $self->move_abs ($bx + $x - $ox, $by + $y - $oy); 1180 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1181 # HACK: the next line is required to enforce placement
1182 $self->{parent}->size_allocate ($self->{parent}{w}, $self->{parent}{h});
1122 }; 1183 };
1184 } else {
1185 return 0;
1186 }
1187
1123 } 1188 1
1124} 1189}
1125 1190
1126sub button_up { 1191sub button_up {
1127 my ($self, $ev, $x, $y) = @_; 1192 my ($self, $ev, $x, $y) = @_;
1128 1193
1129 delete $self->{motion}; 1194 !!delete $self->{motion}
1130} 1195}
1131 1196
1132sub mouse_motion { 1197sub mouse_motion {
1133 my ($self, $ev, $x, $y) = @_; 1198 my ($self, $ev, $x, $y) = @_;
1134 1199
1135 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1200 $self->{motion}->($ev, $x, $y) if $self->{motion};
1201
1202 !!$self->{motion}
1136} 1203}
1137 1204
1138sub _draw { 1205sub _draw {
1139 my ($self) = @_; 1206 my ($self) = @_;
1140 1207
1208 my $child = $self->{children}[0];
1209
1141 my ($w, $h ) = ($self->{w}, $self->{h}); 1210 my ($w, $h ) = ($self->{w}, $self->{h});
1142 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1211 my ($cw, $ch) = ($child->{w}, $child->{h});
1143 1212
1144 glEnable GL_TEXTURE_2D; 1213 glEnable GL_TEXTURE_2D;
1145 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1214 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1146 1215
1147 my $border = $self->border; 1216 my $border = $self->border;
1162 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1231 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1163 } 1232 }
1164 1233
1165 glDisable GL_TEXTURE_2D; 1234 glDisable GL_TEXTURE_2D;
1166 1235
1167 $self->{title}->draw if $self->{title};
1168
1169 $self->child->draw; 1236 $child->draw;
1237
1238 if ($self->{title}) {
1239 glTranslate 0, $border - $self->{h};
1240 $self->{title}->_draw;
1241 }
1170} 1242}
1171 1243
1172############################################################################# 1244#############################################################################
1173 1245
1174package CFClient::UI::Table; 1246package CFClient::UI::Table;
1522} 1594}
1523 1595
1524sub size_allocate { 1596sub size_allocate {
1525 my ($self, $w, $h) = @_; 1597 my ($self, $w, $h) = @_;
1526 1598
1599 delete $self->{ox};
1600
1527 delete $self->{texture}; 1601 delete $self->{texture}
1602 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1528} 1603}
1529 1604
1530sub set_fontsize { 1605sub set_fontsize {
1531 my ($self, $fontsize) = @_; 1606 my ($self, $fontsize) = @_;
1532 1607
1547 $self->{layout}->set_width ($self->{w}); 1622 $self->{layout}->set_width ($self->{w});
1548 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1623 $self->{layout}->set_ellipsise ($self->{ellipsise});
1549 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1624 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1550 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1625 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 1626
1552 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1627 new_from_layout CFClient::Texture $self->{layout}
1628 };
1553 1629
1630 unless (exists $self->{ox}) {
1554 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1631 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1555 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1632 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1556 : ($self->{w} - $tex->{w}) * 0.5); 1633 : ($self->{w} - $tex->{w}) * 0.5);
1557 1634
1558 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1635 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1559 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1636 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1560 : ($self->{h} - $tex->{h}) * 0.5); 1637 : ($self->{h} - $tex->{h}) * 0.5);
1561
1562 $tex
1563 }; 1638 };
1564 1639
1565 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1566 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1567 1642
1605 1680
1606 delete $self->{cur_h}; 1681 delete $self->{cur_h};
1607 1682
1608 return if $self->{text} eq $text; 1683 return if $self->{text} eq $text;
1609 1684
1610 delete $self->{texture};
1611
1612 $self->{last_activity} = $::NOW; 1685 $self->{last_activity} = $::NOW;
1613 $self->{text} = $text; 1686 $self->{text} = $text;
1614 1687
1615 $text =~ s/./*/g if $self->{hidden}; 1688 $text =~ s/./*/g if $self->{hidden};
1616 $self->{layout}->set_text ("$text "); 1689 $self->{layout}->set_text ("$text ");
1617 1690
1618 $self->_emit (changed => $self->{text}); 1691 $self->_emit (changed => $self->{text});
1692 $self->update;
1619} 1693}
1620 1694
1621sub set_text { 1695sub set_text {
1622 my ($self, $text) = @_; 1696 my ($self, $text) = @_;
1623 1697
1662 $self->{cursor} = length $text; 1736 $self->{cursor} = length $text;
1663 } elsif ($uni == 27) { 1737 } elsif ($uni == 27) {
1664 $self->_emit ('escape'); 1738 $self->_emit ('escape');
1665 } elsif ($uni) { 1739 } elsif ($uni) {
1666 substr $text, $self->{cursor}++, 0, chr $uni; 1740 substr $text, $self->{cursor}++, 0, chr $uni;
1741 } else {
1742 return 0;
1667 } 1743 }
1668 1744
1669 $self->_set_text ($text); 1745 $self->_set_text ($text);
1670 1746
1671 $self->realloc; 1747 $self->realloc;
1748
1749 1
1672} 1750}
1673 1751
1674sub focus_in { 1752sub focus_in {
1675 my ($self) = @_; 1753 my ($self) = @_;
1676 1754
1691 utf8::encode $text; 1769 utf8::encode $text;
1692 $self->{cursor} = length substr $text, 0, $idx; 1770 $self->{cursor} = length substr $text, 0, $idx;
1693 1771
1694 $self->_set_text ($self->{text}); 1772 $self->_set_text ($self->{text});
1695 $self->update; 1773 $self->update;
1774
1775 1
1696} 1776}
1697 1777
1698sub mouse_motion { 1778sub mouse_motion {
1699 my ($self, $ev, $x, $y) = @_; 1779 my ($self, $ev, $x, $y) = @_;
1700# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1780# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1781
1782 0
1701} 1783}
1702 1784
1703sub _draw { 1785sub _draw {
1704 my ($self) = @_; 1786 my ($self) = @_;
1705 1787
1706 local $self->{fg} = $self->{fg}; 1788 local $self->{fg} = $self->{fg};
1707 1789
1708 if ($FOCUS == $self) { 1790 if ($FOCUS == $self) {
1709 glColor @{$self->{active_bg}}; 1791 glColor_premultiply @{$self->{active_bg}};
1710 $self->{fg} = $self->{active_fg}; 1792 $self->{fg} = $self->{active_fg};
1711 } else { 1793 } else {
1712 glColor @{$self->{bg}}; 1794 glColor_premultiply @{$self->{bg}};
1713 } 1795 }
1714 1796
1715 glEnable GL_BLEND; 1797 glEnable GL_BLEND;
1716 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1798 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1717 glBegin GL_QUADS; 1799 glBegin GL_QUADS;
1718 glVertex 0 , 0; 1800 glVertex 0 , 0;
1719 glVertex 0 , $self->{h}; 1801 glVertex 0 , $self->{h};
1720 glVertex $self->{w}, $self->{h}; 1802 glVertex $self->{w}, $self->{h};
1721 glVertex $self->{w}, 0; 1803 glVertex $self->{w}, 0;
1782 } else { 1864 } else {
1783 $self->set_text ($self->{history_saveback}); 1865 $self->set_text ($self->{history_saveback});
1784 } 1866 }
1785 1867
1786 } else { 1868 } else {
1787 $self->SUPER::key_down ($ev); 1869 return $self->SUPER::key_down ($ev)
1870 }
1871
1788 } 1872 1
1789
1790} 1873}
1791 1874
1792############################################################################# 1875#############################################################################
1793 1876
1794package CFClient::UI::Button; 1877package CFClient::UI::Button;
1823 my ($self, $ev, $x, $y) = @_; 1906 my ($self, $ev, $x, $y) = @_;
1824 1907
1825 $self->emit ("activate") 1908 $self->emit ("activate")
1826 if $x >= 0 && $x < $self->{w} 1909 if $x >= 0 && $x < $self->{w}
1827 && $y >= 0 && $y < $self->{h}; 1910 && $y >= 0 && $y < $self->{h};
1911
1912 1
1828} 1913}
1829 1914
1830sub _draw { 1915sub _draw {
1831 my ($self) = @_; 1916 my ($self) = @_;
1832 1917
1886 1971
1887 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 1972 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1888 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 1973 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1889 $self->{state} = !$self->{state}; 1974 $self->{state} = !$self->{state};
1890 $self->_emit (changed => $self->{state}); 1975 $self->_emit (changed => $self->{state});
1976 } else {
1977 return 0
1978 }
1979
1891 } 1980 1
1892} 1981}
1893 1982
1894sub _draw { 1983sub _draw {
1895 my ($self) = @_; 1984 my ($self) = @_;
1896 1985
2063 2152
2064 my $h1 = $self->{h} * (1 - $ycut1); 2153 my $h1 = $self->{h} * (1 - $ycut1);
2065 my $h2 = $self->{h} * (1 - $ycut2); 2154 my $h2 = $self->{h} * (1 - $ycut2);
2066 2155
2067 glEnable GL_BLEND; 2156 glEnable GL_BLEND;
2068 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2157 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2158 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2069 glEnable GL_TEXTURE_2D; 2159 glEnable GL_TEXTURE_2D;
2070 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2160 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2071 2161
2072 glBindTexture GL_TEXTURE_2D, $t1->{name}; 2162 glBindTexture GL_TEXTURE_2D, $t1->{name};
2073 glBegin GL_QUADS; 2163 glBegin GL_QUADS;
2234 2324
2235 $self->SUPER::button_down ($ev, $x, $y); 2325 $self->SUPER::button_down ($ev, $x, $y);
2236 2326
2237 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2327 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2238 2328
2239 $self->mouse_motion ($ev, $x, $y); 2329 $self->mouse_motion ($ev, $x, $y)
2240} 2330}
2241 2331
2242sub mouse_motion { 2332sub mouse_motion {
2243 my ($self, $ev, $x, $y) = @_; 2333 my ($self, $ev, $x, $y) = @_;
2244 2334
2248 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2338 my (undef, $lo, $hi, $page) = @{$self->{range}};
2249 2339
2250 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 2340 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2251 2341
2252 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 2342 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2343 } else {
2344 return 0;
2345 }
2346
2253 } 2347 1
2254} 2348}
2255 2349
2256sub update { 2350sub update {
2257 my ($self) = @_; 2351 my ($self) = @_;
2258 2352
2259 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2353 delete $self->{knob_w};
2354 $self->SUPER::update;
2355}
2356
2357sub _draw {
2358 my ($self) = @_;
2359
2360 unless ($self->{knob_w}) {
2260 $self->set_value ($self->{range}[0]); 2361 $self->set_value ($self->{range}[0]);
2261 2362
2262 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2363 my ($value, $lo, $hi, $page) = @{$self->{range}};
2263 my $range = ($hi - $page - $lo) || 1e-100; 2364 my $range = ($hi - $page - $lo) || 1e-100;
2264 2365
2270 $value = ($value - $lo) / $range; 2371 $value = ($value - $lo) / $range;
2271 $value = $value * $self->{scale} + $self->{offset}; 2372 $value = $value * $self->{scale} + $self->{offset};
2272 2373
2273 $self->{knob_x} = $value - $knob_w * 0.5; 2374 $self->{knob_x} = $value - $knob_w * 0.5;
2274 $self->{knob_w} = $knob_w; 2375 $self->{knob_w} = $knob_w;
2275 }); 2376 }
2276
2277 $self->SUPER::update;
2278}
2279
2280sub _draw {
2281 my ($self) = @_;
2282 2377
2283 $self->SUPER::_draw (); 2378 $self->SUPER::_draw ();
2284 2379
2285 glScale $self->{w}, $self->{h}; 2380 glScale $self->{w}, $self->{h};
2286 2381
2780 $self->SUPER::DESTROY; 2875 $self->SUPER::DESTROY;
2781} 2876}
2782 2877
2783############################################################################# 2878#############################################################################
2784 2879
2785package CFClient::UI::Inventory; 2880package CFClient::UI::Buttonbar;
2786 2881
2787our @ISA = CFClient::UI::ScrolledWindow::; 2882our @ISA = CFClient::UI::HBox::;
2788 2883
2789sub new { 2884# TODO: should actualyl wrap buttons and other goodies.
2790 my $class = shift;
2791
2792 my $self = $class->SUPER::new (
2793 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2794 @_,
2795 );
2796
2797 $self
2798}
2799
2800sub set_items {
2801 my ($self, $items) = @_;
2802
2803 $self->{scrolled}->clear;
2804 return unless $items;
2805
2806 my @items = sort {
2807 ($a->{type} <=> $b->{type})
2808 or ($a->{name} cmp $b->{name})
2809 } @$items;
2810
2811 $self->{real_items} = \@items;
2812
2813 my $row = 0;
2814 for my $item (@items) {
2815 CFClient::Item::update_widgets $item;
2816
2817 $self->{scrolled}->add (0, $row, $item->{face_widget});
2818 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2819 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2820
2821 $row++;
2822 }
2823}
2824 2885
2825############################################################################# 2886#############################################################################
2826 2887
2827package CFClient::UI::Menu; 2888package CFClient::UI::Menu;
2828 2889
2878 my ($self, $ev, $x, $y) = @_; 2939 my ($self, $ev, $x, $y) = @_;
2879 2940
2880 # TODO: should use vbox->find_widget or so 2941 # TODO: should use vbox->find_widget or so
2881 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2942 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2882 $self->{hover} = $self->{item}{$HOVER}; 2943 $self->{hover} = $self->{item}{$HOVER};
2944
2945 0
2883} 2946}
2884 2947
2885sub button_up { 2948sub button_up {
2886 my ($self, $ev, $x, $y) = @_; 2949 my ($self, $ev, $x, $y) = @_;
2887 2950
2889 undef $GRAB; 2952 undef $GRAB;
2890 $self->hide; 2953 $self->hide;
2891 2954
2892 $self->_emit ("popdown"); 2955 $self->_emit ("popdown");
2893 $self->{hover}[1]->() if $self->{hover}; 2956 $self->{hover}[1]->() if $self->{hover};
2957 } else {
2958 return 0
2959 }
2960
2961 1
2962}
2963
2964#############################################################################
2965
2966package CFClient::UI::Multiplexer;
2967
2968our @ISA = CFClient::UI::Container::;
2969
2970sub new {
2971 my $class = shift;
2972
2973 my $self = $class->SUPER::new (
2974 @_,
2975 );
2976
2977 $self->{current} = $self->{children}[0]
2978 if @{ $self->{children} };
2979
2980 $self
2981}
2982
2983sub add {
2984 my ($self, @widgets) = @_;
2985
2986 $self->SUPER::add (@widgets);
2987
2988 $self->{current} = $self->{children}[0]
2989 if @{ $self->{children} };
2990}
2991
2992sub set_current_page {
2993 my ($self, $page_or_widget) = @_;
2994
2995 my $widget = ref $page_or_widget
2996 ? $page_or_widget
2997 : $self->{children}[$page_or_widget];
2998
2999 $self->{current} = $widget;
3000 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3001
3002 $self->_emit (page_changed => $self->{current});
3003
3004 $self->realloc;
3005}
3006
3007sub visible_children {
3008 $_[0]{current}
3009}
3010
3011sub size_request {
3012 my ($self) = @_;
3013
3014 $self->{current}->size_request
3015}
3016
3017sub size_allocate {
3018 my ($self, $w, $h) = @_;
3019
3020 $self->{current}->configure (0, 0, $w, $h);
3021}
3022
3023sub _draw {
3024 my ($self) = @_;
3025
3026 $self->{current}->draw;
3027}
3028
3029#############################################################################
3030
3031package CFClient::UI::Notebook;
3032
3033our @ISA = CFClient::UI::VBox::;
3034
3035sub new {
3036 my $class = shift;
3037
3038 my $self = $class->SUPER::new (
3039 buttonbar => (new CFClient::UI::Buttonbar),
3040 multiplexer => (new CFClient::UI::Multiplexer expand => 1),
3041 # filter => # will be put between multiplexer and $self
3042 @_,
3043 );
2894 } 3044
3045 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3046 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3047
3048 $self
3049}
3050
3051sub add {
3052 my ($self, $title, $widget, $tooltip) = @_;
3053
3054 Scalar::Util::weaken $self;
3055
3056 $self->{buttonbar}->add (new CFClient::UI::Button
3057 markup => $title,
3058 tooltip => $tooltip,
3059 on_activate => sub { $self->set_current_page ($widget) },
3060 );
3061
3062 $self->{multiplexer}->add ($widget);
3063}
3064
3065sub set_current_page {
3066 my ($self, $page) = @_;
3067
3068 $self->{multiplexer}->set_current_page ($page);
3069 $self->_emit (page_changed => $self->{multiplexer}{current});
2895} 3070}
2896 3071
2897############################################################################# 3072#############################################################################
2898 3073
2899package CFClient::UI::Statusbox; 3074package CFClient::UI::Statusbox;
3004 $self->SUPER::reconfigure; 3179 $self->SUPER::reconfigure;
3005} 3180}
3006 3181
3007############################################################################# 3182#############################################################################
3008 3183
3184package CFClient::UI::Inventory;
3185
3186our @ISA = CFClient::UI::ScrolledWindow::;
3187
3188sub new {
3189 my $class = shift;
3190
3191 my $self = $class->SUPER::new (
3192 child => (new CFClient::UI::Table col_expand => [0, 1, 0]),
3193 @_,
3194 );
3195
3196 $self
3197}
3198
3199sub set_items {
3200 my ($self, $items) = @_;
3201
3202 $self->{child}->clear;
3203 return unless $items;
3204
3205 my @items = sort {
3206 ($a->{type} <=> $b->{type})
3207 or ($a->{name} cmp $b->{name})
3208 } @$items;
3209
3210 $self->{real_items} = \@items;
3211
3212 my $row = 0;
3213 for my $item (@items) {
3214 CFClient::Item::update_widgets $item;
3215
3216 $self->{child}->add (0, $row, $item->{face_widget});
3217 $self->{child}->add (1, $row, $item->{desc_widget});
3218 $self->{child}->add (2, $row, $item->{weight_widget});
3219
3220 $row++;
3221 }
3222}
3223
3224#############################################################################
3225
3226package CFClient::UI::BindEditor;
3227
3228our @ISA = CFClient::UI::FancyFrame::;
3229
3230sub new {
3231 my $class = shift;
3232
3233 my $self = $class->SUPER::new (binding => [], commands => [], @_);
3234
3235 $self->add (my $vb = new CFClient::UI::VBox);
3236
3237
3238 $vb->add ($self->{rec_btn} = new CFClient::UI::Button
3239 text => "start recording",
3240 tooltip => "Start/Stops recording of actions."
3241 ."All subsequent actions after the recording started will be captured."
3242 ."The actions are displayed after the record was stopped."
3243 ."To bind the action you have to click on the 'Bind' button",
3244 on_activate => sub {
3245 unless ($self->{recording}) {
3246 $self->start;
3247 } else {
3248 $self->stop;
3249 }
3250 });
3251
3252 $vb->add (new CFClient::UI::Label text => "Actions:");
3253 $vb->add ($self->{cmdbox} = new CFClient::UI::VBox);
3254
3255 $vb->add (new CFClient::UI::Label text => "Bound to: ");
3256 $vb->add (my $hb = new CFClient::UI::HBox);
3257 $hb->add ($self->{keylbl} = new CFClient::UI::Label expand => 1);
3258 $hb->add (new CFClient::UI::Button
3259 text => "bind",
3260 tooltip => "This opens a query where you have to press the key combination to bind the recorded actions",
3261 on_activate => sub {
3262 $self->ask_for_bind;
3263 });
3264
3265 $vb->add (my $hb = new CFClient::UI::HBox);
3266 $hb->add (new CFClient::UI::Button
3267 text => "ok",
3268 expand => 1,
3269 tooltip => "This closes the binding editor and saves the binding",
3270 on_activate => sub {
3271 $self->hide;
3272 $self->commit;
3273 });
3274
3275 $hb->add (new CFClient::UI::Button
3276 text => "cancel",
3277 expand => 1,
3278 tooltip => "This closes the binding editor without saving",
3279 on_activate => sub {
3280 $self->hide;
3281 $self->{binding_cancel}->()
3282 if $self->{binding_cancel};
3283 });
3284
3285 $self->update_binding_widgets;
3286
3287 $self
3288}
3289
3290sub commit {
3291 my ($self) = @_;
3292 my ($mod, $sym, $cmds) = $self->get_binding;
3293 if ($sym != 0 && @$cmds > 0) {
3294 $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym)
3295 ."'. Don't forget 'Save Config'!");
3296 $self->{binding_change}->($mod, $sym, $cmds)
3297 if $self->{binding_change};
3298 } else {
3299 $::STATUSBOX->add ("No action bound, no key or action specified!");
3300 $self->{binding_cancel}->()
3301 if $self->{binding_cancel};
3302 }
3303}
3304
3305sub start {
3306 my ($self) = @_;
3307
3308 $self->{rec_btn}->set_text ("stop recording");
3309 $self->{recording} = 1;
3310 $self->clear_command_list;
3311 $::CONN->start_record if $::CONN;
3312}
3313
3314sub stop {
3315 my ($self) = @_;
3316
3317 $self->{rec_btn}->set_text ("start recording");
3318 $self->{recording} = 0;
3319
3320 my $rec;
3321 $rec = $::CONN->stop_record if $::CONN;
3322 return unless ref $rec eq 'ARRAY';
3323 $self->set_command_list ($rec);
3324}
3325
3326
3327sub ask_for_bind_and_commit {
3328 my ($self) = @_;
3329 $self->ask_for_bind (1);
3330}
3331
3332sub ask_for_bind {
3333 my ($self, $commit) = @_;
3334
3335 CFClient::Binder::open_binding_dialog (sub {
3336 my ($mod, $sym) = @_;
3337 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak?
3338 $self->update_binding_widgets;
3339 $self->commit if $commit;
3340 });
3341}
3342
3343# $mod and $sym are the modifiers and key symbol
3344# $cmds is a array ref of strings (the commands)
3345# $cb is the callback that is executed on OK
3346# $ccb is the callback that is executed on CANCEL and
3347# when the binding was unsuccessful on OK
3348sub set_binding {
3349 my ($self, $mod, $sym, $cmds, $cb, $ccb) = @_;
3350
3351 $self->clear_command_list;
3352 $self->{recording} = 0;
3353 $self->{rec_btn}->set_text ("start recording");
3354
3355 $self->{binding} = [$mod, $sym];
3356 $self->{commands} = $cmds;
3357
3358 $self->{binding_change} = $cb;
3359 $self->{binding_cancel} = $ccb;
3360
3361 $self->update_binding_widgets;
3362}
3363
3364# this is a shortcut method that asks for a binding
3365# and then just binds it.
3366sub do_quick_binding {
3367 my ($self, $cmds) = @_;
3368 $self->set_binding (undef, undef, $cmds, sub {
3369 $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2];
3370 });
3371 $self->ask_for_bind (1);
3372}
3373
3374sub update_binding_widgets {
3375 my ($self) = @_;
3376 my ($mod, $sym, $cmds) = $self->get_binding;
3377 $self->{keylbl}->set_text (CFClient::Binder::keycombo_to_name ($mod, $sym));
3378 $self->set_command_list ($cmds);
3379}
3380
3381sub get_binding {
3382 my ($self) = @_;
3383 return (
3384 $self->{binding}->[0],
3385 $self->{binding}->[1],
3386 [ grep { defined $_ } @{$self->{commands}} ]
3387 );
3388}
3389
3390sub clear_command_list {
3391 my ($self) = @_;
3392 $self->{cmdbox}->clear ();
3393}
3394
3395sub set_command_list {
3396 my ($self, $cmds) = @_;
3397
3398 $self->{cmdbox}->clear ();
3399 $self->{commands} = $cmds;
3400
3401 my $idx = 0;
3402
3403 for (@$cmds) {
3404 $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox);
3405
3406 my $i = $idx;
3407 $hb->add (new CFClient::UI::Label text => $_);
3408 $hb->add (new CFClient::UI::Button
3409 text => "delete",
3410 tooltip => "Deletes the action from the record",
3411 on_activate => sub {
3412 $self->{cmdbox}->remove ($hb);
3413 $cmds->[$i] = undef;
3414 });
3415
3416
3417 $idx++
3418 }
3419}
3420
3421#############################################################################
3422
3423package CFClient::UI::SpellList;
3424
3425our @ISA = CFClient::UI::Table::;
3426
3427sub new {
3428 my $class = shift;
3429
3430 my $self = $class->SUPER::new (
3431 binding => [],
3432 commands => [],
3433 @_,
3434 )
3435}
3436
3437# XXX: Do sorting? Argl...
3438sub add_spell {
3439 my ($self, $spell) = @_;
3440 $self->{spells}->{$spell->{name}} = $spell;
3441
3442 $self->add (0, $self->{tbl_idx}, new CFClient::UI::Face
3443 face => $spell->{face},
3444 can_hover => 1,
3445 can_events => 1,
3446 tooltip => $spell->{message});
3447
3448 $self->add (1, $self->{tbl_idx}, new CFClient::UI::Label
3449 text => $spell->{name},
3450 can_hover => 1,
3451 can_events => 1,
3452 tooltip => $spell->{message},
3453 expand => 1);
3454
3455 $self->add (2, $self->{tbl_idx}, new CFClient::UI::Label
3456 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3457 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3458 expand => 1);
3459
3460 $self->add (3, $self->{tbl_idx}++, new CFClient::UI::Button
3461 text => "bind to key",
3462 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) });
3463}
3464
3465sub rebuild_spell_list {
3466 my ($self) = @_;
3467 $self->{tbl_idx} = 0;
3468 $self->add_spell ($_) for values %{$self->{spells}};
3469}
3470
3471sub remove_spell {
3472 my ($self, $spell) = @_;
3473 delete $self->{spells}->{$spell->{name}};
3474 $self->rebuild_spell_list;
3475}
3476
3477#############################################################################
3478
3009package CFClient::UI::Root; 3479package CFClient::UI::Root;
3010 3480
3011our @ISA = CFClient::UI::Container::; 3481our @ISA = CFClient::UI::Container::;
3012 3482
3013use CFClient::OpenGL; 3483use CFClient::OpenGL;
3122 $_->() 3592 $_->()
3123 for values %{delete $self->{refresh_hook}}; 3593 for values %{delete $self->{refresh_hook}};
3124 } 3594 }
3125 3595
3126 if ($self->{realloc}) { 3596 if ($self->{realloc}) {
3597 my %queue;
3127 my @queue; 3598 my @queue;
3599 my $widget;
3128 3600
3601 outer:
3129 while () { 3602 while () {
3130 if ($self->{realloc}) { 3603 if (my $realloc = delete $self->{realloc}) {
3131 #TODO use array-of-depth approach 3604 for $widget (values %$realloc) {
3605 $widget->{visible} or next; # do not resize invisible widgets
3132 3606
3133 use sort 'stable'; 3607 $queue{$widget+0}++ and next; # duplicates are common
3134 3608
3135 @queue = sort { $a->{visible} <=> $b->{visible} } 3609 push @{ $queue[$widget->{visible}] }, $widget;
3136 @queue, values %{delete $self->{realloc}}; 3610 }
3137 } 3611 }
3138 3612
3613 while () {
3614 @queue or last outer;
3615
3139 my $widget = pop @queue || last; 3616 $widget = pop @{ $queue[-1] || [] }
3617 and last;
3618
3619 pop @queue;
3620 }
3140 3621
3141 $widget->{visible} or last; # do not resize invisible widgets 3622 delete $queue{$widget+0};
3142 3623
3143 my ($w, $h) = $widget->size_request; 3624 my ($w, $h) = $widget->size_request;
3144 3625
3145 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2; 3626 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3146 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2; 3627 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3154 $widget->{req_h} = $h; 3635 $widget->{req_h} = $h;
3155 3636
3156 $self->{size_alloc}{$widget+0} = $widget; 3637 $self->{size_alloc}{$widget+0} = $widget;
3157 3638
3158 if (my $parent = $widget->{parent}) { 3639 if (my $parent = $widget->{parent}) {
3159 $self->{realloc}{$parent+0} = $parent; 3640 $self->{realloc}{$parent+0} = $parent
3160 #unshift @queue, $parent; 3641 unless $queue{$parent+0};
3642
3161 $parent->{force_size_alloc} = 1; 3643 $parent->{force_size_alloc} = 1;
3162 $self->{size_alloc}{$parent+0} = $parent; 3644 $self->{size_alloc}{$parent+0} = $parent;
3163 } 3645 }
3164 } 3646 }
3165 3647
3181 3663
3182 $w = int $w + 0.5; 3664 $w = int $w + 0.5;
3183 $h = int $h + 0.5; 3665 $h = int $h + 0.5;
3184 3666
3185 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 3667 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3668 $widget->{old_w} = $widget->{w};
3669 $widget->{old_h} = $widget->{h};
3670
3186 $widget->{w} = $w; 3671 $widget->{w} = $w;
3187 $widget->{h} = $h; 3672 $widget->{h} = $h;
3188 3673
3189 $widget->emit (size_allocate => $w, $h); 3674 $widget->emit (size_allocate => $w, $h);
3190 } 3675 }
3205 glLoadIdentity; 3690 glLoadIdentity;
3206 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 3691 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3207 glMatrixMode GL_MODELVIEW; 3692 glMatrixMode GL_MODELVIEW;
3208 glLoadIdentity; 3693 glLoadIdentity;
3209 3694
3695 {
3696 package CFClient::UI::Base;
3697
3698 ($draw_x, $draw_y, $draw_w, $draw_h) =
3699 (0, 0, $self->{w}, $self->{h});
3700 }
3701
3210 $self->_draw; 3702 $self->_draw;
3211} 3703}
3212 3704
3213############################################################################# 3705#############################################################################
3214 3706

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines