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.331 by root, Sun Jul 23 04:46:02 2006 UTC vs.
Revision 1.354 by root, Sat Nov 18 23:31:23 2006 UTC

1package CFClient::UI; 1package CFPlus::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8use Event; 8use Event;
9 9
10use CFClient; 10use CFPlus;
11use CFPlus::Pod;
11use CFClient::Texture; 12use CFPlus::Texture;
12 13
13our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
14 15
15our $LAYOUT; 16our $LAYOUT;
16our $ROOT; 17our $ROOT;
96 97
97sub feed_sdl_button_down_event { 98sub feed_sdl_button_down_event {
98 my ($ev) = @_; 99 my ($ev) = @_;
99 my ($x, $y) = ($ev->{x}, $ev->{y}); 100 my ($x, $y) = ($ev->{x}, $ev->{y});
100 101
101 unless ($BUTTON_STATE) { 102 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
103
104 unless ($GRAB) {
102 my $widget = $ROOT->find_widget ($x, $y); 105 my $widget = $ROOT->find_widget ($x, $y);
103 106
104 $GRAB = $widget; 107 $GRAB = $widget;
105 $GRAB->update if $GRAB; 108 $GRAB->update if $GRAB;
106 109
107 $TOOLTIP_WATCHER->cb->(); 110 $TOOLTIP_WATCHER->cb->();
108 } 111 }
109
110 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
111 112
112 if ($GRAB) { 113 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) { 114 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel 115 # mousewheel
115 $ev->{dx} = 0; 116 $ev->{dx} = 0;
207 reconfigure_widgets; 208 reconfigure_widgets;
208} 209}
209 210
210############################################################################# 211#############################################################################
211 212
212package CFClient::UI::Event; 213package CFPlus::UI::Event;
213 214
214sub xy { 215sub xy {
215 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 216 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
216} 217}
217 218
218############################################################################# 219#############################################################################
219 220
220package CFClient::UI::Base; 221package CFPlus::UI::Base;
221 222
222use strict; 223use strict;
223 224
224use CFClient::OpenGL; 225use CFPlus::OpenGL;
225 226
226sub new { 227sub new {
227 my $class = shift; 228 my $class = shift;
228 229
229 my $self = bless { 230 my $self = bless {
234 h => undef, 235 h => undef,
235 can_events => 1, 236 can_events => 1,
236 @_ 237 @_
237 }, $class; 238 }, $class;
238 239
239 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 240 Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
240 241
241 for (keys %$self) { 242 for (keys %$self) {
242 if (/^on_(.*)$/) { 243 if (/^on_(.*)$/) {
243 $self->connect ($1 => delete $self->{$_}); 244 $self->connect ($1 => delete $self->{$_});
244 } 245 }
245 } 246 }
246 247
247 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 248 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) {
248 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{x}; 249 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x};
249 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{y}; 250 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y};
250 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{w}; 251 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w};
251 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{h}; 252 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h};
252 253
253 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 254 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
254 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 255 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
255 256
256 $self->show if $layout->{show}; 257 $self->show if $layout->{show};
269sub show { 270sub show {
270 my ($self) = @_; 271 my ($self) = @_;
271 272
272 return if $self->{parent}; 273 return if $self->{parent};
273 274
274 $CFClient::UI::ROOT->add ($self); 275 $CFPlus::UI::ROOT->add ($self);
275} 276}
276 277
277sub set_visible { 278sub set_visible {
278 my ($self) = @_; 279 my ($self) = @_;
279 280
300 delete $self->{root}; 301 delete $self->{root};
301 302
302 undef $GRAB if $GRAB == $self; 303 undef $GRAB if $GRAB == $self;
303 undef $HOVER if $HOVER == $self; 304 undef $HOVER if $HOVER == $self;
304 305
305 $CFClient::UI::TOOLTIP_WATCHER->cb->() 306 $CFPlus::UI::TOOLTIP_WATCHER->cb->()
306 if $TOOLTIP->{owner} == $self; 307 if $TOOLTIP->{owner} == $self;
307 308
308 $self->emit ("focus_out"); 309 $self->emit ("focus_out");
309 $self->emit (visibility_change => 0); 310 $self->emit (visibility_change => 0);
310} 311}
367 my ($self, $x, $y, $w, $h) = @_; 368 my ($self, $x, $y, $w, $h) = @_;
368 369
369 if ($self->{aspect}) { 370 if ($self->{aspect}) {
370 my ($ow, $oh) = ($w, $h); 371 my ($ow, $oh) = ($w, $h);
371 372
372 $w = List::Util::min $w, int $h * $self->{aspect}; 373 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect};
373 $h = List::Util::min $h, int $w / $self->{aspect}; 374 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect};
374 375
375 # use alignment to adjust x, y 376 # use alignment to adjust x, y
376 377
377 $x += int 0.5 * ($ow - $w); 378 $x += int 0.5 * ($ow - $w);
378 $y += int 0.5 * ($oh - $h); 379 $y += int 0.5 * ($oh - $h);
419 420
420 return if $self->{tooltip} eq $tooltip; 421 return if $self->{tooltip} eq $tooltip;
421 422
422 $self->{tooltip} = $tooltip; 423 $self->{tooltip} = $tooltip;
423 424
424 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 425 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
425 delete $CFClient::UI::TOOLTIP->{owner}; 426 delete $CFPlus::UI::TOOLTIP->{owner};
426 $CFClient::UI::TOOLTIP_WATCHER->cb->(); 427 $CFPlus::UI::TOOLTIP_WATCHER->cb->();
427 } 428 }
428} 429}
429 430
430# translate global coordinates to local coordinate system 431# translate global coordinates to local coordinate system
431sub coord2local { 432sub coord2local {
437} 438}
438 439
439# translate local coordinates to global coordinate system 440# translate local coordinates to global coordinate system
440sub coord2global { 441sub coord2global {
441 my ($self, $x, $y) = @_; 442 my ($self, $x, $y) = @_;
443
444 Carp::confess unless $self->{parent};#d#
442 445
443 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 446 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
444} 447}
445 448
446sub invoke_focus_in { 449sub invoke_focus_in {
494 497
495sub connect { 498sub connect {
496 my ($self, $signal, $cb) = @_; 499 my ($self, $signal, $cb) = @_;
497 500
498 push @{ $self->{signal_cb}{$signal} }, $cb; 501 push @{ $self->{signal_cb}{$signal} }, $cb;
502
503 defined wantarray and CFPlus::guard {
504 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
505 @{ $self->{signal_cb}{$signal} };
506 }
499} 507}
500 508
501my %has_coords = ( 509my %has_coords = (
502 button_down => 1, 510 button_down => 1,
503 button_up => 1, 511 button_up => 1,
506); 514);
507 515
508sub emit { 516sub emit {
509 my ($self, $signal, @args) = @_; 517 my ($self, $signal, @args) = @_;
510 518
519 # I do not really like this solution, but I do not like duplication
520 # and needlessly verbose code, either.
511 my @append 521 my @append
512 = $has_coords{$signal} 522 = $has_coords{$signal}
513 ? $args[0]->xy ($self) 523 ? $args[0]->xy ($self)
514 : (); 524 : ();
515 525
516 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 526 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
517 527
528 if ($self->{tooltip} =~ /Apply.*audio/) {
529 warn "2 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d#
530 warn "3 $self->{parent}\n";#d#
531 }
532
518 #d##TODO# stop propagating at first true, do not use sum 533 #d##TODO# stop propagating at first true, do not use sum
534 my $res =
519 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 535 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before
520 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 536 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure
521 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 537 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
538 ;
539 if ($self->{tooltip} =~ /Apply.*audio/) {
540 warn "4 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d#
541 warn "5 $self->{parent}\n";#d#
542 }
543
544 $res
522} 545}
523 546
524sub find_widget { 547sub find_widget {
525 my ($self, $x, $y) = @_; 548 my ($self, $x, $y) = @_;
526 549
610 glVertex $self->{w} - 1, 0; 633 glVertex $self->{w} - 1, 0;
611 glVertex $self->{w} - 1, $self->{h} - 1; 634 glVertex $self->{w} - 1, $self->{h} - 1;
612 glVertex 0 , $self->{h} - 1; 635 glVertex 0 , $self->{h} - 1;
613 glEnd; 636 glEnd;
614 glPopMatrix; 637 glPopMatrix;
615 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 638 #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
616 } 639 }
617 640
618 $self->_draw; 641 $self->_draw;
619 glPopMatrix; 642 glPopMatrix;
620} 643}
626} 649}
627 650
628sub DESTROY { 651sub DESTROY {
629 my ($self) = @_; 652 my ($self) = @_;
630 653
631 return if CFClient::in_destruct; 654 return if CFPlus::in_destruct;
632 655
633 delete $WIDGET{$self+0}; 656 delete $WIDGET{$self+0};
634 657
635 eval { $self->destroy }; 658 eval { $self->destroy };
636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 659 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
637} 660}
638 661
639############################################################################# 662#############################################################################
640 663
641package CFClient::UI::DrawBG; 664package CFPlus::UI::DrawBG;
642 665
643our @ISA = CFClient::UI::Base::; 666our @ISA = CFPlus::UI::Base::;
644 667
645use strict; 668use strict;
646use CFClient::OpenGL; 669use CFPlus::OpenGL;
647 670
648sub new { 671sub new {
649 my $class = shift; 672 my $class = shift;
650 673
651 # range [value, low, high, page] 674 # range [value, low, high, page]
682 } 705 }
683} 706}
684 707
685############################################################################# 708#############################################################################
686 709
687package CFClient::UI::Empty; 710package CFPlus::UI::Empty;
688 711
689our @ISA = CFClient::UI::Base::; 712our @ISA = CFPlus::UI::Base::;
690 713
691sub new { 714sub new {
692 my ($class, %arg) = @_; 715 my ($class, %arg) = @_;
693 $class->SUPER::new (can_events => 0, %arg); 716 $class->SUPER::new (can_events => 0, %arg);
694} 717}
701 724
702sub draw { } 725sub draw { }
703 726
704############################################################################# 727#############################################################################
705 728
706package CFClient::UI::Container; 729package CFPlus::UI::Container;
707 730
708our @ISA = CFClient::UI::Base::; 731our @ISA = CFPlus::UI::Base::;
709 732
710sub new { 733sub new {
711 my ($class, %arg) = @_; 734 my ($class, %arg) = @_;
712 735
713 my $children = delete $arg{children}; 736 my $children = delete $arg{children};
722 if $children; 745 if $children;
723 746
724 $self 747 $self
725} 748}
726 749
750sub realloc {
751 my ($self) = @_;
752
753 $self->{force_realloc} = 1;
754 $self->{force_size_alloc} = 1;
755 $self->SUPER::realloc;
756}
757
727sub add { 758sub add {
728 my ($self, @widgets) = @_; 759 my ($self, @widgets) = @_;
729 760
730 $_->set_parent ($self) 761 $_->set_parent ($self)
731 for @widgets; 762 for @widgets;
791 $_->draw for @{$self->{children}}; 822 $_->draw for @{$self->{children}};
792} 823}
793 824
794############################################################################# 825#############################################################################
795 826
796package CFClient::UI::Bin; 827package CFPlus::UI::Bin;
797 828
798our @ISA = CFClient::UI::Container::; 829our @ISA = CFPlus::UI::Container::;
799 830
800sub new { 831sub new {
801 my ($class, %arg) = @_; 832 my ($class, %arg) = @_;
802 833
803 my $child = (delete $arg{child}) || new CFClient::UI::Empty::; 834 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::;
804 835
805 $class->SUPER::new (children => [$child], %arg) 836 $class->SUPER::new (children => [$child], %arg)
806} 837}
807 838
808sub add { 839sub add {
815sub remove { 846sub remove {
816 my ($self, $widget) = @_; 847 my ($self, $widget) = @_;
817 848
818 $self->SUPER::remove ($widget); 849 $self->SUPER::remove ($widget);
819 850
820 $self->{children} = [new CFClient::UI::Empty] 851 $self->{children} = [new CFPlus::UI::Empty]
821 unless @{$self->{children}}; 852 unless @{$self->{children}};
822} 853}
823 854
824sub child { $_[0]->{children}[0] } 855sub child { $_[0]->{children}[0] }
825 856
837 868
838############################################################################# 869#############################################################################
839 870
840# back-buffered drawing area 871# back-buffered drawing area
841 872
842package CFClient::UI::Window; 873package CFPlus::UI::Window;
843 874
844our @ISA = CFClient::UI::Bin::; 875our @ISA = CFPlus::UI::Bin::;
845 876
846use CFClient::OpenGL; 877use CFPlus::OpenGL;
847 878
848sub new { 879sub new {
849 my ($class, %arg) = @_; 880 my ($class, %arg) = @_;
850 881
851 my $self = $class->SUPER::new (%arg); 882 my $self = $class->SUPER::new (%arg);
873} 904}
874 905
875sub render_child { 906sub render_child {
876 my ($self) = @_; 907 my ($self) = @_;
877 908
878 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 909 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub {
879 glClearColor 0, 0, 0, 0; 910 glClearColor 0, 0, 0, 0;
880 glClear GL_COLOR_BUFFER_BIT; 911 glClear GL_COLOR_BUFFER_BIT;
881 912
882 { 913 {
883 package CFClient::UI::Base; 914 package CFPlus::UI::Base;
884 915
885 ($draw_x, $draw_y, $draw_w, $draw_h) = 916 local ($draw_x, $draw_y, $draw_w, $draw_h) =
886 (0, 0, $self->{w}, $self->{h}); 917 (0, 0, $self->{w}, $self->{h});
918
919 $self->_render;
887 } 920 }
888
889 $self->_render;
890 }; 921 };
891} 922}
892 923
893sub _draw { 924sub _draw {
894 my ($self) = @_; 925 my ($self) = @_;
895
896 my ($w, $h) = @$self{qw(w h)};
897 926
898 my $tex = $self->{texture} 927 my $tex = $self->{texture}
899 or return; 928 or return;
900 929
901 glEnable GL_TEXTURE_2D; 930 glEnable GL_TEXTURE_2D;
902 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 931 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
903 glColor 0, 0, 0, 1; 932 glColor 0, 0, 0, 1;
904 933
905 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 934 $tex->draw_quad_alpha_premultiplied (0, 0);
906 935
907 glDisable GL_TEXTURE_2D; 936 glDisable GL_TEXTURE_2D;
908} 937}
909 938
910############################################################################# 939#############################################################################
911 940
912package CFClient::UI::ViewPort; 941package CFPlus::UI::ViewPort;
913 942
914our @ISA = CFClient::UI::Window::; 943our @ISA = CFPlus::UI::Window::;
915 944
916sub new { 945sub new {
917 my $class = shift; 946 my $class = shift;
918 947
919 $class->SUPER::new ( 948 $class->SUPER::new (
979 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1008 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
980 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1009 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
981 ) { 1010 ) {
982 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1011 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
983 } else { 1012 } else {
984 $self->CFClient::UI::Base::find_widget ($x, $y) 1013 $self->CFPlus::UI::Base::find_widget ($x, $y)
985 } 1014 }
986} 1015}
987 1016
988sub _render { 1017sub _render {
989 my ($self) = @_; 1018 my ($self) = @_;
990 1019
991 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x}; 1020 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x};
992 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y}; 1021 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y};
993 1022
994 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1023 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
995 1024
996 $self->SUPER::_render; 1025 $self->SUPER::_render;
997} 1026}
998 1027
999############################################################################# 1028#############################################################################
1000 1029
1001package CFClient::UI::ScrolledWindow; 1030package CFPlus::UI::ScrolledWindow;
1002 1031
1003our @ISA = CFClient::UI::HBox::; 1032our @ISA = CFPlus::UI::HBox::;
1004 1033
1005sub new { 1034sub new {
1006 my ($class, %arg) = @_; 1035 my ($class, %arg) = @_;
1007 1036
1008 my $child = delete $arg{child}; 1037 my $child = delete $arg{child};
1009 1038
1010 my $self; 1039 my $self;
1011 1040
1012 my $slider = new CFClient::UI::Slider 1041 my $slider = new CFPlus::UI::Slider
1013 vertical => 1, 1042 vertical => 1,
1014 range => [0, 0, 1, 0.01], # HACK fix 1043 range => [0, 0, 1, 0.01], # HACK fix
1015 on_changed => sub { 1044 on_changed => sub {
1016 $self->{vp}->set_offset (0, $_[1]); 1045 $self->{vp}->set_offset (0, $_[1]);
1017 }, 1046 },
1018 ; 1047 ;
1019 1048
1020 $self = $class->SUPER::new ( 1049 $self = $class->SUPER::new (
1021 vp => (new CFClient::UI::ViewPort expand => 1), 1050 vp => (new CFPlus::UI::ViewPort expand => 1),
1022 can_events => 1, 1051 can_events => 1,
1023 slider => $slider, 1052 slider => $slider,
1024 %arg, 1053 %arg,
1025 ); 1054 );
1026 1055
1070 $self->SUPER::invoke_size_allocate ($w, $h) 1099 $self->SUPER::invoke_size_allocate ($w, $h)
1071} 1100}
1072 1101
1073############################################################################# 1102#############################################################################
1074 1103
1075package CFClient::UI::Frame; 1104package CFPlus::UI::Frame;
1076 1105
1077our @ISA = CFClient::UI::Bin::; 1106our @ISA = CFPlus::UI::Bin::;
1078 1107
1079use CFClient::OpenGL; 1108use CFPlus::OpenGL;
1080 1109
1081sub new { 1110sub new {
1082 my $class = shift; 1111 my $class = shift;
1083 1112
1084 $class->SUPER::new ( 1113 $class->SUPER::new (
1110 $self->SUPER::_draw; 1139 $self->SUPER::_draw;
1111} 1140}
1112 1141
1113############################################################################# 1142#############################################################################
1114 1143
1115package CFClient::UI::FancyFrame; 1144package CFPlus::UI::FancyFrame;
1116 1145
1117our @ISA = CFClient::UI::Bin::; 1146our @ISA = CFPlus::UI::Bin::;
1118 1147
1119use CFClient::OpenGL; 1148use CFPlus::OpenGL;
1149
1150sub new {
1151 my ($class, %arg) = @_;
1152
1153 if ((exists $arg{label}) && !ref $arg{label}) {
1154 $arg{label} = new CFPlus::UI::Label
1155 align => 1,
1156 valign => 0,
1157 text => $arg{label},
1158 fontsize => ($arg{border} || 0.8) * 0.75;
1159 }
1160
1161 my $self = $class->SUPER::new (
1162 # label => "",
1163 fg => [0.6, 0.3, 0.1],
1164 border => 0.8,
1165 style => 'single',
1166 %arg,
1167 );
1168
1169 $self
1170}
1171
1172sub add {
1173 my ($self, @widgets) = @_;
1174
1175 $self->SUPER::add (@widgets);
1176 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1177}
1178
1179sub border {
1180 int $_[0]{border} * $::FONTSIZE
1181}
1182
1183sub size_request {
1184 my ($self) = @_;
1185
1186 ($self->{label_w}, undef) = $self->{label}->size_request
1187 if $self->{label};
1188
1189 my ($w, $h) = $self->SUPER::size_request;
1190
1191 (
1192 $w + $self->border * 2,
1193 $h + $self->border * 2,
1194 )
1195}
1196
1197sub invoke_size_allocate {
1198 my ($self, $w, $h) = @_;
1199
1200 my $border = $self->border;
1201
1202 $w -= List::Util::max 0, $border * 2;
1203 $h -= List::Util::max 0, $border * 2;
1204
1205 if (my $label = $self->{label}) {
1206 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1207 $label->{h} = List::Util::min $h, $border;
1208 $label->invoke_size_allocate ($label->{w}, $label->{h});
1209 }
1210
1211 $self->child->configure ($border, $border, $w, $h);
1212
1213 1
1214}
1215
1216sub _draw {
1217 my ($self) = @_;
1218
1219 my $child = $self->{children}[0];
1220
1221 my $border = $self->border;
1222 my ($w, $h) = ($self->{w}, $self->{h});
1223
1224 $child->draw;
1225
1226 glColor @{$self->{fg}};
1227 glBegin GL_LINE_STRIP;
1228 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1229 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1230 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1231 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1232 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1233 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1234 glEnd;
1235
1236 if ($self->{label}) {
1237 glTranslate $border * 2, 0;
1238 $self->{label}->_draw;
1239 }
1240}
1241
1242#############################################################################
1243
1244package CFPlus::UI::Toplevel;
1245
1246our @ISA = CFPlus::UI::Bin::;
1247
1248use CFPlus::OpenGL;
1120 1249
1121my $bg = 1250my $bg =
1122 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", 1251 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png",
1123 mipmap => 1, wrap => 1; 1252 mipmap => 1, wrap => 1;
1124 1253
1125my @border = 1254my @border =
1126 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1255 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1127 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1256 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1128 1257
1129sub new { 1258sub new {
1130 my ($class, %arg) = @_; 1259 my ($class, %arg) = @_;
1131 1260
1137 min_w => 64, 1266 min_w => 64,
1138 min_h => 32, 1267 min_h => 32,
1139 %arg, 1268 %arg,
1140 ); 1269 );
1141 1270
1142 $self->{title_widget} = new CFClient::UI::Label 1271 $self->{title_widget} = new CFPlus::UI::Label
1143 align => 0, 1272 align => 0,
1144 valign => 1, 1273 valign => 1,
1145 text => $self->{title}, 1274 text => $self->{title},
1146 fontsize => $self->{border}, 1275 fontsize => $self->{border},
1147 if exists $self->{title}; 1276 if exists $self->{title};
1148 1277
1149 if ($self->{has_close_button}) { 1278 if ($self->{has_close_button}) {
1150 $self->{close_button} = 1279 $self->{close_button} =
1151 new CFClient::UI::ImageButton 1280 new CFPlus::UI::ImageButton
1152 path => 'x1_close.png', 1281 path => 'x1_close.png',
1153 on_activate => sub { $self->emit ("delete") }; 1282 on_activate => sub { $self->emit ("delete") };
1154 1283
1155 $self->CFClient::UI::Container::add ($self->{close_button}); 1284 $self->CFPlus::UI::Container::add ($self->{close_button});
1156 } 1285 }
1157 1286
1158 $self 1287 $self
1159} 1288}
1160 1289
1161sub add { 1290sub add {
1162 my ($self, @widgets) = @_; 1291 my ($self, @widgets) = @_;
1163 1292
1164 $self->SUPER::add (@widgets); 1293 $self->SUPER::add (@widgets);
1165 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1294 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button};
1166 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1295 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1167} 1296}
1168 1297
1169sub border { 1298sub border {
1170 int $_[0]{border} * $::FONTSIZE 1299 int $_[0]{border} * $::FONTSIZE
1171} 1300}
1279 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1408 $self->{motion}->($ev, $x, $y) if $self->{motion};
1280 1409
1281 ! ! $self->{motion} 1410 ! ! $self->{motion}
1282} 1411}
1283 1412
1413sub invoke_visibility_change {
1414 my ($self, $visible) = @_;
1415
1416 delete $self->{motion} unless $visible;
1417
1418 0
1419}
1420
1284sub _draw { 1421sub _draw {
1285 my ($self) = @_; 1422 my ($self) = @_;
1286 1423
1287 my $child = $self->{children}[0]; 1424 my $child = $self->{children}[0];
1288 1425
1325 if $self->{close_button}; 1462 if $self->{close_button};
1326} 1463}
1327 1464
1328############################################################################# 1465#############################################################################
1329 1466
1330package CFClient::UI::Table; 1467package CFPlus::UI::Table;
1331 1468
1332our @ISA = CFClient::UI::Base::; 1469our @ISA = CFPlus::UI::Base::;
1333 1470
1334use List::Util qw(max sum); 1471use List::Util qw(max sum);
1335 1472
1336use CFClient::OpenGL; 1473use CFPlus::OpenGL;
1337 1474
1338sub new { 1475sub new {
1339 my $class = shift; 1476 my $class = shift;
1340 1477
1341 $class->SUPER::new ( 1478 $class->SUPER::new (
1347sub children { 1484sub children {
1348 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1485 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1349} 1486}
1350 1487
1351sub add { 1488sub add {
1352 my ($self, $x, $y, $child) = @_; 1489 my ($self) = shift;
1353 1490
1491 while (@_) {
1492 my ($x, $y, $child) = splice @_, 0, 3, ();
1354 $child->set_parent ($self); 1493 $child->set_parent ($self);
1355 $self->{children}[$y][$x] = $child; 1494 $self->{children}[$y][$x] = $child;
1495 }
1356 1496
1497 $self->{force_realloc} = 1;
1498 $self->{force_size_alloc} = 1;
1357 $self->realloc; 1499 $self->realloc;
1358} 1500}
1359 1501
1360sub remove { 1502sub remove {
1361 my ($self, $child) = @_; 1503 my ($self, $child) = @_;
1426 1568
1427 # linearly scale sizes 1569 # linearly scale sizes
1428 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1570 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1429 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 1571 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1430 1572
1431 CFClient::UI::harmonize $ws; 1573 CFPlus::UI::harmonize $ws;
1432 CFClient::UI::harmonize $hs; 1574 CFPlus::UI::harmonize $hs;
1433 1575
1434 my $y; 1576 my $y;
1435 1577
1436 for my $r (0 .. $#{$self->{children}}) { 1578 for my $r (0 .. $#{$self->{children}}) {
1437 my $row = $self->{children}[$r] 1579 my $row = $self->{children}[$r]
1480 } 1622 }
1481} 1623}
1482 1624
1483############################################################################# 1625#############################################################################
1484 1626
1485package CFClient::UI::Box; 1627package CFPlus::UI::Box;
1486 1628
1487our @ISA = CFClient::UI::Container::; 1629our @ISA = CFPlus::UI::Container::;
1488 1630
1489sub size_request { 1631sub size_request {
1490 my ($self) = @_; 1632 my ($self) = @_;
1491 1633
1492 $self->{vertical} 1634 $self->{vertical}
1525 $req[$_] += $space * $children[$_]{expand} 1667 $req[$_] += $space * $children[$_]{expand}
1526 for 0 .. $#children; 1668 for 0 .. $#children;
1527 } 1669 }
1528 } 1670 }
1529 1671
1530 CFClient::UI::harmonize \@req; 1672 CFPlus::UI::harmonize \@req;
1531 1673
1532 my $pos = 0; 1674 my $pos = 0;
1533 for (0 .. $#children) { 1675 for (0 .. $#children) {
1534 my $alloc = $req[$_]; 1676 my $alloc = $req[$_];
1535 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1677 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1540 1 1682 1
1541} 1683}
1542 1684
1543############################################################################# 1685#############################################################################
1544 1686
1545package CFClient::UI::HBox; 1687package CFPlus::UI::HBox;
1546 1688
1547our @ISA = CFClient::UI::Box::; 1689our @ISA = CFPlus::UI::Box::;
1548 1690
1549sub new { 1691sub new {
1550 my $class = shift; 1692 my $class = shift;
1551 1693
1552 $class->SUPER::new ( 1694 $class->SUPER::new (
1555 ) 1697 )
1556} 1698}
1557 1699
1558############################################################################# 1700#############################################################################
1559 1701
1560package CFClient::UI::VBox; 1702package CFPlus::UI::VBox;
1561 1703
1562our @ISA = CFClient::UI::Box::; 1704our @ISA = CFPlus::UI::Box::;
1563 1705
1564sub new { 1706sub new {
1565 my $class = shift; 1707 my $class = shift;
1566 1708
1567 $class->SUPER::new ( 1709 $class->SUPER::new (
1570 ) 1712 )
1571} 1713}
1572 1714
1573############################################################################# 1715#############################################################################
1574 1716
1575package CFClient::UI::Label; 1717package CFPlus::UI::Label;
1576 1718
1577our @ISA = CFClient::UI::DrawBG::; 1719our @ISA = CFPlus::UI::DrawBG::;
1578 1720
1579use CFClient::OpenGL; 1721use CFPlus::OpenGL;
1580 1722
1581sub new { 1723sub new {
1582 my ($class, %arg) = @_; 1724 my ($class, %arg) = @_;
1583 1725
1584 my $self = $class->SUPER::new ( 1726 my $self = $class->SUPER::new (
1587 #active_bg => none 1729 #active_bg => none
1588 #font => default_font 1730 #font => default_font
1589 #text => initial text 1731 #text => initial text
1590 #markup => initial narkup 1732 #markup => initial narkup
1591 #max_w => maximum pixel width 1733 #max_w => maximum pixel width
1734 #style => 0, # render flags
1592 ellipsise => 3, # end 1735 ellipsise => 3, # end
1593 layout => (new CFClient::Layout), 1736 layout => (new CFPlus::Layout),
1594 fontsize => 1, 1737 fontsize => 1,
1595 align => -1, 1738 align => -1,
1596 valign => -1, 1739 valign => -1,
1597 padding_x => 2, 1740 padding_x => 2,
1598 padding_y => 2, 1741 padding_y => 2,
1599 can_events => 0, 1742 can_events => 0,
1600 %arg 1743 %arg
1601 ); 1744 );
1602 1745
1603 if (exists $self->{template}) { 1746 if (exists $self->{template}) {
1604 my $layout = new CFClient::Layout; 1747 my $layout = new CFPlus::Layout;
1605 $layout->set_text (delete $self->{template}); 1748 $layout->set_text (delete $self->{template});
1606 $self->{template} = $layout; 1749 $self->{template} = $layout;
1607 } 1750 }
1608 1751
1609 if (exists $self->{markup}) { 1752 if (exists $self->{markup}) {
1611 } else { 1754 } else {
1612 $self->set_text (delete $self->{text}); 1755 $self->set_text (delete $self->{text});
1613 } 1756 }
1614 1757
1615 $self 1758 $self
1616}
1617
1618sub escape($) {
1619 local $_ = $_[0];
1620
1621 s/&/&amp;/g;
1622 s/>/&gt;/g;
1623 s/</&lt;/g;
1624
1625 $_
1626} 1759}
1627 1760
1628sub update { 1761sub update {
1629 my ($self) = @_; 1762 my ($self) = @_;
1630 1763
1757 }; 1890 };
1758 1891
1759 my $w = List::Util::min $self->{w} + 4, $size->[0]; 1892 my $w = List::Util::min $self->{w} + 4, $size->[0];
1760 my $h = List::Util::min $self->{h} + 2, $size->[1]; 1893 my $h = List::Util::min $self->{h} + 2, $size->[1];
1761 1894
1762 $self->{layout}->render ($self->{ox}, $self->{oy}); 1895 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1763} 1896}
1764 1897
1765############################################################################# 1898#############################################################################
1766 1899
1767package CFClient::UI::EntryBase; 1900package CFPlus::UI::EntryBase;
1768 1901
1769our @ISA = CFClient::UI::Label::; 1902our @ISA = CFPlus::UI::Label::;
1770 1903
1771use CFClient::OpenGL; 1904use CFPlus::OpenGL;
1772 1905
1773sub new { 1906sub new {
1774 my $class = shift; 1907 my $class = shift;
1775 1908
1776 $class->SUPER::new ( 1909 $class->SUPER::new (
1838 1971
1839 if ($uni == 8) { 1972 if ($uni == 8) {
1840 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1973 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1841 } elsif ($uni == 127) { 1974 } elsif ($uni == 127) {
1842 substr $text, $self->{cursor}, 1, ""; 1975 substr $text, $self->{cursor}, 1, "";
1843 } elsif ($sym == CFClient::SDLK_LEFT) { 1976 } elsif ($sym == CFPlus::SDLK_LEFT) {
1844 --$self->{cursor} if $self->{cursor}; 1977 --$self->{cursor} if $self->{cursor};
1845 } elsif ($sym == CFClient::SDLK_RIGHT) { 1978 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1846 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1979 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1847 } elsif ($sym == CFClient::SDLK_HOME) { 1980 } elsif ($sym == CFPlus::SDLK_HOME) {
1848 $self->{cursor} = 0; 1981 $self->{cursor} = 0;
1849 } elsif ($sym == CFClient::SDLK_END) { 1982 } elsif ($sym == CFPlus::SDLK_END) {
1850 $self->{cursor} = length $text; 1983 $self->{cursor} = length $text;
1984 } elsif ($uni == 21) { # ctrl-u
1985 $text = "";
1986 $self->{cursor} = 0;
1851 } elsif ($uni == 27) { 1987 } elsif ($uni == 27) {
1852 $self->emit ('escape'); 1988 $self->emit ('escape');
1853 } elsif ($uni) { 1989 } elsif ($uni) {
1854 substr $text, $self->{cursor}++, 0, chr $uni; 1990 substr $text, $self->{cursor}++, 0, chr $uni;
1855 } else { 1991 } else {
1928 utf8::encode $text; 2064 utf8::encode $text;
1929 2065
1930 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2066 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1931 } 2067 }
1932 2068
1933 glColor @{$self->{fg}};
1934 glBegin GL_LINES; 2069 glBegin GL_LINES;
1935 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2070 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1936 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2071 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1937 glEnd; 2072 glEnd;
1938 } 2073 }
1939} 2074}
1940 2075
1941package CFClient::UI::Entry; 2076package CFPlus::UI::Entry;
1942 2077
1943our @ISA = CFClient::UI::EntryBase::; 2078our @ISA = CFPlus::UI::EntryBase::;
1944 2079
1945use CFClient::OpenGL; 2080use CFPlus::OpenGL;
1946 2081
1947sub invoke_key_down { 2082sub invoke_key_down {
1948 my ($self, $ev) = @_; 2083 my ($self, $ev) = @_;
1949 2084
1950 my $sym = $ev->{sym}; 2085 my $sym = $ev->{sym};
1956 $self->{history_pointer} = -1; 2091 $self->{history_pointer} = -1;
1957 $self->{history_saveback} = ''; 2092 $self->{history_saveback} = '';
1958 $self->emit (activate => $txt); 2093 $self->emit (activate => $txt);
1959 $self->update; 2094 $self->update;
1960 2095
1961 } elsif ($sym == CFClient::SDLK_UP) { 2096 } elsif ($sym == CFPlus::SDLK_UP) {
1962 if ($self->{history_pointer} < 0) { 2097 if ($self->{history_pointer} < 0) {
1963 $self->{history_saveback} = $self->get_text; 2098 $self->{history_saveback} = $self->get_text;
1964 } 2099 }
1965 if (@{$self->{history} || []} > 0) { 2100 if (@{$self->{history} || []} > 0) {
1966 $self->{history_pointer}++; 2101 $self->{history_pointer}++;
1968 $self->{history_pointer} = @{$self->{history} || []} - 1; 2103 $self->{history_pointer} = @{$self->{history} || []} - 1;
1969 } 2104 }
1970 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2105 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1971 } 2106 }
1972 2107
1973 } elsif ($sym == CFClient::SDLK_DOWN) { 2108 } elsif ($sym == CFPlus::SDLK_DOWN) {
1974 $self->{history_pointer}--; 2109 $self->{history_pointer}--;
1975 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2110 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1976 2111
1977 if ($self->{history_pointer} >= 0) { 2112 if ($self->{history_pointer} >= 0) {
1978 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2113 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1987 1 2122 1
1988} 2123}
1989 2124
1990############################################################################# 2125#############################################################################
1991 2126
1992package CFClient::UI::Button; 2127package CFPlus::UI::Button;
1993 2128
1994our @ISA = CFClient::UI::Label::; 2129our @ISA = CFPlus::UI::Label::;
1995 2130
1996use CFClient::OpenGL; 2131use CFPlus::OpenGL;
1997 2132
1998my @tex = 2133my @tex =
1999 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2134 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2000 qw(b1_button_active.png); 2135 qw(b1_button_active.png);
2001 2136
2002sub new { 2137sub new {
2003 my $class = shift; 2138 my $class = shift;
2004 2139
2041 $self->SUPER::_draw; 2176 $self->SUPER::_draw;
2042} 2177}
2043 2178
2044############################################################################# 2179#############################################################################
2045 2180
2181package CFPlus::UI::CheckBox;
2182
2183our @ISA = CFPlus::UI::DrawBG::;
2184
2185my @tex =
2186 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2187 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2188
2189use CFPlus::OpenGL;
2190
2191sub new {
2192 my $class = shift;
2193
2194 $class->SUPER::new (
2195 padding_x => 2,
2196 padding_y => 2,
2197 fg => [1, 1, 1],
2198 active_fg => [1, 1, 0],
2199 bg => [0, 0, 0, 0.2],
2200 active_bg => [1, 1, 1, 0.5],
2201 state => 0,
2202 can_hover => 1,
2203 @_
2204 )
2205}
2206
2207sub size_request {
2208 my ($self) = @_;
2209
2210 (6) x 2
2211}
2212
2213sub toggle {
2214 my ($self) = @_;
2215
2216 $self->{state} = !$self->{state};
2217 $self->emit (changed => $self->{state});
2218 $self->update;
2219}
2220
2221sub invoke_button_down {
2222 my ($self, $ev, $x, $y) = @_;
2223
2224 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2225 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2226 $self->toggle;
2227 } else {
2228 return 0
2229 }
2230
2231 1
2232}
2233
2234sub _draw {
2235 my ($self) = @_;
2236
2237 $self->SUPER::_draw;
2238
2239 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2244
2245 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2246
2247 my $tex = $self->{state} ? $tex[1] : $tex[0];
2248
2249 glEnable GL_TEXTURE_2D;
2250 $tex->draw_quad_alpha (0, 0, $s, $s);
2251 glDisable GL_TEXTURE_2D;
2252}
2253
2254#############################################################################
2255
2256package CFPlus::UI::Image;
2257
2258our @ISA = CFPlus::UI::Base::;
2259
2260use CFPlus::OpenGL;
2261
2262our %texture_cache;
2263
2264sub new {
2265 my $class = shift;
2266
2267 my $self = $class->SUPER::new (
2268 can_events => 0,
2269 @_,
2270 );
2271
2272 $self->{path} || $self->{tex}
2273 or Carp::croak "'path' or 'tex' attributes required";
2274
2275 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2276 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2277
2278 Scalar::Util::weaken $texture_cache{$self->{path}};
2279
2280 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2281
2282 $self
2283}
2284
2285sub STORABLE_freeze {
2286 my ($self, $cloning) = @_;
2287
2288 $self->{path}
2289 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n";
2290
2291 $self->{path}
2292}
2293
2294sub STORABLE_attach {
2295 my ($self, $cloning, $path) = @_;
2296
2297 $self->new (path => $path)
2298}
2299
2300sub size_request {
2301 my ($self) = @_;
2302
2303 ($self->{tex}{w}, $self->{tex}{h})
2304}
2305
2306sub _draw {
2307 my ($self) = @_;
2308
2309 my $tex = $self->{tex};
2310
2311 my ($w, $h) = ($self->{w}, $self->{h});
2312
2313 if ($self->{rot90}) {
2314 glRotate 90, 0, 0, 1;
2315 glTranslate 0, -$self->{w}, 0;
2316
2317 ($w, $h) = ($h, $w);
2318 }
2319
2320 glEnable GL_TEXTURE_2D;
2321 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2322
2323 $tex->draw_quad (0, 0, $w, $h);
2324
2325 glDisable GL_TEXTURE_2D;
2326}
2327
2328#############################################################################
2329
2046package CFClient::UI::ImageButton; 2330package CFPlus::UI::ImageButton;
2047 2331
2048our @ISA = CFClient::UI::Image::; 2332our @ISA = CFPlus::UI::Image::;
2049 2333
2050use CFClient::OpenGL; 2334use CFPlus::OpenGL;
2051 2335
2052my %textures; 2336my %textures;
2053 2337
2054sub new { 2338sub new {
2055 my $class = shift; 2339 my $class = shift;
2077 1 2361 1
2078} 2362}
2079 2363
2080############################################################################# 2364#############################################################################
2081 2365
2082package CFClient::UI::CheckBox;
2083
2084our @ISA = CFClient::UI::DrawBG::;
2085
2086my @tex =
2087 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
2088 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2089
2090use CFClient::OpenGL;
2091
2092sub new {
2093 my $class = shift;
2094
2095 $class->SUPER::new (
2096 padding_x => 2,
2097 padding_y => 2,
2098 fg => [1, 1, 1],
2099 active_fg => [1, 1, 0],
2100 bg => [0, 0, 0, 0.2],
2101 active_bg => [1, 1, 1, 0.5],
2102 state => 0,
2103 can_hover => 1,
2104 @_
2105 )
2106}
2107
2108sub size_request {
2109 my ($self) = @_;
2110
2111 (6) x 2
2112}
2113
2114sub toggle {
2115 my ($self) = @_;
2116
2117 $self->{state} = !$self->{state};
2118 $self->emit (changed => $self->{state});
2119 $self->update;
2120}
2121
2122sub invoke_button_down {
2123 my ($self, $ev, $x, $y) = @_;
2124
2125 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2126 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2127 $self->toggle;
2128 } else {
2129 return 0
2130 }
2131
2132 1
2133}
2134
2135sub _draw {
2136 my ($self) = @_;
2137
2138 $self->SUPER::_draw;
2139
2140 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2141
2142 my ($w, $h) = @$self{qw(w h)};
2143
2144 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2145
2146 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2147
2148 my $tex = $self->{state} ? $tex[1] : $tex[0];
2149
2150 glEnable GL_TEXTURE_2D;
2151 $tex->draw_quad_alpha (0, 0, $s, $s);
2152 glDisable GL_TEXTURE_2D;
2153}
2154
2155#############################################################################
2156
2157package CFClient::UI::Image; 2366package CFPlus::UI::VGauge;
2158 2367
2159our @ISA = CFClient::UI::Base::; 2368our @ISA = CFPlus::UI::Base::;
2160
2161use CFClient::OpenGL;
2162
2163our %texture_cache;
2164
2165sub new {
2166 my $class = shift;
2167
2168 my $self = $class->SUPER::new (
2169 can_events => 0,
2170 @_,
2171 );
2172
2173 $self->{path} || $self->{tex}
2174 or Carp::croak "'path' or 'tex' attributes required";
2175
2176 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2177 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2178
2179 Scalar::Util::weaken $texture_cache{$self->{path}};
2180
2181 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2182
2183 $self
2184}
2185
2186sub size_request {
2187 my ($self) = @_;
2188
2189 ($self->{tex}{w}, $self->{tex}{h})
2190}
2191
2192sub _draw {
2193 my ($self) = @_;
2194
2195 my $tex = $self->{tex};
2196
2197 my ($w, $h) = ($self->{w}, $self->{h});
2198
2199 if ($self->{rot90}) {
2200 glRotate 90, 0, 0, 1;
2201 glTranslate 0, -$self->{w}, 0;
2202
2203 ($w, $h) = ($h, $w);
2204 }
2205
2206 glEnable GL_TEXTURE_2D;
2207 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2208
2209 $tex->draw_quad_alpha (0, 0, $w, $h);
2210
2211 glDisable GL_TEXTURE_2D;
2212}
2213
2214#############################################################################
2215
2216package CFClient::UI::VGauge;
2217
2218our @ISA = CFClient::UI::Base::;
2219 2369
2220use List::Util qw(min max); 2370use List::Util qw(min max);
2221 2371
2222use CFClient::OpenGL; 2372use CFPlus::OpenGL;
2223 2373
2224my %tex = ( 2374my %tex = (
2225 food => [ 2375 food => [
2226 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2376 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2227 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2377 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2228 ], 2378 ],
2229 grace => [ 2379 grace => [
2230 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2380 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2231 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2381 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2232 ], 2382 ],
2233 hp => [ 2383 hp => [
2234 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2384 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2235 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2385 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2236 ], 2386 ],
2237 mana => [ 2387 mana => [
2238 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2388 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2239 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2389 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2240 ], 2390 ],
2241); 2391);
2242 2392
2243# eg. VGauge->new (gauge => 'food'), default gauge: food 2393# eg. VGauge->new (gauge => 'food'), default gauge: food
2346 glDisable GL_TEXTURE_2D; 2496 glDisable GL_TEXTURE_2D;
2347} 2497}
2348 2498
2349############################################################################# 2499#############################################################################
2350 2500
2351package CFClient::UI::Gauge; 2501package CFPlus::UI::Gauge;
2352 2502
2353our @ISA = CFClient::UI::VBox::; 2503our @ISA = CFPlus::UI::VBox::;
2354 2504
2355sub new { 2505sub new {
2356 my ($class, %arg) = @_; 2506 my ($class, %arg) = @_;
2357 2507
2358 my $self = $class->SUPER::new ( 2508 my $self = $class->SUPER::new (
2360 can_hover => 1, 2510 can_hover => 1,
2361 can_events => 1, 2511 can_events => 1,
2362 %arg, 2512 %arg,
2363 ); 2513 );
2364 2514
2365 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 2515 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999");
2366 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2516 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2367 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); 2517 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999");
2368 2518
2369 $self 2519 $self
2370} 2520}
2371 2521
2372sub set_fontsize { 2522sub set_fontsize {
2393 $self->{value}->set_text ($val); 2543 $self->{value}->set_text ($val);
2394} 2544}
2395 2545
2396############################################################################# 2546#############################################################################
2397 2547
2398package CFClient::UI::Slider; 2548package CFPlus::UI::Slider;
2399 2549
2400use strict; 2550use strict;
2401 2551
2402use CFClient::OpenGL; 2552use CFPlus::OpenGL;
2403 2553
2404our @ISA = CFClient::UI::DrawBG::; 2554our @ISA = CFPlus::UI::DrawBG::;
2405 2555
2406my @tex = 2556my @tex =
2407 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 2557 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ }
2408 qw(s1_slider.png s1_slider_bg.png); 2558 qw(s1_slider.png s1_slider_bg.png);
2409 2559
2410sub new { 2560sub new {
2411 my $class = shift; 2561 my $class = shift;
2412 2562
2506sub invoke_mouse_wheel { 2656sub invoke_mouse_wheel {
2507 my ($self, $ev) = @_; 2657 my ($self, $ev) = @_;
2508 2658
2509 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2659 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2510 2660
2511 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1); 2661 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2);
2512 2662
2513 ! ! $delta 2663 ! ! $delta
2514} 2664}
2515 2665
2516sub update { 2666sub update {
2567 glDisable GL_TEXTURE_2D; 2717 glDisable GL_TEXTURE_2D;
2568} 2718}
2569 2719
2570############################################################################# 2720#############################################################################
2571 2721
2572package CFClient::UI::ValSlider; 2722package CFPlus::UI::ValSlider;
2573 2723
2574our @ISA = CFClient::UI::HBox::; 2724our @ISA = CFPlus::UI::HBox::;
2575 2725
2576sub new { 2726sub new {
2577 my ($class, %arg) = @_; 2727 my ($class, %arg) = @_;
2578 2728
2579 my $range = delete $arg{range}; 2729 my $range = delete $arg{range};
2580 2730
2581 my $self = $class->SUPER::new ( 2731 my $self = $class->SUPER::new (
2582 slider => (new CFClient::UI::Slider expand => 1, range => $range), 2732 slider => (new CFPlus::UI::Slider expand => 1, range => $range),
2583 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), 2733 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}),
2584 to_value => sub { shift }, 2734 to_value => sub { shift },
2585 from_value => sub { shift }, 2735 from_value => sub { shift },
2586 %arg, 2736 %arg,
2587 ); 2737 );
2588 2738
2608sub set_range { shift->{slider}->set_range (@_) } 2758sub set_range { shift->{slider}->set_range (@_) }
2609sub set_value { shift->{slider}->set_value (@_) } 2759sub set_value { shift->{slider}->set_value (@_) }
2610 2760
2611############################################################################# 2761#############################################################################
2612 2762
2613package CFClient::UI::TextScroller; 2763package CFPlus::UI::TextScroller;
2614 2764
2615our @ISA = CFClient::UI::HBox::; 2765our @ISA = CFPlus::UI::HBox::;
2616 2766
2617use CFClient::OpenGL; 2767use CFPlus::OpenGL;
2618 2768
2619sub new { 2769sub new {
2620 my $class = shift; 2770 my $class = shift;
2621 2771
2622 my $self = $class->SUPER::new ( 2772 my $self = $class->SUPER::new (
2624 can_events => 1, 2774 can_events => 1,
2625 indent => 0, 2775 indent => 0,
2626 #font => default_font 2776 #font => default_font
2627 @_, 2777 @_,
2628 2778
2629 layout => (new CFClient::Layout), 2779 layout => (new CFPlus::Layout),
2630 par => [], 2780 par => [],
2631 height => 0, 2781 height => 0,
2632 children => [ 2782 children => [
2633 (new CFClient::UI::Empty expand => 1), 2783 (new CFPlus::UI::Empty expand => 1),
2634 (new CFClient::UI::Slider vertical => 1), 2784 (new CFPlus::UI::Slider vertical => 1),
2635 ], 2785 ],
2636 ); 2786 );
2637 2787
2638 $self->{children}[1]->connect (changed => sub { $self->update }); 2788 $self->{children}[1]->connect (changed => sub { $self->update });
2639 2789
2716 2866
2717 # todo: base offset on lines or so, not on pixels 2867 # todo: base offset on lines or so, not on pixels
2718 $self->{children}[1]->set_value ($offset); 2868 $self->{children}[1]->set_value ($offset);
2719} 2869}
2720 2870
2871sub current_paragraph {
2872 my ($self) = @_;
2873
2874 $self->{top_paragraph} - 1
2875}
2876
2877sub scroll_to {
2878 my ($self, $para) = @_;
2879
2880 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2881
2882 $self->{scroll_to} = $para;
2883 $self->update;
2884}
2885
2721sub clear { 2886sub clear {
2722 my ($self) = @_; 2887 my ($self) = @_;
2723 2888
2724 my (undef, undef, @other) = @{ $self->{children} }; 2889 my (undef, undef, @other) = @{ $self->{children} };
2725 $self->remove ($_) for @other; 2890 $self->remove ($_) for @other;
2728 $self->{height} = 0; 2893 $self->{height} = 0;
2729 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 2894 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2730} 2895}
2731 2896
2732sub add_paragraph { 2897sub add_paragraph {
2733 my ($self, $color, $para, $indent) = @_; 2898 my $self = shift;
2734 2899
2735 my ($text, @w) = ref $para ? @$para : $para; 2900 for my $para (@_) {
2736
2737 $para = { 2901 $para = {
2902 fg => [1, 1, 1, 1],
2903 indent => 0,
2904 markup => "",
2905 widget => [],
2906 ref $para ? %$para : (markup => $para),
2738 w => 1e10, 2907 w => 1e10,
2739 wrapped => 1, 2908 wrapped => 1,
2740 fg => $color,
2741 indent => $indent,
2742 markup => $text,
2743 widget => \@w,
2744 }; 2909 };
2745 2910
2746 $self->add (@w) if @w; 2911 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2747 push @{$self->{par}}, $para; 2912 push @{$self->{par}}, $para;
2913 }
2748 2914
2749 $self->{need_reflow}++; 2915 $self->{need_reflow}++;
2750 $self->update; 2916 $self->update;
2751} 2917}
2752 2918
2753sub scroll_to_bottom { 2919sub scroll_to_bottom {
2754 my ($self) = @_; 2920 my ($self) = @_;
2755 2921
2756 $self->{scroll_to_bottom} = 1; 2922 $self->{scroll_to} = $#{$self->{par}};
2757 $self->update; 2923 $self->update;
2758} 2924}
2759 2925
2926sub force_uptodate {
2927 my ($self) = @_;
2928
2929 if (delete $self->{need_reflow}) {
2930 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2931
2932 my $height = 0;
2933
2934 for my $para (@{$self->{par}}) {
2935 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2936 my $layout = $self->get_layout ($para);
2937 my ($w, $h) = $layout->size;
2938
2939 $para->{w} = $w + $para->{indent};
2940 $para->{h} = $h;
2941 $para->{wrapped} = $layout->has_wrapped;
2942 }
2943
2944 $para->{y} = $height;
2945 $height += $para->{h};
2946 }
2947
2948 $self->{height} = $height;
2949 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2950
2951 delete $self->{texture};
2952 }
2953
2954 if (my $paridx = delete $self->{scroll_to}) {
2955 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
2956 }
2957}
2958
2760sub update { 2959sub update {
2761 my ($self) = @_; 2960 my ($self) = @_;
2762 2961
2763 $self->SUPER::update; 2962 $self->SUPER::update;
2764 2963
2765 return unless $self->{h} > 0; 2964 return unless $self->{h} > 0;
2766 2965
2767 delete $self->{texture}; 2966 delete $self->{texture};
2768 2967
2769 $ROOT->on_post_alloc ($self => sub { 2968 $ROOT->on_post_alloc ($self => sub {
2969 $self->force_uptodate;
2970
2770 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2971 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2771 2972
2772 if (delete $self->{need_reflow}) {
2773 my $height = 0;
2774
2775 for my $para (@{$self->{par}}) {
2776 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2777 my $layout = $self->get_layout ($para);
2778 my ($w, $h) = $layout->size;
2779
2780 $para->{w} = $w + $para->{indent};
2781 $para->{h} = $h;
2782 $para->{wrapped} = $layout->has_wrapped;
2783 }
2784
2785 $height += $para->{h};
2786 }
2787
2788 $self->{height} = $height;
2789
2790 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2791
2792 delete $self->{texture};
2793 }
2794
2795 if (delete $self->{scroll_to_bottom}) {
2796 $self->{children}[1]->set_value (1e10);
2797 }
2798
2799 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2973 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2800 glClearColor 0, 0, 0, 0; 2974 glClearColor 0, 0, 0, 0;
2801 glClear GL_COLOR_BUFFER_BIT; 2975 glClear GL_COLOR_BUFFER_BIT;
2802 2976
2977 package CFPlus::UI::Base;
2978 local ($draw_x, $draw_y, $draw_w, $draw_h) =
2979 (0, 0, $self->{w}, $self->{h});
2980
2981 my $top = int $self->{children}[1]{range}[0];
2982
2983 my $paridx = 0;
2984 my $top_paragraph;
2803 my $top = int $self->{children}[1]{range}[0]; 2985 my $top = int $self->{children}[1]{range}[0];
2804 2986
2805 my $y0 = $top; 2987 my $y0 = $top;
2806 my $y1 = $top + $H; 2988 my $y1 = $top + $H;
2807 2989
2808 my $y = 0;
2809
2810 for my $para (@{$self->{par}}) { 2990 for my $para (@{$self->{par}}) {
2811 my $h = $para->{h}; 2991 my $h = $para->{h};
2992 my $y = $para->{y};
2812 2993
2813 if ($y0 < $y + $h && $y < $y1) { 2994 if ($y0 < $y + $h && $y < $y1) {
2814
2815 my $layout = $self->get_layout ($para); 2995 my $layout = $self->get_layout ($para);
2816 2996
2817 $layout->render ($para->{indent}, $y - $y0); 2997 $layout->render ($para->{indent}, $y - $y0);
2818 2998
2819 if (my @w = @{ $para->{widget} }) { 2999 if (my @w = @{ $para->{widget} }) {
2828 $_->draw; 3008 $_->draw;
2829 } 3009 }
2830 } 3010 }
2831 } 3011 }
2832 3012
2833 $y += $h; 3013 $paridx++;
3014 $top_paragraph ||= $paridx if $y >= $top;
2834 } 3015 }
3016
3017 $self->{top_paragraph} = $top_paragraph;
2835 }; 3018 };
2836 }); 3019 });
2837} 3020}
2838 3021
2839sub reconfigure { 3022sub reconfigure {
2857 $self->{children}[1]->draw; 3040 $self->{children}[1]->draw;
2858} 3041}
2859 3042
2860############################################################################# 3043#############################################################################
2861 3044
2862package CFClient::UI::Animator; 3045package CFPlus::UI::Animator;
2863 3046
2864use CFClient::OpenGL; 3047use CFPlus::OpenGL;
2865 3048
2866our @ISA = CFClient::UI::Bin::; 3049our @ISA = CFPlus::UI::Bin::;
2867 3050
2868sub moveto { 3051sub moveto {
2869 my ($self, $x, $y) = @_; 3052 my ($self, $x, $y) = @_;
2870 3053
2871 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3054 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2899 glPopMatrix; 3082 glPopMatrix;
2900} 3083}
2901 3084
2902############################################################################# 3085#############################################################################
2903 3086
2904package CFClient::UI::Flopper; 3087package CFPlus::UI::Flopper;
2905 3088
2906our @ISA = CFClient::UI::Button::; 3089our @ISA = CFPlus::UI::Button::;
2907 3090
2908sub new { 3091sub new {
2909 my $class = shift; 3092 my $class = shift;
2910 3093
2911 my $self = $class->SUPER::new ( 3094 my $self = $class->SUPER::new (
2923 $self->{other}->toggle_visibility; 3106 $self->{other}->toggle_visibility;
2924} 3107}
2925 3108
2926############################################################################# 3109#############################################################################
2927 3110
2928package CFClient::UI::Tooltip; 3111package CFPlus::UI::Tooltip;
2929 3112
2930our @ISA = CFClient::UI::Bin::; 3113our @ISA = CFPlus::UI::Bin::;
2931 3114
2932use CFClient::OpenGL; 3115use CFPlus::OpenGL;
2933 3116
2934sub new { 3117sub new {
2935 my $class = shift; 3118 my $class = shift;
2936 3119
2937 $class->SUPER::new ( 3120 $class->SUPER::new (
2940 ) 3123 )
2941} 3124}
2942 3125
2943sub set_tooltip_from { 3126sub set_tooltip_from {
2944 my ($self, $widget) = @_; 3127 my ($self, $widget) = @_;
3128
3129 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3130 if $widget->{tooltip} =~ /^#(.*)$/;
2945 3131
2946 my $tooltip = $widget->{tooltip}; 3132 my $tooltip = $widget->{tooltip};
2947 3133
2948 if ($ENV{CFPLUS_DEBUG} & 2) { 3134 if ($ENV{CFPLUS_DEBUG} & 2) {
2949 $tooltip .= "\n\n" . (ref $widget) . "\n" 3135 $tooltip .= "\n\n" . (ref $widget) . "\n"
2953 } 3139 }
2954 3140
2955 $tooltip =~ s/^\n+//; 3141 $tooltip =~ s/^\n+//;
2956 $tooltip =~ s/\n+$//; 3142 $tooltip =~ s/\n+$//;
2957 3143
2958 $self->add (new CFClient::UI::Label 3144 $self->add (new CFPlus::UI::Label
2959 markup => $tooltip, 3145 markup => $tooltip,
2960 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3146 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2961 fontsize => 0.8, 3147 fontsize => 0.8,
2962 fg => [0, 0, 0, 1], 3148 style => 1, # FLAG_INVERSE
2963 ellipsise => 0, 3149 ellipsise => 0,
2964 font => ($widget->{tooltip_font} || $::FONT_PROP), 3150 font => ($widget->{tooltip_font} || $::FONT_PROP),
2965 ); 3151 );
2966} 3152}
2967 3153
2986 3172
2987 $self->{root}->on_post_alloc ("move_$self" => sub { 3173 $self->{root}->on_post_alloc ("move_$self" => sub {
2988 my $widget = $self->{owner} 3174 my $widget = $self->{owner}
2989 or return; 3175 or return;
2990 3176
3177 if ($widget->{visible}) {
2991 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3178 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2992 3179
2993 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3180 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2994 if $x + $self->{w} > $self->{root}{w}; 3181 if $x + $self->{w} > $self->{root}{w};
2995 3182
2996 $self->move_abs ($x, $y); 3183 $self->move_abs ($x, $y);
3184 } else {
3185 $self->hide;
3186 }
2997 }); 3187 });
2998} 3188}
2999 3189
3000sub _draw { 3190sub _draw {
3001 my ($self) = @_; 3191 my ($self) = @_;
3025 $self->SUPER::_draw; 3215 $self->SUPER::_draw;
3026} 3216}
3027 3217
3028############################################################################# 3218#############################################################################
3029 3219
3030package CFClient::UI::Face; 3220package CFPlus::UI::Face;
3031 3221
3032our @ISA = CFClient::UI::Base::; 3222our @ISA = CFPlus::UI::DrawBG::;
3033 3223
3034use CFClient::OpenGL; 3224use CFPlus::OpenGL;
3035 3225
3036sub new { 3226sub new {
3037 my $class = shift; 3227 my $class = shift;
3038 3228
3039 my $self = $class->SUPER::new ( 3229 my $self = $class->SUPER::new (
3074sub _draw { 3264sub _draw {
3075 my ($self) = @_; 3265 my ($self) = @_;
3076 3266
3077 return unless $::CONN; 3267 return unless $::CONN;
3078 3268
3269 $self->SUPER::_draw;
3270
3079 my $face; 3271 my $face;
3080 3272
3081 if ($self->{frame}) { 3273 if ($self->{frame}) {
3082 my $anim = $::CONN->{anim}[$self->{anim}]; 3274 my $anim = $::CONN->{anim}[$self->{anim}];
3083 3275
3105 $self->SUPER::destroy; 3297 $self->SUPER::destroy;
3106} 3298}
3107 3299
3108############################################################################# 3300#############################################################################
3109 3301
3110package CFClient::UI::Buttonbar; 3302package CFPlus::UI::Buttonbar;
3111 3303
3112our @ISA = CFClient::UI::HBox::; 3304our @ISA = CFPlus::UI::HBox::;
3113 3305
3114# TODO: should actualyl wrap buttons and other goodies. 3306# TODO: should actually wrap buttons and other goodies.
3115 3307
3116############################################################################# 3308#############################################################################
3117 3309
3118package CFClient::UI::Menu; 3310package CFPlus::UI::Menu;
3119 3311
3120our @ISA = CFClient::UI::FancyFrame::; 3312our @ISA = CFPlus::UI::Toplevel::;
3121 3313
3122use CFClient::OpenGL; 3314use CFPlus::OpenGL;
3123 3315
3124sub new { 3316sub new {
3125 my $class = shift; 3317 my $class = shift;
3126 3318
3127 my $self = $class->SUPER::new ( 3319 my $self = $class->SUPER::new (
3128 items => [], 3320 items => [],
3129 z => 100, 3321 z => 100,
3130 @_, 3322 @_,
3131 ); 3323 );
3132 3324
3133 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3325 $self->add ($self->{vbox} = new CFPlus::UI::VBox);
3134 3326
3135 for my $item (@{ $self->{items} }) { 3327 for my $item (@{ $self->{items} }) {
3136 my ($widget, $cb, $tooltip) = @$item; 3328 my ($widget, $cb, $tooltip) = @$item;
3137 3329
3138 # handle various types of items, only text for now 3330 # handle various types of items, only text for now
3139 if (!ref $widget) { 3331 if (!ref $widget) {
3140 if ($widget =~ /\t/) { 3332 if ($widget =~ /\t/) {
3141 my ($left, $right) = split /\t/, $widget, 2; 3333 my ($left, $right) = split /\t/, $widget, 2;
3142 3334
3143 $widget = new CFClient::UI::HBox 3335 $widget = new CFPlus::UI::HBox
3144 can_hover => 1, 3336 can_hover => 1,
3145 can_events => 1, 3337 can_events => 1,
3146 tooltip => $tooltip, 3338 tooltip => $tooltip,
3147 children => [ 3339 children => [
3148 (new CFClient::UI::Label markup => $left, expand => 1), 3340 (new CFPlus::UI::Label markup => $left, expand => 1),
3149 (new CFClient::UI::Label markup => $right, align => +1), 3341 (new CFPlus::UI::Label markup => $right, align => +1),
3150 ], 3342 ],
3151 ; 3343 ;
3152 3344
3153 } else { 3345 } else {
3154 $widget = new CFClient::UI::Label 3346 $widget = new CFPlus::UI::Label
3155 can_hover => 1, 3347 can_hover => 1,
3156 can_events => 1, 3348 can_events => 1,
3157 markup => $widget, 3349 markup => $widget,
3158 tooltip => $tooltip; 3350 tooltip => $tooltip;
3159 } 3351 }
3207 1 3399 1
3208} 3400}
3209 3401
3210############################################################################# 3402#############################################################################
3211 3403
3212package CFClient::UI::Multiplexer; 3404package CFPlus::UI::Multiplexer;
3213 3405
3214our @ISA = CFClient::UI::Container::; 3406our @ISA = CFPlus::UI::Container::;
3215 3407
3216sub new { 3408sub new {
3217 my $class = shift; 3409 my $class = shift;
3218 3410
3219 my $self = $class->SUPER::new ( 3411 my $self = $class->SUPER::new (
3280 $self->{current}->draw; 3472 $self->{current}->draw;
3281} 3473}
3282 3474
3283############################################################################# 3475#############################################################################
3284 3476
3285package CFClient::UI::Notebook; 3477package CFPlus::UI::Notebook;
3286 3478
3287our @ISA = CFClient::UI::VBox::; 3479our @ISA = CFPlus::UI::VBox::;
3288 3480
3289sub new { 3481sub new {
3290 my $class = shift; 3482 my $class = shift;
3291 3483
3292 my $self = $class->SUPER::new ( 3484 my $self = $class->SUPER::new (
3293 buttonbar => (new CFClient::UI::Buttonbar), 3485 buttonbar => (new CFPlus::UI::Buttonbar),
3294 multiplexer => (new CFClient::UI::Multiplexer expand => 1), 3486 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3295 # filter => # will be put between multiplexer and $self 3487 # filter => # will be put between multiplexer and $self
3296 @_, 3488 @_,
3297 ); 3489 );
3298 3490
3299 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3491 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3305sub add { 3497sub add {
3306 my ($self, $title, $widget, $tooltip) = @_; 3498 my ($self, $title, $widget, $tooltip) = @_;
3307 3499
3308 Scalar::Util::weaken $self; 3500 Scalar::Util::weaken $self;
3309 3501
3310 $self->{buttonbar}->add (new CFClient::UI::Button 3502 $self->{buttonbar}->add (new CFPlus::UI::Button
3311 markup => $title, 3503 markup => $title,
3312 tooltip => $tooltip, 3504 tooltip => $tooltip,
3313 on_activate => sub { $self->set_current_page ($widget) }, 3505 on_activate => sub { $self->set_current_page ($widget) },
3314 ); 3506 );
3315 3507
3329 $self->emit (page_changed => $self->{multiplexer}{current}); 3521 $self->emit (page_changed => $self->{multiplexer}{current});
3330} 3522}
3331 3523
3332############################################################################# 3524#############################################################################
3333 3525
3334package CFClient::UI::Combobox; 3526package CFPlus::UI::Selector;
3335 3527
3336use utf8; 3528use utf8;
3337 3529
3338our @ISA = CFClient::UI::Button::; 3530our @ISA = CFPlus::UI::Button::;
3339 3531
3340sub new { 3532sub new {
3341 my $class = shift; 3533 my $class = shift;
3342 3534
3343 my $self = $class->SUPER::new ( 3535 my $self = $class->SUPER::new (
3360 my ($value, $title, $tooltip) = @$_; 3552 my ($value, $title, $tooltip) = @$_;
3361 3553
3362 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 3554 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3363 } 3555 }
3364 3556
3365 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 3557 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
3366} 3558}
3367 3559
3368sub _set_value { 3560sub _set_value {
3369 my ($self, $value) = @_; 3561 my ($self, $value) = @_;
3370 3562
3385 $self->emit (changed => $value); 3577 $self->emit (changed => $value);
3386} 3578}
3387 3579
3388############################################################################# 3580#############################################################################
3389 3581
3390package CFClient::UI::Statusbox; 3582package CFPlus::UI::Statusbox;
3391 3583
3392our @ISA = CFClient::UI::VBox::; 3584our @ISA = CFPlus::UI::VBox::;
3393 3585
3394sub new { 3586sub new {
3395 my $class = shift; 3587 my $class = shift;
3396 3588
3397 my $self = $class->SUPER::new ( 3589 my $self = $class->SUPER::new (
3409sub reorder { 3601sub reorder {
3410 my ($self) = @_; 3602 my ($self) = @_;
3411 my $NOW = Time::HiRes::time; 3603 my $NOW = Time::HiRes::time;
3412 3604
3413 # freeze display when hovering over any label 3605 # freeze display when hovering over any label
3414 return if $CFClient::UI::TOOLTIP->{owner} 3606 return if $CFPlus::UI::TOOLTIP->{owner}
3415 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label}, 3607 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label},
3416 values %{ $self->{item} }; 3608 values %{ $self->{item} };
3417 3609
3418 while (my ($k, $v) = each %{ $self->{item} }) { 3610 while (my ($k, $v) = each %{ $self->{item} }) {
3419 delete $self->{item}{$k} if $v->{timeout} < $NOW; 3611 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3420 } 3612 }
3441 for ($short) { 3633 for ($short) {
3442 s/^\s+//; 3634 s/^\s+//;
3443 s/\s+/ /g; 3635 s/\s+/ /g;
3444 } 3636 }
3445 3637
3446 new CFClient::UI::Label 3638 new CFPlus::UI::Label
3447 markup => $short, 3639 markup => $short,
3448 tooltip => $item->{tooltip}, 3640 tooltip => $item->{tooltip},
3449 tooltip_font => $::FONT_PROP, 3641 tooltip_font => $::FONT_PROP,
3450 tooltip_width => 0.67, 3642 tooltip_width => 0.67,
3451 fontsize => $item->{fontsize} || $self->{fontsize}, 3643 fontsize => $item->{fontsize} || $self->{fontsize},
3530 $self->SUPER::destroy; 3722 $self->SUPER::destroy;
3531} 3723}
3532 3724
3533############################################################################# 3725#############################################################################
3534 3726
3535package CFClient::UI::Inventory; 3727package CFPlus::UI::Inventory;
3536 3728
3537our @ISA = CFClient::UI::Table::; 3729our @ISA = CFPlus::UI::Table::;
3538 3730
3539sub new { 3731sub new {
3540 my $class = shift; 3732 my $class = shift;
3541 3733
3542 my $self = $class->SUPER::new ( 3734 my $self = $class->SUPER::new (
3543 col_expand => [0, 1, 0], 3735 col_expand => [0, 1, 0],
3736 items => [],
3544 @_, 3737 @_,
3545 ); 3738 );
3546 3739
3740 $self->set_sort_order (undef);
3741
3547 $self 3742 $self
3743}
3744
3745sub update_items {
3746 my ($self) = @_;
3747
3748 $self->clear;
3749
3750 my @item = $self->{sort}->(@{ $self->{items} });
3751
3752 my @adds;
3753 my $row = 0;
3754 for my $item ($self->{sort}->(@{ $self->{items} })) {
3755 CFPlus::Item::update_widgets $item;
3756
3757 push @adds, 0, $row, $item->{face_widget};
3758 push @adds, 1, $row, $item->{desc_widget};
3759 push @adds, 2, $row, $item->{weight_widget};
3760
3761 $row++;
3762 }
3763
3764 $self->add (@adds);
3765}
3766
3767sub set_sort_order {
3768 my ($self, $order) = @_;
3769
3770 $self->{sort} = $order ||= sub {
3771 sort {
3772 $a->{type} <=> $b->{type}
3773 or $a->{name} cmp $b->{name}
3774 } @_
3775 };
3776
3777 $self->update_items;
3548} 3778}
3549 3779
3550sub set_items { 3780sub set_items {
3551 my ($self, $items) = @_; 3781 my ($self, $items) = @_;
3552 3782
3553 $self->clear; 3783 $self->{items} = [$items ? values %$items : ()];
3554 return unless $items; 3784 $self->update_items;
3555
3556 my @items = sort {
3557 ($a->{type} <=> $b->{type})
3558 or ($a->{name} cmp $b->{name})
3559 } values %$items;
3560
3561 $self->{real_items} = \@items;
3562
3563 my $row = 0;
3564 for my $item (@items) {
3565 CFClient::Item::update_widgets $item;
3566
3567 $self->add (0, $row, $item->{face_widget});
3568 $self->add (1, $row, $item->{desc_widget});
3569 $self->add (2, $row, $item->{weight_widget});
3570
3571 $row++;
3572 }
3573} 3785}
3574 3786
3575############################################################################# 3787#############################################################################
3576 3788
3577package CFClient::UI::SpellList; 3789package CFPlus::UI::SpellList;
3578 3790
3579our @ISA = CFClient::UI::Table::; 3791our @ISA = CFPlus::UI::Table::;
3580 3792
3581sub new { 3793sub new {
3582 my $class = shift; 3794 my $class = shift;
3583 3795
3584 my $self = $class->SUPER::new ( 3796 my $self = $class->SUPER::new (
3602 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL"); 3814 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3603 3815
3604sub rebuild_spell_list { 3816sub rebuild_spell_list {
3605 my ($self) = @_; 3817 my ($self) = @_;
3606 3818
3607 $CFClient::UI::ROOT->on_refresh ($self => sub { 3819 $CFPlus::UI::ROOT->on_refresh ($self => sub {
3608 $self->clear; 3820 $self->clear;
3609 3821
3610 return unless $::CONN; 3822 return unless $::CONN;
3611 3823
3612 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME); 3824 $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3613 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL); 3825 $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL);
3614 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL); 3826 $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3615 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP); 3827 $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3616 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG); 3828 $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3617 3829
3618 my $row = 0; 3830 my $row = 0;
3619 3831
3620 for (sort { $a cmp $b } keys %{ $self->{spell} }) { 3832 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3621 my $spell = $self->{spell}{$_}; 3833 my $spell = $self->{spell}{$_};
3628 if ($ev->{button} == 1) { 3840 if ($ev->{button} == 1) {
3629 $::CONN->user_send ("cast $spell->{name}"); 3841 $::CONN->user_send ("cast $spell->{name}");
3630 } elsif ($ev->{button} == 2) { 3842 } elsif ($ev->{button} == 2) {
3631 $::CONN->user_send ("invoke $spell->{name}"); 3843 $::CONN->user_send ("invoke $spell->{name}");
3632 } elsif ($ev->{button} == 3) { 3844 } elsif ($ev->{button} == 3) {
3633 (new CFClient::UI::Menu 3845 (new CFPlus::UI::Menu
3634 items => [ 3846 items => [
3635 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], 3847 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3636 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], 3848 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3637 ], 3849 ],
3638 )->popup ($ev); 3850 )->popup ($ev);
3641 } 3853 }
3642 3854
3643 1 3855 1
3644 }; 3856 };
3645 3857
3646 my $tooltip = "$spell->{message}$TOOLTIP_ALL"; 3858 my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL;
3647 3859
3648 #TODO: add path info to tooltip 3860 #TODO: add path info to tooltip
3649 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path}); 3861 #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path});
3650 3862
3651 $self->add (0, $row, new CFClient::UI::Face 3863 $self->add (0, $row, new CFPlus::UI::Face
3652 face => $spell->{face}, 3864 face => $spell->{face},
3653 can_hover => 1, 3865 can_hover => 1,
3654 can_events => 1, 3866 can_events => 1,
3655 tooltip => $tooltip, 3867 tooltip => $tooltip,
3656 on_button_down => $spell_cb, 3868 on_button_down => $spell_cb,
3657 ); 3869 );
3658 3870
3659 $self->add (1, $row, new CFClient::UI::Label 3871 $self->add (1, $row, new CFPlus::UI::Label
3660 expand => 1, 3872 expand => 1,
3661 text => $spell->{name}, 3873 text => $spell->{name},
3662 can_hover => 1, 3874 can_hover => 1,
3663 can_events => 1, 3875 can_events => 1,
3664 tooltip => $tooltip, 3876 tooltip => $tooltip,
3665 on_button_down => $spell_cb, 3877 on_button_down => $spell_cb,
3666 ); 3878 );
3667 3879
3668 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL); 3880 $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3669 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL); 3881 $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3670 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); 3882 $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3671 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); 3883 $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3672 } 3884 }
3673 }); 3885 });
3674} 3886}
3675 3887
3676sub add_spell { 3888sub add_spell {
3694 $self->rebuild_spell_list; 3906 $self->rebuild_spell_list;
3695} 3907}
3696 3908
3697############################################################################# 3909#############################################################################
3698 3910
3699package CFClient::UI::Root; 3911package CFPlus::UI::Root;
3700 3912
3701our @ISA = CFClient::UI::Container::; 3913our @ISA = CFPlus::UI::Container::;
3702 3914
3703use List::Util qw(min max); 3915use List::Util qw(min max);
3704 3916
3705use CFClient::OpenGL; 3917use CFPlus::OpenGL;
3706 3918
3707sub new { 3919sub new {
3708 my $class = shift; 3920 my $class = shift;
3709 3921
3710 my $self = $class->SUPER::new ( 3922 my $self = $class->SUPER::new (
3889 $h = 0 if $h < 0; 4101 $h = 0 if $h < 0;
3890 4102
3891 $w = max $widget->{min_w}, $w; 4103 $w = max $widget->{min_w}, $w;
3892 $h = max $widget->{min_h}, $h; 4104 $h = max $widget->{min_h}, $h;
3893 4105
4106# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4107# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4108
3894 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4109 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3895 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4110 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3896 4111
3897 $w = int $w + 0.5; 4112 $w = int $w + 0.5;
3898 $h = int $h + 0.5; 4113 $h = int $h + 0.5;
3924 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4139 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3925 glMatrixMode GL_MODELVIEW; 4140 glMatrixMode GL_MODELVIEW;
3926 glLoadIdentity; 4141 glLoadIdentity;
3927 4142
3928 { 4143 {
3929 package CFClient::UI::Base; 4144 package CFPlus::UI::Base;
3930 4145
3931 ($draw_x, $draw_y, $draw_w, $draw_h) = 4146 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3932 (0, 0, $self->{w}, $self->{h}); 4147 (0, 0, $self->{w}, $self->{h});
3933 }
3934 4148
3935 $self->_draw; 4149 $self->_draw;
4150 }
3936} 4151}
3937 4152
3938############################################################################# 4153#############################################################################
3939 4154
3940package CFClient::UI; 4155package CFPlus::UI;
3941 4156
3942$ROOT = new CFClient::UI::Root; 4157$ROOT = new CFPlus::UI::Root;
3943$TOOLTIP = new CFClient::UI::Tooltip z => 900; 4158$TOOLTIP = new CFPlus::UI::Tooltip z => 900;
3944 4159
39451 41601
3946 4161

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines