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.200 by root, Sat May 13 00:27:09 2006 UTC vs.
Revision 1.222 by root, Mon May 22 03:59:51 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);
39 $TOOLTIP->check_size; 40 $TOOLTIP->check_size;
40 $TOOLTIP->update; 41 $TOOLTIP->update;
41 } 42 }
132 133
133 for (@$vals) { 134 for (@$vals) {
134 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
135 $rem += $_ - $i; 136 $rem += $_ - $i;
136 $_ = $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;
137 } 169 }
138} 170}
139 171
140############################################################################# 172#############################################################################
141 173
160 if (/^connect_(.*)$/) { 192 if (/^connect_(.*)$/) {
161 $self->connect ($1 => delete $self->{$_}); 193 $self->connect ($1 => delete $self->{$_});
162 } 194 }
163 } 195 }
164 196
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198
165 $self 199 $self
166} 200}
167 201
168sub destroy { 202sub destroy {
169 my ($self) = @_; 203 my ($self) = @_;
172 %$self = (); 206 %$self = ();
173} 207}
174 208
175sub show { 209sub show {
176 my ($self) = @_; 210 my ($self) = @_;
177
178 return if $self->{parent}; 211 return if $self->{parent};
179 212
180 $CFClient::UI::ROOT->add ($self); 213 $CFClient::UI::ROOT->add ($self);
181} 214}
182 215
242 275
243sub size_allocate { 276sub size_allocate {
244 # nothing to be done 277 # nothing to be done
245} 278}
246 279
247sub children {
248}
249
250# call when resolution changes etc.
251sub reconfigure { 280sub reconfigure {
252 my ($self) = @_; 281 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256 282
257 $self->check_size (1); 283 $self->check_size (1);
258 $self->update; 284 $self->update;
259} 285}
260 286
287sub children {
288}
289
261sub set_max_size { 290sub set_max_size {
262 my ($self, $w, $h) = @_; 291 my ($self, $w, $h) = @_;
263 292
264 delete $self->{max_w}; $self->{max_w} = $w if $w; 293 delete $self->{max_w}; $self->{max_w} = $w if $w;
265 delete $self->{max_h}; $self->{max_h} = $h if $h; 294 delete $self->{max_h}; $self->{max_h} = $h if $h;
295}
296
297sub set_tooltip {
298 my ($self, $tooltip) = @_;
299
300 $self->{tooltip} = $tooltip;
301
302 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
303 delete $CFClient::UI::TOOLTIP->{owner};
304 CFClient::UI::check_tooltip;
305 }
266} 306}
267 307
268# translate global coordinates to local coordinate system 308# translate global coordinates to local coordinate system
269sub coord2local { 309sub coord2local {
270 my ($self, $x, $y) = @_; 310 my ($self, $x, $y) = @_;
356 glVertex $self->{w}, 0; 396 glVertex $self->{w}, 0;
357 glVertex $self->{w}, $self->{h}; 397 glVertex $self->{w}, $self->{h};
358 glVertex 0 , $self->{h}; 398 glVertex 0 , $self->{h};
359 glEnd; 399 glEnd;
360 glPopMatrix; 400 glPopMatrix;
361 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 401 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
362 } 402 }
363} 403}
364 404
365sub _draw { 405sub _draw {
366 my ($self) = @_; 406 my ($self) = @_;
386 Scalar::Util::weaken ($self->{parent} = $parent); 426 Scalar::Util::weaken ($self->{parent} = $parent);
387 427
388 # TODO: req_w _does_change after ->reconfigure 428 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size 429 $self->check_size
390 unless exists $self->{req_w}; 430 unless exists $self->{req_w};
431
432 $self->show;
391} 433}
392 434
393sub check_size { 435sub check_size {
394 my ($self, $forced) = @_; 436 my ($self, $forced) = @_;
395 437
417} 459}
418 460
419sub DESTROY { 461sub DESTROY {
420 my ($self) = @_; 462 my ($self) = @_;
421 463
464 delete $WIDGET{$self+0};
422 #$self->deactivate; 465 #$self->deactivate;
423} 466}
424 467
425############################################################################# 468#############################################################################
426 469
435 my $class = shift; 478 my $class = shift;
436 479
437 # range [value, low, high, page] 480 # range [value, low, high, page]
438 481
439 $class->SUPER::new ( 482 $class->SUPER::new (
440 bg => [0, 0, 0, 0.2], 483 #bg => [0, 0, 0, 0.2],
441 active_bg => [1, 1, 1, 0.5], 484 #active_bg => [1, 1, 1, 0.5],
442 @_ 485 @_
443 ) 486 )
444} 487}
445 488
446sub _draw { 489sub _draw {
447 my ($self) = @_; 490 my ($self) = @_;
448 491
492 my $color = $FOCUS == $self && $self->{active_bg}
493 ? $self->{active_bg}
494 : $self->{bg};
495
496 if ($color && (@$color < 4 || $color->[3])) {
449 my ($w, $h) = @$self{qw(w h)}; 497 my ($w, $h) = @$self{qw(w h)};
450 498
451 glEnable GL_BLEND; 499 glEnable GL_BLEND;
452 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 500 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
453 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 501 glColor @$color;
454 502
455 glBegin GL_QUADS; 503 glBegin GL_QUADS;
456 glVertex 0 , 0; 504 glVertex 0 , 0;
457 glVertex 0 , $h; 505 glVertex 0 , $h;
458 glVertex $w, $h; 506 glVertex $w, $h;
459 glVertex $w, 0; 507 glVertex $w, 0;
460 glEnd; 508 glEnd;
461 509
462 glDisable GL_BLEND; 510 glDisable GL_BLEND;
511 }
463} 512}
464 513
465############################################################################# 514#############################################################################
466 515
467package CFClient::UI::Empty; 516package CFClient::UI::Empty;
651 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 700 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
652 glClearColor 0, 0, 0, 0; 701 glClearColor 0, 0, 0, 0;
653 glClear GL_COLOR_BUFFER_BIT; 702 glClear GL_COLOR_BUFFER_BIT;
654 703
655 $self->_render; 704 $self->_render;
656# glColorMask 1, 1, 1, 0;
657# glEnable GL_BLEND;
658# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
659# glRasterPos 0, 0;
660# glCopyPixels 0, 0, $self->{w}, $self->{h};
661# glDisable GL_BLEND;
662# glColorMask 1, 1, 1, 1;
663 }; 705 };
664} 706}
665 707
666sub _draw { 708sub _draw {
667 my ($self) = @_; 709 my ($self) = @_;
671 my $tex = $self->{texture} 713 my $tex = $self->{texture}
672 or return; 714 or return;
673 715
674 glEnable GL_TEXTURE_2D; 716 glEnable GL_TEXTURE_2D;
675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 717 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
676 glColor 0, 0, 0, 1; 718 glColor 1, 1, 1, 1;
677 719
678 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 720 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
679 721
680 glDisable GL_TEXTURE_2D; 722 glDisable GL_TEXTURE_2D;
681} 723}
764 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 806 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
765 }, 807 },
766 ; 808 ;
767 809
768 $self = $class->SUPER::new ( 810 $self = $class->SUPER::new (
769 vp => (new CFClient::UI::ViewPort), 811 vp => (new CFClient::UI::ViewPort expand => 1),
770 slider => $slider, 812 slider => $slider,
771 @_, 813 @_,
772 ); 814 );
773 815
774 $self->{vp}->add ($self->{scrolled}); 816 $self->{vp}->add ($self->{scrolled});
840 882
841 my $self = $class->SUPER::new ( 883 my $self = $class->SUPER::new (
842 bg => [1, 1, 1, 1], 884 bg => [1, 1, 1, 1],
843 border_bg => [1, 1, 1, 1], 885 border_bg => [1, 1, 1, 1],
844 border => 0.6, 886 border => 0.6,
887 toplevel => 1,
845 can_events => 1, 888 can_events => 1,
846 @_ 889 @_
847 ); 890 );
848 891
849 $self->{title} &&= new CFClient::UI::Label 892 $self->{title} &&= new CFClient::UI::Label
874 my ($self, $w, $h) = @_; 917 my ($self, $w, $h) = @_;
875 918
876 $h -= List::Util::max 0, $self->border * 2; 919 $h -= List::Util::max 0, $self->border * 2;
877 $w -= List::Util::max 0, $self->border * 2; 920 $w -= List::Util::max 0, $self->border * 2;
878 921
879 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 922 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
880 if $self->{title}; 923 if $self->{title};
881 924
882 $self->child->configure ($self->border, $self->border, $w, $h); 925 $self->child->configure ($self->border, $self->border, $w, $h);
883} 926}
884 927
1245 1288
1246############################################################################# 1289#############################################################################
1247 1290
1248package CFClient::UI::Label; 1291package CFClient::UI::Label;
1249 1292
1250our @ISA = CFClient::UI::Base::; 1293our @ISA = CFClient::UI::DrawBG::;
1251 1294
1252use CFClient::OpenGL; 1295use CFClient::OpenGL;
1253 1296
1254sub new { 1297sub new {
1255 my ($class, %arg) = @_; 1298 my ($class, %arg) = @_;
1256 1299
1257 my $self = $class->SUPER::new ( 1300 my $self = $class->SUPER::new (
1258 fg => [1, 1, 1], 1301 fg => [1, 1, 1],
1302 #bg => none
1303 #active_bg => none
1259 #font => default_font 1304 #font => default_font
1260 #text => initial text 1305 #text => initial text
1261 #markup => initial narkup 1306 #markup => initial narkup
1307 #max_w => maximum pixel width
1308 ellipsise => 3, # end
1262 layout => (new CFClient::Layout), 1309 layout => (new CFClient::Layout),
1263 fontsize => 1, 1310 fontsize => 1,
1264 align => -1, 1311 align => -1,
1265 valign => -1, 1312 valign => -1,
1266 padding => 2, 1313 padding => 2,
1281 } 1328 }
1282 1329
1283 $self 1330 $self
1284} 1331}
1285 1332
1286sub escape { 1333sub escape($) {
1287 local $_ = $_[1]; 1334 local $_ = $_[0];
1288 1335
1289 s/&/&amp;/g; 1336 s/&/&amp;/g;
1290 s/>/&gt;/g; 1337 s/>/&gt;/g;
1291 s/</&lt;/g; 1338 s/</&lt;/g;
1292 1339
1293 $_[1] 1340 $_
1294} 1341}
1295 1342
1296sub update { 1343sub update {
1297 my ($self) = @_; 1344 my ($self) = @_;
1298 1345
1331sub size_request { 1378sub size_request {
1332 my ($self) = @_; 1379 my ($self) = @_;
1333 1380
1334 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1381 $self->{layout}->set_font ($self->{font}) if $self->{font};
1335 $self->{layout}->set_width ($self->{max_w} || -1); 1382 $self->{layout}->set_width ($self->{max_w} || -1);
1383 $self->{layout}->set_ellipsise ($self->{ellipsise});
1384 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1336 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1385 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1337 1386
1338 my ($w, $h) = $self->{layout}->size; 1387 my ($w, $h) = $self->{layout}->size;
1339 1388
1340 if (exists $self->{template}) { 1389 if (exists $self->{template}) {
1370} 1419}
1371 1420
1372sub _draw { 1421sub _draw {
1373 my ($self) = @_; 1422 my ($self) = @_;
1374 1423
1424 $self->SUPER::_draw; # draw background, if applicable
1425
1375 my $tex = $self->{texture} ||= do { 1426 my $tex = $self->{texture} ||= do {
1376 $self->{layout}->set_foreground (@{$self->{fg}}); 1427 $self->{layout}->set_foreground (@{$self->{fg}});
1377 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1428 $self->{layout}->set_font ($self->{font}) if $self->{font};
1378 $self->{layout}->set_width ($self->{w}); 1429 $self->{layout}->set_width ($self->{w});
1430 $self->{layout}->set_ellipsise ($self->{ellipsise});
1431 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1379 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1432 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1380 1433
1381 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1434 my $tex = new_from_layout CFClient::Texture $self->{layout};
1382 1435
1383 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1436 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1384 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1437 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1385 : ($self->{w} - $tex->{w}) * 0.5; 1438 : ($self->{w} - $tex->{w}) * 0.5);
1386 1439
1387 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1440 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1388 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1441 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1389 : ($self->{h} - $tex->{h}) * 0.5; 1442 : ($self->{h} - $tex->{h}) * 0.5);
1390 1443
1391 $tex 1444 $tex
1392 }; 1445 };
1393 1446
1394 glEnable GL_TEXTURE_2D; 1447 glEnable GL_TEXTURE_2D;
1449sub set_text { 1502sub set_text {
1450 my ($self, $text) = @_; 1503 my ($self, $text) = @_;
1451 1504
1452 $self->{cursor} = length $text; 1505 $self->{cursor} = length $text;
1453 $self->_set_text ($text); 1506 $self->_set_text ($text);
1507 $self->update;
1454 $self->check_size; 1508 $self->check_size;
1455 $self->update;
1456} 1509}
1457 1510
1458sub get_text { 1511sub get_text {
1459 $_[0]{text} 1512 $_[0]{text}
1460} 1513}
1500 substr $text, $self->{cursor}++, 0, chr $uni; 1553 substr $text, $self->{cursor}++, 0, chr $uni;
1501 } 1554 }
1502 1555
1503 $self->_set_text ($text); 1556 $self->_set_text ($text);
1504 $self->update; 1557 $self->update;
1558 $self->check_size;
1505} 1559}
1506 1560
1507sub focus_in { 1561sub focus_in {
1508 my ($self) = @_; 1562 my ($self) = @_;
1509 1563
1638 my $class = shift; 1692 my $class = shift;
1639 1693
1640 $class->SUPER::new ( 1694 $class->SUPER::new (
1641 padding => 4, 1695 padding => 4,
1642 fg => [1, 1, 1], 1696 fg => [1, 1, 1],
1643 bg => [1, 1, 1, 0.2],
1644 active_fg => [0, 0, 1], 1697 active_fg => [0, 0, 1],
1645 can_hover => 1, 1698 can_hover => 1,
1646 align => 0, 1699 align => 0,
1647 valign => 0, 1700 valign => 0,
1648 can_events => 1, 1701 can_events => 1,
1696 1749
1697 $class->SUPER::new ( 1750 $class->SUPER::new (
1698 padding => 2, 1751 padding => 2,
1699 fg => [1, 1, 1], 1752 fg => [1, 1, 1],
1700 active_fg => [1, 1, 0], 1753 active_fg => [1, 1, 0],
1754 bg => [0, 0, 0, 0.2],
1755 active_bg => [1, 1, 1, 0.5],
1701 state => 0, 1756 state => 0,
1702 can_hover => 1, 1757 can_hover => 1,
1703 @_ 1758 @_
1704 ) 1759 )
1705} 1760}
1989 qw(s1_slider.png s1_slider_bg.png); 2044 qw(s1_slider.png s1_slider_bg.png);
1990 2045
1991sub new { 2046sub new {
1992 my $class = shift; 2047 my $class = shift;
1993 2048
1994 # range [value, low, high, page] 2049 # range [value, low, high, page, unit]
1995 2050
1996 # TODO: 0-width page 2051 # TODO: 0-width page
1997 # TODO: req_w/h are wrong with vertical 2052 # TODO: req_w/h are wrong with vertical
1998 # TODO: calculations are off 2053 # TODO: calculations are off
1999 my $self = $class->SUPER::new ( 2054 my $self = $class->SUPER::new (
2000 fg => [1, 1, 1], 2055 fg => [1, 1, 1],
2001 active_fg => [0, 0, 0], 2056 active_fg => [0, 0, 0],
2057 bg => [0, 0, 0, 0.2],
2058 active_bg => [1, 1, 1, 0.5],
2002 range => [0, 0, 100, 10], 2059 range => [0, 0, 100, 10],
2003 req_w => $::WIDTH / 80, 2060 req_w => $::WIDTH / 80,
2004 req_h => $::WIDTH / 80, 2061 req_h => $::WIDTH / 80,
2005 vertical => 0, 2062 vertical => 0,
2006 can_hover => 1, 2063 can_hover => 1,
2007 inner_pad => 5, 2064 inner_pad => 0.02,
2008 @_ 2065 @_
2009 ); 2066 );
2010 2067
2068 $self->set_value ($self->{range}[0]);
2069 $self->update;
2070
2011 $self 2071 $self
2072}
2073
2074sub set_value {
2075 my ($self, $value) = @_;
2076
2077 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2078
2079 $hi = $lo + 1 if $hi <= $lo;
2080
2081 $value = $lo if $value < $lo;
2082 $value = $hi if $value > $hi;
2083
2084 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2085 if $unit;
2086
2087 $page = $hi - $lo if $page > $hi - $lo;
2088
2089 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2090
2091 if ($value != $old_value) {
2092 $self->emit (changed => $value);
2093 $self->update;
2094 }
2012} 2095}
2013 2096
2014sub size_request { 2097sub size_request {
2015 my ($self) = @_; 2098 my ($self) = @_;
2016 2099
2029 2112
2030sub mouse_motion { 2113sub mouse_motion {
2031 my ($self, $ev, $x, $y) = @_; 2114 my ($self, $ev, $x, $y) = @_;
2032 2115
2033 if ($GRAB == $self) { 2116 if ($GRAB == $self) {
2117 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2118
2119 my (undef, $lo, $hi, $page) = @{$self->{range}};
2120
2121 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2122
2123 $self->set_value ($x * ($hi - $lo) + $lo);
2124 }
2125}
2126
2127sub update {
2128 my ($self) = @_;
2129
2130 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2131 $self->set_value ($self->{range}[0]);
2132
2034 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2133 my ($value, $lo, $hi, $page) = @{$self->{range}};
2035 2134
2036 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2135 my $inner_w = 1 - 2 * $self->{inner_pad};
2037 2136
2038 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2137 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2039 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2040 2138
2041 $x -= $inner_pad_px; # substract the padding 2139 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2042 $x = $x * ($hi - $lo) / $inner_w + $lo; 2140 $value = $self->{scale} * ($value - $lo);
2043 $x = $lo if $x < $lo;
2044 $x = $hi - $page if $x > $hi - $page;
2045 $self->{range}[0] = $x;
2046 2141
2047 $self->emit (changed => $x); 2142 $value = $self->{inner_pad} + ($value - $page * 0.5);
2048 $self->update; 2143
2144 $value = 0 if $value < 0;
2145 $page = 1 - $value if $value + $page > 1;
2146
2147 $self->{knob_x} = $value;
2148 $self->{knob_w} = $page;
2049 } 2149 });
2050}
2051 2150
2052# the inner_* stuff is for generating a padding for the slider handle, 2151 $self->SUPER::update;
2053# so that the handle doesn't leave the texture. This calculation isn't 100%
2054# correct propably, but it does the job for now
2055sub _calc_inner_pad_px {
2056 my ($self, $w) = @_;
2057 ($w / 100) * $self->{inner_pad} # % to pixels
2058} 2152}
2059 2153
2060sub _draw { 2154sub _draw {
2061 my ($self) = @_; 2155 my ($self) = @_;
2062 2156
2063 $self->SUPER::_draw (); 2157 $self->SUPER::_draw ();
2064 2158
2065 my ($w, $h) = @$self{qw(w h)}; 2159 glScale $self->{w}, $self->{h};
2066 2160
2067 if ($self->{vertical}) { 2161 if ($self->{vertical}) {
2068 # draw a vertical slider like a rotated horizontal slider 2162 # draw a vertical slider like a rotated horizontal slider
2069 2163
2164 glTranslate 1, 0, 0;
2070 glRotate 90, 0, 0, 1; 2165 glRotate 90, 0, 0, 1;
2071 glTranslate 0, -$self->{w}, 0;
2072
2073 ($w, $h) = ($h, $w);
2074 } 2166 }
2075 2167
2076 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2168 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2077 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2169 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2078 2170
2079 my ($value, $lo, $hi, $page) = @{$self->{range}};
2080
2081 $hi = $value + 1 if $lo == $hi;
2082
2083 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2084 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2085
2086 $page = int $page * $inner_w / ($hi - $lo);
2087 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2088
2089 $w -= $page;
2090 $page &= ~1;
2091 glTranslate $page * 0.5, 0, 0;
2092 $page ||= 2;
2093
2094 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2095 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2096
2097 glEnable GL_TEXTURE_2D; 2171 glEnable GL_TEXTURE_2D;
2098 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2172 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2099 2173
2100 # draw background 2174 # draw background
2101 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2175 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2102 2176
2103 # draw handle 2177 # draw handle
2104 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2178 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2105 2179
2106 glDisable GL_TEXTURE_2D; 2180 glDisable GL_TEXTURE_2D;
2107} 2181}
2108 2182
2109############################################################################# 2183#############################################################################
2142 2216
2143 $self->{fontsize} = $fontsize; 2217 $self->{fontsize} = $fontsize;
2144 $self->reflow; 2218 $self->reflow;
2145} 2219}
2146 2220
2221sub size_allocate {
2222 my ($self, $w, $h) = @_;
2223
2224 $self->SUPER::size_allocate ($w, $h);
2225
2226 $self->{layout}->set_font ($self->{font}) if $self->{font};
2227 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2228 $self->{layout}->set_width ($self->{children}[0]{w});
2229
2230 $self->reflow;
2231}
2232
2147sub text_height { 2233sub text_height {
2148 my ($self, $text) = @_; 2234 my ($self, $text, $indent) = @_;
2149 2235
2150 my $layout = $self->{layout}; 2236 my $layout = $self->{layout};
2151 2237
2152 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2238 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2153 $layout->set_width ($self->{children}[0]{w}); 2239 $layout->set_width ($self->{children}[0]{w} - $indent);
2154 $layout->set_markup ($text); 2240 $layout->set_markup ($text);
2155 2241
2156 ($layout->size)[1] 2242 ($layout->size)[1]
2157} 2243}
2158 2244
2161 2247
2162 $self->{need_reflow}++; 2248 $self->{need_reflow}++;
2163 $self->update; 2249 $self->update;
2164} 2250}
2165 2251
2166sub size_allocate {
2167 my ($self, $w, $h) = @_;
2168
2169 $self->SUPER::size_allocate ($w, $h);
2170
2171 $self->{layout}->set_font ($self->{font}) if $self->{font};
2172 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2173 $self->{layout}->set_width ($self->{children}[0]{w});
2174
2175 $self->reflow;
2176}
2177
2178sub add_paragraph { 2252sub add_paragraph {
2179 my ($self, $color, $text) = @_; 2253 my ($self, $color, $text, $indent) = @_;
2180 2254
2181 #TODO: intelligently "reformat" paragraph 2255 #TODO: intelligently "reformat" paragraph
2182 2256
2257 for my $line (split /\n/, $text) {
2183 my $height = $self->text_height ($text); 2258 my $height = $self->text_height ($line);
2184
2185 $self->{height} += $height; 2259 $self->{height} += $height;
2186
2187 push @{$self->{par}}, [$height, $color, $text]; 2260 push @{$self->{par}}, [$height, $color, $indent, $line];
2261 }
2188 2262
2189 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2263 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2190 $self->{children}[1]->update; 2264 $self->{children}[1]->update;
2191} 2265}
2192 2266
2201 2275
2202 $ROOT->on_post_alloc ($self, sub { 2276 $ROOT->on_post_alloc ($self, sub {
2203 if (delete $self->{need_reflow}) { 2277 if (delete $self->{need_reflow}) {
2204 my $height = 0; 2278 my $height = 0;
2205 2279
2206 $height += $_->[0] = $self->text_height ($_->[2]) 2280 $height += $_->[0] = $self->text_height ($_->[3], $_->[2])
2207 for @{$self->{par}}; 2281 for @{$self->{par}};
2208 2282
2209 $self->{height} = $height; 2283 $self->{height} = $height;
2210 2284
2211 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2285 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2286 $self->{children}[1]->update;
2212 2287
2213 delete $self->{texture}; 2288 delete $self->{texture};
2214 } 2289 }
2215 2290
2216 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2291 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2217 glClearColor 0, 0, 0, 0; 2292 glClearColor 0.5, 0.5, 0.5, 0;
2218 glClear GL_COLOR_BUFFER_BIT; 2293 glClear GL_COLOR_BUFFER_BIT;
2219
2220 glEnable GL_TEXTURE_2D;
2221 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2222 2294
2223 my $top = int $self->{children}[1]{range}[0]; 2295 my $top = int $self->{children}[1]{range}[0];
2224 2296
2225 my $y0 = $top; 2297 my $y0 = $top;
2226 my $y1 = $top + $self->{h}; 2298 my $y1 = $top + $self->{h};
2229 2301
2230 my $layout = $self->{layout}; 2302 my $layout = $self->{layout};
2231 2303
2232 $layout->set_font ($self->{font}) if $self->{font}; 2304 $layout->set_font ($self->{font}) if $self->{font};
2233 2305
2306 glEnable GL_BLEND;
2307 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2308
2234 for my $par (@{$self->{par}}) { 2309 for my $par (@{$self->{par}}) {
2235 my $h = $par->[0]; 2310 my $h = $par->[0];
2236 2311
2237 if ($y0 < $y + $h && $y < $y1) { 2312 if ($y0 < $y + $h && $y < $y1) {
2238 $layout->set_foreground (@{ $par->[1] }); 2313 $layout->set_foreground (@{ $par->[1] });
2314 $layout->set_width ($self->{w} - $par->[2]);
2239 $layout->set_markup ($par->[2]); 2315 $layout->set_markup ($par->[3]);
2240 2316
2241 my ($W, $H) = $layout->size; 2317 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2242 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2318
2319 glRasterPos $par->[2], $y - $y0;
2320 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2243 } 2321 }
2244 2322
2245 $y += $h; 2323 $y += $h;
2246 } 2324 }
2247 2325
2248 glDisable GL_TEXTURE_2D; 2326 glDisable GL_BLEND;
2249 }; 2327 };
2250 }); 2328 });
2251} 2329}
2252 2330
2253sub _draw { 2331sub _draw {
2254 my ($self) = @_; 2332 my ($self) = @_;
2255 2333
2256 glEnable GL_TEXTURE_2D; 2334 glEnable GL_TEXTURE_2D;
2257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2335 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2258 glColor 1, 1, 1, 1; 2336 glColor 1, 1, 1, 1;
2259 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2337 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2260 glDisable GL_TEXTURE_2D; 2338 glDisable GL_TEXTURE_2D;
2261 2339
2262 $self->{children}[1]->draw; 2340 $self->{children}[1]->draw;
2263 2341
2264} 2342}
2363 2441
2364sub set_tooltip_from { 2442sub set_tooltip_from {
2365 my ($self, $widget) = @_; 2443 my ($self, $widget) = @_;
2366 2444
2367 $self->add (new CFClient::UI::Label 2445 $self->add (new CFClient::UI::Label
2368 markup => $widget->{tooltip}, 2446 markup => $widget->{tooltip},
2369 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2447 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2370 fontsize => 0.8, 2448 fontsize => 0.8,
2371 fg => [0, 0, 0, 1], 2449 fg => [0, 0, 0, 1],
2450 ellipsise => 0,
2372 font => ($widget->{tooltip_font} || $::FONT_PROP), 2451 font => ($widget->{tooltip_font} || $::FONT_PROP),
2373 ); 2452 );
2374} 2453}
2375 2454
2376sub size_request { 2455sub size_request {
2377 my ($self) = @_; 2456 my ($self) = @_;
2423use CFClient::OpenGL; 2502use CFClient::OpenGL;
2424 2503
2425sub new { 2504sub new {
2426 my $class = shift; 2505 my $class = shift;
2427 2506
2428 $class->SUPER::new ( 2507 my $self = $class->SUPER::new (
2429 aspect => 1, 2508 aspect => 1,
2430 @_, 2509 @_,
2431 ) 2510 );
2511
2512 if ($self->{anim} && $self->{animspeed}) {
2513 Scalar::Util::weaken (my $widget = $self);
2514
2515 $self->{timer} = Event->timer (
2516 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2517 hard => 1,
2518 interval => $self->{animspeed},
2519 cb => sub {
2520 ++$widget->{frame};
2521 $widget->update;
2522 },
2523 );
2524 }
2525
2526 $self
2432} 2527}
2433 2528
2434sub size_request { 2529sub size_request {
2435 (32, 8) 2530 (32, 8)
2436} 2531}
2437 2532
2533sub update {
2534 my ($self) = @_;
2535
2536 return unless $self->{visible};
2537
2538 $self->SUPER::update;
2539}
2540
2438sub _draw { 2541sub _draw {
2439 my ($self) = @_; 2542 my ($self) = @_;
2440 2543
2441 return unless $::CONN;#d# manage and cache textures differently 2544 return unless $::CONN;#d# manage and cache textures differently
2545
2546 my $face;
2547
2548 if ($self->{frame}) {
2549 my $anim = $::CONN->{anim}[$self->{anim}];
2550
2551 $face = $anim->[ $self->{frame} % @$anim ]
2552 if $anim && @$anim;
2553 }
2554
2442 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2555 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2443 2556
2444 # TODO animation
2445 if ($tex) { 2557 if ($tex) {
2446 glEnable GL_TEXTURE_2D; 2558 glEnable GL_TEXTURE_2D;
2447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2559 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2448 glColor 1, 1, 1, 1; 2560 glColor 1, 1, 1, 1;
2449 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2561 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2450 glDisable GL_TEXTURE_2D; 2562 glDisable GL_TEXTURE_2D;
2451 } 2563 }
2452} 2564}
2453 2565
2566sub DESTROY {
2567 my ($self) = @_;
2568
2569 $self->{timer}->cancel
2570 if $self->{timer};
2571
2572 $self->SUPER::DESTROY;
2573}
2574
2454############################################################################# 2575#############################################################################
2455 2576
2456package CFClient::UI::InventoryItem; 2577package CFClient::UI::InventoryItem;
2457 2578
2458our @ISA = CFClient::UI::HBox::; 2579our @ISA = CFClient::UI::HBox::;
2580
2581sub _item_to_desc {
2582 my ($item) = @_;
2583
2584 my $desc =
2585 $item->{nrof} < 2
2586 ? $item->{name}
2587 : "$item->{nrof} × $item->{name_pl}";
2588
2589 $item->{flags} & Crossfire::Protocol::F_OPEN
2590 and $desc .= " (open)";
2591 $item->{flags} & Crossfire::Protocol::F_APPLIED
2592 and $desc .= " (applied)";
2593 $item->{flags} & Crossfire::Protocol::F_UNPAID
2594 and $desc .= " (unpaid)";
2595 $item->{flags} & Crossfire::Protocol::F_MAGIC
2596 and $desc .= " (magic)";
2597 $item->{flags} & Crossfire::Protocol::F_CURSED
2598 and $desc .= " (cursed)";
2599 $item->{flags} & Crossfire::Protocol::F_DAMNED
2600 and $desc .= " (damned)";
2601 $item->{flags} & Crossfire::Protocol::F_LOCKED
2602 and $desc .= " *";
2603
2604 $desc
2605}
2459 2606
2460sub new { 2607sub new {
2461 my $class = shift; 2608 my $class = shift;
2462 2609
2463 my %args = @_; 2610 my %args = @_;
2464 2611
2465 my $item = delete $args{item}; 2612 my $item = delete $args{item};
2466 2613
2467 my $desc = $item->{nrof} < 2 2614 my $desc = _item_to_desc ($item);
2468 ? $item->{name}
2469 : "$item->{nrof} $item->{name_pl}";
2470
2471 2615
2472 my $self = $class->SUPER::new ( 2616 my $self = $class->SUPER::new (
2473 can_hover => 1, 2617 can_hover => 1,
2474 can_events => 1, 2618 can_events => 1,
2475 tooltip => (CFClient::UI::Label->escape ($desc) 2619 tooltip => ((CFClient::UI::Label::escape $desc)
2476 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2620 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2477 connect_button_down => sub { 2621 connect_button_down => sub {
2478 my ($self, $ev, $x, $y) = @_; 2622 my ($self, $ev, $x, $y) = @_;
2479 2623
2480 # todo: maybe put examine on 1? but should just be a tooltip :( 2624 # todo: maybe put examine on 1? but should just be a tooltip :(
2481 if ($ev->{button} == 1) { 2625 if ($ev->{button} == 1) {
2626 my $targ = $::CONN->{player}{tag};
2627
2628 if ($item->{container} == $::CONN->{player}{tag}) {
2629 $targ = $main::OPENCONT;
2630 }
2631
2482 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2632 $::CONN->send ("move $targ $item->{tag} 0");
2483 } elsif ($ev->{button} == 2) { 2633 } elsif ($ev->{button} == 2) {
2484 $::CONN->send ("apply $item->{tag}"); 2634 $::CONN->send ("apply $item->{tag}");
2485 } elsif ($ev->{button} == 3) { 2635 } elsif ($ev->{button} == 3) {
2486 CFClient::UI::Menu->new ( 2636 my @menu_items = (
2487 items => [
2488 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2637 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2489 [
2490 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2491 sub { $::CONN->send ("lock $item->{tag}") },
2492 ],
2493 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2638 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2494 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2639 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2640 (
2641 $item->{flags} & Crossfire::Protocol::F_LOCKED
2642 ? (
2643 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2644 )
2645 : (
2646 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2495 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], 2647 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2648 )
2496 ], 2649 ),
2497 )->popup ($ev); 2650 );
2651
2652 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2498 } 2653 }
2499 2654
2500 1 2655 1
2501 }, 2656 },
2502 %args 2657 %args
2503 ); 2658 );
2659
2504 2660
2505 $self->add (new CFClient::UI::Face 2661 $self->add (new CFClient::UI::Face
2506 can_events => 0, 2662 can_events => 0,
2507 face => $item->{face}, 2663 face => $item->{face},
2508 anim => $item->{anim}, 2664 anim => $item->{anim},
2509 animspeed => $item->{animspeed}, 2665 animspeed => $item->{animspeed},
2510 ); 2666 );
2511 2667
2512 $self->add (new CFClient::UI::Label 2668 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2513 can_events => 0, 2669
2514 text => $desc, 2670 $self->{item} = $item;
2515 ); 2671
2672 $self->update_item;
2516 2673
2517 $self 2674 $self
2675}
2676
2677sub update_item {
2678 my ($self) = @_;
2679
2680 my $desc = _item_to_desc ($self->{item});
2681
2682 $self->{name_lbl}->set_text ($desc);
2518} 2683}
2519 2684
2520############################################################################# 2685#############################################################################
2521 2686
2522package CFClient::UI::Inventory; 2687package CFClient::UI::Inventory;
2538 my ($self, $items) = @_; 2703 my ($self, $items) = @_;
2539 2704
2540 $self->{scrolled}->clear; 2705 $self->{scrolled}->clear;
2541 return unless $items; 2706 return unless $items;
2542 2707
2543 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2708 my @items = sort {
2709 ($a->{type} <=> $b->{type})
2710 or ($a->{name} cmp $b->{name})
2711 } @$items;
2544 2712
2545 $self->{real_items} = \@items; 2713 $self->{real_items} = \@items;
2546 2714
2547 for my $item (@items) { 2715 for my $item (@items) {
2548 my $desc = $item->{nrof} < 2
2549 ? $item->{name}
2550 : "$item->{nrof} $item->{name_pl}";
2551
2552 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2716 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2717 $item->update_item ();
2553 } 2718 }
2719
2720 $self->{scrolled}->add (@items);
2554 2721
2555# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2722# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2556} 2723}
2557 2724
2558sub size_request { 2725sub size_request {
2636 2803
2637package CFClient::UI::Statusbox; 2804package CFClient::UI::Statusbox;
2638 2805
2639our @ISA = CFClient::UI::VBox::; 2806our @ISA = CFClient::UI::VBox::;
2640 2807
2808sub new {
2809 my $class = shift;
2810
2811 $class->SUPER::new (
2812 fontsize => 0.8,
2813 @_,
2814 )
2815}
2816
2641sub reorder { 2817sub reorder {
2642 my ($self) = @_; 2818 my ($self) = @_;
2643 my $NOW = time; 2819 my $NOW = time;
2644 2820
2645 while (my ($k, $v) = each %{ $self->{item} }) { 2821 while (my ($k, $v) = each %{ $self->{item} }) {
2663 ? "<b>$item->{count} ×</b> $item->{text}" 2839 ? "<b>$item->{count} ×</b> $item->{text}"
2664 : $item->{text}; 2840 : $item->{text};
2665 2841
2666 for ($short) { 2842 for ($short) {
2667 s/^\s+//; 2843 s/^\s+//;
2668 s/\012.*/…/s; 2844 s/\s+/ /g;
2669 my $len = int 40 / $item->{fontsize};
2670 substr $_, $len, length, "…" if $len < length;
2671 } 2845 }
2672 2846
2673 new CFClient::UI::Label 2847 new CFClient::UI::Label
2674 markup => $short, 2848 markup => $short,
2675 tooltip => $item->{tooltip}, 2849 tooltip => $item->{tooltip},
2676 tooltip_font => $::FONT_PROP, 2850 tooltip_font => $::FONT_PROP,
2677 tooltip_width => 0.67, 2851 tooltip_width => 0.67,
2678 fontsize => $item->{fontsize}, 2852 fontsize => $item->{fontsize} || $self->{fontsize},
2853 max_w => $::WIDTH * 0.44,
2679 color => $item->{color}, 2854 fg => $item->{fg},
2680 can_events => 1, 2855 can_events => 1,
2681 can_hover => 1 2856 can_hover => 1
2682 }; 2857 };
2683 } 2858 }
2684 2859
2710 $self->{item}{$group} = { 2885 $self->{item}{$group} = {
2711 id => ++$self->{id}, 2886 id => ++$self->{id},
2712 text => $text, 2887 text => $text,
2713 timeout => $timeout, 2888 timeout => $timeout,
2714 tooltip => $text, 2889 tooltip => $text,
2715 fontsize => 0.8,
2716 color => [0.8, 0.8, 0.8, 0.8], 2890 fg => [0.8, 0.8, 0.8, 0.8],
2717 pri => 0, 2891 pri => 0,
2718 count => 1, 2892 count => 1,
2719 %arg, 2893 %arg,
2720 }; 2894 };
2721 } 2895 }
2722 2896
2723 $self->reorder; 2897 $self->reorder;
2724} 2898}
2725 2899
2900sub reconfigure {
2901 my ($self) = @_;
2902
2903 delete $_->{label}
2904 for values %{ $self->{item} || {} };
2905
2906 $self->reorder;
2907 $self->SUPER::reconfigure;
2908}
2909
2726############################################################################# 2910#############################################################################
2727 2911
2728package CFClient::UI::Root; 2912package CFClient::UI::Root;
2729 2913
2730our @ISA = CFClient::UI::Container::; 2914our @ISA = CFClient::UI::Container::;
2760} 2944}
2761 2945
2762sub size_allocate { 2946sub size_allocate {
2763 my ($self, $w, $h) = @_; 2947 my ($self, $w, $h) = @_;
2764 2948
2765 my $old_w = $self->{old_w}; 2949 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2766 my $old_h = $self->{old_h}; 2950 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2767 2951
2768 if ($old_w && $old_h) { 2952 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2769 for my $child ($self->children) { 2953 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2770 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2771 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2772 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2773 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2774 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2775 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2776 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2777 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2778 }
2779 }
2780 2954
2781 for my $child ($self->children) { 2955 for my $child ($self->children) {
2782 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2956 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2783 2957
2958 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2959 if exists $child->{req_x};
2960
2961 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2962 if exists $child->{req_y};
2963
2784 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2964 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2785 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2965 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2966
2786 $child->configure ($X, $Y, $W, $H); 2967 $child->configure ($X, $Y, $W, $H);
2787 } 2968 }
2788
2789 $self->{old_w} = $w;
2790 $self->{old_h} = $h;
2791} 2969}
2792 2970
2793sub coord2local { 2971sub coord2local {
2794 my ($self, $x, $y) = @_; 2972 my ($self, $x, $y) = @_;
2795 2973
2808 $self->check_size; 2986 $self->check_size;
2809 $::WANT_REFRESH++; 2987 $::WANT_REFRESH++;
2810} 2988}
2811 2989
2812sub add { 2990sub add {
2813 my ($self, $child) = @_; 2991 my ($self, @children) = @_;
2814 2992
2993 for my $child (@children) {
2994 $child->{toplevel} = 1;
2995
2815 # integerise window positions 2996 # integerise window positions
2816 $child->{x} = int $child->{x}; 2997 $child->{x} = int $child->{x};
2817 $child->{y} = int $child->{y}; 2998 $child->{y} = int $child->{y};
2999 }
2818 3000
2819 $self->SUPER::add ($child); 3001 $self->SUPER::add (@children);
3002
3003 while (@children) {
3004 my $w = pop @children;
3005 push @children, $w->children;
3006 $w->{visible} = 1;
3007 }
3008}
3009
3010sub remove {
3011 my ($self, @children) = @_;
3012
3013 $self->SUPER::remove (@children);
3014
3015 while (@children) {
3016 my $w = pop @children;
3017 push @children, $w->children;
3018 delete $w->{visible};
3019 }
2820} 3020}
2821 3021
2822sub on_refresh { 3022sub on_refresh {
2823 my ($self, $id, $cb) = @_; 3023 my ($self, $id, $cb) = @_;
2824 3024
2893 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3093 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2894 glClear GL_COLOR_BUFFER_BIT; 3094 glClear GL_COLOR_BUFFER_BIT;
2895 3095
2896 glMatrixMode GL_PROJECTION; 3096 glMatrixMode GL_PROJECTION;
2897 glLoadIdentity; 3097 glLoadIdentity;
2898 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3098 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2899 glMatrixMode GL_MODELVIEW; 3099 glMatrixMode GL_MODELVIEW;
2900 glLoadIdentity; 3100 glLoadIdentity;
2901 3101
2902 $self->_draw; 3102 $self->_draw;
2903} 3103}
2905############################################################################# 3105#############################################################################
2906 3106
2907package CFClient::UI; 3107package CFClient::UI;
2908 3108
2909$ROOT = new CFClient::UI::Root; 3109$ROOT = new CFClient::UI::Root;
2910$TOOLTIP = new CFClient::UI::Tooltip; 3110$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2911 3111
29121 31121
2913 3113

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines