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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines