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.190 by root, Mon May 8 22:17:38 2006 UTC vs.
Revision 1.203 by root, Sun May 14 21:13:11 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
10our ($FOCUS, $HOVER, $GRAB); # various widgets 11our ($FOCUS, $HOVER, $GRAB); # various widgets
11 12
12our $ROOT; 13our $ROOT;
13our $TOOLTIP; 14our $TOOLTIP;
14our $BUTTON_STATE; 15our $BUTTON_STATE;
16
17our %WIDGET; # all widgets, weak-referenced
15 18
16sub check_tooltip { 19sub check_tooltip {
17 if (!$GRAB) { 20 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (length $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
23 26
24 my $tip = $widget->{tooltip}; 27 my $tip = $widget->{tooltip};
25 28
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 29 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 30
28 $TOOLTIP->set_markup ($widget->{tooltip}); 31 $TOOLTIP->set_tooltip_from ($widget);
29
30 $TOOLTIP->show; 32 $TOOLTIP->show;
31 33
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 35
34 if ($x + $TOOLTIP->{w} > $::WIDTH) { 36 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 37 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 } 38 }
37 39
38 $TOOLTIP->move ($x, $y); 40 $TOOLTIP->move ($x, $y);
41 $TOOLTIP->check_size;
42 $TOOLTIP->update;
39 } 43 }
40 44
41 return; 45 return;
42 } 46 }
43 } 47 }
130 134
131 for (@$vals) { 135 for (@$vals) {
132 my $i = int $_ + $rem; 136 my $i = int $_ + $rem;
133 $rem += $_ - $i; 137 $rem += $_ - $i;
134 $_ = $i; 138 $_ = $i;
139 }
140}
141
142# call when resolution changes etc.
143sub rescale_widgets {
144 my ($sx, $sy) = @_;
145
146 for my $widget (values %WIDGET) {
147 if ($widget->{toplevel}) {
148 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
149 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
150 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
151 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
152 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
153 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
154 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
155 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
156 }
157
158 $widget->reconfigure;
135 } 159 }
136} 160}
137 161
138############################################################################# 162#############################################################################
139 163
158 if (/^connect_(.*)$/) { 182 if (/^connect_(.*)$/) {
159 $self->connect ($1 => delete $self->{$_}); 183 $self->connect ($1 => delete $self->{$_});
160 } 184 }
161 } 185 }
162 186
187 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
188
163 $self 189 $self
164} 190}
165 191
166sub destroy { 192sub destroy {
167 my ($self) = @_; 193 my ($self) = @_;
232 $self->{y} = $y; 258 $self->{y} = $y;
233 $self->update; 259 $self->update;
234 } 260 }
235 261
236 if ($self->{w} != $w || $self->{h} != $h) { 262 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 263 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 $self->emit (size_allocate => $w, $h);
243 } 264 }
244} 265}
245 266
246sub size_allocate { 267sub size_allocate {
247 # nothing to be done 268 # nothing to be done
248} 269}
249 270
271sub reconfigure {
272 my ($self) = @_;
273
274 $self->check_size (1);
275 $self->update;
276}
277
250sub children { 278sub children {
251}
252
253# call when resolution changes etc.
254sub reconfigure {
255 my ($self) = @_;
256
257 $_->reconfigure
258 for $self->children;
259
260 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h});
262 $self->update;
263} 279}
264 280
265sub set_max_size { 281sub set_max_size {
266 my ($self, $w, $h) = @_; 282 my ($self, $w, $h) = @_;
267 283
268 delete $self->{max_w}; $self->{max_w} = $w if $w; 284 delete $self->{max_w}; $self->{max_w} = $w if $w;
269 delete $self->{max_h}; $self->{max_h} = $h if $h; 285 delete $self->{max_h}; $self->{max_h} = $h if $h;
270}
271
272# return top left coordinates
273sub _topleft {
274 my ($self, $x, $y) = @_;
275
276 $self->{parent}
277 or Carp::confess "no parent widget in _topleft\n";#d#
278
279 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
280} 286}
281 287
282# translate global coordinates to local coordinate system 288# translate global coordinates to local coordinate system
283sub coord2local { 289sub coord2local {
284 my ($self, $x, $y) = @_; 290 my ($self, $x, $y) = @_;
285 291
286 my ($X, $Y) = $self->_topleft; 292 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
287 ($x - $X, $y - $Y)
288} 293}
289 294
290# translate local coordinates to global coordinate system 295# translate local coordinates to global coordinate system
291sub coord2global { 296sub coord2global {
292 my ($self, $x, $y) = @_; 297 my ($self, $x, $y) = @_;
293 298
294 my ($X, $Y) = $self->_topleft; 299 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
295 ($x + $X, $y + $Y)
296} 300}
297 301
298sub focus_in { 302sub focus_in {
299 my ($self) = @_; 303 my ($self) = @_;
300 304
398 402
399sub set_parent { 403sub set_parent {
400 my ($self, $parent) = @_; 404 my ($self, $parent) = @_;
401 405
402 Scalar::Util::weaken ($self->{parent} = $parent); 406 Scalar::Util::weaken ($self->{parent} = $parent);
407
408 # TODO: req_w _does_change after ->reconfigure
409 $self->check_size
410 unless exists $self->{req_w};
403} 411}
404 412
405sub check_size { 413sub check_size {
406 my ($self) = @_; 414 my ($self, $forced) = @_;
407 415
408 $self->_check_size; 416 $self->{force_alloc} = 1 if $forced;
409 return;
410 #TODO: but needs a vastly more difficult algorithm
411 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
412
413 $check_size->{$self} = $self;
414
415 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
416 while (%$check_size) {
417 my @widgets = values %$check_size;
418 $_->_check_size
419 for @widgets;
420 }
421 });
422}
423
424sub _check_size {
425 my ($self) = @_;
426
427 delete $CFClient::UI::ROOT->{_check_size}{$self}; 417 $CFClient::UI::ROOT->{check_size}{$self} = $self;
428
429 $self->{parent}
430 or return 1;
431
432 my ($w, $h) = $self->{user_w} && $self->{user_h}
433 ? @$self{qw(user_w user_h)}
434 : $self->size_request;
435
436 if ($w != $self->{req_w} || $h != $self->{req_h}) {
437 $self->{req_w} = $w;
438 $self->{req_h} = $h;
439
440 $self->{parent}->_check_size
441 or $self->size_allocate (
442 (List::Util::max $self->{w}, $w),
443 (List::Util::max $self->{h}, $h),
444 );
445
446 1
447 } else {
448 0
449 }
450} 418}
451 419
452sub update { 420sub update {
453 my ($self) = @_; 421 my ($self) = @_;
454 422
455 my $update = $CFClient::UI::ROOT->{_update} ||= {}; 423 $self->{parent}->update
456 424 if $self->{parent};
457 $update->{$self} = $self;
458
459 $CFClient::UI::ROOT->on_refresh (_update => sub {
460 while (%$update) {
461 my @widgets = values %$update;
462 %$update = ();
463
464 $_->{parent} && $_->{parent}->update
465 for @widgets;
466 }
467 });
468} 425}
469 426
470sub connect { 427sub connect {
471 my ($self, $signal, $cb) = @_; 428 my ($self, $signal, $cb) = @_;
472 429
480} 437}
481 438
482sub DESTROY { 439sub DESTROY {
483 my ($self) = @_; 440 my ($self) = @_;
484 441
442 delete $WIDGET{$self+0};
485 #$self->deactivate; 443 #$self->deactivate;
486} 444}
487 445
488############################################################################# 446#############################################################################
489 447
574 $self->{children} = [ 532 $self->{children} = [
575 sort { $a->{z} <=> $b->{z} } 533 sort { $a->{z} <=> $b->{z} }
576 @{$self->{children}}, @widgets 534 @{$self->{children}}, @widgets
577 ]; 535 ];
578 536
579 $_->check_size 537 $self->check_size (1);
580 for @widgets;
581
582 $self->update; 538 $self->update;
583} 539}
584 540
585sub children { 541sub children {
586 @{ $_[0]{children} } 542 @{ $_[0]{children} }
693} 649}
694 650
695sub update { 651sub update {
696 my ($self) = @_; 652 my ($self) = @_;
697 653
698 $ROOT->on_refresh ($self => sub { $self->render_child }); 654 $ROOT->on_post_alloc ($self => sub { $self->render_child });
699 $self->SUPER::update; 655 $self->SUPER::update;
700} 656}
701 657
702sub size_allocate { 658sub size_allocate {
703 my ($self, $w, $h) = @_; 659 my ($self, $w, $h) = @_;
734 my ($w, $h) = ($self->w, $self->h); 690 my ($w, $h) = ($self->w, $self->h);
735 691
736 my $tex = $self->{texture} 692 my $tex = $self->{texture}
737 or return; 693 or return;
738 694
739 glEnable GL_BLEND;
740 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
741 glEnable GL_TEXTURE_2D; 695 glEnable GL_TEXTURE_2D;
742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 696 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
743 glColor 0, 0, 0, 1; 697 glColor 0, 0, 0, 1;
744 698
745 $tex->draw_quad (0, 0, $w, $h); 699 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
746 700
747 glDisable GL_BLEND;
748 glDisable GL_TEXTURE_2D; 701 glDisable GL_TEXTURE_2D;
749} 702}
750 703
751############################################################################# 704#############################################################################
752 705
776 $self->{view_y} = int $y; 729 $self->{view_y} = int $y;
777 730
778 $self->update; 731 $self->update;
779} 732}
780 733
781# hmm, this does not work for topleft of $self... but we should not aks for that 734# hmm, this does not work for topleft of $self... but we should not ask for that
782sub _topleft { 735sub coord2local {
783 my ($self, $x, $y) = @_; 736 my ($self, $x, $y) = @_;
784 737
785 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 738 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
739}
740
741sub coord2global {
742 my ($self, $x, $y) = @_;
743
744 $x = List::Util::min $self->{w}, $x - $self->{view_x};
745 $y = List::Util::min $self->{h}, $y - $self->{view_y};
746
747 $self->SUPER::coord2global ($x, $y)
786} 748}
787 749
788sub find_widget { 750sub find_widget {
789 my ($self, $x, $y) = @_; 751 my ($self, $x, $y) = @_;
790 752
835 $self->add ($self->{slider}); 797 $self->add ($self->{slider});
836 798
837 $self 799 $self
838} 800}
839 801
840#TODO# update range on size_allocate depeneing on child 802#TODO# update range on size_allocate depending on child
841# update viewport offset on scroll 803# update viewport offset on scroll
842 804
843############################################################################# 805#############################################################################
844 806
845package CFClient::UI::Frame; 807package CFClient::UI::Frame;
849use CFClient::OpenGL; 811use CFClient::OpenGL;
850 812
851sub new { 813sub new {
852 my $class = shift; 814 my $class = shift;
853 815
854 my $self = $class->SUPER::new ( 816 $class->SUPER::new (
855 bg => [1, 1, 1, 1], 817 bg => undef,
856 border_bg => [1, 1, 1, 1],
857 border => 0.8,
858 @_ 818 @_,
859 ); 819 )
860
861 $self
862} 820}
863 821
864sub _draw { 822sub _draw {
865 my ($self) = @_; 823 my ($self) = @_;
866 824
867 my ($w, $h) = ($self->{w}, $self->{h}); 825 if ($self->{bg}) {
826 my ($w, $h) = @$self{qw(w h)};
868 827
869 glEnable GL_BLEND; 828 glEnable GL_BLEND;
870 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 829 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
871 glEnable GL_TEXTURE_2D; 830 glColor @{ $self->{bg} };
872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
873 831
874# glBegin GL_QUADS; 832 glBegin GL_QUADS;
875# glColor 0, 0, 0, 0;
876# glVertex 0 , 0; 833 glVertex 0 , 0;
877# glVertex 0 , $h; 834 glVertex 0 , $h;
878# glVertex $w, $h; 835 glVertex $w, $h;
879# glVertex $w, 0; 836 glVertex $w, 0;
880# glEnd; 837 glEnd;
881 838
882
883 $self->child->draw;
884 glDisable GL_BLEND; 839 glDisable GL_BLEND;
885 glDisable GL_TEXTURE_2D; 840 }
841
842 $self->SUPER::_draw;
886} 843}
887 844
888############################################################################# 845#############################################################################
889 846
890package CFClient::UI::FancyFrame; 847package CFClient::UI::FancyFrame;
904 861
905 my $self = $class->SUPER::new ( 862 my $self = $class->SUPER::new (
906 bg => [1, 1, 1, 1], 863 bg => [1, 1, 1, 1],
907 border_bg => [1, 1, 1, 1], 864 border_bg => [1, 1, 1, 1],
908 border => 0.6, 865 border => 0.6,
866 toplevel => 1,
909 can_events => 1, 867 can_events => 1,
910 @_ 868 @_
911 ); 869 );
912 870
913 $self->{title} &&= new CFClient::UI::Label 871 $self->{title} &&= new CFClient::UI::Label
1006 my ($self) = @_; 964 my ($self) = @_;
1007 965
1008 my ($w, $h ) = ($self->{w}, $self->{h}); 966 my ($w, $h ) = ($self->{w}, $self->{h});
1009 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 967 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
1010 968
1011 glEnable GL_BLEND;
1012 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1013 glEnable GL_TEXTURE_2D; 969 glEnable GL_TEXTURE_2D;
1014 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 970 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1015 971
1016 my $border = $self->border; 972 my $border = $self->border;
1017 973
1018 glColor @{ $self->{border_bg} }; 974 glColor @{ $self->{border_bg} };
1019 $tex[1]->draw_quad (0, 0, $w, $border); 975 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
1020 $tex[3]->draw_quad (0, $border, $border, $ch); 976 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
1021 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 977 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1022 $tex[4]->draw_quad (0, $h - $border, $w, $border); 978 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
1023 979
1024 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 980 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1025 my $bg = $tex[0]; 981 my $bg = $tex[0];
1026 982
1027 # TODO: repeat texture not scale 983 # TODO: repeat texture not scale
1031 glColor @{ $self->{bg} }; 987 glColor @{ $self->{bg} };
1032 988
1033 $bg->{s} = $rep_x; 989 $bg->{s} = $rep_x;
1034 $bg->{t} = $rep_y; 990 $bg->{t} = $rep_y;
1035 $bg->{wrap_mode} = 1; 991 $bg->{wrap_mode} = 1;
1036 $bg->draw_quad ($border, $border, $cw, $ch); 992 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
993 }
1037 994
1038 glDisable GL_TEXTURE_2D; 995 glDisable GL_TEXTURE_2D;
1039 glDisable GL_BLEND;
1040 }
1041 996
1042 $self->{title}->draw if $self->{title}; 997 $self->{title}->draw if $self->{title};
1043 998
1044 $self->child->draw; 999 $self->child->draw;
1045} 1000}
1272} 1227}
1273 1228
1274sub size_allocate { 1229sub size_allocate {
1275 my ($self, $w, $h) = @_; 1230 my ($self, $w, $h) = @_;
1276 1231
1232 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1233
1277 my $children = $self->{children}; 1234 my $children = $self->{children};
1278 1235
1279 my @h = map $_->{req_h}, @$children; 1236 my @h = map $_->{req_h}, @$children;
1280 1237
1281 my $req_h = List::Util::sum @h; 1238 my $req_h = List::Util::sum @h;
1320 my ($class, %arg) = @_; 1277 my ($class, %arg) = @_;
1321 1278
1322 my $self = $class->SUPER::new ( 1279 my $self = $class->SUPER::new (
1323 fg => [1, 1, 1], 1280 fg => [1, 1, 1],
1324 #font => default_font 1281 #font => default_font
1282 #text => initial text
1283 #markup => initial narkup
1284 layout => (new CFClient::Layout),
1325 fontsize => 1, 1285 fontsize => 1,
1326 text => "",
1327 align => -1, 1286 align => -1,
1328 valign => -1, 1287 valign => -1,
1329 padding => 2, 1288 padding => 2,
1330 layout => new CFClient::Layout,
1331 can_events => 0, 1289 can_events => 0,
1332 %arg 1290 %arg
1333 ); 1291 );
1334 1292
1335 if (exists $self->{template}) { 1293 if (exists $self->{template}) {
1336 my $layout = new CFClient::Layout; 1294 my $layout = new CFClient::Layout;
1337 $layout->set_text (delete $self->{template}); 1295 $layout->set_text (delete $self->{template});
1338 $self->{template} = $layout; 1296 $self->{template} = $layout;
1339 } 1297 }
1340 1298
1341 $self->set_text (delete $self->{text}) if exists $self->{text}; 1299 if (exists $self->{markup}) {
1342 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1300 $self->set_markup (delete $self->{markup});
1301 } else {
1302 $self->set_text (delete $self->{text});
1303 }
1343 1304
1344 $self 1305 $self
1345} 1306}
1346 1307
1347sub escape { 1308sub escape {
1365 my ($self, $text) = @_; 1326 my ($self, $text) = @_;
1366 1327
1367 return if $self->{text} eq "T$text"; 1328 return if $self->{text} eq "T$text";
1368 $self->{text} = "T$text"; 1329 $self->{text} = "T$text";
1369 1330
1331 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1370 $self->{layout}->set_text ($text); 1332 $self->{layout}->set_text ($text);
1371 1333
1372 delete $self->{texture};
1373 $self->update; 1334 $self->update;
1374 $self->check_size; 1335 $self->check_size;
1375} 1336}
1376 1337
1377sub set_markup { 1338sub set_markup {
1378 my ($self, $markup) = @_; 1339 my ($self, $markup) = @_;
1379 1340
1380 return if $self->{text} eq "M$markup"; 1341 return if $self->{text} eq "M$markup";
1381 $self->{text} = "M$markup"; 1342 $self->{text} = "M$markup";
1382 1343
1344 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1345
1346 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1383 $self->{layout}->set_markup ($markup); 1347 $self->{layout}->set_markup ($markup);
1384 1348
1385 delete $self->{texture};
1386 $self->update; 1349 $self->update;
1387 $self->check_size; 1350 $self->check_size;
1388} 1351}
1389 1352
1390sub size_request { 1353sub size_request {
1430 1393
1431sub _draw { 1394sub _draw {
1432 my ($self) = @_; 1395 my ($self) = @_;
1433 1396
1434 my $tex = $self->{texture} ||= do { 1397 my $tex = $self->{texture} ||= do {
1398 $self->{layout}->set_foreground (@{$self->{fg}});
1435 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1399 $self->{layout}->set_font ($self->{font}) if $self->{font};
1436 $self->{layout}->set_width ($self->{w}); 1400 $self->{layout}->set_width ($self->{w});
1437 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1401 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1402
1438 new_from_layout CFClient::Texture $self->{layout} 1403 my $tex = new_from_layout CFClient::Texture $self->{layout};
1404
1405 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1406 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1407 : ($self->{w} - $tex->{w}) * 0.5;
1408
1409 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1410 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1411 : ($self->{h} - $tex->{h}) * 0.5;
1412
1413 $tex
1439 }; 1414 };
1440 1415
1441 glEnable GL_BLEND;
1442 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1443 glEnable GL_TEXTURE_2D; 1416 glEnable GL_TEXTURE_2D;
1444 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1445 1418
1419 if ($tex->{format} == GL_ALPHA) {
1446 glColor @{$self->{fg}}; 1420 glColor @{$self->{fg}};
1447
1448 $self->{ox} = int (
1449 $self->{align} < 0 ? $self->{padding}
1450 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1451 : ($self->{w} - $tex->{w}) * 0.5
1452 );
1453
1454 $self->{oy} = int (
1455 $self->{valign} < 0 ? $self->{padding}
1456 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1457 : ($self->{h} - $tex->{h}) * 0.5
1458 );
1459
1460 $tex->draw_quad ($self->{ox}, $self->{oy}); 1421 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1422 } else {
1423 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1424 }
1461 1425
1462 glDisable GL_TEXTURE_2D; 1426 glDisable GL_TEXTURE_2D;
1463 glDisable GL_BLEND;
1464} 1427}
1465 1428
1466############################################################################# 1429#############################################################################
1467 1430
1468package CFClient::UI::EntryBase; 1431package CFClient::UI::EntryBase;
1503 $self->{layout}->set_text ("$text "); 1466 $self->{layout}->set_text ("$text ");
1504 1467
1505 $self->emit (changed => $self->{text}); 1468 $self->emit (changed => $self->{text});
1506} 1469}
1507 1470
1471sub set_text {
1472 my ($self, $text) = @_;
1473
1474 $self->{cursor} = length $text;
1475 $self->_set_text ($text);
1476 $self->check_size;
1477 $self->update;
1478}
1479
1508sub get_text { 1480sub get_text {
1509 $_[0]{text} 1481 $_[0]{text}
1510} 1482}
1511 1483
1512sub size_request { 1484sub size_request {
1519 1491
1520sub size_allocate { 1492sub size_allocate {
1521 my ($self, $w, $h) = @_; 1493 my ($self, $w, $h) = @_;
1522 1494
1523 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1495 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1524}
1525
1526sub set_text {
1527 my ($self, $text) = @_;
1528
1529 $self->{cursor} = length $text;
1530 $self->_set_text ($text);
1531 $self->update;
1532} 1496}
1533 1497
1534sub key_down { 1498sub key_down {
1535 my ($self, $ev) = @_; 1499 my ($self, $ev) = @_;
1536 1500
1538 my $sym = $ev->{sym}; 1502 my $sym = $ev->{sym};
1539 my $uni = $ev->{unicode}; 1503 my $uni = $ev->{unicode};
1540 1504
1541 my $text = $self->get_text; 1505 my $text = $self->get_text;
1542 1506
1543 if ($sym == 8) { 1507 if ($uni == 8) {
1544 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1508 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1545 } elsif ($sym == 127) { 1509 } elsif ($uni == 127) {
1546 substr $text, $self->{cursor}, 1, ""; 1510 substr $text, $self->{cursor}, 1, "";
1547 } elsif ($sym == CFClient::SDLK_LEFT) { 1511 } elsif ($sym == CFClient::SDLK_LEFT) {
1548 --$self->{cursor} if $self->{cursor}; 1512 --$self->{cursor} if $self->{cursor};
1549 } elsif ($sym == CFClient::SDLK_RIGHT) { 1513 } elsif ($sym == CFClient::SDLK_RIGHT) {
1550 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1514 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1551 } elsif ($sym == CFClient::SDLK_HOME) { 1515 } elsif ($sym == CFClient::SDLK_HOME) {
1552 $self->{cursor} = 0; 1516 $self->{cursor} = 0;
1553 } elsif ($sym == CFClient::SDLK_END) { 1517 } elsif ($sym == CFClient::SDLK_END) {
1554 $self->{cursor} = length $text; 1518 $self->{cursor} = length $text;
1555 } elsif ($sym == 27) { 1519 } elsif ($uni == 27) {
1556 $self->emit ('escape'); 1520 $self->emit ('escape');
1557 } elsif ($uni) { 1521 } elsif ($uni) {
1558 substr $text, $self->{cursor}++, 0, chr $uni; 1522 substr $text, $self->{cursor}++, 0, chr $uni;
1559 } 1523 }
1560 1524
1724 1688
1725 if ($GRAB == $self) { 1689 if ($GRAB == $self) {
1726 $self->{fg} = $self->{active_fg}; 1690 $self->{fg} = $self->{active_fg};
1727 } 1691 }
1728 1692
1729 glEnable GL_BLEND;
1730 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1731 glEnable GL_TEXTURE_2D; 1693 glEnable GL_TEXTURE_2D;
1732 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1733 glColor 0, 0, 0, 1; 1695 glColor 0, 0, 0, 1;
1734 1696
1735 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1697 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1736 1698
1737 glDisable GL_TEXTURE_2D; 1699 glDisable GL_TEXTURE_2D;
1738 glDisable GL_BLEND;
1739 1700
1740 $self->SUPER::_draw; 1701 $self->SUPER::_draw;
1741} 1702}
1742 1703
1743############################################################################# 1704#############################################################################
1790 1751
1791 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1752 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1792 1753
1793 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1754 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1794 1755
1795 glEnable GL_BLEND; 1756 my $tex = $self->{state} ? $tex[1] : $tex[0];
1757
1796 glEnable GL_TEXTURE_2D; 1758 glEnable GL_TEXTURE_2D;
1797 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1798
1799 my $tex = $self->{state} ? $tex[1] : $tex[0];
1800
1801 $tex->draw_quad (0, 0, $s, $s); 1759 $tex->draw_quad_alpha (0, 0, $s, $s);
1802
1803 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1804 glDisable GL_BLEND;
1805} 1761}
1806 1762
1807############################################################################# 1763#############################################################################
1808 1764
1809package CFClient::UI::Image; 1765package CFClient::UI::Image;
1852 glTranslate 0, -$self->{w}, 0; 1808 glTranslate 0, -$self->{w}, 0;
1853 1809
1854 ($w, $h) = ($h, $w); 1810 ($w, $h) = ($h, $w);
1855 } 1811 }
1856 1812
1857 glEnable GL_BLEND;
1858 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1859 glEnable GL_TEXTURE_2D; 1813 glEnable GL_TEXTURE_2D;
1860 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1814 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1861 1815
1862 $tex->draw_quad (0, 0, $w, $h); 1816 $tex->draw_quad_alpha (0, 0, $w, $h);
1863 1817
1864 glDisable GL_BLEND;
1865 glDisable GL_TEXTURE_2D; 1818 glDisable GL_TEXTURE_2D;
1866} 1819}
1867 1820
1868############################################################################# 1821#############################################################################
1869 1822
2161 $page ||= 2; 2114 $page ||= 2;
2162 2115
2163 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2116 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2164 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2117 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2165 2118
2166 glEnable GL_BLEND;
2167 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2168 glEnable GL_TEXTURE_2D; 2119 glEnable GL_TEXTURE_2D;
2169 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2120 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2170 2121
2171 # draw background 2122 # draw background
2172 $tex[1]->draw_quad (0, 0, $w, $h); 2123 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2173 2124
2174 # draw handle 2125 # draw handle
2175 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2126 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2176 2127
2177 glDisable GL_BLEND;
2178 glDisable GL_TEXTURE_2D; 2128 glDisable GL_TEXTURE_2D;
2179} 2129}
2180 2130
2181############################################################################# 2131#############################################################################
2182 2132
2193 fontsize => 1, 2143 fontsize => 1,
2194 can_events => 0, 2144 can_events => 0,
2195 #font => default_font 2145 #font => default_font
2196 @_, 2146 @_,
2197 2147
2198 layout => (new CFClient::Layout), 2148 layout => (new CFClient::Layout 1),
2199 par => [], 2149 par => [],
2200 height => 0, 2150 height => 0,
2201 children => [ 2151 children => [
2202 (new CFClient::UI::Empty expand => 1), 2152 (new CFClient::UI::Empty expand => 1),
2203 (new CFClient::UI::Slider vertical => 1), 2153 (new CFClient::UI::Slider vertical => 1),
2221 2171
2222 my $layout = $self->{layout}; 2172 my $layout = $self->{layout};
2223 2173
2224 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2174 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2225 $layout->set_width ($self->{children}[0]{w}); 2175 $layout->set_width ($self->{children}[0]{w});
2226 $layout->set_text ($text); 2176 $layout->set_markup ($text);
2227 2177
2228 ($layout->size)[1] 2178 ($layout->size)[1]
2229} 2179}
2230 2180
2231sub reflow { 2181sub reflow {
2269 2219
2270 return unless $self->{h} > 0; 2220 return unless $self->{h} > 0;
2271 2221
2272 delete $self->{texture}; 2222 delete $self->{texture};
2273 2223
2274 $ROOT->on_refresh ($self, sub { 2224 $ROOT->on_post_alloc ($self, sub {
2275 if (delete $self->{need_reflow}) { 2225 if (delete $self->{need_reflow}) {
2276 my $height = 0; 2226 my $height = 0;
2277 2227
2278 $height += $_->[0] = $self->text_height ($_->[2]) 2228 $height += $_->[0] = $self->text_height ($_->[2])
2279 for @{$self->{par}}; 2229 for @{$self->{par}};
2287 2237
2288 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2238 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2289 glClearColor 0, 0, 0, 0; 2239 glClearColor 0, 0, 0, 0;
2290 glClear GL_COLOR_BUFFER_BIT; 2240 glClear GL_COLOR_BUFFER_BIT;
2291 2241
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D; 2242 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2243 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 2244
2297 my $top = int $self->{children}[1]{range}[0]; 2245 my $top = int $self->{children}[1]{range}[0];
2298 2246
2307 2255
2308 for my $par (@{$self->{par}}) { 2256 for my $par (@{$self->{par}}) {
2309 my $h = $par->[0]; 2257 my $h = $par->[0];
2310 2258
2311 if ($y0 < $y + $h && $y < $y1) { 2259 if ($y0 < $y + $h && $y < $y1) {
2260 $layout->set_foreground (@{ $par->[1] });
2312 $layout->set_text ($par->[2]); 2261 $layout->set_markup ($par->[2]);
2313 2262
2314 glColor @{ $par->[1] };
2315 my ($W, $H) = $layout->size; 2263 my ($W, $H) = $layout->size;
2316 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2264 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2317 } 2265 }
2318 2266
2319 $y += $h; 2267 $y += $h;
2320 } 2268 }
2321 2269
2322 glDisable GL_TEXTURE_2D; 2270 glDisable GL_TEXTURE_2D;
2323 glDisable GL_BLEND;
2324 }; 2271 };
2325 }); 2272 });
2326} 2273}
2327 2274
2328sub _draw { 2275sub _draw {
2329 my ($self) = @_; 2276 my ($self) = @_;
2330 2277
2331 glEnable GL_BLEND;
2332 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2333 glEnable GL_TEXTURE_2D; 2278 glEnable GL_TEXTURE_2D;
2334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2279 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2335 glColor 1, 1, 1, 1; 2280 glColor 1, 1, 1, 1;
2336 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2281 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2337 glDisable GL_TEXTURE_2D; 2282 glDisable GL_TEXTURE_2D;
2338 glDisable GL_BLEND;
2339 2283
2340 $self->{children}[1]->draw; 2284 $self->{children}[1]->draw;
2341 2285
2342} 2286}
2343 2287
2437 @_, 2381 @_,
2438 can_events => 0, 2382 can_events => 0,
2439 ) 2383 )
2440} 2384}
2441 2385
2442sub set_markup { 2386sub set_tooltip_from {
2443 my ($self, $text) = @_; 2387 my ($self, $widget) = @_;
2444 2388
2445 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2389 $self->add (new CFClient::UI::Label
2446 $self->{label}->set_markup ($text); 2390 markup => $widget->{tooltip},
2447 $self->add ($self->{label}); 2391 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2392 fontsize => 0.8,
2393 fg => [0, 0, 0, 1],
2394 font => ($widget->{tooltip_font} || $::FONT_PROP),
2395 );
2448} 2396}
2449 2397
2450sub size_request { 2398sub size_request {
2451 my ($self) = @_; 2399 my ($self) = @_;
2452
2453 $self->child->set_max_size ($::WIDTH * 0.3);
2454 2400
2455 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2401 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2456 2402
2457 ($w + 4, $h + 4) 2403 ($w + 4, $h + 4)
2458} 2404}
2464} 2410}
2465 2411
2466sub _draw { 2412sub _draw {
2467 my ($self) = @_; 2413 my ($self) = @_;
2468 2414
2469 glPushMatrix;
2470 glTranslate 0.375, 0.375; 2415 glTranslate 0.375, 0.375;
2471 2416
2472 my ($w, $h) = @$self{qw(w h)}; 2417 my ($w, $h) = @$self{qw(w h)};
2473 2418
2474 glColor 1, 0.8, 0.4; 2419 glColor 1, 0.8, 0.4;
2485 glVertex 0 , $h; 2430 glVertex 0 , $h;
2486 glVertex $w, $h; 2431 glVertex $w, $h;
2487 glVertex $w, 0; 2432 glVertex $w, 0;
2488 glEnd; 2433 glEnd;
2489 2434
2490 glPopMatrix; 2435 glTranslate 2 - 0.375, 2 - 0.375;
2491
2492 glTranslate 2, 2;
2493 $self->SUPER::_draw; 2436 $self->SUPER::_draw;
2494} 2437}
2495 2438
2496############################################################################# 2439#############################################################################
2497 2440
2520 return unless $::CONN;#d# manage and cache textures differently 2463 return unless $::CONN;#d# manage and cache textures differently
2521 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2464 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2522 2465
2523 # TODO animation 2466 # TODO animation
2524 if ($tex) { 2467 if ($tex) {
2525 glEnable GL_BLEND;
2526 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2527 glEnable GL_TEXTURE_2D; 2468 glEnable GL_TEXTURE_2D;
2528 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2469 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2529 glColor 1, 1, 1, 1; 2470 glColor 1, 1, 1, 1;
2530 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2471 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2531 glDisable GL_TEXTURE_2D; 2472 glDisable GL_TEXTURE_2D;
2532 glDisable GL_BLEND;
2533 } 2473 }
2534} 2474}
2535 2475
2536############################################################################# 2476#############################################################################
2537 2477
2542sub new { 2482sub new {
2543 my $class = shift; 2483 my $class = shift;
2544 2484
2545 my %args = @_; 2485 my %args = @_;
2546 2486
2547 my $item = $args{item}; 2487 my $item = delete $args{item};
2548 2488
2549 my $desc = $item->{nrof} < 2 2489 my $desc = $item->{nrof} < 2
2550 ? $item->{name} 2490 ? $item->{name}
2551 : "$item->{nrof} $item->{name_pl}"; 2491 : "$item->{nrof} $item->{name_pl}";
2552 2492
2581 2521
2582 1 2522 1
2583 }, 2523 },
2584 %args 2524 %args
2585 ); 2525 );
2526
2586 $self->add(new CFClient::UI::Face 2527 $self->add (new CFClient::UI::Face
2587 can_events => 0, 2528 can_events => 0,
2588 face => $item->{face}, 2529 face => $item->{face},
2589 anim => $item->{anim}, 2530 anim => $item->{anim},
2590 animspeed => $item->{animspeed}); 2531 animspeed => $item->{animspeed},
2532 );
2533
2591 $self->add(new CFClient::UI::Label 2534 $self->add (new CFClient::UI::Label
2592 can_events => 0, 2535 can_events => 0,
2593 text => $desc); 2536 text => $desc,
2537 );
2594 2538
2595 $self 2539 $self
2596} 2540}
2597 2541
2598############################################################################# 2542#############################################################################
2616 my ($self, $items) = @_; 2560 my ($self, $items) = @_;
2617 2561
2618 $self->{scrolled}->clear; 2562 $self->{scrolled}->clear;
2619 return unless $items; 2563 return unless $items;
2620 2564
2621 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2565 my @items = sort {
2566 ($a->{type} <=> $b->{type})
2567 or ($a->{name} cmp $b->{name})
2568 } @$items;
2622 2569
2623 $self->{real_items} = \@items; 2570 $self->{real_items} = \@items;
2624 2571
2625 for my $item (@items) { 2572 for my $item (@items) {
2626 my $desc = $item->{nrof} < 2 2573 my $desc = $item->{nrof} < 2
2627 ? $item->{name} 2574 ? $item->{name}
2628 : "$item->{nrof} $item->{name_pl}"; 2575 : "$item->{nrof} $item->{name_pl}";
2629 2576
2630 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2577 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2631 } 2578 }
2579
2580 $self->{scrolled}->add (@items);
2632 2581
2633# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2582# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2634} 2583}
2635 2584
2636sub size_request { 2585sub size_request {
2710 } 2659 }
2711} 2660}
2712 2661
2713############################################################################# 2662#############################################################################
2714 2663
2664package CFClient::UI::Statusbox;
2665
2666our @ISA = CFClient::UI::VBox::;
2667
2668sub reorder {
2669 my ($self) = @_;
2670 my $NOW = time;
2671
2672 while (my ($k, $v) = each %{ $self->{item} }) {
2673 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2674 }
2675
2676 my @widgets;
2677
2678 my @items = sort {
2679 $a->{pri} <=> $b->{pri}
2680 or $b->{id} <=> $a->{id}
2681 } values %{ $self->{item} };
2682
2683 my $count = 10 + 1;
2684 for my $item (@items) {
2685 last unless --$count;
2686
2687 push @widgets, $item->{label} ||= do {
2688 # TODO: doesn't handle markup well (read as: at all)
2689 my $short = $item->{count} > 1
2690 ? "<b>$item->{count} ×</b> $item->{text}"
2691 : $item->{text};
2692
2693 for ($short) {
2694 s/^\s+//;
2695 s/\012.*/…/s;
2696 my $len = int 40 / $item->{fontsize};
2697 substr $_, $len, length, "…" if $len < length;
2698 }
2699
2700 new CFClient::UI::Label
2701 markup => $short,
2702 tooltip => $item->{tooltip},
2703 tooltip_font => $::FONT_PROP,
2704 tooltip_width => 0.67,
2705 fontsize => $item->{fontsize},
2706 color => $item->{color},
2707 can_events => 1,
2708 can_hover => 1
2709 };
2710 }
2711
2712 $self->clear;
2713 $self->SUPER::add (reverse @widgets);
2714}
2715
2716sub add {
2717 my ($self, $text, %arg) = @_;
2718
2719 $text =~ s/^\s+//;
2720 $text =~ s/\s+$//;
2721
2722 my $timeout = time + ((delete $arg{timeout}) || 60);
2723
2724 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2725
2726 if (my $item = $self->{item}{$group}) {
2727 if ($item->{text} eq $text) {
2728 $item->{count}++;
2729 } else {
2730 $item->{count} = 1;
2731 $item->{text} = $item->{tooltip} = $text;
2732 }
2733 $item->{id} = ++$self->{id};
2734 $item->{timeout} = $timeout;
2735 delete $item->{label};
2736 } else {
2737 $self->{item}{$group} = {
2738 id => ++$self->{id},
2739 text => $text,
2740 timeout => $timeout,
2741 tooltip => $text,
2742 fontsize => 0.8,
2743 color => [0.8, 0.8, 0.8, 0.8],
2744 pri => 0,
2745 count => 1,
2746 %arg,
2747 };
2748 }
2749
2750 $self->reorder;
2751}
2752
2753#############################################################################
2754
2715package CFClient::UI::Root; 2755package CFClient::UI::Root;
2716 2756
2717our @ISA = CFClient::UI::Container::; 2757our @ISA = CFClient::UI::Container::;
2718 2758
2719use CFClient::OpenGL; 2759use CFClient::OpenGL;
2720 2760
2761sub new {
2762 my $class = shift;
2763
2764 $class->SUPER::new (
2765 @_,
2766 )
2767}
2768
2769sub configure {
2770 my ($self, $x, $y, $w, $h) = @_;
2771
2772 $self->{w} = $w;
2773 $self->{h} = $h;
2774}
2775
2721sub check_size { 2776sub check_size {
2722 my ($self) = @_; 2777 my ($self) = @_;
2723 2778
2724 $self->configure (0, 0, $self->{w}, $self->{h}); 2779 $self->size_allocate ($self->{w}, $self->{h})
2780 if $self->{w};
2725} 2781}
2726 2782
2727sub size_request { 2783sub size_request {
2728 my ($self) = @_; 2784 my ($self) = @_;
2729 2785
2731} 2787}
2732 2788
2733sub size_allocate { 2789sub size_allocate {
2734 my ($self, $w, $h) = @_; 2790 my ($self, $w, $h) = @_;
2735 2791
2736 my $old_w = $self->{old_w}; 2792 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2737 my $old_h = $self->{old_h}; 2793 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2738 2794
2739 if ($old_w && $old_h) { 2795 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2740 for my $child ($self->children) { 2796 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2741 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2742 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2743 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2744 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2745 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2746 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2747 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2748 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2749 }
2750 }
2751
2752 $self->{old_w} = $w;
2753 $self->{old_h} = $h;
2754}
2755
2756sub configure {
2757 my ($self, $x, $y, $w, $h) = @_;
2758
2759 $self->SUPER::configure ($x, $y, $w, $h);
2760 2797
2761 for my $child ($self->children) { 2798 for my $child ($self->children) {
2762 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2799 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2763 2800
2764 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2801 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2765 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2802 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2766 $child->configure ($X, $Y, $W, $H); 2803 $child->configure ($X, $Y, $W, $H);
2767 } 2804 }
2768} 2805}
2769 2806
2770sub _topleft { 2807sub coord2local {
2771 my ($self, $x, $y) = @_; 2808 my ($self, $x, $y) = @_;
2772 2809
2773 ($x, $y) 2810 ($x, $y)
2774} 2811}
2775 2812
2813sub coord2global {
2814 my ($self, $x, $y) = @_;
2815
2816 ($x, $y)
2817}
2818
2776sub update { 2819sub update {
2777 my ($self) = @_; 2820 my ($self) = @_;
2778 2821
2779 $self->check_size; 2822 $self->check_size;
2780 ::refresh (); 2823 $::WANT_REFRESH++;
2781} 2824}
2782 2825
2783sub add { 2826sub add {
2784 my ($self, $child) = @_; 2827 my ($self, @children) = @_;
2785 2828
2829 for my $child (@children) {
2830 $child->{toplevel} = 1;
2831
2786 # integerize window positions 2832 # integerise window positions
2787 $child->{x} = int $child->{x}; 2833 $child->{x} = int $child->{x};
2788 $child->{y} = int $child->{y}; 2834 $child->{y} = int $child->{y};
2835 }
2789 2836
2790 $self->SUPER::add ($child); 2837 $self->SUPER::add (@children);
2791} 2838}
2792 2839
2793sub on_refresh { 2840sub on_refresh {
2794 my ($self, $id, $cb) = @_; 2841 my ($self, $id, $cb) = @_;
2795 2842
2796 $self->{refresh_hook}{$id} = $cb; 2843 $self->{refresh_hook}{$id} = $cb;
2797} 2844}
2798 2845
2846sub on_post_alloc {
2847 my ($self, $id, $cb) = @_;
2848
2849 $self->{post_alloc_hook}{$id} = $cb;
2850}
2851
2799sub draw { 2852sub draw {
2800 my ($self) = @_; 2853 my ($self) = @_;
2801 2854
2802 while (my $rcb = delete $self->{refresh_hook}) { 2855 while ($self->{refresh_hook}) {
2803 $_->() for values %$rcb; 2856 $_->()
2857 for values %{delete $self->{refresh_hook}};
2858 }
2859
2860 if ($self->{check_size}) {
2861 my @queue = ([], []);
2862
2863 for (;;) {
2864 if ($self->{check_size}) {
2865 # heuristic: check containers last
2866 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2867 for values %{delete $self->{check_size}}
2868 }
2869
2870 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2871
2872 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2873 ? @$widget{qw(user_w user_h)}
2874 : $widget->size_request;
2875
2876 if (delete $widget->{force_alloc}
2877 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2878 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2879
2880 $widget->{req_w} = $w;
2881 $widget->{req_h} = $h;
2882
2883 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2884
2885 $widget->{parent}->check_size
2886 if $widget->{parent};
2887 }
2888 }
2889 }
2890
2891 while ($self->{size_alloc}) {
2892 for (values %{delete $self->{size_alloc}}) {
2893 my ($widget, $w, $h) = @$_;
2894
2895 $w = 0 if $w < 0;
2896 $h = 0 if $h < 0;
2897
2898 $widget->{w} = $w;
2899 $widget->{h} = $h;
2900 $widget->size_allocate ($w, $h);
2901 $widget->emit (size_allocate => $w, $h);
2902 }
2903 }
2904
2905 while ($self->{post_alloc_hook}) {
2906 $_->()
2907 for values %{delete $self->{post_alloc_hook}};
2804 } 2908 }
2805 2909
2806 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2910 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2807 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2911 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2808 glClear GL_COLOR_BUFFER_BIT; 2912 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines