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.173 by root, Tue Apr 25 10:30:48 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 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 }
37
38 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size;
40 $TOOLTIP->update;
31 } 41 }
32 42
33 return; 43 return;
34 } 44 }
35 } 45 }
224 $self->{y} = $y; 234 $self->{y} = $y;
225 $self->update; 235 $self->update;
226 } 236 }
227 237
228 if ($self->{w} != $w || $self->{h} != $h) { 238 if ($self->{w} != $w || $self->{h} != $h) {
229 $self->{w} = $w; 239 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
230 $self->{h} = $h;
231
232 $self->size_allocate ($w, $h);
233 $self->update;
234 } 240 }
235} 241}
236 242
237sub size_allocate { 243sub size_allocate {
238 # nothing to be done 244 # nothing to be done
239} 245}
240 246
241sub children { 247sub children {
242} 248}
243 249
244# call when resoltuion changes etc. 250# call when resolution changes etc.
245sub reconfigure { 251sub reconfigure {
246 my ($self) = @_; 252 my ($self) = @_;
247 253
248 $_->reconfigure 254 $_->reconfigure
249 for $self->children; 255 for $self->children;
250 256
251 $_->check_size; 257 $self->check_size (1);
258 $self->update;
252} 259}
253 260
254sub set_max_size { 261sub set_max_size {
255 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
256 263
257 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
258 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 delete $self->{max_h}; $self->{max_h} = $h if $h;
259}
260
261# return top left coordinates
262sub _topleft {
263 my ($self, $x, $y) = @_;
264
265 $self->{parent}
266 or Carp::confess "no parent widget in _topleft\n";#d#
267
268 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
269} 266}
270 267
271# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
272sub coord2local { 269sub coord2local {
273 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
274 271
275 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
276 ($x - $X, $y - $Y)
277} 273}
278 274
279# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
280sub coord2global { 276sub coord2global {
281 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
282 278
283 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
284 ($x + $X, $y + $Y)
285} 280}
286 281
287sub focus_in { 282sub focus_in {
288 my ($self) = @_; 283 my ($self) = @_;
289 284
387 382
388sub set_parent { 383sub set_parent {
389 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
390 385
391 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};
392} 392}
393 393
394sub check_size { 394sub check_size {
395 my ($self) = @_; 395 my ($self, $forced) = @_;
396 396
397 $self->{parent} 397 $self->{force_alloc} = 1 if $forced;
398 or return 1; 398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
399
400 my ($w, $h) = $self->{user_w} && $self->{user_h}
401 ? @$self{qw(user_w user_h)}
402 : $self->size_request;
403
404 if ($w != $self->{req_w} || $h != $self->{req_h}) {
405 $self->{req_w} = $w;
406 $self->{req_h} = $h;
407
408 $self->{parent}->check_size
409 or $self->size_allocate (
410 (List::Util::max $self->{w}, $w),
411 (List::Util::max $self->{h}, $h),
412 );
413
414 1
415 } else {
416 0
417 }
418} 399}
419 400
420sub update { 401sub update {
421 my ($self) = @_; 402 my ($self) = @_;
422 403
519 500
520 $self 501 $self
521} 502}
522 503
523sub add { 504sub add {
524 my ($self, $child) = @_; 505 my ($self, @widgets) = @_;
525 506
526 $child->set_parent ($self); 507 $_->set_parent ($self)
508 for @widgets;
527 509
528 use sort 'stable'; 510 use sort 'stable';
529 511
530 $self->{children} = [ 512 $self->{children} = [
531 sort { $a->{z} <=> $b->{z} } 513 sort { $a->{z} <=> $b->{z} }
532 @{$self->{children}}, $child 514 @{$self->{children}}, @widgets
533 ]; 515 ];
534 516
535 $child->check_size; 517 $self->check_size (1);
518 $self->update;
536} 519}
537 520
538sub children { 521sub children {
539 @{ $_[0]{children} } 522 @{ $_[0]{children} }
540} 523}
559 542
560 for (@$children) { 543 for (@$children) {
561 delete $_->{parent}; 544 delete $_->{parent};
562 $_->hide; 545 $_->hide;
563 } 546 }
547
548 $self->check_size;
549 $self->update;
564} 550}
565 551
566sub find_widget { 552sub find_widget {
567 my ($self, $x, $y) = @_; 553 my ($self, $x, $y) = @_;
568 554
643} 629}
644 630
645sub update { 631sub update {
646 my ($self) = @_; 632 my ($self) = @_;
647 633
648 # we want to do this delayed... 634 $ROOT->on_post_alloc ($self => sub { $self->render_child });
649 $self->render_chld;
650 $self->SUPER::update; 635 $self->SUPER::update;
651}
652
653sub render_chld {
654 my ($self) = @_;
655
656 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
657 glClearColor 0, 0, 0, 1;
658 glClear GL_COLOR_BUFFER_BIT;
659 $self->child->draw;
660 };
661} 636}
662 637
663sub size_allocate { 638sub size_allocate {
664 my ($self, $w, $h) = @_; 639 my ($self, $w, $h) = @_;
665 640
666 $self->child->configure (0, 0, $w, $h); 641 $self->SUPER::size_allocate ($w, $h);
642 $self->update;
643}
667 644
645sub _render {
646 $_[0]{children}[0]->draw;
647}
648
649sub render_child {
650 my ($self) = @_;
651
652 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
653 glClearColor 0, 0, 0, 0;
654 glClear GL_COLOR_BUFFER_BIT;
655
668 $self->render_chld; 656 $self->_render;
657# glColorMask 1, 1, 1, 0;
658# glEnable GL_BLEND;
659# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
660# glRasterPos 0, 0;
661# glCopyPixels 0, 0, $self->{w}, $self->{h};
662# glDisable GL_BLEND;
663# glColorMask 1, 1, 1, 1;
664 };
669} 665}
670 666
671sub _draw { 667sub _draw {
672 my ($self) = @_; 668 my ($self) = @_;
673 669
674 my ($w, $h) = ($self->w, $self->h); 670 my ($w, $h) = ($self->w, $self->h);
675 671
676 my $tex = $self->{texture} 672 my $tex = $self->{texture}
677 or return; 673 or return;
678 674
679 glEnable GL_BLEND;
680 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
681 glEnable GL_TEXTURE_2D; 675 glEnable GL_TEXTURE_2D;
682 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
677 glColor 0, 0, 0, 1;
683 678
684 $tex->draw_quad (0, 0, $w, $h); 679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
685 680
686 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
688} 682}
689 683
690############################################################################# 684#############################################################################
691 685
692package CFClient::UI::ViewPort; 686package CFClient::UI::ViewPort;
693 687
694our @ISA = CFClient::UI::Window::; 688our @ISA = CFClient::UI::Window::;
695 689
696sub new { die }
697
698sub size_request { 690sub size_request {
699 my ($self) = @_; 691 my ($self) = @_;
700 692
701 @$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)};
702 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 694 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
703 695
704 @$self{qw(child_w child_h)} 696 @$self{qw(child_w child_h)}
705} 697}
706 698
707sub _draw { 699sub size_allocate {
708 my ($self) = @_; 700 my ($self, $w, $h) = @_;
709 701
710 $self->{children}[1]->draw; 702 $self->update;
711} 703}
712 704
705sub set_offset {
706 my ($self, $x, $y) = @_;
713 707
714############################################################################# 708 $self->{view_x} = int $x;
709 $self->{view_y} = int $y;
715 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
716package CFClient::UI::Frame; 752package CFClient::UI::ScrolledWindow;
717 753
718our @ISA = CFClient::UI::Bin::; 754our @ISA = CFClient::UI::HBox::;
719
720use CFClient::OpenGL;
721 755
722sub new { 756sub new {
723 my $class = shift; 757 my $class = shift;
724 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
725 my $self = $class->SUPER::new ( 769 $self = $class->SUPER::new (
726 bg => [1, 1, 1, 1], 770 vp => (new CFClient::UI::ViewPort),
727 border_bg => [1, 1, 1, 1], 771 slider => $slider,
728 border => 0.8,
729 @_ 772 @_,
730 ); 773 );
731 774
775 $self->{vp}->add ($self->{scrolled});
776 $self->add ($self->{vp});
777 $self->add ($self->{slider});
778
732 $self 779 $self
733} 780}
734 781
735sub _draw { 782#TODO# update range on size_allocate depeneing on child
736 my ($self) = @_; 783# update viewport offset on scroll
737 784
738 my ($w, $h) = ($self->{w}, $self->{h}); 785#############################################################################
739 786
740 glEnable GL_BLEND; 787package CFClient::UI::Frame;
741 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
742 glEnable GL_TEXTURE_2D;
743 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
744 788
745# glBegin GL_QUADS; 789our @ISA = CFClient::UI::Bin::;
746# glColor 0, 0, 0, 0;
747# glVertex 0 , 0;
748# glVertex 0 , $h;
749# glVertex $w, $h;
750# glVertex $w, 0;
751# glEnd;
752 790
753 791use CFClient::OpenGL;
754 $self->child->draw;
755 glDisable GL_BLEND;
756 glDisable GL_TEXTURE_2D;
757}
758 792
759############################################################################# 793#############################################################################
760 794
761package CFClient::UI::FancyFrame; 795package CFClient::UI::FancyFrame;
762 796
774 # TODO: user_x, user_y, overwrite moveto? 808 # TODO: user_x, user_y, overwrite moveto?
775 809
776 my $self = $class->SUPER::new ( 810 my $self = $class->SUPER::new (
777 bg => [1, 1, 1, 1], 811 bg => [1, 1, 1, 1],
778 border_bg => [1, 1, 1, 1], 812 border_bg => [1, 1, 1, 1],
779 border => 0.8, 813 border => 0.6,
780 can_events => 1, 814 can_events => 1,
781 @_ 815 @_
782 ); 816 );
783 817
784 $self->{title} &&= new CFClient::UI::Label 818 $self->{title} &&= new CFClient::UI::Label
785 align => 0, 819 align => 0,
786 valign => 1, 820 valign => 1,
787 text => $self->{title}, 821 text => $self->{title},
788 fontsize => 1; 822 fontsize => $self->{border};
789 823
790 $self 824 $self
791} 825}
792 826
793sub border { 827sub border {
818} 852}
819 853
820sub button_down { 854sub button_down {
821 my ($self, $ev, $x, $y) = @_; 855 my ($self, $ev, $x, $y) = @_;
822 856
857 my ($w, $h) = @$self{qw(w h)};
823 my $border = $self->border; 858 my $border = $self->border;
824 859
825 if ($x < $self->{w} && $x >= $self->{w} - $border 860 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
826 && $y < $self->{h} && $y >= $self->{h} - $border) { 861 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
827 862
863 if ($lr & $td) {
864 my ($wx, $wy) = ($self->{x}, $self->{y});
828 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 865 my ($ox, $oy) = ($ev->{x}, $ev->{y});
829 my ($bw, $bh) = ($self->{w}, $self->{h}); 866 my ($bw, $bh) = ($self->{w}, $self->{h});
830 867
868 my $mx = $x < $border;
869 my $my = $y < $border;
870
831 $self->{motion} = sub { 871 $self->{motion} = sub {
832 my ($ev, $x, $y) = @_; 872 my ($ev, $x, $y) = @_;
833 873
834 ($x, $y) = ($ev->{x}, $ev->{y}); 874 my $dx = $ev->{x} - $ox;
875 my $dy = $ev->{y} - $oy;
835 876
836 $self->{user_w} = $bw + $x - $ox; 877 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
837 $self->{user_h} = $bh + $y - $oy; 878 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
879 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
838 $self->check_size; 880 $self->check_size;
839 }; 881 };
840 882
841 } elsif ($x >= 0 && $x < $self->{w} 883 } elsif ($lr ^ $td) {
842 && $y >= 0 && $y < $border) {
843
844 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 884 my ($ox, $oy) = ($ev->{x}, $ev->{y});
845 my ($bx, $by) = ($self->{x}, $self->{y}); 885 my ($bx, $by) = ($self->{x}, $self->{y});
846 886
847 $self->{motion} = sub { 887 $self->{motion} = sub {
848 my ($ev, $x, $y) = @_; 888 my ($ev, $x, $y) = @_;
871 my ($self) = @_; 911 my ($self) = @_;
872 912
873 my ($w, $h ) = ($self->{w}, $self->{h}); 913 my ($w, $h ) = ($self->{w}, $self->{h});
874 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 914 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
875 915
876 glEnable GL_BLEND;
877 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
878 glEnable GL_TEXTURE_2D; 916 glEnable GL_TEXTURE_2D;
879 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
880 918
881 my $border = $self->border; 919 my $border = $self->border;
882 920
883 glColor @{ $self->{border_bg} }; 921 glColor @{ $self->{border_bg} };
884 $tex[1]->draw_quad (0, 0, $w, $border); 922 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
885 $tex[3]->draw_quad (0, $border, $border, $ch); 923 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
886 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 924 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
887 $tex[4]->draw_quad (0, $h - $border, $w, $border); 925 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
888 926
927 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
889 my $bg = $tex[0]; 928 my $bg = $tex[0];
890 929
891 # TODO: repeat texture not scale 930 # TODO: repeat texture not scale
892 my $rep_x = $cw / $bg->{w}; 931 my $rep_x = $cw / $bg->{w};
893 my $rep_y = $ch / $bg->{h}; 932 my $rep_y = $ch / $bg->{h};
894 933
895 glColor @{ $self->{bg} }; 934 glColor @{ $self->{bg} };
896 935
897 $bg->{s} = $rep_x; 936 $bg->{s} = $rep_x;
898 $bg->{t} = $rep_y; 937 $bg->{t} = $rep_y;
899 $bg->{wrap_mode} = 1; 938 $bg->{wrap_mode} = 1;
900 $bg->draw_quad ($border, $border, $cw, $ch); 939 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
940 }
901 941
902 glDisable GL_TEXTURE_2D; 942 glDisable GL_TEXTURE_2D;
903 glDisable GL_BLEND;
904 943
905 $self->{title}->draw if $self->{title}; 944 $self->{title}->draw if $self->{title};
945
906 $self->child->draw; 946 $self->child->draw;
907} 947}
908 948
909############################################################################# 949#############################################################################
910 950
1134} 1174}
1135 1175
1136sub size_allocate { 1176sub size_allocate {
1137 my ($self, $w, $h) = @_; 1177 my ($self, $w, $h) = @_;
1138 1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1139 my $children = $self->{children}; 1181 my $children = $self->{children};
1140 1182
1141 my @h = map $_->{req_h}, @$children; 1183 my @h = map $_->{req_h}, @$children;
1142 1184
1143 my $req_h = List::Util::sum @h; 1185 my $req_h = List::Util::sum @h;
1182 my ($class, %arg) = @_; 1224 my ($class, %arg) = @_;
1183 1225
1184 my $self = $class->SUPER::new ( 1226 my $self = $class->SUPER::new (
1185 fg => [1, 1, 1], 1227 fg => [1, 1, 1],
1186 #font => default_font 1228 #font => default_font
1229 #text => initial text
1230 #markup => initial narkup
1231 layout => (new CFClient::Layout),
1187 fontsize => 1, 1232 fontsize => 1,
1188 text => "",
1189 align => -1, 1233 align => -1,
1190 valign => -1, 1234 valign => -1,
1191 padding => 2, 1235 padding => 2,
1192 layout => new CFClient::Layout,
1193 can_events => 0, 1236 can_events => 0,
1194 %arg 1237 %arg
1195 ); 1238 );
1196 1239
1197 if (exists $self->{template}) { 1240 if (exists $self->{template}) {
1198 my $layout = new CFClient::Layout; 1241 my $layout = new CFClient::Layout;
1199 $layout->set_text (delete $self->{template}); 1242 $layout->set_text (delete $self->{template});
1200 $self->{template} = $layout; 1243 $self->{template} = $layout;
1201 } 1244 }
1202 1245
1203 $self->set_text (delete $self->{text}) if exists $self->{text}; 1246 if (exists $self->{markup}) {
1204 $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 }
1205 1251
1206 $self 1252 $self
1207} 1253}
1208 1254
1209sub escape { 1255sub escape {
1221 1267
1222 delete $self->{texture}; 1268 delete $self->{texture};
1223 $self->SUPER::update; 1269 $self->SUPER::update;
1224} 1270}
1225 1271
1226sub reconfigure {
1227 my ($self) = @_;
1228
1229 delete $self->{texture};
1230}
1231
1232sub set_text { 1272sub set_text {
1233 my ($self, $text) = @_; 1273 my ($self, $text) = @_;
1234 1274
1235 return if $self->{text} eq "T$text"; 1275 return if $self->{text} eq "T$text";
1236 $self->{text} = "T$text"; 1276 $self->{text} = "T$text";
1237 1277
1278 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1238 $self->{layout}->set_text ($text); 1279 $self->{layout}->set_text ($text);
1239 1280
1240 delete $self->{texture};
1241 $self->update; 1281 $self->update;
1242 $self->check_size; 1282 $self->check_size;
1243} 1283}
1244 1284
1245sub set_markup { 1285sub set_markup {
1246 my ($self, $markup) = @_; 1286 my ($self, $markup) = @_;
1247 1287
1248 return if $self->{text} eq "M$markup"; 1288 return if $self->{text} eq "M$markup";
1249 $self->{text} = "M$markup"; 1289 $self->{text} = "M$markup";
1250 1290
1291 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1292
1293 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1251 $self->{layout}->set_markup ($markup); 1294 $self->{layout}->set_markup ($markup);
1252 1295
1253 delete $self->{texture};
1254 $self->update; 1296 $self->update;
1255 $self->check_size; 1297 $self->check_size;
1256} 1298}
1257 1299
1258sub size_request { 1300sub size_request {
1289sub set_fontsize { 1331sub set_fontsize {
1290 my ($self, $fontsize) = @_; 1332 my ($self, $fontsize) = @_;
1291 1333
1292 $self->{fontsize} = $fontsize; 1334 $self->{fontsize} = $fontsize;
1293 delete $self->{texture}; 1335 delete $self->{texture};
1336
1337 $self->update;
1294 $self->check_size; 1338 $self->check_size;
1295 $self->update;
1296} 1339}
1297 1340
1298sub _draw { 1341sub _draw {
1299 my ($self) = @_; 1342 my ($self) = @_;
1300 1343
1301 my $tex = $self->{texture} ||= do { 1344 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}});
1302 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1346 $self->{layout}->set_font ($self->{font}) if $self->{font};
1303 $self->{layout}->set_width ($self->{w}); 1347 $self->{layout}->set_width ($self->{w});
1304 $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
1305 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
1306 }; 1361 };
1307 1362
1308 glEnable GL_BLEND;
1309 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1310 glEnable GL_TEXTURE_2D; 1363 glEnable GL_TEXTURE_2D;
1311 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1364 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1312 1365
1366 if ($tex->{format} == GL_ALPHA) {
1313 glColor @{$self->{fg}}; 1367 glColor @{$self->{fg}};
1314
1315 $self->{ox} = int (
1316 $self->{align} < 0 ? $self->{padding}
1317 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1318 : ($self->{w} - $tex->{w}) * 0.5
1319 );
1320
1321 $self->{oy} = int (
1322 $self->{valign} < 0 ? $self->{padding}
1323 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1324 : ($self->{h} - $tex->{h}) * 0.5
1325 );
1326
1327 $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 }
1328 1372
1329 glDisable GL_TEXTURE_2D; 1373 glDisable GL_TEXTURE_2D;
1330 glDisable GL_BLEND;
1331} 1374}
1332 1375
1333############################################################################# 1376#############################################################################
1334 1377
1335package CFClient::UI::EntryBase; 1378package CFClient::UI::EntryBase;
1370 $self->{layout}->set_text ("$text "); 1413 $self->{layout}->set_text ("$text ");
1371 1414
1372 $self->emit (changed => $self->{text}); 1415 $self->emit (changed => $self->{text});
1373} 1416}
1374 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
1375sub get_text { 1427sub get_text {
1376 $_[0]{text} 1428 $_[0]{text}
1377} 1429}
1378 1430
1379sub size_request { 1431sub size_request {
1386 1438
1387sub size_allocate { 1439sub size_allocate {
1388 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1389 1441
1390 $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
1391}
1392
1393sub set_text {
1394 my ($self, $text) = @_;
1395
1396 $self->{cursor} = length $text;
1397 $self->_set_text ($text);
1398 $self->update;
1399} 1443}
1400 1444
1401sub key_down { 1445sub key_down {
1402 my ($self, $ev) = @_; 1446 my ($self, $ev) = @_;
1403 1447
1591 1635
1592 if ($GRAB == $self) { 1636 if ($GRAB == $self) {
1593 $self->{fg} = $self->{active_fg}; 1637 $self->{fg} = $self->{active_fg};
1594 } 1638 }
1595 1639
1596 glEnable GL_BLEND;
1597 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1598 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1599 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1600 glColor 0, 0, 0, 1; 1642 glColor 0, 0, 0, 1;
1601 1643
1602 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1644 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1603 1645
1604 glDisable GL_TEXTURE_2D; 1646 glDisable GL_TEXTURE_2D;
1605 glDisable GL_BLEND;
1606 1647
1607 $self->SUPER::_draw; 1648 $self->SUPER::_draw;
1608} 1649}
1609 1650
1610############################################################################# 1651#############################################################################
1657 1698
1658 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;
1659 1700
1660 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1701 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1661 1702
1662 glEnable GL_BLEND; 1703 my $tex = $self->{state} ? $tex[1] : $tex[0];
1704
1663 glEnable GL_TEXTURE_2D; 1705 glEnable GL_TEXTURE_2D;
1664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1665
1666 my $tex = $self->{state} ? $tex[1] : $tex[0];
1667
1668 $tex->draw_quad (0, 0, $s, $s); 1706 $tex->draw_quad_alpha (0, 0, $s, $s);
1669
1670 glDisable GL_TEXTURE_2D; 1707 glDisable GL_TEXTURE_2D;
1671 glDisable GL_BLEND;
1672} 1708}
1673 1709
1674############################################################################# 1710#############################################################################
1675 1711
1676package CFClient::UI::Image; 1712package CFClient::UI::Image;
1719 glTranslate 0, -$self->{w}, 0; 1755 glTranslate 0, -$self->{w}, 0;
1720 1756
1721 ($w, $h) = ($h, $w); 1757 ($w, $h) = ($h, $w);
1722 } 1758 }
1723 1759
1724 glEnable GL_BLEND;
1725 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1726 glEnable GL_TEXTURE_2D; 1760 glEnable GL_TEXTURE_2D;
1727 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1728 1762
1729 $tex->draw_quad (0, 0, $w, $h); 1763 $tex->draw_quad_alpha (0, 0, $w, $h);
1730 1764
1731 glDisable GL_BLEND;
1732 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1733} 1766}
1734 1767
1735############################################################################# 1768#############################################################################
1736 1769
1934 # TODO: calculations are off 1967 # TODO: calculations are off
1935 my $self = $class->SUPER::new ( 1968 my $self = $class->SUPER::new (
1936 fg => [1, 1, 1], 1969 fg => [1, 1, 1],
1937 active_fg => [0, 0, 0], 1970 active_fg => [0, 0, 0],
1938 range => [0, 0, 100, 10], 1971 range => [0, 0, 100, 10],
1939 req_w => 20, 1972 req_w => $::WIDTH / 80,
1940 req_h => 20, 1973 req_h => $::WIDTH / 80,
1941 vertical => 0, 1974 vertical => 0,
1942 can_hover => 1, 1975 can_hover => 1,
1943 inner_pad => 5, 1976 inner_pad => 5,
1944 @_ 1977 @_
1945 ); 1978 );
2028 $page ||= 2; 2061 $page ||= 2;
2029 2062
2030 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2031 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2032 2065
2033 glEnable GL_BLEND;
2034 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2035 glEnable GL_TEXTURE_2D; 2066 glEnable GL_TEXTURE_2D;
2036 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2037 2068
2038 # draw background 2069 # draw background
2039 $tex[1]->draw_quad (0, 0, $w, $h); 2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2040 2071
2041 # draw handle 2072 # draw handle
2042 $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);
2043 2074
2044 glDisable GL_BLEND;
2045 glDisable GL_TEXTURE_2D; 2075 glDisable GL_TEXTURE_2D;
2046} 2076}
2047 2077
2048############################################################################# 2078#############################################################################
2049 2079
2060 fontsize => 1, 2090 fontsize => 1,
2061 can_events => 0, 2091 can_events => 0,
2062 #font => default_font 2092 #font => default_font
2063 @_, 2093 @_,
2064 2094
2065 layout => (new CFClient::Layout), 2095 layout => (new CFClient::Layout 1),
2066 par => [], 2096 par => [],
2067 height => 0, 2097 height => 0,
2068 children => [ 2098 children => [
2069 (new CFClient::UI::Empty expand => 1), 2099 (new CFClient::UI::Empty expand => 1),
2070 (new CFClient::UI::Slider vertical => 1), 2100 (new CFClient::UI::Slider vertical => 1),
2071 ], 2101 ],
2072 ); 2102 );
2073 2103
2074 $self->{children}[1]->connect (changed => sub { 2104 $self->{children}[1]->connect (changed => sub { $self->update });
2075 $self->update;
2076 });
2077 2105
2078 $self 2106 $self
2079} 2107}
2080 2108
2081sub set_fontsize { 2109sub set_fontsize {
2089 my ($self, $text) = @_; 2117 my ($self, $text) = @_;
2090 2118
2091 my $layout = $self->{layout}; 2119 my $layout = $self->{layout};
2092 2120
2093 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2094 $layout->set_width ($self->{w}); 2122 $layout->set_width ($self->{children}[0]{w});
2095 $layout->set_text ($text); 2123 $layout->set_markup ($text);
2096 2124
2097 ($layout->size)[1] 2125 ($layout->size)[1]
2098} 2126}
2099 2127
2100sub reflow { 2128sub reflow {
2138 2166
2139 return unless $self->{h} > 0; 2167 return unless $self->{h} > 0;
2140 2168
2141 delete $self->{texture}; 2169 delete $self->{texture};
2142 2170
2143 $ROOT->on_refresh ($self, sub { 2171 $ROOT->on_post_alloc ($self, sub {
2144 if (delete $self->{need_reflow}) { 2172 if (delete $self->{need_reflow}) {
2145 my $height = 0; 2173 my $height = 0;
2146 2174
2147 $height += $_->[0] = $self->text_height ($_->[2]) 2175 $height += $_->[0] = $self->text_height ($_->[2])
2148 for @{$self->{par}}; 2176 for @{$self->{par}};
2152 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2180 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2153 2181
2154 delete $self->{texture}; 2182 delete $self->{texture};
2155 } 2183 }
2156 2184
2157 $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 {
2158 glClearColor 0, 0, 0, 1; 2186 glClearColor 0, 0, 0, 0;
2159 glClear GL_COLOR_BUFFER_BIT; 2187 glClear GL_COLOR_BUFFER_BIT;
2160 2188
2161 glEnable GL_BLEND;
2162 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2163 glEnable GL_TEXTURE_2D; 2189 glEnable GL_TEXTURE_2D;
2164 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2165 2191
2166 my $top = int $self->{children}[1]{range}[0]; 2192 my $top = int $self->{children}[1]{range}[0];
2167 2193
2176 2202
2177 for my $par (@{$self->{par}}) { 2203 for my $par (@{$self->{par}}) {
2178 my $h = $par->[0]; 2204 my $h = $par->[0];
2179 2205
2180 if ($y0 < $y + $h && $y < $y1) { 2206 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] });
2181 $layout->set_text ($par->[2]); 2208 $layout->set_markup ($par->[2]);
2182 2209
2183 glColor @{ $par->[1] };
2184 my ($W, $H) = $layout->size; 2210 my ($W, $H) = $layout->size;
2185 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);
2186 } 2212 }
2187 2213
2188 $y += $h; 2214 $y += $h;
2189 } 2215 }
2190 2216
2191 glDisable GL_TEXTURE_2D; 2217 glDisable GL_TEXTURE_2D;
2192 glDisable GL_BLEND;
2193 }; 2218 };
2194 }); 2219 });
2195} 2220}
2196 2221
2197sub _draw { 2222sub _draw {
2198 my ($self) = @_; 2223 my ($self) = @_;
2199 2224
2200 if ($self->{texture}) {
2201 glEnable GL_TEXTURE_2D; 2225 glEnable GL_TEXTURE_2D;
2202 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2203 glColor 1, 1, 1, 1; 2227 glColor 1, 1, 1, 1;
2204 $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});
2205 glDisable GL_TEXTURE_2D; 2229 glDisable GL_TEXTURE_2D;
2206 }
2207 2230
2208 $self->{children}[1]->draw; 2231 $self->{children}[1]->draw;
2209 2232
2210} 2233}
2211 2234
2305 @_, 2328 @_,
2306 can_events => 0, 2329 can_events => 0,
2307 ) 2330 )
2308} 2331}
2309 2332
2310sub set_markup { 2333sub set_tooltip_from {
2311 my ($self, $text) = @_; 2334 my ($self, $widget) = @_;
2312 2335
2313 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2336 $self->add (new CFClient::UI::Label
2314 $self->{label}->set_markup ($text); 2337 markup => $widget->{tooltip},
2315 $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 );
2316} 2343}
2317 2344
2318sub size_request { 2345sub size_request {
2319 my ($self) = @_; 2346 my ($self) = @_;
2320
2321 $self->child->set_max_size ($::WIDTH * 0.3);
2322 2347
2323 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2348 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2324 2349
2325 ($w + 4, $h + 4) 2350 ($w + 4, $h + 4)
2326} 2351}
2332} 2357}
2333 2358
2334sub _draw { 2359sub _draw {
2335 my ($self) = @_; 2360 my ($self) = @_;
2336 2361
2337 glPushMatrix;
2338 glTranslate 0.375, 0.375; 2362 glTranslate 0.375, 0.375;
2339 2363
2340 my ($w, $h) = @$self{qw(w h)}; 2364 my ($w, $h) = @$self{qw(w h)};
2341 2365
2342 glColor 1, 0.8, 0.4; 2366 glColor 1, 0.8, 0.4;
2353 glVertex 0 , $h; 2377 glVertex 0 , $h;
2354 glVertex $w, $h; 2378 glVertex $w, $h;
2355 glVertex $w, 0; 2379 glVertex $w, 0;
2356 glEnd; 2380 glEnd;
2357 2381
2358 glPopMatrix; 2382 glTranslate 2 - 0.375, 2 - 0.375;
2359
2360 glTranslate 2, 2;
2361 $self->SUPER::_draw; 2383 $self->SUPER::_draw;
2362} 2384}
2363 2385
2364############################################################################# 2386#############################################################################
2365 2387
2380 2402
2381sub size_request { 2403sub size_request {
2382 (32, 8) 2404 (32, 8)
2383} 2405}
2384 2406
2385sub draw { 2407sub _draw {
2386 my ($self) = @_; 2408 my ($self) = @_;
2387 2409
2410 return unless $::CONN;#d# manage and cache textures differently
2388 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2389 2412
2413 # TODO animation
2390 if ($tex) { 2414 if ($tex) {
2391 glEnable GL_BLEND;
2392 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2393 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2394 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2395 glColor 1, 1, 1, 1; 2417 glColor 1, 1, 1, 1;
2396 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2397 glDisable GL_TEXTURE_2D; 2419 glDisable GL_TEXTURE_2D;
2398 glDisable GL_BLEND;
2399 } 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;
2400} 2693}
2401 2694
2402############################################################################# 2695#############################################################################
2403 2696
2404package CFClient::UI::Root; 2697package CFClient::UI::Root;
2405 2698
2406our @ISA = CFClient::UI::Container::; 2699our @ISA = CFClient::UI::Container::;
2407 2700
2408use CFClient::OpenGL; 2701use CFClient::OpenGL;
2409 2702
2410sub check_size { 2703sub new {
2411 my ($self) = @_; 2704 my $class = shift;
2412 2705
2413 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2706 $class->SUPER::new (
2414} 2707 @_,
2415 2708 )
2416sub size_request {
2417 ($::WIDTH, $::HEIGHT)
2418} 2709}
2419 2710
2420sub configure { 2711sub configure {
2421 my ($self, $x, $y, $w, $h) = @_; 2712 my ($self, $x, $y, $w, $h) = @_;
2422 2713
2423 $self->SUPER::configure ($x, $y, $w, $h); 2714 $self->{w} = $w;
2715 $self->{h} = $h;
2716}
2424 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
2425 for my $child (@{$self->{children}}) { 2750 for my $child ($self->children) {
2426 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)};
2427 2752
2428 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2753 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2429 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2430 $child->configure ($X, $Y, $W,$H); 2755 $child->configure ($X, $Y, $W, $H);
2431 } 2756 }
2432}
2433 2757
2434sub _topleft { 2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760}
2761
2762sub coord2local {
2435 my ($self, $x, $y) = @_; 2763 my ($self, $x, $y) = @_;
2436 2764
2437 ($x, $y) 2765 ($x, $y)
2438} 2766}
2439 2767
2768sub coord2global {
2769 my ($self, $x, $y) = @_;
2770
2771 ($x, $y)
2772}
2773
2440sub update { 2774sub update {
2441 my ($self) = @_; 2775 my ($self) = @_;
2442 2776
2443 $self->check_size; 2777 $self->check_size;
2444 ::refresh (); 2778 $::WANT_REFRESH++;
2445} 2779}
2446 2780
2447sub add { 2781sub add {
2448 my ($self, $child) = @_; 2782 my ($self, $child) = @_;
2449 2783
2450 # integerize window positions 2784 # integerise window positions
2451 $child->{x} = int $child->{x}; 2785 $child->{x} = int $child->{x};
2452 $child->{y} = int $child->{y}; 2786 $child->{y} = int $child->{y};
2453 2787
2454 $self->SUPER::add ($child); 2788 $self->SUPER::add ($child);
2455} 2789}
2458 my ($self, $id, $cb) = @_; 2792 my ($self, $id, $cb) = @_;
2459 2793
2460 $self->{refresh_hook}{$id} = $cb; 2794 $self->{refresh_hook}{$id} = $cb;
2461} 2795}
2462 2796
2797sub on_post_alloc {
2798 my ($self, $id, $cb) = @_;
2799
2800 $self->{post_alloc_hook}{$id} = $cb;
2801}
2802
2463sub draw { 2803sub draw {
2464 my ($self) = @_; 2804 my ($self) = @_;
2465 2805
2466 while (my $rcb = delete $self->{refresh_hook}) { 2806 while ($self->{refresh_hook}) {
2467 $_->() 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}};
2468 } 2859 }
2469 2860
2470 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2861 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2471 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2472 glClear GL_COLOR_BUFFER_BIT; 2863 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines