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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines