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.196 by root, Fri May 12 00:16:34 2006 UTC vs.
Revision 1.210 by root, Wed May 17 10:14:52 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}) {
29 $TOOLTIP->set_tooltip_from ($widget); 31 $TOOLTIP->set_tooltip_from ($widget);
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) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
36 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
37 38
38 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
39 } 42 }
40 43
41 return; 44 return;
42 } 45 }
43 } 46 }
130 133
131 for (@$vals) { 134 for (@$vals) {
132 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
133 $rem += $_ - $i; 136 $rem += $_ - $i;
134 $_ = $i; 137 $_ = $i;
138 }
139}
140
141# call when resolution changes etc.
142sub rescale_widgets {
143 my ($sx, $sy) = @_;
144
145 for my $widget (values %WIDGET) {
146 if ($widget->{toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
148 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
149 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
150 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
151 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
152 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
153 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
154 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
155 }
156
157 $widget->reconfigure;
135 } 158 }
136} 159}
137 160
138############################################################################# 161#############################################################################
139 162
158 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
159 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
160 } 183 }
161 } 184 }
162 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
163 $self 188 $self
164} 189}
165 190
166sub destroy { 191sub destroy {
167 my ($self) = @_; 192 my ($self) = @_;
240 265
241sub size_allocate { 266sub size_allocate {
242 # nothing to be done 267 # nothing to be done
243} 268}
244 269
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
245sub children { 277sub children {
246}
247
248# call when resolution changes etc.
249sub reconfigure {
250 my ($self) = @_;
251
252 $_->reconfigure
253 for $self->children;
254
255 $self->check_size;
256 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
257 $self->update;
258} 278}
259 279
260sub set_max_size { 280sub set_max_size {
261 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
262 282
263 delete $self->{max_w}; $self->{max_w} = $w if $w; 283 delete $self->{max_w}; $self->{max_w} = $w if $w;
264 delete $self->{max_h}; $self->{max_h} = $h if $h; 284 delete $self->{max_h}; $self->{max_h} = $h if $h;
285}
286
287sub set_tooltip {
288 my ($self, $tooltip) = @_;
289
290 $self->{tooltip} = $tooltip;
291
292 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
293 delete $CFClient::UI::TOOLTIP->{owner};
294 CFClient::UI::check_tooltip;
295 }
265} 296}
266 297
267# translate global coordinates to local coordinate system 298# translate global coordinates to local coordinate system
268sub coord2local { 299sub coord2local {
269 my ($self, $x, $y) = @_; 300 my ($self, $x, $y) = @_;
355 glVertex $self->{w}, 0; 386 glVertex $self->{w}, 0;
356 glVertex $self->{w}, $self->{h}; 387 glVertex $self->{w}, $self->{h};
357 glVertex 0 , $self->{h}; 388 glVertex 0 , $self->{h};
358 glEnd; 389 glEnd;
359 glPopMatrix; 390 glPopMatrix;
360 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 391 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
361 } 392 }
362} 393}
363 394
364sub _draw { 395sub _draw {
365 my ($self) = @_; 396 my ($self) = @_;
382sub set_parent { 413sub set_parent {
383 my ($self, $parent) = @_; 414 my ($self, $parent) = @_;
384 415
385 Scalar::Util::weaken ($self->{parent} = $parent); 416 Scalar::Util::weaken ($self->{parent} = $parent);
386 417
418 # TODO: req_w _does_change after ->reconfigure
387 $self->check_size 419 $self->check_size
388 unless exists $self->{req_w}; 420 unless exists $self->{req_w};
389} 421}
390 422
391sub check_size { 423sub check_size {
392 my ($self) = @_; 424 my ($self, $forced) = @_;
393 425
426 $self->{force_alloc} = 1 if $forced;
394 $CFClient::UI::ROOT->{check_size}{$self} = $self; 427 $CFClient::UI::ROOT->{check_size}{$self} = $self;
395} 428}
396 429
397sub update { 430sub update {
398 my ($self) = @_; 431 my ($self) = @_;
414} 447}
415 448
416sub DESTROY { 449sub DESTROY {
417 my ($self) = @_; 450 my ($self) = @_;
418 451
452 delete $WIDGET{$self+0};
419 #$self->deactivate; 453 #$self->deactivate;
420} 454}
421 455
422############################################################################# 456#############################################################################
423 457
432 my $class = shift; 466 my $class = shift;
433 467
434 # range [value, low, high, page] 468 # range [value, low, high, page]
435 469
436 $class->SUPER::new ( 470 $class->SUPER::new (
437 bg => [0, 0, 0, 0.2], 471 #bg => [0, 0, 0, 0.2],
438 active_bg => [1, 1, 1, 0.5], 472 #active_bg => [1, 1, 1, 0.5],
439 @_ 473 @_
440 ) 474 )
441} 475}
442 476
443sub _draw { 477sub _draw {
444 my ($self) = @_; 478 my ($self) = @_;
445 479
480 my $color = $FOCUS == $self && $self->{active_bg}
481 ? $self->{active_bg}
482 : $self->{bg};
483
484 if ($color && (@$color < 4 || $color->[3])) {
446 my ($w, $h) = @$self{qw(w h)}; 485 my ($w, $h) = @$self{qw(w h)};
447 486
448 glEnable GL_BLEND; 487 glEnable GL_BLEND;
449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 488 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
450 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 489 glColor @$color;
451 490
452 glBegin GL_QUADS; 491 glBegin GL_QUADS;
453 glVertex 0 , 0; 492 glVertex 0 , 0;
454 glVertex 0 , $h; 493 glVertex 0 , $h;
455 glVertex $w, $h; 494 glVertex $w, $h;
456 glVertex $w, 0; 495 glVertex $w, 0;
457 glEnd; 496 glEnd;
458 497
459 glDisable GL_BLEND; 498 glDisable GL_BLEND;
499 }
460} 500}
461 501
462############################################################################# 502#############################################################################
463 503
464package CFClient::UI::Empty; 504package CFClient::UI::Empty;
508 $self->{children} = [ 548 $self->{children} = [
509 sort { $a->{z} <=> $b->{z} } 549 sort { $a->{z} <=> $b->{z} }
510 @{$self->{children}}, @widgets 550 @{$self->{children}}, @widgets
511 ]; 551 ];
512 552
513 $self->check_size; 553 $self->check_size (1);
514 $self->update; 554 $self->update;
515} 555}
516 556
517sub children { 557sub children {
518 @{ $_[0]{children} } 558 @{ $_[0]{children} }
625} 665}
626 666
627sub update { 667sub update {
628 my ($self) = @_; 668 my ($self) = @_;
629 669
630 $ROOT->on_refresh ($self => sub { $self->render_child }); 670 $ROOT->on_post_alloc ($self => sub { $self->render_child });
631 $self->SUPER::update; 671 $self->SUPER::update;
632} 672}
633 673
634sub size_allocate { 674sub size_allocate {
635 my ($self, $w, $h) = @_; 675 my ($self, $w, $h) = @_;
773 $self->add ($self->{slider}); 813 $self->add ($self->{slider});
774 814
775 $self 815 $self
776} 816}
777 817
778#TODO# update range on size_allocate depeneing on child 818#TODO# update range on size_allocate depending on child
779# update viewport offset on scroll 819# update viewport offset on scroll
780 820
781############################################################################# 821#############################################################################
782 822
783package CFClient::UI::Frame; 823package CFClient::UI::Frame;
787use CFClient::OpenGL; 827use CFClient::OpenGL;
788 828
789sub new { 829sub new {
790 my $class = shift; 830 my $class = shift;
791 831
792 my $self = $class->SUPER::new ( 832 $class->SUPER::new (
793 bg => [1, 1, 1, 1], 833 bg => undef,
794 border_bg => [1, 1, 1, 1],
795 border => 0.8,
796 @_ 834 @_,
797 ); 835 )
798
799 $self
800} 836}
801 837
802sub _draw { 838sub _draw {
803 my ($self) = @_; 839 my ($self) = @_;
804 840
805 my ($w, $h) = ($self->{w}, $self->{h}); 841 if ($self->{bg}) {
842 my ($w, $h) = @$self{qw(w h)};
806 843
807 glEnable GL_BLEND; 844 glEnable GL_BLEND;
808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 845 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
809 glEnable GL_TEXTURE_2D; 846 glColor @{ $self->{bg} };
810 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
811 847
812# glBegin GL_QUADS; 848 glBegin GL_QUADS;
813# glColor 0, 0, 0, 0;
814# glVertex 0 , 0; 849 glVertex 0 , 0;
815# glVertex 0 , $h; 850 glVertex 0 , $h;
816# glVertex $w, $h; 851 glVertex $w, $h;
817# glVertex $w, 0; 852 glVertex $w, 0;
818# glEnd; 853 glEnd;
819 854
820
821 $self->child->draw;
822 glDisable GL_BLEND; 855 glDisable GL_BLEND;
823 glDisable GL_TEXTURE_2D; 856 }
857
858 $self->SUPER::_draw;
824} 859}
825 860
826############################################################################# 861#############################################################################
827 862
828package CFClient::UI::FancyFrame; 863package CFClient::UI::FancyFrame;
842 877
843 my $self = $class->SUPER::new ( 878 my $self = $class->SUPER::new (
844 bg => [1, 1, 1, 1], 879 bg => [1, 1, 1, 1],
845 border_bg => [1, 1, 1, 1], 880 border_bg => [1, 1, 1, 1],
846 border => 0.6, 881 border => 0.6,
882 toplevel => 1,
847 can_events => 1, 883 can_events => 1,
848 @_ 884 @_
849 ); 885 );
850 886
851 $self->{title} &&= new CFClient::UI::Label 887 $self->{title} &&= new CFClient::UI::Label
876 my ($self, $w, $h) = @_; 912 my ($self, $w, $h) = @_;
877 913
878 $h -= List::Util::max 0, $self->border * 2; 914 $h -= List::Util::max 0, $self->border * 2;
879 $w -= List::Util::max 0, $self->border * 2; 915 $w -= List::Util::max 0, $self->border * 2;
880 916
881 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 917 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
882 if $self->{title}; 918 if $self->{title};
883 919
884 $self->child->configure ($self->border, $self->border, $w, $h); 920 $self->child->configure ($self->border, $self->border, $w, $h);
885} 921}
886 922
944 my ($self) = @_; 980 my ($self) = @_;
945 981
946 my ($w, $h ) = ($self->{w}, $self->{h}); 982 my ($w, $h ) = ($self->{w}, $self->{h});
947 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 983 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
948 984
949 glEnable GL_BLEND;
950 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
951 glEnable GL_TEXTURE_2D; 985 glEnable GL_TEXTURE_2D;
952 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
953 987
954 my $border = $self->border; 988 my $border = $self->border;
955 989
956 glColor @{ $self->{border_bg} }; 990 glColor @{ $self->{border_bg} };
957 $tex[1]->draw_quad (0, 0, $w, $border); 991 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
958 $tex[3]->draw_quad (0, $border, $border, $ch); 992 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
959 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 993 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
960 $tex[4]->draw_quad (0, $h - $border, $w, $border); 994 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
961 995
962 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 996 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
963 my $bg = $tex[0]; 997 my $bg = $tex[0];
964 998
965 # TODO: repeat texture not scale 999 # TODO: repeat texture not scale
969 glColor @{ $self->{bg} }; 1003 glColor @{ $self->{bg} };
970 1004
971 $bg->{s} = $rep_x; 1005 $bg->{s} = $rep_x;
972 $bg->{t} = $rep_y; 1006 $bg->{t} = $rep_y;
973 $bg->{wrap_mode} = 1; 1007 $bg->{wrap_mode} = 1;
974 $bg->draw_quad ($border, $border, $cw, $ch); 1008 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1009 }
975 1010
976 glDisable GL_TEXTURE_2D; 1011 glDisable GL_TEXTURE_2D;
977 glDisable GL_BLEND;
978 }
979 1012
980 $self->{title}->draw if $self->{title}; 1013 $self->{title}->draw if $self->{title};
981 1014
982 $self->child->draw; 1015 $self->child->draw;
983} 1016}
1250 1283
1251############################################################################# 1284#############################################################################
1252 1285
1253package CFClient::UI::Label; 1286package CFClient::UI::Label;
1254 1287
1255our @ISA = CFClient::UI::Base::; 1288our @ISA = CFClient::UI::DrawBG::;
1256 1289
1257use CFClient::OpenGL; 1290use CFClient::OpenGL;
1258 1291
1259sub new { 1292sub new {
1260 my ($class, %arg) = @_; 1293 my ($class, %arg) = @_;
1261 1294
1262 my $self = $class->SUPER::new ( 1295 my $self = $class->SUPER::new (
1263 fg => [1, 1, 1], 1296 fg => [1, 1, 1],
1297 #bg => none
1298 #active_bg => none
1264 #font => default_font 1299 #font => default_font
1265 #text => initial text 1300 #text => initial text
1266 #markup => initial narkup 1301 #markup => initial narkup
1267 layout => (new CFClient::Layout), 1302 layout => (new CFClient::Layout),
1268 fontsize => 1, 1303 fontsize => 1,
1286 } 1321 }
1287 1322
1288 $self 1323 $self
1289} 1324}
1290 1325
1291sub escape { 1326sub escape($) {
1292 local $_ = $_[1]; 1327 local $_ = $_[0];
1293 1328
1294 s/&/&amp;/g; 1329 s/&/&amp;/g;
1295 s/>/&gt;/g; 1330 s/>/&gt;/g;
1296 s/</&lt;/g; 1331 s/</&lt;/g;
1297 1332
1298 $_[1] 1333 $_
1299} 1334}
1300 1335
1301sub update { 1336sub update {
1302 my ($self) = @_; 1337 my ($self) = @_;
1303 1338
1374 $self->check_size; 1409 $self->check_size;
1375} 1410}
1376 1411
1377sub _draw { 1412sub _draw {
1378 my ($self) = @_; 1413 my ($self) = @_;
1414
1415 $self->SUPER::_draw; # draw background, if applicable
1379 1416
1380 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1381 $self->{layout}->set_foreground (@{$self->{fg}}); 1418 $self->{layout}->set_foreground (@{$self->{fg}});
1382 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1383 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1384 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1421 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1385 1422
1386 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1423 my $tex = new_from_layout CFClient::Texture $self->{layout};
1387 1424
1388 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1389 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1390 : ($self->{w} - $tex->{w}) * 0.5; 1427 : ($self->{w} - $tex->{w}) * 0.5);
1391 1428
1392 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1429 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1393 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1430 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1394 : ($self->{h} - $tex->{h}) * 0.5; 1431 : ($self->{h} - $tex->{h}) * 0.5);
1395 1432
1396 $tex 1433 $tex
1397 }; 1434 };
1398 1435
1399 glEnable GL_TEXTURE_2D; 1436 glEnable GL_TEXTURE_2D;
1454sub set_text { 1491sub set_text {
1455 my ($self, $text) = @_; 1492 my ($self, $text) = @_;
1456 1493
1457 $self->{cursor} = length $text; 1494 $self->{cursor} = length $text;
1458 $self->_set_text ($text); 1495 $self->_set_text ($text);
1496 $self->update;
1459 $self->check_size; 1497 $self->check_size;
1460 $self->update;
1461} 1498}
1462 1499
1463sub get_text { 1500sub get_text {
1464 $_[0]{text} 1501 $_[0]{text}
1465} 1502}
1485 my $sym = $ev->{sym}; 1522 my $sym = $ev->{sym};
1486 my $uni = $ev->{unicode}; 1523 my $uni = $ev->{unicode};
1487 1524
1488 my $text = $self->get_text; 1525 my $text = $self->get_text;
1489 1526
1490 if ($sym == 8) { 1527 if ($uni == 8) {
1491 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1528 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1492 } elsif ($sym == 127) { 1529 } elsif ($uni == 127) {
1493 substr $text, $self->{cursor}, 1, ""; 1530 substr $text, $self->{cursor}, 1, "";
1494 } elsif ($sym == CFClient::SDLK_LEFT) { 1531 } elsif ($sym == CFClient::SDLK_LEFT) {
1495 --$self->{cursor} if $self->{cursor}; 1532 --$self->{cursor} if $self->{cursor};
1496 } elsif ($sym == CFClient::SDLK_RIGHT) { 1533 } elsif ($sym == CFClient::SDLK_RIGHT) {
1497 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1534 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1498 } elsif ($sym == CFClient::SDLK_HOME) { 1535 } elsif ($sym == CFClient::SDLK_HOME) {
1499 $self->{cursor} = 0; 1536 $self->{cursor} = 0;
1500 } elsif ($sym == CFClient::SDLK_END) { 1537 } elsif ($sym == CFClient::SDLK_END) {
1501 $self->{cursor} = length $text; 1538 $self->{cursor} = length $text;
1502 } elsif ($sym == 27) { 1539 } elsif ($uni == 27) {
1503 $self->emit ('escape'); 1540 $self->emit ('escape');
1504 } elsif ($uni) { 1541 } elsif ($uni) {
1505 substr $text, $self->{cursor}++, 0, chr $uni; 1542 substr $text, $self->{cursor}++, 0, chr $uni;
1506 } 1543 }
1507 1544
1508 $self->_set_text ($text); 1545 $self->_set_text ($text);
1509 $self->update; 1546 $self->update;
1547 $self->check_size;
1510} 1548}
1511 1549
1512sub focus_in { 1550sub focus_in {
1513 my ($self) = @_; 1551 my ($self) = @_;
1514 1552
1643 my $class = shift; 1681 my $class = shift;
1644 1682
1645 $class->SUPER::new ( 1683 $class->SUPER::new (
1646 padding => 4, 1684 padding => 4,
1647 fg => [1, 1, 1], 1685 fg => [1, 1, 1],
1648 bg => [1, 1, 1, 0.2],
1649 active_fg => [0, 0, 1], 1686 active_fg => [0, 0, 1],
1650 can_hover => 1, 1687 can_hover => 1,
1651 align => 0, 1688 align => 0,
1652 valign => 0, 1689 valign => 0,
1653 can_events => 1, 1690 can_events => 1,
1701 1738
1702 $class->SUPER::new ( 1739 $class->SUPER::new (
1703 padding => 2, 1740 padding => 2,
1704 fg => [1, 1, 1], 1741 fg => [1, 1, 1],
1705 active_fg => [1, 1, 0], 1742 active_fg => [1, 1, 0],
1743 bg => [0, 0, 0, 0.2],
1744 active_bg => [1, 1, 1, 0.5],
1706 state => 0, 1745 state => 0,
1707 can_hover => 1, 1746 can_hover => 1,
1708 @_ 1747 @_
1709 ) 1748 )
1710} 1749}
1734 1773
1735 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1774 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1736 1775
1737 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1776 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1738 1777
1778 my $tex = $self->{state} ? $tex[1] : $tex[0];
1779
1739 glEnable GL_TEXTURE_2D; 1780 glEnable GL_TEXTURE_2D;
1740
1741 my $tex = $self->{state} ? $tex[1] : $tex[0];
1742
1743 $tex->draw_quad_alpha (0, 0, $s, $s); 1781 $tex->draw_quad_alpha (0, 0, $s, $s);
1744
1745 glDisable GL_TEXTURE_2D; 1782 glDisable GL_TEXTURE_2D;
1746} 1783}
1747 1784
1748############################################################################# 1785#############################################################################
1749 1786
1996 qw(s1_slider.png s1_slider_bg.png); 2033 qw(s1_slider.png s1_slider_bg.png);
1997 2034
1998sub new { 2035sub new {
1999 my $class = shift; 2036 my $class = shift;
2000 2037
2001 # range [value, low, high, page] 2038 # range [value, low, high, page, unit]
2002 2039
2003 # TODO: 0-width page 2040 # TODO: 0-width page
2004 # TODO: req_w/h are wrong with vertical 2041 # TODO: req_w/h are wrong with vertical
2005 # TODO: calculations are off 2042 # TODO: calculations are off
2006 my $self = $class->SUPER::new ( 2043 my $self = $class->SUPER::new (
2007 fg => [1, 1, 1], 2044 fg => [1, 1, 1],
2008 active_fg => [0, 0, 0], 2045 active_fg => [0, 0, 0],
2046 bg => [0, 0, 0, 0.2],
2047 active_bg => [1, 1, 1, 0.5],
2009 range => [0, 0, 100, 10], 2048 range => [0, 0, 100, 10],
2010 req_w => $::WIDTH / 80, 2049 req_w => $::WIDTH / 80,
2011 req_h => $::WIDTH / 80, 2050 req_h => $::WIDTH / 80,
2012 vertical => 0, 2051 vertical => 0,
2013 can_hover => 1, 2052 can_hover => 1,
2014 inner_pad => 5, 2053 inner_pad => .05,
2015 @_ 2054 @_
2016 ); 2055 );
2017 2056
2057 $self->set_value ($self->{range}[0]);
2058 $self->update;
2059
2018 $self 2060 $self
2061}
2062
2063sub set_value {
2064 my ($self, $value) = @_;
2065
2066 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2067
2068 $hi = $lo + 1 if $hi <= $lo;
2069
2070 $value = $lo if $value < $lo;
2071 $value = $hi if $value > $hi;
2072
2073 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2074 if $unit;
2075
2076 $page = $hi - $lo if $page > $hi - $lo;
2077
2078 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2079
2080 if ($value != $old_value) {
2081 $self->emit (changed => $value);
2082 $self->update;
2083 }
2019} 2084}
2020 2085
2021sub size_request { 2086sub size_request {
2022 my ($self) = @_; 2087 my ($self) = @_;
2023 2088
2036 2101
2037sub mouse_motion { 2102sub mouse_motion {
2038 my ($self, $ev, $x, $y) = @_; 2103 my ($self, $ev, $x, $y) = @_;
2039 2104
2040 if ($GRAB == $self) { 2105 if ($GRAB == $self) {
2106 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2107
2108 my (undef, $lo, $hi, $page) = @{$self->{range}};
2109
2110 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2111
2112 $self->set_value ($x * ($hi - $lo) + $lo);
2113 }
2114}
2115
2116sub update {
2117 my ($self) = @_;
2118
2119 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2120 $self->set_value ($self->{range}[0]);
2121
2041 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2122 my ($value, $lo, $hi, $page) = @{$self->{range}};
2042 2123
2043 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2124 my $inner_w = 1 - 2 * $self->{inner_pad};
2044 2125
2045 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2046 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2047 2127
2048 $x -= $inner_pad_px; # substract the padding 2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2049 $x = $x * ($hi - $lo) / $inner_w + $lo; 2129 $value = $self->{scale} * ($value - $lo);
2050 $x = $lo if $x < $lo;
2051 $x = $hi - $page if $x > $hi - $page;
2052 $self->{range}[0] = $x;
2053 2130
2054 $self->emit (changed => $x); 2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
2055 $self->update; 2132
2133 $value = 0 if $value < 0;
2134 $page = 1 - $value if $value + $page > 1;
2135
2136 $self->{knob_x} = $value;
2137 $self->{knob_w} = $page;
2056 } 2138 });
2057}
2058 2139
2059# the inner_* stuff is for generating a padding for the slider handle, 2140 $self->SUPER::update;
2060# so that the handle doesn't leave the texture. This calculation isn't 100%
2061# correct propably, but it does the job for now
2062sub _calc_inner_pad_px {
2063 my ($self, $w) = @_;
2064 ($w / 100) * $self->{inner_pad} # % to pixels
2065} 2141}
2066 2142
2067sub _draw { 2143sub _draw {
2068 my ($self) = @_; 2144 my ($self) = @_;
2069 2145
2070 $self->SUPER::_draw (); 2146 $self->SUPER::_draw ();
2071 2147
2072 my ($w, $h) = @$self{qw(w h)}; 2148 glScale $self->{w}, $self->{h};
2073 2149
2074 if ($self->{vertical}) { 2150 if ($self->{vertical}) {
2075 # draw a vertical slider like a rotated horizontal slider 2151 # draw a vertical slider like a rotated horizontal slider
2076 2152
2077 glRotate 90, 0, 0, 1; 2153 glRotate 90, 0, 0, 1;
2078 glTranslate 0, -$self->{w}, 0; 2154 glTranslate 0, 1, 0;
2079
2080 ($w, $h) = ($h, $w);
2081 } 2155 }
2082 2156
2083 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2084 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2085 2159
2086 my ($value, $lo, $hi, $page) = @{$self->{range}};
2087
2088 $hi = $value + 1 if $lo == $hi;
2089
2090 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2091 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2092
2093 $page = int $page * $inner_w / ($hi - $lo);
2094 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2095
2096 $w -= $page;
2097 $page &= ~1;
2098 glTranslate $page * 0.5, 0, 0;
2099 $page ||= 2;
2100
2101 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2102 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2103
2104 glEnable GL_TEXTURE_2D; 2160 glEnable GL_TEXTURE_2D;
2105 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2161 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2106 2162
2107 # draw background 2163 # draw background
2108 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2164 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2109 2165
2110 # draw handle 2166 # draw handle
2111 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2167 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2112 2168
2113 glDisable GL_TEXTURE_2D; 2169 glDisable GL_TEXTURE_2D;
2114} 2170}
2115 2171
2116############################################################################# 2172#############################################################################
2204 2260
2205 return unless $self->{h} > 0; 2261 return unless $self->{h} > 0;
2206 2262
2207 delete $self->{texture}; 2263 delete $self->{texture};
2208 2264
2209 $ROOT->on_refresh ($self, sub { 2265 $ROOT->on_post_alloc ($self, sub {
2210 if (delete $self->{need_reflow}) { 2266 if (delete $self->{need_reflow}) {
2211 my $height = 0; 2267 my $height = 0;
2212 2268
2213 $height += $_->[0] = $self->text_height ($_->[2]) 2269 $height += $_->[0] = $self->text_height ($_->[2])
2214 for @{$self->{par}}; 2270 for @{$self->{par}};
2369} 2425}
2370 2426
2371sub set_tooltip_from { 2427sub set_tooltip_from {
2372 my ($self, $widget) = @_; 2428 my ($self, $widget) = @_;
2373 2429
2374 $self->{label} = new CFClient::UI::Label 2430 $self->add (new CFClient::UI::Label
2375 markup => $widget->{tooltip}, 2431 markup => $widget->{tooltip},
2376 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2432 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2377 fontsize => 0.8, 2433 fontsize => 0.8,
2378 fg => [0, 0, 0, 1], 2434 fg => [0, 0, 0, 1],
2379 font => ($widget->{tooltip_font} || $::FONT_PROP); 2435 font => ($widget->{tooltip_font} || $::FONT_PROP),
2380 2436 );
2381 $self->add ($self->{label});
2382} 2437}
2383 2438
2384sub size_request { 2439sub size_request {
2385 my ($self) = @_; 2440 my ($self) = @_;
2386 2441
2396} 2451}
2397 2452
2398sub _draw { 2453sub _draw {
2399 my ($self) = @_; 2454 my ($self) = @_;
2400 2455
2401 glPushMatrix;
2402 glTranslate 0.375, 0.375; 2456 glTranslate 0.375, 0.375;
2403 2457
2404 my ($w, $h) = @$self{qw(w h)}; 2458 my ($w, $h) = @$self{qw(w h)};
2405 2459
2406 glColor 1, 0.8, 0.4; 2460 glColor 1, 0.8, 0.4;
2417 glVertex 0 , $h; 2471 glVertex 0 , $h;
2418 glVertex $w, $h; 2472 glVertex $w, $h;
2419 glVertex $w, 0; 2473 glVertex $w, 0;
2420 glEnd; 2474 glEnd;
2421 2475
2422 glPopMatrix; 2476 glTranslate 2 - 0.375, 2 - 0.375;
2423
2424 glTranslate 2, 2;
2425 $self->SUPER::_draw; 2477 $self->SUPER::_draw;
2426} 2478}
2427 2479
2428############################################################################# 2480#############################################################################
2429 2481
2477 2529
2478 my $desc = $item->{nrof} < 2 2530 my $desc = $item->{nrof} < 2
2479 ? $item->{name} 2531 ? $item->{name}
2480 : "$item->{nrof} $item->{name_pl}"; 2532 : "$item->{nrof} $item->{name_pl}";
2481 2533
2534 $item->{flags} & Crossfire::Protocol::F_OPEN
2535 and $desc .= " (open)";
2536 $item->{flags} & Crossfire::Protocol::F_APPLIED
2537 and $desc .= " (applied)";
2538 $item->{flags} & Crossfire::Protocol::F_UNPAID
2539 and $desc .= " (unpaid)";
2540 $item->{flags} & Crossfire::Protocol::F_MAGIC
2541 and $desc .= " (magic)";
2542 $item->{flags} & Crossfire::Protocol::F_CURSED
2543 and $desc .= " (cursed)";
2544 $item->{flags} & Crossfire::Protocol::F_DAMNED
2545 and $desc .= " (damned)";
2546 $item->{flags} & Crossfire::Protocol::F_LOCKED
2547 and $desc .= " *";
2482 2548
2483 my $self = $class->SUPER::new ( 2549 my $self = $class->SUPER::new (
2484 can_hover => 1, 2550 can_hover => 1,
2485 can_events => 1, 2551 can_events => 1,
2486 tooltip => (CFClient::UI::Label->escape ($desc) 2552 tooltip => ((CFClient::UI::Label::escape $desc)
2487 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2553 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2488 connect_button_down => sub { 2554 connect_button_down => sub {
2489 my ($self, $ev, $x, $y) = @_; 2555 my ($self, $ev, $x, $y) = @_;
2490 2556
2491 # todo: maybe put examine on 1? but should just be a tooltip :( 2557 # todo: maybe put examine on 1? but should just be a tooltip :(
2492 if ($ev->{button} == 1) { 2558 if ($ev->{button} == 1) {
2493 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2559 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2494 } elsif ($ev->{button} == 2) { 2560 } elsif ($ev->{button} == 2) {
2495 $::CONN->send ("apply $item->{tag}"); 2561 $::CONN->send ("apply $item->{tag}");
2496 } elsif ($ev->{button} == 3) { 2562 } elsif ($ev->{button} == 3) {
2497 CFClient::UI::Menu->new ( 2563 my @menu_items = (
2498 items => [
2499 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2564 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2565 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2566 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2567 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2500 [ 2568 [
2501 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2569 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2502 sub { $::CONN->send ("lock $item->{tag}") }, 2570 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2503 ],
2504 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2505 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2506 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2507 ], 2571 ],
2508 )->popup ($ev); 2572 );
2573
2574 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2509 } 2575 }
2510 2576
2511 1 2577 1
2512 }, 2578 },
2513 %args 2579 %args
2549 my ($self, $items) = @_; 2615 my ($self, $items) = @_;
2550 2616
2551 $self->{scrolled}->clear; 2617 $self->{scrolled}->clear;
2552 return unless $items; 2618 return unless $items;
2553 2619
2554 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2620 my @items = sort {
2621 ($a->{type} <=> $b->{type})
2622 or ($a->{name} cmp $b->{name})
2623 } @$items;
2555 2624
2556 $self->{real_items} = \@items; 2625 $self->{real_items} = \@items;
2557 2626
2558 for my $item (@items) { 2627 for my $item (@items) {
2559 my $desc = $item->{nrof} < 2 2628 my $desc = $item->{nrof} < 2
2560 ? $item->{name} 2629 ? $item->{name}
2561 : "$item->{nrof} $item->{name_pl}"; 2630 : "$item->{nrof} $item->{name_pl}";
2562 2631
2563 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2632 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2564 } 2633 }
2634
2635 $self->{scrolled}->add (@items);
2565 2636
2566# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2637# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2567} 2638}
2568 2639
2569sub size_request { 2640sub size_request {
2647 2718
2648package CFClient::UI::Statusbox; 2719package CFClient::UI::Statusbox;
2649 2720
2650our @ISA = CFClient::UI::VBox::; 2721our @ISA = CFClient::UI::VBox::;
2651 2722
2723sub new {
2724 my $class = shift;
2725
2726 $class->SUPER::new (
2727 fontsize => 0.8,
2728 @_,
2729 )
2730}
2731
2652sub reorder { 2732sub reorder {
2653 my ($self) = @_; 2733 my ($self) = @_;
2654 my $NOW = time; 2734 my $NOW = time;
2655 2735
2656 while (my ($k, $v) = each %{ $self->{item} }) { 2736 while (my ($k, $v) = each %{ $self->{item} }) {
2657 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2737 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2658 } 2738 }
2659 2739
2660 my @widgets; 2740 my @widgets;
2661 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; 2741
2742 my @items = sort {
2743 $a->{pri} <=> $b->{pri}
2744 or $b->{id} <=> $a->{id}
2745 } values %{ $self->{item} };
2746
2662 my $count = 10 + 1; 2747 my $count = 10 + 1;
2663 for my $item (@items) { 2748 for my $item (@items) {
2664 last unless --$count; 2749 last unless --$count;
2665 2750
2666 push @widgets, $item->{label} ||= do { 2751 push @widgets, $item->{label} ||= do {
2667 # TODO: doesn't handle markup well (read as: at all) 2752 # TODO: doesn't handle markup well (read as: at all)
2668 my $short = delete $item->{text}; 2753 my $short = $item->{count} > 1
2754 ? "<b>$item->{count} ×</b> $item->{text}"
2755 : $item->{text};
2756
2757 my $fontsize = $item->{fontsize} || $self->{fontsize};
2758
2669 for ($short) { 2759 for ($short) {
2670 s/^\s+//; 2760 s/^\s+//;
2671 s/\012.*//s; 2761 s/\s+/ /g;
2672 my $len = int 30 / $item->{fontsize}; 2762 my $len = int 40 / $fontsize;
2673 substr $_, $len, length, "…" if $len < length; 2763 substr $_, $len, length, "…" if $len < length;
2674 } 2764 }
2675 2765
2676 new CFClient::UI::Label 2766 new CFClient::UI::Label
2677 markup => $short, 2767 markup => $short,
2678 tooltip => delete $item->{tooltip}, 2768 tooltip => $item->{tooltip},
2679 tooltip_font => $::FONT_PROP, 2769 tooltip_font => $::FONT_PROP,
2680 tooltip_width => 0.75, 2770 tooltip_width => 0.67,
2681 fontsize => delete $item->{fontsize}, 2771 fontsize => $fontsize,
2682 color => delete $item->{color}, 2772 fg => $item->{fg},
2683 can_events => 1, 2773 can_events => 1,
2684 can_hover => 1, 2774 can_hover => 1
2685 }; 2775 };
2686 } 2776 }
2687 2777
2688 $self->clear; 2778 $self->clear;
2689 $self->SUPER::add (@widgets); 2779 $self->SUPER::add (reverse @widgets);
2690} 2780}
2691 2781
2692sub add { 2782sub add {
2693 my ($self, $text, %arg) = @_; 2783 my ($self, $text, %arg) = @_;
2694 2784
2695 my $item = { 2785 $text =~ s/^\s+//;
2696 time => time, 2786 $text =~ s/\s+$//;
2787
2788 my $timeout = time + ((delete $arg{timeout}) || 60);
2789
2790 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2791
2792 if (my $item = $self->{item}{$group}) {
2793 if ($item->{text} eq $text) {
2794 $item->{count}++;
2795 } else {
2796 $item->{count} = 1;
2797 $item->{text} = $item->{tooltip} = $text;
2798 }
2799 $item->{id} = ++$self->{id};
2800 $item->{timeout} = $timeout;
2801 delete $item->{label};
2802 } else {
2803 $self->{item}{$group} = {
2804 id => ++$self->{id},
2697 text => $text, 2805 text => $text,
2698 timeout => 60, 2806 timeout => $timeout,
2699 tooltip => $text, 2807 tooltip => $text,
2700 fontsize => 0.8,
2701 color => [0.8, 0.8, 0.8, 0.8], 2808 fg => [0.8, 0.8, 0.8, 0.8],
2809 pri => 0,
2810 count => 1,
2702 %arg, 2811 %arg,
2812 };
2703 }; 2813 }
2704
2705 $item->{timeout} += time;
2706 $item->{group} ||= $item+0;
2707
2708 $item = $self->{item}{$item->{group}} ||= $item;
2709 2814
2710 $self->reorder; 2815 $self->reorder;
2711} 2816}
2712 2817
2713############################################################################# 2818#############################################################################
2747} 2852}
2748 2853
2749sub size_allocate { 2854sub size_allocate {
2750 my ($self, $w, $h) = @_; 2855 my ($self, $w, $h) = @_;
2751 2856
2752 my $old_w = $self->{old_w}; 2857 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2753 my $old_h = $self->{old_h}; 2858 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2754 2859
2755 if ($old_w && $old_h) { 2860 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2756 for my $child ($self->children) { 2861 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2757 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2758 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2759 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2760 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2761 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2762 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2763 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2764 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2765 }
2766 }
2767 2862
2768 for my $child ($self->children) { 2863 for my $child ($self->children) {
2769 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2864 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2770 2865
2866 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2867 if exists $child->{req_x};
2868
2869 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2870 if exists $child->{req_y};
2871
2771 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2872 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2772 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2873 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2874
2773 $child->configure ($X, $Y, $W, $H); 2875 $child->configure ($X, $Y, $W, $H);
2774 } 2876 }
2775
2776 $self->{old_w} = $w;
2777 $self->{old_h} = $h;
2778} 2877}
2779 2878
2780sub coord2local { 2879sub coord2local {
2781 my ($self, $x, $y) = @_; 2880 my ($self, $x, $y) = @_;
2782 2881
2795 $self->check_size; 2894 $self->check_size;
2796 $::WANT_REFRESH++; 2895 $::WANT_REFRESH++;
2797} 2896}
2798 2897
2799sub add { 2898sub add {
2800 my ($self, $child) = @_; 2899 my ($self, @children) = @_;
2801 2900
2901 for my $child (@children) {
2902 $child->{toplevel} = 1;
2903
2802 # integerise window positions 2904 # integerise window positions
2803 $child->{x} = int $child->{x}; 2905 $child->{x} = int $child->{x};
2804 $child->{y} = int $child->{y}; 2906 $child->{y} = int $child->{y};
2907 }
2805 2908
2806 $self->SUPER::add ($child); 2909 $self->SUPER::add (@children);
2807} 2910}
2808 2911
2809sub on_refresh { 2912sub on_refresh {
2810 my ($self, $id, $cb) = @_; 2913 my ($self, $id, $cb) = @_;
2811 2914
2812 $self->{refresh_hook}{$id} = $cb; 2915 $self->{refresh_hook}{$id} = $cb;
2813} 2916}
2814 2917
2918sub on_post_alloc {
2919 my ($self, $id, $cb) = @_;
2920
2921 $self->{post_alloc_hook}{$id} = $cb;
2922}
2923
2815sub draw { 2924sub draw {
2816 my ($self) = @_; 2925 my ($self) = @_;
2926
2927 while ($self->{refresh_hook}) {
2928 $_->()
2929 for values %{delete $self->{refresh_hook}};
2930 }
2817 2931
2818 if ($self->{check_size}) { 2932 if ($self->{check_size}) {
2819 my @queue = ([], []); 2933 my @queue = ([], []);
2820 2934
2821 for (;;) { 2935 for (;;) {
2829 2943
2830 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2944 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2831 ? @$widget{qw(user_w user_h)} 2945 ? @$widget{qw(user_w user_h)}
2832 : $widget->size_request; 2946 : $widget->size_request;
2833 2947
2948 if (delete $widget->{force_alloc}
2834 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2949 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2835 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 2950 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2836 2951
2837 $widget->{req_w} = $w; 2952 $widget->{req_w} = $w;
2838 $widget->{req_h} = $h; 2953 $widget->{req_h} = $h;
2839 2954
2840 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2955 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2841 2956
2842 $widget->{parent}->check_size 2957 $widget->{parent}->check_size
2843 if $widget->{parent}; 2958 if $widget->{parent};
2844 } 2959 }
2845 } 2960 }
2857 $widget->size_allocate ($w, $h); 2972 $widget->size_allocate ($w, $h);
2858 $widget->emit (size_allocate => $w, $h); 2973 $widget->emit (size_allocate => $w, $h);
2859 } 2974 }
2860 } 2975 }
2861 2976
2862 while ($self->{refresh_hook}) { 2977 while ($self->{post_alloc_hook}) {
2863 $_->() 2978 $_->()
2864 for values %{delete $self->{refresh_hook}}; 2979 for values %{delete $self->{post_alloc_hook}};
2865 } 2980 }
2866 2981
2867 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2982 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2868 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2983 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2869 glClear GL_COLOR_BUFFER_BIT; 2984 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines