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.194 by root, Thu May 11 23:41:47 2006 UTC vs.
Revision 1.230 by root, Wed May 24 21:59:54 2006 UTC

11our ($FOCUS, $HOVER, $GRAB); # various widgets 11our ($FOCUS, $HOVER, $GRAB); # various widgets
12 12
13our $ROOT; 13our $ROOT;
14our $TOOLTIP; 14our $TOOLTIP;
15our $BUTTON_STATE; 15our $BUTTON_STATE;
16
17our %WIDGET; # all widgets, weak-referenced
16 18
17sub check_tooltip { 19sub check_tooltip {
18 if (!$GRAB) { 20 if (!$GRAB) {
19 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
20 if (length $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
24 26
25 my $tip = $widget->{tooltip}; 27 my $tip = $widget->{tooltip};
26 28
27 $tip = $tip->($widget) if CODE:: eq ref $tip; 29 $tip = $tip->($widget) if CODE:: eq ref $tip;
28 30
29 $TOOLTIP->set_markup ($widget->{tooltip}); 31 $TOOLTIP->set_tooltip_from ($widget);
30
31 $TOOLTIP->show; 32 $TOOLTIP->show;
32 33
33 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
34 35
35 if ($x + $TOOLTIP->{w} > $::WIDTH) {
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
38 38
39 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
40 } 42 }
41 43
42 return; 44 return;
43 } 45 }
44 } 46 }
132 for (@$vals) { 134 for (@$vals) {
133 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
134 $rem += $_ - $i; 136 $rem += $_ - $i;
135 $_ = $i; 137 $_ = $i;
136 } 138 }
139}
140
141sub full_refresh {
142 # make a copy, otherwise for complains about freed values.
143 my @widgets = values %WIDGET;
144
145 $_->update
146 for @widgets;
147}
148
149sub reconfigure_widgets {
150 # make a copy, otherwise C<for> complains about freed values.
151 my @widgets = values %WIDGET;
152
153 $_->reconfigure
154 for @widgets;
155}
156
157# call when resolution changes etc.
158sub rescale_widgets {
159 my ($sx, $sy) = @_;
160
161 for my $widget (values %WIDGET) {
162 if ($widget->{is_toplevel}) {
163 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
164 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
165 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
166 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
167 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
168 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
169 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
170 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
171 }
172 }
173
174 reconfigure_widgets;
137} 175}
138 176
139############################################################################# 177#############################################################################
140 178
141package CFClient::UI::Base; 179package CFClient::UI::Base;
159 if (/^connect_(.*)$/) { 197 if (/^connect_(.*)$/) {
160 $self->connect ($1 => delete $self->{$_}); 198 $self->connect ($1 => delete $self->{$_});
161 } 199 }
162 } 200 }
163 201
202 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
203
164 $self 204 $self
165} 205}
166 206
167sub destroy { 207sub destroy {
168 my ($self) = @_; 208 my ($self) = @_;
171 %$self = (); 211 %$self = ();
172} 212}
173 213
174sub show { 214sub show {
175 my ($self) = @_; 215 my ($self) = @_;
176
177 return if $self->{parent}; 216 return if $self->{parent};
178 217
179 $CFClient::UI::ROOT->add ($self); 218 $CFClient::UI::ROOT->add ($self);
219}
220
221sub show_centered {
222 my ($self) = @_;
223 return if $self->{parent};
224
225 $self->show;
226
227 $CFClient::UI::ROOT->on_post_alloc (
228 "centered $self" => sub {
229 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
230 },
231 );
180} 232}
181 233
182sub hide { 234sub hide {
183 my ($self) = @_; 235 my ($self) = @_;
184 236
241 293
242sub size_allocate { 294sub size_allocate {
243 # nothing to be done 295 # nothing to be done
244} 296}
245 297
298sub reconfigure {
299 my ($self) = @_;
300
301 $self->check_size (1);
302 $self->update;
303}
304
246sub children { 305sub children {
247}
248
249# call when resolution changes etc.
250sub reconfigure {
251 my ($self) = @_;
252
253 $_->reconfigure
254 for $self->children;
255
256 $self->check_size;
257 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
258 $self->update;
259} 306}
260 307
261sub set_max_size { 308sub set_max_size {
262 my ($self, $w, $h) = @_; 309 my ($self, $w, $h) = @_;
263 310
264 delete $self->{max_w}; $self->{max_w} = $w if $w; 311 delete $self->{max_w}; $self->{max_w} = $w if $w;
265 delete $self->{max_h}; $self->{max_h} = $h if $h; 312 delete $self->{max_h}; $self->{max_h} = $h if $h;
313}
314
315sub set_tooltip {
316 my ($self, $tooltip) = @_;
317
318 $self->{tooltip} = $tooltip;
319
320 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
321 delete $CFClient::UI::TOOLTIP->{owner};
322 CFClient::UI::check_tooltip;
323 }
266} 324}
267 325
268# translate global coordinates to local coordinate system 326# translate global coordinates to local coordinate system
269sub coord2local { 327sub coord2local {
270 my ($self, $x, $y) = @_; 328 my ($self, $x, $y) = @_;
356 glVertex $self->{w}, 0; 414 glVertex $self->{w}, 0;
357 glVertex $self->{w}, $self->{h}; 415 glVertex $self->{w}, $self->{h};
358 glVertex 0 , $self->{h}; 416 glVertex 0 , $self->{h};
359 glEnd; 417 glEnd;
360 glPopMatrix; 418 glPopMatrix;
361 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 419 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
362 } 420 }
363} 421}
364 422
365sub _draw { 423sub _draw {
366 my ($self) = @_; 424 my ($self) = @_;
383sub set_parent { 441sub set_parent {
384 my ($self, $parent) = @_; 442 my ($self, $parent) = @_;
385 443
386 Scalar::Util::weaken ($self->{parent} = $parent); 444 Scalar::Util::weaken ($self->{parent} = $parent);
387 445
446 # TODO: req_w _does_change after ->reconfigure
388 $self->check_size 447 $self->check_size
389 unless exists $self->{req_w}; 448 unless exists $self->{req_w};
449
450 $self->show;
390} 451}
391 452
392sub check_size { 453sub check_size {
393 my ($self) = @_; 454 my ($self, $forced) = @_;
394 455
456 $self->{force_alloc} = 1 if $forced;
395 $CFClient::UI::ROOT->{check_size}{$self} = $self; 457 $CFClient::UI::ROOT->{check_size}{$self} = $self;
396} 458}
397 459
398sub update { 460sub update {
399 my ($self) = @_; 461 my ($self) = @_;
415} 477}
416 478
417sub DESTROY { 479sub DESTROY {
418 my ($self) = @_; 480 my ($self) = @_;
419 481
482 delete $WIDGET{$self+0};
420 #$self->deactivate; 483 #$self->deactivate;
421} 484}
422 485
423############################################################################# 486#############################################################################
424 487
433 my $class = shift; 496 my $class = shift;
434 497
435 # range [value, low, high, page] 498 # range [value, low, high, page]
436 499
437 $class->SUPER::new ( 500 $class->SUPER::new (
438 bg => [0, 0, 0, 0.2], 501 #bg => [0, 0, 0, 0.2],
439 active_bg => [1, 1, 1, 0.5], 502 #active_bg => [1, 1, 1, 0.5],
440 @_ 503 @_
441 ) 504 )
442} 505}
443 506
444sub _draw { 507sub _draw {
445 my ($self) = @_; 508 my ($self) = @_;
446 509
510 my $color = $FOCUS == $self && $self->{active_bg}
511 ? $self->{active_bg}
512 : $self->{bg};
513
514 if ($color && (@$color < 4 || $color->[3])) {
447 my ($w, $h) = @$self{qw(w h)}; 515 my ($w, $h) = @$self{qw(w h)};
448 516
449 glEnable GL_BLEND; 517 glEnable GL_BLEND;
450 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 518 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
451 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 519 glColor @$color;
452 520
453 glBegin GL_QUADS; 521 glBegin GL_QUADS;
454 glVertex 0 , 0; 522 glVertex 0 , 0;
455 glVertex 0 , $h; 523 glVertex 0 , $h;
456 glVertex $w, $h; 524 glVertex $w, $h;
457 glVertex $w, 0; 525 glVertex $w, 0;
458 glEnd; 526 glEnd;
459 527
460 glDisable GL_BLEND; 528 glDisable GL_BLEND;
529 }
461} 530}
462 531
463############################################################################# 532#############################################################################
464 533
465package CFClient::UI::Empty; 534package CFClient::UI::Empty;
509 $self->{children} = [ 578 $self->{children} = [
510 sort { $a->{z} <=> $b->{z} } 579 sort { $a->{z} <=> $b->{z} }
511 @{$self->{children}}, @widgets 580 @{$self->{children}}, @widgets
512 ]; 581 ];
513 582
514 $self->check_size; 583 $self->check_size (1);
515 $self->update; 584 $self->update;
516} 585}
517 586
518sub children { 587sub children {
519 @{ $_[0]{children} } 588 @{ $_[0]{children} }
626} 695}
627 696
628sub update { 697sub update {
629 my ($self) = @_; 698 my ($self) = @_;
630 699
631 $ROOT->on_refresh ($self => sub { $self->render_child }); 700 $ROOT->on_post_alloc ($self => sub { $self->render_child });
632 $self->SUPER::update; 701 $self->SUPER::update;
633} 702}
634 703
635sub size_allocate { 704sub size_allocate {
636 my ($self, $w, $h) = @_; 705 my ($self, $w, $h) = @_;
649 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 718 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
650 glClearColor 0, 0, 0, 0; 719 glClearColor 0, 0, 0, 0;
651 glClear GL_COLOR_BUFFER_BIT; 720 glClear GL_COLOR_BUFFER_BIT;
652 721
653 $self->_render; 722 $self->_render;
654# glColorMask 1, 1, 1, 0;
655# glEnable GL_BLEND;
656# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
657# glRasterPos 0, 0;
658# glCopyPixels 0, 0, $self->{w}, $self->{h};
659# glDisable GL_BLEND;
660# glColorMask 1, 1, 1, 1;
661 }; 723 };
662} 724}
663 725
664sub _draw { 726sub _draw {
665 my ($self) = @_; 727 my ($self) = @_;
669 my $tex = $self->{texture} 731 my $tex = $self->{texture}
670 or return; 732 or return;
671 733
672 glEnable GL_TEXTURE_2D; 734 glEnable GL_TEXTURE_2D;
673 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
674 glColor 0, 0, 0, 1; 736 glColor 1, 1, 1, 1;
675 737
676 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 738 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
677 739
678 glDisable GL_TEXTURE_2D; 740 glDisable GL_TEXTURE_2D;
679} 741}
686 748
687sub size_request { 749sub size_request {
688 my ($self) = @_; 750 my ($self) = @_;
689 751
690 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 752 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
691 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
692 753
693 @$self{qw(child_w child_h)} 754 @$self{qw(child_w child_h)}
694} 755}
695 756
696sub size_allocate { 757sub size_allocate {
697 my ($self, $w, $h) = @_; 758 my ($self, $w, $h) = @_;
698 759
760 my ($cw, $ch) = @$self{qw(child_w child_h)};
761# $w = $self->{w};
762 $self->child->configure (0, 0, $cw, $ch);
699 $self->update; 763 $self->update;
700} 764}
701 765
702sub set_offset { 766sub set_offset {
703 my ($self, $x, $y) = @_; 767 my ($self, $x, $y) = @_;
757 821
758 my $slider = new CFClient::UI::Slider 822 my $slider = new CFClient::UI::Slider
759 vertical => 1, 823 vertical => 1,
760 range => [0, 0, 1, 0.01], # HACK fix 824 range => [0, 0, 1, 0.01], # HACK fix
761 connect_changed => sub { 825 connect_changed => sub {
762 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 826 $self->{vp}->set_offset (0, $_[1]);
763 }, 827 },
764 ; 828 ;
765 829
766 $self = $class->SUPER::new ( 830 $self = $class->SUPER::new (
767 vp => (new CFClient::UI::ViewPort), 831 vp => (new CFClient::UI::ViewPort expand => 1),
768 slider => $slider, 832 slider => $slider,
769 @_, 833 @_,
770 ); 834 );
771 835
772 $self->{vp}->add ($self->{scrolled}); 836 $self->{vp}->add ($self->{scrolled});
774 $self->add ($self->{slider}); 838 $self->add ($self->{slider});
775 839
776 $self 840 $self
777} 841}
778 842
843sub size_allocate {
844 my ($self, $w, $h) = @_;
845
846 $self->SUPER::size_allocate ($w, $h);
847
848 my $child = $self->{vp}->child;
849 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
850}
851
779#TODO# update range on size_allocate depeneing on child 852#TODO# update range on size_allocate depending on child
780# update viewport offset on scroll 853# update viewport offset on scroll
781 854
782############################################################################# 855#############################################################################
783 856
784package CFClient::UI::Frame; 857package CFClient::UI::Frame;
788use CFClient::OpenGL; 861use CFClient::OpenGL;
789 862
790sub new { 863sub new {
791 my $class = shift; 864 my $class = shift;
792 865
793 my $self = $class->SUPER::new ( 866 $class->SUPER::new (
794 bg => [1, 1, 1, 1], 867 bg => undef,
795 border_bg => [1, 1, 1, 1],
796 border => 0.8,
797 @_ 868 @_,
798 ); 869 )
799
800 $self
801} 870}
802 871
803sub _draw { 872sub _draw {
804 my ($self) = @_; 873 my ($self) = @_;
805 874
806 my ($w, $h) = ($self->{w}, $self->{h}); 875 if ($self->{bg}) {
876 my ($w, $h) = @$self{qw(w h)};
807 877
808 glEnable GL_BLEND; 878 glEnable GL_BLEND;
809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 879 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
810 glEnable GL_TEXTURE_2D; 880 glColor @{ $self->{bg} };
811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
812 881
813# glBegin GL_QUADS; 882 glBegin GL_QUADS;
814# glColor 0, 0, 0, 0;
815# glVertex 0 , 0; 883 glVertex 0 , 0;
816# glVertex 0 , $h; 884 glVertex 0 , $h;
817# glVertex $w, $h; 885 glVertex $w, $h;
818# glVertex $w, 0; 886 glVertex $w, 0;
819# glEnd; 887 glEnd;
820 888
821
822 $self->child->draw;
823 glDisable GL_BLEND; 889 glDisable GL_BLEND;
824 glDisable GL_TEXTURE_2D; 890 }
891
892 $self->SUPER::_draw;
825} 893}
826 894
827############################################################################# 895#############################################################################
828 896
829package CFClient::UI::FancyFrame; 897package CFClient::UI::FancyFrame;
840 my $class = shift; 908 my $class = shift;
841 909
842 # TODO: user_x, user_y, overwrite moveto? 910 # TODO: user_x, user_y, overwrite moveto?
843 911
844 my $self = $class->SUPER::new ( 912 my $self = $class->SUPER::new (
845 bg => [1, 1, 1, 1], 913 bg => [1, 1, 1, 1],
846 border_bg => [1, 1, 1, 1], 914 border_bg => [1, 1, 1, 1],
847 border => 0.6, 915 border => 0.6,
916 is_toplevel => 1,
848 can_events => 1, 917 can_events => 1,
849 @_ 918 @_
850 ); 919 );
851 920
852 $self->{title} &&= new CFClient::UI::Label 921 $self->{title} &&= new CFClient::UI::Label
853 align => 0, 922 align => 0,
877 my ($self, $w, $h) = @_; 946 my ($self, $w, $h) = @_;
878 947
879 $h -= List::Util::max 0, $self->border * 2; 948 $h -= List::Util::max 0, $self->border * 2;
880 $w -= List::Util::max 0, $self->border * 2; 949 $w -= List::Util::max 0, $self->border * 2;
881 950
882 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 951 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
883 if $self->{title}; 952 if $self->{title};
884 953
885 $self->child->configure ($self->border, $self->border, $w, $h); 954 $self->child->configure ($self->border, $self->border, $w, $h);
886} 955}
887 956
945 my ($self) = @_; 1014 my ($self) = @_;
946 1015
947 my ($w, $h ) = ($self->{w}, $self->{h}); 1016 my ($w, $h ) = ($self->{w}, $self->{h});
948 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1017 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
949 1018
950 glEnable GL_BLEND;
951 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
952 glEnable GL_TEXTURE_2D; 1019 glEnable GL_TEXTURE_2D;
953 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1020 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
954 1021
955 my $border = $self->border; 1022 my $border = $self->border;
956 1023
957 glColor @{ $self->{border_bg} }; 1024 glColor @{ $self->{border_bg} };
958 $tex[1]->draw_quad (0, 0, $w, $border); 1025 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
959 $tex[3]->draw_quad (0, $border, $border, $ch); 1026 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
960 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 1027 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
961 $tex[4]->draw_quad (0, $h - $border, $w, $border); 1028 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
962 1029
963 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1030 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
964 my $bg = $tex[0]; 1031 my $bg = $tex[0];
965 1032
966 # TODO: repeat texture not scale 1033 # TODO: repeat texture not scale
970 glColor @{ $self->{bg} }; 1037 glColor @{ $self->{bg} };
971 1038
972 $bg->{s} = $rep_x; 1039 $bg->{s} = $rep_x;
973 $bg->{t} = $rep_y; 1040 $bg->{t} = $rep_y;
974 $bg->{wrap_mode} = 1; 1041 $bg->{wrap_mode} = 1;
975 $bg->draw_quad ($border, $border, $cw, $ch); 1042 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1043 }
976 1044
977 glDisable GL_TEXTURE_2D; 1045 glDisable GL_TEXTURE_2D;
978 glDisable GL_BLEND;
979 }
980 1046
981 $self->{title}->draw if $self->{title}; 1047 $self->{title}->draw if $self->{title};
982 1048
983 $self->child->draw; 1049 $self->child->draw;
984} 1050}
1251 1317
1252############################################################################# 1318#############################################################################
1253 1319
1254package CFClient::UI::Label; 1320package CFClient::UI::Label;
1255 1321
1256our @ISA = CFClient::UI::Base::; 1322our @ISA = CFClient::UI::DrawBG::;
1257 1323
1258use CFClient::OpenGL; 1324use CFClient::OpenGL;
1259 1325
1260sub new { 1326sub new {
1261 my ($class, %arg) = @_; 1327 my ($class, %arg) = @_;
1262 1328
1263 my $self = $class->SUPER::new ( 1329 my $self = $class->SUPER::new (
1264 fg => [1, 1, 1], 1330 fg => [1, 1, 1],
1331 #bg => none
1332 #active_bg => none
1265 #font => default_font 1333 #font => default_font
1266 #text => initial text 1334 #text => initial text
1267 #markup => initial narkup 1335 #markup => initial narkup
1336 #max_w => maximum pixel width
1337 ellipsise => 3, # end
1268 layout => (new CFClient::Layout), 1338 layout => (new CFClient::Layout),
1269 fontsize => 1, 1339 fontsize => 1,
1270 align => -1, 1340 align => -1,
1271 valign => -1, 1341 valign => -1,
1272 padding => 2, 1342 padding => 2,
1287 } 1357 }
1288 1358
1289 $self 1359 $self
1290} 1360}
1291 1361
1292sub escape { 1362sub escape($) {
1293 local $_ = $_[1]; 1363 local $_ = $_[0];
1294 1364
1295 s/&/&amp;/g; 1365 s/&/&amp;/g;
1296 s/>/&gt;/g; 1366 s/>/&gt;/g;
1297 s/</&lt;/g; 1367 s/</&lt;/g;
1298 1368
1299 $_[1] 1369 $_
1300} 1370}
1301 1371
1302sub update { 1372sub update {
1303 my ($self) = @_; 1373 my ($self) = @_;
1304 1374
1337sub size_request { 1407sub size_request {
1338 my ($self) = @_; 1408 my ($self) = @_;
1339 1409
1340 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1410 $self->{layout}->set_font ($self->{font}) if $self->{font};
1341 $self->{layout}->set_width ($self->{max_w} || -1); 1411 $self->{layout}->set_width ($self->{max_w} || -1);
1412 $self->{layout}->set_ellipsise ($self->{ellipsise});
1413 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1342 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1414 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1343 1415
1344 my ($w, $h) = $self->{layout}->size; 1416 my ($w, $h) = $self->{layout}->size;
1345 1417
1346 if (exists $self->{template}) { 1418 if (exists $self->{template}) {
1376} 1448}
1377 1449
1378sub _draw { 1450sub _draw {
1379 my ($self) = @_; 1451 my ($self) = @_;
1380 1452
1453 $self->SUPER::_draw; # draw background, if applicable
1454
1381 my $tex = $self->{texture} ||= do { 1455 my $tex = $self->{texture} ||= do {
1382 $self->{layout}->set_foreground (@{$self->{fg}}); 1456 $self->{layout}->set_foreground (@{$self->{fg}});
1383 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1457 $self->{layout}->set_font ($self->{font}) if $self->{font};
1384 $self->{layout}->set_width ($self->{w}); 1458 $self->{layout}->set_width ($self->{w});
1459 $self->{layout}->set_ellipsise ($self->{ellipsise});
1460 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1385 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1461 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1386 1462
1387 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1463 my $tex = new_from_layout CFClient::Texture $self->{layout};
1388 1464
1389 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1465 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1390 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1466 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1391 : ($self->{w} - $tex->{w}) * 0.5; 1467 : ($self->{w} - $tex->{w}) * 0.5);
1392 1468
1393 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1469 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1394 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1470 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1395 : ($self->{h} - $tex->{h}) * 0.5; 1471 : ($self->{h} - $tex->{h}) * 0.5);
1396 1472
1397 $tex 1473 $tex
1398 }; 1474 };
1399 1475
1400 glEnable GL_TEXTURE_2D; 1476 glEnable GL_TEXTURE_2D;
1428 active_fg => [0, 0, 0], 1504 active_fg => [0, 0, 0],
1429 can_hover => 1, 1505 can_hover => 1,
1430 can_focus => 1, 1506 can_focus => 1,
1431 valign => 0, 1507 valign => 0,
1432 can_events => 1, 1508 can_events => 1,
1509 #text => ...
1433 @_ 1510 @_
1434 ) 1511 )
1435} 1512}
1436 1513
1437sub _set_text { 1514sub _set_text {
1455sub set_text { 1532sub set_text {
1456 my ($self, $text) = @_; 1533 my ($self, $text) = @_;
1457 1534
1458 $self->{cursor} = length $text; 1535 $self->{cursor} = length $text;
1459 $self->_set_text ($text); 1536 $self->_set_text ($text);
1537 $self->update;
1460 $self->check_size; 1538 $self->check_size;
1461 $self->update;
1462} 1539}
1463 1540
1464sub get_text { 1541sub get_text {
1465 $_[0]{text} 1542 $_[0]{text}
1466} 1543}
1486 my $sym = $ev->{sym}; 1563 my $sym = $ev->{sym};
1487 my $uni = $ev->{unicode}; 1564 my $uni = $ev->{unicode};
1488 1565
1489 my $text = $self->get_text; 1566 my $text = $self->get_text;
1490 1567
1491 if ($sym == 8) { 1568 if ($uni == 8) {
1492 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1569 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1493 } elsif ($sym == 127) { 1570 } elsif ($uni == 127) {
1494 substr $text, $self->{cursor}, 1, ""; 1571 substr $text, $self->{cursor}, 1, "";
1495 } elsif ($sym == CFClient::SDLK_LEFT) { 1572 } elsif ($sym == CFClient::SDLK_LEFT) {
1496 --$self->{cursor} if $self->{cursor}; 1573 --$self->{cursor} if $self->{cursor};
1497 } elsif ($sym == CFClient::SDLK_RIGHT) { 1574 } elsif ($sym == CFClient::SDLK_RIGHT) {
1498 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1575 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1499 } elsif ($sym == CFClient::SDLK_HOME) { 1576 } elsif ($sym == CFClient::SDLK_HOME) {
1500 $self->{cursor} = 0; 1577 $self->{cursor} = 0;
1501 } elsif ($sym == CFClient::SDLK_END) { 1578 } elsif ($sym == CFClient::SDLK_END) {
1502 $self->{cursor} = length $text; 1579 $self->{cursor} = length $text;
1503 } elsif ($sym == 27) { 1580 } elsif ($uni == 27) {
1504 $self->emit ('escape'); 1581 $self->emit ('escape');
1505 } elsif ($uni) { 1582 } elsif ($uni) {
1506 substr $text, $self->{cursor}++, 0, chr $uni; 1583 substr $text, $self->{cursor}++, 0, chr $uni;
1507 } 1584 }
1508 1585
1509 $self->_set_text ($text); 1586 $self->_set_text ($text);
1510 $self->update; 1587 $self->update;
1588 $self->check_size;
1511} 1589}
1512 1590
1513sub focus_in { 1591sub focus_in {
1514 my ($self) = @_; 1592 my ($self) = @_;
1515 1593
1644 my $class = shift; 1722 my $class = shift;
1645 1723
1646 $class->SUPER::new ( 1724 $class->SUPER::new (
1647 padding => 4, 1725 padding => 4,
1648 fg => [1, 1, 1], 1726 fg => [1, 1, 1],
1649 bg => [1, 1, 1, 0.2],
1650 active_fg => [0, 0, 1], 1727 active_fg => [0, 0, 1],
1651 can_hover => 1, 1728 can_hover => 1,
1652 align => 0, 1729 align => 0,
1653 valign => 0, 1730 valign => 0,
1654 can_events => 1, 1731 can_events => 1,
1672 1749
1673 if ($GRAB == $self) { 1750 if ($GRAB == $self) {
1674 $self->{fg} = $self->{active_fg}; 1751 $self->{fg} = $self->{active_fg};
1675 } 1752 }
1676 1753
1677 glEnable GL_BLEND;
1678 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1679 glEnable GL_TEXTURE_2D; 1754 glEnable GL_TEXTURE_2D;
1680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1755 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1681 glColor 0, 0, 0, 1; 1756 glColor 0, 0, 0, 1;
1682 1757
1683 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1758 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1684 1759
1685 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1686 glDisable GL_BLEND;
1687 1761
1688 $self->SUPER::_draw; 1762 $self->SUPER::_draw;
1689} 1763}
1690 1764
1691############################################################################# 1765#############################################################################
1705 1779
1706 $class->SUPER::new ( 1780 $class->SUPER::new (
1707 padding => 2, 1781 padding => 2,
1708 fg => [1, 1, 1], 1782 fg => [1, 1, 1],
1709 active_fg => [1, 1, 0], 1783 active_fg => [1, 1, 0],
1784 bg => [0, 0, 0, 0.2],
1785 active_bg => [1, 1, 1, 0.5],
1710 state => 0, 1786 state => 0,
1711 can_hover => 1, 1787 can_hover => 1,
1712 @_ 1788 @_
1713 ) 1789 )
1714} 1790}
1738 1814
1739 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1815 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1740 1816
1741 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1817 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1742 1818
1743 glEnable GL_BLEND; 1819 my $tex = $self->{state} ? $tex[1] : $tex[0];
1820
1744 glEnable GL_TEXTURE_2D; 1821 glEnable GL_TEXTURE_2D;
1745 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1746
1747 my $tex = $self->{state} ? $tex[1] : $tex[0];
1748
1749 $tex->draw_quad (0, 0, $s, $s); 1822 $tex->draw_quad_alpha (0, 0, $s, $s);
1750
1751 glDisable GL_TEXTURE_2D; 1823 glDisable GL_TEXTURE_2D;
1752 glDisable GL_BLEND;
1753} 1824}
1754 1825
1755############################################################################# 1826#############################################################################
1756 1827
1757package CFClient::UI::Image; 1828package CFClient::UI::Image;
1800 glTranslate 0, -$self->{w}, 0; 1871 glTranslate 0, -$self->{w}, 0;
1801 1872
1802 ($w, $h) = ($h, $w); 1873 ($w, $h) = ($h, $w);
1803 } 1874 }
1804 1875
1805 glEnable GL_BLEND;
1806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1807 glEnable GL_TEXTURE_2D; 1876 glEnable GL_TEXTURE_2D;
1808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1877 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1809 1878
1810 $tex->draw_quad (0, 0, $w, $h); 1879 $tex->draw_quad_alpha (0, 0, $w, $h);
1811 1880
1812 glDisable GL_BLEND;
1813 glDisable GL_TEXTURE_2D; 1881 glDisable GL_TEXTURE_2D;
1814} 1882}
1815 1883
1816############################################################################# 1884#############################################################################
1817 1885
2006 qw(s1_slider.png s1_slider_bg.png); 2074 qw(s1_slider.png s1_slider_bg.png);
2007 2075
2008sub new { 2076sub new {
2009 my $class = shift; 2077 my $class = shift;
2010 2078
2011 # range [value, low, high, page] 2079 # range [value, low, high, page, unit]
2012 2080
2013 # TODO: 0-width page 2081 # TODO: 0-width page
2014 # TODO: req_w/h are wrong with vertical 2082 # TODO: req_w/h are wrong with vertical
2015 # TODO: calculations are off 2083 # TODO: calculations are off
2016 my $self = $class->SUPER::new ( 2084 my $self = $class->SUPER::new (
2017 fg => [1, 1, 1], 2085 fg => [1, 1, 1],
2018 active_fg => [0, 0, 0], 2086 active_fg => [0, 0, 0],
2087 bg => [0, 0, 0, 0.2],
2088 active_bg => [1, 1, 1, 0.5],
2019 range => [0, 0, 100, 10], 2089 range => [0, 0, 100, 10, 0],
2020 req_w => $::WIDTH / 80, 2090 req_w => $::WIDTH / 80,
2021 req_h => $::WIDTH / 80, 2091 req_h => $::WIDTH / 80,
2022 vertical => 0, 2092 vertical => 0,
2023 can_hover => 1, 2093 can_hover => 1,
2024 inner_pad => 5, 2094 inner_pad => 0.02,
2025 @_ 2095 @_
2026 ); 2096 );
2027 2097
2098 $self->set_value ($self->{range}[0]);
2099 $self->update;
2100
2028 $self 2101 $self
2102}
2103
2104sub set_range {
2105 my ($self, $range) = @_;
2106
2107 $self->{range} = $range;
2108
2109 $self->update;
2110}
2111
2112sub set_value {
2113 my ($self, $value) = @_;
2114
2115 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2116
2117 $hi = $lo + 1 if $hi <= $lo;
2118
2119 $page = $hi - $lo if $page > $hi - $lo;
2120
2121 $value = $lo if $value < $lo;
2122 $value = $hi - $page if $value > $hi - $page;
2123
2124 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2125 if $unit;
2126
2127 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2128
2129 if ($value != $old_value) {
2130 $self->emit (changed => $value);
2131 $self->update;
2132 }
2029} 2133}
2030 2134
2031sub size_request { 2135sub size_request {
2032 my ($self) = @_; 2136 my ($self) = @_;
2033 2137
2039 2143
2040sub button_down { 2144sub button_down {
2041 my ($self, $ev, $x, $y) = @_; 2145 my ($self, $ev, $x, $y) = @_;
2042 2146
2043 $self->SUPER::button_down ($ev, $x, $y); 2147 $self->SUPER::button_down ($ev, $x, $y);
2148
2149 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2150
2044 $self->mouse_motion ($ev, $x, $y); 2151 $self->mouse_motion ($ev, $x, $y);
2045} 2152}
2046 2153
2047sub mouse_motion { 2154sub mouse_motion {
2048 my ($self, $ev, $x, $y) = @_; 2155 my ($self, $ev, $x, $y) = @_;
2049 2156
2050 if ($GRAB == $self) { 2157 if ($GRAB == $self) {
2158 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2159
2160 my (undef, $lo, $hi, $page) = @{$self->{range}};
2161
2162 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2163
2164 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2165 }
2166}
2167
2168sub update {
2169 my ($self) = @_;
2170
2171 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2172 $self->set_value ($self->{range}[0]);
2173
2051 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2174 my ($value, $lo, $hi, $page) = @{$self->{range}};
2175 my $range = ($hi - $page - $lo) || 1e-100;
2052 2176
2053 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2177 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2054 2178
2055 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2179 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2056 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 2180 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2057 2181
2058 $x -= $inner_pad_px; # substract the padding 2182 $value = ($value - $lo) / $range;
2059 $x = $x * ($hi - $lo) / $inner_w + $lo; 2183 $value = $value * $self->{scale} + $self->{offset};
2060 $x = $lo if $x < $lo;
2061 $x = $hi - $page if $x > $hi - $page;
2062 $self->{range}[0] = $x;
2063 2184
2064 $self->emit (changed => $x); 2185 $self->{knob_x} = $value - $knob_w * 0.5;
2065 $self->update; 2186 $self->{knob_w} = $knob_w;
2066 } 2187 });
2067}
2068 2188
2069# the inner_* stuff is for generating a padding for the slider handle, 2189 $self->SUPER::update;
2070# so that the handle doesn't leave the texture. This calculation isn't 100%
2071# correct propably, but it does the job for now
2072sub _calc_inner_pad_px {
2073 my ($self, $w) = @_;
2074 ($w / 100) * $self->{inner_pad} # % to pixels
2075} 2190}
2076 2191
2077sub _draw { 2192sub _draw {
2078 my ($self) = @_; 2193 my ($self) = @_;
2079 2194
2080 $self->SUPER::_draw (); 2195 $self->SUPER::_draw ();
2081 2196
2082 my ($w, $h) = @$self{qw(w h)}; 2197 glScale $self->{w}, $self->{h};
2083 2198
2084 if ($self->{vertical}) { 2199 if ($self->{vertical}) {
2085 # draw a vertical slider like a rotated horizontal slider 2200 # draw a vertical slider like a rotated horizontal slider
2086 2201
2202 glTranslate 1, 0, 0;
2087 glRotate 90, 0, 0, 1; 2203 glRotate 90, 0, 0, 1;
2088 glTranslate 0, -$self->{w}, 0;
2089
2090 ($w, $h) = ($h, $w);
2091 } 2204 }
2092 2205
2093 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2206 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2094 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2207 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2095 2208
2096 my ($value, $lo, $hi, $page) = @{$self->{range}};
2097
2098 $hi = $value + 1 if $lo == $hi;
2099
2100 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2101 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2102
2103 $page = int $page * $inner_w / ($hi - $lo);
2104 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2105
2106 $w -= $page;
2107 $page &= ~1;
2108 glTranslate $page * 0.5, 0, 0;
2109 $page ||= 2;
2110
2111 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2112 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2113
2114 glEnable GL_BLEND;
2115 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2116 glEnable GL_TEXTURE_2D; 2209 glEnable GL_TEXTURE_2D;
2117 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2210 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2118 2211
2119 # draw background 2212 # draw background
2120 $tex[1]->draw_quad (0, 0, $w, $h); 2213 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2121 2214
2122 # draw handle 2215 # draw handle
2123 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2216 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2124 2217
2125 glDisable GL_BLEND;
2126 glDisable GL_TEXTURE_2D; 2218 glDisable GL_TEXTURE_2D;
2127} 2219}
2220
2221#############################################################################
2222
2223package CFClient::UI::ValSlider;
2224
2225our @ISA = CFClient::UI::HBox::;
2226
2227sub new {
2228 my ($class, %arg) = @_;
2229
2230 my $range = delete $arg{range};
2231
2232 my $self = $class->SUPER::new (
2233 slider => (new CFClient::UI::Slider expand => 1, range => $range),
2234 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}),
2235 to_value => sub { shift },
2236 from_value => sub { shift },
2237 %arg,
2238 );
2239
2240 $self->{slider}->connect (changed => sub {
2241 my ($self, $value) = @_;
2242 $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2243 $self->{parent}->emit (changed => $value);
2244 });
2245
2246# $self->{entry}->connect (changed => sub {
2247# my ($self, $value) = @_;
2248# $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2249# $self->{parent}->emit (changed => $value);
2250# });
2251
2252 $self->add ($self->{slider}, $self->{entry});
2253
2254 $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2255
2256 $self
2257}
2258
2259sub set_range { shift->{slider}->set_range (@_) }
2260sub set_value { shift->{slider}->set_value (@_) }
2128 2261
2129############################################################################# 2262#############################################################################
2130 2263
2131package CFClient::UI::TextView; 2264package CFClient::UI::TextView;
2132 2265
2141 fontsize => 1, 2274 fontsize => 1,
2142 can_events => 0, 2275 can_events => 0,
2143 #font => default_font 2276 #font => default_font
2144 @_, 2277 @_,
2145 2278
2146 layout => (new CFClient::Layout), 2279 layout => (new CFClient::Layout 1),
2147 par => [], 2280 par => [],
2148 height => 0, 2281 height => 0,
2149 children => [ 2282 children => [
2150 (new CFClient::UI::Empty expand => 1), 2283 (new CFClient::UI::Empty expand => 1),
2151 (new CFClient::UI::Slider vertical => 1), 2284 (new CFClient::UI::Slider vertical => 1),
2162 2295
2163 $self->{fontsize} = $fontsize; 2296 $self->{fontsize} = $fontsize;
2164 $self->reflow; 2297 $self->reflow;
2165} 2298}
2166 2299
2167sub text_height {
2168 my ($self, $text) = @_;
2169
2170 my $layout = $self->{layout};
2171
2172 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2173 $layout->set_width ($self->{children}[0]{w});
2174 $layout->set_text ($text);
2175
2176 ($layout->size)[1]
2177}
2178
2179sub reflow {
2180 my ($self) = @_;
2181
2182 $self->{need_reflow}++;
2183 $self->update;
2184}
2185
2186sub size_allocate { 2300sub size_allocate {
2187 my ($self, $w, $h) = @_; 2301 my ($self, $w, $h) = @_;
2188 2302
2189 $self->SUPER::size_allocate ($w, $h); 2303 $self->SUPER::size_allocate ($w, $h);
2190 2304
2193 $self->{layout}->set_width ($self->{children}[0]{w}); 2307 $self->{layout}->set_width ($self->{children}[0]{w});
2194 2308
2195 $self->reflow; 2309 $self->reflow;
2196} 2310}
2197 2311
2312sub text_size {
2313 my ($self, $text, $indent) = @_;
2314
2315 my $layout = $self->{layout};
2316
2317 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2318 $layout->set_width ($self->{children}[0]{w} - $indent);
2319 $layout->set_markup ($text);
2320
2321 $layout->size
2322}
2323
2324sub reflow {
2325 my ($self) = @_;
2326
2327 $self->{need_reflow}++;
2328 $self->update;
2329}
2330
2331sub set_offset {
2332 my ($self, $offset) = @_;
2333
2334 # todo: base offset on lines or so, not on pixels
2335 $self->{children}[1]->set_value ($offset);
2336}
2337
2338sub clear {
2339 my ($self) = @_;
2340
2341 $self->{par} = [];
2342 $self->{height} = 0;
2343 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2344}
2345
2198sub add_paragraph { 2346sub add_paragraph {
2199 my ($self, $color, $text) = @_; 2347 my ($self, $color, $text, $indent) = @_;
2200 2348
2201 #TODO: intelligently "reformat" paragraph 2349 for my $line (split /\n/, $text) {
2202 2350 my ($w, $h) = $self->text_size ($line);
2203 my $height = $self->text_height ($text);
2204
2205 $self->{height} += $height; 2351 $self->{height} += $h;
2352 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2353 }
2206 2354
2207 push @{$self->{par}}, [$height, $color, $text];
2208
2209 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2355 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2210 $self->{children}[1]->update;
2211} 2356}
2212 2357
2213sub update { 2358sub update {
2214 my ($self) = @_; 2359 my ($self) = @_;
2215 2360
2217 2362
2218 return unless $self->{h} > 0; 2363 return unless $self->{h} > 0;
2219 2364
2220 delete $self->{texture}; 2365 delete $self->{texture};
2221 2366
2222 $ROOT->on_refresh ($self, sub { 2367 $ROOT->on_post_alloc ($self, sub {
2368 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2369
2223 if (delete $self->{need_reflow}) { 2370 if (delete $self->{need_reflow}) {
2224 my $height = 0; 2371 my $height = 0;
2225 2372
2226 $height += $_->[0] = $self->text_height ($_->[2]) 2373 my $layout = $self->{layout};
2374
2375 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2376
2227 for @{$self->{par}}; 2377 for (@{$self->{par}}) {
2378 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2379 $layout->set_width ($W - $_->[3]);
2380 $layout->set_markup ($_->[4]);
2381 my ($w, $h) = $layout->size;
2382 $_->[0] = $w + $_->[3];
2383 $_->[1] = $h;
2384 }
2385
2386 $height += $_->[1];
2387 }
2228 2388
2229 $self->{height} = $height; 2389 $self->{height} = $height;
2230 2390
2231 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2391 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2232 2392
2233 delete $self->{texture}; 2393 delete $self->{texture};
2234 } 2394 }
2235 2395
2236 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2396 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2237 glClearColor 0, 0, 0, 0; 2397 glClearColor 0.5, 0.5, 0.5, 0;
2238 glClear GL_COLOR_BUFFER_BIT; 2398 glClear GL_COLOR_BUFFER_BIT;
2239 2399
2400 my $top = int $self->{children}[1]{range}[0];
2401
2402 my $y0 = $top;
2403 my $y1 = $top + $H;
2404
2405 my $y = 0;
2406
2407 my $layout = $self->{layout};
2408
2409 $layout->set_font ($self->{font}) if $self->{font};
2410
2240 glEnable GL_BLEND; 2411 glEnable GL_BLEND;
2412 #TODO# not correct in windows where rgba is forced off
2241 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2413 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2242 glEnable GL_TEXTURE_2D;
2243 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2244
2245 my $top = int $self->{children}[1]{range}[0];
2246
2247 my $y0 = $top;
2248 my $y1 = $top + $self->{h};
2249
2250 my $y = 0;
2251
2252 my $layout = $self->{layout};
2253
2254 $layout->set_font ($self->{font}) if $self->{font};
2255 2414
2256 for my $par (@{$self->{par}}) { 2415 for my $par (@{$self->{par}}) {
2257 my $h = $par->[0]; 2416 my $h = $par->[1];
2258 2417
2259 if ($y0 < $y + $h && $y < $y1) { 2418 if ($y0 < $y + $h && $y < $y1) {
2419 $layout->set_foreground (@{ $par->[2] });
2420 $layout->set_width ($W - $par->[3]);
2260 $layout->set_text ($par->[2]); 2421 $layout->set_markup ($par->[4]);
2261 2422
2262 glColor @{ $par->[1] }; 2423 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2263 my ($W, $H) = $layout->size; 2424
2264 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2425 glRasterPos $par->[3], $y - $y0;
2426 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2265 } 2427 }
2266 2428
2267 $y += $h; 2429 $y += $h;
2268 } 2430 }
2269 2431
2270 glDisable GL_TEXTURE_2D;
2271 glDisable GL_BLEND; 2432 glDisable GL_BLEND;
2272 }; 2433 };
2273 }); 2434 });
2274} 2435}
2275 2436
2276sub _draw { 2437sub _draw {
2277 my ($self) = @_; 2438 my ($self) = @_;
2278 2439
2279 glEnable GL_BLEND;
2280 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2281 glEnable GL_TEXTURE_2D; 2440 glEnable GL_TEXTURE_2D;
2282 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2441 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2283 glColor 1, 1, 1, 1; 2442 glColor 1, 1, 1, 1;
2284 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2443 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2285 glDisable GL_TEXTURE_2D; 2444 glDisable GL_TEXTURE_2D;
2286 glDisable GL_BLEND;
2287 2445
2288 $self->{children}[1]->draw; 2446 $self->{children}[1]->draw;
2289 2447
2290} 2448}
2291 2449
2385 @_, 2543 @_,
2386 can_events => 0, 2544 can_events => 0,
2387 ) 2545 )
2388} 2546}
2389 2547
2390sub set_markup { 2548sub set_tooltip_from {
2391 my ($self, $text) = @_; 2549 my ($self, $widget) = @_;
2392 2550
2393 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2551 $self->add (new CFClient::UI::Label
2394 $self->{label}->set_markup ($text); 2552 markup => $widget->{tooltip},
2395 $self->add ($self->{label}); 2553 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2554 fontsize => 0.8,
2555 fg => [0, 0, 0, 1],
2556 ellipsise => 0,
2557 font => ($widget->{tooltip_font} || $::FONT_PROP),
2558 );
2396} 2559}
2397 2560
2398sub size_request { 2561sub size_request {
2399 my ($self) = @_; 2562 my ($self) = @_;
2400
2401 $self->child->set_max_size ($::WIDTH * 0.3);
2402 2563
2403 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2564 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2404 2565
2405 ($w + 4, $h + 4) 2566 ($w + 4, $h + 4)
2406} 2567}
2412} 2573}
2413 2574
2414sub _draw { 2575sub _draw {
2415 my ($self) = @_; 2576 my ($self) = @_;
2416 2577
2417 glPushMatrix;
2418 glTranslate 0.375, 0.375; 2578 glTranslate 0.375, 0.375;
2419 2579
2420 my ($w, $h) = @$self{qw(w h)}; 2580 my ($w, $h) = @$self{qw(w h)};
2421 2581
2422 glColor 1, 0.8, 0.4; 2582 glColor 1, 0.8, 0.4;
2433 glVertex 0 , $h; 2593 glVertex 0 , $h;
2434 glVertex $w, $h; 2594 glVertex $w, $h;
2435 glVertex $w, 0; 2595 glVertex $w, 0;
2436 glEnd; 2596 glEnd;
2437 2597
2438 glPopMatrix; 2598 glTranslate 2 - 0.375, 2 - 0.375;
2439
2440 glTranslate 2, 2;
2441 $self->SUPER::_draw; 2599 $self->SUPER::_draw;
2442} 2600}
2443 2601
2444############################################################################# 2602#############################################################################
2445 2603
2450use CFClient::OpenGL; 2608use CFClient::OpenGL;
2451 2609
2452sub new { 2610sub new {
2453 my $class = shift; 2611 my $class = shift;
2454 2612
2455 $class->SUPER::new ( 2613 my $self = $class->SUPER::new (
2456 aspect => 1, 2614 aspect => 1,
2457 @_, 2615 @_,
2458 ) 2616 );
2617
2618 if ($self->{anim} && $self->{animspeed}) {
2619 Scalar::Util::weaken (my $widget = $self);
2620
2621 $self->{timer} = Event->timer (
2622 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2623 hard => 1,
2624 interval => $self->{animspeed},
2625 cb => sub {
2626 ++$widget->{frame};
2627 $widget->update;
2628 },
2629 );
2630 }
2631
2632 $self
2459} 2633}
2460 2634
2461sub size_request { 2635sub size_request {
2462 (32, 8) 2636 (32, 8)
2463} 2637}
2464 2638
2639sub update {
2640 my ($self) = @_;
2641
2642 return unless $self->{visible};
2643
2644 $self->SUPER::update;
2645}
2646
2465sub _draw { 2647sub _draw {
2466 my ($self) = @_; 2648 my ($self) = @_;
2467 2649
2468 return unless $::CONN;#d# manage and cache textures differently 2650 return unless $::CONN;
2651
2652 my $face;
2653
2654 if ($self->{frame}) {
2655 my $anim = $::CONN->{anim}[$self->{anim}];
2656
2657 $face = $anim->[ $self->{frame} % @$anim ]
2658 if $anim && @$anim;
2659 }
2660
2469 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2661 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2470 2662
2471 # TODO animation
2472 if ($tex) { 2663 if ($tex) {
2473 glEnable GL_BLEND;
2474 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2475 glEnable GL_TEXTURE_2D; 2664 glEnable GL_TEXTURE_2D;
2476 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2477 glColor 1, 1, 1, 1; 2666 glColor 1, 1, 1, 1;
2478 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2667 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2479 glDisable GL_TEXTURE_2D; 2668 glDisable GL_TEXTURE_2D;
2480 glDisable GL_BLEND;
2481 } 2669 }
2670}
2671
2672sub DESTROY {
2673 my ($self) = @_;
2674
2675 $self->{timer}->cancel
2676 if $self->{timer};
2677
2678 $self->SUPER::DESTROY;
2482} 2679}
2483 2680
2484############################################################################# 2681#############################################################################
2485 2682
2486package CFClient::UI::InventoryItem; 2683package CFClient::UI::InventoryItem;
2487 2684
2488our @ISA = CFClient::UI::HBox::; 2685our @ISA = CFClient::UI::HBox::;
2686
2687sub _item_to_desc {
2688 my ($item) = @_;
2689
2690 my $desc =
2691 $item->{nrof} < 2
2692 ? $item->{name}
2693 : "$item->{nrof} × $item->{name_pl}";
2694
2695 $item->{flags} & Crossfire::Protocol::F_OPEN
2696 and $desc .= " (open)";
2697 $item->{flags} & Crossfire::Protocol::F_APPLIED
2698 and $desc .= " (applied)";
2699 $item->{flags} & Crossfire::Protocol::F_UNPAID
2700 and $desc .= " (unpaid)";
2701 $item->{flags} & Crossfire::Protocol::F_MAGIC
2702 and $desc .= " (magic)";
2703 $item->{flags} & Crossfire::Protocol::F_CURSED
2704 and $desc .= " (cursed)";
2705 $item->{flags} & Crossfire::Protocol::F_DAMNED
2706 and $desc .= " (damned)";
2707 $item->{flags} & Crossfire::Protocol::F_LOCKED
2708 and $desc .= " *";
2709
2710 $desc
2711}
2489 2712
2490sub new { 2713sub new {
2491 my $class = shift; 2714 my $class = shift;
2492 2715
2493 my %args = @_; 2716 my %args = @_;
2494 2717
2495 my $item = delete $args{item}; 2718 my $item = delete $args{item};
2496 2719
2497 my $desc = $item->{nrof} < 2 2720 my $desc = _item_to_desc ($item);
2498 ? $item->{name}
2499 : "$item->{nrof} $item->{name_pl}";
2500
2501 2721
2502 my $self = $class->SUPER::new ( 2722 my $self = $class->SUPER::new (
2503 can_hover => 1, 2723 can_hover => 1,
2504 can_events => 1, 2724 can_events => 1,
2505 tooltip => (CFClient::UI::Label->escape ($desc) 2725 tooltip => ((CFClient::UI::Label::escape $desc)
2506 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2726 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2507 connect_button_down => sub { 2727 connect_button_down => sub {
2508 my ($self, $ev, $x, $y) = @_; 2728 my ($self, $ev, $x, $y) = @_;
2509 2729
2510 # todo: maybe put examine on 1? but should just be a tooltip :( 2730 # todo: maybe put examine on 1? but should just be a tooltip :(
2731 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2732 my $targ = $::CONN->{player}{tag};
2733
2734 if ($item->{container} == $::CONN->{player}{tag}) {
2735 $targ = $main::OPENCONT;
2736 }
2737
2738 $::CONN->send ("move $targ $item->{tag} 0");
2511 if ($ev->{button} == 1) { 2739 } elsif ($ev->{button} == 1) {
2512 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2740 $::CONN->send ("examine $item->{tag}");
2513 } elsif ($ev->{button} == 2) { 2741 } elsif ($ev->{button} == 2) {
2514 $::CONN->send ("apply $item->{tag}"); 2742 $::CONN->send ("apply $item->{tag}");
2515 } elsif ($ev->{button} == 3) { 2743 } elsif ($ev->{button} == 3) {
2516 CFClient::UI::Menu->new ( 2744 my @menu_items = (
2517 items => [
2518 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2745 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2519 [
2520 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2521 sub { $::CONN->send ("lock $item->{tag}") },
2522 ],
2523 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2746 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2524 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2747 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2748 (
2749 $item->{flags} & Crossfire::Protocol::F_LOCKED
2750 ? (
2751 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2752 )
2753 : (
2754 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2525 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], 2755 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2756 )
2526 ], 2757 ),
2527 )->popup ($ev); 2758 );
2759
2760 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2528 } 2761 }
2529 2762
2530 1 2763 1
2531 }, 2764 },
2532 %args 2765 %args
2533 ); 2766 );
2767
2534 2768
2535 $self->add (new CFClient::UI::Face 2769 $self->add (new CFClient::UI::Face
2536 can_events => 0, 2770 can_events => 0,
2537 face => $item->{face}, 2771 face => $item->{face},
2538 anim => $item->{anim}, 2772 anim => $item->{anim},
2539 animspeed => $item->{animspeed}, 2773 animspeed => $item->{animspeed},
2540 ); 2774 );
2541 2775
2542 $self->add (new CFClient::UI::Label 2776 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2543 can_events => 0, 2777
2544 text => $desc, 2778 $self->{item} = $item;
2545 ); 2779
2780 $self->update_item;
2546 2781
2547 $self 2782 $self
2783}
2784
2785sub update_item {
2786 my ($self) = @_;
2787
2788 my $desc = _item_to_desc ($self->{item});
2789
2790 $self->{name_lbl}->set_text ($desc);
2548} 2791}
2549 2792
2550############################################################################# 2793#############################################################################
2551 2794
2552package CFClient::UI::Inventory; 2795package CFClient::UI::Inventory;
2555 2798
2556sub new { 2799sub new {
2557 my $class = shift; 2800 my $class = shift;
2558 2801
2559 my $self = $class->SUPER::new ( 2802 my $self = $class->SUPER::new (
2560 scrolled => (new CFClient::UI::VBox), 2803 scrolled => (new CFClient::UI::Table),
2561 @_, 2804 @_,
2562 ); 2805 );
2563 2806
2564 $self 2807 $self
2565} 2808}
2568 my ($self, $items) = @_; 2811 my ($self, $items) = @_;
2569 2812
2570 $self->{scrolled}->clear; 2813 $self->{scrolled}->clear;
2571 return unless $items; 2814 return unless $items;
2572 2815
2573 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2816 my @items = sort {
2817 ($a->{type} <=> $b->{type})
2818 or ($a->{name} cmp $b->{name})
2819 } @$items;
2574 2820
2575 $self->{real_items} = \@items; 2821 $self->{real_items} = \@items;
2576 2822
2577 for my $item (@items) { 2823 for my $item (@items) {
2578 my $desc = $item->{nrof} < 2 2824 $item->{item} = $item;
2579 ? $item->{name}
2580 : "$item->{nrof} $item->{name_pl}";
2581
2582 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2825 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2826 $item->update_item ();
2827 }
2828
2829 my $i = 0;
2830 for (@items) {
2831 $self->{scrolled}->add (0, $i, $_);
2832 my $nrof = $_->{item}->{nrof} || 1;
2833 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2583 } 2834 }
2584 2835
2585# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2836# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2586} 2837}
2587 2838
2666 2917
2667package CFClient::UI::Statusbox; 2918package CFClient::UI::Statusbox;
2668 2919
2669our @ISA = CFClient::UI::VBox::; 2920our @ISA = CFClient::UI::VBox::;
2670 2921
2922sub new {
2923 my $class = shift;
2924
2925 $class->SUPER::new (
2926 fontsize => 0.8,
2927 @_,
2928 )
2929}
2930
2671sub reorder { 2931sub reorder {
2672 my ($self) = @_; 2932 my ($self) = @_;
2673 my $NOW = time; 2933 my $NOW = time;
2674 2934
2675 while (my ($k, $v) = each %{ $self->{item} }) { 2935 while (my ($k, $v) = each %{ $self->{item} }) {
2676 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2936 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2677 } 2937 }
2678 2938
2679 my @widgets; 2939 my @widgets;
2680 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; 2940
2941 my @items = sort {
2942 $a->{pri} <=> $b->{pri}
2943 or $b->{id} <=> $a->{id}
2944 } values %{ $self->{item} };
2945
2681 my $count = 10 + 1; 2946 my $count = 10 + 1;
2682 for my $item (@items) { 2947 for my $item (@items) {
2683 last unless --$count; 2948 last unless --$count;
2684 2949
2685 push @widgets, $item->{label} ||= do { 2950 push @widgets, $item->{label} ||= do {
2686 # TODO: doesn't handle markup well (read as: at all) 2951 # TODO: doesn't handle markup well (read as: at all)
2687 my $short = delete $item->{text}; 2952 my $short = $item->{count} > 1
2953 ? "<b>$item->{count} ×</b> $item->{text}"
2954 : $item->{text};
2955
2688 for ($short) { 2956 for ($short) {
2689 s/^\s+//; 2957 s/^\s+//;
2690 s/\012.*//s; 2958 s/\s+/ /g;
2691 my $len = int 30 / $item->{fontsize};
2692 substr $_, $len, length, "…" if $len < length;
2693 } 2959 }
2694 2960
2695 new CFClient::UI::Label 2961 new CFClient::UI::Label
2696 markup => $short, 2962 markup => $short,
2697 tooltip => delete $item->{tooltip}, 2963 tooltip => $item->{tooltip},
2698 fontsize => delete $item->{fontsize}, 2964 tooltip_font => $::FONT_PROP,
2699 color => delete $item->{color}, 2965 tooltip_width => 0.67,
2966 fontsize => $item->{fontsize} || $self->{fontsize},
2967 max_w => $::WIDTH * 0.44,
2968 fg => $item->{fg},
2700 can_events => 1, 2969 can_events => 1,
2701 can_hover => 1, 2970 can_hover => 1
2702 }; 2971 };
2703 } 2972 }
2704 2973
2705 $self->clear; 2974 $self->clear;
2706 $self->SUPER::add (@widgets); 2975 $self->SUPER::add (reverse @widgets);
2707} 2976}
2708 2977
2709sub add { 2978sub add {
2710 my ($self, $text, %arg) = @_; 2979 my ($self, $text, %arg) = @_;
2711 2980
2712 my $item = { 2981 $text =~ s/^\s+//;
2713 time => time, 2982 $text =~ s/\s+$//;
2983
2984 my $timeout = time + ((delete $arg{timeout}) || 60);
2985
2986 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2987
2988 if (my $item = $self->{item}{$group}) {
2989 if ($item->{text} eq $text) {
2990 $item->{count}++;
2991 } else {
2992 $item->{count} = 1;
2993 $item->{text} = $item->{tooltip} = $text;
2994 }
2995 $item->{id} = ++$self->{id};
2996 $item->{timeout} = $timeout;
2997 delete $item->{label};
2998 } else {
2999 $self->{item}{$group} = {
3000 id => ++$self->{id},
2714 text => $text, 3001 text => $text,
2715 timeout => 60, 3002 timeout => $timeout,
2716 tooltip => $text, 3003 tooltip => $text,
2717 fontsize => 0.8,
2718 color => [0.8, 0.8, 0.8, 0.8], 3004 fg => [0.8, 0.8, 0.8, 0.8],
3005 pri => 0,
3006 count => 1,
2719 %arg, 3007 %arg,
3008 };
2720 }; 3009 }
2721
2722 $item->{timeout} += time;
2723 $item->{group} ||= $item+0;
2724
2725 $item = $self->{item}{$item->{group}} ||= $item;
2726 3010
2727 $self->reorder; 3011 $self->reorder;
3012}
3013
3014sub reconfigure {
3015 my ($self) = @_;
3016
3017 delete $_->{label}
3018 for values %{ $self->{item} || {} };
3019
3020 $self->reorder;
3021 $self->SUPER::reconfigure;
2728} 3022}
2729 3023
2730############################################################################# 3024#############################################################################
2731 3025
2732package CFClient::UI::Root; 3026package CFClient::UI::Root;
2764} 3058}
2765 3059
2766sub size_allocate { 3060sub size_allocate {
2767 my ($self, $w, $h) = @_; 3061 my ($self, $w, $h) = @_;
2768 3062
2769 my $old_w = $self->{old_w};
2770 my $old_h = $self->{old_h};
2771
2772 if ($old_w && $old_h) {
2773 for my $child ($self->children) {
2774 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2775 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2776 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2777 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2778 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2779 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2780 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2781 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2782 }
2783 }
2784
2785 for my $child ($self->children) { 3063 for my $child ($self->children) {
2786 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3064 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2787 3065
3066 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3067 if exists $child->{req_x};
3068
3069 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3070 if exists $child->{req_y};
3071
2788 $X = List::Util::max 0, List::Util::min $w - $W, $X; 3072 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2789 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 3073 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3074
2790 $child->configure ($X, $Y, $W, $H); 3075 $child->configure ($X, $Y, $W, $H);
2791 } 3076 }
2792
2793 $self->{old_w} = $w;
2794 $self->{old_h} = $h;
2795} 3077}
2796 3078
2797sub coord2local { 3079sub coord2local {
2798 my ($self, $x, $y) = @_; 3080 my ($self, $x, $y) = @_;
2799 3081
2812 $self->check_size; 3094 $self->check_size;
2813 $::WANT_REFRESH++; 3095 $::WANT_REFRESH++;
2814} 3096}
2815 3097
2816sub add { 3098sub add {
2817 my ($self, $child) = @_; 3099 my ($self, @children) = @_;
2818 3100
3101 for (my @widgets = @children; my $w = pop @widgets; ) {
3102 push @widgets, $w->children;
3103 $w->{root} = $self;
3104 $w->{visible} = 1;
3105 }
3106
3107 for my $child (@children) {
3108 $child->{is_toplevel} = 1;
3109
2819 # integerise window positions 3110 # integerise window positions
2820 $child->{x} = int $child->{x}; 3111 $child->{x} = int $child->{x};
2821 $child->{y} = int $child->{y}; 3112 $child->{y} = int $child->{y};
3113 }
2822 3114
2823 $self->SUPER::add ($child); 3115 $self->SUPER::add (@children);
3116}
3117
3118sub remove {
3119 my ($self, @children) = @_;
3120
3121 $self->SUPER::remove (@children);
3122
3123 while (@children) {
3124 my $w = pop @children;
3125 push @children, $w->children;
3126 delete $w->{visible};
3127 }
2824} 3128}
2825 3129
2826sub on_refresh { 3130sub on_refresh {
2827 my ($self, $id, $cb) = @_; 3131 my ($self, $id, $cb) = @_;
2828 3132
2829 $self->{refresh_hook}{$id} = $cb; 3133 $self->{refresh_hook}{$id} = $cb;
2830} 3134}
2831 3135
3136sub on_post_alloc {
3137 my ($self, $id, $cb) = @_;
3138
3139 $self->{post_alloc_hook}{$id} = $cb;
3140}
3141
2832sub draw { 3142sub draw {
2833 my ($self) = @_; 3143 my ($self) = @_;
3144
3145 while ($self->{refresh_hook}) {
3146 $_->()
3147 for values %{delete $self->{refresh_hook}};
3148 }
2834 3149
2835 if ($self->{check_size}) { 3150 if ($self->{check_size}) {
2836 my @queue = ([], []); 3151 my @queue = ([], []);
2837 3152
2838 for (;;) { 3153 for (;;) {
2846 3161
2847 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3162 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2848 ? @$widget{qw(user_w user_h)} 3163 ? @$widget{qw(user_w user_h)}
2849 : $widget->size_request; 3164 : $widget->size_request;
2850 3165
3166 if (delete $widget->{force_alloc}
2851 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 3167 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2852 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3168 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2853 3169
2854 $widget->{req_w} = $w; 3170 $widget->{req_w} = $w;
2855 $widget->{req_h} = $h; 3171 $widget->{req_h} = $h;
2856 3172
2857 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 3173 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2858 3174
2859 $widget->{parent}->check_size 3175 $widget->{parent}->check_size
2860 if $widget->{parent}; 3176 if $widget->{parent};
2861 } 3177 }
2862 } 3178 }
2874 $widget->size_allocate ($w, $h); 3190 $widget->size_allocate ($w, $h);
2875 $widget->emit (size_allocate => $w, $h); 3191 $widget->emit (size_allocate => $w, $h);
2876 } 3192 }
2877 } 3193 }
2878 3194
2879 while ($self->{refresh_hook}) { 3195 while ($self->{post_alloc_hook}) {
2880 $_->() 3196 $_->()
2881 for values %{delete $self->{refresh_hook}}; 3197 for values %{delete $self->{post_alloc_hook}};
2882 } 3198 }
2883 3199
2884 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3200 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2885 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3201 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2886 glClear GL_COLOR_BUFFER_BIT; 3202 glClear GL_COLOR_BUFFER_BIT;
2887 3203
2888 glMatrixMode GL_PROJECTION; 3204 glMatrixMode GL_PROJECTION;
2889 glLoadIdentity; 3205 glLoadIdentity;
2890 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3206 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2891 glMatrixMode GL_MODELVIEW; 3207 glMatrixMode GL_MODELVIEW;
2892 glLoadIdentity; 3208 glLoadIdentity;
2893 3209
2894 $self->_draw; 3210 $self->_draw;
2895} 3211}
2897############################################################################# 3213#############################################################################
2898 3214
2899package CFClient::UI; 3215package CFClient::UI;
2900 3216
2901$ROOT = new CFClient::UI::Root; 3217$ROOT = new CFClient::UI::Root;
2902$TOOLTIP = new CFClient::UI::Tooltip; 3218$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2903 3219
29041 32201
2905 3221

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines