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.172 by root, Tue Apr 25 09:52:04 2006 UTC vs.
Revision 1.183 by elmex, Sun Apr 30 13:10:46 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 {
251}
252
253# call when resolution changes etc.
254sub reconfigure {
255 my ($self) = @_;
256
257 $_->reconfigure
258 for $self->children;
259
260 $_->check_size;
242} 261}
243 262
244sub set_max_size { 263sub set_max_size {
245 my ($self, $w, $h) = @_; 264 my ($self, $w, $h) = @_;
246 265
521 sort { $a->{z} <=> $b->{z} } 540 sort { $a->{z} <=> $b->{z} }
522 @{$self->{children}}, $child 541 @{$self->{children}}, $child
523 ]; 542 ];
524 543
525 $child->check_size; 544 $child->check_size;
545 $self->update;
526} 546}
527 547
528sub children { 548sub children {
529 @{ $_[0]{children} } 549 @{ $_[0]{children} }
530} 550}
549 569
550 for (@$children) { 570 for (@$children) {
551 delete $_->{parent}; 571 delete $_->{parent};
552 $_->hide; 572 $_->hide;
553 } 573 }
574
575 $self->check_size;
576 $self->update;
554} 577}
555 578
556sub find_widget { 579sub find_widget {
557 my ($self, $x, $y) = @_; 580 my ($self, $x, $y) = @_;
558 581
633} 656}
634 657
635sub update { 658sub update {
636 my ($self) = @_; 659 my ($self) = @_;
637 660
638 # we want to do this delayed... 661 $ROOT->on_refresh ($self => sub { $self->render_child });
639 $self->render_chld;
640 $self->SUPER::update; 662 $self->SUPER::update;
641}
642
643sub render_chld {
644 my ($self) = @_;
645
646 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
647 glClearColor 0, 0, 0, 1;
648 glClear GL_COLOR_BUFFER_BIT;
649 $self->child->draw;
650 };
651} 663}
652 664
653sub size_allocate { 665sub size_allocate {
654 my ($self, $w, $h) = @_; 666 my ($self, $w, $h) = @_;
655 667
656 $self->child->configure (0, 0, $w, $h); 668 $self->SUPER::size_allocate ($w, $h);
669 $self->update;
670}
657 671
672sub _render {
673 $_[0]{children}[0]->draw;
674}
675
676sub render_child {
677 my ($self) = @_;
678
679 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
680 glClearColor 0, 0, 0, 0;
681 glClear GL_COLOR_BUFFER_BIT;
682
658 $self->render_chld; 683 $self->_render;
684# glColorMask 1, 1, 1, 0;
685# glEnable GL_BLEND;
686# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
687# glRasterPos 0, 0;
688# glCopyPixels 0, 0, $self->{w}, $self->{h};
689# glDisable GL_BLEND;
690# glColorMask 1, 1, 1, 1;
691 };
659} 692}
660 693
661sub _draw { 694sub _draw {
662 my ($self) = @_; 695 my ($self) = @_;
663 696
665 698
666 my $tex = $self->{texture} 699 my $tex = $self->{texture}
667 or return; 700 or return;
668 701
669 glEnable GL_BLEND; 702 glEnable GL_BLEND;
670 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 703 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
671 glEnable GL_TEXTURE_2D; 704 glEnable GL_TEXTURE_2D;
672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
706 glColor 0, 0, 0, 1;
673 707
674 $tex->draw_quad (0, 0, $w, $h); 708 $tex->draw_quad (0, 0, $w, $h);
675 709
676 glDisable GL_BLEND; 710 glDisable GL_BLEND;
677 glDisable GL_TEXTURE_2D; 711 glDisable GL_TEXTURE_2D;
681 715
682package CFClient::UI::ViewPort; 716package CFClient::UI::ViewPort;
683 717
684our @ISA = CFClient::UI::Window::; 718our @ISA = CFClient::UI::Window::;
685 719
686sub new { die }
687
688sub size_request { 720sub size_request {
689 my ($self) = @_; 721 my ($self) = @_;
690 722
691 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 723 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
692 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 724 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
693 725
694 @$self{qw(child_w child_h)} 726 @$self{qw(child_w child_h)}
695} 727}
696 728
697sub _draw { 729sub size_allocate {
698 my ($self) = @_; 730 my ($self, $w, $h) = @_;
699 731
700 $self->{children}[1]->draw; 732 $self->update;
701} 733}
702 734
735sub set_offset {
736 my ($self, $x, $y) = @_;
737
738 $self->{view_x} = int $x;
739 $self->{view_y} = int $y;
740
741 $self->update;
742}
743
744sub _render {
745 my ($self) = @_;
746
747 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
748
749 $self->SUPER::_render;
750}
751
752#############################################################################
753
754package CFClient::UI::ScrolledWindow;
755
756our @ISA = CFClient::UI::HBox::;
757
758sub new {
759 my $class = shift;
760
761 my $self;
762
763 my $slider = new CFClient::UI::Slider
764 vertical => 1,
765 range => [0, 0, 1, 0.01], # HACK fix
766 connect_changed => sub {
767 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
768 },
769 ;
770
771 $self = $class->SUPER::new (
772 vp => (new CFClient::UI::ViewPort),
773 slider => $slider,
774 @_,
775 );
776
777 $self->{vp}->add ($self->{scrolled});
778 $self->add ($self->{vp});
779 $self->add ($self->{slider});
780
781 $self
782}
783
784#TODO# update range on size_allocate depeneing on child
785# update viewport offset on scroll
703 786
704############################################################################# 787#############################################################################
705 788
706package CFClient::UI::Frame; 789package CFClient::UI::Frame;
707 790
764 # TODO: user_x, user_y, overwrite moveto? 847 # TODO: user_x, user_y, overwrite moveto?
765 848
766 my $self = $class->SUPER::new ( 849 my $self = $class->SUPER::new (
767 bg => [1, 1, 1, 1], 850 bg => [1, 1, 1, 1],
768 border_bg => [1, 1, 1, 1], 851 border_bg => [1, 1, 1, 1],
769 border => 0.8, 852 border => 0.6,
770 can_events => 1, 853 can_events => 1,
771 @_ 854 @_
772 ); 855 );
773 856
774 $self->{title} &&= new CFClient::UI::Label 857 $self->{title} &&= new CFClient::UI::Label
775 align => 0, 858 align => 0,
776 valign => 1, 859 valign => 1,
777 text => $self->{title}, 860 text => $self->{title},
778 fontsize => 1; 861 fontsize => $self->{border};
779 862
780 $self 863 $self
781} 864}
782 865
783sub border { 866sub border {
808} 891}
809 892
810sub button_down { 893sub button_down {
811 my ($self, $ev, $x, $y) = @_; 894 my ($self, $ev, $x, $y) = @_;
812 895
896 my ($w, $h) = @$self{qw(w h)};
813 my $border = $self->border; 897 my $border = $self->border;
814 898
815 if ($x < $self->{w} && $x >= $self->{w} - $border 899 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
816 && $y < $self->{h} && $y >= $self->{h} - $border) { 900 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
817 901
902 if ($lr & $td) {
903 my ($wx, $wy) = ($self->{x}, $self->{y});
818 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 904 my ($ox, $oy) = ($ev->{x}, $ev->{y});
819 my ($bw, $bh) = ($self->{w}, $self->{h}); 905 my ($bw, $bh) = ($self->{w}, $self->{h});
820 906
907 my $mx = $x < $border;
908 my $my = $y < $border;
909
821 $self->{motion} = sub { 910 $self->{motion} = sub {
822 my ($ev, $x, $y) = @_; 911 my ($ev, $x, $y) = @_;
823 912
824 ($x, $y) = ($ev->{x}, $ev->{y}); 913 my $dx = $ev->{x} - $ox;
914 my $dy = $ev->{y} - $oy;
825 915
826 $self->{user_w} = $bw + $x - $ox; 916 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
827 $self->{user_h} = $bh + $y - $oy; 917 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
918 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
828 $self->check_size; 919 $self->check_size;
829 }; 920 };
830 921
831 } elsif ($x >= 0 && $x < $self->{w} 922 } elsif ($lr ^ $td) {
832 && $y >= 0 && $y < $border) {
833
834 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 923 my ($ox, $oy) = ($ev->{x}, $ev->{y});
835 my ($bx, $by) = ($self->{x}, $self->{y}); 924 my ($bx, $by) = ($self->{x}, $self->{y});
836 925
837 $self->{motion} = sub { 926 $self->{motion} = sub {
838 my ($ev, $x, $y) = @_; 927 my ($ev, $x, $y) = @_;
874 $tex[1]->draw_quad (0, 0, $w, $border); 963 $tex[1]->draw_quad (0, 0, $w, $border);
875 $tex[3]->draw_quad (0, $border, $border, $ch); 964 $tex[3]->draw_quad (0, $border, $border, $ch);
876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 965 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
877 $tex[4]->draw_quad (0, $h - $border, $w, $border); 966 $tex[4]->draw_quad (0, $h - $border, $w, $border);
878 967
968 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
879 my $bg = $tex[0]; 969 my $bg = $tex[0];
880 970
881 # TODO: repeat texture not scale 971 # TODO: repeat texture not scale
882 my $rep_x = $cw / $bg->{w}; 972 my $rep_x = $cw / $bg->{w};
883 my $rep_y = $ch / $bg->{h}; 973 my $rep_y = $ch / $bg->{h};
884 974
885 glColor @{ $self->{bg} }; 975 glColor @{ $self->{bg} };
886 976
887 $bg->{s} = $rep_x; 977 $bg->{s} = $rep_x;
888 $bg->{t} = $rep_y; 978 $bg->{t} = $rep_y;
889 $bg->{wrap_mode} = 1; 979 $bg->{wrap_mode} = 1;
890 $bg->draw_quad ($border, $border, $cw, $ch); 980 $bg->draw_quad ($border, $border, $cw, $ch);
891 981
892 glDisable GL_TEXTURE_2D; 982 glDisable GL_TEXTURE_2D;
893 glDisable GL_BLEND; 983 glDisable GL_BLEND;
984 }
894 985
895 $self->{title}->draw if $self->{title}; 986 $self->{title}->draw if $self->{title};
987
896 $self->child->draw; 988 $self->child->draw;
897} 989}
898 990
899############################################################################# 991#############################################################################
900 992
1204 s/</&lt;/g; 1296 s/</&lt;/g;
1205 1297
1206 $_[1] 1298 $_[1]
1207} 1299}
1208 1300
1301sub update {
1302 my ($self) = @_;
1303
1304 delete $self->{texture};
1305 $self->SUPER::update;
1306}
1307
1308sub reconfigure {
1309 my ($self) = @_;
1310
1311 delete $self->{texture};
1312}
1313
1209sub set_text { 1314sub set_text {
1210 my ($self, $text) = @_; 1315 my ($self, $text) = @_;
1211 1316
1317 return if $self->{text} eq "T$text";
1318 $self->{text} = "T$text";
1319
1212 $self->{layout}->set_text ($text); 1320 $self->{layout}->set_text ($text);
1213 1321
1214 delete $self->{texture}; 1322 delete $self->{texture};
1323 $self->update;
1215 $self->check_size; 1324 $self->check_size;
1216 $self->update;
1217} 1325}
1218 1326
1219sub set_markup { 1327sub set_markup {
1220 my ($self, $markup) = @_; 1328 my ($self, $markup) = @_;
1221 1329
1330 return if $self->{text} eq "M$markup";
1331 $self->{text} = "M$markup";
1332
1222 $self->{layout}->set_markup ($markup); 1333 $self->{layout}->set_markup ($markup);
1223 1334
1224 delete $self->{texture}; 1335 delete $self->{texture};
1336 $self->update;
1225 $self->check_size; 1337 $self->check_size;
1226 $self->update;
1227} 1338}
1228 1339
1229sub size_request { 1340sub size_request {
1230 my ($self) = @_; 1341 my ($self) = @_;
1231 1342
1755} 1866}
1756 1867
1757sub set_max { 1868sub set_max {
1758 my ($self, $max) = @_; 1869 my ($self, $max) = @_;
1759 1870
1871 return if $self->{max_val} == $max;
1872
1760 $self->{max_val} = $max; 1873 $self->{max_val} = $max;
1874 $self->update;
1761} 1875}
1762 1876
1763sub set_value { 1877sub set_value {
1764 my ($self, $val, $max) = @_; 1878 my ($self, $val, $max) = @_;
1765 1879
1766 $self->set_max ($max) 1880 $self->set_max ($max)
1767 if defined $max; 1881 if defined $max;
1768 1882
1769 $max = $self->{max_val}; 1883 return if $self->{val} == $val;
1884
1770 $self->{val} = $val; 1885 $self->{val} = $val;
1771
1772 $self->update; 1886 $self->update;
1773} 1887}
1774 1888
1775sub _draw { 1889sub _draw {
1776 my ($self) = @_; 1890 my ($self) = @_;
1859 1973
1860 $self->{value}->set_fontsize ($fsize); 1974 $self->{value}->set_fontsize ($fsize);
1861 $self->{max} ->set_fontsize ($fsize); 1975 $self->{max} ->set_fontsize ($fsize);
1862} 1976}
1863 1977
1978sub set_max {
1979 my ($self, $max) = @_;
1980
1981 $self->{gauge}->set_max ($max);
1982 $self->{max}->set_text ($max);
1983}
1984
1864sub set_value { 1985sub set_value {
1865 my ($self, $val, $max) = @_; 1986 my ($self, $val, $max) = @_;
1866 1987
1867 $self->set_max ($max) 1988 $self->set_max ($max)
1868 if defined $max; 1989 if defined $max;
1869 1990
1870 $self->{gauge}->set_value ($val, $max); 1991 $self->{gauge}->set_value ($val, $max);
1871 $self->{value}->set_text ($val); 1992 $self->{value}->set_text ($val);
1872}
1873
1874sub set_max {
1875 my ($self, $max) = @_;
1876
1877 $self->{gauge}->set_max ($max);
1878 $self->{max}->set_text ($max);
1879} 1993}
1880 1994
1881############################################################################# 1995#############################################################################
1882 1996
1883package CFClient::UI::Slider; 1997package CFClient::UI::Slider;
1902 # TODO: calculations are off 2016 # TODO: calculations are off
1903 my $self = $class->SUPER::new ( 2017 my $self = $class->SUPER::new (
1904 fg => [1, 1, 1], 2018 fg => [1, 1, 1],
1905 active_fg => [0, 0, 0], 2019 active_fg => [0, 0, 0],
1906 range => [0, 0, 100, 10], 2020 range => [0, 0, 100, 10],
1907 req_w => 20, 2021 req_w => $::WIDTH / 80,
1908 req_h => 20, 2022 req_h => $::WIDTH / 80,
1909 vertical => 0, 2023 vertical => 0,
1910 can_hover => 1, 2024 can_hover => 1,
1911 inner_pad => 5, 2025 inner_pad => 5,
1912 @_ 2026 @_
1913 ); 2027 );
2037 (new CFClient::UI::Empty expand => 1), 2151 (new CFClient::UI::Empty expand => 1),
2038 (new CFClient::UI::Slider vertical => 1), 2152 (new CFClient::UI::Slider vertical => 1),
2039 ], 2153 ],
2040 ); 2154 );
2041 2155
2042 $self->{children}[1]->connect (changed => sub { 2156 $self->{children}[1]->connect (changed => sub { $self->update });
2043 $self->update;
2044 });
2045 2157
2046 $self 2158 $self
2047} 2159}
2048 2160
2049sub set_fontsize { 2161sub set_fontsize {
2057 my ($self, $text) = @_; 2169 my ($self, $text) = @_;
2058 2170
2059 my $layout = $self->{layout}; 2171 my $layout = $self->{layout};
2060 2172
2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2173 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2062 $layout->set_width ($self->{w}); 2174 $layout->set_width ($self->{children}[0]{w});
2063 $layout->set_text ($text); 2175 $layout->set_text ($text);
2064 2176
2065 ($layout->size)[1] 2177 ($layout->size)[1]
2066} 2178}
2067 2179
2120 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2232 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2121 2233
2122 delete $self->{texture}; 2234 delete $self->{texture};
2123 } 2235 }
2124 2236
2125 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2237 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2126 glClearColor 0, 0, 0, 1; 2238 glClearColor 0, 0, 0, 0;
2127 glClear GL_COLOR_BUFFER_BIT; 2239 glClear GL_COLOR_BUFFER_BIT;
2128 2240
2129 glEnable GL_BLEND; 2241 glEnable GL_BLEND;
2130 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2242 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2131 glEnable GL_TEXTURE_2D; 2243 glEnable GL_TEXTURE_2D;
2163} 2275}
2164 2276
2165sub _draw { 2277sub _draw {
2166 my ($self) = @_; 2278 my ($self) = @_;
2167 2279
2168 if ($self->{texture}) { 2280 glEnable GL_BLEND;
2281 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2169 glEnable GL_TEXTURE_2D; 2282 glEnable GL_TEXTURE_2D;
2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2283 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2171 glColor 1, 1, 1, 1; 2284 glColor 1, 1, 1, 1;
2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2285 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2173 glDisable GL_TEXTURE_2D; 2286 glDisable GL_TEXTURE_2D;
2174 } 2287 glDisable GL_BLEND;
2175 2288
2176 $self->{children}[1]->draw; 2289 $self->{children}[1]->draw;
2177 2290
2178} 2291}
2179 2292
2348 2461
2349sub size_request { 2462sub size_request {
2350 (32, 8) 2463 (32, 8)
2351} 2464}
2352 2465
2353sub draw { 2466sub _draw {
2354 my ($self) = @_; 2467 my ($self) = @_;
2355 2468
2356 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2469 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2357 2470
2471 # TODO animation
2358 if ($tex) { 2472 if ($tex) {
2359 glEnable GL_BLEND; 2473 glEnable GL_BLEND;
2360 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2474 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2361 glEnable GL_TEXTURE_2D; 2475 glEnable GL_TEXTURE_2D;
2362 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2476 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2367 } 2481 }
2368} 2482}
2369 2483
2370############################################################################# 2484#############################################################################
2371 2485
2486package CFClient::UI::Menu;
2487
2488our @ISA = CFClient::UI::FancyFrame::;
2489
2490use CFClient::OpenGL;
2491
2492sub new {
2493 my $class = shift;
2494
2495 my $self = $class->SUPER::new (
2496 items => [],
2497 z => 100,
2498 @_,
2499 );
2500
2501 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2502
2503 for my $item (@{ $self->{items} }) {
2504 my ($widget, $cb) = @$item;
2505
2506 # handle various types of items, only text for now
2507 if (!ref $widget) {
2508 $widget = new CFClient::UI::Label
2509 can_hover => 1,
2510 can_events => 1,
2511 text => $widget;
2512 }
2513
2514 $self->{item}{$widget} = $item;
2515
2516 $self->{vbox}->add ($widget);
2517 }
2518
2519 $self
2520}
2521
2522# popup given the event (must be a mouse button down event currently)
2523sub popup {
2524 my ($self, $ev) = @_;
2525
2526 $self->emit ("popdown");
2527
2528 # maybe save $GRAB? must be careful about events...
2529 $GRAB = $self;
2530 $self->{button} = $ev->{button};
2531
2532 $self->show;
2533 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2534}
2535
2536sub mouse_motion {
2537 my ($self, $ev, $x, $y) = @_;
2538
2539 # TODO: should use vbox->find_widget or so
2540 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2541 $self->{hover} = $self->{item}{$HOVER};
2542}
2543
2544sub button_up {
2545 my ($self, $ev, $x, $y) = @_;
2546
2547 if ($ev->{button} == $self->{button}) {
2548 undef $GRAB;
2549 $self->hide;
2550
2551 $self->emit ("popdown");
2552 $self->{hover}[1]->() if $self->{hover};
2553 }
2554}
2555
2556#############################################################################
2557
2372package CFClient::UI::Root; 2558package CFClient::UI::Root;
2373 2559
2374our @ISA = CFClient::UI::Container::; 2560our @ISA = CFClient::UI::Container::;
2375 2561
2376use CFClient::OpenGL; 2562use CFClient::OpenGL;
2448 $self->_draw; 2634 $self->_draw;
2449} 2635}
2450 2636
2451############################################################################# 2637#############################################################################
2452 2638
2639package CFClient::UI::InventoryItem;
2640
2641our @ISA = CFClient::UI::HBox::;
2642
2643sub new {
2644 my $class = shift;
2645
2646 my %args = @_;
2647
2648 my $item = $args{item};
2649
2650 my $desc = $item->{nrof} < 2
2651 ? $item->{name}
2652 : "$item->{nrof} $item->{name_pl}";
2653
2654
2655 my $self = $class->SUPER::new (
2656 can_hover => 1,
2657 can_events => 1,
2658 tooltip => (CFClient::UI::Label->escape ($desc)
2659 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2660 connect_button_down => sub {
2661 my ($self, $ev, $x, $y) = @_;
2662
2663 # todo: maybe put examine on 1? but should just be a tooltip :(
2664 if ($ev->{button} == 1) {
2665 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2666 } elsif ($ev->{button} == 2) {
2667 $::CONN->send ("apply $item->{tag}");
2668 } elsif ($ev->{button} == 3) {
2669 CFClient::UI::Menu->new (
2670 items => [
2671 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2672 [
2673 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2674 sub { $::CONN->send ("lock $item->{tag}") },
2675 ],
2676 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2677 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2678 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2679 ],
2680 )->popup ($ev);
2681 }
2682
2683 1
2684 },
2685 %args
2686 );
2687 $self->add(new CFClient::UI::Face
2688 can_events => 0,
2689 face => $item->{face},
2690 anim => $item->{anim},
2691 animspeed => $item->{animspeed});
2692 $self->add(new CFClient::UI::Label
2693 can_events => 0,
2694 text => $desc);
2695
2696 $self
2697}
2698
2699#############################################################################
2700
2701package CFClient::UI::Inventory;
2702
2703our @ISA = CFClient::UI::ScrolledWindow::;
2704
2705sub new {
2706 my $class = shift;
2707
2708 my $self = $class->SUPER::new (
2709 scrolled => (new CFClient::UI::VBox),
2710 @_,
2711 );
2712
2713 $self
2714}
2715
2716sub set_items {
2717 my ($self, $items) = @_;
2718
2719 $self->{scrolled}->clear;
2720 return unless $items;
2721
2722 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2723
2724 $self->{real_items} = \@items;
2725
2726 for my $item (@items) {
2727 my $desc = $item->{nrof} < 2
2728 ? $item->{name}
2729 : "$item->{nrof} $item->{name_pl}";
2730
2731 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2732 }
2733
2734# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2735}
2736
2737sub size_request {
2738 my ($self) = @_;
2739 ($self->{req_w}, $self->{req_h});
2740}
2741
2742#############################################################################
2743
2453package CFClient::UI; 2744package CFClient::UI;
2454 2745
2455$ROOT = new CFClient::UI::Root; 2746$ROOT = new CFClient::UI::Root;
2456$TOOLTIP = new CFClient::UI::Tooltip; 2747$TOOLTIP = new CFClient::UI::Tooltip;
2457 2748

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines