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.198 by root, Fri May 12 02:08:52 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) = @_;
383sub set_parent { 424sub set_parent {
384 my ($self, $parent) = @_; 425 my ($self, $parent) = @_;
385 426
386 Scalar::Util::weaken ($self->{parent} = $parent); 427 Scalar::Util::weaken ($self->{parent} = $parent);
387 428
388
389 # TODO: req_w _does_change after ->reconfigure 429 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size 430 $self->check_size
391 unless exists $self->{req_w}; 431 unless exists $self->{req_w};
392} 432}
393 433
418} 458}
419 459
420sub DESTROY { 460sub DESTROY {
421 my ($self) = @_; 461 my ($self) = @_;
422 462
463 delete $WIDGET{$self+0};
423 #$self->deactivate; 464 #$self->deactivate;
424} 465}
425 466
426############################################################################# 467#############################################################################
427 468
436 my $class = shift; 477 my $class = shift;
437 478
438 # range [value, low, high, page] 479 # range [value, low, high, page]
439 480
440 $class->SUPER::new ( 481 $class->SUPER::new (
441 bg => [0, 0, 0, 0.2], 482 #bg => [0, 0, 0, 0.2],
442 active_bg => [1, 1, 1, 0.5], 483 #active_bg => [1, 1, 1, 0.5],
443 @_ 484 @_
444 ) 485 )
445} 486}
446 487
447sub _draw { 488sub _draw {
448 my ($self) = @_; 489 my ($self) = @_;
449 490
491 my $color = $FOCUS == $self && $self->{active_bg}
492 ? $self->{active_bg}
493 : $self->{bg};
494
495 if ($color && (@$color < 4 || $color->[3])) {
450 my ($w, $h) = @$self{qw(w h)}; 496 my ($w, $h) = @$self{qw(w h)};
451 497
452 glEnable GL_BLEND; 498 glEnable GL_BLEND;
453 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
454 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 500 glColor @$color;
455 501
456 glBegin GL_QUADS; 502 glBegin GL_QUADS;
457 glVertex 0 , 0; 503 glVertex 0 , 0;
458 glVertex 0 , $h; 504 glVertex 0 , $h;
459 glVertex $w, $h; 505 glVertex $w, $h;
460 glVertex $w, 0; 506 glVertex $w, 0;
461 glEnd; 507 glEnd;
462 508
463 glDisable GL_BLEND; 509 glDisable GL_BLEND;
510 }
464} 511}
465 512
466############################################################################# 513#############################################################################
467 514
468package CFClient::UI::Empty; 515package CFClient::UI::Empty;
652 $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 {
653 glClearColor 0, 0, 0, 0; 700 glClearColor 0, 0, 0, 0;
654 glClear GL_COLOR_BUFFER_BIT; 701 glClear GL_COLOR_BUFFER_BIT;
655 702
656 $self->_render; 703 $self->_render;
657# glColorMask 1, 1, 1, 0;
658# glEnable GL_BLEND;
659# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
660# glRasterPos 0, 0;
661# glCopyPixels 0, 0, $self->{w}, $self->{h};
662# glDisable GL_BLEND;
663# glColorMask 1, 1, 1, 1;
664 }; 704 };
665} 705}
666 706
667sub _draw { 707sub _draw {
668 my ($self) = @_; 708 my ($self) = @_;
672 my $tex = $self->{texture} 712 my $tex = $self->{texture}
673 or return; 713 or return;
674 714
675 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
677 glColor 0, 0, 0, 1; 717 glColor 1, 1, 1, 1;
678 718
679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 719 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
680 720
681 glDisable GL_TEXTURE_2D; 721 glDisable GL_TEXTURE_2D;
682} 722}
765 $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}));
766 }, 806 },
767 ; 807 ;
768 808
769 $self = $class->SUPER::new ( 809 $self = $class->SUPER::new (
770 vp => (new CFClient::UI::ViewPort), 810 vp => (new CFClient::UI::ViewPort expand => 1),
771 slider => $slider, 811 slider => $slider,
772 @_, 812 @_,
773 ); 813 );
774 814
775 $self->{vp}->add ($self->{scrolled}); 815 $self->{vp}->add ($self->{scrolled});
777 $self->add ($self->{slider}); 817 $self->add ($self->{slider});
778 818
779 $self 819 $self
780} 820}
781 821
782#TODO# update range on size_allocate depeneing on child 822#TODO# update range on size_allocate depending on child
783# update viewport offset on scroll 823# update viewport offset on scroll
784 824
785############################################################################# 825#############################################################################
786 826
787package CFClient::UI::Frame; 827package CFClient::UI::Frame;
788 828
789our @ISA = CFClient::UI::Bin::; 829our @ISA = CFClient::UI::Bin::;
790 830
791use CFClient::OpenGL; 831use CFClient::OpenGL;
832
833sub new {
834 my $class = shift;
835
836 $class->SUPER::new (
837 bg => undef,
838 @_,
839 )
840}
841
842sub _draw {
843 my ($self) = @_;
844
845 if ($self->{bg}) {
846 my ($w, $h) = @$self{qw(w h)};
847
848 glEnable GL_BLEND;
849 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
850 glColor @{ $self->{bg} };
851
852 glBegin GL_QUADS;
853 glVertex 0 , 0;
854 glVertex 0 , $h;
855 glVertex $w, $h;
856 glVertex $w, 0;
857 glEnd;
858
859 glDisable GL_BLEND;
860 }
861
862 $self->SUPER::_draw;
863}
792 864
793############################################################################# 865#############################################################################
794 866
795package CFClient::UI::FancyFrame; 867package CFClient::UI::FancyFrame;
796 868
809 881
810 my $self = $class->SUPER::new ( 882 my $self = $class->SUPER::new (
811 bg => [1, 1, 1, 1], 883 bg => [1, 1, 1, 1],
812 border_bg => [1, 1, 1, 1], 884 border_bg => [1, 1, 1, 1],
813 border => 0.6, 885 border => 0.6,
886 toplevel => 1,
814 can_events => 1, 887 can_events => 1,
815 @_ 888 @_
816 ); 889 );
817 890
818 $self->{title} &&= new CFClient::UI::Label 891 $self->{title} &&= new CFClient::UI::Label
843 my ($self, $w, $h) = @_; 916 my ($self, $w, $h) = @_;
844 917
845 $h -= List::Util::max 0, $self->border * 2; 918 $h -= List::Util::max 0, $self->border * 2;
846 $w -= List::Util::max 0, $self->border * 2; 919 $w -= List::Util::max 0, $self->border * 2;
847 920
848 $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)
849 if $self->{title}; 922 if $self->{title};
850 923
851 $self->child->configure ($self->border, $self->border, $w, $h); 924 $self->child->configure ($self->border, $self->border, $w, $h);
852} 925}
853 926
1214 1287
1215############################################################################# 1288#############################################################################
1216 1289
1217package CFClient::UI::Label; 1290package CFClient::UI::Label;
1218 1291
1219our @ISA = CFClient::UI::Base::; 1292our @ISA = CFClient::UI::DrawBG::;
1220 1293
1221use CFClient::OpenGL; 1294use CFClient::OpenGL;
1222 1295
1223sub new { 1296sub new {
1224 my ($class, %arg) = @_; 1297 my ($class, %arg) = @_;
1225 1298
1226 my $self = $class->SUPER::new ( 1299 my $self = $class->SUPER::new (
1227 fg => [1, 1, 1], 1300 fg => [1, 1, 1],
1301 #bg => none
1302 #active_bg => none
1228 #font => default_font 1303 #font => default_font
1229 #text => initial text 1304 #text => initial text
1230 #markup => initial narkup 1305 #markup => initial narkup
1306 #max_w => maximum pixel width
1307 ellipsise => 3, # end
1231 layout => (new CFClient::Layout), 1308 layout => (new CFClient::Layout),
1232 fontsize => 1, 1309 fontsize => 1,
1233 align => -1, 1310 align => -1,
1234 valign => -1, 1311 valign => -1,
1235 padding => 2, 1312 padding => 2,
1250 } 1327 }
1251 1328
1252 $self 1329 $self
1253} 1330}
1254 1331
1255sub escape { 1332sub escape($) {
1256 local $_ = $_[1]; 1333 local $_ = $_[0];
1257 1334
1258 s/&/&amp;/g; 1335 s/&/&amp;/g;
1259 s/>/&gt;/g; 1336 s/>/&gt;/g;
1260 s/</&lt;/g; 1337 s/</&lt;/g;
1261 1338
1262 $_[1] 1339 $_
1263} 1340}
1264 1341
1265sub update { 1342sub update {
1266 my ($self) = @_; 1343 my ($self) = @_;
1267 1344
1300sub size_request { 1377sub size_request {
1301 my ($self) = @_; 1378 my ($self) = @_;
1302 1379
1303 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1380 $self->{layout}->set_font ($self->{font}) if $self->{font};
1304 $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});
1305 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1384 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1306 1385
1307 my ($w, $h) = $self->{layout}->size; 1386 my ($w, $h) = $self->{layout}->size;
1308 1387
1309 if (exists $self->{template}) { 1388 if (exists $self->{template}) {
1339} 1418}
1340 1419
1341sub _draw { 1420sub _draw {
1342 my ($self) = @_; 1421 my ($self) = @_;
1343 1422
1423 $self->SUPER::_draw; # draw background, if applicable
1424
1344 my $tex = $self->{texture} ||= do { 1425 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}}); 1426 $self->{layout}->set_foreground (@{$self->{fg}});
1346 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1427 $self->{layout}->set_font ($self->{font}) if $self->{font};
1347 $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});
1348 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1431 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1349 1432
1350 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1433 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351 1434
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1435 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1436 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5; 1437 : ($self->{w} - $tex->{w}) * 0.5);
1355 1438
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1439 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1440 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5; 1441 : ($self->{h} - $tex->{h}) * 0.5);
1359 1442
1360 $tex 1443 $tex
1361 }; 1444 };
1362 1445
1363 glEnable GL_TEXTURE_2D; 1446 glEnable GL_TEXTURE_2D;
1418sub set_text { 1501sub set_text {
1419 my ($self, $text) = @_; 1502 my ($self, $text) = @_;
1420 1503
1421 $self->{cursor} = length $text; 1504 $self->{cursor} = length $text;
1422 $self->_set_text ($text); 1505 $self->_set_text ($text);
1506 $self->update;
1423 $self->check_size; 1507 $self->check_size;
1424 $self->update;
1425} 1508}
1426 1509
1427sub get_text { 1510sub get_text {
1428 $_[0]{text} 1511 $_[0]{text}
1429} 1512}
1449 my $sym = $ev->{sym}; 1532 my $sym = $ev->{sym};
1450 my $uni = $ev->{unicode}; 1533 my $uni = $ev->{unicode};
1451 1534
1452 my $text = $self->get_text; 1535 my $text = $self->get_text;
1453 1536
1454 if ($sym == 8) { 1537 if ($uni == 8) {
1455 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1538 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1456 } elsif ($sym == 127) { 1539 } elsif ($uni == 127) {
1457 substr $text, $self->{cursor}, 1, ""; 1540 substr $text, $self->{cursor}, 1, "";
1458 } elsif ($sym == CFClient::SDLK_LEFT) { 1541 } elsif ($sym == CFClient::SDLK_LEFT) {
1459 --$self->{cursor} if $self->{cursor}; 1542 --$self->{cursor} if $self->{cursor};
1460 } elsif ($sym == CFClient::SDLK_RIGHT) { 1543 } elsif ($sym == CFClient::SDLK_RIGHT) {
1461 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1544 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1462 } elsif ($sym == CFClient::SDLK_HOME) { 1545 } elsif ($sym == CFClient::SDLK_HOME) {
1463 $self->{cursor} = 0; 1546 $self->{cursor} = 0;
1464 } elsif ($sym == CFClient::SDLK_END) { 1547 } elsif ($sym == CFClient::SDLK_END) {
1465 $self->{cursor} = length $text; 1548 $self->{cursor} = length $text;
1466 } elsif ($sym == 27) { 1549 } elsif ($uni == 27) {
1467 $self->emit ('escape'); 1550 $self->emit ('escape');
1468 } elsif ($uni) { 1551 } elsif ($uni) {
1469 substr $text, $self->{cursor}++, 0, chr $uni; 1552 substr $text, $self->{cursor}++, 0, chr $uni;
1470 } 1553 }
1471 1554
1472 $self->_set_text ($text); 1555 $self->_set_text ($text);
1473 $self->update; 1556 $self->update;
1557 $self->check_size;
1474} 1558}
1475 1559
1476sub focus_in { 1560sub focus_in {
1477 my ($self) = @_; 1561 my ($self) = @_;
1478 1562
1607 my $class = shift; 1691 my $class = shift;
1608 1692
1609 $class->SUPER::new ( 1693 $class->SUPER::new (
1610 padding => 4, 1694 padding => 4,
1611 fg => [1, 1, 1], 1695 fg => [1, 1, 1],
1612 bg => [1, 1, 1, 0.2],
1613 active_fg => [0, 0, 1], 1696 active_fg => [0, 0, 1],
1614 can_hover => 1, 1697 can_hover => 1,
1615 align => 0, 1698 align => 0,
1616 valign => 0, 1699 valign => 0,
1617 can_events => 1, 1700 can_events => 1,
1665 1748
1666 $class->SUPER::new ( 1749 $class->SUPER::new (
1667 padding => 2, 1750 padding => 2,
1668 fg => [1, 1, 1], 1751 fg => [1, 1, 1],
1669 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],
1670 state => 0, 1755 state => 0,
1671 can_hover => 1, 1756 can_hover => 1,
1672 @_ 1757 @_
1673 ) 1758 )
1674} 1759}
1958 qw(s1_slider.png s1_slider_bg.png); 2043 qw(s1_slider.png s1_slider_bg.png);
1959 2044
1960sub new { 2045sub new {
1961 my $class = shift; 2046 my $class = shift;
1962 2047
1963 # range [value, low, high, page] 2048 # range [value, low, high, page, unit]
1964 2049
1965 # TODO: 0-width page 2050 # TODO: 0-width page
1966 # TODO: req_w/h are wrong with vertical 2051 # TODO: req_w/h are wrong with vertical
1967 # TODO: calculations are off 2052 # TODO: calculations are off
1968 my $self = $class->SUPER::new ( 2053 my $self = $class->SUPER::new (
1969 fg => [1, 1, 1], 2054 fg => [1, 1, 1],
1970 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],
1971 range => [0, 0, 100, 10], 2058 range => [0, 0, 100, 10],
1972 req_w => $::WIDTH / 80, 2059 req_w => $::WIDTH / 80,
1973 req_h => $::WIDTH / 80, 2060 req_h => $::WIDTH / 80,
1974 vertical => 0, 2061 vertical => 0,
1975 can_hover => 1, 2062 can_hover => 1,
1976 inner_pad => 5, 2063 inner_pad => 0.02,
1977 @_ 2064 @_
1978 ); 2065 );
1979 2066
2067 $self->set_value ($self->{range}[0]);
2068 $self->update;
2069
1980 $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 }
1981} 2094}
1982 2095
1983sub size_request { 2096sub size_request {
1984 my ($self) = @_; 2097 my ($self) = @_;
1985 2098
1998 2111
1999sub mouse_motion { 2112sub mouse_motion {
2000 my ($self, $ev, $x, $y) = @_; 2113 my ($self, $ev, $x, $y) = @_;
2001 2114
2002 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
2003 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2132 my ($value, $lo, $hi, $page) = @{$self->{range}};
2004 2133
2005 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2134 my $inner_w = 1 - 2 * $self->{inner_pad};
2006 2135
2007 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2136 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2008 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2009 2137
2010 $x -= $inner_pad_px; # substract the padding 2138 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2011 $x = $x * ($hi - $lo) / $inner_w + $lo; 2139 $value = $self->{scale} * ($value - $lo);
2012 $x = $lo if $x < $lo;
2013 $x = $hi - $page if $x > $hi - $page;
2014 $self->{range}[0] = $x;
2015 2140
2016 $self->emit (changed => $x); 2141 $value = $self->{inner_pad} + ($value - $page * 0.5);
2017 $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;
2018 } 2148 });
2019}
2020 2149
2021# the inner_* stuff is for generating a padding for the slider handle, 2150 $self->SUPER::update;
2022# so that the handle doesn't leave the texture. This calculation isn't 100%
2023# correct propably, but it does the job for now
2024sub _calc_inner_pad_px {
2025 my ($self, $w) = @_;
2026 ($w / 100) * $self->{inner_pad} # % to pixels
2027} 2151}
2028 2152
2029sub _draw { 2153sub _draw {
2030 my ($self) = @_; 2154 my ($self) = @_;
2031 2155
2032 $self->SUPER::_draw (); 2156 $self->SUPER::_draw ();
2033 2157
2034 my ($w, $h) = @$self{qw(w h)}; 2158 glScale $self->{w}, $self->{h};
2035 2159
2036 if ($self->{vertical}) { 2160 if ($self->{vertical}) {
2037 # draw a vertical slider like a rotated horizontal slider 2161 # draw a vertical slider like a rotated horizontal slider
2038 2162
2163 glTranslate 1, 0, 0;
2039 glRotate 90, 0, 0, 1; 2164 glRotate 90, 0, 0, 1;
2040 glTranslate 0, -$self->{w}, 0;
2041
2042 ($w, $h) = ($h, $w);
2043 } 2165 }
2044 2166
2045 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2167 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2046 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2168 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2047 2169
2048 my ($value, $lo, $hi, $page) = @{$self->{range}};
2049
2050 $hi = $value + 1 if $lo == $hi;
2051
2052 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2053 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2054
2055 $page = int $page * $inner_w / ($hi - $lo);
2056 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2057
2058 $w -= $page;
2059 $page &= ~1;
2060 glTranslate $page * 0.5, 0, 0;
2061 $page ||= 2;
2062
2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2065
2066 glEnable GL_TEXTURE_2D; 2170 glEnable GL_TEXTURE_2D;
2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2171 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2068 2172
2069 # draw background 2173 # draw background
2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2174 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2071 2175
2072 # draw handle 2176 # draw handle
2073 $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);
2074 2178
2075 glDisable GL_TEXTURE_2D; 2179 glDisable GL_TEXTURE_2D;
2076} 2180}
2077 2181
2078############################################################################# 2182#############################################################################
2111 2215
2112 $self->{fontsize} = $fontsize; 2216 $self->{fontsize} = $fontsize;
2113 $self->reflow; 2217 $self->reflow;
2114} 2218}
2115 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
2116sub text_height { 2232sub text_height {
2117 my ($self, $text) = @_; 2233 my ($self, $text, $indent) = @_;
2118 2234
2119 my $layout = $self->{layout}; 2235 my $layout = $self->{layout};
2120 2236
2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2237 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2122 $layout->set_width ($self->{children}[0]{w}); 2238 $layout->set_width ($self->{children}[0]{w} - $indent);
2123 $layout->set_markup ($text); 2239 $layout->set_markup ($text);
2124 2240
2125 ($layout->size)[1] 2241 ($layout->size)[1]
2126} 2242}
2127 2243
2130 2246
2131 $self->{need_reflow}++; 2247 $self->{need_reflow}++;
2132 $self->update; 2248 $self->update;
2133} 2249}
2134 2250
2135sub size_allocate {
2136 my ($self, $w, $h) = @_;
2137
2138 $self->SUPER::size_allocate ($w, $h);
2139
2140 $self->{layout}->set_font ($self->{font}) if $self->{font};
2141 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2142 $self->{layout}->set_width ($self->{children}[0]{w});
2143
2144 $self->reflow;
2145}
2146
2147sub add_paragraph { 2251sub add_paragraph {
2148 my ($self, $color, $text) = @_; 2252 my ($self, $color, $text, $indent) = @_;
2149 2253
2150 #TODO: intelligently "reformat" paragraph 2254 #TODO: intelligently "reformat" paragraph
2151 2255
2256 for my $line (split /\n/, $text) {
2152 my $height = $self->text_height ($text); 2257 my $height = $self->text_height ($line);
2153
2154 $self->{height} += $height; 2258 $self->{height} += $height;
2155
2156 push @{$self->{par}}, [$height, $color, $text]; 2259 push @{$self->{par}}, [$height, $color, $indent, $line];
2260 }
2157 2261
2158 $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}];
2159 $self->{children}[1]->update; 2263 $self->{children}[1]->update;
2160} 2264}
2161 2265
2170 2274
2171 $ROOT->on_post_alloc ($self, sub { 2275 $ROOT->on_post_alloc ($self, sub {
2172 if (delete $self->{need_reflow}) { 2276 if (delete $self->{need_reflow}) {
2173 my $height = 0; 2277 my $height = 0;
2174 2278
2175 $height += $_->[0] = $self->text_height ($_->[2]) 2279 $height += $_->[0] = $self->text_height ($_->[3], $_->[2])
2176 for @{$self->{par}}; 2280 for @{$self->{par}};
2177 2281
2178 $self->{height} = $height; 2282 $self->{height} = $height;
2179 2283
2180 $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;
2181 2286
2182 delete $self->{texture}; 2287 delete $self->{texture};
2183 } 2288 }
2184 2289
2185 $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 {
2186 glClearColor 0, 0, 0, 0; 2291 glClearColor 0.5, 0.5, 0.5, 0;
2187 glClear GL_COLOR_BUFFER_BIT; 2292 glClear GL_COLOR_BUFFER_BIT;
2188
2189 glEnable GL_TEXTURE_2D;
2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2191 2293
2192 my $top = int $self->{children}[1]{range}[0]; 2294 my $top = int $self->{children}[1]{range}[0];
2193 2295
2194 my $y0 = $top; 2296 my $y0 = $top;
2195 my $y1 = $top + $self->{h}; 2297 my $y1 = $top + $self->{h};
2198 2300
2199 my $layout = $self->{layout}; 2301 my $layout = $self->{layout};
2200 2302
2201 $layout->set_font ($self->{font}) if $self->{font}; 2303 $layout->set_font ($self->{font}) if $self->{font};
2202 2304
2305 glEnable GL_BLEND;
2306 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2307
2203 for my $par (@{$self->{par}}) { 2308 for my $par (@{$self->{par}}) {
2204 my $h = $par->[0]; 2309 my $h = $par->[0];
2205 2310
2206 if ($y0 < $y + $h && $y < $y1) { 2311 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] }); 2312 $layout->set_foreground (@{ $par->[1] });
2313 $layout->set_width ($self->{w} - $par->[2]);
2208 $layout->set_markup ($par->[2]); 2314 $layout->set_markup ($par->[3]);
2209 2315
2210 my ($W, $H) = $layout->size; 2316 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2211 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;
2212 } 2320 }
2213 2321
2214 $y += $h; 2322 $y += $h;
2215 } 2323 }
2216 2324
2217 glDisable GL_TEXTURE_2D; 2325 glDisable GL_BLEND;
2218 }; 2326 };
2219 }); 2327 });
2220} 2328}
2221 2329
2222sub _draw { 2330sub _draw {
2223 my ($self) = @_; 2331 my ($self) = @_;
2224 2332
2225 glEnable GL_TEXTURE_2D; 2333 glEnable GL_TEXTURE_2D;
2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2227 glColor 1, 1, 1, 1; 2335 glColor 1, 1, 1, 1;
2228 $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});
2229 glDisable GL_TEXTURE_2D; 2337 glDisable GL_TEXTURE_2D;
2230 2338
2231 $self->{children}[1]->draw; 2339 $self->{children}[1]->draw;
2232 2340
2233} 2341}
2332 2440
2333sub set_tooltip_from { 2441sub set_tooltip_from {
2334 my ($self, $widget) = @_; 2442 my ($self, $widget) = @_;
2335 2443
2336 $self->add (new CFClient::UI::Label 2444 $self->add (new CFClient::UI::Label
2337 markup => $widget->{tooltip}, 2445 markup => $widget->{tooltip},
2338 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2446 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2339 fontsize => 0.8, 2447 fontsize => 0.8,
2340 fg => [0, 0, 0, 1], 2448 fg => [0, 0, 0, 1],
2449 ellipsise => 0,
2341 font => ($widget->{tooltip_font} || $::FONT_PROP), 2450 font => ($widget->{tooltip_font} || $::FONT_PROP),
2342 ); 2451 );
2343} 2452}
2344 2453
2345sub size_request { 2454sub size_request {
2346 my ($self) = @_; 2455 my ($self) = @_;
2392use CFClient::OpenGL; 2501use CFClient::OpenGL;
2393 2502
2394sub new { 2503sub new {
2395 my $class = shift; 2504 my $class = shift;
2396 2505
2397 $class->SUPER::new ( 2506 my $self = $class->SUPER::new (
2398 aspect => 1, 2507 aspect => 1,
2399 @_, 2508 @_,
2400 ) 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
2401} 2526}
2402 2527
2403sub size_request { 2528sub size_request {
2404 (32, 8) 2529 (32, 8)
2405} 2530}
2406 2531
2407sub _draw { 2532sub _draw {
2408 my ($self) = @_; 2533 my ($self) = @_;
2409 2534
2410 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
2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2546 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2412 2547
2413 # TODO animation
2414 if ($tex) { 2548 if ($tex) {
2415 glEnable GL_TEXTURE_2D; 2549 glEnable GL_TEXTURE_2D;
2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2550 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2417 glColor 1, 1, 1, 1; 2551 glColor 1, 1, 1, 1;
2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2552 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2419 glDisable GL_TEXTURE_2D; 2553 glDisable GL_TEXTURE_2D;
2420 } 2554 }
2421} 2555}
2422 2556
2557sub DESTROY {
2558 my ($self) = @_;
2559
2560 $self->{timer}->cancel
2561 if $self->{timer};
2562
2563 $self->SUPER::DESTROY;
2564}
2565
2423############################################################################# 2566#############################################################################
2424 2567
2425package CFClient::UI::InventoryItem; 2568package CFClient::UI::InventoryItem;
2426 2569
2427our @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}
2428 2597
2429sub new { 2598sub new {
2430 my $class = shift; 2599 my $class = shift;
2431 2600
2432 my %args = @_; 2601 my %args = @_;
2433 2602
2434 my $item = delete $args{item}; 2603 my $item = delete $args{item};
2435 2604
2436 my $desc = $item->{nrof} < 2 2605 my $desc = _item_to_desc ($item);
2437 ? $item->{name}
2438 : "$item->{nrof} $item->{name_pl}";
2439
2440 2606
2441 my $self = $class->SUPER::new ( 2607 my $self = $class->SUPER::new (
2442 can_hover => 1, 2608 can_hover => 1,
2443 can_events => 1, 2609 can_events => 1,
2444 tooltip => (CFClient::UI::Label->escape ($desc) 2610 tooltip => ((CFClient::UI::Label::escape $desc)
2445 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2611 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2446 connect_button_down => sub { 2612 connect_button_down => sub {
2447 my ($self, $ev, $x, $y) = @_; 2613 my ($self, $ev, $x, $y) = @_;
2448 2614
2449 # 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 :(
2450 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
2451 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2623 $::CONN->send ("move $targ $item->{tag} 0");
2452 } elsif ($ev->{button} == 2) { 2624 } elsif ($ev->{button} == 2) {
2453 $::CONN->send ("apply $item->{tag}"); 2625 $::CONN->send ("apply $item->{tag}");
2454 } elsif ($ev->{button} == 3) { 2626 } elsif ($ev->{button} == 3) {
2455 CFClient::UI::Menu->new ( 2627 my @menu_items = (
2456 items => [
2457 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2628 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2458 [
2459 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2460 sub { $::CONN->send ("lock $item->{tag}") },
2461 ],
2462 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2629 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2463 ["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}) }],
2464 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], 2638 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2639 )
2465 ], 2640 ),
2466 )->popup ($ev); 2641 );
2642
2643 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2467 } 2644 }
2468 2645
2469 1 2646 1
2470 }, 2647 },
2471 %args 2648 %args
2472 ); 2649 );
2650
2473 2651
2474 $self->add (new CFClient::UI::Face 2652 $self->add (new CFClient::UI::Face
2475 can_events => 0, 2653 can_events => 0,
2476 face => $item->{face}, 2654 face => $item->{face},
2477 anim => $item->{anim}, 2655 anim => $item->{anim},
2478 animspeed => $item->{animspeed}, 2656 animspeed => $item->{animspeed},
2479 ); 2657 );
2480 2658
2481 $self->add (new CFClient::UI::Label 2659 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2482 can_events => 0, 2660
2483 text => $desc, 2661 $self->{item} = $item;
2484 ); 2662
2663 $self->update_item;
2485 2664
2486 $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);
2487} 2674}
2488 2675
2489############################################################################# 2676#############################################################################
2490 2677
2491package CFClient::UI::Inventory; 2678package CFClient::UI::Inventory;
2507 my ($self, $items) = @_; 2694 my ($self, $items) = @_;
2508 2695
2509 $self->{scrolled}->clear; 2696 $self->{scrolled}->clear;
2510 return unless $items; 2697 return unless $items;
2511 2698
2512 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;
2513 2703
2514 $self->{real_items} = \@items; 2704 $self->{real_items} = \@items;
2515 2705
2516 for my $item (@items) { 2706 for my $item (@items) {
2517 my $desc = $item->{nrof} < 2
2518 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}";
2520
2521 $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 ();
2522 } 2709 }
2710
2711 $self->{scrolled}->add (@items);
2523 2712
2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2713# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2525} 2714}
2526 2715
2527sub size_request { 2716sub size_request {
2605 2794
2606package CFClient::UI::Statusbox; 2795package CFClient::UI::Statusbox;
2607 2796
2608our @ISA = CFClient::UI::VBox::; 2797our @ISA = CFClient::UI::VBox::;
2609 2798
2799sub new {
2800 my $class = shift;
2801
2802 $class->SUPER::new (
2803 fontsize => 0.8,
2804 @_,
2805 )
2806}
2807
2610sub reorder { 2808sub reorder {
2611 my ($self) = @_; 2809 my ($self) = @_;
2612 my $NOW = time; 2810 my $NOW = time;
2613 2811
2614 while (my ($k, $v) = each %{ $self->{item} }) { 2812 while (my ($k, $v) = each %{ $self->{item} }) {
2632 ? "<b>$item->{count} ×</b> $item->{text}" 2830 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text}; 2831 : $item->{text};
2634 2832
2635 for ($short) { 2833 for ($short) {
2636 s/^\s+//; 2834 s/^\s+//;
2637 s/\012.*/…/s; 2835 s/\s+/ /g;
2638 my $len = int 40 / $item->{fontsize};
2639 substr $_, $len, length, "…" if $len < length;
2640 } 2836 }
2641 2837
2642 new CFClient::UI::Label 2838 new CFClient::UI::Label
2643 markup => $short, 2839 markup => $short,
2644 tooltip => $item->{tooltip}, 2840 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP, 2841 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67, 2842 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize}, 2843 fontsize => $item->{fontsize} || $self->{fontsize},
2844 max_w => $::WIDTH * 0.44,
2648 color => $item->{color}, 2845 fg => $item->{fg},
2649 can_events => 1, 2846 can_events => 1,
2650 can_hover => 1 2847 can_hover => 1
2651 }; 2848 };
2652 } 2849 }
2653 2850
2679 $self->{item}{$group} = { 2876 $self->{item}{$group} = {
2680 id => ++$self->{id}, 2877 id => ++$self->{id},
2681 text => $text, 2878 text => $text,
2682 timeout => $timeout, 2879 timeout => $timeout,
2683 tooltip => $text, 2880 tooltip => $text,
2684 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8], 2881 fg => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0, 2882 pri => 0,
2687 count => 1, 2883 count => 1,
2688 %arg, 2884 %arg,
2689 }; 2885 };
2690 } 2886 }
2691 2887
2692 $self->reorder; 2888 $self->reorder;
2693} 2889}
2694 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
2695############################################################################# 2901#############################################################################
2696 2902
2697package CFClient::UI::Root; 2903package CFClient::UI::Root;
2698 2904
2699our @ISA = CFClient::UI::Container::; 2905our @ISA = CFClient::UI::Container::;
2729} 2935}
2730 2936
2731sub size_allocate { 2937sub size_allocate {
2732 my ($self, $w, $h) = @_; 2938 my ($self, $w, $h) = @_;
2733 2939
2734 my $old_w = $self->{old_w}; 2940 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2735 my $old_h = $self->{old_h}; 2941 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2736 2942
2737 if ($old_w && $old_h) { 2943 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2738 for my $child ($self->children) { 2944 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2739 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2740 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2741 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2742 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2743 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2744 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2745 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2746 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2747 }
2748 }
2749 2945
2750 for my $child ($self->children) { 2946 for my $child ($self->children) {
2751 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)};
2752 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
2753 $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;
2754 $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
2755 $child->configure ($X, $Y, $W, $H); 2958 $child->configure ($X, $Y, $W, $H);
2756 } 2959 }
2757
2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760} 2960}
2761 2961
2762sub coord2local { 2962sub coord2local {
2763 my ($self, $x, $y) = @_; 2963 my ($self, $x, $y) = @_;
2764 2964
2777 $self->check_size; 2977 $self->check_size;
2778 $::WANT_REFRESH++; 2978 $::WANT_REFRESH++;
2779} 2979}
2780 2980
2781sub add { 2981sub add {
2782 my ($self, $child) = @_; 2982 my ($self, @children) = @_;
2783 2983
2984 for my $child (@children) {
2985 $child->{toplevel} = 1;
2986
2784 # integerise window positions 2987 # integerise window positions
2785 $child->{x} = int $child->{x}; 2988 $child->{x} = int $child->{x};
2786 $child->{y} = int $child->{y}; 2989 $child->{y} = int $child->{y};
2990 }
2787 2991
2788 $self->SUPER::add ($child); 2992 $self->SUPER::add (@children);
2789} 2993}
2790 2994
2791sub on_refresh { 2995sub on_refresh {
2792 my ($self, $id, $cb) = @_; 2996 my ($self, $id, $cb) = @_;
2793 2997
2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3066 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2863 glClear GL_COLOR_BUFFER_BIT; 3067 glClear GL_COLOR_BUFFER_BIT;
2864 3068
2865 glMatrixMode GL_PROJECTION; 3069 glMatrixMode GL_PROJECTION;
2866 glLoadIdentity; 3070 glLoadIdentity;
2867 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3071 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2868 glMatrixMode GL_MODELVIEW; 3072 glMatrixMode GL_MODELVIEW;
2869 glLoadIdentity; 3073 glLoadIdentity;
2870 3074
2871 $self->_draw; 3075 $self->_draw;
2872} 3076}
2874############################################################################# 3078#############################################################################
2875 3079
2876package CFClient::UI; 3080package CFClient::UI;
2877 3081
2878$ROOT = new CFClient::UI::Root; 3082$ROOT = new CFClient::UI::Root;
2879$TOOLTIP = new CFClient::UI::Tooltip; 3083$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2880 3084
28811 30851
2882 3086

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines