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.172 by root, Tue Apr 25 09:52:04 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
250# call when resolution changes etc.
251sub reconfigure {
252 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256
257 $self->check_size (1);
258 $self->update;
259}
260
244sub set_max_size { 261sub set_max_size {
245 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
246 263
247 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
248 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 delete $self->{max_h}; $self->{max_h} = $h if $h;
249}
250
251# return top left coordinates
252sub _topleft {
253 my ($self, $x, $y) = @_;
254
255 $self->{parent}
256 or Carp::confess "no parent widget in _topleft\n";#d#
257
258 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
259} 266}
260 267
261# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
262sub coord2local { 269sub coord2local {
263 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
264 271
265 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
266 ($x - $X, $y - $Y)
267} 273}
268 274
269# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
270sub coord2global { 276sub coord2global {
271 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
272 278
273 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
274 ($x + $X, $y + $Y)
275} 280}
276 281
277sub focus_in { 282sub focus_in {
278 my ($self) = @_; 283 my ($self) = @_;
279 284
377 382
378sub set_parent { 383sub set_parent {
379 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
380 385
381 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};
382} 392}
383 393
384sub check_size { 394sub check_size {
385 my ($self) = @_; 395 my ($self, $forced) = @_;
386 396
387 $self->{parent} 397 $self->{force_alloc} = 1 if $forced;
388 or return 1; 398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
389
390 my ($w, $h) = $self->{user_w} && $self->{user_h}
391 ? @$self{qw(user_w user_h)}
392 : $self->size_request;
393
394 if ($w != $self->{req_w} || $h != $self->{req_h}) {
395 $self->{req_w} = $w;
396 $self->{req_h} = $h;
397
398 $self->{parent}->check_size
399 or $self->size_allocate (
400 (List::Util::max $self->{w}, $w),
401 (List::Util::max $self->{h}, $h),
402 );
403
404 1
405 } else {
406 0
407 }
408} 399}
409 400
410sub update { 401sub update {
411 my ($self) = @_; 402 my ($self) = @_;
412 403
509 500
510 $self 501 $self
511} 502}
512 503
513sub add { 504sub add {
514 my ($self, $child) = @_; 505 my ($self, @widgets) = @_;
515 506
516 $child->set_parent ($self); 507 $_->set_parent ($self)
508 for @widgets;
517 509
518 use sort 'stable'; 510 use sort 'stable';
519 511
520 $self->{children} = [ 512 $self->{children} = [
521 sort { $a->{z} <=> $b->{z} } 513 sort { $a->{z} <=> $b->{z} }
522 @{$self->{children}}, $child 514 @{$self->{children}}, @widgets
523 ]; 515 ];
524 516
525 $child->check_size; 517 $self->check_size (1);
518 $self->update;
526} 519}
527 520
528sub children { 521sub children {
529 @{ $_[0]{children} } 522 @{ $_[0]{children} }
530} 523}
549 542
550 for (@$children) { 543 for (@$children) {
551 delete $_->{parent}; 544 delete $_->{parent};
552 $_->hide; 545 $_->hide;
553 } 546 }
547
548 $self->check_size;
549 $self->update;
554} 550}
555 551
556sub find_widget { 552sub find_widget {
557 my ($self, $x, $y) = @_; 553 my ($self, $x, $y) = @_;
558 554
633} 629}
634 630
635sub update { 631sub update {
636 my ($self) = @_; 632 my ($self) = @_;
637 633
638 # we want to do this delayed... 634 $ROOT->on_post_alloc ($self => sub { $self->render_child });
639 $self->render_chld;
640 $self->SUPER::update; 635 $self->SUPER::update;
641}
642
643sub render_chld {
644 my ($self) = @_;
645
646 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
647 glClearColor 0, 0, 0, 1;
648 glClear GL_COLOR_BUFFER_BIT;
649 $self->child->draw;
650 };
651} 636}
652 637
653sub size_allocate { 638sub size_allocate {
654 my ($self, $w, $h) = @_; 639 my ($self, $w, $h) = @_;
655 640
656 $self->child->configure (0, 0, $w, $h); 641 $self->SUPER::size_allocate ($w, $h);
642 $self->update;
643}
657 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
658 $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 };
659} 665}
660 666
661sub _draw { 667sub _draw {
662 my ($self) = @_; 668 my ($self) = @_;
663 669
664 my ($w, $h) = ($self->w, $self->h); 670 my ($w, $h) = ($self->w, $self->h);
665 671
666 my $tex = $self->{texture} 672 my $tex = $self->{texture}
667 or return; 673 or return;
668 674
669 glEnable GL_BLEND;
670 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
671 glEnable GL_TEXTURE_2D; 675 glEnable GL_TEXTURE_2D;
672 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;
673 678
674 $tex->draw_quad (0, 0, $w, $h); 679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
675 680
676 glDisable GL_BLEND;
677 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
678} 682}
679 683
680############################################################################# 684#############################################################################
681 685
682package CFClient::UI::ViewPort; 686package CFClient::UI::ViewPort;
683 687
684our @ISA = CFClient::UI::Window::; 688our @ISA = CFClient::UI::Window::;
685 689
686sub new { die }
687
688sub size_request { 690sub size_request {
689 my ($self) = @_; 691 my ($self) = @_;
690 692
691 @$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)};
692 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 694 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
693 695
694 @$self{qw(child_w child_h)} 696 @$self{qw(child_w child_h)}
695} 697}
696 698
697sub _draw { 699sub size_allocate {
698 my ($self) = @_; 700 my ($self, $w, $h) = @_;
699 701
700 $self->{children}[1]->draw; 702 $self->update;
701} 703}
702 704
705sub set_offset {
706 my ($self, $x, $y) = @_;
703 707
704############################################################################# 708 $self->{view_x} = int $x;
709 $self->{view_y} = int $y;
705 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
706package CFClient::UI::Frame; 752package CFClient::UI::ScrolledWindow;
707 753
708our @ISA = CFClient::UI::Bin::; 754our @ISA = CFClient::UI::HBox::;
709
710use CFClient::OpenGL;
711 755
712sub new { 756sub new {
713 my $class = shift; 757 my $class = shift;
714 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
715 my $self = $class->SUPER::new ( 769 $self = $class->SUPER::new (
716 bg => [1, 1, 1, 1], 770 vp => (new CFClient::UI::ViewPort),
717 border_bg => [1, 1, 1, 1], 771 slider => $slider,
718 border => 0.8,
719 @_ 772 @_,
720 ); 773 );
721 774
775 $self->{vp}->add ($self->{scrolled});
776 $self->add ($self->{vp});
777 $self->add ($self->{slider});
778
722 $self 779 $self
723} 780}
724 781
725sub _draw { 782#TODO# update range on size_allocate depeneing on child
726 my ($self) = @_; 783# update viewport offset on scroll
727 784
728 my ($w, $h) = ($self->{w}, $self->{h}); 785#############################################################################
729 786
730 glEnable GL_BLEND; 787package CFClient::UI::Frame;
731 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
732 glEnable GL_TEXTURE_2D;
733 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
734 788
735# glBegin GL_QUADS; 789our @ISA = CFClient::UI::Bin::;
736# glColor 0, 0, 0, 0;
737# glVertex 0 , 0;
738# glVertex 0 , $h;
739# glVertex $w, $h;
740# glVertex $w, 0;
741# glEnd;
742 790
743 791use CFClient::OpenGL;
744 $self->child->draw;
745 glDisable GL_BLEND;
746 glDisable GL_TEXTURE_2D;
747}
748 792
749############################################################################# 793#############################################################################
750 794
751package CFClient::UI::FancyFrame; 795package CFClient::UI::FancyFrame;
752 796
764 # TODO: user_x, user_y, overwrite moveto? 808 # TODO: user_x, user_y, overwrite moveto?
765 809
766 my $self = $class->SUPER::new ( 810 my $self = $class->SUPER::new (
767 bg => [1, 1, 1, 1], 811 bg => [1, 1, 1, 1],
768 border_bg => [1, 1, 1, 1], 812 border_bg => [1, 1, 1, 1],
769 border => 0.8, 813 border => 0.6,
770 can_events => 1, 814 can_events => 1,
771 @_ 815 @_
772 ); 816 );
773 817
774 $self->{title} &&= new CFClient::UI::Label 818 $self->{title} &&= new CFClient::UI::Label
775 align => 0, 819 align => 0,
776 valign => 1, 820 valign => 1,
777 text => $self->{title}, 821 text => $self->{title},
778 fontsize => 1; 822 fontsize => $self->{border};
779 823
780 $self 824 $self
781} 825}
782 826
783sub border { 827sub border {
808} 852}
809 853
810sub button_down { 854sub button_down {
811 my ($self, $ev, $x, $y) = @_; 855 my ($self, $ev, $x, $y) = @_;
812 856
857 my ($w, $h) = @$self{qw(w h)};
813 my $border = $self->border; 858 my $border = $self->border;
814 859
815 if ($x < $self->{w} && $x >= $self->{w} - $border 860 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
816 && $y < $self->{h} && $y >= $self->{h} - $border) { 861 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
817 862
863 if ($lr & $td) {
864 my ($wx, $wy) = ($self->{x}, $self->{y});
818 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 865 my ($ox, $oy) = ($ev->{x}, $ev->{y});
819 my ($bw, $bh) = ($self->{w}, $self->{h}); 866 my ($bw, $bh) = ($self->{w}, $self->{h});
820 867
868 my $mx = $x < $border;
869 my $my = $y < $border;
870
821 $self->{motion} = sub { 871 $self->{motion} = sub {
822 my ($ev, $x, $y) = @_; 872 my ($ev, $x, $y) = @_;
823 873
824 ($x, $y) = ($ev->{x}, $ev->{y}); 874 my $dx = $ev->{x} - $ox;
875 my $dy = $ev->{y} - $oy;
825 876
826 $self->{user_w} = $bw + $x - $ox; 877 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
827 $self->{user_h} = $bh + $y - $oy; 878 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
879 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
828 $self->check_size; 880 $self->check_size;
829 }; 881 };
830 882
831 } elsif ($x >= 0 && $x < $self->{w} 883 } elsif ($lr ^ $td) {
832 && $y >= 0 && $y < $border) {
833
834 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 884 my ($ox, $oy) = ($ev->{x}, $ev->{y});
835 my ($bx, $by) = ($self->{x}, $self->{y}); 885 my ($bx, $by) = ($self->{x}, $self->{y});
836 886
837 $self->{motion} = sub { 887 $self->{motion} = sub {
838 my ($ev, $x, $y) = @_; 888 my ($ev, $x, $y) = @_;
861 my ($self) = @_; 911 my ($self) = @_;
862 912
863 my ($w, $h ) = ($self->{w}, $self->{h}); 913 my ($w, $h ) = ($self->{w}, $self->{h});
864 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 914 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
865 915
866 glEnable GL_BLEND;
867 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
868 glEnable GL_TEXTURE_2D; 916 glEnable GL_TEXTURE_2D;
869 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
870 918
871 my $border = $self->border; 919 my $border = $self->border;
872 920
873 glColor @{ $self->{border_bg} }; 921 glColor @{ $self->{border_bg} };
874 $tex[1]->draw_quad (0, 0, $w, $border); 922 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
875 $tex[3]->draw_quad (0, $border, $border, $ch); 923 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 924 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
877 $tex[4]->draw_quad (0, $h - $border, $w, $border); 925 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
878 926
927 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
879 my $bg = $tex[0]; 928 my $bg = $tex[0];
880 929
881 # TODO: repeat texture not scale 930 # TODO: repeat texture not scale
882 my $rep_x = $cw / $bg->{w}; 931 my $rep_x = $cw / $bg->{w};
883 my $rep_y = $ch / $bg->{h}; 932 my $rep_y = $ch / $bg->{h};
884 933
885 glColor @{ $self->{bg} }; 934 glColor @{ $self->{bg} };
886 935
887 $bg->{s} = $rep_x; 936 $bg->{s} = $rep_x;
888 $bg->{t} = $rep_y; 937 $bg->{t} = $rep_y;
889 $bg->{wrap_mode} = 1; 938 $bg->{wrap_mode} = 1;
890 $bg->draw_quad ($border, $border, $cw, $ch); 939 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
940 }
891 941
892 glDisable GL_TEXTURE_2D; 942 glDisable GL_TEXTURE_2D;
893 glDisable GL_BLEND;
894 943
895 $self->{title}->draw if $self->{title}; 944 $self->{title}->draw if $self->{title};
945
896 $self->child->draw; 946 $self->child->draw;
897} 947}
898 948
899############################################################################# 949#############################################################################
900 950
1124} 1174}
1125 1175
1126sub size_allocate { 1176sub size_allocate {
1127 my ($self, $w, $h) = @_; 1177 my ($self, $w, $h) = @_;
1128 1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1129 my $children = $self->{children}; 1181 my $children = $self->{children};
1130 1182
1131 my @h = map $_->{req_h}, @$children; 1183 my @h = map $_->{req_h}, @$children;
1132 1184
1133 my $req_h = List::Util::sum @h; 1185 my $req_h = List::Util::sum @h;
1172 my ($class, %arg) = @_; 1224 my ($class, %arg) = @_;
1173 1225
1174 my $self = $class->SUPER::new ( 1226 my $self = $class->SUPER::new (
1175 fg => [1, 1, 1], 1227 fg => [1, 1, 1],
1176 #font => default_font 1228 #font => default_font
1229 #text => initial text
1230 #markup => initial narkup
1231 layout => (new CFClient::Layout),
1177 fontsize => 1, 1232 fontsize => 1,
1178 text => "",
1179 align => -1, 1233 align => -1,
1180 valign => -1, 1234 valign => -1,
1181 padding => 2, 1235 padding => 2,
1182 layout => new CFClient::Layout,
1183 can_events => 0, 1236 can_events => 0,
1184 %arg 1237 %arg
1185 ); 1238 );
1186 1239
1187 if (exists $self->{template}) { 1240 if (exists $self->{template}) {
1188 my $layout = new CFClient::Layout; 1241 my $layout = new CFClient::Layout;
1189 $layout->set_text (delete $self->{template}); 1242 $layout->set_text (delete $self->{template});
1190 $self->{template} = $layout; 1243 $self->{template} = $layout;
1191 } 1244 }
1192 1245
1193 $self->set_text (delete $self->{text}) if exists $self->{text}; 1246 if (exists $self->{markup}) {
1194 $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 }
1195 1251
1196 $self 1252 $self
1197} 1253}
1198 1254
1199sub escape { 1255sub escape {
1204 s/</&lt;/g; 1260 s/</&lt;/g;
1205 1261
1206 $_[1] 1262 $_[1]
1207} 1263}
1208 1264
1265sub update {
1266 my ($self) = @_;
1267
1268 delete $self->{texture};
1269 $self->SUPER::update;
1270}
1271
1209sub set_text { 1272sub set_text {
1210 my ($self, $text) = @_; 1273 my ($self, $text) = @_;
1211 1274
1275 return if $self->{text} eq "T$text";
1276 $self->{text} = "T$text";
1277
1278 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1212 $self->{layout}->set_text ($text); 1279 $self->{layout}->set_text ($text);
1213 1280
1214 delete $self->{texture}; 1281 $self->update;
1215 $self->check_size; 1282 $self->check_size;
1216 $self->update;
1217} 1283}
1218 1284
1219sub set_markup { 1285sub set_markup {
1220 my ($self, $markup) = @_; 1286 my ($self, $markup) = @_;
1221 1287
1288 return if $self->{text} eq "M$markup";
1289 $self->{text} = "M$markup";
1290
1291 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1292
1293 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1222 $self->{layout}->set_markup ($markup); 1294 $self->{layout}->set_markup ($markup);
1223 1295
1224 delete $self->{texture}; 1296 $self->update;
1225 $self->check_size; 1297 $self->check_size;
1226 $self->update;
1227} 1298}
1228 1299
1229sub size_request { 1300sub size_request {
1230 my ($self) = @_; 1301 my ($self) = @_;
1231 1302
1260sub set_fontsize { 1331sub set_fontsize {
1261 my ($self, $fontsize) = @_; 1332 my ($self, $fontsize) = @_;
1262 1333
1263 $self->{fontsize} = $fontsize; 1334 $self->{fontsize} = $fontsize;
1264 delete $self->{texture}; 1335 delete $self->{texture};
1336
1337 $self->update;
1265 $self->check_size; 1338 $self->check_size;
1266 $self->update;
1267} 1339}
1268 1340
1269sub _draw { 1341sub _draw {
1270 my ($self) = @_; 1342 my ($self) = @_;
1271 1343
1272 my $tex = $self->{texture} ||= do { 1344 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}});
1273 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1346 $self->{layout}->set_font ($self->{font}) if $self->{font};
1274 $self->{layout}->set_width ($self->{w}); 1347 $self->{layout}->set_width ($self->{w});
1275 $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
1276 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
1277 }; 1361 };
1278 1362
1279 glEnable GL_BLEND;
1280 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1281 glEnable GL_TEXTURE_2D; 1363 glEnable GL_TEXTURE_2D;
1282 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1364 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1283 1365
1366 if ($tex->{format} == GL_ALPHA) {
1284 glColor @{$self->{fg}}; 1367 glColor @{$self->{fg}};
1285
1286 $self->{ox} = int (
1287 $self->{align} < 0 ? $self->{padding}
1288 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1289 : ($self->{w} - $tex->{w}) * 0.5
1290 );
1291
1292 $self->{oy} = int (
1293 $self->{valign} < 0 ? $self->{padding}
1294 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1295 : ($self->{h} - $tex->{h}) * 0.5
1296 );
1297
1298 $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 }
1299 1372
1300 glDisable GL_TEXTURE_2D; 1373 glDisable GL_TEXTURE_2D;
1301 glDisable GL_BLEND;
1302} 1374}
1303 1375
1304############################################################################# 1376#############################################################################
1305 1377
1306package CFClient::UI::EntryBase; 1378package CFClient::UI::EntryBase;
1341 $self->{layout}->set_text ("$text "); 1413 $self->{layout}->set_text ("$text ");
1342 1414
1343 $self->emit (changed => $self->{text}); 1415 $self->emit (changed => $self->{text});
1344} 1416}
1345 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
1346sub get_text { 1427sub get_text {
1347 $_[0]{text} 1428 $_[0]{text}
1348} 1429}
1349 1430
1350sub size_request { 1431sub size_request {
1357 1438
1358sub size_allocate { 1439sub size_allocate {
1359 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1360 1441
1361 $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
1362}
1363
1364sub set_text {
1365 my ($self, $text) = @_;
1366
1367 $self->{cursor} = length $text;
1368 $self->_set_text ($text);
1369 $self->update;
1370} 1443}
1371 1444
1372sub key_down { 1445sub key_down {
1373 my ($self, $ev) = @_; 1446 my ($self, $ev) = @_;
1374 1447
1562 1635
1563 if ($GRAB == $self) { 1636 if ($GRAB == $self) {
1564 $self->{fg} = $self->{active_fg}; 1637 $self->{fg} = $self->{active_fg};
1565 } 1638 }
1566 1639
1567 glEnable GL_BLEND;
1568 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1569 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1570 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1571 glColor 0, 0, 0, 1; 1642 glColor 0, 0, 0, 1;
1572 1643
1573 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1644 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1574 1645
1575 glDisable GL_TEXTURE_2D; 1646 glDisable GL_TEXTURE_2D;
1576 glDisable GL_BLEND;
1577 1647
1578 $self->SUPER::_draw; 1648 $self->SUPER::_draw;
1579} 1649}
1580 1650
1581############################################################################# 1651#############################################################################
1628 1698
1629 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;
1630 1700
1631 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1701 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1632 1702
1633 glEnable GL_BLEND; 1703 my $tex = $self->{state} ? $tex[1] : $tex[0];
1704
1634 glEnable GL_TEXTURE_2D; 1705 glEnable GL_TEXTURE_2D;
1635 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1636
1637 my $tex = $self->{state} ? $tex[1] : $tex[0];
1638
1639 $tex->draw_quad (0, 0, $s, $s); 1706 $tex->draw_quad_alpha (0, 0, $s, $s);
1640
1641 glDisable GL_TEXTURE_2D; 1707 glDisable GL_TEXTURE_2D;
1642 glDisable GL_BLEND;
1643} 1708}
1644 1709
1645############################################################################# 1710#############################################################################
1646 1711
1647package CFClient::UI::Image; 1712package CFClient::UI::Image;
1690 glTranslate 0, -$self->{w}, 0; 1755 glTranslate 0, -$self->{w}, 0;
1691 1756
1692 ($w, $h) = ($h, $w); 1757 ($w, $h) = ($h, $w);
1693 } 1758 }
1694 1759
1695 glEnable GL_BLEND;
1696 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1697 glEnable GL_TEXTURE_2D; 1760 glEnable GL_TEXTURE_2D;
1698 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1699 1762
1700 $tex->draw_quad (0, 0, $w, $h); 1763 $tex->draw_quad_alpha (0, 0, $w, $h);
1701 1764
1702 glDisable GL_BLEND;
1703 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1704} 1766}
1705 1767
1706############################################################################# 1768#############################################################################
1707 1769
1755} 1817}
1756 1818
1757sub set_max { 1819sub set_max {
1758 my ($self, $max) = @_; 1820 my ($self, $max) = @_;
1759 1821
1822 return if $self->{max_val} == $max;
1823
1760 $self->{max_val} = $max; 1824 $self->{max_val} = $max;
1825 $self->update;
1761} 1826}
1762 1827
1763sub set_value { 1828sub set_value {
1764 my ($self, $val, $max) = @_; 1829 my ($self, $val, $max) = @_;
1765 1830
1766 $self->set_max ($max) 1831 $self->set_max ($max)
1767 if defined $max; 1832 if defined $max;
1768 1833
1769 $max = $self->{max_val}; 1834 return if $self->{val} == $val;
1835
1770 $self->{val} = $val; 1836 $self->{val} = $val;
1771
1772 $self->update; 1837 $self->update;
1773} 1838}
1774 1839
1775sub _draw { 1840sub _draw {
1776 my ($self) = @_; 1841 my ($self) = @_;
1859 1924
1860 $self->{value}->set_fontsize ($fsize); 1925 $self->{value}->set_fontsize ($fsize);
1861 $self->{max} ->set_fontsize ($fsize); 1926 $self->{max} ->set_fontsize ($fsize);
1862} 1927}
1863 1928
1929sub set_max {
1930 my ($self, $max) = @_;
1931
1932 $self->{gauge}->set_max ($max);
1933 $self->{max}->set_text ($max);
1934}
1935
1864sub set_value { 1936sub set_value {
1865 my ($self, $val, $max) = @_; 1937 my ($self, $val, $max) = @_;
1866 1938
1867 $self->set_max ($max) 1939 $self->set_max ($max)
1868 if defined $max; 1940 if defined $max;
1869 1941
1870 $self->{gauge}->set_value ($val, $max); 1942 $self->{gauge}->set_value ($val, $max);
1871 $self->{value}->set_text ($val); 1943 $self->{value}->set_text ($val);
1872}
1873
1874sub set_max {
1875 my ($self, $max) = @_;
1876
1877 $self->{gauge}->set_max ($max);
1878 $self->{max}->set_text ($max);
1879} 1944}
1880 1945
1881############################################################################# 1946#############################################################################
1882 1947
1883package CFClient::UI::Slider; 1948package CFClient::UI::Slider;
1902 # TODO: calculations are off 1967 # TODO: calculations are off
1903 my $self = $class->SUPER::new ( 1968 my $self = $class->SUPER::new (
1904 fg => [1, 1, 1], 1969 fg => [1, 1, 1],
1905 active_fg => [0, 0, 0], 1970 active_fg => [0, 0, 0],
1906 range => [0, 0, 100, 10], 1971 range => [0, 0, 100, 10],
1907 req_w => 20, 1972 req_w => $::WIDTH / 80,
1908 req_h => 20, 1973 req_h => $::WIDTH / 80,
1909 vertical => 0, 1974 vertical => 0,
1910 can_hover => 1, 1975 can_hover => 1,
1911 inner_pad => 5, 1976 inner_pad => 5,
1912 @_ 1977 @_
1913 ); 1978 );
1996 $page ||= 2; 2061 $page ||= 2;
1997 2062
1998 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1999 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2000 2065
2001 glEnable GL_BLEND;
2002 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2003 glEnable GL_TEXTURE_2D; 2066 glEnable GL_TEXTURE_2D;
2004 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2005 2068
2006 # draw background 2069 # draw background
2007 $tex[1]->draw_quad (0, 0, $w, $h); 2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2008 2071
2009 # draw handle 2072 # draw handle
2010 $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);
2011 2074
2012 glDisable GL_BLEND;
2013 glDisable GL_TEXTURE_2D; 2075 glDisable GL_TEXTURE_2D;
2014} 2076}
2015 2077
2016############################################################################# 2078#############################################################################
2017 2079
2028 fontsize => 1, 2090 fontsize => 1,
2029 can_events => 0, 2091 can_events => 0,
2030 #font => default_font 2092 #font => default_font
2031 @_, 2093 @_,
2032 2094
2033 layout => (new CFClient::Layout), 2095 layout => (new CFClient::Layout 1),
2034 par => [], 2096 par => [],
2035 height => 0, 2097 height => 0,
2036 children => [ 2098 children => [
2037 (new CFClient::UI::Empty expand => 1), 2099 (new CFClient::UI::Empty expand => 1),
2038 (new CFClient::UI::Slider vertical => 1), 2100 (new CFClient::UI::Slider vertical => 1),
2039 ], 2101 ],
2040 ); 2102 );
2041 2103
2042 $self->{children}[1]->connect (changed => sub { 2104 $self->{children}[1]->connect (changed => sub { $self->update });
2043 $self->update;
2044 });
2045 2105
2046 $self 2106 $self
2047} 2107}
2048 2108
2049sub set_fontsize { 2109sub set_fontsize {
2057 my ($self, $text) = @_; 2117 my ($self, $text) = @_;
2058 2118
2059 my $layout = $self->{layout}; 2119 my $layout = $self->{layout};
2060 2120
2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2062 $layout->set_width ($self->{w}); 2122 $layout->set_width ($self->{children}[0]{w});
2063 $layout->set_text ($text); 2123 $layout->set_markup ($text);
2064 2124
2065 ($layout->size)[1] 2125 ($layout->size)[1]
2066} 2126}
2067 2127
2068sub reflow { 2128sub reflow {
2106 2166
2107 return unless $self->{h} > 0; 2167 return unless $self->{h} > 0;
2108 2168
2109 delete $self->{texture}; 2169 delete $self->{texture};
2110 2170
2111 $ROOT->on_refresh ($self, sub { 2171 $ROOT->on_post_alloc ($self, sub {
2112 if (delete $self->{need_reflow}) { 2172 if (delete $self->{need_reflow}) {
2113 my $height = 0; 2173 my $height = 0;
2114 2174
2115 $height += $_->[0] = $self->text_height ($_->[2]) 2175 $height += $_->[0] = $self->text_height ($_->[2])
2116 for @{$self->{par}}; 2176 for @{$self->{par}};
2120 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2180 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2121 2181
2122 delete $self->{texture}; 2182 delete $self->{texture};
2123 } 2183 }
2124 2184
2125 $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 {
2126 glClearColor 0, 0, 0, 1; 2186 glClearColor 0, 0, 0, 0;
2127 glClear GL_COLOR_BUFFER_BIT; 2187 glClear GL_COLOR_BUFFER_BIT;
2128 2188
2129 glEnable GL_BLEND;
2130 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2131 glEnable GL_TEXTURE_2D; 2189 glEnable GL_TEXTURE_2D;
2132 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2133 2191
2134 my $top = int $self->{children}[1]{range}[0]; 2192 my $top = int $self->{children}[1]{range}[0];
2135 2193
2144 2202
2145 for my $par (@{$self->{par}}) { 2203 for my $par (@{$self->{par}}) {
2146 my $h = $par->[0]; 2204 my $h = $par->[0];
2147 2205
2148 if ($y0 < $y + $h && $y < $y1) { 2206 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] });
2149 $layout->set_text ($par->[2]); 2208 $layout->set_markup ($par->[2]);
2150 2209
2151 glColor @{ $par->[1] };
2152 my ($W, $H) = $layout->size; 2210 my ($W, $H) = $layout->size;
2153 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);
2154 } 2212 }
2155 2213
2156 $y += $h; 2214 $y += $h;
2157 } 2215 }
2158 2216
2159 glDisable GL_TEXTURE_2D; 2217 glDisable GL_TEXTURE_2D;
2160 glDisable GL_BLEND;
2161 }; 2218 };
2162 }); 2219 });
2163} 2220}
2164 2221
2165sub _draw { 2222sub _draw {
2166 my ($self) = @_; 2223 my ($self) = @_;
2167 2224
2168 if ($self->{texture}) {
2169 glEnable GL_TEXTURE_2D; 2225 glEnable GL_TEXTURE_2D;
2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2171 glColor 1, 1, 1, 1; 2227 glColor 1, 1, 1, 1;
2172 $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});
2173 glDisable GL_TEXTURE_2D; 2229 glDisable GL_TEXTURE_2D;
2174 }
2175 2230
2176 $self->{children}[1]->draw; 2231 $self->{children}[1]->draw;
2177 2232
2178} 2233}
2179 2234
2273 @_, 2328 @_,
2274 can_events => 0, 2329 can_events => 0,
2275 ) 2330 )
2276} 2331}
2277 2332
2278sub set_markup { 2333sub set_tooltip_from {
2279 my ($self, $text) = @_; 2334 my ($self, $widget) = @_;
2280 2335
2281 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2336 $self->add (new CFClient::UI::Label
2282 $self->{label}->set_markup ($text); 2337 markup => $widget->{tooltip},
2283 $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 );
2284} 2343}
2285 2344
2286sub size_request { 2345sub size_request {
2287 my ($self) = @_; 2346 my ($self) = @_;
2288
2289 $self->child->set_max_size ($::WIDTH * 0.3);
2290 2347
2291 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2348 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2292 2349
2293 ($w + 4, $h + 4) 2350 ($w + 4, $h + 4)
2294} 2351}
2300} 2357}
2301 2358
2302sub _draw { 2359sub _draw {
2303 my ($self) = @_; 2360 my ($self) = @_;
2304 2361
2305 glPushMatrix;
2306 glTranslate 0.375, 0.375; 2362 glTranslate 0.375, 0.375;
2307 2363
2308 my ($w, $h) = @$self{qw(w h)}; 2364 my ($w, $h) = @$self{qw(w h)};
2309 2365
2310 glColor 1, 0.8, 0.4; 2366 glColor 1, 0.8, 0.4;
2321 glVertex 0 , $h; 2377 glVertex 0 , $h;
2322 glVertex $w, $h; 2378 glVertex $w, $h;
2323 glVertex $w, 0; 2379 glVertex $w, 0;
2324 glEnd; 2380 glEnd;
2325 2381
2326 glPopMatrix; 2382 glTranslate 2 - 0.375, 2 - 0.375;
2327
2328 glTranslate 2, 2;
2329 $self->SUPER::_draw; 2383 $self->SUPER::_draw;
2330} 2384}
2331 2385
2332############################################################################# 2386#############################################################################
2333 2387
2348 2402
2349sub size_request { 2403sub size_request {
2350 (32, 8) 2404 (32, 8)
2351} 2405}
2352 2406
2353sub draw { 2407sub _draw {
2354 my ($self) = @_; 2408 my ($self) = @_;
2355 2409
2410 return unless $::CONN;#d# manage and cache textures differently
2356 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2357 2412
2413 # TODO animation
2358 if ($tex) { 2414 if ($tex) {
2359 glEnable GL_BLEND;
2360 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2361 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2362 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2363 glColor 1, 1, 1, 1; 2417 glColor 1, 1, 1, 1;
2364 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2365 glDisable GL_TEXTURE_2D; 2419 glDisable GL_TEXTURE_2D;
2366 glDisable GL_BLEND;
2367 } 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;
2368} 2693}
2369 2694
2370############################################################################# 2695#############################################################################
2371 2696
2372package CFClient::UI::Root; 2697package CFClient::UI::Root;
2373 2698
2374our @ISA = CFClient::UI::Container::; 2699our @ISA = CFClient::UI::Container::;
2375 2700
2376use CFClient::OpenGL; 2701use CFClient::OpenGL;
2377 2702
2378sub check_size { 2703sub new {
2379 my ($self) = @_; 2704 my $class = shift;
2380 2705
2381 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2706 $class->SUPER::new (
2382} 2707 @_,
2383 2708 )
2384sub size_request {
2385 ($::WIDTH, $::HEIGHT)
2386} 2709}
2387 2710
2388sub configure { 2711sub configure {
2389 my ($self, $x, $y, $w, $h) = @_; 2712 my ($self, $x, $y, $w, $h) = @_;
2390 2713
2391 $self->SUPER::configure ($x, $y, $w, $h); 2714 $self->{w} = $w;
2715 $self->{h} = $h;
2716}
2392 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
2393 for my $child (@{$self->{children}}) { 2750 for my $child ($self->children) {
2394 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)};
2395 2752
2396 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2753 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2397 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2398 $child->configure ($X, $Y, $W,$H); 2755 $child->configure ($X, $Y, $W, $H);
2399 } 2756 }
2400}
2401 2757
2402sub _topleft { 2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760}
2761
2762sub coord2local {
2403 my ($self, $x, $y) = @_; 2763 my ($self, $x, $y) = @_;
2404 2764
2405 ($x, $y) 2765 ($x, $y)
2406} 2766}
2407 2767
2768sub coord2global {
2769 my ($self, $x, $y) = @_;
2770
2771 ($x, $y)
2772}
2773
2408sub update { 2774sub update {
2409 my ($self) = @_; 2775 my ($self) = @_;
2410 2776
2411 $self->check_size; 2777 $self->check_size;
2412 ::refresh (); 2778 $::WANT_REFRESH++;
2413} 2779}
2414 2780
2415sub add { 2781sub add {
2416 my ($self, $child) = @_; 2782 my ($self, $child) = @_;
2417 2783
2418 # integerize window positions 2784 # integerise window positions
2419 $child->{x} = int $child->{x}; 2785 $child->{x} = int $child->{x};
2420 $child->{y} = int $child->{y}; 2786 $child->{y} = int $child->{y};
2421 2787
2422 $self->SUPER::add ($child); 2788 $self->SUPER::add ($child);
2423} 2789}
2426 my ($self, $id, $cb) = @_; 2792 my ($self, $id, $cb) = @_;
2427 2793
2428 $self->{refresh_hook}{$id} = $cb; 2794 $self->{refresh_hook}{$id} = $cb;
2429} 2795}
2430 2796
2797sub on_post_alloc {
2798 my ($self, $id, $cb) = @_;
2799
2800 $self->{post_alloc_hook}{$id} = $cb;
2801}
2802
2431sub draw { 2803sub draw {
2432 my ($self) = @_; 2804 my ($self) = @_;
2433 2805
2434 while (my $rcb = delete $self->{refresh_hook}) { 2806 while ($self->{refresh_hook}) {
2435 $_->() 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}};
2436 } 2859 }
2437 2860
2438 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2861 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2439 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2440 glClear GL_COLOR_BUFFER_BIT; 2863 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines