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.182 by root, Sun Apr 30 11:17:08 2006 UTC vs.
Revision 1.201 by root, Sat May 13 23:17:49 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
23 24
24 my $tip = $widget->{tooltip}; 25 my $tip = $widget->{tooltip};
25 26
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 27 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 28
28 $TOOLTIP->set_markup ($widget->{tooltip}); 29 $TOOLTIP->set_tooltip_from ($widget);
29
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31 31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) { 34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 } 36 }
37 37
38 $TOOLTIP->move ($x, $y); 38 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size;
40 $TOOLTIP->update;
39 } 41 }
40 42
41 return; 43 return;
42 } 44 }
43 } 45 }
232 $self->{y} = $y; 234 $self->{y} = $y;
233 $self->update; 235 $self->update;
234 } 236 }
235 237
236 if ($self->{w} != $w || $self->{h} != $h) { 238 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 239 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 $self->emit (size_allocate => $w, $h);
243 } 240 }
244} 241}
245 242
246sub size_allocate { 243sub size_allocate {
247 # nothing to be done 244 # nothing to be done
255 my ($self) = @_; 252 my ($self) = @_;
256 253
257 $_->reconfigure 254 $_->reconfigure
258 for $self->children; 255 for $self->children;
259 256
260 $_->check_size; 257 $self->check_size (1);
258 $self->update;
261} 259}
262 260
263sub set_max_size { 261sub set_max_size {
264 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
265 263
266 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
267 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 delete $self->{max_h}; $self->{max_h} = $h if $h;
268}
269
270# return top left coordinates
271sub _topleft {
272 my ($self, $x, $y) = @_;
273
274 $self->{parent}
275 or Carp::confess "no parent widget in _topleft\n";#d#
276
277 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
278} 266}
279 267
280# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
281sub coord2local { 269sub coord2local {
282 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
283 271
284 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
285 ($x - $X, $y - $Y)
286} 273}
287 274
288# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
289sub coord2global { 276sub coord2global {
290 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
291 278
292 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
293 ($x + $X, $y + $Y)
294} 280}
295 281
296sub focus_in { 282sub focus_in {
297 my ($self) = @_; 283 my ($self) = @_;
298 284
396 382
397sub set_parent { 383sub set_parent {
398 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
399 385
400 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size
390 unless exists $self->{req_w};
401} 391}
402 392
403sub check_size { 393sub check_size {
404 my ($self) = @_; 394 my ($self, $forced) = @_;
405 395
406 $self->{parent} 396 $self->{force_alloc} = 1 if $forced;
407 or return 1; 397 $CFClient::UI::ROOT->{check_size}{$self} = $self;
408
409 my ($w, $h) = $self->{user_w} && $self->{user_h}
410 ? @$self{qw(user_w user_h)}
411 : $self->size_request;
412
413 if ($w != $self->{req_w} || $h != $self->{req_h}) {
414 $self->{req_w} = $w;
415 $self->{req_h} = $h;
416
417 $self->{parent}->check_size
418 or $self->size_allocate (
419 (List::Util::max $self->{w}, $w),
420 (List::Util::max $self->{h}, $h),
421 );
422
423 1
424 } else {
425 0
426 }
427} 398}
428 399
429sub update { 400sub update {
430 my ($self) = @_; 401 my ($self) = @_;
431 402
528 499
529 $self 500 $self
530} 501}
531 502
532sub add { 503sub add {
533 my ($self, $child) = @_; 504 my ($self, @widgets) = @_;
534 505
535 $child->set_parent ($self); 506 $_->set_parent ($self)
507 for @widgets;
536 508
537 use sort 'stable'; 509 use sort 'stable';
538 510
539 $self->{children} = [ 511 $self->{children} = [
540 sort { $a->{z} <=> $b->{z} } 512 sort { $a->{z} <=> $b->{z} }
541 @{$self->{children}}, $child 513 @{$self->{children}}, @widgets
542 ]; 514 ];
543 515
544 $child->check_size; 516 $self->check_size (1);
545 $self->update; 517 $self->update;
546} 518}
547 519
548sub children { 520sub children {
549 @{ $_[0]{children} } 521 @{ $_[0]{children} }
656} 628}
657 629
658sub update { 630sub update {
659 my ($self) = @_; 631 my ($self) = @_;
660 632
661 $ROOT->on_refresh ($self => sub { $self->render_child }); 633 $ROOT->on_post_alloc ($self => sub { $self->render_child });
662 $self->SUPER::update; 634 $self->SUPER::update;
663} 635}
664 636
665sub size_allocate { 637sub size_allocate {
666 my ($self, $w, $h) = @_; 638 my ($self, $w, $h) = @_;
697 my ($w, $h) = ($self->w, $self->h); 669 my ($w, $h) = ($self->w, $self->h);
698 670
699 my $tex = $self->{texture} 671 my $tex = $self->{texture}
700 or return; 672 or return;
701 673
702 glEnable GL_BLEND;
703 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
704 glEnable GL_TEXTURE_2D; 674 glEnable GL_TEXTURE_2D;
705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 675 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
706 glColor 0, 0, 0, 1; 676 glColor 0, 0, 0, 1;
707 677
708 $tex->draw_quad (0, 0, $w, $h); 678 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
709 679
710 glDisable GL_BLEND;
711 glDisable GL_TEXTURE_2D; 680 glDisable GL_TEXTURE_2D;
712} 681}
713 682
714############################################################################# 683#############################################################################
715 684
737 706
738 $self->{view_x} = int $x; 707 $self->{view_x} = int $x;
739 $self->{view_y} = int $y; 708 $self->{view_y} = int $y;
740 709
741 $self->update; 710 $self->update;
711}
712
713# hmm, this does not work for topleft of $self... but we should not ask for that
714sub coord2local {
715 my ($self, $x, $y) = @_;
716
717 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
718}
719
720sub coord2global {
721 my ($self, $x, $y) = @_;
722
723 $x = List::Util::min $self->{w}, $x - $self->{view_x};
724 $y = List::Util::min $self->{h}, $y - $self->{view_y};
725
726 $self->SUPER::coord2global ($x, $y)
727}
728
729sub find_widget {
730 my ($self, $x, $y) = @_;
731
732 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
733 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
734 ) {
735 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
736 } else {
737 $self->CFClient::UI::Base::find_widget ($x, $y)
738 }
742} 739}
743 740
744sub _render { 741sub _render {
745 my ($self) = @_; 742 my ($self) = @_;
746 743
779 $self->add ($self->{slider}); 776 $self->add ($self->{slider});
780 777
781 $self 778 $self
782} 779}
783 780
784#TODO# update range on size_allocate depeneing on child 781#TODO# update range on size_allocate depending on child
785# update viewport offset on scroll 782# update viewport offset on scroll
786 783
787############################################################################# 784#############################################################################
788 785
789package CFClient::UI::Frame; 786package CFClient::UI::Frame;
793use CFClient::OpenGL; 790use CFClient::OpenGL;
794 791
795sub new { 792sub new {
796 my $class = shift; 793 my $class = shift;
797 794
798 my $self = $class->SUPER::new ( 795 $class->SUPER::new (
799 bg => [1, 1, 1, 1], 796 bg => undef,
800 border_bg => [1, 1, 1, 1],
801 border => 0.8,
802 @_ 797 @_,
803 ); 798 )
804
805 $self
806} 799}
807 800
808sub _draw { 801sub _draw {
809 my ($self) = @_; 802 my ($self) = @_;
810 803
811 my ($w, $h) = ($self->{w}, $self->{h}); 804 if ($self->{bg}) {
805 my ($w, $h) = @$self{qw(w h)};
812 806
813 glEnable GL_BLEND; 807 glEnable GL_BLEND;
814 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
815 glEnable GL_TEXTURE_2D; 809 glColor @{ $self->{bg} };
816 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
817 810
818# glBegin GL_QUADS; 811 glBegin GL_QUADS;
819# glColor 0, 0, 0, 0;
820# glVertex 0 , 0; 812 glVertex 0 , 0;
821# glVertex 0 , $h; 813 glVertex 0 , $h;
822# glVertex $w, $h; 814 glVertex $w, $h;
823# glVertex $w, 0; 815 glVertex $w, 0;
824# glEnd; 816 glEnd;
825 817
826
827 $self->child->draw;
828 glDisable GL_BLEND; 818 glDisable GL_BLEND;
829 glDisable GL_TEXTURE_2D; 819 }
820
821 $self->SUPER::_draw;
830} 822}
831 823
832############################################################################# 824#############################################################################
833 825
834package CFClient::UI::FancyFrame; 826package CFClient::UI::FancyFrame;
950 my ($self) = @_; 942 my ($self) = @_;
951 943
952 my ($w, $h ) = ($self->{w}, $self->{h}); 944 my ($w, $h ) = ($self->{w}, $self->{h});
953 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 945 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
954 946
955 glEnable GL_BLEND;
956 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
957 glEnable GL_TEXTURE_2D; 947 glEnable GL_TEXTURE_2D;
958 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 948 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
959 949
960 my $border = $self->border; 950 my $border = $self->border;
961 951
962 glColor @{ $self->{border_bg} }; 952 glColor @{ $self->{border_bg} };
963 $tex[1]->draw_quad (0, 0, $w, $border); 953 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
964 $tex[3]->draw_quad (0, $border, $border, $ch); 954 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
965 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 955 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
966 $tex[4]->draw_quad (0, $h - $border, $w, $border); 956 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
967 957
968 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 958 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
969 my $bg = $tex[0]; 959 my $bg = $tex[0];
970 960
971 # TODO: repeat texture not scale 961 # TODO: repeat texture not scale
975 glColor @{ $self->{bg} }; 965 glColor @{ $self->{bg} };
976 966
977 $bg->{s} = $rep_x; 967 $bg->{s} = $rep_x;
978 $bg->{t} = $rep_y; 968 $bg->{t} = $rep_y;
979 $bg->{wrap_mode} = 1; 969 $bg->{wrap_mode} = 1;
980 $bg->draw_quad ($border, $border, $cw, $ch); 970 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
971 }
981 972
982 glDisable GL_TEXTURE_2D; 973 glDisable GL_TEXTURE_2D;
983 glDisable GL_BLEND;
984 }
985 974
986 $self->{title}->draw if $self->{title}; 975 $self->{title}->draw if $self->{title};
987 976
988 $self->child->draw; 977 $self->child->draw;
989} 978}
1216} 1205}
1217 1206
1218sub size_allocate { 1207sub size_allocate {
1219 my ($self, $w, $h) = @_; 1208 my ($self, $w, $h) = @_;
1220 1209
1210 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1211
1221 my $children = $self->{children}; 1212 my $children = $self->{children};
1222 1213
1223 my @h = map $_->{req_h}, @$children; 1214 my @h = map $_->{req_h}, @$children;
1224 1215
1225 my $req_h = List::Util::sum @h; 1216 my $req_h = List::Util::sum @h;
1264 my ($class, %arg) = @_; 1255 my ($class, %arg) = @_;
1265 1256
1266 my $self = $class->SUPER::new ( 1257 my $self = $class->SUPER::new (
1267 fg => [1, 1, 1], 1258 fg => [1, 1, 1],
1268 #font => default_font 1259 #font => default_font
1260 #text => initial text
1261 #markup => initial narkup
1262 layout => (new CFClient::Layout),
1269 fontsize => 1, 1263 fontsize => 1,
1270 text => "",
1271 align => -1, 1264 align => -1,
1272 valign => -1, 1265 valign => -1,
1273 padding => 2, 1266 padding => 2,
1274 layout => new CFClient::Layout,
1275 can_events => 0, 1267 can_events => 0,
1276 %arg 1268 %arg
1277 ); 1269 );
1278 1270
1279 if (exists $self->{template}) { 1271 if (exists $self->{template}) {
1280 my $layout = new CFClient::Layout; 1272 my $layout = new CFClient::Layout;
1281 $layout->set_text (delete $self->{template}); 1273 $layout->set_text (delete $self->{template});
1282 $self->{template} = $layout; 1274 $self->{template} = $layout;
1283 } 1275 }
1284 1276
1285 $self->set_text (delete $self->{text}) if exists $self->{text}; 1277 if (exists $self->{markup}) {
1286 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1278 $self->set_markup (delete $self->{markup});
1279 } else {
1280 $self->set_text (delete $self->{text});
1281 }
1287 1282
1288 $self 1283 $self
1289} 1284}
1290 1285
1291sub escape { 1286sub escape {
1303 1298
1304 delete $self->{texture}; 1299 delete $self->{texture};
1305 $self->SUPER::update; 1300 $self->SUPER::update;
1306} 1301}
1307 1302
1308sub reconfigure {
1309 my ($self) = @_;
1310
1311 delete $self->{texture};
1312}
1313
1314sub set_text { 1303sub set_text {
1315 my ($self, $text) = @_; 1304 my ($self, $text) = @_;
1316 1305
1317 return if $self->{text} eq "T$text"; 1306 return if $self->{text} eq "T$text";
1318 $self->{text} = "T$text"; 1307 $self->{text} = "T$text";
1319 1308
1309 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1320 $self->{layout}->set_text ($text); 1310 $self->{layout}->set_text ($text);
1321 1311
1322 delete $self->{texture};
1323 $self->update; 1312 $self->update;
1324 $self->check_size; 1313 $self->check_size;
1325} 1314}
1326 1315
1327sub set_markup { 1316sub set_markup {
1328 my ($self, $markup) = @_; 1317 my ($self, $markup) = @_;
1329 1318
1330 return if $self->{text} eq "M$markup"; 1319 return if $self->{text} eq "M$markup";
1331 $self->{text} = "M$markup"; 1320 $self->{text} = "M$markup";
1332 1321
1322 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1323
1324 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1333 $self->{layout}->set_markup ($markup); 1325 $self->{layout}->set_markup ($markup);
1334 1326
1335 delete $self->{texture};
1336 $self->update; 1327 $self->update;
1337 $self->check_size; 1328 $self->check_size;
1338} 1329}
1339 1330
1340sub size_request { 1331sub size_request {
1371sub set_fontsize { 1362sub set_fontsize {
1372 my ($self, $fontsize) = @_; 1363 my ($self, $fontsize) = @_;
1373 1364
1374 $self->{fontsize} = $fontsize; 1365 $self->{fontsize} = $fontsize;
1375 delete $self->{texture}; 1366 delete $self->{texture};
1367
1368 $self->update;
1376 $self->check_size; 1369 $self->check_size;
1377 $self->update;
1378} 1370}
1379 1371
1380sub _draw { 1372sub _draw {
1381 my ($self) = @_; 1373 my ($self) = @_;
1382 1374
1383 my $tex = $self->{texture} ||= do { 1375 my $tex = $self->{texture} ||= do {
1376 $self->{layout}->set_foreground (@{$self->{fg}});
1384 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1377 $self->{layout}->set_font ($self->{font}) if $self->{font};
1385 $self->{layout}->set_width ($self->{w}); 1378 $self->{layout}->set_width ($self->{w});
1386 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1379 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1380
1387 new_from_layout CFClient::Texture $self->{layout} 1381 my $tex = new_from_layout CFClient::Texture $self->{layout};
1382
1383 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1384 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1385 : ($self->{w} - $tex->{w}) * 0.5;
1386
1387 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1388 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1389 : ($self->{h} - $tex->{h}) * 0.5;
1390
1391 $tex
1388 }; 1392 };
1389 1393
1390 glEnable GL_BLEND;
1391 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1392 glEnable GL_TEXTURE_2D; 1394 glEnable GL_TEXTURE_2D;
1393 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1395 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1394 1396
1397 if ($tex->{format} == GL_ALPHA) {
1395 glColor @{$self->{fg}}; 1398 glColor @{$self->{fg}};
1396
1397 $self->{ox} = int (
1398 $self->{align} < 0 ? $self->{padding}
1399 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1400 : ($self->{w} - $tex->{w}) * 0.5
1401 );
1402
1403 $self->{oy} = int (
1404 $self->{valign} < 0 ? $self->{padding}
1405 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1406 : ($self->{h} - $tex->{h}) * 0.5
1407 );
1408
1409 $tex->draw_quad ($self->{ox}, $self->{oy}); 1399 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1400 } else {
1401 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1402 }
1410 1403
1411 glDisable GL_TEXTURE_2D; 1404 glDisable GL_TEXTURE_2D;
1412 glDisable GL_BLEND;
1413} 1405}
1414 1406
1415############################################################################# 1407#############################################################################
1416 1408
1417package CFClient::UI::EntryBase; 1409package CFClient::UI::EntryBase;
1452 $self->{layout}->set_text ("$text "); 1444 $self->{layout}->set_text ("$text ");
1453 1445
1454 $self->emit (changed => $self->{text}); 1446 $self->emit (changed => $self->{text});
1455} 1447}
1456 1448
1449sub set_text {
1450 my ($self, $text) = @_;
1451
1452 $self->{cursor} = length $text;
1453 $self->_set_text ($text);
1454 $self->check_size;
1455 $self->update;
1456}
1457
1457sub get_text { 1458sub get_text {
1458 $_[0]{text} 1459 $_[0]{text}
1459} 1460}
1460 1461
1461sub size_request { 1462sub size_request {
1468 1469
1469sub size_allocate { 1470sub size_allocate {
1470 my ($self, $w, $h) = @_; 1471 my ($self, $w, $h) = @_;
1471 1472
1472 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1473 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1473}
1474
1475sub set_text {
1476 my ($self, $text) = @_;
1477
1478 $self->{cursor} = length $text;
1479 $self->_set_text ($text);
1480 $self->update;
1481} 1474}
1482 1475
1483sub key_down { 1476sub key_down {
1484 my ($self, $ev) = @_; 1477 my ($self, $ev) = @_;
1485 1478
1487 my $sym = $ev->{sym}; 1480 my $sym = $ev->{sym};
1488 my $uni = $ev->{unicode}; 1481 my $uni = $ev->{unicode};
1489 1482
1490 my $text = $self->get_text; 1483 my $text = $self->get_text;
1491 1484
1492 if ($sym == 8) { 1485 if ($uni == 8) {
1493 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1486 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1494 } elsif ($sym == 127) { 1487 } elsif ($uni == 127) {
1495 substr $text, $self->{cursor}, 1, ""; 1488 substr $text, $self->{cursor}, 1, "";
1496 } elsif ($sym == CFClient::SDLK_LEFT) { 1489 } elsif ($sym == CFClient::SDLK_LEFT) {
1497 --$self->{cursor} if $self->{cursor}; 1490 --$self->{cursor} if $self->{cursor};
1498 } elsif ($sym == CFClient::SDLK_RIGHT) { 1491 } elsif ($sym == CFClient::SDLK_RIGHT) {
1499 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1492 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1500 } elsif ($sym == CFClient::SDLK_HOME) { 1493 } elsif ($sym == CFClient::SDLK_HOME) {
1501 $self->{cursor} = 0; 1494 $self->{cursor} = 0;
1502 } elsif ($sym == CFClient::SDLK_END) { 1495 } elsif ($sym == CFClient::SDLK_END) {
1503 $self->{cursor} = length $text; 1496 $self->{cursor} = length $text;
1504 } elsif ($sym == 27) { 1497 } elsif ($uni == 27) {
1505 $self->emit ('escape'); 1498 $self->emit ('escape');
1506 } elsif ($uni) { 1499 } elsif ($uni) {
1507 substr $text, $self->{cursor}++, 0, chr $uni; 1500 substr $text, $self->{cursor}++, 0, chr $uni;
1508 } 1501 }
1509 1502
1673 1666
1674 if ($GRAB == $self) { 1667 if ($GRAB == $self) {
1675 $self->{fg} = $self->{active_fg}; 1668 $self->{fg} = $self->{active_fg};
1676 } 1669 }
1677 1670
1678 glEnable GL_BLEND;
1679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1680 glEnable GL_TEXTURE_2D; 1671 glEnable GL_TEXTURE_2D;
1681 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1682 glColor 0, 0, 0, 1; 1673 glColor 0, 0, 0, 1;
1683 1674
1684 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1675 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1685 1676
1686 glDisable GL_TEXTURE_2D; 1677 glDisable GL_TEXTURE_2D;
1687 glDisable GL_BLEND;
1688 1678
1689 $self->SUPER::_draw; 1679 $self->SUPER::_draw;
1690} 1680}
1691 1681
1692############################################################################# 1682#############################################################################
1739 1729
1740 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1730 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1741 1731
1742 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1743 1733
1744 glEnable GL_BLEND; 1734 my $tex = $self->{state} ? $tex[1] : $tex[0];
1735
1745 glEnable GL_TEXTURE_2D; 1736 glEnable GL_TEXTURE_2D;
1746 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1747
1748 my $tex = $self->{state} ? $tex[1] : $tex[0];
1749
1750 $tex->draw_quad (0, 0, $s, $s); 1737 $tex->draw_quad_alpha (0, 0, $s, $s);
1751
1752 glDisable GL_TEXTURE_2D; 1738 glDisable GL_TEXTURE_2D;
1753 glDisable GL_BLEND;
1754} 1739}
1755 1740
1756############################################################################# 1741#############################################################################
1757 1742
1758package CFClient::UI::Image; 1743package CFClient::UI::Image;
1801 glTranslate 0, -$self->{w}, 0; 1786 glTranslate 0, -$self->{w}, 0;
1802 1787
1803 ($w, $h) = ($h, $w); 1788 ($w, $h) = ($h, $w);
1804 } 1789 }
1805 1790
1806 glEnable GL_BLEND;
1807 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1808 glEnable GL_TEXTURE_2D; 1791 glEnable GL_TEXTURE_2D;
1809 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1792 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1810 1793
1811 $tex->draw_quad (0, 0, $w, $h); 1794 $tex->draw_quad_alpha (0, 0, $w, $h);
1812 1795
1813 glDisable GL_BLEND;
1814 glDisable GL_TEXTURE_2D; 1796 glDisable GL_TEXTURE_2D;
1815} 1797}
1816 1798
1817############################################################################# 1799#############################################################################
1818 1800
2110 $page ||= 2; 2092 $page ||= 2;
2111 2093
2112 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2094 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2113 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2095 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2114 2096
2115 glEnable GL_BLEND;
2116 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2117 glEnable GL_TEXTURE_2D; 2097 glEnable GL_TEXTURE_2D;
2118 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2098 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2119 2099
2120 # draw background 2100 # draw background
2121 $tex[1]->draw_quad (0, 0, $w, $h); 2101 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2122 2102
2123 # draw handle 2103 # draw handle
2124 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2104 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2125 2105
2126 glDisable GL_BLEND;
2127 glDisable GL_TEXTURE_2D; 2106 glDisable GL_TEXTURE_2D;
2128} 2107}
2129 2108
2130############################################################################# 2109#############################################################################
2131 2110
2142 fontsize => 1, 2121 fontsize => 1,
2143 can_events => 0, 2122 can_events => 0,
2144 #font => default_font 2123 #font => default_font
2145 @_, 2124 @_,
2146 2125
2147 layout => (new CFClient::Layout), 2126 layout => (new CFClient::Layout 1),
2148 par => [], 2127 par => [],
2149 height => 0, 2128 height => 0,
2150 children => [ 2129 children => [
2151 (new CFClient::UI::Empty expand => 1), 2130 (new CFClient::UI::Empty expand => 1),
2152 (new CFClient::UI::Slider vertical => 1), 2131 (new CFClient::UI::Slider vertical => 1),
2170 2149
2171 my $layout = $self->{layout}; 2150 my $layout = $self->{layout};
2172 2151
2173 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2152 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2174 $layout->set_width ($self->{children}[0]{w}); 2153 $layout->set_width ($self->{children}[0]{w});
2175 $layout->set_text ($text); 2154 $layout->set_markup ($text);
2176 2155
2177 ($layout->size)[1] 2156 ($layout->size)[1]
2178} 2157}
2179 2158
2180sub reflow { 2159sub reflow {
2218 2197
2219 return unless $self->{h} > 0; 2198 return unless $self->{h} > 0;
2220 2199
2221 delete $self->{texture}; 2200 delete $self->{texture};
2222 2201
2223 $ROOT->on_refresh ($self, sub { 2202 $ROOT->on_post_alloc ($self, sub {
2224 if (delete $self->{need_reflow}) { 2203 if (delete $self->{need_reflow}) {
2225 my $height = 0; 2204 my $height = 0;
2226 2205
2227 $height += $_->[0] = $self->text_height ($_->[2]) 2206 $height += $_->[0] = $self->text_height ($_->[2])
2228 for @{$self->{par}}; 2207 for @{$self->{par}};
2236 2215
2237 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2216 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2238 glClearColor 0, 0, 0, 0; 2217 glClearColor 0, 0, 0, 0;
2239 glClear GL_COLOR_BUFFER_BIT; 2218 glClear GL_COLOR_BUFFER_BIT;
2240 2219
2241 glEnable GL_BLEND;
2242 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2243 glEnable GL_TEXTURE_2D; 2220 glEnable GL_TEXTURE_2D;
2244 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2221 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2245 2222
2246 my $top = int $self->{children}[1]{range}[0]; 2223 my $top = int $self->{children}[1]{range}[0];
2247 2224
2256 2233
2257 for my $par (@{$self->{par}}) { 2234 for my $par (@{$self->{par}}) {
2258 my $h = $par->[0]; 2235 my $h = $par->[0];
2259 2236
2260 if ($y0 < $y + $h && $y < $y1) { 2237 if ($y0 < $y + $h && $y < $y1) {
2238 $layout->set_foreground (@{ $par->[1] });
2261 $layout->set_text ($par->[2]); 2239 $layout->set_markup ($par->[2]);
2262 2240
2263 glColor @{ $par->[1] };
2264 my ($W, $H) = $layout->size; 2241 my ($W, $H) = $layout->size;
2265 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2242 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2266 } 2243 }
2267 2244
2268 $y += $h; 2245 $y += $h;
2269 } 2246 }
2270 2247
2271 glDisable GL_TEXTURE_2D; 2248 glDisable GL_TEXTURE_2D;
2272 glDisable GL_BLEND;
2273 }; 2249 };
2274 }); 2250 });
2275} 2251}
2276 2252
2277sub _draw { 2253sub _draw {
2278 my ($self) = @_; 2254 my ($self) = @_;
2279 2255
2280 glEnable GL_BLEND;
2281 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2282 glEnable GL_TEXTURE_2D; 2256 glEnable GL_TEXTURE_2D;
2283 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2284 glColor 1, 1, 1, 1; 2258 glColor 1, 1, 1, 1;
2285 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2259 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2286 glDisable GL_TEXTURE_2D; 2260 glDisable GL_TEXTURE_2D;
2287 glDisable GL_BLEND;
2288 2261
2289 $self->{children}[1]->draw; 2262 $self->{children}[1]->draw;
2290 2263
2291} 2264}
2292 2265
2386 @_, 2359 @_,
2387 can_events => 0, 2360 can_events => 0,
2388 ) 2361 )
2389} 2362}
2390 2363
2391sub set_markup { 2364sub set_tooltip_from {
2392 my ($self, $text) = @_; 2365 my ($self, $widget) = @_;
2393 2366
2394 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2367 $self->add (new CFClient::UI::Label
2395 $self->{label}->set_markup ($text); 2368 markup => $widget->{tooltip},
2396 $self->add ($self->{label}); 2369 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2370 fontsize => 0.8,
2371 fg => [0, 0, 0, 1],
2372 font => ($widget->{tooltip_font} || $::FONT_PROP),
2373 );
2397} 2374}
2398 2375
2399sub size_request { 2376sub size_request {
2400 my ($self) = @_; 2377 my ($self) = @_;
2401
2402 $self->child->set_max_size ($::WIDTH * 0.3);
2403 2378
2404 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2379 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2405 2380
2406 ($w + 4, $h + 4) 2381 ($w + 4, $h + 4)
2407} 2382}
2413} 2388}
2414 2389
2415sub _draw { 2390sub _draw {
2416 my ($self) = @_; 2391 my ($self) = @_;
2417 2392
2418 glPushMatrix;
2419 glTranslate 0.375, 0.375; 2393 glTranslate 0.375, 0.375;
2420 2394
2421 my ($w, $h) = @$self{qw(w h)}; 2395 my ($w, $h) = @$self{qw(w h)};
2422 2396
2423 glColor 1, 0.8, 0.4; 2397 glColor 1, 0.8, 0.4;
2434 glVertex 0 , $h; 2408 glVertex 0 , $h;
2435 glVertex $w, $h; 2409 glVertex $w, $h;
2436 glVertex $w, 0; 2410 glVertex $w, 0;
2437 glEnd; 2411 glEnd;
2438 2412
2439 glPopMatrix; 2413 glTranslate 2 - 0.375, 2 - 0.375;
2440
2441 glTranslate 2, 2;
2442 $self->SUPER::_draw; 2414 $self->SUPER::_draw;
2443} 2415}
2444 2416
2445############################################################################# 2417#############################################################################
2446 2418
2464} 2436}
2465 2437
2466sub _draw { 2438sub _draw {
2467 my ($self) = @_; 2439 my ($self) = @_;
2468 2440
2441 return unless $::CONN;#d# manage and cache textures differently
2469 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2442 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2470 2443
2471 # TODO animation 2444 # TODO animation
2472 if ($tex) { 2445 if ($tex) {
2473 glEnable GL_BLEND;
2474 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2475 glEnable GL_TEXTURE_2D; 2446 glEnable GL_TEXTURE_2D;
2476 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2477 glColor 1, 1, 1, 1; 2448 glColor 1, 1, 1, 1;
2478 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2449 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2479 glDisable GL_TEXTURE_2D; 2450 glDisable GL_TEXTURE_2D;
2480 glDisable GL_BLEND;
2481 } 2451 }
2452}
2453
2454#############################################################################
2455
2456package CFClient::UI::InventoryItem;
2457
2458our @ISA = CFClient::UI::HBox::;
2459
2460sub new {
2461 my $class = shift;
2462
2463 my %args = @_;
2464
2465 my $item = delete $args{item};
2466
2467 my $desc = $item->{nrof} < 2
2468 ? $item->{name}
2469 : "$item->{nrof} $item->{name_pl}";
2470
2471
2472 my $self = $class->SUPER::new (
2473 can_hover => 1,
2474 can_events => 1,
2475 tooltip => (CFClient::UI::Label->escape ($desc)
2476 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2477 connect_button_down => sub {
2478 my ($self, $ev, $x, $y) = @_;
2479
2480 # todo: maybe put examine on 1? but should just be a tooltip :(
2481 if ($ev->{button} == 1) {
2482 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2483 } elsif ($ev->{button} == 2) {
2484 $::CONN->send ("apply $item->{tag}");
2485 } elsif ($ev->{button} == 3) {
2486 CFClient::UI::Menu->new (
2487 items => [
2488 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2489 [
2490 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2491 sub { $::CONN->send ("lock $item->{tag}") },
2492 ],
2493 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2494 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2495 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2496 ],
2497 )->popup ($ev);
2498 }
2499
2500 1
2501 },
2502 %args
2503 );
2504
2505 $self->add (new CFClient::UI::Face
2506 can_events => 0,
2507 face => $item->{face},
2508 anim => $item->{anim},
2509 animspeed => $item->{animspeed},
2510 );
2511
2512 $self->add (new CFClient::UI::Label
2513 can_events => 0,
2514 text => $desc,
2515 );
2516
2517 $self
2518}
2519
2520#############################################################################
2521
2522package CFClient::UI::Inventory;
2523
2524our @ISA = CFClient::UI::ScrolledWindow::;
2525
2526sub new {
2527 my $class = shift;
2528
2529 my $self = $class->SUPER::new (
2530 scrolled => (new CFClient::UI::VBox),
2531 @_,
2532 );
2533
2534 $self
2535}
2536
2537sub set_items {
2538 my ($self, $items) = @_;
2539
2540 $self->{scrolled}->clear;
2541 return unless $items;
2542
2543 my @items = sort {
2544 ($a->{type} <=> $b->{type})
2545 or ($a->{name} cmp $b->{name})
2546 } @$items;
2547
2548 $self->{real_items} = \@items;
2549
2550 for my $item (@items) {
2551 my $desc = $item->{nrof} < 2
2552 ? $item->{name}
2553 : "$item->{nrof} $item->{name_pl}";
2554
2555 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2556 }
2557
2558 $self->{scrolled}->add (@items);
2559
2560# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2561}
2562
2563sub size_request {
2564 my ($self) = @_;
2565 ($self->{req_w}, $self->{req_h});
2482} 2566}
2483 2567
2484############################################################################# 2568#############################################################################
2485 2569
2486package CFClient::UI::Menu; 2570package CFClient::UI::Menu;
2553 } 2637 }
2554} 2638}
2555 2639
2556############################################################################# 2640#############################################################################
2557 2641
2642package CFClient::UI::Statusbox;
2643
2644our @ISA = CFClient::UI::VBox::;
2645
2646sub reorder {
2647 my ($self) = @_;
2648 my $NOW = time;
2649
2650 while (my ($k, $v) = each %{ $self->{item} }) {
2651 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2652 }
2653
2654 my @widgets;
2655
2656 my @items = sort {
2657 $a->{pri} <=> $b->{pri}
2658 or $b->{id} <=> $a->{id}
2659 } values %{ $self->{item} };
2660
2661 my $count = 10 + 1;
2662 for my $item (@items) {
2663 last unless --$count;
2664
2665 push @widgets, $item->{label} ||= do {
2666 # TODO: doesn't handle markup well (read as: at all)
2667 my $short = $item->{count} > 1
2668 ? "<b>$item->{count} ×</b> $item->{text}"
2669 : $item->{text};
2670
2671 for ($short) {
2672 s/^\s+//;
2673 s/\012.*/…/s;
2674 my $len = int 40 / $item->{fontsize};
2675 substr $_, $len, length, "…" if $len < length;
2676 }
2677
2678 new CFClient::UI::Label
2679 markup => $short,
2680 tooltip => $item->{tooltip},
2681 tooltip_font => $::FONT_PROP,
2682 tooltip_width => 0.67,
2683 fontsize => $item->{fontsize},
2684 color => $item->{color},
2685 can_events => 1,
2686 can_hover => 1
2687 };
2688 }
2689
2690 $self->clear;
2691 $self->SUPER::add (reverse @widgets);
2692}
2693
2694sub add {
2695 my ($self, $text, %arg) = @_;
2696
2697 $text =~ s/^\s+//;
2698 $text =~ s/\s+$//;
2699
2700 my $timeout = time + ((delete $arg{timeout}) || 60);
2701
2702 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2703
2704 if (my $item = $self->{item}{$group}) {
2705 if ($item->{text} eq $text) {
2706 $item->{count}++;
2707 } else {
2708 $item->{count} = 1;
2709 $item->{text} = $item->{tooltip} = $text;
2710 }
2711 $item->{id} = ++$self->{id};
2712 $item->{timeout} = $timeout;
2713 delete $item->{label};
2714 } else {
2715 $self->{item}{$group} = {
2716 id => ++$self->{id},
2717 text => $text,
2718 timeout => $timeout,
2719 tooltip => $text,
2720 fontsize => 0.8,
2721 color => [0.8, 0.8, 0.8, 0.8],
2722 pri => 0,
2723 count => 1,
2724 %arg,
2725 };
2726 }
2727
2728 $self->reorder;
2729}
2730
2731#############################################################################
2732
2558package CFClient::UI::Root; 2733package CFClient::UI::Root;
2559 2734
2560our @ISA = CFClient::UI::Container::; 2735our @ISA = CFClient::UI::Container::;
2561 2736
2562use CFClient::OpenGL; 2737use CFClient::OpenGL;
2563 2738
2564sub check_size { 2739sub new {
2565 my ($self) = @_; 2740 my $class = shift;
2566 2741
2567 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2742 $class->SUPER::new (
2568} 2743 @_,
2569 2744 )
2570sub size_request {
2571 ($::WIDTH, $::HEIGHT)
2572} 2745}
2573 2746
2574sub configure { 2747sub configure {
2575 my ($self, $x, $y, $w, $h) = @_; 2748 my ($self, $x, $y, $w, $h) = @_;
2576 2749
2577 $self->SUPER::configure ($x, $y, $w, $h); 2750 $self->{w} = $w;
2751 $self->{h} = $h;
2752}
2578 2753
2754sub check_size {
2755 my ($self) = @_;
2756
2757 $self->size_allocate ($self->{w}, $self->{h})
2758 if $self->{w};
2759}
2760
2761sub size_request {
2762 my ($self) = @_;
2763
2764 ($self->{w}, $self->{h})
2765}
2766
2767sub size_allocate {
2768 my ($self, $w, $h) = @_;
2769
2770 my $old_w = $self->{old_w};
2771 my $old_h = $self->{old_h};
2772
2773 if ($old_w && $old_h) {
2774 for my $child ($self->children) {
2775 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2776 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2777 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2778 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2779 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2780 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2781 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2782 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2783 }
2784 }
2785
2579 for my $child (@{$self->{children}}) { 2786 for my $child ($self->children) {
2580 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2787 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2581 2788
2582 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2789 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2583 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2790 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2584 $child->configure ($X, $Y, $W,$H); 2791 $child->configure ($X, $Y, $W, $H);
2585 } 2792 }
2586}
2587 2793
2588sub _topleft { 2794 $self->{old_w} = $w;
2795 $self->{old_h} = $h;
2796}
2797
2798sub coord2local {
2589 my ($self, $x, $y) = @_; 2799 my ($self, $x, $y) = @_;
2590 2800
2591 ($x, $y) 2801 ($x, $y)
2592} 2802}
2593 2803
2804sub coord2global {
2805 my ($self, $x, $y) = @_;
2806
2807 ($x, $y)
2808}
2809
2594sub update { 2810sub update {
2595 my ($self) = @_; 2811 my ($self) = @_;
2596 2812
2597 $self->check_size; 2813 $self->check_size;
2598 ::refresh (); 2814 $::WANT_REFRESH++;
2599} 2815}
2600 2816
2601sub add { 2817sub add {
2602 my ($self, $child) = @_; 2818 my ($self, $child) = @_;
2603 2819
2604 # integerize window positions 2820 # integerise window positions
2605 $child->{x} = int $child->{x}; 2821 $child->{x} = int $child->{x};
2606 $child->{y} = int $child->{y}; 2822 $child->{y} = int $child->{y};
2607 2823
2608 $self->SUPER::add ($child); 2824 $self->SUPER::add ($child);
2609} 2825}
2612 my ($self, $id, $cb) = @_; 2828 my ($self, $id, $cb) = @_;
2613 2829
2614 $self->{refresh_hook}{$id} = $cb; 2830 $self->{refresh_hook}{$id} = $cb;
2615} 2831}
2616 2832
2833sub on_post_alloc {
2834 my ($self, $id, $cb) = @_;
2835
2836 $self->{post_alloc_hook}{$id} = $cb;
2837}
2838
2617sub draw { 2839sub draw {
2618 my ($self) = @_; 2840 my ($self) = @_;
2619 2841
2620 while (my $rcb = delete $self->{refresh_hook}) { 2842 while ($self->{refresh_hook}) {
2621 $_->() for values %$rcb; 2843 $_->()
2844 for values %{delete $self->{refresh_hook}};
2845 }
2846
2847 if ($self->{check_size}) {
2848 my @queue = ([], []);
2849
2850 for (;;) {
2851 if ($self->{check_size}) {
2852 # heuristic: check containers last
2853 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2854 for values %{delete $self->{check_size}}
2855 }
2856
2857 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2858
2859 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2860 ? @$widget{qw(user_w user_h)}
2861 : $widget->size_request;
2862
2863 if (delete $widget->{force_alloc}
2864 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2865 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2866
2867 $widget->{req_w} = $w;
2868 $widget->{req_h} = $h;
2869
2870 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2871
2872 $widget->{parent}->check_size
2873 if $widget->{parent};
2874 }
2875 }
2876 }
2877
2878 while ($self->{size_alloc}) {
2879 for (values %{delete $self->{size_alloc}}) {
2880 my ($widget, $w, $h) = @$_;
2881
2882 $w = 0 if $w < 0;
2883 $h = 0 if $h < 0;
2884
2885 $widget->{w} = $w;
2886 $widget->{h} = $h;
2887 $widget->size_allocate ($w, $h);
2888 $widget->emit (size_allocate => $w, $h);
2889 }
2890 }
2891
2892 while ($self->{post_alloc_hook}) {
2893 $_->()
2894 for values %{delete $self->{post_alloc_hook}};
2622 } 2895 }
2623 2896
2624 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2897 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2625 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2898 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2626 glClear GL_COLOR_BUFFER_BIT; 2899 glClear GL_COLOR_BUFFER_BIT;
2634 $self->_draw; 2907 $self->_draw;
2635} 2908}
2636 2909
2637############################################################################# 2910#############################################################################
2638 2911
2639package CFClient::UI::Inventory;
2640
2641our @ISA = CFClient::UI::ScrolledWindow::;
2642
2643sub new {
2644 my $class = shift;
2645
2646 my $self = $class->SUPER::new (
2647 scrolled => (new CFClient::UI::VBox),
2648 @_,
2649 );
2650
2651 $self
2652}
2653
2654sub set_items {
2655 my ($self, $items) = @_;
2656
2657 $self->{scrolled}->clear;
2658 return unless $items;
2659
2660 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2661
2662 $self->{real_items} = \@items;
2663
2664 for my $item (@items) {
2665 my $desc = $item->{nrof} < 2
2666 ? $item->{name}
2667 : "$item->{nrof} $item->{name_pl}";
2668
2669 my $hb = new CFClient::UI::HBox;
2670
2671 $hb->add (my $f = new CFClient::UI::Face
2672 can_events => 0,
2673 face => $item->{face},
2674 anim => $item->{anim},
2675 animspeed => $item->{animspeed},
2676 expand => 1,
2677 );
2678 $hb->add (new CFClient::UI::Label text => $desc, expand => 1);
2679 $self->{scrolled}->add ($hb);
2680 }
2681
2682# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2683}
2684
2685sub size_request {
2686 my ($self) = @_;
2687 (100, 200)
2688}
2689
2690#############################################################################
2691
2692package CFClient::UI; 2912package CFClient::UI;
2693 2913
2694$ROOT = new CFClient::UI::Root; 2914$ROOT = new CFClient::UI::Root;
2695$TOOLTIP = new CFClient::UI::Tooltip; 2915$TOOLTIP = new CFClient::UI::Tooltip;
2696 2916

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines