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.264 by root, Thu Jun 1 02:58:30 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}
1528 ;#d# 1602 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1529} 1603}
1530 1604
1531sub set_fontsize { 1605sub set_fontsize {
1532 my ($self, $fontsize) = @_; 1606 my ($self, $fontsize) = @_;
1533 1607
1548 $self->{layout}->set_width ($self->{w}); 1622 $self->{layout}->set_width ($self->{w});
1549 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1623 $self->{layout}->set_ellipsise ($self->{ellipsise});
1550 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1624 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1551 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1625 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1552 1626
1553 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1627 new_from_layout CFClient::Texture $self->{layout}
1628 };
1554 1629
1630 unless (exists $self->{ox}) {
1555 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1631 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1556 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1632 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1557 : ($self->{w} - $tex->{w}) * 0.5); 1633 : ($self->{w} - $tex->{w}) * 0.5);
1558 1634
1559 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1635 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1560 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1636 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1561 : ($self->{h} - $tex->{h}) * 0.5); 1637 : ($self->{h} - $tex->{h}) * 0.5);
1562
1563 $tex
1564 }; 1638 };
1565 1639
1566 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1567 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1568 1642
1606 1680
1607 delete $self->{cur_h}; 1681 delete $self->{cur_h};
1608 1682
1609 return if $self->{text} eq $text; 1683 return if $self->{text} eq $text;
1610 1684
1611 delete $self->{texture};
1612
1613 $self->{last_activity} = $::NOW; 1685 $self->{last_activity} = $::NOW;
1614 $self->{text} = $text; 1686 $self->{text} = $text;
1615 1687
1616 $text =~ s/./*/g if $self->{hidden}; 1688 $text =~ s/./*/g if $self->{hidden};
1617 $self->{layout}->set_text ("$text "); 1689 $self->{layout}->set_text ("$text ");
1618 1690
1619 $self->_emit (changed => $self->{text}); 1691 $self->_emit (changed => $self->{text});
1692 $self->update;
1620} 1693}
1621 1694
1622sub set_text { 1695sub set_text {
1623 my ($self, $text) = @_; 1696 my ($self, $text) = @_;
1624 1697
1663 $self->{cursor} = length $text; 1736 $self->{cursor} = length $text;
1664 } elsif ($uni == 27) { 1737 } elsif ($uni == 27) {
1665 $self->_emit ('escape'); 1738 $self->_emit ('escape');
1666 } elsif ($uni) { 1739 } elsif ($uni) {
1667 substr $text, $self->{cursor}++, 0, chr $uni; 1740 substr $text, $self->{cursor}++, 0, chr $uni;
1741 } else {
1742 return 0;
1668 } 1743 }
1669 1744
1670 $self->_set_text ($text); 1745 $self->_set_text ($text);
1671 1746
1672 $self->realloc; 1747 $self->realloc;
1748
1749 1
1673} 1750}
1674 1751
1675sub focus_in { 1752sub focus_in {
1676 my ($self) = @_; 1753 my ($self) = @_;
1677 1754
1692 utf8::encode $text; 1769 utf8::encode $text;
1693 $self->{cursor} = length substr $text, 0, $idx; 1770 $self->{cursor} = length substr $text, 0, $idx;
1694 1771
1695 $self->_set_text ($self->{text}); 1772 $self->_set_text ($self->{text});
1696 $self->update; 1773 $self->update;
1774
1775 1
1697} 1776}
1698 1777
1699sub mouse_motion { 1778sub mouse_motion {
1700 my ($self, $ev, $x, $y) = @_; 1779 my ($self, $ev, $x, $y) = @_;
1701# 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
1702} 1783}
1703 1784
1704sub _draw { 1785sub _draw {
1705 my ($self) = @_; 1786 my ($self) = @_;
1706 1787
1707 local $self->{fg} = $self->{fg}; 1788 local $self->{fg} = $self->{fg};
1708 1789
1709 if ($FOCUS == $self) { 1790 if ($FOCUS == $self) {
1710 glColor @{$self->{active_bg}}; 1791 glColor_premultiply @{$self->{active_bg}};
1711 $self->{fg} = $self->{active_fg}; 1792 $self->{fg} = $self->{active_fg};
1712 } else { 1793 } else {
1713 glColor @{$self->{bg}}; 1794 glColor_premultiply @{$self->{bg}};
1714 } 1795 }
1715 1796
1716 glEnable GL_BLEND; 1797 glEnable GL_BLEND;
1717 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1798 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1718 glBegin GL_QUADS; 1799 glBegin GL_QUADS;
1719 glVertex 0 , 0; 1800 glVertex 0 , 0;
1720 glVertex 0 , $self->{h}; 1801 glVertex 0 , $self->{h};
1721 glVertex $self->{w}, $self->{h}; 1802 glVertex $self->{w}, $self->{h};
1722 glVertex $self->{w}, 0; 1803 glVertex $self->{w}, 0;
1783 } else { 1864 } else {
1784 $self->set_text ($self->{history_saveback}); 1865 $self->set_text ($self->{history_saveback});
1785 } 1866 }
1786 1867
1787 } else { 1868 } else {
1788 $self->SUPER::key_down ($ev); 1869 return $self->SUPER::key_down ($ev)
1870 }
1871
1789 } 1872 1
1790
1791} 1873}
1792 1874
1793############################################################################# 1875#############################################################################
1794 1876
1795package CFClient::UI::Button; 1877package CFClient::UI::Button;
1824 my ($self, $ev, $x, $y) = @_; 1906 my ($self, $ev, $x, $y) = @_;
1825 1907
1826 $self->emit ("activate") 1908 $self->emit ("activate")
1827 if $x >= 0 && $x < $self->{w} 1909 if $x >= 0 && $x < $self->{w}
1828 && $y >= 0 && $y < $self->{h}; 1910 && $y >= 0 && $y < $self->{h};
1911
1912 1
1829} 1913}
1830 1914
1831sub _draw { 1915sub _draw {
1832 my ($self) = @_; 1916 my ($self) = @_;
1833 1917
1887 1971
1888 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 1972 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1889 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 1973 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1890 $self->{state} = !$self->{state}; 1974 $self->{state} = !$self->{state};
1891 $self->_emit (changed => $self->{state}); 1975 $self->_emit (changed => $self->{state});
1976 } else {
1977 return 0
1978 }
1979
1892 } 1980 1
1893} 1981}
1894 1982
1895sub _draw { 1983sub _draw {
1896 my ($self) = @_; 1984 my ($self) = @_;
1897 1985
2064 2152
2065 my $h1 = $self->{h} * (1 - $ycut1); 2153 my $h1 = $self->{h} * (1 - $ycut1);
2066 my $h2 = $self->{h} * (1 - $ycut2); 2154 my $h2 = $self->{h} * (1 - $ycut2);
2067 2155
2068 glEnable GL_BLEND; 2156 glEnable GL_BLEND;
2069 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;
2070 glEnable GL_TEXTURE_2D; 2159 glEnable GL_TEXTURE_2D;
2071 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2160 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2072 2161
2073 glBindTexture GL_TEXTURE_2D, $t1->{name}; 2162 glBindTexture GL_TEXTURE_2D, $t1->{name};
2074 glBegin GL_QUADS; 2163 glBegin GL_QUADS;
2235 2324
2236 $self->SUPER::button_down ($ev, $x, $y); 2325 $self->SUPER::button_down ($ev, $x, $y);
2237 2326
2238 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2327 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2239 2328
2240 $self->mouse_motion ($ev, $x, $y); 2329 $self->mouse_motion ($ev, $x, $y)
2241} 2330}
2242 2331
2243sub mouse_motion { 2332sub mouse_motion {
2244 my ($self, $ev, $x, $y) = @_; 2333 my ($self, $ev, $x, $y) = @_;
2245 2334
2249 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2338 my (undef, $lo, $hi, $page) = @{$self->{range}};
2250 2339
2251 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 2340 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2252 2341
2253 $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
2254 } 2347 1
2255} 2348}
2256 2349
2257sub update { 2350sub update {
2258 my ($self) = @_; 2351 my ($self) = @_;
2259 2352
2260 $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}) {
2261 $self->set_value ($self->{range}[0]); 2361 $self->set_value ($self->{range}[0]);
2262 2362
2263 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2363 my ($value, $lo, $hi, $page) = @{$self->{range}};
2264 my $range = ($hi - $page - $lo) || 1e-100; 2364 my $range = ($hi - $page - $lo) || 1e-100;
2265 2365
2271 $value = ($value - $lo) / $range; 2371 $value = ($value - $lo) / $range;
2272 $value = $value * $self->{scale} + $self->{offset}; 2372 $value = $value * $self->{scale} + $self->{offset};
2273 2373
2274 $self->{knob_x} = $value - $knob_w * 0.5; 2374 $self->{knob_x} = $value - $knob_w * 0.5;
2275 $self->{knob_w} = $knob_w; 2375 $self->{knob_w} = $knob_w;
2276 }); 2376 }
2277
2278 $self->SUPER::update;
2279}
2280
2281sub _draw {
2282 my ($self) = @_;
2283 2377
2284 $self->SUPER::_draw (); 2378 $self->SUPER::_draw ();
2285 2379
2286 glScale $self->{w}, $self->{h}; 2380 glScale $self->{w}, $self->{h};
2287 2381
2781 $self->SUPER::DESTROY; 2875 $self->SUPER::DESTROY;
2782} 2876}
2783 2877
2784############################################################################# 2878#############################################################################
2785 2879
2786package CFClient::UI::Inventory; 2880package CFClient::UI::Buttonbar;
2787 2881
2788our @ISA = CFClient::UI::ScrolledWindow::; 2882our @ISA = CFClient::UI::HBox::;
2789 2883
2790sub new { 2884# TODO: should actualyl wrap buttons and other goodies.
2791 my $class = shift;
2792
2793 my $self = $class->SUPER::new (
2794 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2795 @_,
2796 );
2797
2798 $self
2799}
2800
2801sub set_items {
2802 my ($self, $items) = @_;
2803
2804 $self->{scrolled}->clear;
2805 return unless $items;
2806
2807 my @items = sort {
2808 ($a->{type} <=> $b->{type})
2809 or ($a->{name} cmp $b->{name})
2810 } @$items;
2811
2812 $self->{real_items} = \@items;
2813
2814 my $row = 0;
2815 for my $item (@items) {
2816 CFClient::Item::update_widgets $item;
2817
2818 $self->{scrolled}->add (0, $row, $item->{face_widget});
2819 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2820 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2821
2822 $row++;
2823 }
2824}
2825 2885
2826############################################################################# 2886#############################################################################
2827 2887
2828package CFClient::UI::Menu; 2888package CFClient::UI::Menu;
2829 2889
2879 my ($self, $ev, $x, $y) = @_; 2939 my ($self, $ev, $x, $y) = @_;
2880 2940
2881 # TODO: should use vbox->find_widget or so 2941 # TODO: should use vbox->find_widget or so
2882 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2942 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2883 $self->{hover} = $self->{item}{$HOVER}; 2943 $self->{hover} = $self->{item}{$HOVER};
2944
2945 0
2884} 2946}
2885 2947
2886sub button_up { 2948sub button_up {
2887 my ($self, $ev, $x, $y) = @_; 2949 my ($self, $ev, $x, $y) = @_;
2888 2950
2890 undef $GRAB; 2952 undef $GRAB;
2891 $self->hide; 2953 $self->hide;
2892 2954
2893 $self->_emit ("popdown"); 2955 $self->_emit ("popdown");
2894 $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 );
2895 } 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});
2896} 3070}
2897 3071
2898############################################################################# 3072#############################################################################
2899 3073
2900package CFClient::UI::Statusbox; 3074package CFClient::UI::Statusbox;
3005 $self->SUPER::reconfigure; 3179 $self->SUPER::reconfigure;
3006} 3180}
3007 3181
3008############################################################################# 3182#############################################################################
3009 3183
3010package CFClient::UI::Root; 3184package CFClient::UI::Inventory;
3011 3185
3012our @ISA = CFClient::UI::Container::; 3186our @ISA = CFClient::UI::ScrolledWindow::;
3013
3014use CFClient::OpenGL;
3015 3187
3016sub new { 3188sub new {
3017 my $class = shift; 3189 my $class = shift;
3018 3190
3019 my $self = $class->SUPER::new ( 3191 my $self = $class->SUPER::new (
3020 visible => 1, 3192 child => (new CFClient::UI::Table col_expand => [0, 1, 0]),
3021 @_, 3193 @_,
3022 ); 3194 );
3023 3195
3024 Scalar::Util::weaken ($self->{root} = $self);
3025
3026 $self 3196 $self
3027} 3197}
3028 3198
3029sub size_request { 3199sub set_items {
3030 my ($self) = @_;
3031
3032 ($self->{w}, $self->{h})
3033}
3034
3035sub _to_pixel {
3036 my ($coord, $size, $max) = @_;
3037
3038 $coord =
3039 $coord eq "center" ? ($max - $size) * 0.5
3040 : $coord eq "max" ? $max
3041 : $coord;
3042
3043 $coord = 0 if $coord < 0;
3044 $coord = $max - $size if $coord > $max - $size;
3045
3046 int $coord + 0.5
3047}
3048
3049sub size_allocate {
3050 my ($self, $w, $h) = @_;
3051
3052 for my $child ($self->children) {
3053 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3054
3055 $X = $child->{force_x} if exists $child->{force_x};
3056 $Y = $child->{force_y} if exists $child->{force_y};
3057
3058 $X = _to_pixel $X, $W, $self->{w};
3059 $Y = _to_pixel $Y, $H, $self->{h};
3060
3061 $child->configure ($X, $Y, $W, $H);
3062 }
3063}
3064
3065sub coord2local {
3066 my ($self, $x, $y) = @_;
3067
3068 ($x, $y)
3069}
3070
3071sub coord2global {
3072 my ($self, $x, $y) = @_;
3073
3074 ($x, $y)
3075}
3076
3077sub update {
3078 my ($self) = @_;
3079
3080 $::WANT_REFRESH++;
3081}
3082
3083sub add {
3084 my ($self, @children) = @_;
3085
3086 $_->{is_toplevel} = 1
3087 for @children;
3088
3089 $self->SUPER::add (@children);
3090}
3091
3092sub remove {
3093 my ($self, @children) = @_;
3094
3095 $self->SUPER::remove (@children);
3096
3097 delete $self->{is_toplevel}
3098 for @children;
3099
3100 while (@children) {
3101 my $w = pop @children;
3102 push @children, $w->children;
3103 $w->set_invisible;
3104 }
3105}
3106
3107sub on_refresh {
3108 my ($self, $id, $cb) = @_;
3109
3110 $self->{refresh_hook}{$id} = $cb;
3111}
3112
3113sub on_post_alloc {
3114 my ($self, $id, $cb) = @_;
3115
3116 $self->{post_alloc_hook}{$id} = $cb;
3117}
3118
3119sub draw {
3120 my ($self) = @_;
3121
3122 while ($self->{refresh_hook}) {
3123 $_->()
3124 for values %{delete $self->{refresh_hook}};
3125 }
3126
3127 if ($self->{realloc}) {
3128 my @queue;
3129
3130 while () {
3131 if ($self->{realloc}) {
3132 #TODO use array-of-depth approach
3133
3134 use sort 'stable';
3135
3136 @queue = sort { $a->{visible} <=> $b->{visible} }
3137 @queue, values %{delete $self->{realloc}};
3138 }
3139
3140 my $widget = pop @queue || last;
3141
3142 $widget->{visible} or last; # do not resize invisible widgets
3143
3144 my ($w, $h) = $widget->size_request;
3145
3146 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3147 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3148
3149 $w = $widget->{force_w} if exists $widget->{force_w};
3150 $h = $widget->{force_h} if exists $widget->{force_h};
3151
3152 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3153 || delete $widget->{force_realloc}) {
3154 $widget->{req_w} = $w;
3155 $widget->{req_h} = $h;
3156
3157 $self->{size_alloc}{$widget+0} = $widget;
3158
3159 if (my $parent = $widget->{parent}) {
3160 $self->{realloc}{$parent+0} = $parent;
3161 #unshift @queue, $parent;
3162 $parent->{force_size_alloc} = 1;
3163 $self->{size_alloc}{$parent+0} = $parent;
3164 }
3165 }
3166
3167 delete $self->{realloc}{$widget+0};
3168 }
3169 }
3170
3171 while (my $size_alloc = delete $self->{size_alloc}) {
3172 my @queue = sort { $b->{visible} <=> $a->{visible} }
3173 values %$size_alloc;
3174
3175 while () {
3176 my $widget = pop @queue || last;
3177
3178 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3179
3180 $w = 0 if $w < 0;
3181 $h = 0 if $h < 0;
3182
3183 $w = int $w + 0.5;
3184 $h = int $h + 0.5;
3185
3186 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3187 $widget->{w} = $w;
3188 $widget->{h} = $h;
3189
3190 $widget->emit (size_allocate => $w, $h);
3191 }
3192 }
3193 }
3194
3195 while ($self->{post_alloc_hook}) {
3196 $_->()
3197 for values %{delete $self->{post_alloc_hook}};
3198 }
3199
3200
3201 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3202 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3203 glClear GL_COLOR_BUFFER_BIT;
3204
3205 glMatrixMode GL_PROJECTION;
3206 glLoadIdentity;
3207 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3208 glMatrixMode GL_MODELVIEW;
3209 glLoadIdentity;
3210
3211 $self->_draw;
3212}
3213
3214#############################################################################
3215
3216package CFClient::UI::SpellList;
3217
3218our @ISA = CFClient::UI::FancyFrame::;
3219
3220sub new {
3221 my $class = shift;
3222
3223 my $self = $class->SUPER::new (binding => [], commands => [], @_);
3224
3225 $self->add (new CFClient::UI::ScrolledWindow
3226 scrolled => $self->{spellbox} = new CFClient::UI::Table);
3227
3228 $self;
3229}
3230
3231# XXX: Do sorting? Argl...
3232sub add_spell {
3233 my ($self, $spell) = @_; 3200 my ($self, $items) = @_;
3234 $self->{spells}->{$spell->{name}} = $spell;
3235 3201
3236 $self->{spellbox}->add (0, $self->{tbl_idx}, new CFClient::UI::Face 3202 $self->{child}->clear;
3237 face => $spell->{face}, 3203 return unless $items;
3238 can_hover => 1,
3239 can_events => 1,
3240 tooltip => $spell->{message});
3241 3204
3242 $self->{spellbox}->add (1, $self->{tbl_idx}, new CFClient::UI::Label 3205 my @items = sort {
3243 text => $spell->{name}, 3206 ($a->{type} <=> $b->{type})
3244 can_hover => 1, 3207 or ($a->{name} cmp $b->{name})
3245 can_events => 1, 3208 } @$items;
3246 tooltip => $spell->{message},
3247 expand => 1);
3248 3209
3249 $self->{spellbox}->add (2, $self->{tbl_idx}, new CFClient::UI::Label 3210 $self->{real_items} = \@items;
3250 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3251 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3252 expand => 1);
3253 3211
3254 $self->{spellbox}->add (3, $self->{tbl_idx}++, new CFClient::UI::Button 3212 my $row = 0;
3255 text => "bind to key", 3213 for my $item (@items) {
3256 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }); 3214 CFClient::Item::update_widgets $item;
3257}
3258 3215
3259sub rebuild_spell_list { 3216 $self->{child}->add (0, $row, $item->{face_widget});
3260 my ($self) = @_; 3217 $self->{child}->add (1, $row, $item->{desc_widget});
3261 $self->{tbl_idx} = 0; 3218 $self->{child}->add (2, $row, $item->{weight_widget});
3262 $self->add_spell ($_) for values %{$self->{spells}};
3263}
3264 3219
3265sub remove_spell { 3220 $row++;
3266 my ($self, $spell) = @_; 3221 }
3267 delete $self->{spells}->{$spell->{name}};
3268 $self->rebuild_spell_list;
3269} 3222}
3270 3223
3271############################################################################# 3224#############################################################################
3272 3225
3273package CFClient::UI::BindEditor; 3226package CFClient::UI::BindEditor;
3368 $rec = $::CONN->stop_record if $::CONN; 3321 $rec = $::CONN->stop_record if $::CONN;
3369 return unless ref $rec eq 'ARRAY'; 3322 return unless ref $rec eq 'ARRAY';
3370 $self->set_command_list ($rec); 3323 $self->set_command_list ($rec);
3371} 3324}
3372 3325
3373# if $commit is true, the binding will be set after the user entered a key combo 3326
3327sub ask_for_bind_and_commit {
3328 my ($self) = @_;
3329 $self->ask_for_bind (1);
3330}
3331
3374sub ask_for_bind { 3332sub ask_for_bind {
3375 my ($self, $commit) = @_; 3333 my ($self, $commit) = @_;
3376 3334
3377 CFClient::Binder::open_binding_dialog (sub { 3335 CFClient::Binder::open_binding_dialog (sub {
3378 my ($mod, $sym) = @_; 3336 my ($mod, $sym) = @_;
3460 } 3418 }
3461} 3419}
3462 3420
3463############################################################################# 3421#############################################################################
3464 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
3479package CFClient::UI::Root;
3480
3481our @ISA = CFClient::UI::Container::;
3482
3483use CFClient::OpenGL;
3484
3485sub new {
3486 my $class = shift;
3487
3488 my $self = $class->SUPER::new (
3489 visible => 1,
3490 @_,
3491 );
3492
3493 Scalar::Util::weaken ($self->{root} = $self);
3494
3495 $self
3496}
3497
3498sub size_request {
3499 my ($self) = @_;
3500
3501 ($self->{w}, $self->{h})
3502}
3503
3504sub _to_pixel {
3505 my ($coord, $size, $max) = @_;
3506
3507 $coord =
3508 $coord eq "center" ? ($max - $size) * 0.5
3509 : $coord eq "max" ? $max
3510 : $coord;
3511
3512 $coord = 0 if $coord < 0;
3513 $coord = $max - $size if $coord > $max - $size;
3514
3515 int $coord + 0.5
3516}
3517
3518sub size_allocate {
3519 my ($self, $w, $h) = @_;
3520
3521 for my $child ($self->children) {
3522 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3523
3524 $X = $child->{force_x} if exists $child->{force_x};
3525 $Y = $child->{force_y} if exists $child->{force_y};
3526
3527 $X = _to_pixel $X, $W, $self->{w};
3528 $Y = _to_pixel $Y, $H, $self->{h};
3529
3530 $child->configure ($X, $Y, $W, $H);
3531 }
3532}
3533
3534sub coord2local {
3535 my ($self, $x, $y) = @_;
3536
3537 ($x, $y)
3538}
3539
3540sub coord2global {
3541 my ($self, $x, $y) = @_;
3542
3543 ($x, $y)
3544}
3545
3546sub update {
3547 my ($self) = @_;
3548
3549 $::WANT_REFRESH++;
3550}
3551
3552sub add {
3553 my ($self, @children) = @_;
3554
3555 $_->{is_toplevel} = 1
3556 for @children;
3557
3558 $self->SUPER::add (@children);
3559}
3560
3561sub remove {
3562 my ($self, @children) = @_;
3563
3564 $self->SUPER::remove (@children);
3565
3566 delete $self->{is_toplevel}
3567 for @children;
3568
3569 while (@children) {
3570 my $w = pop @children;
3571 push @children, $w->children;
3572 $w->set_invisible;
3573 }
3574}
3575
3576sub on_refresh {
3577 my ($self, $id, $cb) = @_;
3578
3579 $self->{refresh_hook}{$id} = $cb;
3580}
3581
3582sub on_post_alloc {
3583 my ($self, $id, $cb) = @_;
3584
3585 $self->{post_alloc_hook}{$id} = $cb;
3586}
3587
3588sub draw {
3589 my ($self) = @_;
3590
3591 while ($self->{refresh_hook}) {
3592 $_->()
3593 for values %{delete $self->{refresh_hook}};
3594 }
3595
3596 if ($self->{realloc}) {
3597 my %queue;
3598 my @queue;
3599 my $widget;
3600
3601 outer:
3602 while () {
3603 if (my $realloc = delete $self->{realloc}) {
3604 for $widget (values %$realloc) {
3605 $widget->{visible} or next; # do not resize invisible widgets
3606
3607 $queue{$widget+0}++ and next; # duplicates are common
3608
3609 push @{ $queue[$widget->{visible}] }, $widget;
3610 }
3611 }
3612
3613 while () {
3614 @queue or last outer;
3615
3616 $widget = pop @{ $queue[-1] || [] }
3617 and last;
3618
3619 pop @queue;
3620 }
3621
3622 delete $queue{$widget+0};
3623
3624 my ($w, $h) = $widget->size_request;
3625
3626 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3627 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3628
3629 $w = $widget->{force_w} if exists $widget->{force_w};
3630 $h = $widget->{force_h} if exists $widget->{force_h};
3631
3632 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3633 || delete $widget->{force_realloc}) {
3634 $widget->{req_w} = $w;
3635 $widget->{req_h} = $h;
3636
3637 $self->{size_alloc}{$widget+0} = $widget;
3638
3639 if (my $parent = $widget->{parent}) {
3640 $self->{realloc}{$parent+0} = $parent
3641 unless $queue{$parent+0};
3642
3643 $parent->{force_size_alloc} = 1;
3644 $self->{size_alloc}{$parent+0} = $parent;
3645 }
3646 }
3647
3648 delete $self->{realloc}{$widget+0};
3649 }
3650 }
3651
3652 while (my $size_alloc = delete $self->{size_alloc}) {
3653 my @queue = sort { $b->{visible} <=> $a->{visible} }
3654 values %$size_alloc;
3655
3656 while () {
3657 my $widget = pop @queue || last;
3658
3659 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3660
3661 $w = 0 if $w < 0;
3662 $h = 0 if $h < 0;
3663
3664 $w = int $w + 0.5;
3665 $h = int $h + 0.5;
3666
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
3671 $widget->{w} = $w;
3672 $widget->{h} = $h;
3673
3674 $widget->emit (size_allocate => $w, $h);
3675 }
3676 }
3677 }
3678
3679 while ($self->{post_alloc_hook}) {
3680 $_->()
3681 for values %{delete $self->{post_alloc_hook}};
3682 }
3683
3684
3685 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3686 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3687 glClear GL_COLOR_BUFFER_BIT;
3688
3689 glMatrixMode GL_PROJECTION;
3690 glLoadIdentity;
3691 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3692 glMatrixMode GL_MODELVIEW;
3693 glLoadIdentity;
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
3702 $self->_draw;
3703}
3704
3705#############################################################################
3706
3465package CFClient::UI; 3707package CFClient::UI;
3466 3708
3467$ROOT = new CFClient::UI::Root; 3709$ROOT = new CFClient::UI::Root;
3468$TOOLTIP = new CFClient::UI::Tooltip z => 900; 3710$TOOLTIP = new CFClient::UI::Tooltip z => 900;
3469 3711

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines