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.195 by root, Thu May 11 23:54:31 2006 UTC vs.
Revision 1.220 by root, Mon May 22 03:28:55 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}, $widget->{tooltip_font}); 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 }
131 133
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;
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
149# call when resolution changes etc.
150sub rescale_widgets {
151 my ($sx, $sy) = @_;
152
153 # make a copy, otherwise for complains about freed values.
154 my @widgets = values %WIDGET;
155
156 for my $widget (@widgets) {
157 if ($widget->{toplevel}) {
158 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
159 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
160 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
162 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
163 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
164 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
165 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
166 }
167
168 $widget->reconfigure;
136 } 169 }
137} 170}
138 171
139############################################################################# 172#############################################################################
140 173
159 if (/^connect_(.*)$/) { 192 if (/^connect_(.*)$/) {
160 $self->connect ($1 => delete $self->{$_}); 193 $self->connect ($1 => delete $self->{$_});
161 } 194 }
162 } 195 }
163 196
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198
164 $self 199 $self
165} 200}
166 201
167sub destroy { 202sub destroy {
168 my ($self) = @_; 203 my ($self) = @_;
241 276
242sub size_allocate { 277sub size_allocate {
243 # nothing to be done 278 # nothing to be done
244} 279}
245 280
281sub reconfigure {
282 my ($self) = @_;
283
284 $self->check_size (1);
285 $self->update;
286}
287
246sub children { 288sub 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} 289}
260 290
261sub set_max_size { 291sub set_max_size {
262 my ($self, $w, $h) = @_; 292 my ($self, $w, $h) = @_;
263 293
264 delete $self->{max_w}; $self->{max_w} = $w if $w; 294 delete $self->{max_w}; $self->{max_w} = $w if $w;
265 delete $self->{max_h}; $self->{max_h} = $h if $h; 295 delete $self->{max_h}; $self->{max_h} = $h if $h;
296}
297
298sub set_tooltip {
299 my ($self, $tooltip) = @_;
300
301 $self->{tooltip} = $tooltip;
302
303 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
304 delete $CFClient::UI::TOOLTIP->{owner};
305 CFClient::UI::check_tooltip;
306 }
266} 307}
267 308
268# translate global coordinates to local coordinate system 309# translate global coordinates to local coordinate system
269sub coord2local { 310sub coord2local {
270 my ($self, $x, $y) = @_; 311 my ($self, $x, $y) = @_;
356 glVertex $self->{w}, 0; 397 glVertex $self->{w}, 0;
357 glVertex $self->{w}, $self->{h}; 398 glVertex $self->{w}, $self->{h};
358 glVertex 0 , $self->{h}; 399 glVertex 0 , $self->{h};
359 glEnd; 400 glEnd;
360 glPopMatrix; 401 glPopMatrix;
361 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 402 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
362 } 403 }
363} 404}
364 405
365sub _draw { 406sub _draw {
366 my ($self) = @_; 407 my ($self) = @_;
383sub set_parent { 424sub set_parent {
384 my ($self, $parent) = @_; 425 my ($self, $parent) = @_;
385 426
386 Scalar::Util::weaken ($self->{parent} = $parent); 427 Scalar::Util::weaken ($self->{parent} = $parent);
387 428
429 # TODO: req_w _does_change after ->reconfigure
388 $self->check_size 430 $self->check_size
389 unless exists $self->{req_w}; 431 unless exists $self->{req_w};
390} 432}
391 433
392sub check_size { 434sub check_size {
393 my ($self) = @_; 435 my ($self, $forced) = @_;
394 436
437 $self->{force_alloc} = 1 if $forced;
395 $CFClient::UI::ROOT->{check_size}{$self} = $self; 438 $CFClient::UI::ROOT->{check_size}{$self} = $self;
396} 439}
397 440
398sub update { 441sub update {
399 my ($self) = @_; 442 my ($self) = @_;
415} 458}
416 459
417sub DESTROY { 460sub DESTROY {
418 my ($self) = @_; 461 my ($self) = @_;
419 462
463 delete $WIDGET{$self+0};
420 #$self->deactivate; 464 #$self->deactivate;
421} 465}
422 466
423############################################################################# 467#############################################################################
424 468
433 my $class = shift; 477 my $class = shift;
434 478
435 # range [value, low, high, page] 479 # range [value, low, high, page]
436 480
437 $class->SUPER::new ( 481 $class->SUPER::new (
438 bg => [0, 0, 0, 0.2], 482 #bg => [0, 0, 0, 0.2],
439 active_bg => [1, 1, 1, 0.5], 483 #active_bg => [1, 1, 1, 0.5],
440 @_ 484 @_
441 ) 485 )
442} 486}
443 487
444sub _draw { 488sub _draw {
445 my ($self) = @_; 489 my ($self) = @_;
446 490
491 my $color = $FOCUS == $self && $self->{active_bg}
492 ? $self->{active_bg}
493 : $self->{bg};
494
495 if ($color && (@$color < 4 || $color->[3])) {
447 my ($w, $h) = @$self{qw(w h)}; 496 my ($w, $h) = @$self{qw(w h)};
448 497
449 glEnable GL_BLEND; 498 glEnable GL_BLEND;
450 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
451 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 500 glColor @$color;
452 501
453 glBegin GL_QUADS; 502 glBegin GL_QUADS;
454 glVertex 0 , 0; 503 glVertex 0 , 0;
455 glVertex 0 , $h; 504 glVertex 0 , $h;
456 glVertex $w, $h; 505 glVertex $w, $h;
457 glVertex $w, 0; 506 glVertex $w, 0;
458 glEnd; 507 glEnd;
459 508
460 glDisable GL_BLEND; 509 glDisable GL_BLEND;
510 }
461} 511}
462 512
463############################################################################# 513#############################################################################
464 514
465package CFClient::UI::Empty; 515package CFClient::UI::Empty;
509 $self->{children} = [ 559 $self->{children} = [
510 sort { $a->{z} <=> $b->{z} } 560 sort { $a->{z} <=> $b->{z} }
511 @{$self->{children}}, @widgets 561 @{$self->{children}}, @widgets
512 ]; 562 ];
513 563
514 $self->check_size; 564 $self->check_size (1);
515 $self->update; 565 $self->update;
516} 566}
517 567
518sub children { 568sub children {
519 @{ $_[0]{children} } 569 @{ $_[0]{children} }
626} 676}
627 677
628sub update { 678sub update {
629 my ($self) = @_; 679 my ($self) = @_;
630 680
631 $ROOT->on_refresh ($self => sub { $self->render_child }); 681 $ROOT->on_post_alloc ($self => sub { $self->render_child });
632 $self->SUPER::update; 682 $self->SUPER::update;
633} 683}
634 684
635sub size_allocate { 685sub size_allocate {
636 my ($self, $w, $h) = @_; 686 my ($self, $w, $h) = @_;
649 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 699 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
650 glClearColor 0, 0, 0, 0; 700 glClearColor 0, 0, 0, 0;
651 glClear GL_COLOR_BUFFER_BIT; 701 glClear GL_COLOR_BUFFER_BIT;
652 702
653 $self->_render; 703 $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 }; 704 };
662} 705}
663 706
664sub _draw { 707sub _draw {
665 my ($self) = @_; 708 my ($self) = @_;
669 my $tex = $self->{texture} 712 my $tex = $self->{texture}
670 or return; 713 or return;
671 714
672 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
673 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
674 glColor 0, 0, 0, 1; 717 glColor 1, 1, 1, 1;
675 718
676 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 719 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
677 720
678 glDisable GL_TEXTURE_2D; 721 glDisable GL_TEXTURE_2D;
679} 722}
762 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 805 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
763 }, 806 },
764 ; 807 ;
765 808
766 $self = $class->SUPER::new ( 809 $self = $class->SUPER::new (
767 vp => (new CFClient::UI::ViewPort), 810 vp => (new CFClient::UI::ViewPort expand => 1),
768 slider => $slider, 811 slider => $slider,
769 @_, 812 @_,
770 ); 813 );
771 814
772 $self->{vp}->add ($self->{scrolled}); 815 $self->{vp}->add ($self->{scrolled});
774 $self->add ($self->{slider}); 817 $self->add ($self->{slider});
775 818
776 $self 819 $self
777} 820}
778 821
779#TODO# update range on size_allocate depeneing on child 822#TODO# update range on size_allocate depending on child
780# update viewport offset on scroll 823# update viewport offset on scroll
781 824
782############################################################################# 825#############################################################################
783 826
784package CFClient::UI::Frame; 827package CFClient::UI::Frame;
788use CFClient::OpenGL; 831use CFClient::OpenGL;
789 832
790sub new { 833sub new {
791 my $class = shift; 834 my $class = shift;
792 835
793 my $self = $class->SUPER::new ( 836 $class->SUPER::new (
794 bg => [1, 1, 1, 1], 837 bg => undef,
795 border_bg => [1, 1, 1, 1],
796 border => 0.8,
797 @_ 838 @_,
798 ); 839 )
799
800 $self
801} 840}
802 841
803sub _draw { 842sub _draw {
804 my ($self) = @_; 843 my ($self) = @_;
805 844
806 my ($w, $h) = ($self->{w}, $self->{h}); 845 if ($self->{bg}) {
846 my ($w, $h) = @$self{qw(w h)};
807 847
808 glEnable GL_BLEND; 848 glEnable GL_BLEND;
809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 849 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
810 glEnable GL_TEXTURE_2D; 850 glColor @{ $self->{bg} };
811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
812 851
813# glBegin GL_QUADS; 852 glBegin GL_QUADS;
814# glColor 0, 0, 0, 0;
815# glVertex 0 , 0; 853 glVertex 0 , 0;
816# glVertex 0 , $h; 854 glVertex 0 , $h;
817# glVertex $w, $h; 855 glVertex $w, $h;
818# glVertex $w, 0; 856 glVertex $w, 0;
819# glEnd; 857 glEnd;
820 858
821
822 $self->child->draw;
823 glDisable GL_BLEND; 859 glDisable GL_BLEND;
824 glDisable GL_TEXTURE_2D; 860 }
861
862 $self->SUPER::_draw;
825} 863}
826 864
827############################################################################# 865#############################################################################
828 866
829package CFClient::UI::FancyFrame; 867package CFClient::UI::FancyFrame;
843 881
844 my $self = $class->SUPER::new ( 882 my $self = $class->SUPER::new (
845 bg => [1, 1, 1, 1], 883 bg => [1, 1, 1, 1],
846 border_bg => [1, 1, 1, 1], 884 border_bg => [1, 1, 1, 1],
847 border => 0.6, 885 border => 0.6,
886 toplevel => 1,
848 can_events => 1, 887 can_events => 1,
849 @_ 888 @_
850 ); 889 );
851 890
852 $self->{title} &&= new CFClient::UI::Label 891 $self->{title} &&= new CFClient::UI::Label
877 my ($self, $w, $h) = @_; 916 my ($self, $w, $h) = @_;
878 917
879 $h -= List::Util::max 0, $self->border * 2; 918 $h -= List::Util::max 0, $self->border * 2;
880 $w -= List::Util::max 0, $self->border * 2; 919 $w -= List::Util::max 0, $self->border * 2;
881 920
882 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 921 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
883 if $self->{title}; 922 if $self->{title};
884 923
885 $self->child->configure ($self->border, $self->border, $w, $h); 924 $self->child->configure ($self->border, $self->border, $w, $h);
886} 925}
887 926
945 my ($self) = @_; 984 my ($self) = @_;
946 985
947 my ($w, $h ) = ($self->{w}, $self->{h}); 986 my ($w, $h ) = ($self->{w}, $self->{h});
948 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 987 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
949 988
950 glEnable GL_BLEND;
951 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
952 glEnable GL_TEXTURE_2D; 989 glEnable GL_TEXTURE_2D;
953 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
954 991
955 my $border = $self->border; 992 my $border = $self->border;
956 993
957 glColor @{ $self->{border_bg} }; 994 glColor @{ $self->{border_bg} };
958 $tex[1]->draw_quad (0, 0, $w, $border); 995 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
959 $tex[3]->draw_quad (0, $border, $border, $ch); 996 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
960 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 997 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
961 $tex[4]->draw_quad (0, $h - $border, $w, $border); 998 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
962 999
963 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1000 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
964 my $bg = $tex[0]; 1001 my $bg = $tex[0];
965 1002
966 # TODO: repeat texture not scale 1003 # TODO: repeat texture not scale
970 glColor @{ $self->{bg} }; 1007 glColor @{ $self->{bg} };
971 1008
972 $bg->{s} = $rep_x; 1009 $bg->{s} = $rep_x;
973 $bg->{t} = $rep_y; 1010 $bg->{t} = $rep_y;
974 $bg->{wrap_mode} = 1; 1011 $bg->{wrap_mode} = 1;
975 $bg->draw_quad ($border, $border, $cw, $ch); 1012 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1013 }
976 1014
977 glDisable GL_TEXTURE_2D; 1015 glDisable GL_TEXTURE_2D;
978 glDisable GL_BLEND;
979 }
980 1016
981 $self->{title}->draw if $self->{title}; 1017 $self->{title}->draw if $self->{title};
982 1018
983 $self->child->draw; 1019 $self->child->draw;
984} 1020}
1251 1287
1252############################################################################# 1288#############################################################################
1253 1289
1254package CFClient::UI::Label; 1290package CFClient::UI::Label;
1255 1291
1256our @ISA = CFClient::UI::Base::; 1292our @ISA = CFClient::UI::DrawBG::;
1257 1293
1258use CFClient::OpenGL; 1294use CFClient::OpenGL;
1259 1295
1260sub new { 1296sub new {
1261 my ($class, %arg) = @_; 1297 my ($class, %arg) = @_;
1262 1298
1263 my $self = $class->SUPER::new ( 1299 my $self = $class->SUPER::new (
1264 fg => [1, 1, 1], 1300 fg => [1, 1, 1],
1301 #bg => none
1302 #active_bg => none
1265 #font => default_font 1303 #font => default_font
1266 #text => initial text 1304 #text => initial text
1267 #markup => initial narkup 1305 #markup => initial narkup
1306 #max_w => maximum pixel width
1307 ellipsise => 3, # end
1268 layout => (new CFClient::Layout), 1308 layout => (new CFClient::Layout),
1269 fontsize => 1, 1309 fontsize => 1,
1270 align => -1, 1310 align => -1,
1271 valign => -1, 1311 valign => -1,
1272 padding => 2, 1312 padding => 2,
1287 } 1327 }
1288 1328
1289 $self 1329 $self
1290} 1330}
1291 1331
1292sub escape { 1332sub escape($) {
1293 local $_ = $_[1]; 1333 local $_ = $_[0];
1294 1334
1295 s/&/&amp;/g; 1335 s/&/&amp;/g;
1296 s/>/&gt;/g; 1336 s/>/&gt;/g;
1297 s/</&lt;/g; 1337 s/</&lt;/g;
1298 1338
1299 $_[1] 1339 $_
1300} 1340}
1301 1341
1302sub update { 1342sub update {
1303 my ($self) = @_; 1343 my ($self) = @_;
1304 1344
1337sub size_request { 1377sub size_request {
1338 my ($self) = @_; 1378 my ($self) = @_;
1339 1379
1340 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1380 $self->{layout}->set_font ($self->{font}) if $self->{font};
1341 $self->{layout}->set_width ($self->{max_w} || -1); 1381 $self->{layout}->set_width ($self->{max_w} || -1);
1382 $self->{layout}->set_ellipsise ($self->{ellipsise});
1383 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1342 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1384 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1343 1385
1344 my ($w, $h) = $self->{layout}->size; 1386 my ($w, $h) = $self->{layout}->size;
1345 1387
1346 if (exists $self->{template}) { 1388 if (exists $self->{template}) {
1376} 1418}
1377 1419
1378sub _draw { 1420sub _draw {
1379 my ($self) = @_; 1421 my ($self) = @_;
1380 1422
1423 $self->SUPER::_draw; # draw background, if applicable
1424
1381 my $tex = $self->{texture} ||= do { 1425 my $tex = $self->{texture} ||= do {
1382 $self->{layout}->set_foreground (@{$self->{fg}}); 1426 $self->{layout}->set_foreground (@{$self->{fg}});
1383 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1427 $self->{layout}->set_font ($self->{font}) if $self->{font};
1384 $self->{layout}->set_width ($self->{w}); 1428 $self->{layout}->set_width ($self->{w});
1429 $self->{layout}->set_ellipsise ($self->{ellipsise});
1430 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1385 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1431 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1386 1432
1387 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1433 my $tex = new_from_layout CFClient::Texture $self->{layout};
1388 1434
1389 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1435 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1390 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1436 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1391 : ($self->{w} - $tex->{w}) * 0.5; 1437 : ($self->{w} - $tex->{w}) * 0.5);
1392 1438
1393 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1439 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1394 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1440 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1395 : ($self->{h} - $tex->{h}) * 0.5; 1441 : ($self->{h} - $tex->{h}) * 0.5);
1396 1442
1397 $tex 1443 $tex
1398 }; 1444 };
1399 1445
1400 glEnable GL_TEXTURE_2D; 1446 glEnable GL_TEXTURE_2D;
1455sub set_text { 1501sub set_text {
1456 my ($self, $text) = @_; 1502 my ($self, $text) = @_;
1457 1503
1458 $self->{cursor} = length $text; 1504 $self->{cursor} = length $text;
1459 $self->_set_text ($text); 1505 $self->_set_text ($text);
1506 $self->update;
1460 $self->check_size; 1507 $self->check_size;
1461 $self->update;
1462} 1508}
1463 1509
1464sub get_text { 1510sub get_text {
1465 $_[0]{text} 1511 $_[0]{text}
1466} 1512}
1486 my $sym = $ev->{sym}; 1532 my $sym = $ev->{sym};
1487 my $uni = $ev->{unicode}; 1533 my $uni = $ev->{unicode};
1488 1534
1489 my $text = $self->get_text; 1535 my $text = $self->get_text;
1490 1536
1491 if ($sym == 8) { 1537 if ($uni == 8) {
1492 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1538 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1493 } elsif ($sym == 127) { 1539 } elsif ($uni == 127) {
1494 substr $text, $self->{cursor}, 1, ""; 1540 substr $text, $self->{cursor}, 1, "";
1495 } elsif ($sym == CFClient::SDLK_LEFT) { 1541 } elsif ($sym == CFClient::SDLK_LEFT) {
1496 --$self->{cursor} if $self->{cursor}; 1542 --$self->{cursor} if $self->{cursor};
1497 } elsif ($sym == CFClient::SDLK_RIGHT) { 1543 } elsif ($sym == CFClient::SDLK_RIGHT) {
1498 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1544 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1499 } elsif ($sym == CFClient::SDLK_HOME) { 1545 } elsif ($sym == CFClient::SDLK_HOME) {
1500 $self->{cursor} = 0; 1546 $self->{cursor} = 0;
1501 } elsif ($sym == CFClient::SDLK_END) { 1547 } elsif ($sym == CFClient::SDLK_END) {
1502 $self->{cursor} = length $text; 1548 $self->{cursor} = length $text;
1503 } elsif ($sym == 27) { 1549 } elsif ($uni == 27) {
1504 $self->emit ('escape'); 1550 $self->emit ('escape');
1505 } elsif ($uni) { 1551 } elsif ($uni) {
1506 substr $text, $self->{cursor}++, 0, chr $uni; 1552 substr $text, $self->{cursor}++, 0, chr $uni;
1507 } 1553 }
1508 1554
1509 $self->_set_text ($text); 1555 $self->_set_text ($text);
1510 $self->update; 1556 $self->update;
1557 $self->check_size;
1511} 1558}
1512 1559
1513sub focus_in { 1560sub focus_in {
1514 my ($self) = @_; 1561 my ($self) = @_;
1515 1562
1644 my $class = shift; 1691 my $class = shift;
1645 1692
1646 $class->SUPER::new ( 1693 $class->SUPER::new (
1647 padding => 4, 1694 padding => 4,
1648 fg => [1, 1, 1], 1695 fg => [1, 1, 1],
1649 bg => [1, 1, 1, 0.2],
1650 active_fg => [0, 0, 1], 1696 active_fg => [0, 0, 1],
1651 can_hover => 1, 1697 can_hover => 1,
1652 align => 0, 1698 align => 0,
1653 valign => 0, 1699 valign => 0,
1654 can_events => 1, 1700 can_events => 1,
1702 1748
1703 $class->SUPER::new ( 1749 $class->SUPER::new (
1704 padding => 2, 1750 padding => 2,
1705 fg => [1, 1, 1], 1751 fg => [1, 1, 1],
1706 active_fg => [1, 1, 0], 1752 active_fg => [1, 1, 0],
1753 bg => [0, 0, 0, 0.2],
1754 active_bg => [1, 1, 1, 0.5],
1707 state => 0, 1755 state => 0,
1708 can_hover => 1, 1756 can_hover => 1,
1709 @_ 1757 @_
1710 ) 1758 )
1711} 1759}
1735 1783
1736 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1784 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1737 1785
1738 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1786 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1739 1787
1788 my $tex = $self->{state} ? $tex[1] : $tex[0];
1789
1740 glEnable GL_TEXTURE_2D; 1790 glEnable GL_TEXTURE_2D;
1741
1742 my $tex = $self->{state} ? $tex[1] : $tex[0];
1743
1744 $tex->draw_quad_alpha (0, 0, $s, $s); 1791 $tex->draw_quad_alpha (0, 0, $s, $s);
1745
1746 glDisable GL_TEXTURE_2D; 1792 glDisable GL_TEXTURE_2D;
1747} 1793}
1748 1794
1749############################################################################# 1795#############################################################################
1750 1796
1997 qw(s1_slider.png s1_slider_bg.png); 2043 qw(s1_slider.png s1_slider_bg.png);
1998 2044
1999sub new { 2045sub new {
2000 my $class = shift; 2046 my $class = shift;
2001 2047
2002 # range [value, low, high, page] 2048 # range [value, low, high, page, unit]
2003 2049
2004 # TODO: 0-width page 2050 # TODO: 0-width page
2005 # TODO: req_w/h are wrong with vertical 2051 # TODO: req_w/h are wrong with vertical
2006 # TODO: calculations are off 2052 # TODO: calculations are off
2007 my $self = $class->SUPER::new ( 2053 my $self = $class->SUPER::new (
2008 fg => [1, 1, 1], 2054 fg => [1, 1, 1],
2009 active_fg => [0, 0, 0], 2055 active_fg => [0, 0, 0],
2056 bg => [0, 0, 0, 0.2],
2057 active_bg => [1, 1, 1, 0.5],
2010 range => [0, 0, 100, 10], 2058 range => [0, 0, 100, 10],
2011 req_w => $::WIDTH / 80, 2059 req_w => $::WIDTH / 80,
2012 req_h => $::WIDTH / 80, 2060 req_h => $::WIDTH / 80,
2013 vertical => 0, 2061 vertical => 0,
2014 can_hover => 1, 2062 can_hover => 1,
2015 inner_pad => 5, 2063 inner_pad => 0.02,
2016 @_ 2064 @_
2017 ); 2065 );
2018 2066
2067 $self->set_value ($self->{range}[0]);
2068 $self->update;
2069
2019 $self 2070 $self
2071}
2072
2073sub set_value {
2074 my ($self, $value) = @_;
2075
2076 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2077
2078 $hi = $lo + 1 if $hi <= $lo;
2079
2080 $value = $lo if $value < $lo;
2081 $value = $hi if $value > $hi;
2082
2083 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2084 if $unit;
2085
2086 $page = $hi - $lo if $page > $hi - $lo;
2087
2088 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2089
2090 if ($value != $old_value) {
2091 $self->emit (changed => $value);
2092 $self->update;
2093 }
2020} 2094}
2021 2095
2022sub size_request { 2096sub size_request {
2023 my ($self) = @_; 2097 my ($self) = @_;
2024 2098
2037 2111
2038sub mouse_motion { 2112sub mouse_motion {
2039 my ($self, $ev, $x, $y) = @_; 2113 my ($self, $ev, $x, $y) = @_;
2040 2114
2041 if ($GRAB == $self) { 2115 if ($GRAB == $self) {
2116 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2117
2118 my (undef, $lo, $hi, $page) = @{$self->{range}};
2119
2120 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2121
2122 $self->set_value ($x * ($hi - $lo) + $lo);
2123 }
2124}
2125
2126sub update {
2127 my ($self) = @_;
2128
2129 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2130 $self->set_value ($self->{range}[0]);
2131
2042 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2132 my ($value, $lo, $hi, $page) = @{$self->{range}};
2043 2133
2044 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2134 my $inner_w = 1 - 2 * $self->{inner_pad};
2045 2135
2046 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2136 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2047 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2048 2137
2049 $x -= $inner_pad_px; # substract the padding 2138 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2050 $x = $x * ($hi - $lo) / $inner_w + $lo; 2139 $value = $self->{scale} * ($value - $lo);
2051 $x = $lo if $x < $lo;
2052 $x = $hi - $page if $x > $hi - $page;
2053 $self->{range}[0] = $x;
2054 2140
2055 $self->emit (changed => $x); 2141 $value = $self->{inner_pad} + ($value - $page * 0.5);
2056 $self->update; 2142
2143 $value = 0 if $value < 0;
2144 $page = 1 - $value if $value + $page > 1;
2145
2146 $self->{knob_x} = $value;
2147 $self->{knob_w} = $page;
2057 } 2148 });
2058}
2059 2149
2060# the inner_* stuff is for generating a padding for the slider handle, 2150 $self->SUPER::update;
2061# so that the handle doesn't leave the texture. This calculation isn't 100%
2062# correct propably, but it does the job for now
2063sub _calc_inner_pad_px {
2064 my ($self, $w) = @_;
2065 ($w / 100) * $self->{inner_pad} # % to pixels
2066} 2151}
2067 2152
2068sub _draw { 2153sub _draw {
2069 my ($self) = @_; 2154 my ($self) = @_;
2070 2155
2071 $self->SUPER::_draw (); 2156 $self->SUPER::_draw ();
2072 2157
2073 my ($w, $h) = @$self{qw(w h)}; 2158 glScale $self->{w}, $self->{h};
2074 2159
2075 if ($self->{vertical}) { 2160 if ($self->{vertical}) {
2076 # draw a vertical slider like a rotated horizontal slider 2161 # draw a vertical slider like a rotated horizontal slider
2077 2162
2163 glTranslate 1, 0, 0;
2078 glRotate 90, 0, 0, 1; 2164 glRotate 90, 0, 0, 1;
2079 glTranslate 0, -$self->{w}, 0;
2080
2081 ($w, $h) = ($h, $w);
2082 } 2165 }
2083 2166
2084 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2167 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2085 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2168 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2086 2169
2087 my ($value, $lo, $hi, $page) = @{$self->{range}};
2088
2089 $hi = $value + 1 if $lo == $hi;
2090
2091 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2092 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2093
2094 $page = int $page * $inner_w / ($hi - $lo);
2095 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2096
2097 $w -= $page;
2098 $page &= ~1;
2099 glTranslate $page * 0.5, 0, 0;
2100 $page ||= 2;
2101
2102 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2103 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2104
2105 glEnable GL_TEXTURE_2D; 2170 glEnable GL_TEXTURE_2D;
2106 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2171 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2107 2172
2108 # draw background 2173 # draw background
2109 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2174 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2110 2175
2111 # draw handle 2176 # draw handle
2112 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2177 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2113 2178
2114 glDisable GL_TEXTURE_2D; 2179 glDisable GL_TEXTURE_2D;
2115} 2180}
2116 2181
2117############################################################################# 2182#############################################################################
2150 2215
2151 $self->{fontsize} = $fontsize; 2216 $self->{fontsize} = $fontsize;
2152 $self->reflow; 2217 $self->reflow;
2153} 2218}
2154 2219
2220sub size_allocate {
2221 my ($self, $w, $h) = @_;
2222
2223 $self->SUPER::size_allocate ($w, $h);
2224
2225 $self->{layout}->set_font ($self->{font}) if $self->{font};
2226 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2227 $self->{layout}->set_width ($self->{children}[0]{w});
2228
2229 $self->reflow;
2230}
2231
2155sub text_height { 2232sub text_height {
2156 my ($self, $text) = @_; 2233 my ($self, $text, $indent) = @_;
2157 2234
2158 my $layout = $self->{layout}; 2235 my $layout = $self->{layout};
2159 2236
2160 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2237 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2161 $layout->set_width ($self->{children}[0]{w}); 2238 $layout->set_width ($self->{children}[0]{w} - $indent);
2162 $layout->set_markup ($text); 2239 $layout->set_markup ($text);
2163 2240
2164 ($layout->size)[1] 2241 ($layout->size)[1]
2165} 2242}
2166 2243
2169 2246
2170 $self->{need_reflow}++; 2247 $self->{need_reflow}++;
2171 $self->update; 2248 $self->update;
2172} 2249}
2173 2250
2174sub size_allocate {
2175 my ($self, $w, $h) = @_;
2176
2177 $self->SUPER::size_allocate ($w, $h);
2178
2179 $self->{layout}->set_font ($self->{font}) if $self->{font};
2180 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2181 $self->{layout}->set_width ($self->{children}[0]{w});
2182
2183 $self->reflow;
2184}
2185
2186sub add_paragraph { 2251sub add_paragraph {
2187 my ($self, $color, $text) = @_; 2252 my ($self, $color, $text, $indent) = @_;
2188 2253
2189 #TODO: intelligently "reformat" paragraph 2254 #TODO: intelligently "reformat" paragraph
2190 2255
2256 for my $line (split /\n/, $text) {
2191 my $height = $self->text_height ($text); 2257 my $height = $self->text_height ($line);
2192
2193 $self->{height} += $height; 2258 $self->{height} += $height;
2194
2195 push @{$self->{par}}, [$height, $color, $text]; 2259 push @{$self->{par}}, [$height, $color, $indent, $line];
2260 }
2196 2261
2197 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2262 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2198 $self->{children}[1]->update; 2263 $self->{children}[1]->update;
2199} 2264}
2200 2265
2205 2270
2206 return unless $self->{h} > 0; 2271 return unless $self->{h} > 0;
2207 2272
2208 delete $self->{texture}; 2273 delete $self->{texture};
2209 2274
2210 $ROOT->on_refresh ($self, sub { 2275 $ROOT->on_post_alloc ($self, sub {
2211 if (delete $self->{need_reflow}) { 2276 if (delete $self->{need_reflow}) {
2212 my $height = 0; 2277 my $height = 0;
2213 2278
2214 $height += $_->[0] = $self->text_height ($_->[2]) 2279 $height += $_->[0] = $self->text_height ($_->[3], $_->[2])
2215 for @{$self->{par}}; 2280 for @{$self->{par}};
2216 2281
2217 $self->{height} = $height; 2282 $self->{height} = $height;
2218 2283
2219 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2284 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2285 $self->{children}[1]->update;
2220 2286
2221 delete $self->{texture}; 2287 delete $self->{texture};
2222 } 2288 }
2223 2289
2224 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2290 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2225 glClearColor 0, 0, 0, 0; 2291 glClearColor 0.5, 0.5, 0.5, 0;
2226 glClear GL_COLOR_BUFFER_BIT; 2292 glClear GL_COLOR_BUFFER_BIT;
2227
2228 glEnable GL_TEXTURE_2D;
2229 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2230 2293
2231 my $top = int $self->{children}[1]{range}[0]; 2294 my $top = int $self->{children}[1]{range}[0];
2232 2295
2233 my $y0 = $top; 2296 my $y0 = $top;
2234 my $y1 = $top + $self->{h}; 2297 my $y1 = $top + $self->{h};
2237 2300
2238 my $layout = $self->{layout}; 2301 my $layout = $self->{layout};
2239 2302
2240 $layout->set_font ($self->{font}) if $self->{font}; 2303 $layout->set_font ($self->{font}) if $self->{font};
2241 2304
2305 glEnable GL_BLEND;
2306 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2307
2242 for my $par (@{$self->{par}}) { 2308 for my $par (@{$self->{par}}) {
2243 my $h = $par->[0]; 2309 my $h = $par->[0];
2244 2310
2245 if ($y0 < $y + $h && $y < $y1) { 2311 if ($y0 < $y + $h && $y < $y1) {
2246 $layout->set_foreground (@{ $par->[1] }); 2312 $layout->set_foreground (@{ $par->[1] });
2313 $layout->set_width ($self->{w} - $par->[2]);
2247 $layout->set_markup ($par->[2]); 2314 $layout->set_markup ($par->[3]);
2248 2315
2249 my ($W, $H) = $layout->size; 2316 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2250 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2317
2318 glRasterPos $par->[2], $y - $y0;
2319 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2251 } 2320 }
2252 2321
2253 $y += $h; 2322 $y += $h;
2254 } 2323 }
2255 2324
2256 glDisable GL_TEXTURE_2D; 2325 glDisable GL_BLEND;
2257 }; 2326 };
2258 }); 2327 });
2259} 2328}
2260 2329
2261sub _draw { 2330sub _draw {
2262 my ($self) = @_; 2331 my ($self) = @_;
2263 2332
2264 glEnable GL_TEXTURE_2D; 2333 glEnable GL_TEXTURE_2D;
2265 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2266 glColor 1, 1, 1, 1; 2335 glColor 1, 1, 1, 1;
2267 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2336 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2268 glDisable GL_TEXTURE_2D; 2337 glDisable GL_TEXTURE_2D;
2269 2338
2270 $self->{children}[1]->draw; 2339 $self->{children}[1]->draw;
2271 2340
2272} 2341}
2367 @_, 2436 @_,
2368 can_events => 0, 2437 can_events => 0,
2369 ) 2438 )
2370} 2439}
2371 2440
2372sub set_markup { 2441sub set_tooltip_from {
2373 my ($self, $text, $font) = @_; 2442 my ($self, $widget) = @_;
2374 2443
2375 $self->{label} = new CFClient::UI::Label 2444 $self->add (new CFClient::UI::Label
2445 markup => $widget->{tooltip},
2446 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2376 fontsize => 0.8, 2447 fontsize => 0.8,
2377 fg => [0, 0, 0], 2448 fg => [0, 0, 0, 1],
2449 ellipsise => 0,
2378 font => ($font || $::FONT_PROP); 2450 font => ($widget->{tooltip_font} || $::FONT_PROP),
2379 2451 );
2380 $self->{label}->set_max_size ($::WIDTH * 0.3);
2381 $self->{label}->set_markup ($text);
2382 $self->add ($self->{label});
2383} 2452}
2384 2453
2385sub size_request { 2454sub size_request {
2386 my ($self) = @_; 2455 my ($self) = @_;
2387 2456
2397} 2466}
2398 2467
2399sub _draw { 2468sub _draw {
2400 my ($self) = @_; 2469 my ($self) = @_;
2401 2470
2402 glPushMatrix;
2403 glTranslate 0.375, 0.375; 2471 glTranslate 0.375, 0.375;
2404 2472
2405 my ($w, $h) = @$self{qw(w h)}; 2473 my ($w, $h) = @$self{qw(w h)};
2406 2474
2407 glColor 1, 0.8, 0.4; 2475 glColor 1, 0.8, 0.4;
2418 glVertex 0 , $h; 2486 glVertex 0 , $h;
2419 glVertex $w, $h; 2487 glVertex $w, $h;
2420 glVertex $w, 0; 2488 glVertex $w, 0;
2421 glEnd; 2489 glEnd;
2422 2490
2423 glPopMatrix; 2491 glTranslate 2 - 0.375, 2 - 0.375;
2424
2425 glTranslate 2, 2;
2426 $self->SUPER::_draw; 2492 $self->SUPER::_draw;
2427} 2493}
2428 2494
2429############################################################################# 2495#############################################################################
2430 2496
2435use CFClient::OpenGL; 2501use CFClient::OpenGL;
2436 2502
2437sub new { 2503sub new {
2438 my $class = shift; 2504 my $class = shift;
2439 2505
2440 $class->SUPER::new ( 2506 my $self = $class->SUPER::new (
2441 aspect => 1, 2507 aspect => 1,
2442 @_, 2508 @_,
2443 ) 2509 );
2510
2511 if ($self->{anim} && $self->{animspeed}) {
2512 Scalar::Util::weaken (my $widget = $self);
2513
2514 $self->{timer} = Event->timer (
2515 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2516 hard => 1,
2517 interval => $self->{animspeed},
2518 cb => sub {
2519 ++$widget->{frame};
2520 $widget->update;
2521 },
2522 );
2523 }
2524
2525 $self
2444} 2526}
2445 2527
2446sub size_request { 2528sub size_request {
2447 (32, 8) 2529 (32, 8)
2448} 2530}
2449 2531
2450sub _draw { 2532sub _draw {
2451 my ($self) = @_; 2533 my ($self) = @_;
2452 2534
2453 return unless $::CONN;#d# manage and cache textures differently 2535 return unless $::CONN;#d# manage and cache textures differently
2536
2537 my $face;
2538
2539 if ($self->{frame}) {
2540 my $anim = $::CONN->{anim}[$self->{anim}];
2541
2542 $face = $anim->[ $self->{frame} % @$anim ]
2543 if $anim && @$anim;
2544 }
2545
2454 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2546 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2455 2547
2456 # TODO animation
2457 if ($tex) { 2548 if ($tex) {
2458 glEnable GL_TEXTURE_2D; 2549 glEnable GL_TEXTURE_2D;
2459 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2550 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2460 glColor 1, 1, 1, 1; 2551 glColor 1, 1, 1, 1;
2461 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2552 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2462 glDisable GL_TEXTURE_2D; 2553 glDisable GL_TEXTURE_2D;
2463 } 2554 }
2464} 2555}
2465 2556
2557sub DESTROY {
2558 my ($self) = @_;
2559
2560 $self->{timer}->cancel
2561 if $self->{timer};
2562
2563 $self->SUPER::DESTROY;
2564}
2565
2466############################################################################# 2566#############################################################################
2467 2567
2468package CFClient::UI::InventoryItem; 2568package CFClient::UI::InventoryItem;
2469 2569
2470our @ISA = CFClient::UI::HBox::; 2570our @ISA = CFClient::UI::HBox::;
2571
2572sub _item_to_desc {
2573 my ($item) = @_;
2574
2575 my $desc =
2576 $item->{nrof} < 2
2577 ? $item->{name}
2578 : "$item->{nrof} × $item->{name_pl}";
2579
2580 $item->{flags} & Crossfire::Protocol::F_OPEN
2581 and $desc .= " (open)";
2582 $item->{flags} & Crossfire::Protocol::F_APPLIED
2583 and $desc .= " (applied)";
2584 $item->{flags} & Crossfire::Protocol::F_UNPAID
2585 and $desc .= " (unpaid)";
2586 $item->{flags} & Crossfire::Protocol::F_MAGIC
2587 and $desc .= " (magic)";
2588 $item->{flags} & Crossfire::Protocol::F_CURSED
2589 and $desc .= " (cursed)";
2590 $item->{flags} & Crossfire::Protocol::F_DAMNED
2591 and $desc .= " (damned)";
2592 $item->{flags} & Crossfire::Protocol::F_LOCKED
2593 and $desc .= " *";
2594
2595 $desc
2596}
2471 2597
2472sub new { 2598sub new {
2473 my $class = shift; 2599 my $class = shift;
2474 2600
2475 my %args = @_; 2601 my %args = @_;
2476 2602
2477 my $item = delete $args{item}; 2603 my $item = delete $args{item};
2478 2604
2479 my $desc = $item->{nrof} < 2 2605 my $desc = _item_to_desc ($item);
2480 ? $item->{name}
2481 : "$item->{nrof} $item->{name_pl}";
2482
2483 2606
2484 my $self = $class->SUPER::new ( 2607 my $self = $class->SUPER::new (
2485 can_hover => 1, 2608 can_hover => 1,
2486 can_events => 1, 2609 can_events => 1,
2487 tooltip => (CFClient::UI::Label->escape ($desc) 2610 tooltip => ((CFClient::UI::Label::escape $desc)
2488 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2611 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2489 connect_button_down => sub { 2612 connect_button_down => sub {
2490 my ($self, $ev, $x, $y) = @_; 2613 my ($self, $ev, $x, $y) = @_;
2491 2614
2492 # todo: maybe put examine on 1? but should just be a tooltip :( 2615 # todo: maybe put examine on 1? but should just be a tooltip :(
2493 if ($ev->{button} == 1) { 2616 if ($ev->{button} == 1) {
2617 my $targ = $::CONN->{player}{tag};
2618
2619 if ($item->{container} == $::CONN->{player}{tag}) {
2620 $targ = $main::OPENCONT;
2621 }
2622
2494 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2623 $::CONN->send ("move $targ $item->{tag} 0");
2495 } elsif ($ev->{button} == 2) { 2624 } elsif ($ev->{button} == 2) {
2496 $::CONN->send ("apply $item->{tag}"); 2625 $::CONN->send ("apply $item->{tag}");
2497 } elsif ($ev->{button} == 3) { 2626 } elsif ($ev->{button} == 3) {
2498 CFClient::UI::Menu->new ( 2627 my @menu_items = (
2499 items => [
2500 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2628 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2501 [
2502 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2503 sub { $::CONN->send ("lock $item->{tag}") },
2504 ],
2505 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2629 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2506 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2630 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2631 (
2632 $item->{flags} & Crossfire::Protocol::F_LOCKED
2633 ? (
2634 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2635 )
2636 : (
2637 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2507 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], 2638 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2639 )
2508 ], 2640 ),
2509 )->popup ($ev); 2641 );
2642
2643 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2510 } 2644 }
2511 2645
2512 1 2646 1
2513 }, 2647 },
2514 %args 2648 %args
2515 ); 2649 );
2650
2516 2651
2517 $self->add (new CFClient::UI::Face 2652 $self->add (new CFClient::UI::Face
2518 can_events => 0, 2653 can_events => 0,
2519 face => $item->{face}, 2654 face => $item->{face},
2520 anim => $item->{anim}, 2655 anim => $item->{anim},
2521 animspeed => $item->{animspeed}, 2656 animspeed => $item->{animspeed},
2522 ); 2657 );
2523 2658
2524 $self->add (new CFClient::UI::Label 2659 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2525 can_events => 0, 2660
2526 text => $desc, 2661 $self->{item} = $item;
2527 ); 2662
2663 $self->update_item;
2528 2664
2529 $self 2665 $self
2666}
2667
2668sub update_item {
2669 my ($self) = @_;
2670
2671 my $desc = _item_to_desc ($self->{item});
2672
2673 $self->{name_lbl}->set_text ($desc);
2530} 2674}
2531 2675
2532############################################################################# 2676#############################################################################
2533 2677
2534package CFClient::UI::Inventory; 2678package CFClient::UI::Inventory;
2550 my ($self, $items) = @_; 2694 my ($self, $items) = @_;
2551 2695
2552 $self->{scrolled}->clear; 2696 $self->{scrolled}->clear;
2553 return unless $items; 2697 return unless $items;
2554 2698
2555 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2699 my @items = sort {
2700 ($a->{type} <=> $b->{type})
2701 or ($a->{name} cmp $b->{name})
2702 } @$items;
2556 2703
2557 $self->{real_items} = \@items; 2704 $self->{real_items} = \@items;
2558 2705
2559 for my $item (@items) { 2706 for my $item (@items) {
2560 my $desc = $item->{nrof} < 2
2561 ? $item->{name}
2562 : "$item->{nrof} $item->{name_pl}";
2563
2564 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2707 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2708 $item->update_item ();
2565 } 2709 }
2710
2711 $self->{scrolled}->add (@items);
2566 2712
2567# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2713# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2568} 2714}
2569 2715
2570sub size_request { 2716sub size_request {
2648 2794
2649package CFClient::UI::Statusbox; 2795package CFClient::UI::Statusbox;
2650 2796
2651our @ISA = CFClient::UI::VBox::; 2797our @ISA = CFClient::UI::VBox::;
2652 2798
2799sub new {
2800 my $class = shift;
2801
2802 $class->SUPER::new (
2803 fontsize => 0.8,
2804 @_,
2805 )
2806}
2807
2653sub reorder { 2808sub reorder {
2654 my ($self) = @_; 2809 my ($self) = @_;
2655 my $NOW = time; 2810 my $NOW = time;
2656 2811
2657 while (my ($k, $v) = each %{ $self->{item} }) { 2812 while (my ($k, $v) = each %{ $self->{item} }) {
2658 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2813 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2659 } 2814 }
2660 2815
2661 my @widgets; 2816 my @widgets;
2662 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; 2817
2818 my @items = sort {
2819 $a->{pri} <=> $b->{pri}
2820 or $b->{id} <=> $a->{id}
2821 } values %{ $self->{item} };
2822
2663 my $count = 10 + 1; 2823 my $count = 10 + 1;
2664 for my $item (@items) { 2824 for my $item (@items) {
2665 last unless --$count; 2825 last unless --$count;
2666 2826
2667 push @widgets, $item->{label} ||= do { 2827 push @widgets, $item->{label} ||= do {
2668 # TODO: doesn't handle markup well (read as: at all) 2828 # TODO: doesn't handle markup well (read as: at all)
2669 my $short = delete $item->{text}; 2829 my $short = $item->{count} > 1
2830 ? "<b>$item->{count} ×</b> $item->{text}"
2831 : $item->{text};
2832
2670 for ($short) { 2833 for ($short) {
2671 s/^\s+//; 2834 s/^\s+//;
2672 s/\012.*//s; 2835 s/\s+/ /g;
2673 my $len = int 30 / $item->{fontsize};
2674 substr $_, $len, length, "…" if $len < length;
2675 } 2836 }
2676 2837
2677 new CFClient::UI::Label 2838 new CFClient::UI::Label
2678 markup => $short, 2839 markup => $short,
2679 tooltip => delete $item->{tooltip}, 2840 tooltip => $item->{tooltip},
2680 fontsize => delete $item->{fontsize}, 2841 tooltip_font => $::FONT_PROP,
2681 color => delete $item->{color}, 2842 tooltip_width => 0.67,
2843 fontsize => $item->{fontsize} || $self->{fontsize},
2844 max_w => $::WIDTH * 0.44,
2845 fg => $item->{fg},
2682 can_events => 1, 2846 can_events => 1,
2683 can_hover => 1, 2847 can_hover => 1
2684 }; 2848 };
2685 } 2849 }
2686 2850
2687 $self->clear; 2851 $self->clear;
2688 $self->SUPER::add (@widgets); 2852 $self->SUPER::add (reverse @widgets);
2689} 2853}
2690 2854
2691sub add { 2855sub add {
2692 my ($self, $text, %arg) = @_; 2856 my ($self, $text, %arg) = @_;
2693 2857
2694 my $item = { 2858 $text =~ s/^\s+//;
2695 time => time, 2859 $text =~ s/\s+$//;
2860
2861 my $timeout = time + ((delete $arg{timeout}) || 60);
2862
2863 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2864
2865 if (my $item = $self->{item}{$group}) {
2866 if ($item->{text} eq $text) {
2867 $item->{count}++;
2868 } else {
2869 $item->{count} = 1;
2870 $item->{text} = $item->{tooltip} = $text;
2871 }
2872 $item->{id} = ++$self->{id};
2873 $item->{timeout} = $timeout;
2874 delete $item->{label};
2875 } else {
2876 $self->{item}{$group} = {
2877 id => ++$self->{id},
2696 text => $text, 2878 text => $text,
2697 timeout => 60, 2879 timeout => $timeout,
2698 tooltip => $text, 2880 tooltip => $text,
2699 fontsize => 0.8,
2700 color => [0.8, 0.8, 0.8, 0.8], 2881 fg => [0.8, 0.8, 0.8, 0.8],
2882 pri => 0,
2883 count => 1,
2701 %arg, 2884 %arg,
2885 };
2702 }; 2886 }
2703
2704 $item->{timeout} += time;
2705 $item->{group} ||= $item+0;
2706
2707 $item = $self->{item}{$item->{group}} ||= $item;
2708 2887
2709 $self->reorder; 2888 $self->reorder;
2889}
2890
2891sub reconfigure {
2892 my ($self) = @_;
2893
2894 delete $_->{label}
2895 for values %{ $self->{item} || {} };
2896
2897 $self->reorder;
2898 $self->SUPER::reconfigure;
2710} 2899}
2711 2900
2712############################################################################# 2901#############################################################################
2713 2902
2714package CFClient::UI::Root; 2903package CFClient::UI::Root;
2746} 2935}
2747 2936
2748sub size_allocate { 2937sub size_allocate {
2749 my ($self, $w, $h) = @_; 2938 my ($self, $w, $h) = @_;
2750 2939
2751 my $old_w = $self->{old_w}; 2940 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2752 my $old_h = $self->{old_h}; 2941 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2753 2942
2754 if ($old_w && $old_h) { 2943 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2755 for my $child ($self->children) { 2944 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2756 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2757 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2758 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2759 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2760 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2761 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2762 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2763 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2764 }
2765 }
2766 2945
2767 for my $child ($self->children) { 2946 for my $child ($self->children) {
2768 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2947 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2769 2948
2949 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2950 if exists $child->{req_x};
2951
2952 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2953 if exists $child->{req_y};
2954
2770 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2955 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2771 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2956 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2957
2772 $child->configure ($X, $Y, $W, $H); 2958 $child->configure ($X, $Y, $W, $H);
2773 } 2959 }
2774
2775 $self->{old_w} = $w;
2776 $self->{old_h} = $h;
2777} 2960}
2778 2961
2779sub coord2local { 2962sub coord2local {
2780 my ($self, $x, $y) = @_; 2963 my ($self, $x, $y) = @_;
2781 2964
2794 $self->check_size; 2977 $self->check_size;
2795 $::WANT_REFRESH++; 2978 $::WANT_REFRESH++;
2796} 2979}
2797 2980
2798sub add { 2981sub add {
2799 my ($self, $child) = @_; 2982 my ($self, @children) = @_;
2800 2983
2984 for my $child (@children) {
2985 $child->{toplevel} = 1;
2986
2801 # integerise window positions 2987 # integerise window positions
2802 $child->{x} = int $child->{x}; 2988 $child->{x} = int $child->{x};
2803 $child->{y} = int $child->{y}; 2989 $child->{y} = int $child->{y};
2990 }
2804 2991
2805 $self->SUPER::add ($child); 2992 $self->SUPER::add (@children);
2806} 2993}
2807 2994
2808sub on_refresh { 2995sub on_refresh {
2809 my ($self, $id, $cb) = @_; 2996 my ($self, $id, $cb) = @_;
2810 2997
2811 $self->{refresh_hook}{$id} = $cb; 2998 $self->{refresh_hook}{$id} = $cb;
2812} 2999}
2813 3000
3001sub on_post_alloc {
3002 my ($self, $id, $cb) = @_;
3003
3004 $self->{post_alloc_hook}{$id} = $cb;
3005}
3006
2814sub draw { 3007sub draw {
2815 my ($self) = @_; 3008 my ($self) = @_;
3009
3010 while ($self->{refresh_hook}) {
3011 $_->()
3012 for values %{delete $self->{refresh_hook}};
3013 }
2816 3014
2817 if ($self->{check_size}) { 3015 if ($self->{check_size}) {
2818 my @queue = ([], []); 3016 my @queue = ([], []);
2819 3017
2820 for (;;) { 3018 for (;;) {
2828 3026
2829 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3027 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2830 ? @$widget{qw(user_w user_h)} 3028 ? @$widget{qw(user_w user_h)}
2831 : $widget->size_request; 3029 : $widget->size_request;
2832 3030
3031 if (delete $widget->{force_alloc}
2833 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 3032 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2834 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3033 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2835 3034
2836 $widget->{req_w} = $w; 3035 $widget->{req_w} = $w;
2837 $widget->{req_h} = $h; 3036 $widget->{req_h} = $h;
2838 3037
2839 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 3038 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2840 3039
2841 $widget->{parent}->check_size 3040 $widget->{parent}->check_size
2842 if $widget->{parent}; 3041 if $widget->{parent};
2843 } 3042 }
2844 } 3043 }
2856 $widget->size_allocate ($w, $h); 3055 $widget->size_allocate ($w, $h);
2857 $widget->emit (size_allocate => $w, $h); 3056 $widget->emit (size_allocate => $w, $h);
2858 } 3057 }
2859 } 3058 }
2860 3059
2861 while ($self->{refresh_hook}) { 3060 while ($self->{post_alloc_hook}) {
2862 $_->() 3061 $_->()
2863 for values %{delete $self->{refresh_hook}}; 3062 for values %{delete $self->{post_alloc_hook}};
2864 } 3063 }
2865 3064
2866 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3065 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2867 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3066 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2868 glClear GL_COLOR_BUFFER_BIT; 3067 glClear GL_COLOR_BUFFER_BIT;
2869 3068
2870 glMatrixMode GL_PROJECTION; 3069 glMatrixMode GL_PROJECTION;
2871 glLoadIdentity; 3070 glLoadIdentity;
2872 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3071 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2873 glMatrixMode GL_MODELVIEW; 3072 glMatrixMode GL_MODELVIEW;
2874 glLoadIdentity; 3073 glLoadIdentity;
2875 3074
2876 $self->_draw; 3075 $self->_draw;
2877} 3076}
2879############################################################################# 3078#############################################################################
2880 3079
2881package CFClient::UI; 3080package CFClient::UI;
2882 3081
2883$ROOT = new CFClient::UI::Root; 3082$ROOT = new CFClient::UI::Root;
2884$TOOLTIP = new CFClient::UI::Tooltip; 3083$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2885 3084
28861 30851
2887 3086

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines