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.169 by elmex, Mon Apr 24 10:43:39 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
247sub children {
248}
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
241sub set_max_size { 261sub set_max_size {
242 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
243 263
244 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
245 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 delete $self->{max_h}; $self->{max_h} = $h if $h;
246}
247
248# return top left coordinates
249sub _topleft {
250 my ($self, $x, $y) = @_;
251
252 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
253} 266}
254 267
255# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
256sub coord2local { 269sub coord2local {
257 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
258 271
259 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
260 ($x - $X, $y - $Y)
261} 273}
262 274
263# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
264sub coord2global { 276sub coord2global {
265 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
266 278
267 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
268 ($x + $X, $y + $Y)
269} 280}
270 281
271sub focus_in { 282sub focus_in {
272 my ($self) = @_; 283 my ($self) = @_;
273 284
371 382
372sub set_parent { 383sub set_parent {
373 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
374 385
375 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};
376} 392}
377 393
378sub check_size { 394sub check_size {
379 my ($self) = @_; 395 my ($self, $forced) = @_;
380 396
381 $self->{parent} 397 $self->{force_alloc} = 1 if $forced;
382 or return 1; 398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
383
384 my ($w, $h) = $self->{user_w} && $self->{user_h}
385 ? @$self{qw(user_w user_h)}
386 : $self->size_request;
387
388 if ($w != $self->{req_w} || $h != $self->{req_h}) {
389 $self->{req_w} = $w;
390 $self->{req_h} = $h;
391
392 $self->{parent}->check_size
393 or $self->size_allocate (
394 (List::Util::max $self->{w}, $w),
395 (List::Util::max $self->{h}, $h),
396 );
397
398 1
399 } else {
400 0
401 }
402} 399}
403 400
404sub update { 401sub update {
405 my ($self) = @_; 402 my ($self) = @_;
406 403
503 500
504 $self 501 $self
505} 502}
506 503
507sub add { 504sub add {
508 my ($self, $child) = @_; 505 my ($self, @widgets) = @_;
509 506
510 $child->set_parent ($self); 507 $_->set_parent ($self)
508 for @widgets;
511 509
512 use sort 'stable'; 510 use sort 'stable';
513 511
514 $self->{children} = [ 512 $self->{children} = [
515 sort { $a->{z} <=> $b->{z} } 513 sort { $a->{z} <=> $b->{z} }
516 @{$self->{children}}, $child 514 @{$self->{children}}, @widgets
517 ]; 515 ];
518 516
519 $child->check_size; 517 $self->check_size (1);
518 $self->update;
519}
520
521sub children {
522 @{ $_[0]{children} }
520} 523}
521 524
522sub remove { 525sub remove {
523 my ($self, $child) = @_; 526 my ($self, $child) = @_;
524 527
539 542
540 for (@$children) { 543 for (@$children) {
541 delete $_->{parent}; 544 delete $_->{parent};
542 $_->hide; 545 $_->hide;
543 } 546 }
547
548 $self->check_size;
549 $self->update;
544} 550}
545 551
546sub find_widget { 552sub find_widget {
547 my ($self, $x, $y) = @_; 553 my ($self, $x, $y) = @_;
548 554
623} 629}
624 630
625sub update { 631sub update {
626 my ($self) = @_; 632 my ($self) = @_;
627 633
628 # we want to do this delayed... 634 $ROOT->on_post_alloc ($self => sub { $self->render_child });
629 $self->render_chld;
630 $self->SUPER::update; 635 $self->SUPER::update;
631}
632
633sub render_chld {
634 my ($self) = @_;
635
636 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
637 glClearColor 0, 0, 0, 1;
638 glClear GL_COLOR_BUFFER_BIT;
639 $self->child->draw;
640 };
641} 636}
642 637
643sub size_allocate { 638sub size_allocate {
644 my ($self, $w, $h) = @_; 639 my ($self, $w, $h) = @_;
645 640
646 $self->child->configure (0, 0, $w, $h); 641 $self->SUPER::size_allocate ($w, $h);
642 $self->update;
643}
647 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
648 $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 };
649} 665}
650 666
651sub _draw { 667sub _draw {
652 my ($self) = @_; 668 my ($self) = @_;
653 669
654 my ($w, $h) = ($self->w, $self->h); 670 my ($w, $h) = ($self->w, $self->h);
655 671
656 my $tex = $self->{texture} 672 my $tex = $self->{texture}
657 or return; 673 or return;
658 674
659 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 675 glEnable GL_TEXTURE_2D;
662 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;
663 678
664 $tex->draw_quad (0, 0, $w, $h); 679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
665 680
666 glDisable GL_BLEND;
667 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
668} 682}
669 683
670############################################################################# 684#############################################################################
671 685
672package CFClient::UI::ViewPort; 686package CFClient::UI::ViewPort;
673 687
674our @ISA = CFClient::UI::Window::; 688our @ISA = CFClient::UI::Window::;
675 689
676sub new { die }
677
678sub size_request { 690sub size_request {
679 my ($self) = @_; 691 my ($self) = @_;
680 692
681 @$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)};
682 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 694 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
683 695
684 @$self{qw(child_w child_h)} 696 @$self{qw(child_w child_h)}
685} 697}
686 698
687sub _draw { 699sub size_allocate {
688 my ($self) = @_; 700 my ($self, $w, $h) = @_;
689 701
690 $self->{children}[1]->draw; 702 $self->update;
691} 703}
692 704
705sub set_offset {
706 my ($self, $x, $y) = @_;
693 707
694############################################################################# 708 $self->{view_x} = int $x;
709 $self->{view_y} = int $y;
695 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
696package CFClient::UI::Frame; 752package CFClient::UI::ScrolledWindow;
697 753
698our @ISA = CFClient::UI::Bin::; 754our @ISA = CFClient::UI::HBox::;
699
700use CFClient::OpenGL;
701 755
702sub new { 756sub new {
703 my $class = shift; 757 my $class = shift;
704 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
705 my $self = $class->SUPER::new ( 769 $self = $class->SUPER::new (
706 bg => [1, 1, 1, 1], 770 vp => (new CFClient::UI::ViewPort),
707 border_bg => [1, 1, 1, 1], 771 slider => $slider,
708 border => 0.8,
709 @_ 772 @_,
710 ); 773 );
711 774
775 $self->{vp}->add ($self->{scrolled});
776 $self->add ($self->{vp});
777 $self->add ($self->{slider});
778
712 $self 779 $self
713} 780}
714 781
715sub _draw { 782#TODO# update range on size_allocate depeneing on child
716 my ($self) = @_; 783# update viewport offset on scroll
717 784
718 my ($w, $h) = ($self->{w}, $self->{h}); 785#############################################################################
719 786
720 glEnable GL_BLEND; 787package CFClient::UI::Frame;
721 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
722 glEnable GL_TEXTURE_2D;
723 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
724 788
725# glBegin GL_QUADS; 789our @ISA = CFClient::UI::Bin::;
726# glColor 0, 0, 0, 0;
727# glVertex 0 , 0;
728# glVertex 0 , $h;
729# glVertex $w, $h;
730# glVertex $w, 0;
731# glEnd;
732 790
733 791use CFClient::OpenGL;
734 $self->child->draw;
735 glDisable GL_BLEND;
736 glDisable GL_TEXTURE_2D;
737}
738 792
739############################################################################# 793#############################################################################
740 794
741package CFClient::UI::FancyFrame; 795package CFClient::UI::FancyFrame;
742 796
754 # TODO: user_x, user_y, overwrite moveto? 808 # TODO: user_x, user_y, overwrite moveto?
755 809
756 my $self = $class->SUPER::new ( 810 my $self = $class->SUPER::new (
757 bg => [1, 1, 1, 1], 811 bg => [1, 1, 1, 1],
758 border_bg => [1, 1, 1, 1], 812 border_bg => [1, 1, 1, 1],
759 border => 0.8, 813 border => 0.6,
760 can_events => 1, 814 can_events => 1,
761 @_ 815 @_
762 ); 816 );
763 817
764 $self->{title} &&= new CFClient::UI::Label 818 $self->{title} &&= new CFClient::UI::Label
765 align => 0, 819 align => 0,
766 valign => 1, 820 valign => 1,
767 text => $self->{title}, 821 text => $self->{title},
768 fontsize => 1; 822 fontsize => $self->{border};
769 823
770 $self 824 $self
771} 825}
772 826
773sub border { 827sub border {
798} 852}
799 853
800sub button_down { 854sub button_down {
801 my ($self, $ev, $x, $y) = @_; 855 my ($self, $ev, $x, $y) = @_;
802 856
857 my ($w, $h) = @$self{qw(w h)};
803 my $border = $self->border; 858 my $border = $self->border;
804 859
805 if ($x < $self->{w} && $x >= $self->{w} - $border 860 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
806 && $y < $self->{h} && $y >= $self->{h} - $border) { 861 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
807 862
863 if ($lr & $td) {
864 my ($wx, $wy) = ($self->{x}, $self->{y});
808 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 865 my ($ox, $oy) = ($ev->{x}, $ev->{y});
809 my ($bw, $bh) = ($self->{w}, $self->{h}); 866 my ($bw, $bh) = ($self->{w}, $self->{h});
810 867
868 my $mx = $x < $border;
869 my $my = $y < $border;
870
811 $self->{motion} = sub { 871 $self->{motion} = sub {
812 my ($ev, $x, $y) = @_; 872 my ($ev, $x, $y) = @_;
813 873
814 ($x, $y) = ($ev->{x}, $ev->{y}); 874 my $dx = $ev->{x} - $ox;
875 my $dy = $ev->{y} - $oy;
815 876
816 $self->{user_w} = $bw + $x - $ox; 877 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
817 $self->{user_h} = $bh + $y - $oy; 878 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
879 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
818 $self->check_size; 880 $self->check_size;
819 }; 881 };
820 882
821 } elsif ($x >= 0 && $x < $self->{w} 883 } elsif ($lr ^ $td) {
822 && $y >= 0 && $y < $border) {
823
824 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 884 my ($ox, $oy) = ($ev->{x}, $ev->{y});
825 my ($bx, $by) = ($self->{x}, $self->{y}); 885 my ($bx, $by) = ($self->{x}, $self->{y});
826 886
827 $self->{motion} = sub { 887 $self->{motion} = sub {
828 my ($ev, $x, $y) = @_; 888 my ($ev, $x, $y) = @_;
851 my ($self) = @_; 911 my ($self) = @_;
852 912
853 my ($w, $h ) = ($self->{w}, $self->{h}); 913 my ($w, $h ) = ($self->{w}, $self->{h});
854 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 914 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
855 915
856 glEnable GL_BLEND;
857 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
858 glEnable GL_TEXTURE_2D; 916 glEnable GL_TEXTURE_2D;
859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
860 918
861 my $border = $self->border; 919 my $border = $self->border;
862 920
863 glColor @{ $self->{border_bg} }; 921 glColor @{ $self->{border_bg} };
864 $tex[1]->draw_quad (0, 0, $w, $border); 922 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
865 $tex[3]->draw_quad (0, $border, $border, $ch); 923 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
866 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 924 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
867 $tex[4]->draw_quad (0, $h - $border, $w, $border); 925 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
868 926
927 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
869 my $bg = $tex[0]; 928 my $bg = $tex[0];
870 929
871 # TODO: repeat texture not scale 930 # TODO: repeat texture not scale
872 my $rep_x = $cw / $bg->{w}; 931 my $rep_x = $cw / $bg->{w};
873 my $rep_y = $ch / $bg->{h}; 932 my $rep_y = $ch / $bg->{h};
874 933
875 glColor @{ $self->{bg} }; 934 glColor @{ $self->{bg} };
876 935
877 $bg->{s} = $rep_x; 936 $bg->{s} = $rep_x;
878 $bg->{t} = $rep_y; 937 $bg->{t} = $rep_y;
879 $bg->{wrap_mode} = 1; 938 $bg->{wrap_mode} = 1;
880 $bg->draw_quad ($border, $border, $cw, $ch); 939 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
940 }
881 941
882 glDisable GL_TEXTURE_2D; 942 glDisable GL_TEXTURE_2D;
883 glDisable GL_BLEND;
884 943
885 $self->{title}->draw if $self->{title}; 944 $self->{title}->draw if $self->{title};
945
886 $self->child->draw; 946 $self->child->draw;
887} 947}
888 948
889############################################################################# 949#############################################################################
890 950
912 $self->{children}[$y][$x] = $child; 972 $self->{children}[$y][$x] = $child;
913 973
914 $child->check_size; 974 $child->check_size;
915} 975}
916 976
977sub children {
978 grep $_, map @$_, grep $_, @{ $_[0]{children} }
979}
980
917# TODO: move to container class maybe? send childs a signal on removal? 981# TODO: move to container class maybe? send childs a signal on removal?
918sub clear { 982sub clear {
919 my ($self) = @_; 983 my ($self) = @_;
920 984
921 my $children = delete $self->{children}; 985 my @children = $self->children;
986 delete $self->{children};
922 987
923 for (grep $_, map @$_, grep $_, @$children) { 988 for (@children) {
924 delete $_->{parent}; 989 delete $_->{parent};
925 $_->hide; 990 $_->hide;
926 } 991 }
927 992
928 $self->update; 993 $self->update;
1109} 1174}
1110 1175
1111sub size_allocate { 1176sub size_allocate {
1112 my ($self, $w, $h) = @_; 1177 my ($self, $w, $h) = @_;
1113 1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1114 my $children = $self->{children}; 1181 my $children = $self->{children};
1115 1182
1116 my @h = map $_->{req_h}, @$children; 1183 my @h = map $_->{req_h}, @$children;
1117 1184
1118 my $req_h = List::Util::sum @h; 1185 my $req_h = List::Util::sum @h;
1157 my ($class, %arg) = @_; 1224 my ($class, %arg) = @_;
1158 1225
1159 my $self = $class->SUPER::new ( 1226 my $self = $class->SUPER::new (
1160 fg => [1, 1, 1], 1227 fg => [1, 1, 1],
1161 #font => default_font 1228 #font => default_font
1229 #text => initial text
1230 #markup => initial narkup
1231 layout => (new CFClient::Layout),
1162 fontsize => 1, 1232 fontsize => 1,
1163 text => "",
1164 align => -1, 1233 align => -1,
1165 valign => -1, 1234 valign => -1,
1166 padding => 2, 1235 padding => 2,
1167 layout => new CFClient::Layout,
1168 can_events => 0, 1236 can_events => 0,
1169 %arg 1237 %arg
1170 ); 1238 );
1171 1239
1172 if (exists $self->{template}) { 1240 if (exists $self->{template}) {
1173 my $layout = new CFClient::Layout; 1241 my $layout = new CFClient::Layout;
1174 $layout->set_text (delete $self->{template}); 1242 $layout->set_text (delete $self->{template});
1175 $self->{template} = $layout; 1243 $self->{template} = $layout;
1176 } 1244 }
1177 1245
1178 $self->set_text (delete $self->{text}) if exists $self->{text}; 1246 if (exists $self->{markup}) {
1179 $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 }
1180 1251
1181 $self 1252 $self
1182} 1253}
1183 1254
1184sub escape { 1255sub escape {
1189 s/</&lt;/g; 1260 s/</&lt;/g;
1190 1261
1191 $_[1] 1262 $_[1]
1192} 1263}
1193 1264
1265sub update {
1266 my ($self) = @_;
1267
1268 delete $self->{texture};
1269 $self->SUPER::update;
1270}
1271
1194sub set_text { 1272sub set_text {
1195 my ($self, $text) = @_; 1273 my ($self, $text) = @_;
1196 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;
1197 $self->{layout}->set_text ($text); 1279 $self->{layout}->set_text ($text);
1198 1280
1199 delete $self->{texture}; 1281 $self->update;
1200 $self->check_size; 1282 $self->check_size;
1201 $self->update;
1202} 1283}
1203 1284
1204sub set_markup { 1285sub set_markup {
1205 my ($self, $markup) = @_; 1286 my ($self, $markup) = @_;
1206 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;
1207 $self->{layout}->set_markup ($markup); 1294 $self->{layout}->set_markup ($markup);
1208 1295
1209 delete $self->{texture}; 1296 $self->update;
1210 $self->check_size; 1297 $self->check_size;
1211 $self->update;
1212} 1298}
1213 1299
1214sub size_request { 1300sub size_request {
1215 my ($self) = @_; 1301 my ($self) = @_;
1216 1302
1245sub set_fontsize { 1331sub set_fontsize {
1246 my ($self, $fontsize) = @_; 1332 my ($self, $fontsize) = @_;
1247 1333
1248 $self->{fontsize} = $fontsize; 1334 $self->{fontsize} = $fontsize;
1249 delete $self->{texture}; 1335 delete $self->{texture};
1336
1337 $self->update;
1250 $self->check_size; 1338 $self->check_size;
1251 $self->update;
1252} 1339}
1253 1340
1254sub _draw { 1341sub _draw {
1255 my ($self) = @_; 1342 my ($self) = @_;
1256 1343
1257 my $tex = $self->{texture} ||= do { 1344 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}});
1258 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1346 $self->{layout}->set_font ($self->{font}) if $self->{font};
1259 $self->{layout}->set_width ($self->{w}); 1347 $self->{layout}->set_width ($self->{w});
1260 $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
1261 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
1262 }; 1361 };
1263 1362
1264 glEnable GL_BLEND;
1265 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1266 glEnable GL_TEXTURE_2D; 1363 glEnable GL_TEXTURE_2D;
1267 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1364 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1268 1365
1366 if ($tex->{format} == GL_ALPHA) {
1269 glColor @{$self->{fg}}; 1367 glColor @{$self->{fg}};
1270
1271 $self->{ox} = int (
1272 $self->{align} < 0 ? $self->{padding}
1273 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1274 : ($self->{w} - $tex->{w}) * 0.5
1275 );
1276
1277 $self->{oy} = int (
1278 $self->{valign} < 0 ? $self->{padding}
1279 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1280 : ($self->{h} - $tex->{h}) * 0.5
1281 );
1282
1283 $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 }
1284 1372
1285 glDisable GL_TEXTURE_2D; 1373 glDisable GL_TEXTURE_2D;
1286 glDisable GL_BLEND;
1287} 1374}
1288 1375
1289############################################################################# 1376#############################################################################
1290 1377
1291package CFClient::UI::EntryBase; 1378package CFClient::UI::EntryBase;
1326 $self->{layout}->set_text ("$text "); 1413 $self->{layout}->set_text ("$text ");
1327 1414
1328 $self->emit (changed => $self->{text}); 1415 $self->emit (changed => $self->{text});
1329} 1416}
1330 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
1331sub get_text { 1427sub get_text {
1332 $_[0]{text} 1428 $_[0]{text}
1333} 1429}
1334 1430
1335sub size_request { 1431sub size_request {
1342 1438
1343sub size_allocate { 1439sub size_allocate {
1344 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1345 1441
1346 $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
1347}
1348
1349sub set_text {
1350 my ($self, $text) = @_;
1351
1352 $self->{cursor} = length $text;
1353 $self->_set_text ($text);
1354 $self->update;
1355} 1443}
1356 1444
1357sub key_down { 1445sub key_down {
1358 my ($self, $ev) = @_; 1446 my ($self, $ev) = @_;
1359 1447
1547 1635
1548 if ($GRAB == $self) { 1636 if ($GRAB == $self) {
1549 $self->{fg} = $self->{active_fg}; 1637 $self->{fg} = $self->{active_fg};
1550 } 1638 }
1551 1639
1552 glEnable GL_BLEND;
1553 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1554 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1555 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1556 glColor 0, 0, 0, 1; 1642 glColor 0, 0, 0, 1;
1557 1643
1558 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1644 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1559 1645
1560 glDisable GL_TEXTURE_2D; 1646 glDisable GL_TEXTURE_2D;
1561 glDisable GL_BLEND;
1562 1647
1563 $self->SUPER::_draw; 1648 $self->SUPER::_draw;
1564} 1649}
1565 1650
1566############################################################################# 1651#############################################################################
1613 1698
1614 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;
1615 1700
1616 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1701 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1617 1702
1618 glEnable GL_BLEND; 1703 my $tex = $self->{state} ? $tex[1] : $tex[0];
1704
1619 glEnable GL_TEXTURE_2D; 1705 glEnable GL_TEXTURE_2D;
1620 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1621
1622 my $tex = $self->{state} ? $tex[1] : $tex[0];
1623
1624 $tex->draw_quad (0, 0, $s, $s); 1706 $tex->draw_quad_alpha (0, 0, $s, $s);
1625
1626 glDisable GL_TEXTURE_2D; 1707 glDisable GL_TEXTURE_2D;
1627 glDisable GL_BLEND;
1628} 1708}
1629 1709
1630############################################################################# 1710#############################################################################
1631 1711
1632package CFClient::UI::Image; 1712package CFClient::UI::Image;
1675 glTranslate 0, -$self->{w}, 0; 1755 glTranslate 0, -$self->{w}, 0;
1676 1756
1677 ($w, $h) = ($h, $w); 1757 ($w, $h) = ($h, $w);
1678 } 1758 }
1679 1759
1680 glEnable GL_BLEND;
1681 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1682 glEnable GL_TEXTURE_2D; 1760 glEnable GL_TEXTURE_2D;
1683 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1684 1762
1685 $tex->draw_quad (0, 0, $w, $h); 1763 $tex->draw_quad_alpha (0, 0, $w, $h);
1686 1764
1687 glDisable GL_BLEND;
1688 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1689} 1766}
1690 1767
1691############################################################################# 1768#############################################################################
1692 1769
1740} 1817}
1741 1818
1742sub set_max { 1819sub set_max {
1743 my ($self, $max) = @_; 1820 my ($self, $max) = @_;
1744 1821
1822 return if $self->{max_val} == $max;
1823
1745 $self->{max_val} = $max; 1824 $self->{max_val} = $max;
1825 $self->update;
1746} 1826}
1747 1827
1748sub set_value { 1828sub set_value {
1749 my ($self, $val, $max) = @_; 1829 my ($self, $val, $max) = @_;
1750 1830
1751 $self->set_max ($max) 1831 $self->set_max ($max)
1752 if defined $max; 1832 if defined $max;
1753 1833
1754 $max = $self->{max_val}; 1834 return if $self->{val} == $val;
1835
1755 $self->{val} = $val; 1836 $self->{val} = $val;
1756
1757 $self->update; 1837 $self->update;
1758} 1838}
1759 1839
1760sub _draw { 1840sub _draw {
1761 my ($self) = @_; 1841 my ($self) = @_;
1824 1904
1825sub new { 1905sub new {
1826 my ($class, %arg) = @_; 1906 my ($class, %arg) = @_;
1827 1907
1828 my $self = $class->SUPER::new ( 1908 my $self = $class->SUPER::new (
1829 tooltip => $arg{type}, 1909 tooltip => $arg{type},
1830 can_hover => 1, 1910 can_hover => 1,
1911 can_events => 1,
1831 %arg, 1912 %arg,
1832 ); 1913 );
1833 1914
1834 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1915 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1835 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 1916 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1843 1924
1844 $self->{value}->set_fontsize ($fsize); 1925 $self->{value}->set_fontsize ($fsize);
1845 $self->{max} ->set_fontsize ($fsize); 1926 $self->{max} ->set_fontsize ($fsize);
1846} 1927}
1847 1928
1929sub set_max {
1930 my ($self, $max) = @_;
1931
1932 $self->{gauge}->set_max ($max);
1933 $self->{max}->set_text ($max);
1934}
1935
1848sub set_value { 1936sub set_value {
1849 my ($self, $val, $max) = @_; 1937 my ($self, $val, $max) = @_;
1850 1938
1851 $self->set_max ($max) 1939 $self->set_max ($max)
1852 if defined $max; 1940 if defined $max;
1853 1941
1854 $self->{gauge}->set_value ($val, $max); 1942 $self->{gauge}->set_value ($val, $max);
1855 $self->{value}->set_text ($val); 1943 $self->{value}->set_text ($val);
1856}
1857
1858sub set_max {
1859 my ($self, $max) = @_;
1860
1861 $self->{gauge}->set_max ($max);
1862 $self->{max}->set_text ($max);
1863} 1944}
1864 1945
1865############################################################################# 1946#############################################################################
1866 1947
1867package CFClient::UI::Slider; 1948package CFClient::UI::Slider;
1886 # TODO: calculations are off 1967 # TODO: calculations are off
1887 my $self = $class->SUPER::new ( 1968 my $self = $class->SUPER::new (
1888 fg => [1, 1, 1], 1969 fg => [1, 1, 1],
1889 active_fg => [0, 0, 0], 1970 active_fg => [0, 0, 0],
1890 range => [0, 0, 100, 10], 1971 range => [0, 0, 100, 10],
1891 req_w => 20, 1972 req_w => $::WIDTH / 80,
1892 req_h => 20, 1973 req_h => $::WIDTH / 80,
1893 vertical => 0, 1974 vertical => 0,
1894 can_hover => 1, 1975 can_hover => 1,
1895 inner_pad => 5, 1976 inner_pad => 5,
1896 @_ 1977 @_
1897 ); 1978 );
1980 $page ||= 2; 2061 $page ||= 2;
1981 2062
1982 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1983 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1984 2065
1985 glEnable GL_BLEND;
1986 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1987 glEnable GL_TEXTURE_2D; 2066 glEnable GL_TEXTURE_2D;
1988 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1989 2068
1990 # draw background 2069 # draw background
1991 $tex[1]->draw_quad (0, 0, $w, $h); 2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
1992 2071
1993 # draw handle 2072 # draw handle
1994 $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);
1995 2074
1996 glDisable GL_BLEND;
1997 glDisable GL_TEXTURE_2D; 2075 glDisable GL_TEXTURE_2D;
1998} 2076}
1999 2077
2000############################################################################# 2078#############################################################################
2001 2079
2012 fontsize => 1, 2090 fontsize => 1,
2013 can_events => 0, 2091 can_events => 0,
2014 #font => default_font 2092 #font => default_font
2015 @_, 2093 @_,
2016 2094
2017 layout => (new CFClient::Layout), 2095 layout => (new CFClient::Layout 1),
2018 par => [], 2096 par => [],
2019 height => 0, 2097 height => 0,
2020 children => [ 2098 children => [
2021 (new CFClient::UI::Empty expand => 1), 2099 (new CFClient::UI::Empty expand => 1),
2022 (new CFClient::UI::Slider vertical => 1), 2100 (new CFClient::UI::Slider vertical => 1),
2023 ], 2101 ],
2024 ); 2102 );
2025 2103
2026 $self->{children}[1]->connect (changed => sub { 2104 $self->{children}[1]->connect (changed => sub { $self->update });
2027 $self->update;
2028 });
2029 2105
2030 $self 2106 $self
2031} 2107}
2032 2108
2033sub set_fontsize { 2109sub set_fontsize {
2041 my ($self, $text) = @_; 2117 my ($self, $text) = @_;
2042 2118
2043 my $layout = $self->{layout}; 2119 my $layout = $self->{layout};
2044 2120
2045 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2046 $layout->set_width ($self->{w}); 2122 $layout->set_width ($self->{children}[0]{w});
2047 $layout->set_text ($text); 2123 $layout->set_markup ($text);
2048 2124
2049 ($layout->size)[1] 2125 ($layout->size)[1]
2050} 2126}
2051 2127
2052sub reflow { 2128sub reflow {
2090 2166
2091 return unless $self->{h} > 0; 2167 return unless $self->{h} > 0;
2092 2168
2093 delete $self->{texture}; 2169 delete $self->{texture};
2094 2170
2095 $ROOT->on_refresh ($self, sub { 2171 $ROOT->on_post_alloc ($self, sub {
2096 if (delete $self->{need_reflow}) { 2172 if (delete $self->{need_reflow}) {
2097 my $height = 0; 2173 my $height = 0;
2098 2174
2099 $height += $_->[0] = $self->text_height ($_->[2]) 2175 $height += $_->[0] = $self->text_height ($_->[2])
2100 for @{$self->{par}}; 2176 for @{$self->{par}};
2104 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2180 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2105 2181
2106 delete $self->{texture}; 2182 delete $self->{texture};
2107 } 2183 }
2108 2184
2109 $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 {
2110 glClearColor 0, 0, 0, 1; 2186 glClearColor 0, 0, 0, 0;
2111 glClear GL_COLOR_BUFFER_BIT; 2187 glClear GL_COLOR_BUFFER_BIT;
2112 2188
2113 glEnable GL_BLEND;
2114 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2115 glEnable GL_TEXTURE_2D; 2189 glEnable GL_TEXTURE_2D;
2116 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2117 2191
2118 my $top = int $self->{children}[1]{range}[0]; 2192 my $top = int $self->{children}[1]{range}[0];
2119 2193
2128 2202
2129 for my $par (@{$self->{par}}) { 2203 for my $par (@{$self->{par}}) {
2130 my $h = $par->[0]; 2204 my $h = $par->[0];
2131 2205
2132 if ($y0 < $y + $h && $y < $y1) { 2206 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] });
2133 $layout->set_text ($par->[2]); 2208 $layout->set_markup ($par->[2]);
2134 2209
2135 glColor @{ $par->[1] };
2136 my ($W, $H) = $layout->size; 2210 my ($W, $H) = $layout->size;
2137 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);
2138 } 2212 }
2139 2213
2140 $y += $h; 2214 $y += $h;
2141 } 2215 }
2142 2216
2143 glDisable GL_TEXTURE_2D; 2217 glDisable GL_TEXTURE_2D;
2144 glDisable GL_BLEND;
2145 }; 2218 };
2146 }); 2219 });
2147} 2220}
2148 2221
2149sub _draw { 2222sub _draw {
2150 my ($self) = @_; 2223 my ($self) = @_;
2151 2224
2152 if ($self->{texture}) {
2153 glEnable GL_TEXTURE_2D; 2225 glEnable GL_TEXTURE_2D;
2154 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2155 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2227 glColor 1, 1, 1, 1;
2228 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2156 glDisable GL_TEXTURE_2D; 2229 glDisable GL_TEXTURE_2D;
2157 }
2158 2230
2159 $self->{children}[1]->draw; 2231 $self->{children}[1]->draw;
2160 2232
2161} 2233}
2162 2234
2256 @_, 2328 @_,
2257 can_events => 0, 2329 can_events => 0,
2258 ) 2330 )
2259} 2331}
2260 2332
2261sub set_markup { 2333sub set_tooltip_from {
2262 my ($self, $text) = @_; 2334 my ($self, $widget) = @_;
2263 2335
2264 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2336 $self->add (new CFClient::UI::Label
2265 $self->{label}->set_markup ($text); 2337 markup => $widget->{tooltip},
2266 $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 );
2267} 2343}
2268 2344
2269sub size_request { 2345sub size_request {
2270 my ($self) = @_; 2346 my ($self) = @_;
2271
2272 $self->child->set_max_size ($::WIDTH * 0.3);
2273 2347
2274 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2348 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2275 2349
2276 ($w + 4, $h + 4) 2350 ($w + 4, $h + 4)
2277} 2351}
2283} 2357}
2284 2358
2285sub _draw { 2359sub _draw {
2286 my ($self) = @_; 2360 my ($self) = @_;
2287 2361
2288 glPushMatrix;
2289 glTranslate 0.375, 0.375; 2362 glTranslate 0.375, 0.375;
2290 2363
2291 my ($w, $h) = @$self{qw(w h)}; 2364 my ($w, $h) = @$self{qw(w h)};
2292 2365
2293 glColor 1, 0.8, 0.4; 2366 glColor 1, 0.8, 0.4;
2304 glVertex 0 , $h; 2377 glVertex 0 , $h;
2305 glVertex $w, $h; 2378 glVertex $w, $h;
2306 glVertex $w, 0; 2379 glVertex $w, 0;
2307 glEnd; 2380 glEnd;
2308 2381
2309 glPopMatrix; 2382 glTranslate 2 - 0.375, 2 - 0.375;
2310
2311 glTranslate 2, 2;
2312 $self->SUPER::_draw; 2383 $self->SUPER::_draw;
2313} 2384}
2314 2385
2315############################################################################# 2386#############################################################################
2316 2387
2331 2402
2332sub size_request { 2403sub size_request {
2333 (32, 8) 2404 (32, 8)
2334} 2405}
2335 2406
2336sub draw { 2407sub _draw {
2337 my ($self) = @_; 2408 my ($self) = @_;
2338 2409
2410 return unless $::CONN;#d# manage and cache textures differently
2339 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2340 2412
2413 # TODO animation
2341 if ($tex) { 2414 if ($tex) {
2342 glEnable GL_BLEND;
2343 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2344 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2346 glColor 1, 1, 1, 1; 2417 glColor 1, 1, 1, 1;
2347 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2348 glDisable GL_TEXTURE_2D; 2419 glDisable GL_TEXTURE_2D;
2349 glDisable GL_BLEND;
2350 } 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;
2351} 2693}
2352 2694
2353############################################################################# 2695#############################################################################
2354 2696
2355package CFClient::UI::Root; 2697package CFClient::UI::Root;
2356 2698
2357our @ISA = CFClient::UI::Container::; 2699our @ISA = CFClient::UI::Container::;
2358 2700
2359use CFClient::OpenGL; 2701use CFClient::OpenGL;
2360 2702
2361sub check_size { 2703sub new {
2362 my ($self) = @_; 2704 my $class = shift;
2363 2705
2364 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2706 $class->SUPER::new (
2365} 2707 @_,
2366 2708 )
2367sub size_request {
2368 ($::WIDTH, $::HEIGHT)
2369} 2709}
2370 2710
2371sub configure { 2711sub configure {
2372 my ($self, $x, $y, $w, $h) = @_; 2712 my ($self, $x, $y, $w, $h) = @_;
2373 2713
2374 $self->SUPER::configure ($x, $y, $w, $h); 2714 $self->{w} = $w;
2715 $self->{h} = $h;
2716}
2375 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
2376 for my $child (@{$self->{children}}) { 2750 for my $child ($self->children) {
2377 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)};
2378 2752
2379 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2753 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2380 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2381 $child->configure ($X, $Y, $W,$H); 2755 $child->configure ($X, $Y, $W, $H);
2382 } 2756 }
2383}
2384 2757
2385sub _topleft { 2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760}
2761
2762sub coord2local {
2386 my ($self, $x, $y) = @_; 2763 my ($self, $x, $y) = @_;
2387 2764
2388 ($x, $y) 2765 ($x, $y)
2389} 2766}
2390 2767
2768sub coord2global {
2769 my ($self, $x, $y) = @_;
2770
2771 ($x, $y)
2772}
2773
2391sub update { 2774sub update {
2392 my ($self) = @_; 2775 my ($self) = @_;
2393 2776
2394 $self->check_size; 2777 $self->check_size;
2395 ::refresh (); 2778 $::WANT_REFRESH++;
2396} 2779}
2397 2780
2398sub add { 2781sub add {
2399 my ($self, $child) = @_; 2782 my ($self, $child) = @_;
2400 2783
2401 # integerize window positions 2784 # integerise window positions
2402 $child->{x} = int $child->{x}; 2785 $child->{x} = int $child->{x};
2403 $child->{y} = int $child->{y}; 2786 $child->{y} = int $child->{y};
2404 2787
2405 $self->SUPER::add ($child); 2788 $self->SUPER::add ($child);
2406} 2789}
2409 my ($self, $id, $cb) = @_; 2792 my ($self, $id, $cb) = @_;
2410 2793
2411 $self->{refresh_hook}{$id} = $cb; 2794 $self->{refresh_hook}{$id} = $cb;
2412} 2795}
2413 2796
2797sub on_post_alloc {
2798 my ($self, $id, $cb) = @_;
2799
2800 $self->{post_alloc_hook}{$id} = $cb;
2801}
2802
2414sub draw { 2803sub draw {
2415 my ($self) = @_; 2804 my ($self) = @_;
2416 2805
2417 while (my $rcb = delete $self->{refresh_hook}) { 2806 while ($self->{refresh_hook}) {
2418 $_->() 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}};
2419 } 2859 }
2420 2860
2421 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2861 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2422 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2423 glClear GL_COLOR_BUFFER_BIT; 2863 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines