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.181 by root, Sat Apr 29 16:17:09 2006 UTC vs.
Revision 1.198 by root, Fri May 12 02:08:52 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 } 240 }
243} 241}
244 242
245sub size_allocate { 243sub size_allocate {
246 # nothing to be done 244 # nothing to be done
247} 245}
248 246
249sub children { 247sub children {
250} 248}
251 249
252# call when resoltuion changes etc. 250# call when resolution changes etc.
253sub reconfigure { 251sub reconfigure {
254 my ($self) = @_; 252 my ($self) = @_;
255 253
256 $_->reconfigure 254 $_->reconfigure
257 for $self->children; 255 for $self->children;
258 256
259 $_->check_size; 257 $self->check_size (1);
258 $self->update;
260} 259}
261 260
262sub set_max_size { 261sub set_max_size {
263 my ($self, $w, $h) = @_; 262 my ($self, $w, $h) = @_;
264 263
265 delete $self->{max_w}; $self->{max_w} = $w if $w; 264 delete $self->{max_w}; $self->{max_w} = $w if $w;
266 delete $self->{max_h}; $self->{max_h} = $h if $h; 265 delete $self->{max_h}; $self->{max_h} = $h if $h;
267}
268
269# return top left coordinates
270sub _topleft {
271 my ($self, $x, $y) = @_;
272
273 $self->{parent}
274 or Carp::confess "no parent widget in _topleft\n";#d#
275
276 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
277} 266}
278 267
279# translate global coordinates to local coordinate system 268# translate global coordinates to local coordinate system
280sub coord2local { 269sub coord2local {
281 my ($self, $x, $y) = @_; 270 my ($self, $x, $y) = @_;
282 271
283 my ($X, $Y) = $self->_topleft; 272 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
284 ($x - $X, $y - $Y)
285} 273}
286 274
287# translate local coordinates to global coordinate system 275# translate local coordinates to global coordinate system
288sub coord2global { 276sub coord2global {
289 my ($self, $x, $y) = @_; 277 my ($self, $x, $y) = @_;
290 278
291 my ($X, $Y) = $self->_topleft; 279 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
292 ($x + $X, $y + $Y)
293} 280}
294 281
295sub focus_in { 282sub focus_in {
296 my ($self) = @_; 283 my ($self) = @_;
297 284
395 382
396sub set_parent { 383sub set_parent {
397 my ($self, $parent) = @_; 384 my ($self, $parent) = @_;
398 385
399 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388
389 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size
391 unless exists $self->{req_w};
400} 392}
401 393
402sub check_size { 394sub check_size {
403 my ($self) = @_; 395 my ($self, $forced) = @_;
404 396
405 $self->{parent} 397 $self->{force_alloc} = 1 if $forced;
406 or return 1; 398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
407
408 my ($w, $h) = $self->{user_w} && $self->{user_h}
409 ? @$self{qw(user_w user_h)}
410 : $self->size_request;
411
412 if ($w != $self->{req_w} || $h != $self->{req_h}) {
413 $self->{req_w} = $w;
414 $self->{req_h} = $h;
415
416 $self->{parent}->check_size
417 or $self->size_allocate (
418 (List::Util::max $self->{w}, $w),
419 (List::Util::max $self->{h}, $h),
420 );
421
422 1
423 } else {
424 0
425 }
426} 399}
427 400
428sub update { 401sub update {
429 my ($self) = @_; 402 my ($self) = @_;
430 403
527 500
528 $self 501 $self
529} 502}
530 503
531sub add { 504sub add {
532 my ($self, $child) = @_; 505 my ($self, @widgets) = @_;
533 506
534 $child->set_parent ($self); 507 $_->set_parent ($self)
508 for @widgets;
535 509
536 use sort 'stable'; 510 use sort 'stable';
537 511
538 $self->{children} = [ 512 $self->{children} = [
539 sort { $a->{z} <=> $b->{z} } 513 sort { $a->{z} <=> $b->{z} }
540 @{$self->{children}}, $child 514 @{$self->{children}}, @widgets
541 ]; 515 ];
542 516
543 $child->check_size; 517 $self->check_size (1);
518 $self->update;
544} 519}
545 520
546sub children { 521sub children {
547 @{ $_[0]{children} } 522 @{ $_[0]{children} }
548} 523}
567 542
568 for (@$children) { 543 for (@$children) {
569 delete $_->{parent}; 544 delete $_->{parent};
570 $_->hide; 545 $_->hide;
571 } 546 }
547
548 $self->check_size;
549 $self->update;
572} 550}
573 551
574sub find_widget { 552sub find_widget {
575 my ($self, $x, $y) = @_; 553 my ($self, $x, $y) = @_;
576 554
651} 629}
652 630
653sub update { 631sub update {
654 my ($self) = @_; 632 my ($self) = @_;
655 633
656 $ROOT->on_refresh ($self => sub { $self->render_child }); 634 $ROOT->on_post_alloc ($self => sub { $self->render_child });
657 $self->SUPER::update; 635 $self->SUPER::update;
658} 636}
659 637
660sub size_allocate { 638sub size_allocate {
661 my ($self, $w, $h) = @_; 639 my ($self, $w, $h) = @_;
662 640
663 $self->SUPER::size_allocate ($w, $h); 641 $self->SUPER::size_allocate ($w, $h);
664 $self->update; 642 $self->update;
643}
644
645sub _render {
646 $_[0]{children}[0]->draw;
665} 647}
666 648
667sub render_child { 649sub render_child {
668 my ($self) = @_; 650 my ($self) = @_;
669 651
670 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 652 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
671 glClearColor 0, 0, 0, 0; 653 glClearColor 0, 0, 0, 0;
672 glClear GL_COLOR_BUFFER_BIT; 654 glClear GL_COLOR_BUFFER_BIT;
673 $self->child->draw; 655
656 $self->_render;
674# glColorMask 1, 1, 1, 0; 657# glColorMask 1, 1, 1, 0;
675# glEnable GL_BLEND; 658# glEnable GL_BLEND;
676# glBlendFunc GL_SRC_ALPHA, GL_ZERO; 659# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
677# glRasterPos 0, 0; 660# glRasterPos 0, 0;
678# glCopyPixels 0, 0, $self->{w}, $self->{h}; 661# glCopyPixels 0, 0, $self->{w}, $self->{h};
687 my ($w, $h) = ($self->w, $self->h); 670 my ($w, $h) = ($self->w, $self->h);
688 671
689 my $tex = $self->{texture} 672 my $tex = $self->{texture}
690 or return; 673 or return;
691 674
692 glEnable GL_BLEND;
693 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
694 glEnable GL_TEXTURE_2D; 675 glEnable GL_TEXTURE_2D;
695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
696 glColor 0, 0, 0, 1; 677 glColor 0, 0, 0, 1;
697 678
698 $tex->draw_quad (0, 0, $w, $h); 679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
699 680
700 glDisable GL_BLEND;
701 glDisable GL_TEXTURE_2D; 681 glDisable GL_TEXTURE_2D;
702} 682}
703 683
704############################################################################# 684#############################################################################
705 685
706package CFClient::UI::ViewPort; 686package CFClient::UI::ViewPort;
707 687
708our @ISA = CFClient::UI::Window::; 688our @ISA = CFClient::UI::Window::;
709 689
710sub new { die }
711
712sub size_request { 690sub size_request {
713 my ($self) = @_; 691 my ($self) = @_;
714 692
715 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 693 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
716 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 694 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
717 695
718 @$self{qw(child_w child_h)} 696 @$self{qw(child_w child_h)}
719} 697}
720 698
721sub _draw { 699sub size_allocate {
722 my ($self) = @_; 700 my ($self, $w, $h) = @_;
723 701
724 $self->{children}[1]->draw; 702 $self->update;
725} 703}
726 704
705sub set_offset {
706 my ($self, $x, $y) = @_;
727 707
728############################################################################# 708 $self->{view_x} = int $x;
709 $self->{view_y} = int $y;
729 710
711 $self->update;
712}
713
714# hmm, this does not work for topleft of $self... but we should not ask for that
715sub coord2local {
716 my ($self, $x, $y) = @_;
717
718 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
719}
720
721sub coord2global {
722 my ($self, $x, $y) = @_;
723
724 $x = List::Util::min $self->{w}, $x - $self->{view_x};
725 $y = List::Util::min $self->{h}, $y - $self->{view_y};
726
727 $self->SUPER::coord2global ($x, $y)
728}
729
730sub find_widget {
731 my ($self, $x, $y) = @_;
732
733 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
734 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
735 ) {
736 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
737 } else {
738 $self->CFClient::UI::Base::find_widget ($x, $y)
739 }
740}
741
742sub _render {
743 my ($self) = @_;
744
745 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
746
747 $self->SUPER::_render;
748}
749
750#############################################################################
751
730package CFClient::UI::Frame; 752package CFClient::UI::ScrolledWindow;
731 753
732our @ISA = CFClient::UI::Bin::; 754our @ISA = CFClient::UI::HBox::;
733
734use CFClient::OpenGL;
735 755
736sub new { 756sub new {
737 my $class = shift; 757 my $class = shift;
738 758
759 my $self;
760
761 my $slider = new CFClient::UI::Slider
762 vertical => 1,
763 range => [0, 0, 1, 0.01], # HACK fix
764 connect_changed => sub {
765 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
766 },
767 ;
768
739 my $self = $class->SUPER::new ( 769 $self = $class->SUPER::new (
740 bg => [1, 1, 1, 1], 770 vp => (new CFClient::UI::ViewPort),
741 border_bg => [1, 1, 1, 1], 771 slider => $slider,
742 border => 0.8,
743 @_ 772 @_,
744 ); 773 );
745 774
775 $self->{vp}->add ($self->{scrolled});
776 $self->add ($self->{vp});
777 $self->add ($self->{slider});
778
746 $self 779 $self
747} 780}
748 781
749sub _draw { 782#TODO# update range on size_allocate depeneing on child
750 my ($self) = @_; 783# update viewport offset on scroll
751 784
752 my ($w, $h) = ($self->{w}, $self->{h}); 785#############################################################################
753 786
754 glEnable GL_BLEND; 787package CFClient::UI::Frame;
755 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
756 glEnable GL_TEXTURE_2D;
757 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
758 788
759# glBegin GL_QUADS; 789our @ISA = CFClient::UI::Bin::;
760# glColor 0, 0, 0, 0;
761# glVertex 0 , 0;
762# glVertex 0 , $h;
763# glVertex $w, $h;
764# glVertex $w, 0;
765# glEnd;
766 790
767 791use CFClient::OpenGL;
768 $self->child->draw;
769 glDisable GL_BLEND;
770 glDisable GL_TEXTURE_2D;
771}
772 792
773############################################################################# 793#############################################################################
774 794
775package CFClient::UI::FancyFrame; 795package CFClient::UI::FancyFrame;
776 796
891 my ($self) = @_; 911 my ($self) = @_;
892 912
893 my ($w, $h ) = ($self->{w}, $self->{h}); 913 my ($w, $h ) = ($self->{w}, $self->{h});
894 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 914 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
895 915
896 glEnable GL_BLEND;
897 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
898 glEnable GL_TEXTURE_2D; 916 glEnable GL_TEXTURE_2D;
899 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
900 918
901 my $border = $self->border; 919 my $border = $self->border;
902 920
903 glColor @{ $self->{border_bg} }; 921 glColor @{ $self->{border_bg} };
904 $tex[1]->draw_quad (0, 0, $w, $border); 922 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
905 $tex[3]->draw_quad (0, $border, $border, $ch); 923 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
906 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 924 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
907 $tex[4]->draw_quad (0, $h - $border, $w, $border); 925 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
908 926
909 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 927 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
910 my $bg = $tex[0]; 928 my $bg = $tex[0];
911 929
912 # TODO: repeat texture not scale 930 # TODO: repeat texture not scale
916 glColor @{ $self->{bg} }; 934 glColor @{ $self->{bg} };
917 935
918 $bg->{s} = $rep_x; 936 $bg->{s} = $rep_x;
919 $bg->{t} = $rep_y; 937 $bg->{t} = $rep_y;
920 $bg->{wrap_mode} = 1; 938 $bg->{wrap_mode} = 1;
921 $bg->draw_quad ($border, $border, $cw, $ch); 939 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
940 }
922 941
923 glDisable GL_TEXTURE_2D; 942 glDisable GL_TEXTURE_2D;
924 glDisable GL_BLEND;
925 }
926 943
927 $self->{title}->draw if $self->{title}; 944 $self->{title}->draw if $self->{title};
928 945
929 $self->child->draw; 946 $self->child->draw;
930} 947}
1157} 1174}
1158 1175
1159sub size_allocate { 1176sub size_allocate {
1160 my ($self, $w, $h) = @_; 1177 my ($self, $w, $h) = @_;
1161 1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1162 my $children = $self->{children}; 1181 my $children = $self->{children};
1163 1182
1164 my @h = map $_->{req_h}, @$children; 1183 my @h = map $_->{req_h}, @$children;
1165 1184
1166 my $req_h = List::Util::sum @h; 1185 my $req_h = List::Util::sum @h;
1205 my ($class, %arg) = @_; 1224 my ($class, %arg) = @_;
1206 1225
1207 my $self = $class->SUPER::new ( 1226 my $self = $class->SUPER::new (
1208 fg => [1, 1, 1], 1227 fg => [1, 1, 1],
1209 #font => default_font 1228 #font => default_font
1229 #text => initial text
1230 #markup => initial narkup
1231 layout => (new CFClient::Layout),
1210 fontsize => 1, 1232 fontsize => 1,
1211 text => "",
1212 align => -1, 1233 align => -1,
1213 valign => -1, 1234 valign => -1,
1214 padding => 2, 1235 padding => 2,
1215 layout => new CFClient::Layout,
1216 can_events => 0, 1236 can_events => 0,
1217 %arg 1237 %arg
1218 ); 1238 );
1219 1239
1220 if (exists $self->{template}) { 1240 if (exists $self->{template}) {
1221 my $layout = new CFClient::Layout; 1241 my $layout = new CFClient::Layout;
1222 $layout->set_text (delete $self->{template}); 1242 $layout->set_text (delete $self->{template});
1223 $self->{template} = $layout; 1243 $self->{template} = $layout;
1224 } 1244 }
1225 1245
1226 $self->set_text (delete $self->{text}) if exists $self->{text}; 1246 if (exists $self->{markup}) {
1227 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1247 $self->set_markup (delete $self->{markup});
1248 } else {
1249 $self->set_text (delete $self->{text});
1250 }
1228 1251
1229 $self 1252 $self
1230} 1253}
1231 1254
1232sub escape { 1255sub escape {
1244 1267
1245 delete $self->{texture}; 1268 delete $self->{texture};
1246 $self->SUPER::update; 1269 $self->SUPER::update;
1247} 1270}
1248 1271
1249sub reconfigure {
1250 my ($self) = @_;
1251
1252 delete $self->{texture};
1253}
1254
1255sub set_text { 1272sub set_text {
1256 my ($self, $text) = @_; 1273 my ($self, $text) = @_;
1257 1274
1258 return if $self->{text} eq "T$text"; 1275 return if $self->{text} eq "T$text";
1259 $self->{text} = "T$text"; 1276 $self->{text} = "T$text";
1260 1277
1278 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1261 $self->{layout}->set_text ($text); 1279 $self->{layout}->set_text ($text);
1262 1280
1263 delete $self->{texture};
1264 $self->update; 1281 $self->update;
1265 $self->check_size; 1282 $self->check_size;
1266} 1283}
1267 1284
1268sub set_markup { 1285sub set_markup {
1269 my ($self, $markup) = @_; 1286 my ($self, $markup) = @_;
1270 1287
1271 return if $self->{text} eq "M$markup"; 1288 return if $self->{text} eq "M$markup";
1272 $self->{text} = "M$markup"; 1289 $self->{text} = "M$markup";
1273 1290
1291 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1292
1293 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1274 $self->{layout}->set_markup ($markup); 1294 $self->{layout}->set_markup ($markup);
1275 1295
1276 delete $self->{texture};
1277 $self->update; 1296 $self->update;
1278 $self->check_size; 1297 $self->check_size;
1279} 1298}
1280 1299
1281sub size_request { 1300sub size_request {
1312sub set_fontsize { 1331sub set_fontsize {
1313 my ($self, $fontsize) = @_; 1332 my ($self, $fontsize) = @_;
1314 1333
1315 $self->{fontsize} = $fontsize; 1334 $self->{fontsize} = $fontsize;
1316 delete $self->{texture}; 1335 delete $self->{texture};
1336
1337 $self->update;
1317 $self->check_size; 1338 $self->check_size;
1318 $self->update;
1319} 1339}
1320 1340
1321sub _draw { 1341sub _draw {
1322 my ($self) = @_; 1342 my ($self) = @_;
1323 1343
1324 my $tex = $self->{texture} ||= do { 1344 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}});
1325 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1346 $self->{layout}->set_font ($self->{font}) if $self->{font};
1326 $self->{layout}->set_width ($self->{w}); 1347 $self->{layout}->set_width ($self->{w});
1327 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1348 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1349
1328 new_from_layout CFClient::Texture $self->{layout} 1350 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5;
1355
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5;
1359
1360 $tex
1329 }; 1361 };
1330 1362
1331 glEnable GL_BLEND;
1332 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1333 glEnable GL_TEXTURE_2D; 1363 glEnable GL_TEXTURE_2D;
1334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1364 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1335 1365
1366 if ($tex->{format} == GL_ALPHA) {
1336 glColor @{$self->{fg}}; 1367 glColor @{$self->{fg}};
1337
1338 $self->{ox} = int (
1339 $self->{align} < 0 ? $self->{padding}
1340 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1341 : ($self->{w} - $tex->{w}) * 0.5
1342 );
1343
1344 $self->{oy} = int (
1345 $self->{valign} < 0 ? $self->{padding}
1346 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1347 : ($self->{h} - $tex->{h}) * 0.5
1348 );
1349
1350 $tex->draw_quad ($self->{ox}, $self->{oy}); 1368 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1369 } else {
1370 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1371 }
1351 1372
1352 glDisable GL_TEXTURE_2D; 1373 glDisable GL_TEXTURE_2D;
1353 glDisable GL_BLEND;
1354} 1374}
1355 1375
1356############################################################################# 1376#############################################################################
1357 1377
1358package CFClient::UI::EntryBase; 1378package CFClient::UI::EntryBase;
1393 $self->{layout}->set_text ("$text "); 1413 $self->{layout}->set_text ("$text ");
1394 1414
1395 $self->emit (changed => $self->{text}); 1415 $self->emit (changed => $self->{text});
1396} 1416}
1397 1417
1418sub set_text {
1419 my ($self, $text) = @_;
1420
1421 $self->{cursor} = length $text;
1422 $self->_set_text ($text);
1423 $self->check_size;
1424 $self->update;
1425}
1426
1398sub get_text { 1427sub get_text {
1399 $_[0]{text} 1428 $_[0]{text}
1400} 1429}
1401 1430
1402sub size_request { 1431sub size_request {
1409 1438
1410sub size_allocate { 1439sub size_allocate {
1411 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1412 1441
1413 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1442 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1414}
1415
1416sub set_text {
1417 my ($self, $text) = @_;
1418
1419 $self->{cursor} = length $text;
1420 $self->_set_text ($text);
1421 $self->update;
1422} 1443}
1423 1444
1424sub key_down { 1445sub key_down {
1425 my ($self, $ev) = @_; 1446 my ($self, $ev) = @_;
1426 1447
1614 1635
1615 if ($GRAB == $self) { 1636 if ($GRAB == $self) {
1616 $self->{fg} = $self->{active_fg}; 1637 $self->{fg} = $self->{active_fg};
1617 } 1638 }
1618 1639
1619 glEnable GL_BLEND;
1620 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1621 glEnable GL_TEXTURE_2D; 1640 glEnable GL_TEXTURE_2D;
1622 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1623 glColor 0, 0, 0, 1; 1642 glColor 0, 0, 0, 1;
1624 1643
1625 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1644 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1626 1645
1627 glDisable GL_TEXTURE_2D; 1646 glDisable GL_TEXTURE_2D;
1628 glDisable GL_BLEND;
1629 1647
1630 $self->SUPER::_draw; 1648 $self->SUPER::_draw;
1631} 1649}
1632 1650
1633############################################################################# 1651#############################################################################
1680 1698
1681 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1699 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1682 1700
1683 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1701 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1684 1702
1685 glEnable GL_BLEND; 1703 my $tex = $self->{state} ? $tex[1] : $tex[0];
1704
1686 glEnable GL_TEXTURE_2D; 1705 glEnable GL_TEXTURE_2D;
1687 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1688
1689 my $tex = $self->{state} ? $tex[1] : $tex[0];
1690
1691 $tex->draw_quad (0, 0, $s, $s); 1706 $tex->draw_quad_alpha (0, 0, $s, $s);
1692
1693 glDisable GL_TEXTURE_2D; 1707 glDisable GL_TEXTURE_2D;
1694 glDisable GL_BLEND;
1695} 1708}
1696 1709
1697############################################################################# 1710#############################################################################
1698 1711
1699package CFClient::UI::Image; 1712package CFClient::UI::Image;
1742 glTranslate 0, -$self->{w}, 0; 1755 glTranslate 0, -$self->{w}, 0;
1743 1756
1744 ($w, $h) = ($h, $w); 1757 ($w, $h) = ($h, $w);
1745 } 1758 }
1746 1759
1747 glEnable GL_BLEND;
1748 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1749 glEnable GL_TEXTURE_2D; 1760 glEnable GL_TEXTURE_2D;
1750 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1751 1762
1752 $tex->draw_quad (0, 0, $w, $h); 1763 $tex->draw_quad_alpha (0, 0, $w, $h);
1753 1764
1754 glDisable GL_BLEND;
1755 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1756} 1766}
1757 1767
1758############################################################################# 1768#############################################################################
1759 1769
1957 # TODO: calculations are off 1967 # TODO: calculations are off
1958 my $self = $class->SUPER::new ( 1968 my $self = $class->SUPER::new (
1959 fg => [1, 1, 1], 1969 fg => [1, 1, 1],
1960 active_fg => [0, 0, 0], 1970 active_fg => [0, 0, 0],
1961 range => [0, 0, 100, 10], 1971 range => [0, 0, 100, 10],
1962 req_w => 20, 1972 req_w => $::WIDTH / 80,
1963 req_h => 20, 1973 req_h => $::WIDTH / 80,
1964 vertical => 0, 1974 vertical => 0,
1965 can_hover => 1, 1975 can_hover => 1,
1966 inner_pad => 5, 1976 inner_pad => 5,
1967 @_ 1977 @_
1968 ); 1978 );
2051 $page ||= 2; 2061 $page ||= 2;
2052 2062
2053 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2054 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2055 2065
2056 glEnable GL_BLEND;
2057 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2058 glEnable GL_TEXTURE_2D; 2066 glEnable GL_TEXTURE_2D;
2059 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2060 2068
2061 # draw background 2069 # draw background
2062 $tex[1]->draw_quad (0, 0, $w, $h); 2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2063 2071
2064 # draw handle 2072 # draw handle
2065 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2073 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2066 2074
2067 glDisable GL_BLEND;
2068 glDisable GL_TEXTURE_2D; 2075 glDisable GL_TEXTURE_2D;
2069} 2076}
2070 2077
2071############################################################################# 2078#############################################################################
2072 2079
2083 fontsize => 1, 2090 fontsize => 1,
2084 can_events => 0, 2091 can_events => 0,
2085 #font => default_font 2092 #font => default_font
2086 @_, 2093 @_,
2087 2094
2088 layout => (new CFClient::Layout), 2095 layout => (new CFClient::Layout 1),
2089 par => [], 2096 par => [],
2090 height => 0, 2097 height => 0,
2091 children => [ 2098 children => [
2092 (new CFClient::UI::Empty expand => 1), 2099 (new CFClient::UI::Empty expand => 1),
2093 (new CFClient::UI::Slider vertical => 1), 2100 (new CFClient::UI::Slider vertical => 1),
2111 2118
2112 my $layout = $self->{layout}; 2119 my $layout = $self->{layout};
2113 2120
2114 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2115 $layout->set_width ($self->{children}[0]{w}); 2122 $layout->set_width ($self->{children}[0]{w});
2116 $layout->set_text ($text); 2123 $layout->set_markup ($text);
2117 2124
2118 ($layout->size)[1] 2125 ($layout->size)[1]
2119} 2126}
2120 2127
2121sub reflow { 2128sub reflow {
2159 2166
2160 return unless $self->{h} > 0; 2167 return unless $self->{h} > 0;
2161 2168
2162 delete $self->{texture}; 2169 delete $self->{texture};
2163 2170
2164 $ROOT->on_refresh ($self, sub { 2171 $ROOT->on_post_alloc ($self, sub {
2165 if (delete $self->{need_reflow}) { 2172 if (delete $self->{need_reflow}) {
2166 my $height = 0; 2173 my $height = 0;
2167 2174
2168 $height += $_->[0] = $self->text_height ($_->[2]) 2175 $height += $_->[0] = $self->text_height ($_->[2])
2169 for @{$self->{par}}; 2176 for @{$self->{par}};
2177 2184
2178 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2185 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2179 glClearColor 0, 0, 0, 0; 2186 glClearColor 0, 0, 0, 0;
2180 glClear GL_COLOR_BUFFER_BIT; 2187 glClear GL_COLOR_BUFFER_BIT;
2181 2188
2182 glEnable GL_BLEND;
2183 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2184 glEnable GL_TEXTURE_2D; 2189 glEnable GL_TEXTURE_2D;
2185 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2186 2191
2187 my $top = int $self->{children}[1]{range}[0]; 2192 my $top = int $self->{children}[1]{range}[0];
2188 2193
2197 2202
2198 for my $par (@{$self->{par}}) { 2203 for my $par (@{$self->{par}}) {
2199 my $h = $par->[0]; 2204 my $h = $par->[0];
2200 2205
2201 if ($y0 < $y + $h && $y < $y1) { 2206 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] });
2202 $layout->set_text ($par->[2]); 2208 $layout->set_markup ($par->[2]);
2203 2209
2204 glColor @{ $par->[1] };
2205 my ($W, $H) = $layout->size; 2210 my ($W, $H) = $layout->size;
2206 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2211 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2207 } 2212 }
2208 2213
2209 $y += $h; 2214 $y += $h;
2210 } 2215 }
2211 2216
2212 glDisable GL_TEXTURE_2D; 2217 glDisable GL_TEXTURE_2D;
2213 glDisable GL_BLEND;
2214 }; 2218 };
2215 }); 2219 });
2216} 2220}
2217 2221
2218sub _draw { 2222sub _draw {
2219 my ($self) = @_; 2223 my ($self) = @_;
2220 2224
2221 glEnable GL_BLEND;
2222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2223 glEnable GL_TEXTURE_2D; 2225 glEnable GL_TEXTURE_2D;
2224 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2225 glColor 1, 1, 1, 1; 2227 glColor 1, 1, 1, 1;
2226 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2228 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2227 glDisable GL_TEXTURE_2D; 2229 glDisable GL_TEXTURE_2D;
2228 glDisable GL_BLEND;
2229 2230
2230 $self->{children}[1]->draw; 2231 $self->{children}[1]->draw;
2231 2232
2232} 2233}
2233 2234
2327 @_, 2328 @_,
2328 can_events => 0, 2329 can_events => 0,
2329 ) 2330 )
2330} 2331}
2331 2332
2332sub set_markup { 2333sub set_tooltip_from {
2333 my ($self, $text) = @_; 2334 my ($self, $widget) = @_;
2334 2335
2335 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2336 $self->add (new CFClient::UI::Label
2336 $self->{label}->set_markup ($text); 2337 markup => $widget->{tooltip},
2337 $self->add ($self->{label}); 2338 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2339 fontsize => 0.8,
2340 fg => [0, 0, 0, 1],
2341 font => ($widget->{tooltip_font} || $::FONT_PROP),
2342 );
2338} 2343}
2339 2344
2340sub size_request { 2345sub size_request {
2341 my ($self) = @_; 2346 my ($self) = @_;
2342
2343 $self->child->set_max_size ($::WIDTH * 0.3);
2344 2347
2345 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2348 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2346 2349
2347 ($w + 4, $h + 4) 2350 ($w + 4, $h + 4)
2348} 2351}
2354} 2357}
2355 2358
2356sub _draw { 2359sub _draw {
2357 my ($self) = @_; 2360 my ($self) = @_;
2358 2361
2359 glPushMatrix;
2360 glTranslate 0.375, 0.375; 2362 glTranslate 0.375, 0.375;
2361 2363
2362 my ($w, $h) = @$self{qw(w h)}; 2364 my ($w, $h) = @$self{qw(w h)};
2363 2365
2364 glColor 1, 0.8, 0.4; 2366 glColor 1, 0.8, 0.4;
2375 glVertex 0 , $h; 2377 glVertex 0 , $h;
2376 glVertex $w, $h; 2378 glVertex $w, $h;
2377 glVertex $w, 0; 2379 glVertex $w, 0;
2378 glEnd; 2380 glEnd;
2379 2381
2380 glPopMatrix; 2382 glTranslate 2 - 0.375, 2 - 0.375;
2381
2382 glTranslate 2, 2;
2383 $self->SUPER::_draw; 2383 $self->SUPER::_draw;
2384} 2384}
2385 2385
2386############################################################################# 2386#############################################################################
2387 2387
2405} 2405}
2406 2406
2407sub _draw { 2407sub _draw {
2408 my ($self) = @_; 2408 my ($self) = @_;
2409 2409
2410 return unless $::CONN;#d# manage and cache textures differently
2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2411 2412
2412 # TODO animation 2413 # TODO animation
2413 if ($tex) { 2414 if ($tex) {
2414 glEnable GL_BLEND;
2415 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2416 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2418 glColor 1, 1, 1, 1; 2417 glColor 1, 1, 1, 1;
2419 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2420 glDisable GL_TEXTURE_2D; 2419 glDisable GL_TEXTURE_2D;
2421 glDisable GL_BLEND;
2422 } 2420 }
2421}
2422
2423#############################################################################
2424
2425package CFClient::UI::InventoryItem;
2426
2427our @ISA = CFClient::UI::HBox::;
2428
2429sub new {
2430 my $class = shift;
2431
2432 my %args = @_;
2433
2434 my $item = delete $args{item};
2435
2436 my $desc = $item->{nrof} < 2
2437 ? $item->{name}
2438 : "$item->{nrof} $item->{name_pl}";
2439
2440
2441 my $self = $class->SUPER::new (
2442 can_hover => 1,
2443 can_events => 1,
2444 tooltip => (CFClient::UI::Label->escape ($desc)
2445 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2446 connect_button_down => sub {
2447 my ($self, $ev, $x, $y) = @_;
2448
2449 # todo: maybe put examine on 1? but should just be a tooltip :(
2450 if ($ev->{button} == 1) {
2451 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2452 } elsif ($ev->{button} == 2) {
2453 $::CONN->send ("apply $item->{tag}");
2454 } elsif ($ev->{button} == 3) {
2455 CFClient::UI::Menu->new (
2456 items => [
2457 ["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}") }],
2463 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2464 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2465 ],
2466 )->popup ($ev);
2467 }
2468
2469 1
2470 },
2471 %args
2472 );
2473
2474 $self->add (new CFClient::UI::Face
2475 can_events => 0,
2476 face => $item->{face},
2477 anim => $item->{anim},
2478 animspeed => $item->{animspeed},
2479 );
2480
2481 $self->add (new CFClient::UI::Label
2482 can_events => 0,
2483 text => $desc,
2484 );
2485
2486 $self
2487}
2488
2489#############################################################################
2490
2491package CFClient::UI::Inventory;
2492
2493our @ISA = CFClient::UI::ScrolledWindow::;
2494
2495sub new {
2496 my $class = shift;
2497
2498 my $self = $class->SUPER::new (
2499 scrolled => (new CFClient::UI::VBox),
2500 @_,
2501 );
2502
2503 $self
2504}
2505
2506sub set_items {
2507 my ($self, $items) = @_;
2508
2509 $self->{scrolled}->clear;
2510 return unless $items;
2511
2512 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2513
2514 $self->{real_items} = \@items;
2515
2516 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);
2522 }
2523
2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2525}
2526
2527sub size_request {
2528 my ($self) = @_;
2529 ($self->{req_w}, $self->{req_h});
2423} 2530}
2424 2531
2425############################################################################# 2532#############################################################################
2426 2533
2427package CFClient::UI::Menu; 2534package CFClient::UI::Menu;
2475} 2582}
2476 2583
2477sub mouse_motion { 2584sub mouse_motion {
2478 my ($self, $ev, $x, $y) = @_; 2585 my ($self, $ev, $x, $y) = @_;
2479 2586
2480 # TODO: should use vbox->fdind_widget or so 2587 # TODO: should use vbox->find_widget or so
2481 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2588 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2482 $self->{hover} = $self->{item}{$HOVER}; 2589 $self->{hover} = $self->{item}{$HOVER};
2483} 2590}
2484 2591
2485sub button_up { 2592sub button_up {
2494 } 2601 }
2495} 2602}
2496 2603
2497############################################################################# 2604#############################################################################
2498 2605
2606package CFClient::UI::Statusbox;
2607
2608our @ISA = CFClient::UI::VBox::;
2609
2610sub reorder {
2611 my ($self) = @_;
2612 my $NOW = time;
2613
2614 while (my ($k, $v) = each %{ $self->{item} }) {
2615 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2616 }
2617
2618 my @widgets;
2619
2620 my @items = sort {
2621 $a->{pri} <=> $b->{pri}
2622 or $b->{id} <=> $a->{id}
2623 } values %{ $self->{item} };
2624
2625 my $count = 10 + 1;
2626 for my $item (@items) {
2627 last unless --$count;
2628
2629 push @widgets, $item->{label} ||= do {
2630 # TODO: doesn't handle markup well (read as: at all)
2631 my $short = $item->{count} > 1
2632 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text};
2634
2635 for ($short) {
2636 s/^\s+//;
2637 s/\012.*/…/s;
2638 my $len = int 40 / $item->{fontsize};
2639 substr $_, $len, length, "…" if $len < length;
2640 }
2641
2642 new CFClient::UI::Label
2643 markup => $short,
2644 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize},
2648 color => $item->{color},
2649 can_events => 1,
2650 can_hover => 1
2651 };
2652 }
2653
2654 $self->clear;
2655 $self->SUPER::add (reverse @widgets);
2656}
2657
2658sub add {
2659 my ($self, $text, %arg) = @_;
2660
2661 $text =~ s/^\s+//;
2662 $text =~ s/\s+$//;
2663
2664 my $timeout = time + ((delete $arg{timeout}) || 60);
2665
2666 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2667
2668 if (my $item = $self->{item}{$group}) {
2669 if ($item->{text} eq $text) {
2670 $item->{count}++;
2671 } else {
2672 $item->{count} = 1;
2673 $item->{text} = $item->{tooltip} = $text;
2674 }
2675 $item->{id} = ++$self->{id};
2676 $item->{timeout} = $timeout;
2677 delete $item->{label};
2678 } else {
2679 $self->{item}{$group} = {
2680 id => ++$self->{id},
2681 text => $text,
2682 timeout => $timeout,
2683 tooltip => $text,
2684 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0,
2687 count => 1,
2688 %arg,
2689 };
2690 }
2691
2692 $self->reorder;
2693}
2694
2695#############################################################################
2696
2499package CFClient::UI::Root; 2697package CFClient::UI::Root;
2500 2698
2501our @ISA = CFClient::UI::Container::; 2699our @ISA = CFClient::UI::Container::;
2502 2700
2503use CFClient::OpenGL; 2701use CFClient::OpenGL;
2504 2702
2505sub check_size { 2703sub new {
2506 my ($self) = @_; 2704 my $class = shift;
2507 2705
2508 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2706 $class->SUPER::new (
2509} 2707 @_,
2510 2708 )
2511sub size_request {
2512 ($::WIDTH, $::HEIGHT)
2513} 2709}
2514 2710
2515sub configure { 2711sub configure {
2516 my ($self, $x, $y, $w, $h) = @_; 2712 my ($self, $x, $y, $w, $h) = @_;
2517 2713
2518 $self->SUPER::configure ($x, $y, $w, $h); 2714 $self->{w} = $w;
2715 $self->{h} = $h;
2716}
2519 2717
2718sub check_size {
2719 my ($self) = @_;
2720
2721 $self->size_allocate ($self->{w}, $self->{h})
2722 if $self->{w};
2723}
2724
2725sub size_request {
2726 my ($self) = @_;
2727
2728 ($self->{w}, $self->{h})
2729}
2730
2731sub size_allocate {
2732 my ($self, $w, $h) = @_;
2733
2734 my $old_w = $self->{old_w};
2735 my $old_h = $self->{old_h};
2736
2737 if ($old_w && $old_h) {
2738 for my $child ($self->children) {
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
2520 for my $child (@{$self->{children}}) { 2750 for my $child ($self->children) {
2521 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2751 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2522 2752
2523 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2753 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2524 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2525 $child->configure ($X, $Y, $W,$H); 2755 $child->configure ($X, $Y, $W, $H);
2526 } 2756 }
2527}
2528 2757
2529sub _topleft { 2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760}
2761
2762sub coord2local {
2530 my ($self, $x, $y) = @_; 2763 my ($self, $x, $y) = @_;
2531 2764
2532 ($x, $y) 2765 ($x, $y)
2533} 2766}
2534 2767
2768sub coord2global {
2769 my ($self, $x, $y) = @_;
2770
2771 ($x, $y)
2772}
2773
2535sub update { 2774sub update {
2536 my ($self) = @_; 2775 my ($self) = @_;
2537 2776
2538 $self->check_size; 2777 $self->check_size;
2539 ::refresh (); 2778 $::WANT_REFRESH++;
2540} 2779}
2541 2780
2542sub add { 2781sub add {
2543 my ($self, $child) = @_; 2782 my ($self, $child) = @_;
2544 2783
2545 # integerize window positions 2784 # integerise window positions
2546 $child->{x} = int $child->{x}; 2785 $child->{x} = int $child->{x};
2547 $child->{y} = int $child->{y}; 2786 $child->{y} = int $child->{y};
2548 2787
2549 $self->SUPER::add ($child); 2788 $self->SUPER::add ($child);
2550} 2789}
2553 my ($self, $id, $cb) = @_; 2792 my ($self, $id, $cb) = @_;
2554 2793
2555 $self->{refresh_hook}{$id} = $cb; 2794 $self->{refresh_hook}{$id} = $cb;
2556} 2795}
2557 2796
2797sub on_post_alloc {
2798 my ($self, $id, $cb) = @_;
2799
2800 $self->{post_alloc_hook}{$id} = $cb;
2801}
2802
2558sub draw { 2803sub draw {
2559 my ($self) = @_; 2804 my ($self) = @_;
2560 2805
2561 while (my $rcb = delete $self->{refresh_hook}) { 2806 while ($self->{refresh_hook}) {
2562 $_->() for values %$rcb; 2807 $_->()
2808 for values %{delete $self->{refresh_hook}};
2809 }
2810
2811 if ($self->{check_size}) {
2812 my @queue = ([], []);
2813
2814 for (;;) {
2815 if ($self->{check_size}) {
2816 # heuristic: check containers last
2817 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2818 for values %{delete $self->{check_size}}
2819 }
2820
2821 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2822
2823 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2824 ? @$widget{qw(user_w user_h)}
2825 : $widget->size_request;
2826
2827 if (delete $widget->{force_alloc}
2828 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2829 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2830
2831 $widget->{req_w} = $w;
2832 $widget->{req_h} = $h;
2833
2834 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2835
2836 $widget->{parent}->check_size
2837 if $widget->{parent};
2838 }
2839 }
2840 }
2841
2842 while ($self->{size_alloc}) {
2843 for (values %{delete $self->{size_alloc}}) {
2844 my ($widget, $w, $h) = @$_;
2845
2846 $w = 0 if $w < 0;
2847 $h = 0 if $h < 0;
2848
2849 $widget->{w} = $w;
2850 $widget->{h} = $h;
2851 $widget->size_allocate ($w, $h);
2852 $widget->emit (size_allocate => $w, $h);
2853 }
2854 }
2855
2856 while ($self->{post_alloc_hook}) {
2857 $_->()
2858 for values %{delete $self->{post_alloc_hook}};
2563 } 2859 }
2564 2860
2565 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2861 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2566 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2567 glClear GL_COLOR_BUFFER_BIT; 2863 glClear GL_COLOR_BUFFER_BIT;
2575 $self->_draw; 2871 $self->_draw;
2576} 2872}
2577 2873
2578############################################################################# 2874#############################################################################
2579 2875
2580package CFClient::UI::Inventory;
2581
2582our @ISA = CFClient::UI::Container::;
2583
2584use CFClient::OpenGL;
2585
2586sub new {
2587 my $class = shift;
2588
2589 my $self = $class->SUPER::new (@_);
2590
2591 $self
2592}
2593
2594sub size_allocate {
2595 my ($self, $w, $h) = @_;
2596
2597 $self->{w} = $w;
2598 $self->{h} = $h;
2599
2600 $self->check_size;
2601}
2602
2603sub set_items {
2604 my ($self, $items) = @_;
2605 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2606
2607 $self->{real_items} = \@items;
2608
2609 for my $item (@items) {
2610 my $desc = $item->{nrof} < 2
2611 ? $item->{name}
2612 : "$item->{nrof} $item->{name_pl}";
2613
2614 $self->add (my $hb = new CFClient::UI::HBox);
2615
2616 $hb->add (my $f = new CFClient::UI::Face
2617 can_events => 0,
2618 face => $item->{face},
2619 anim => $item->{anim},
2620 animspeed => $item->{animspeed},
2621 expand => 1,
2622 );
2623 $hb->add (new CFClient::UI::Label text => $desc, expand => 1);
2624 }
2625
2626 $self->{max_pos} = (scalar @items) - 1;
2627
2628 my $range = $self->{range};
2629 my $page = $self->{h} / 32; # what information source to use for face size?
2630 # it should be configurable I guess...
2631 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2632 $range->update;
2633}
2634
2635sub size_request {
2636 my ($self) = @_;
2637 (100, 200)
2638}
2639
2640sub set_range {
2641 my ($self, $range) = @_;
2642
2643 $self->{range} = $range;
2644 $range->connect (changed => sub { $self->update });
2645
2646 my $page = $self->{h} / 32; # waht information souce to use for face size?
2647 # it should be configurable i guess...
2648
2649 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2650 $range->update;
2651
2652 $self->update;
2653}
2654
2655sub _draw {
2656 my ($self) = @_;
2657
2658 my ($w, $h) = ($self->{w}, $self->{h});
2659
2660 $self->{pos} = int $self->{range}{range}[0];
2661
2662 my $y = 0;
2663 my $cnt = 0;
2664 my $hrem = $self->{h}; # horiz. remaining space
2665
2666 for (my $i = $self->{pos}; $i < @{$self->{children} || []}; $i++) {
2667 my $chld = $self->{children}->[$i];
2668
2669 if ($hrem >= $chld->{h}) {
2670 $chld->configure (0, $y, $chld->{w}, $chld->{h});
2671
2672 $chld->draw;
2673
2674 $hrem -= $chld->{h};
2675 $y += $chld->{h};
2676 } else {
2677 last
2678 }
2679 }
2680}
2681
2682
2683#############################################################################
2684
2685package CFClient::UI; 2876package CFClient::UI;
2686 2877
2687$ROOT = new CFClient::UI::Root; 2878$ROOT = new CFClient::UI::Root;
2688$TOOLTIP = new CFClient::UI::Tooltip; 2879$TOOLTIP = new CFClient::UI::Tooltip;
2689 2880

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines