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.186 by root, Mon May 8 20:39:34 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 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h});
262 $self->update;
242} 263}
243 264
244sub set_max_size { 265sub set_max_size {
245 my ($self, $w, $h) = @_; 266 my ($self, $w, $h) = @_;
246 267
521 sort { $a->{z} <=> $b->{z} } 542 sort { $a->{z} <=> $b->{z} }
522 @{$self->{children}}, $child 543 @{$self->{children}}, $child
523 ]; 544 ];
524 545
525 $child->check_size; 546 $child->check_size;
547 $self->update;
526} 548}
527 549
528sub children { 550sub children {
529 @{ $_[0]{children} } 551 @{ $_[0]{children} }
530} 552}
549 571
550 for (@$children) { 572 for (@$children) {
551 delete $_->{parent}; 573 delete $_->{parent};
552 $_->hide; 574 $_->hide;
553 } 575 }
576
577 $self->check_size;
578 $self->update;
554} 579}
555 580
556sub find_widget { 581sub find_widget {
557 my ($self, $x, $y) = @_; 582 my ($self, $x, $y) = @_;
558 583
633} 658}
634 659
635sub update { 660sub update {
636 my ($self) = @_; 661 my ($self) = @_;
637 662
638 # we want to do this delayed... 663 $ROOT->on_refresh ($self => sub { $self->render_child });
639 $self->render_chld;
640 $self->SUPER::update; 664 $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} 665}
652 666
653sub size_allocate { 667sub size_allocate {
654 my ($self, $w, $h) = @_; 668 my ($self, $w, $h) = @_;
655 669
656 $self->child->configure (0, 0, $w, $h); 670 $self->SUPER::size_allocate ($w, $h);
671 $self->update;
672}
657 673
674sub _render {
675 $_[0]{children}[0]->draw;
676}
677
678sub render_child {
679 my ($self) = @_;
680
681 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
682 glClearColor 0, 0, 0, 0;
683 glClear GL_COLOR_BUFFER_BIT;
684
658 $self->render_chld; 685 $self->_render;
686# glColorMask 1, 1, 1, 0;
687# glEnable GL_BLEND;
688# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
689# glRasterPos 0, 0;
690# glCopyPixels 0, 0, $self->{w}, $self->{h};
691# glDisable GL_BLEND;
692# glColorMask 1, 1, 1, 1;
693 };
659} 694}
660 695
661sub _draw { 696sub _draw {
662 my ($self) = @_; 697 my ($self) = @_;
663 698
665 700
666 my $tex = $self->{texture} 701 my $tex = $self->{texture}
667 or return; 702 or return;
668 703
669 glEnable GL_BLEND; 704 glEnable GL_BLEND;
670 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
671 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
672 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
708 glColor 0, 0, 0, 1;
673 709
674 $tex->draw_quad (0, 0, $w, $h); 710 $tex->draw_quad (0, 0, $w, $h);
675 711
676 glDisable GL_BLEND; 712 glDisable GL_BLEND;
677 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
681 717
682package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
683 719
684our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
685 721
686sub new { die }
687
688sub size_request { 722sub size_request {
689 my ($self) = @_; 723 my ($self) = @_;
690 724
691 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 725 @$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)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
693 727
694 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
695} 729}
696 730
697sub _draw { 731sub size_allocate {
698 my ($self) = @_; 732 my ($self, $w, $h) = @_;
699 733
700 $self->{children}[1]->draw; 734 $self->update;
701} 735}
702 736
737sub set_offset {
738 my ($self, $x, $y) = @_;
739
740 $self->{view_x} = int $x;
741 $self->{view_y} = int $y;
742
743 $self->update;
744}
745
746# hmm, this does not work for topleft of $self... but we should not aks for that
747sub _topleft {
748 my ($self, $x, $y) = @_;
749
750 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y})
751}
752
753sub find_widget {
754 my ($self, $x, $y) = @_;
755
756 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
757 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
758 ) {
759 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
760 } else {
761 $self->CFClient::UI::Base::find_widget ($x, $y)
762 }
763}
764
765sub _render {
766 my ($self) = @_;
767
768 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
769
770 $self->SUPER::_render;
771}
772
773#############################################################################
774
775package CFClient::UI::ScrolledWindow;
776
777our @ISA = CFClient::UI::HBox::;
778
779sub new {
780 my $class = shift;
781
782 my $self;
783
784 my $slider = new CFClient::UI::Slider
785 vertical => 1,
786 range => [0, 0, 1, 0.01], # HACK fix
787 connect_changed => sub {
788 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
789 },
790 ;
791
792 $self = $class->SUPER::new (
793 vp => (new CFClient::UI::ViewPort),
794 slider => $slider,
795 @_,
796 );
797
798 $self->{vp}->add ($self->{scrolled});
799 $self->add ($self->{vp});
800 $self->add ($self->{slider});
801
802 $self
803}
804
805#TODO# update range on size_allocate depeneing on child
806# update viewport offset on scroll
703 807
704############################################################################# 808#############################################################################
705 809
706package CFClient::UI::Frame; 810package CFClient::UI::Frame;
707 811
764 # TODO: user_x, user_y, overwrite moveto? 868 # TODO: user_x, user_y, overwrite moveto?
765 869
766 my $self = $class->SUPER::new ( 870 my $self = $class->SUPER::new (
767 bg => [1, 1, 1, 1], 871 bg => [1, 1, 1, 1],
768 border_bg => [1, 1, 1, 1], 872 border_bg => [1, 1, 1, 1],
769 border => 0.8, 873 border => 0.6,
770 can_events => 1, 874 can_events => 1,
771 @_ 875 @_
772 ); 876 );
773 877
774 $self->{title} &&= new CFClient::UI::Label 878 $self->{title} &&= new CFClient::UI::Label
775 align => 0, 879 align => 0,
776 valign => 1, 880 valign => 1,
777 text => $self->{title}, 881 text => $self->{title},
778 fontsize => 1; 882 fontsize => $self->{border};
779 883
780 $self 884 $self
781} 885}
782 886
783sub border { 887sub border {
808} 912}
809 913
810sub button_down { 914sub button_down {
811 my ($self, $ev, $x, $y) = @_; 915 my ($self, $ev, $x, $y) = @_;
812 916
917 my ($w, $h) = @$self{qw(w h)};
813 my $border = $self->border; 918 my $border = $self->border;
814 919
815 if ($x < $self->{w} && $x >= $self->{w} - $border 920 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
816 && $y < $self->{h} && $y >= $self->{h} - $border) { 921 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
817 922
923 if ($lr & $td) {
924 my ($wx, $wy) = ($self->{x}, $self->{y});
818 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 925 my ($ox, $oy) = ($ev->{x}, $ev->{y});
819 my ($bw, $bh) = ($self->{w}, $self->{h}); 926 my ($bw, $bh) = ($self->{w}, $self->{h});
820 927
928 my $mx = $x < $border;
929 my $my = $y < $border;
930
821 $self->{motion} = sub { 931 $self->{motion} = sub {
822 my ($ev, $x, $y) = @_; 932 my ($ev, $x, $y) = @_;
823 933
824 ($x, $y) = ($ev->{x}, $ev->{y}); 934 my $dx = $ev->{x} - $ox;
935 my $dy = $ev->{y} - $oy;
825 936
826 $self->{user_w} = $bw + $x - $ox; 937 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
827 $self->{user_h} = $bh + $y - $oy; 938 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
939 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
828 $self->check_size; 940 $self->check_size;
829 }; 941 };
830 942
831 } elsif ($x >= 0 && $x < $self->{w} 943 } elsif ($lr ^ $td) {
832 && $y >= 0 && $y < $border) {
833
834 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 944 my ($ox, $oy) = ($ev->{x}, $ev->{y});
835 my ($bx, $by) = ($self->{x}, $self->{y}); 945 my ($bx, $by) = ($self->{x}, $self->{y});
836 946
837 $self->{motion} = sub { 947 $self->{motion} = sub {
838 my ($ev, $x, $y) = @_; 948 my ($ev, $x, $y) = @_;
874 $tex[1]->draw_quad (0, 0, $w, $border); 984 $tex[1]->draw_quad (0, 0, $w, $border);
875 $tex[3]->draw_quad (0, $border, $border, $ch); 985 $tex[3]->draw_quad (0, $border, $border, $ch);
876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 986 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
877 $tex[4]->draw_quad (0, $h - $border, $w, $border); 987 $tex[4]->draw_quad (0, $h - $border, $w, $border);
878 988
989 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
879 my $bg = $tex[0]; 990 my $bg = $tex[0];
880 991
881 # TODO: repeat texture not scale 992 # TODO: repeat texture not scale
882 my $rep_x = $cw / $bg->{w}; 993 my $rep_x = $cw / $bg->{w};
883 my $rep_y = $ch / $bg->{h}; 994 my $rep_y = $ch / $bg->{h};
884 995
885 glColor @{ $self->{bg} }; 996 glColor @{ $self->{bg} };
886 997
887 $bg->{s} = $rep_x; 998 $bg->{s} = $rep_x;
888 $bg->{t} = $rep_y; 999 $bg->{t} = $rep_y;
889 $bg->{wrap_mode} = 1; 1000 $bg->{wrap_mode} = 1;
890 $bg->draw_quad ($border, $border, $cw, $ch); 1001 $bg->draw_quad ($border, $border, $cw, $ch);
891 1002
892 glDisable GL_TEXTURE_2D; 1003 glDisable GL_TEXTURE_2D;
893 glDisable GL_BLEND; 1004 glDisable GL_BLEND;
1005 }
894 1006
895 $self->{title}->draw if $self->{title}; 1007 $self->{title}->draw if $self->{title};
1008
896 $self->child->draw; 1009 $self->child->draw;
897} 1010}
898 1011
899############################################################################# 1012#############################################################################
900 1013
1204 s/</&lt;/g; 1317 s/</&lt;/g;
1205 1318
1206 $_[1] 1319 $_[1]
1207} 1320}
1208 1321
1322sub update {
1323 my ($self) = @_;
1324
1325 delete $self->{texture};
1326 $self->SUPER::update;
1327}
1328
1209sub set_text { 1329sub set_text {
1210 my ($self, $text) = @_; 1330 my ($self, $text) = @_;
1211 1331
1332 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text";
1334
1212 $self->{layout}->set_text ($text); 1335 $self->{layout}->set_text ($text);
1213 1336
1214 delete $self->{texture}; 1337 delete $self->{texture};
1338 $self->update;
1215 $self->check_size; 1339 $self->check_size;
1216 $self->update;
1217} 1340}
1218 1341
1219sub set_markup { 1342sub set_markup {
1220 my ($self, $markup) = @_; 1343 my ($self, $markup) = @_;
1221 1344
1345 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup";
1347
1222 $self->{layout}->set_markup ($markup); 1348 $self->{layout}->set_markup ($markup);
1223 1349
1224 delete $self->{texture}; 1350 delete $self->{texture};
1351 $self->update;
1225 $self->check_size; 1352 $self->check_size;
1226 $self->update;
1227} 1353}
1228 1354
1229sub size_request { 1355sub size_request {
1230 my ($self) = @_; 1356 my ($self) = @_;
1231 1357
1260sub set_fontsize { 1386sub set_fontsize {
1261 my ($self, $fontsize) = @_; 1387 my ($self, $fontsize) = @_;
1262 1388
1263 $self->{fontsize} = $fontsize; 1389 $self->{fontsize} = $fontsize;
1264 delete $self->{texture}; 1390 delete $self->{texture};
1391
1392 $self->update;
1265 $self->check_size; 1393 $self->check_size;
1266 $self->update;
1267} 1394}
1268 1395
1269sub _draw { 1396sub _draw {
1270 my ($self) = @_; 1397 my ($self) = @_;
1271 1398
1755} 1882}
1756 1883
1757sub set_max { 1884sub set_max {
1758 my ($self, $max) = @_; 1885 my ($self, $max) = @_;
1759 1886
1887 return if $self->{max_val} == $max;
1888
1760 $self->{max_val} = $max; 1889 $self->{max_val} = $max;
1890 $self->update;
1761} 1891}
1762 1892
1763sub set_value { 1893sub set_value {
1764 my ($self, $val, $max) = @_; 1894 my ($self, $val, $max) = @_;
1765 1895
1766 $self->set_max ($max) 1896 $self->set_max ($max)
1767 if defined $max; 1897 if defined $max;
1768 1898
1769 $max = $self->{max_val}; 1899 return if $self->{val} == $val;
1900
1770 $self->{val} = $val; 1901 $self->{val} = $val;
1771
1772 $self->update; 1902 $self->update;
1773} 1903}
1774 1904
1775sub _draw { 1905sub _draw {
1776 my ($self) = @_; 1906 my ($self) = @_;
1859 1989
1860 $self->{value}->set_fontsize ($fsize); 1990 $self->{value}->set_fontsize ($fsize);
1861 $self->{max} ->set_fontsize ($fsize); 1991 $self->{max} ->set_fontsize ($fsize);
1862} 1992}
1863 1993
1994sub set_max {
1995 my ($self, $max) = @_;
1996
1997 $self->{gauge}->set_max ($max);
1998 $self->{max}->set_text ($max);
1999}
2000
1864sub set_value { 2001sub set_value {
1865 my ($self, $val, $max) = @_; 2002 my ($self, $val, $max) = @_;
1866 2003
1867 $self->set_max ($max) 2004 $self->set_max ($max)
1868 if defined $max; 2005 if defined $max;
1869 2006
1870 $self->{gauge}->set_value ($val, $max); 2007 $self->{gauge}->set_value ($val, $max);
1871 $self->{value}->set_text ($val); 2008 $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} 2009}
1880 2010
1881############################################################################# 2011#############################################################################
1882 2012
1883package CFClient::UI::Slider; 2013package CFClient::UI::Slider;
1902 # TODO: calculations are off 2032 # TODO: calculations are off
1903 my $self = $class->SUPER::new ( 2033 my $self = $class->SUPER::new (
1904 fg => [1, 1, 1], 2034 fg => [1, 1, 1],
1905 active_fg => [0, 0, 0], 2035 active_fg => [0, 0, 0],
1906 range => [0, 0, 100, 10], 2036 range => [0, 0, 100, 10],
1907 req_w => 20, 2037 req_w => $::WIDTH / 80,
1908 req_h => 20, 2038 req_h => $::WIDTH / 80,
1909 vertical => 0, 2039 vertical => 0,
1910 can_hover => 1, 2040 can_hover => 1,
1911 inner_pad => 5, 2041 inner_pad => 5,
1912 @_ 2042 @_
1913 ); 2043 );
2037 (new CFClient::UI::Empty expand => 1), 2167 (new CFClient::UI::Empty expand => 1),
2038 (new CFClient::UI::Slider vertical => 1), 2168 (new CFClient::UI::Slider vertical => 1),
2039 ], 2169 ],
2040 ); 2170 );
2041 2171
2042 $self->{children}[1]->connect (changed => sub { 2172 $self->{children}[1]->connect (changed => sub { $self->update });
2043 $self->update;
2044 });
2045 2173
2046 $self 2174 $self
2047} 2175}
2048 2176
2049sub set_fontsize { 2177sub set_fontsize {
2057 my ($self, $text) = @_; 2185 my ($self, $text) = @_;
2058 2186
2059 my $layout = $self->{layout}; 2187 my $layout = $self->{layout};
2060 2188
2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2062 $layout->set_width ($self->{w}); 2190 $layout->set_width ($self->{children}[0]{w});
2063 $layout->set_text ($text); 2191 $layout->set_text ($text);
2064 2192
2065 ($layout->size)[1] 2193 ($layout->size)[1]
2066} 2194}
2067 2195
2120 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2248 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2121 2249
2122 delete $self->{texture}; 2250 delete $self->{texture};
2123 } 2251 }
2124 2252
2125 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2253 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2126 glClearColor 0, 0, 0, 1; 2254 glClearColor 0, 0, 0, 0;
2127 glClear GL_COLOR_BUFFER_BIT; 2255 glClear GL_COLOR_BUFFER_BIT;
2128 2256
2129 glEnable GL_BLEND; 2257 glEnable GL_BLEND;
2130 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2131 glEnable GL_TEXTURE_2D; 2259 glEnable GL_TEXTURE_2D;
2163} 2291}
2164 2292
2165sub _draw { 2293sub _draw {
2166 my ($self) = @_; 2294 my ($self) = @_;
2167 2295
2168 if ($self->{texture}) { 2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2169 glEnable GL_TEXTURE_2D; 2298 glEnable GL_TEXTURE_2D;
2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2299 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2171 glColor 1, 1, 1, 1; 2300 glColor 1, 1, 1, 1;
2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2301 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2173 glDisable GL_TEXTURE_2D; 2302 glDisable GL_TEXTURE_2D;
2174 } 2303 glDisable GL_BLEND;
2175 2304
2176 $self->{children}[1]->draw; 2305 $self->{children}[1]->draw;
2177 2306
2178} 2307}
2179 2308
2348 2477
2349sub size_request { 2478sub size_request {
2350 (32, 8) 2479 (32, 8)
2351} 2480}
2352 2481
2353sub draw { 2482sub _draw {
2354 my ($self) = @_; 2483 my ($self) = @_;
2355 2484
2485 return unless $::CONN;#d# manage and cache textures differently
2356 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2357 2487
2488 # TODO animation
2358 if ($tex) { 2489 if ($tex) {
2359 glEnable GL_BLEND; 2490 glEnable GL_BLEND;
2360 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2361 glEnable GL_TEXTURE_2D; 2492 glEnable GL_TEXTURE_2D;
2362 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2367 } 2498 }
2368} 2499}
2369 2500
2370############################################################################# 2501#############################################################################
2371 2502
2503package CFClient::UI::InventoryItem;
2504
2505our @ISA = CFClient::UI::HBox::;
2506
2507sub new {
2508 my $class = shift;
2509
2510 my %args = @_;
2511
2512 my $item = $args{item};
2513
2514 my $desc = $item->{nrof} < 2
2515 ? $item->{name}
2516 : "$item->{nrof} $item->{name_pl}";
2517
2518
2519 my $self = $class->SUPER::new (
2520 can_hover => 1,
2521 can_events => 1,
2522 tooltip => (CFClient::UI::Label->escape ($desc)
2523 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2524 connect_button_down => sub {
2525 my ($self, $ev, $x, $y) = @_;
2526
2527 # todo: maybe put examine on 1? but should just be a tooltip :(
2528 if ($ev->{button} == 1) {
2529 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2530 } elsif ($ev->{button} == 2) {
2531 $::CONN->send ("apply $item->{tag}");
2532 } elsif ($ev->{button} == 3) {
2533 CFClient::UI::Menu->new (
2534 items => [
2535 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2536 [
2537 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2538 sub { $::CONN->send ("lock $item->{tag}") },
2539 ],
2540 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2541 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2542 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2543 ],
2544 )->popup ($ev);
2545 }
2546
2547 1
2548 },
2549 %args
2550 );
2551 $self->add(new CFClient::UI::Face
2552 can_events => 0,
2553 face => $item->{face},
2554 anim => $item->{anim},
2555 animspeed => $item->{animspeed});
2556 $self->add(new CFClient::UI::Label
2557 can_events => 0,
2558 text => $desc);
2559
2560 $self
2561}
2562
2563#############################################################################
2564
2565package CFClient::UI::Inventory;
2566
2567our @ISA = CFClient::UI::ScrolledWindow::;
2568
2569sub new {
2570 my $class = shift;
2571
2572 my $self = $class->SUPER::new (
2573 scrolled => (new CFClient::UI::VBox),
2574 @_,
2575 );
2576
2577 $self
2578}
2579
2580sub set_items {
2581 my ($self, $items) = @_;
2582
2583 $self->{scrolled}->clear;
2584 return unless $items;
2585
2586 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2587
2588 $self->{real_items} = \@items;
2589
2590 for my $item (@items) {
2591 my $desc = $item->{nrof} < 2
2592 ? $item->{name}
2593 : "$item->{nrof} $item->{name_pl}";
2594
2595 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2596 }
2597
2598# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2599}
2600
2601sub size_request {
2602 my ($self) = @_;
2603 ($self->{req_w}, $self->{req_h});
2604}
2605
2606#############################################################################
2607
2608package CFClient::UI::Menu;
2609
2610our @ISA = CFClient::UI::FancyFrame::;
2611
2612use CFClient::OpenGL;
2613
2614sub new {
2615 my $class = shift;
2616
2617 my $self = $class->SUPER::new (
2618 items => [],
2619 z => 100,
2620 @_,
2621 );
2622
2623 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2624
2625 for my $item (@{ $self->{items} }) {
2626 my ($widget, $cb) = @$item;
2627
2628 # handle various types of items, only text for now
2629 if (!ref $widget) {
2630 $widget = new CFClient::UI::Label
2631 can_hover => 1,
2632 can_events => 1,
2633 text => $widget;
2634 }
2635
2636 $self->{item}{$widget} = $item;
2637
2638 $self->{vbox}->add ($widget);
2639 }
2640
2641 $self
2642}
2643
2644# popup given the event (must be a mouse button down event currently)
2645sub popup {
2646 my ($self, $ev) = @_;
2647
2648 $self->emit ("popdown");
2649
2650 # maybe save $GRAB? must be careful about events...
2651 $GRAB = $self;
2652 $self->{button} = $ev->{button};
2653
2654 $self->show;
2655 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2656}
2657
2658sub mouse_motion {
2659 my ($self, $ev, $x, $y) = @_;
2660
2661 # TODO: should use vbox->find_widget or so
2662 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2663 $self->{hover} = $self->{item}{$HOVER};
2664}
2665
2666sub button_up {
2667 my ($self, $ev, $x, $y) = @_;
2668
2669 if ($ev->{button} == $self->{button}) {
2670 undef $GRAB;
2671 $self->hide;
2672
2673 $self->emit ("popdown");
2674 $self->{hover}[1]->() if $self->{hover};
2675 }
2676}
2677
2678#############################################################################
2679
2372package CFClient::UI::Root; 2680package CFClient::UI::Root;
2373 2681
2374our @ISA = CFClient::UI::Container::; 2682our @ISA = CFClient::UI::Container::;
2375 2683
2376use CFClient::OpenGL; 2684use CFClient::OpenGL;
2377 2685
2378sub check_size { 2686sub check_size {
2379 my ($self) = @_; 2687 my ($self) = @_;
2380 2688
2381 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2689 $self->configure (0, 0, $self->{w}, $self->{h});
2382} 2690}
2383 2691
2384sub size_request { 2692sub size_request {
2385 ($::WIDTH, $::HEIGHT) 2693 my ($self) = @_;
2694
2695 ($self->{w}, $self->{h})
2696}
2697
2698sub size_allocate {
2699 my ($self, $w, $h) = @_;
2700
2701 my $old_w = $self->{old_w};
2702 my $old_h = $self->{old_h};
2703
2704 if ($old_w && $old_h) {
2705 for my $child ($self->children) {
2706 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2707 $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w;
2708 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2709 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2710 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2711 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2712 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2713 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2714 }
2715 }
2716
2717 $self->{old_w} = $w;
2718 $self->{old_h} = $h;
2386} 2719}
2387 2720
2388sub configure { 2721sub configure {
2389 my ($self, $x, $y, $w, $h) = @_; 2722 my ($self, $x, $y, $w, $h) = @_;
2390 2723
2391 $self->SUPER::configure ($x, $y, $w, $h); 2724 $self->SUPER::configure ($x, $y, $w, $h);
2392 2725
2393 for my $child (@{$self->{children}}) { 2726 for my $child ($self->children) {
2394 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2727 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2395 2728
2396 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2729 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2397 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2398 $child->configure ($X, $Y, $W,$H); 2731 $child->configure ($X, $Y, $W, $H);
2399 } 2732 }
2400} 2733}
2401 2734
2402sub _topleft { 2735sub _topleft {
2403 my ($self, $x, $y) = @_; 2736 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines