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.175 by root, Tue Apr 25 11:48:31 2006 UTC vs.
Revision 1.198 by root, Fri May 12 02:08:52 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
23 24
24 my $tip = $widget->{tooltip}; 25 my $tip = $widget->{tooltip};
25 26
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 27 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 28
28 $TOOLTIP->set_markup ($widget->{tooltip}); 29 $TOOLTIP->set_tooltip_from ($widget);
29
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31 31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) { 34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 } 36 }
37 37
38 $TOOLTIP->move ($x, $y); 38 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size;
40 $TOOLTIP->update;
39 } 41 }
40 42
41 return; 43 return;
42 } 44 }
43 } 45 }
232 $self->{y} = $y; 234 $self->{y} = $y;
233 $self->update; 235 $self->update;
234 } 236 }
235 237
236 if ($self->{w} != $w || $self->{h} != $h) { 238 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 239 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 } 240 }
243} 241}
244 242
245sub size_allocate { 243sub size_allocate {
246 # nothing to be done 244 # nothing to be done
247} 245}
248 246
249sub children { 247sub children {
250} 248}
251 249
252# call when resoltuion changes etc. 250# call when resolution changes etc.
253sub reconfigure { 251sub reconfigure {
254 my ($self) = @_; 252 my ($self) = @_;
255 253
256 $_->reconfigure 254 $_->reconfigure
257 for $self->children; 255 for $self->children;
258 256
259 $_->check_size; 257 $self->check_size (1);
258 $self->update;
260} 259}
261 260
262sub set_max_size { 261sub set_max_size {
263 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
264 263
265 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
266 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 delete $self->{max_h}; $self->{max_h} = $h if $h;
267}
268
269# return top left coordinates
270sub _topleft {
271 my ($self, $x, $y) = @_;
272
273 $self->{parent}
274 or Carp::confess "no parent widget in _topleft\n";#d#
275
276 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
277} 266}
278 267
279# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
280sub coord2local { 269sub coord2local {
281 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
282 271
283 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
284 ($x - $X, $y - $Y)
285} 273}
286 274
287# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
288sub coord2global { 276sub coord2global {
289 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
290 278
291 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
292 ($x + $X, $y + $Y)
293} 280}
294 281
295sub focus_in { 282sub focus_in {
296 my ($self) = @_; 283 my ($self) = @_;
297 284
395 382
396sub set_parent { 383sub set_parent {
397 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
398 385
399 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388
389 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size
391 unless exists $self->{req_w};
400} 392}
401 393
402sub check_size { 394sub check_size {
403 my ($self) = @_; 395 my ($self, $forced) = @_;
404 396
405 $self->{parent} 397 $self->{force_alloc} = 1 if $forced;
406 or return 1; 398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
407
408 my ($w, $h) = $self->{user_w} && $self->{user_h}
409 ? @$self{qw(user_w user_h)}
410 : $self->size_request;
411
412 if ($w != $self->{req_w} || $h != $self->{req_h}) {
413 $self->{req_w} = $w;
414 $self->{req_h} = $h;
415
416 $self->{parent}->check_size
417 or $self->size_allocate (
418 (List::Util::max $self->{w}, $w),
419 (List::Util::max $self->{h}, $h),
420 );
421
422 1
423 } else {
424 0
425 }
426} 399}
427 400
428sub update { 401sub update {
429 my ($self) = @_; 402 my ($self) = @_;
430 403
527 500
528 $self 501 $self
529} 502}
530 503
531sub add { 504sub add {
532 my ($self, $child) = @_; 505 my ($self, @widgets) = @_;
533 506
534 $child->set_parent ($self); 507 $_->set_parent ($self)
508 for @widgets;
535 509
536 use sort 'stable'; 510 use sort 'stable';
537 511
538 $self->{children} = [ 512 $self->{children} = [
539 sort { $a->{z} <=> $b->{z} } 513 sort { $a->{z} <=> $b->{z} }
540 @{$self->{children}}, $child 514 @{$self->{children}}, @widgets
541 ]; 515 ];
542 516
543 $child->check_size; 517 $self->check_size (1);
518 $self->update;
544} 519}
545 520
546sub children { 521sub children {
547 @{ $_[0]{children} } 522 @{ $_[0]{children} }
548} 523}
567 542
568 for (@$children) { 543 for (@$children) {
569 delete $_->{parent}; 544 delete $_->{parent};
570 $_->hide; 545 $_->hide;
571 } 546 }
547
548 $self->check_size;
549 $self->update;
572} 550}
573 551
574sub find_widget { 552sub find_widget {
575 my ($self, $x, $y) = @_; 553 my ($self, $x, $y) = @_;
576 554
651} 629}
652 630
653sub update { 631sub update {
654 my ($self) = @_; 632 my ($self) = @_;
655 633
656 $ROOT->on_refresh ($self => sub { $self->render_child }); 634 $ROOT->on_post_alloc ($self => sub { $self->render_child });
657 $self->SUPER::update; 635 $self->SUPER::update;
658} 636}
659 637
660sub size_allocate { 638sub size_allocate {
661 my ($self, $w, $h) = @_; 639 my ($self, $w, $h) = @_;
662 640
663 $self->SUPER::size_allocate ($w, $h); 641 $self->SUPER::size_allocate ($w, $h);
664 $self->update; 642 $self->update;
643}
644
645sub _render {
646 $_[0]{children}[0]->draw;
665} 647}
666 648
667sub render_child { 649sub render_child {
668 my ($self) = @_; 650 my ($self) = @_;
669 651
670 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 652 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
671 glClearColor 0, 0, 0, 0; 653 glClearColor 0, 0, 0, 0;
672 glClear GL_COLOR_BUFFER_BIT; 654 glClear GL_COLOR_BUFFER_BIT;
673 $self->child->draw; 655
656 $self->_render;
674# glColorMask 1, 1, 1, 0; 657# glColorMask 1, 1, 1, 0;
675# glEnable GL_BLEND; 658# glEnable GL_BLEND;
676# glBlendFunc GL_SRC_ALPHA, GL_ZERO; 659# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
677# glRasterPos 0, 0; 660# glRasterPos 0, 0;
678# glCopyPixels 0, 0, $self->{w}, $self->{h}; 661# glCopyPixels 0, 0, $self->{w}, $self->{h};
687 my ($w, $h) = ($self->w, $self->h); 670 my ($w, $h) = ($self->w, $self->h);
688 671
689 my $tex = $self->{texture} 672 my $tex = $self->{texture}
690 or return; 673 or return;
691 674
692 glEnable GL_BLEND;
693 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
694 glEnable GL_TEXTURE_2D; 675 glEnable GL_TEXTURE_2D;
695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
696 glColor 0, 0, 0, 1; 677 glColor 0, 0, 0, 1;
697 678
698 $tex->draw_quad (0, 0, $w, $h); 679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
699 680
700 glDisable GL_BLEND;
701 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
702} 682}
703 683
704############################################################################# 684#############################################################################
705 685
706package CFClient::UI::ViewPort; 686package CFClient::UI::ViewPort;
707 687
708our @ISA = CFClient::UI::Window::; 688our @ISA = CFClient::UI::Window::;
709 689
710sub new { die }
711
712sub size_request { 690sub size_request {
713 my ($self) = @_; 691 my ($self) = @_;
714 692
715 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 693 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
716 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 694 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
717 695
718 @$self{qw(child_w child_h)} 696 @$self{qw(child_w child_h)}
719} 697}
720 698
721sub _draw { 699sub size_allocate {
722 my ($self) = @_; 700 my ($self, $w, $h) = @_;
723 701
724 $self->{children}[1]->draw; 702 $self->update;
725} 703}
726 704
705sub set_offset {
706 my ($self, $x, $y) = @_;
727 707
728############################################################################# 708 $self->{view_x} = int $x;
709 $self->{view_y} = int $y;
729 710
711 $self->update;
712}
713
714# hmm, this does not work for topleft of $self... but we should not ask for that
715sub coord2local {
716 my ($self, $x, $y) = @_;
717
718 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
719}
720
721sub coord2global {
722 my ($self, $x, $y) = @_;
723
724 $x = List::Util::min $self->{w}, $x - $self->{view_x};
725 $y = List::Util::min $self->{h}, $y - $self->{view_y};
726
727 $self->SUPER::coord2global ($x, $y)
728}
729
730sub find_widget {
731 my ($self, $x, $y) = @_;
732
733 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
734 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
735 ) {
736 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
737 } else {
738 $self->CFClient::UI::Base::find_widget ($x, $y)
739 }
740}
741
742sub _render {
743 my ($self) = @_;
744
745 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
746
747 $self->SUPER::_render;
748}
749
750#############################################################################
751
730package CFClient::UI::Frame; 752package CFClient::UI::ScrolledWindow;
731 753
732our @ISA = CFClient::UI::Bin::; 754our @ISA = CFClient::UI::HBox::;
733
734use CFClient::OpenGL;
735 755
736sub new { 756sub new {
737 my $class = shift; 757 my $class = shift;
738 758
759 my $self;
760
761 my $slider = new CFClient::UI::Slider
762 vertical => 1,
763 range => [0, 0, 1, 0.01], # HACK fix
764 connect_changed => sub {
765 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
766 },
767 ;
768
739 my $self = $class->SUPER::new ( 769 $self = $class->SUPER::new (
740 bg => [1, 1, 1, 1], 770 vp => (new CFClient::UI::ViewPort),
741 border_bg => [1, 1, 1, 1], 771 slider => $slider,
742 border => 0.8,
743 @_ 772 @_,
744 ); 773 );
745 774
775 $self->{vp}->add ($self->{scrolled});
776 $self->add ($self->{vp});
777 $self->add ($self->{slider});
778
746 $self 779 $self
747} 780}
748 781
749sub _draw { 782#TODO# update range on size_allocate depeneing on child
750 my ($self) = @_; 783# update viewport offset on scroll
751 784
752 my ($w, $h) = ($self->{w}, $self->{h}); 785#############################################################################
753 786
754 glEnable GL_BLEND; 787package CFClient::UI::Frame;
755 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
756 glEnable GL_TEXTURE_2D;
757 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
758 788
759# glBegin GL_QUADS; 789our @ISA = CFClient::UI::Bin::;
760# glColor 0, 0, 0, 0;
761# glVertex 0 , 0;
762# glVertex 0 , $h;
763# glVertex $w, $h;
764# glVertex $w, 0;
765# glEnd;
766 790
767 791use CFClient::OpenGL;
768 $self->child->draw;
769 glDisable GL_BLEND;
770 glDisable GL_TEXTURE_2D;
771}
772 792
773############################################################################# 793#############################################################################
774 794
775package CFClient::UI::FancyFrame; 795package CFClient::UI::FancyFrame;
776 796
788 # TODO: user_x, user_y, overwrite moveto? 808 # TODO: user_x, user_y, overwrite moveto?
789 809
790 my $self = $class->SUPER::new ( 810 my $self = $class->SUPER::new (
791 bg => [1, 1, 1, 1], 811 bg => [1, 1, 1, 1],
792 border_bg => [1, 1, 1, 1], 812 border_bg => [1, 1, 1, 1],
793 border => 0.8, 813 border => 0.6,
794 can_events => 1, 814 can_events => 1,
795 @_ 815 @_
796 ); 816 );
797 817
798 $self->{title} &&= new CFClient::UI::Label 818 $self->{title} &&= new CFClient::UI::Label
799 align => 0, 819 align => 0,
800 valign => 1, 820 valign => 1,
801 text => $self->{title}, 821 text => $self->{title},
802 fontsize => 1; 822 fontsize => $self->{border};
803 823
804 $self 824 $self
805} 825}
806 826
807sub border { 827sub border {
832} 852}
833 853
834sub button_down { 854sub button_down {
835 my ($self, $ev, $x, $y) = @_; 855 my ($self, $ev, $x, $y) = @_;
836 856
857 my ($w, $h) = @$self{qw(w h)};
837 my $border = $self->border; 858 my $border = $self->border;
838 859
839 if ($x < $self->{w} && $x >= $self->{w} - $border 860 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
840 && $y < $self->{h} && $y >= $self->{h} - $border) { 861 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
841 862
863 if ($lr & $td) {
864 my ($wx, $wy) = ($self->{x}, $self->{y});
842 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 865 my ($ox, $oy) = ($ev->{x}, $ev->{y});
843 my ($bw, $bh) = ($self->{w}, $self->{h}); 866 my ($bw, $bh) = ($self->{w}, $self->{h});
844 867
868 my $mx = $x < $border;
869 my $my = $y < $border;
870
845 $self->{motion} = sub { 871 $self->{motion} = sub {
846 my ($ev, $x, $y) = @_; 872 my ($ev, $x, $y) = @_;
847 873
848 ($x, $y) = ($ev->{x}, $ev->{y}); 874 my $dx = $ev->{x} - $ox;
875 my $dy = $ev->{y} - $oy;
849 876
850 $self->{user_w} = $bw + $x - $ox; 877 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
851 $self->{user_h} = $bh + $y - $oy; 878 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
879 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
852 $self->check_size; 880 $self->check_size;
853 }; 881 };
854 882
855 } elsif ($x >= 0 && $x < $self->{w} 883 } elsif ($lr ^ $td) {
856 && $y >= 0 && $y < $border) {
857
858 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 884 my ($ox, $oy) = ($ev->{x}, $ev->{y});
859 my ($bx, $by) = ($self->{x}, $self->{y}); 885 my ($bx, $by) = ($self->{x}, $self->{y});
860 886
861 $self->{motion} = sub { 887 $self->{motion} = sub {
862 my ($ev, $x, $y) = @_; 888 my ($ev, $x, $y) = @_;
885 my ($self) = @_; 911 my ($self) = @_;
886 912
887 my ($w, $h ) = ($self->{w}, $self->{h}); 913 my ($w, $h ) = ($self->{w}, $self->{h});
888 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 914 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
889 915
890 glEnable GL_BLEND;
891 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
892 glEnable GL_TEXTURE_2D; 916 glEnable GL_TEXTURE_2D;
893 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
894 918
895 my $border = $self->border; 919 my $border = $self->border;
896 920
897 glColor @{ $self->{border_bg} }; 921 glColor @{ $self->{border_bg} };
898 $tex[1]->draw_quad (0, 0, $w, $border); 922 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
899 $tex[3]->draw_quad (0, $border, $border, $ch); 923 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
900 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 924 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
901 $tex[4]->draw_quad (0, $h - $border, $w, $border); 925 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
902 926
927 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
903 my $bg = $tex[0]; 928 my $bg = $tex[0];
904 929
905 # TODO: repeat texture not scale 930 # TODO: repeat texture not scale
906 my $rep_x = $cw / $bg->{w}; 931 my $rep_x = $cw / $bg->{w};
907 my $rep_y = $ch / $bg->{h}; 932 my $rep_y = $ch / $bg->{h};
908 933
909 glColor @{ $self->{bg} }; 934 glColor @{ $self->{bg} };
910 935
911 $bg->{s} = $rep_x; 936 $bg->{s} = $rep_x;
912 $bg->{t} = $rep_y; 937 $bg->{t} = $rep_y;
913 $bg->{wrap_mode} = 1; 938 $bg->{wrap_mode} = 1;
914 $bg->draw_quad ($border, $border, $cw, $ch); 939 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
940 }
915 941
916 glDisable GL_TEXTURE_2D; 942 glDisable GL_TEXTURE_2D;
917 glDisable GL_BLEND;
918 943
919 $self->{title}->draw if $self->{title}; 944 $self->{title}->draw if $self->{title};
945
920 $self->child->draw; 946 $self->child->draw;
921} 947}
922 948
923############################################################################# 949#############################################################################
924 950
1148} 1174}
1149 1175
1150sub size_allocate { 1176sub size_allocate {
1151 my ($self, $w, $h) = @_; 1177 my ($self, $w, $h) = @_;
1152 1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1153 my $children = $self->{children}; 1181 my $children = $self->{children};
1154 1182
1155 my @h = map $_->{req_h}, @$children; 1183 my @h = map $_->{req_h}, @$children;
1156 1184
1157 my $req_h = List::Util::sum @h; 1185 my $req_h = List::Util::sum @h;
1196 my ($class, %arg) = @_; 1224 my ($class, %arg) = @_;
1197 1225
1198 my $self = $class->SUPER::new ( 1226 my $self = $class->SUPER::new (
1199 fg => [1, 1, 1], 1227 fg => [1, 1, 1],
1200 #font => default_font 1228 #font => default_font
1229 #text => initial text
1230 #markup => initial narkup
1231 layout => (new CFClient::Layout),
1201 fontsize => 1, 1232 fontsize => 1,
1202 text => "",
1203 align => -1, 1233 align => -1,
1204 valign => -1, 1234 valign => -1,
1205 padding => 2, 1235 padding => 2,
1206 layout => new CFClient::Layout,
1207 can_events => 0, 1236 can_events => 0,
1208 %arg 1237 %arg
1209 ); 1238 );
1210 1239
1211 if (exists $self->{template}) { 1240 if (exists $self->{template}) {
1212 my $layout = new CFClient::Layout; 1241 my $layout = new CFClient::Layout;
1213 $layout->set_text (delete $self->{template}); 1242 $layout->set_text (delete $self->{template});
1214 $self->{template} = $layout; 1243 $self->{template} = $layout;
1215 } 1244 }
1216 1245
1217 $self->set_text (delete $self->{text}) if exists $self->{text}; 1246 if (exists $self->{markup}) {
1218 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1247 $self->set_markup (delete $self->{markup});
1248 } else {
1249 $self->set_text (delete $self->{text});
1250 }
1219 1251
1220 $self 1252 $self
1221} 1253}
1222 1254
1223sub escape { 1255sub escape {
1235 1267
1236 delete $self->{texture}; 1268 delete $self->{texture};
1237 $self->SUPER::update; 1269 $self->SUPER::update;
1238} 1270}
1239 1271
1240sub reconfigure {
1241 my ($self) = @_;
1242
1243 delete $self->{texture};
1244}
1245
1246sub set_text { 1272sub set_text {
1247 my ($self, $text) = @_; 1273 my ($self, $text) = @_;
1248 1274
1249 return if $self->{text} eq "T$text"; 1275 return if $self->{text} eq "T$text";
1250 $self->{text} = "T$text"; 1276 $self->{text} = "T$text";
1251 1277
1278 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1252 $self->{layout}->set_text ($text); 1279 $self->{layout}->set_text ($text);
1253 1280
1254 delete $self->{texture};
1255 $self->update; 1281 $self->update;
1256 $self->check_size; 1282 $self->check_size;
1257} 1283}
1258 1284
1259sub set_markup { 1285sub set_markup {
1260 my ($self, $markup) = @_; 1286 my ($self, $markup) = @_;
1261 1287
1262 return if $self->{text} eq "M$markup"; 1288 return if $self->{text} eq "M$markup";
1263 $self->{text} = "M$markup"; 1289 $self->{text} = "M$markup";
1264 1290
1291 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1292
1293 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1265 $self->{layout}->set_markup ($markup); 1294 $self->{layout}->set_markup ($markup);
1266 1295
1267 delete $self->{texture};
1268 $self->update; 1296 $self->update;
1269 $self->check_size; 1297 $self->check_size;
1270} 1298}
1271 1299
1272sub size_request { 1300sub size_request {
1303sub set_fontsize { 1331sub set_fontsize {
1304 my ($self, $fontsize) = @_; 1332 my ($self, $fontsize) = @_;
1305 1333
1306 $self->{fontsize} = $fontsize; 1334 $self->{fontsize} = $fontsize;
1307 delete $self->{texture}; 1335 delete $self->{texture};
1336
1337 $self->update;
1308 $self->check_size; 1338 $self->check_size;
1309 $self->update;
1310} 1339}
1311 1340
1312sub _draw { 1341sub _draw {
1313 my ($self) = @_; 1342 my ($self) = @_;
1314 1343
1315 my $tex = $self->{texture} ||= do { 1344 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}});
1316 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1346 $self->{layout}->set_font ($self->{font}) if $self->{font};
1317 $self->{layout}->set_width ($self->{w}); 1347 $self->{layout}->set_width ($self->{w});
1318 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1348 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1349
1319 new_from_layout CFClient::Texture $self->{layout} 1350 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5;
1355
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5;
1359
1360 $tex
1320 }; 1361 };
1321 1362
1322 glEnable GL_BLEND;
1323 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1324 glEnable GL_TEXTURE_2D; 1363 glEnable GL_TEXTURE_2D;
1325 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1364 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1326 1365
1366 if ($tex->{format} == GL_ALPHA) {
1327 glColor @{$self->{fg}}; 1367 glColor @{$self->{fg}};
1328
1329 $self->{ox} = int (
1330 $self->{align} < 0 ? $self->{padding}
1331 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1332 : ($self->{w} - $tex->{w}) * 0.5
1333 );
1334
1335 $self->{oy} = int (
1336 $self->{valign} < 0 ? $self->{padding}
1337 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1338 : ($self->{h} - $tex->{h}) * 0.5
1339 );
1340
1341 $tex->draw_quad ($self->{ox}, $self->{oy}); 1368 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1369 } else {
1370 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1371 }
1342 1372
1343 glDisable GL_TEXTURE_2D; 1373 glDisable GL_TEXTURE_2D;
1344 glDisable GL_BLEND;
1345} 1374}
1346 1375
1347############################################################################# 1376#############################################################################
1348 1377
1349package CFClient::UI::EntryBase; 1378package CFClient::UI::EntryBase;
1384 $self->{layout}->set_text ("$text "); 1413 $self->{layout}->set_text ("$text ");
1385 1414
1386 $self->emit (changed => $self->{text}); 1415 $self->emit (changed => $self->{text});
1387} 1416}
1388 1417
1418sub set_text {
1419 my ($self, $text) = @_;
1420
1421 $self->{cursor} = length $text;
1422 $self->_set_text ($text);
1423 $self->check_size;
1424 $self->update;
1425}
1426
1389sub get_text { 1427sub get_text {
1390 $_[0]{text} 1428 $_[0]{text}
1391} 1429}
1392 1430
1393sub size_request { 1431sub size_request {
1400 1438
1401sub size_allocate { 1439sub size_allocate {
1402 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1403 1441
1404 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1442 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1405}
1406
1407sub set_text {
1408 my ($self, $text) = @_;
1409
1410 $self->{cursor} = length $text;
1411 $self->_set_text ($text);
1412 $self->update;
1413} 1443}
1414 1444
1415sub key_down { 1445sub key_down {
1416 my ($self, $ev) = @_; 1446 my ($self, $ev) = @_;
1417 1447
1605 1635
1606 if ($GRAB == $self) { 1636 if ($GRAB == $self) {
1607 $self->{fg} = $self->{active_fg}; 1637 $self->{fg} = $self->{active_fg};
1608 } 1638 }
1609 1639
1610 glEnable GL_BLEND;
1611 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1612 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1613 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1614 glColor 0, 0, 0, 1; 1642 glColor 0, 0, 0, 1;
1615 1643
1616 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1644 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1617 1645
1618 glDisable GL_TEXTURE_2D; 1646 glDisable GL_TEXTURE_2D;
1619 glDisable GL_BLEND;
1620 1647
1621 $self->SUPER::_draw; 1648 $self->SUPER::_draw;
1622} 1649}
1623 1650
1624############################################################################# 1651#############################################################################
1671 1698
1672 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1699 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1673 1700
1674 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1701 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1675 1702
1676 glEnable GL_BLEND; 1703 my $tex = $self->{state} ? $tex[1] : $tex[0];
1704
1677 glEnable GL_TEXTURE_2D; 1705 glEnable GL_TEXTURE_2D;
1678 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1679
1680 my $tex = $self->{state} ? $tex[1] : $tex[0];
1681
1682 $tex->draw_quad (0, 0, $s, $s); 1706 $tex->draw_quad_alpha (0, 0, $s, $s);
1683
1684 glDisable GL_TEXTURE_2D; 1707 glDisable GL_TEXTURE_2D;
1685 glDisable GL_BLEND;
1686} 1708}
1687 1709
1688############################################################################# 1710#############################################################################
1689 1711
1690package CFClient::UI::Image; 1712package CFClient::UI::Image;
1733 glTranslate 0, -$self->{w}, 0; 1755 glTranslate 0, -$self->{w}, 0;
1734 1756
1735 ($w, $h) = ($h, $w); 1757 ($w, $h) = ($h, $w);
1736 } 1758 }
1737 1759
1738 glEnable GL_BLEND;
1739 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1740 glEnable GL_TEXTURE_2D; 1760 glEnable GL_TEXTURE_2D;
1741 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1742 1762
1743 $tex->draw_quad (0, 0, $w, $h); 1763 $tex->draw_quad_alpha (0, 0, $w, $h);
1744 1764
1745 glDisable GL_BLEND;
1746 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1747} 1766}
1748 1767
1749############################################################################# 1768#############################################################################
1750 1769
1948 # TODO: calculations are off 1967 # TODO: calculations are off
1949 my $self = $class->SUPER::new ( 1968 my $self = $class->SUPER::new (
1950 fg => [1, 1, 1], 1969 fg => [1, 1, 1],
1951 active_fg => [0, 0, 0], 1970 active_fg => [0, 0, 0],
1952 range => [0, 0, 100, 10], 1971 range => [0, 0, 100, 10],
1953 req_w => 20, 1972 req_w => $::WIDTH / 80,
1954 req_h => 20, 1973 req_h => $::WIDTH / 80,
1955 vertical => 0, 1974 vertical => 0,
1956 can_hover => 1, 1975 can_hover => 1,
1957 inner_pad => 5, 1976 inner_pad => 5,
1958 @_ 1977 @_
1959 ); 1978 );
2042 $page ||= 2; 2061 $page ||= 2;
2043 2062
2044 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2045 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2046 2065
2047 glEnable GL_BLEND;
2048 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2049 glEnable GL_TEXTURE_2D; 2066 glEnable GL_TEXTURE_2D;
2050 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2051 2068
2052 # draw background 2069 # draw background
2053 $tex[1]->draw_quad (0, 0, $w, $h); 2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2054 2071
2055 # draw handle 2072 # draw handle
2056 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2073 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2057 2074
2058 glDisable GL_BLEND;
2059 glDisable GL_TEXTURE_2D; 2075 glDisable GL_TEXTURE_2D;
2060} 2076}
2061 2077
2062############################################################################# 2078#############################################################################
2063 2079
2074 fontsize => 1, 2090 fontsize => 1,
2075 can_events => 0, 2091 can_events => 0,
2076 #font => default_font 2092 #font => default_font
2077 @_, 2093 @_,
2078 2094
2079 layout => (new CFClient::Layout), 2095 layout => (new CFClient::Layout 1),
2080 par => [], 2096 par => [],
2081 height => 0, 2097 height => 0,
2082 children => [ 2098 children => [
2083 (new CFClient::UI::Empty expand => 1), 2099 (new CFClient::UI::Empty expand => 1),
2084 (new CFClient::UI::Slider vertical => 1), 2100 (new CFClient::UI::Slider vertical => 1),
2085 ], 2101 ],
2086 ); 2102 );
2087 2103
2088 $self->{children}[1]->connect (changed => sub { 2104 $self->{children}[1]->connect (changed => sub { $self->update });
2089 $self->update;
2090 });
2091 2105
2092 $self 2106 $self
2093} 2107}
2094 2108
2095sub set_fontsize { 2109sub set_fontsize {
2103 my ($self, $text) = @_; 2117 my ($self, $text) = @_;
2104 2118
2105 my $layout = $self->{layout}; 2119 my $layout = $self->{layout};
2106 2120
2107 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2108 $layout->set_width ($self->{w}); 2122 $layout->set_width ($self->{children}[0]{w});
2109 $layout->set_text ($text); 2123 $layout->set_markup ($text);
2110 2124
2111 ($layout->size)[1] 2125 ($layout->size)[1]
2112} 2126}
2113 2127
2114sub reflow { 2128sub reflow {
2152 2166
2153 return unless $self->{h} > 0; 2167 return unless $self->{h} > 0;
2154 2168
2155 delete $self->{texture}; 2169 delete $self->{texture};
2156 2170
2157 $ROOT->on_refresh ($self, sub { 2171 $ROOT->on_post_alloc ($self, sub {
2158 if (delete $self->{need_reflow}) { 2172 if (delete $self->{need_reflow}) {
2159 my $height = 0; 2173 my $height = 0;
2160 2174
2161 $height += $_->[0] = $self->text_height ($_->[2]) 2175 $height += $_->[0] = $self->text_height ($_->[2])
2162 for @{$self->{par}}; 2176 for @{$self->{par}};
2166 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2180 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2167 2181
2168 delete $self->{texture}; 2182 delete $self->{texture};
2169 } 2183 }
2170 2184
2171 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2185 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2172 glClearColor 0, 0, 0, 0; 2186 glClearColor 0, 0, 0, 0;
2173 glClear GL_COLOR_BUFFER_BIT; 2187 glClear GL_COLOR_BUFFER_BIT;
2174 2188
2175 glEnable GL_BLEND;
2176 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2177 glEnable GL_TEXTURE_2D; 2189 glEnable GL_TEXTURE_2D;
2178 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2179 2191
2180 my $top = int $self->{children}[1]{range}[0]; 2192 my $top = int $self->{children}[1]{range}[0];
2181 2193
2190 2202
2191 for my $par (@{$self->{par}}) { 2203 for my $par (@{$self->{par}}) {
2192 my $h = $par->[0]; 2204 my $h = $par->[0];
2193 2205
2194 if ($y0 < $y + $h && $y < $y1) { 2206 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] });
2195 $layout->set_text ($par->[2]); 2208 $layout->set_markup ($par->[2]);
2196 2209
2197 glColor @{ $par->[1] };
2198 my ($W, $H) = $layout->size; 2210 my ($W, $H) = $layout->size;
2199 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2211 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2200 } 2212 }
2201 2213
2202 $y += $h; 2214 $y += $h;
2203 } 2215 }
2204 2216
2205 glDisable GL_TEXTURE_2D; 2217 glDisable GL_TEXTURE_2D;
2206 glDisable GL_BLEND;
2207 }; 2218 };
2208 }); 2219 });
2209} 2220}
2210 2221
2211sub _draw { 2222sub _draw {
2212 my ($self) = @_; 2223 my ($self) = @_;
2213 2224
2214 if ($self->{texture}) {
2215 glEnable GL_BLEND;
2216 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2217 glEnable GL_TEXTURE_2D; 2225 glEnable GL_TEXTURE_2D;
2218 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2219 glColor 1, 1, 1, 1; 2227 glColor 1, 1, 1, 1;
2220 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2228 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2221 glDisable GL_TEXTURE_2D; 2229 glDisable GL_TEXTURE_2D;
2222 glDisable GL_BLEND;
2223 }
2224 2230
2225 $self->{children}[1]->draw; 2231 $self->{children}[1]->draw;
2226 2232
2227} 2233}
2228 2234
2322 @_, 2328 @_,
2323 can_events => 0, 2329 can_events => 0,
2324 ) 2330 )
2325} 2331}
2326 2332
2327sub set_markup { 2333sub set_tooltip_from {
2328 my ($self, $text) = @_; 2334 my ($self, $widget) = @_;
2329 2335
2330 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2336 $self->add (new CFClient::UI::Label
2331 $self->{label}->set_markup ($text); 2337 markup => $widget->{tooltip},
2332 $self->add ($self->{label}); 2338 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2339 fontsize => 0.8,
2340 fg => [0, 0, 0, 1],
2341 font => ($widget->{tooltip_font} || $::FONT_PROP),
2342 );
2333} 2343}
2334 2344
2335sub size_request { 2345sub size_request {
2336 my ($self) = @_; 2346 my ($self) = @_;
2337
2338 $self->child->set_max_size ($::WIDTH * 0.3);
2339 2347
2340 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2348 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2341 2349
2342 ($w + 4, $h + 4) 2350 ($w + 4, $h + 4)
2343} 2351}
2349} 2357}
2350 2358
2351sub _draw { 2359sub _draw {
2352 my ($self) = @_; 2360 my ($self) = @_;
2353 2361
2354 glPushMatrix;
2355 glTranslate 0.375, 0.375; 2362 glTranslate 0.375, 0.375;
2356 2363
2357 my ($w, $h) = @$self{qw(w h)}; 2364 my ($w, $h) = @$self{qw(w h)};
2358 2365
2359 glColor 1, 0.8, 0.4; 2366 glColor 1, 0.8, 0.4;
2370 glVertex 0 , $h; 2377 glVertex 0 , $h;
2371 glVertex $w, $h; 2378 glVertex $w, $h;
2372 glVertex $w, 0; 2379 glVertex $w, 0;
2373 glEnd; 2380 glEnd;
2374 2381
2375 glPopMatrix; 2382 glTranslate 2 - 0.375, 2 - 0.375;
2376
2377 glTranslate 2, 2;
2378 $self->SUPER::_draw; 2383 $self->SUPER::_draw;
2379} 2384}
2380 2385
2381############################################################################# 2386#############################################################################
2382 2387
2397 2402
2398sub size_request { 2403sub size_request {
2399 (32, 8) 2404 (32, 8)
2400} 2405}
2401 2406
2402sub draw { 2407sub _draw {
2403 my ($self) = @_; 2408 my ($self) = @_;
2404 2409
2410 return unless $::CONN;#d# manage and cache textures differently
2405 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2406 2412
2413 # TODO animation
2407 if ($tex) { 2414 if ($tex) {
2408 glEnable GL_BLEND;
2409 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2410 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2411 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2412 glColor 1, 1, 1, 1; 2417 glColor 1, 1, 1, 1;
2413 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2414 glDisable GL_TEXTURE_2D; 2419 glDisable GL_TEXTURE_2D;
2415 glDisable GL_BLEND;
2416 } 2420 }
2421}
2422
2423#############################################################################
2424
2425package CFClient::UI::InventoryItem;
2426
2427our @ISA = CFClient::UI::HBox::;
2428
2429sub new {
2430 my $class = shift;
2431
2432 my %args = @_;
2433
2434 my $item = delete $args{item};
2435
2436 my $desc = $item->{nrof} < 2
2437 ? $item->{name}
2438 : "$item->{nrof} $item->{name_pl}";
2439
2440
2441 my $self = $class->SUPER::new (
2442 can_hover => 1,
2443 can_events => 1,
2444 tooltip => (CFClient::UI::Label->escape ($desc)
2445 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2446 connect_button_down => sub {
2447 my ($self, $ev, $x, $y) = @_;
2448
2449 # todo: maybe put examine on 1? but should just be a tooltip :(
2450 if ($ev->{button} == 1) {
2451 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2452 } elsif ($ev->{button} == 2) {
2453 $::CONN->send ("apply $item->{tag}");
2454 } elsif ($ev->{button} == 3) {
2455 CFClient::UI::Menu->new (
2456 items => [
2457 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2458 [
2459 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2460 sub { $::CONN->send ("lock $item->{tag}") },
2461 ],
2462 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2463 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2464 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2465 ],
2466 )->popup ($ev);
2467 }
2468
2469 1
2470 },
2471 %args
2472 );
2473
2474 $self->add (new CFClient::UI::Face
2475 can_events => 0,
2476 face => $item->{face},
2477 anim => $item->{anim},
2478 animspeed => $item->{animspeed},
2479 );
2480
2481 $self->add (new CFClient::UI::Label
2482 can_events => 0,
2483 text => $desc,
2484 );
2485
2486 $self
2487}
2488
2489#############################################################################
2490
2491package CFClient::UI::Inventory;
2492
2493our @ISA = CFClient::UI::ScrolledWindow::;
2494
2495sub new {
2496 my $class = shift;
2497
2498 my $self = $class->SUPER::new (
2499 scrolled => (new CFClient::UI::VBox),
2500 @_,
2501 );
2502
2503 $self
2504}
2505
2506sub set_items {
2507 my ($self, $items) = @_;
2508
2509 $self->{scrolled}->clear;
2510 return unless $items;
2511
2512 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2513
2514 $self->{real_items} = \@items;
2515
2516 for my $item (@items) {
2517 my $desc = $item->{nrof} < 2
2518 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}";
2520
2521 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2522 }
2523
2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2525}
2526
2527sub size_request {
2528 my ($self) = @_;
2529 ($self->{req_w}, $self->{req_h});
2530}
2531
2532#############################################################################
2533
2534package CFClient::UI::Menu;
2535
2536our @ISA = CFClient::UI::FancyFrame::;
2537
2538use CFClient::OpenGL;
2539
2540sub new {
2541 my $class = shift;
2542
2543 my $self = $class->SUPER::new (
2544 items => [],
2545 z => 100,
2546 @_,
2547 );
2548
2549 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2550
2551 for my $item (@{ $self->{items} }) {
2552 my ($widget, $cb) = @$item;
2553
2554 # handle various types of items, only text for now
2555 if (!ref $widget) {
2556 $widget = new CFClient::UI::Label
2557 can_hover => 1,
2558 can_events => 1,
2559 text => $widget;
2560 }
2561
2562 $self->{item}{$widget} = $item;
2563
2564 $self->{vbox}->add ($widget);
2565 }
2566
2567 $self
2568}
2569
2570# popup given the event (must be a mouse button down event currently)
2571sub popup {
2572 my ($self, $ev) = @_;
2573
2574 $self->emit ("popdown");
2575
2576 # maybe save $GRAB? must be careful about events...
2577 $GRAB = $self;
2578 $self->{button} = $ev->{button};
2579
2580 $self->show;
2581 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2582}
2583
2584sub mouse_motion {
2585 my ($self, $ev, $x, $y) = @_;
2586
2587 # TODO: should use vbox->find_widget or so
2588 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2589 $self->{hover} = $self->{item}{$HOVER};
2590}
2591
2592sub button_up {
2593 my ($self, $ev, $x, $y) = @_;
2594
2595 if ($ev->{button} == $self->{button}) {
2596 undef $GRAB;
2597 $self->hide;
2598
2599 $self->emit ("popdown");
2600 $self->{hover}[1]->() if $self->{hover};
2601 }
2602}
2603
2604#############################################################################
2605
2606package CFClient::UI::Statusbox;
2607
2608our @ISA = CFClient::UI::VBox::;
2609
2610sub reorder {
2611 my ($self) = @_;
2612 my $NOW = time;
2613
2614 while (my ($k, $v) = each %{ $self->{item} }) {
2615 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2616 }
2617
2618 my @widgets;
2619
2620 my @items = sort {
2621 $a->{pri} <=> $b->{pri}
2622 or $b->{id} <=> $a->{id}
2623 } values %{ $self->{item} };
2624
2625 my $count = 10 + 1;
2626 for my $item (@items) {
2627 last unless --$count;
2628
2629 push @widgets, $item->{label} ||= do {
2630 # TODO: doesn't handle markup well (read as: at all)
2631 my $short = $item->{count} > 1
2632 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text};
2634
2635 for ($short) {
2636 s/^\s+//;
2637 s/\012.*/…/s;
2638 my $len = int 40 / $item->{fontsize};
2639 substr $_, $len, length, "…" if $len < length;
2640 }
2641
2642 new CFClient::UI::Label
2643 markup => $short,
2644 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize},
2648 color => $item->{color},
2649 can_events => 1,
2650 can_hover => 1
2651 };
2652 }
2653
2654 $self->clear;
2655 $self->SUPER::add (reverse @widgets);
2656}
2657
2658sub add {
2659 my ($self, $text, %arg) = @_;
2660
2661 $text =~ s/^\s+//;
2662 $text =~ s/\s+$//;
2663
2664 my $timeout = time + ((delete $arg{timeout}) || 60);
2665
2666 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2667
2668 if (my $item = $self->{item}{$group}) {
2669 if ($item->{text} eq $text) {
2670 $item->{count}++;
2671 } else {
2672 $item->{count} = 1;
2673 $item->{text} = $item->{tooltip} = $text;
2674 }
2675 $item->{id} = ++$self->{id};
2676 $item->{timeout} = $timeout;
2677 delete $item->{label};
2678 } else {
2679 $self->{item}{$group} = {
2680 id => ++$self->{id},
2681 text => $text,
2682 timeout => $timeout,
2683 tooltip => $text,
2684 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0,
2687 count => 1,
2688 %arg,
2689 };
2690 }
2691
2692 $self->reorder;
2417} 2693}
2418 2694
2419############################################################################# 2695#############################################################################
2420 2696
2421package CFClient::UI::Root; 2697package CFClient::UI::Root;
2422 2698
2423our @ISA = CFClient::UI::Container::; 2699our @ISA = CFClient::UI::Container::;
2424 2700
2425use CFClient::OpenGL; 2701use CFClient::OpenGL;
2426 2702
2427sub check_size { 2703sub new {
2428 my ($self) = @_; 2704 my $class = shift;
2429 2705
2430 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2706 $class->SUPER::new (
2431} 2707 @_,
2432 2708 )
2433sub size_request {
2434 ($::WIDTH, $::HEIGHT)
2435} 2709}
2436 2710
2437sub configure { 2711sub configure {
2438 my ($self, $x, $y, $w, $h) = @_; 2712 my ($self, $x, $y, $w, $h) = @_;
2439 2713
2440 $self->SUPER::configure ($x, $y, $w, $h); 2714 $self->{w} = $w;
2715 $self->{h} = $h;
2716}
2441 2717
2718sub check_size {
2719 my ($self) = @_;
2720
2721 $self->size_allocate ($self->{w}, $self->{h})
2722 if $self->{w};
2723}
2724
2725sub size_request {
2726 my ($self) = @_;
2727
2728 ($self->{w}, $self->{h})
2729}
2730
2731sub size_allocate {
2732 my ($self, $w, $h) = @_;
2733
2734 my $old_w = $self->{old_w};
2735 my $old_h = $self->{old_h};
2736
2737 if ($old_w && $old_h) {
2738 for my $child ($self->children) {
2739 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2740 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2741 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2742 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2743 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2744 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2745 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2746 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2747 }
2748 }
2749
2442 for my $child (@{$self->{children}}) { 2750 for my $child ($self->children) {
2443 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2751 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2444 2752
2445 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2753 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2446 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2447 $child->configure ($X, $Y, $W,$H); 2755 $child->configure ($X, $Y, $W, $H);
2448 } 2756 }
2449}
2450 2757
2451sub _topleft { 2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760}
2761
2762sub coord2local {
2452 my ($self, $x, $y) = @_; 2763 my ($self, $x, $y) = @_;
2453 2764
2454 ($x, $y) 2765 ($x, $y)
2455} 2766}
2456 2767
2768sub coord2global {
2769 my ($self, $x, $y) = @_;
2770
2771 ($x, $y)
2772}
2773
2457sub update { 2774sub update {
2458 my ($self) = @_; 2775 my ($self) = @_;
2459 2776
2460 $self->check_size; 2777 $self->check_size;
2461 ::refresh (); 2778 $::WANT_REFRESH++;
2462} 2779}
2463 2780
2464sub add { 2781sub add {
2465 my ($self, $child) = @_; 2782 my ($self, $child) = @_;
2466 2783
2467 # integerize window positions 2784 # integerise window positions
2468 $child->{x} = int $child->{x}; 2785 $child->{x} = int $child->{x};
2469 $child->{y} = int $child->{y}; 2786 $child->{y} = int $child->{y};
2470 2787
2471 $self->SUPER::add ($child); 2788 $self->SUPER::add ($child);
2472} 2789}
2475 my ($self, $id, $cb) = @_; 2792 my ($self, $id, $cb) = @_;
2476 2793
2477 $self->{refresh_hook}{$id} = $cb; 2794 $self->{refresh_hook}{$id} = $cb;
2478} 2795}
2479 2796
2797sub on_post_alloc {
2798 my ($self, $id, $cb) = @_;
2799
2800 $self->{post_alloc_hook}{$id} = $cb;
2801}
2802
2480sub draw { 2803sub draw {
2481 my ($self) = @_; 2804 my ($self) = @_;
2482 2805
2483 while (my $rcb = delete $self->{refresh_hook}) { 2806 while ($self->{refresh_hook}) {
2484 $_->() for values %$rcb; 2807 $_->()
2808 for values %{delete $self->{refresh_hook}};
2809 }
2810
2811 if ($self->{check_size}) {
2812 my @queue = ([], []);
2813
2814 for (;;) {
2815 if ($self->{check_size}) {
2816 # heuristic: check containers last
2817 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2818 for values %{delete $self->{check_size}}
2819 }
2820
2821 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2822
2823 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2824 ? @$widget{qw(user_w user_h)}
2825 : $widget->size_request;
2826
2827 if (delete $widget->{force_alloc}
2828 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2829 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2830
2831 $widget->{req_w} = $w;
2832 $widget->{req_h} = $h;
2833
2834 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2835
2836 $widget->{parent}->check_size
2837 if $widget->{parent};
2838 }
2839 }
2840 }
2841
2842 while ($self->{size_alloc}) {
2843 for (values %{delete $self->{size_alloc}}) {
2844 my ($widget, $w, $h) = @$_;
2845
2846 $w = 0 if $w < 0;
2847 $h = 0 if $h < 0;
2848
2849 $widget->{w} = $w;
2850 $widget->{h} = $h;
2851 $widget->size_allocate ($w, $h);
2852 $widget->emit (size_allocate => $w, $h);
2853 }
2854 }
2855
2856 while ($self->{post_alloc_hook}) {
2857 $_->()
2858 for values %{delete $self->{post_alloc_hook}};
2485 } 2859 }
2486 2860
2487 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2861 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2488 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2489 glClear GL_COLOR_BUFFER_BIT; 2863 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines