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.173 by root, Tue Apr 25 10:30:48 2006 UTC vs.
Revision 1.190 by root, Mon May 8 22:17:38 2006 UTC

24 my $tip = $widget->{tooltip}; 24 my $tip = $widget->{tooltip};
25 25
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 27
28 $TOOLTIP->set_markup ($widget->{tooltip}); 28 $TOOLTIP->set_markup ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); 29
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 }
37
38 $TOOLTIP->move ($x, $y);
31 } 39 }
32 40
33 return; 41 return;
34 } 42 }
35 } 43 }
229 $self->{w} = $w; 237 $self->{w} = $w;
230 $self->{h} = $h; 238 $self->{h} = $h;
231 239
232 $self->size_allocate ($w, $h); 240 $self->size_allocate ($w, $h);
233 $self->update; 241 $self->update;
242 $self->emit (size_allocate => $w, $h);
234 } 243 }
235} 244}
236 245
237sub size_allocate { 246sub size_allocate {
238 # nothing to be done 247 # nothing to be done
239} 248}
240 249
241sub children { 250sub children {
242} 251}
243 252
244# call when resoltuion changes etc. 253# call when resolution changes etc.
245sub reconfigure { 254sub reconfigure {
246 my ($self) = @_; 255 my ($self) = @_;
247 256
248 $_->reconfigure 257 $_->reconfigure
249 for $self->children; 258 for $self->children;
250 259
251 $_->check_size; 260 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h});
262 $self->update;
252} 263}
253 264
254sub set_max_size { 265sub set_max_size {
255 my ($self, $w, $h) = @_; 266 my ($self, $w, $h) = @_;
256 267
392} 403}
393 404
394sub check_size { 405sub check_size {
395 my ($self) = @_; 406 my ($self) = @_;
396 407
408 $self->_check_size;
409 return;
410 #TODO: but needs a vastly more difficult algorithm
411 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
412
413 $check_size->{$self} = $self;
414
415 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
416 while (%$check_size) {
417 my @widgets = values %$check_size;
418 $_->_check_size
419 for @widgets;
420 }
421 });
422}
423
424sub _check_size {
425 my ($self) = @_;
426
427 delete $CFClient::UI::ROOT->{_check_size}{$self};
428
397 $self->{parent} 429 $self->{parent}
398 or return 1; 430 or return 1;
399 431
400 my ($w, $h) = $self->{user_w} && $self->{user_h} 432 my ($w, $h) = $self->{user_w} && $self->{user_h}
401 ? @$self{qw(user_w user_h)} 433 ? @$self{qw(user_w user_h)}
403 435
404 if ($w != $self->{req_w} || $h != $self->{req_h}) { 436 if ($w != $self->{req_w} || $h != $self->{req_h}) {
405 $self->{req_w} = $w; 437 $self->{req_w} = $w;
406 $self->{req_h} = $h; 438 $self->{req_h} = $h;
407 439
408 $self->{parent}->check_size 440 $self->{parent}->_check_size
409 or $self->size_allocate ( 441 or $self->size_allocate (
410 (List::Util::max $self->{w}, $w), 442 (List::Util::max $self->{w}, $w),
411 (List::Util::max $self->{h}, $h), 443 (List::Util::max $self->{h}, $h),
412 ); 444 );
413 445
418} 450}
419 451
420sub update { 452sub update {
421 my ($self) = @_; 453 my ($self) = @_;
422 454
423 $self->{parent}->update 455 my $update = $CFClient::UI::ROOT->{_update} ||= {};
424 if $self->{parent}; 456
457 $update->{$self} = $self;
458
459 $CFClient::UI::ROOT->on_refresh (_update => sub {
460 while (%$update) {
461 my @widgets = values %$update;
462 %$update = ();
463
464 $_->{parent} && $_->{parent}->update
465 for @widgets;
466 }
467 });
425} 468}
426 469
427sub connect { 470sub connect {
428 my ($self, $signal, $cb) = @_; 471 my ($self, $signal, $cb) = @_;
429 472
519 562
520 $self 563 $self
521} 564}
522 565
523sub add { 566sub add {
524 my ($self, $child) = @_; 567 my ($self, @widgets) = @_;
525 568
526 $child->set_parent ($self); 569 $_->set_parent ($self)
570 for @widgets;
527 571
528 use sort 'stable'; 572 use sort 'stable';
529 573
530 $self->{children} = [ 574 $self->{children} = [
531 sort { $a->{z} <=> $b->{z} } 575 sort { $a->{z} <=> $b->{z} }
532 @{$self->{children}}, $child 576 @{$self->{children}}, @widgets
533 ]; 577 ];
534 578
535 $child->check_size; 579 $_->check_size
580 for @widgets;
581
582 $self->update;
536} 583}
537 584
538sub children { 585sub children {
539 @{ $_[0]{children} } 586 @{ $_[0]{children} }
540} 587}
559 606
560 for (@$children) { 607 for (@$children) {
561 delete $_->{parent}; 608 delete $_->{parent};
562 $_->hide; 609 $_->hide;
563 } 610 }
611
612 $self->check_size;
613 $self->update;
564} 614}
565 615
566sub find_widget { 616sub find_widget {
567 my ($self, $x, $y) = @_; 617 my ($self, $x, $y) = @_;
568 618
643} 693}
644 694
645sub update { 695sub update {
646 my ($self) = @_; 696 my ($self) = @_;
647 697
648 # we want to do this delayed... 698 $ROOT->on_refresh ($self => sub { $self->render_child });
649 $self->render_chld;
650 $self->SUPER::update; 699 $self->SUPER::update;
651}
652
653sub render_chld {
654 my ($self) = @_;
655
656 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
657 glClearColor 0, 0, 0, 1;
658 glClear GL_COLOR_BUFFER_BIT;
659 $self->child->draw;
660 };
661} 700}
662 701
663sub size_allocate { 702sub size_allocate {
664 my ($self, $w, $h) = @_; 703 my ($self, $w, $h) = @_;
665 704
666 $self->child->configure (0, 0, $w, $h); 705 $self->SUPER::size_allocate ($w, $h);
706 $self->update;
707}
667 708
709sub _render {
710 $_[0]{children}[0]->draw;
711}
712
713sub render_child {
714 my ($self) = @_;
715
716 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
717 glClearColor 0, 0, 0, 0;
718 glClear GL_COLOR_BUFFER_BIT;
719
668 $self->render_chld; 720 $self->_render;
721# glColorMask 1, 1, 1, 0;
722# glEnable GL_BLEND;
723# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
724# glRasterPos 0, 0;
725# glCopyPixels 0, 0, $self->{w}, $self->{h};
726# glDisable GL_BLEND;
727# glColorMask 1, 1, 1, 1;
728 };
669} 729}
670 730
671sub _draw { 731sub _draw {
672 my ($self) = @_; 732 my ($self) = @_;
673 733
675 735
676 my $tex = $self->{texture} 736 my $tex = $self->{texture}
677 or return; 737 or return;
678 738
679 glEnable GL_BLEND; 739 glEnable GL_BLEND;
680 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 740 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
681 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
682 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
743 glColor 0, 0, 0, 1;
683 744
684 $tex->draw_quad (0, 0, $w, $h); 745 $tex->draw_quad (0, 0, $w, $h);
685 746
686 glDisable GL_BLEND; 747 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 748 glDisable GL_TEXTURE_2D;
691 752
692package CFClient::UI::ViewPort; 753package CFClient::UI::ViewPort;
693 754
694our @ISA = CFClient::UI::Window::; 755our @ISA = CFClient::UI::Window::;
695 756
696sub new { die }
697
698sub size_request { 757sub size_request {
699 my ($self) = @_; 758 my ($self) = @_;
700 759
701 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 760 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
702 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 761 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
703 762
704 @$self{qw(child_w child_h)} 763 @$self{qw(child_w child_h)}
705} 764}
706 765
707sub _draw { 766sub size_allocate {
708 my ($self) = @_; 767 my ($self, $w, $h) = @_;
709 768
710 $self->{children}[1]->draw; 769 $self->update;
711} 770}
712 771
772sub set_offset {
773 my ($self, $x, $y) = @_;
774
775 $self->{view_x} = int $x;
776 $self->{view_y} = int $y;
777
778 $self->update;
779}
780
781# hmm, this does not work for topleft of $self... but we should not aks for that
782sub _topleft {
783 my ($self, $x, $y) = @_;
784
785 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y})
786}
787
788sub find_widget {
789 my ($self, $x, $y) = @_;
790
791 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
792 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
793 ) {
794 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
795 } else {
796 $self->CFClient::UI::Base::find_widget ($x, $y)
797 }
798}
799
800sub _render {
801 my ($self) = @_;
802
803 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
804
805 $self->SUPER::_render;
806}
807
808#############################################################################
809
810package CFClient::UI::ScrolledWindow;
811
812our @ISA = CFClient::UI::HBox::;
813
814sub new {
815 my $class = shift;
816
817 my $self;
818
819 my $slider = new CFClient::UI::Slider
820 vertical => 1,
821 range => [0, 0, 1, 0.01], # HACK fix
822 connect_changed => sub {
823 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
824 },
825 ;
826
827 $self = $class->SUPER::new (
828 vp => (new CFClient::UI::ViewPort),
829 slider => $slider,
830 @_,
831 );
832
833 $self->{vp}->add ($self->{scrolled});
834 $self->add ($self->{vp});
835 $self->add ($self->{slider});
836
837 $self
838}
839
840#TODO# update range on size_allocate depeneing on child
841# update viewport offset on scroll
713 842
714############################################################################# 843#############################################################################
715 844
716package CFClient::UI::Frame; 845package CFClient::UI::Frame;
717 846
774 # TODO: user_x, user_y, overwrite moveto? 903 # TODO: user_x, user_y, overwrite moveto?
775 904
776 my $self = $class->SUPER::new ( 905 my $self = $class->SUPER::new (
777 bg => [1, 1, 1, 1], 906 bg => [1, 1, 1, 1],
778 border_bg => [1, 1, 1, 1], 907 border_bg => [1, 1, 1, 1],
779 border => 0.8, 908 border => 0.6,
780 can_events => 1, 909 can_events => 1,
781 @_ 910 @_
782 ); 911 );
783 912
784 $self->{title} &&= new CFClient::UI::Label 913 $self->{title} &&= new CFClient::UI::Label
785 align => 0, 914 align => 0,
786 valign => 1, 915 valign => 1,
787 text => $self->{title}, 916 text => $self->{title},
788 fontsize => 1; 917 fontsize => $self->{border};
789 918
790 $self 919 $self
791} 920}
792 921
793sub border { 922sub border {
818} 947}
819 948
820sub button_down { 949sub button_down {
821 my ($self, $ev, $x, $y) = @_; 950 my ($self, $ev, $x, $y) = @_;
822 951
952 my ($w, $h) = @$self{qw(w h)};
823 my $border = $self->border; 953 my $border = $self->border;
824 954
825 if ($x < $self->{w} && $x >= $self->{w} - $border 955 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
826 && $y < $self->{h} && $y >= $self->{h} - $border) { 956 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
827 957
958 if ($lr & $td) {
959 my ($wx, $wy) = ($self->{x}, $self->{y});
828 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 960 my ($ox, $oy) = ($ev->{x}, $ev->{y});
829 my ($bw, $bh) = ($self->{w}, $self->{h}); 961 my ($bw, $bh) = ($self->{w}, $self->{h});
830 962
963 my $mx = $x < $border;
964 my $my = $y < $border;
965
831 $self->{motion} = sub { 966 $self->{motion} = sub {
832 my ($ev, $x, $y) = @_; 967 my ($ev, $x, $y) = @_;
833 968
834 ($x, $y) = ($ev->{x}, $ev->{y}); 969 my $dx = $ev->{x} - $ox;
970 my $dy = $ev->{y} - $oy;
835 971
836 $self->{user_w} = $bw + $x - $ox; 972 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
837 $self->{user_h} = $bh + $y - $oy; 973 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
974 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
838 $self->check_size; 975 $self->check_size;
839 }; 976 };
840 977
841 } elsif ($x >= 0 && $x < $self->{w} 978 } elsif ($lr ^ $td) {
842 && $y >= 0 && $y < $border) {
843
844 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 979 my ($ox, $oy) = ($ev->{x}, $ev->{y});
845 my ($bx, $by) = ($self->{x}, $self->{y}); 980 my ($bx, $by) = ($self->{x}, $self->{y});
846 981
847 $self->{motion} = sub { 982 $self->{motion} = sub {
848 my ($ev, $x, $y) = @_; 983 my ($ev, $x, $y) = @_;
884 $tex[1]->draw_quad (0, 0, $w, $border); 1019 $tex[1]->draw_quad (0, 0, $w, $border);
885 $tex[3]->draw_quad (0, $border, $border, $ch); 1020 $tex[3]->draw_quad (0, $border, $border, $ch);
886 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 1021 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
887 $tex[4]->draw_quad (0, $h - $border, $w, $border); 1022 $tex[4]->draw_quad (0, $h - $border, $w, $border);
888 1023
1024 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
889 my $bg = $tex[0]; 1025 my $bg = $tex[0];
890 1026
891 # TODO: repeat texture not scale 1027 # TODO: repeat texture not scale
892 my $rep_x = $cw / $bg->{w}; 1028 my $rep_x = $cw / $bg->{w};
893 my $rep_y = $ch / $bg->{h}; 1029 my $rep_y = $ch / $bg->{h};
894 1030
895 glColor @{ $self->{bg} }; 1031 glColor @{ $self->{bg} };
896 1032
897 $bg->{s} = $rep_x; 1033 $bg->{s} = $rep_x;
898 $bg->{t} = $rep_y; 1034 $bg->{t} = $rep_y;
899 $bg->{wrap_mode} = 1; 1035 $bg->{wrap_mode} = 1;
900 $bg->draw_quad ($border, $border, $cw, $ch); 1036 $bg->draw_quad ($border, $border, $cw, $ch);
901 1037
902 glDisable GL_TEXTURE_2D; 1038 glDisable GL_TEXTURE_2D;
903 glDisable GL_BLEND; 1039 glDisable GL_BLEND;
1040 }
904 1041
905 $self->{title}->draw if $self->{title}; 1042 $self->{title}->draw if $self->{title};
1043
906 $self->child->draw; 1044 $self->child->draw;
907} 1045}
908 1046
909############################################################################# 1047#############################################################################
910 1048
1221 1359
1222 delete $self->{texture}; 1360 delete $self->{texture};
1223 $self->SUPER::update; 1361 $self->SUPER::update;
1224} 1362}
1225 1363
1226sub reconfigure {
1227 my ($self) = @_;
1228
1229 delete $self->{texture};
1230}
1231
1232sub set_text { 1364sub set_text {
1233 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1234 1366
1235 return if $self->{text} eq "T$text"; 1367 return if $self->{text} eq "T$text";
1236 $self->{text} = "T$text"; 1368 $self->{text} = "T$text";
1289sub set_fontsize { 1421sub set_fontsize {
1290 my ($self, $fontsize) = @_; 1422 my ($self, $fontsize) = @_;
1291 1423
1292 $self->{fontsize} = $fontsize; 1424 $self->{fontsize} = $fontsize;
1293 delete $self->{texture}; 1425 delete $self->{texture};
1426
1427 $self->update;
1294 $self->check_size; 1428 $self->check_size;
1295 $self->update;
1296} 1429}
1297 1430
1298sub _draw { 1431sub _draw {
1299 my ($self) = @_; 1432 my ($self) = @_;
1300 1433
1934 # TODO: calculations are off 2067 # TODO: calculations are off
1935 my $self = $class->SUPER::new ( 2068 my $self = $class->SUPER::new (
1936 fg => [1, 1, 1], 2069 fg => [1, 1, 1],
1937 active_fg => [0, 0, 0], 2070 active_fg => [0, 0, 0],
1938 range => [0, 0, 100, 10], 2071 range => [0, 0, 100, 10],
1939 req_w => 20, 2072 req_w => $::WIDTH / 80,
1940 req_h => 20, 2073 req_h => $::WIDTH / 80,
1941 vertical => 0, 2074 vertical => 0,
1942 can_hover => 1, 2075 can_hover => 1,
1943 inner_pad => 5, 2076 inner_pad => 5,
1944 @_ 2077 @_
1945 ); 2078 );
2069 (new CFClient::UI::Empty expand => 1), 2202 (new CFClient::UI::Empty expand => 1),
2070 (new CFClient::UI::Slider vertical => 1), 2203 (new CFClient::UI::Slider vertical => 1),
2071 ], 2204 ],
2072 ); 2205 );
2073 2206
2074 $self->{children}[1]->connect (changed => sub { 2207 $self->{children}[1]->connect (changed => sub { $self->update });
2075 $self->update;
2076 });
2077 2208
2078 $self 2209 $self
2079} 2210}
2080 2211
2081sub set_fontsize { 2212sub set_fontsize {
2089 my ($self, $text) = @_; 2220 my ($self, $text) = @_;
2090 2221
2091 my $layout = $self->{layout}; 2222 my $layout = $self->{layout};
2092 2223
2093 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2224 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2094 $layout->set_width ($self->{w}); 2225 $layout->set_width ($self->{children}[0]{w});
2095 $layout->set_text ($text); 2226 $layout->set_text ($text);
2096 2227
2097 ($layout->size)[1] 2228 ($layout->size)[1]
2098} 2229}
2099 2230
2152 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2283 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2153 2284
2154 delete $self->{texture}; 2285 delete $self->{texture};
2155 } 2286 }
2156 2287
2157 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2288 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2158 glClearColor 0, 0, 0, 1; 2289 glClearColor 0, 0, 0, 0;
2159 glClear GL_COLOR_BUFFER_BIT; 2290 glClear GL_COLOR_BUFFER_BIT;
2160 2291
2161 glEnable GL_BLEND; 2292 glEnable GL_BLEND;
2162 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2163 glEnable GL_TEXTURE_2D; 2294 glEnable GL_TEXTURE_2D;
2195} 2326}
2196 2327
2197sub _draw { 2328sub _draw {
2198 my ($self) = @_; 2329 my ($self) = @_;
2199 2330
2200 if ($self->{texture}) { 2331 glEnable GL_BLEND;
2332 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2201 glEnable GL_TEXTURE_2D; 2333 glEnable GL_TEXTURE_2D;
2202 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2203 glColor 1, 1, 1, 1; 2335 glColor 1, 1, 1, 1;
2204 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2336 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2205 glDisable GL_TEXTURE_2D; 2337 glDisable GL_TEXTURE_2D;
2206 } 2338 glDisable GL_BLEND;
2207 2339
2208 $self->{children}[1]->draw; 2340 $self->{children}[1]->draw;
2209 2341
2210} 2342}
2211 2343
2380 2512
2381sub size_request { 2513sub size_request {
2382 (32, 8) 2514 (32, 8)
2383} 2515}
2384 2516
2385sub draw { 2517sub _draw {
2386 my ($self) = @_; 2518 my ($self) = @_;
2387 2519
2520 return unless $::CONN;#d# manage and cache textures differently
2388 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2521 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2389 2522
2523 # TODO animation
2390 if ($tex) { 2524 if ($tex) {
2391 glEnable GL_BLEND; 2525 glEnable GL_BLEND;
2392 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2526 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2393 glEnable GL_TEXTURE_2D; 2527 glEnable GL_TEXTURE_2D;
2394 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2528 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2399 } 2533 }
2400} 2534}
2401 2535
2402############################################################################# 2536#############################################################################
2403 2537
2538package CFClient::UI::InventoryItem;
2539
2540our @ISA = CFClient::UI::HBox::;
2541
2542sub new {
2543 my $class = shift;
2544
2545 my %args = @_;
2546
2547 my $item = $args{item};
2548
2549 my $desc = $item->{nrof} < 2
2550 ? $item->{name}
2551 : "$item->{nrof} $item->{name_pl}";
2552
2553
2554 my $self = $class->SUPER::new (
2555 can_hover => 1,
2556 can_events => 1,
2557 tooltip => (CFClient::UI::Label->escape ($desc)
2558 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2559 connect_button_down => sub {
2560 my ($self, $ev, $x, $y) = @_;
2561
2562 # todo: maybe put examine on 1? but should just be a tooltip :(
2563 if ($ev->{button} == 1) {
2564 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2565 } elsif ($ev->{button} == 2) {
2566 $::CONN->send ("apply $item->{tag}");
2567 } elsif ($ev->{button} == 3) {
2568 CFClient::UI::Menu->new (
2569 items => [
2570 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2571 [
2572 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2573 sub { $::CONN->send ("lock $item->{tag}") },
2574 ],
2575 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2576 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2577 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2578 ],
2579 )->popup ($ev);
2580 }
2581
2582 1
2583 },
2584 %args
2585 );
2586 $self->add(new CFClient::UI::Face
2587 can_events => 0,
2588 face => $item->{face},
2589 anim => $item->{anim},
2590 animspeed => $item->{animspeed});
2591 $self->add(new CFClient::UI::Label
2592 can_events => 0,
2593 text => $desc);
2594
2595 $self
2596}
2597
2598#############################################################################
2599
2600package CFClient::UI::Inventory;
2601
2602our @ISA = CFClient::UI::ScrolledWindow::;
2603
2604sub new {
2605 my $class = shift;
2606
2607 my $self = $class->SUPER::new (
2608 scrolled => (new CFClient::UI::VBox),
2609 @_,
2610 );
2611
2612 $self
2613}
2614
2615sub set_items {
2616 my ($self, $items) = @_;
2617
2618 $self->{scrolled}->clear;
2619 return unless $items;
2620
2621 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2622
2623 $self->{real_items} = \@items;
2624
2625 for my $item (@items) {
2626 my $desc = $item->{nrof} < 2
2627 ? $item->{name}
2628 : "$item->{nrof} $item->{name_pl}";
2629
2630 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2631 }
2632
2633# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2634}
2635
2636sub size_request {
2637 my ($self) = @_;
2638 ($self->{req_w}, $self->{req_h});
2639}
2640
2641#############################################################################
2642
2643package CFClient::UI::Menu;
2644
2645our @ISA = CFClient::UI::FancyFrame::;
2646
2647use CFClient::OpenGL;
2648
2649sub new {
2650 my $class = shift;
2651
2652 my $self = $class->SUPER::new (
2653 items => [],
2654 z => 100,
2655 @_,
2656 );
2657
2658 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2659
2660 for my $item (@{ $self->{items} }) {
2661 my ($widget, $cb) = @$item;
2662
2663 # handle various types of items, only text for now
2664 if (!ref $widget) {
2665 $widget = new CFClient::UI::Label
2666 can_hover => 1,
2667 can_events => 1,
2668 text => $widget;
2669 }
2670
2671 $self->{item}{$widget} = $item;
2672
2673 $self->{vbox}->add ($widget);
2674 }
2675
2676 $self
2677}
2678
2679# popup given the event (must be a mouse button down event currently)
2680sub popup {
2681 my ($self, $ev) = @_;
2682
2683 $self->emit ("popdown");
2684
2685 # maybe save $GRAB? must be careful about events...
2686 $GRAB = $self;
2687 $self->{button} = $ev->{button};
2688
2689 $self->show;
2690 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2691}
2692
2693sub mouse_motion {
2694 my ($self, $ev, $x, $y) = @_;
2695
2696 # TODO: should use vbox->find_widget or so
2697 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2698 $self->{hover} = $self->{item}{$HOVER};
2699}
2700
2701sub button_up {
2702 my ($self, $ev, $x, $y) = @_;
2703
2704 if ($ev->{button} == $self->{button}) {
2705 undef $GRAB;
2706 $self->hide;
2707
2708 $self->emit ("popdown");
2709 $self->{hover}[1]->() if $self->{hover};
2710 }
2711}
2712
2713#############################################################################
2714
2404package CFClient::UI::Root; 2715package CFClient::UI::Root;
2405 2716
2406our @ISA = CFClient::UI::Container::; 2717our @ISA = CFClient::UI::Container::;
2407 2718
2408use CFClient::OpenGL; 2719use CFClient::OpenGL;
2409 2720
2410sub check_size { 2721sub check_size {
2411 my ($self) = @_; 2722 my ($self) = @_;
2412 2723
2413 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2724 $self->configure (0, 0, $self->{w}, $self->{h});
2414} 2725}
2415 2726
2416sub size_request { 2727sub size_request {
2417 ($::WIDTH, $::HEIGHT) 2728 my ($self) = @_;
2729
2730 ($self->{w}, $self->{h})
2731}
2732
2733sub size_allocate {
2734 my ($self, $w, $h) = @_;
2735
2736 my $old_w = $self->{old_w};
2737 my $old_h = $self->{old_h};
2738
2739 if ($old_w && $old_h) {
2740 for my $child ($self->children) {
2741 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2742 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2743 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2744 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2745 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2746 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2747 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2748 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2749 }
2750 }
2751
2752 $self->{old_w} = $w;
2753 $self->{old_h} = $h;
2418} 2754}
2419 2755
2420sub configure { 2756sub configure {
2421 my ($self, $x, $y, $w, $h) = @_; 2757 my ($self, $x, $y, $w, $h) = @_;
2422 2758
2423 $self->SUPER::configure ($x, $y, $w, $h); 2759 $self->SUPER::configure ($x, $y, $w, $h);
2424 2760
2425 for my $child (@{$self->{children}}) { 2761 for my $child ($self->children) {
2426 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2762 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2427 2763
2428 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2764 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2429 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2765 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2430 $child->configure ($X, $Y, $W,$H); 2766 $child->configure ($X, $Y, $W, $H);
2431 } 2767 }
2432} 2768}
2433 2769
2434sub _topleft { 2770sub _topleft {
2435 my ($self, $x, $y) = @_; 2771 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines