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.199 by root, Fri May 12 15:45:42 2006 UTC vs.
Revision 1.220 by root, Mon May 22 03:28:55 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines