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.171 by root, Mon Apr 24 11:54:26 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
248}
249
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;
239} 263}
240 264
241sub set_max_size { 265sub set_max_size {
242 my ($self, $w, $h) = @_; 266 my ($self, $w, $h) = @_;
243 267
518 sort { $a->{z} <=> $b->{z} } 542 sort { $a->{z} <=> $b->{z} }
519 @{$self->{children}}, $child 543 @{$self->{children}}, $child
520 ]; 544 ];
521 545
522 $child->check_size; 546 $child->check_size;
547 $self->update;
548}
549
550sub children {
551 @{ $_[0]{children} }
523} 552}
524 553
525sub remove { 554sub remove {
526 my ($self, $child) = @_; 555 my ($self, $child) = @_;
527 556
542 571
543 for (@$children) { 572 for (@$children) {
544 delete $_->{parent}; 573 delete $_->{parent};
545 $_->hide; 574 $_->hide;
546 } 575 }
576
577 $self->check_size;
578 $self->update;
547} 579}
548 580
549sub find_widget { 581sub find_widget {
550 my ($self, $x, $y) = @_; 582 my ($self, $x, $y) = @_;
551 583
626} 658}
627 659
628sub update { 660sub update {
629 my ($self) = @_; 661 my ($self) = @_;
630 662
631 # we want to do this delayed... 663 $ROOT->on_refresh ($self => sub { $self->render_child });
632 $self->render_chld;
633 $self->SUPER::update; 664 $self->SUPER::update;
634}
635
636sub render_chld {
637 my ($self) = @_;
638
639 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
640 glClearColor 0, 0, 0, 1;
641 glClear GL_COLOR_BUFFER_BIT;
642 $self->child->draw;
643 };
644} 665}
645 666
646sub size_allocate { 667sub size_allocate {
647 my ($self, $w, $h) = @_; 668 my ($self, $w, $h) = @_;
648 669
649 $self->child->configure (0, 0, $w, $h); 670 $self->SUPER::size_allocate ($w, $h);
671 $self->update;
672}
650 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
651 $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 };
652} 694}
653 695
654sub _draw { 696sub _draw {
655 my ($self) = @_; 697 my ($self) = @_;
656 698
658 700
659 my $tex = $self->{texture} 701 my $tex = $self->{texture}
660 or return; 702 or return;
661 703
662 glEnable GL_BLEND; 704 glEnable GL_BLEND;
663 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
664 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
665 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;
666 709
667 $tex->draw_quad (0, 0, $w, $h); 710 $tex->draw_quad (0, 0, $w, $h);
668 711
669 glDisable GL_BLEND; 712 glDisable GL_BLEND;
670 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
674 717
675package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
676 719
677our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
678 721
679sub new { die }
680
681sub size_request { 722sub size_request {
682 my ($self) = @_; 723 my ($self) = @_;
683 724
684 @$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)};
685 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
686 727
687 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
688} 729}
689 730
690sub _draw { 731sub size_allocate {
691 my ($self) = @_; 732 my ($self, $w, $h) = @_;
692 733
693 $self->{children}[1]->draw; 734 $self->update;
694} 735}
695 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
696 807
697############################################################################# 808#############################################################################
698 809
699package CFClient::UI::Frame; 810package CFClient::UI::Frame;
700 811
757 # TODO: user_x, user_y, overwrite moveto? 868 # TODO: user_x, user_y, overwrite moveto?
758 869
759 my $self = $class->SUPER::new ( 870 my $self = $class->SUPER::new (
760 bg => [1, 1, 1, 1], 871 bg => [1, 1, 1, 1],
761 border_bg => [1, 1, 1, 1], 872 border_bg => [1, 1, 1, 1],
762 border => 0.8, 873 border => 0.6,
763 can_events => 1, 874 can_events => 1,
764 @_ 875 @_
765 ); 876 );
766 877
767 $self->{title} &&= new CFClient::UI::Label 878 $self->{title} &&= new CFClient::UI::Label
768 align => 0, 879 align => 0,
769 valign => 1, 880 valign => 1,
770 text => $self->{title}, 881 text => $self->{title},
771 fontsize => 1; 882 fontsize => $self->{border};
772 883
773 $self 884 $self
774} 885}
775 886
776sub border { 887sub border {
801} 912}
802 913
803sub button_down { 914sub button_down {
804 my ($self, $ev, $x, $y) = @_; 915 my ($self, $ev, $x, $y) = @_;
805 916
917 my ($w, $h) = @$self{qw(w h)};
806 my $border = $self->border; 918 my $border = $self->border;
807 919
808 if ($x < $self->{w} && $x >= $self->{w} - $border 920 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
809 && $y < $self->{h} && $y >= $self->{h} - $border) { 921 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
810 922
923 if ($lr & $td) {
924 my ($wx, $wy) = ($self->{x}, $self->{y});
811 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 925 my ($ox, $oy) = ($ev->{x}, $ev->{y});
812 my ($bw, $bh) = ($self->{w}, $self->{h}); 926 my ($bw, $bh) = ($self->{w}, $self->{h});
813 927
928 my $mx = $x < $border;
929 my $my = $y < $border;
930
814 $self->{motion} = sub { 931 $self->{motion} = sub {
815 my ($ev, $x, $y) = @_; 932 my ($ev, $x, $y) = @_;
816 933
817 ($x, $y) = ($ev->{x}, $ev->{y}); 934 my $dx = $ev->{x} - $ox;
935 my $dy = $ev->{y} - $oy;
818 936
819 $self->{user_w} = $bw + $x - $ox; 937 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
820 $self->{user_h} = $bh + $y - $oy; 938 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
939 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
821 $self->check_size; 940 $self->check_size;
822 }; 941 };
823 942
824 } elsif ($x >= 0 && $x < $self->{w} 943 } elsif ($lr ^ $td) {
825 && $y >= 0 && $y < $border) {
826
827 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 944 my ($ox, $oy) = ($ev->{x}, $ev->{y});
828 my ($bx, $by) = ($self->{x}, $self->{y}); 945 my ($bx, $by) = ($self->{x}, $self->{y});
829 946
830 $self->{motion} = sub { 947 $self->{motion} = sub {
831 my ($ev, $x, $y) = @_; 948 my ($ev, $x, $y) = @_;
867 $tex[1]->draw_quad (0, 0, $w, $border); 984 $tex[1]->draw_quad (0, 0, $w, $border);
868 $tex[3]->draw_quad (0, $border, $border, $ch); 985 $tex[3]->draw_quad (0, $border, $border, $ch);
869 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 986 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
870 $tex[4]->draw_quad (0, $h - $border, $w, $border); 987 $tex[4]->draw_quad (0, $h - $border, $w, $border);
871 988
989 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
872 my $bg = $tex[0]; 990 my $bg = $tex[0];
873 991
874 # TODO: repeat texture not scale 992 # TODO: repeat texture not scale
875 my $rep_x = $cw / $bg->{w}; 993 my $rep_x = $cw / $bg->{w};
876 my $rep_y = $ch / $bg->{h}; 994 my $rep_y = $ch / $bg->{h};
877 995
878 glColor @{ $self->{bg} }; 996 glColor @{ $self->{bg} };
879 997
880 $bg->{s} = $rep_x; 998 $bg->{s} = $rep_x;
881 $bg->{t} = $rep_y; 999 $bg->{t} = $rep_y;
882 $bg->{wrap_mode} = 1; 1000 $bg->{wrap_mode} = 1;
883 $bg->draw_quad ($border, $border, $cw, $ch); 1001 $bg->draw_quad ($border, $border, $cw, $ch);
884 1002
885 glDisable GL_TEXTURE_2D; 1003 glDisable GL_TEXTURE_2D;
886 glDisable GL_BLEND; 1004 glDisable GL_BLEND;
1005 }
887 1006
888 $self->{title}->draw if $self->{title}; 1007 $self->{title}->draw if $self->{title};
1008
889 $self->child->draw; 1009 $self->child->draw;
890} 1010}
891 1011
892############################################################################# 1012#############################################################################
893 1013
915 $self->{children}[$y][$x] = $child; 1035 $self->{children}[$y][$x] = $child;
916 1036
917 $child->check_size; 1037 $child->check_size;
918} 1038}
919 1039
1040sub children {
1041 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1042}
1043
920# TODO: move to container class maybe? send childs a signal on removal? 1044# TODO: move to container class maybe? send childs a signal on removal?
921sub clear { 1045sub clear {
922 my ($self) = @_; 1046 my ($self) = @_;
923 1047
924 my $children = delete $self->{children}; 1048 my @children = $self->children;
1049 delete $self->{children};
925 1050
926 for (grep $_, map @$_, grep $_, @$children) { 1051 for (@children) {
927 delete $_->{parent}; 1052 delete $_->{parent};
928 $_->hide; 1053 $_->hide;
929 } 1054 }
930 1055
931 $self->update; 1056 $self->update;
1192 s/</&lt;/g; 1317 s/</&lt;/g;
1193 1318
1194 $_[1] 1319 $_[1]
1195} 1320}
1196 1321
1322sub update {
1323 my ($self) = @_;
1324
1325 delete $self->{texture};
1326 $self->SUPER::update;
1327}
1328
1197sub set_text { 1329sub set_text {
1198 my ($self, $text) = @_; 1330 my ($self, $text) = @_;
1199 1331
1332 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text";
1334
1200 $self->{layout}->set_text ($text); 1335 $self->{layout}->set_text ($text);
1201 1336
1202 delete $self->{texture}; 1337 delete $self->{texture};
1338 $self->update;
1203 $self->check_size; 1339 $self->check_size;
1204 $self->update;
1205} 1340}
1206 1341
1207sub set_markup { 1342sub set_markup {
1208 my ($self, $markup) = @_; 1343 my ($self, $markup) = @_;
1209 1344
1345 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup";
1347
1210 $self->{layout}->set_markup ($markup); 1348 $self->{layout}->set_markup ($markup);
1211 1349
1212 delete $self->{texture}; 1350 delete $self->{texture};
1351 $self->update;
1213 $self->check_size; 1352 $self->check_size;
1214 $self->update;
1215} 1353}
1216 1354
1217sub size_request { 1355sub size_request {
1218 my ($self) = @_; 1356 my ($self) = @_;
1219 1357
1248sub set_fontsize { 1386sub set_fontsize {
1249 my ($self, $fontsize) = @_; 1387 my ($self, $fontsize) = @_;
1250 1388
1251 $self->{fontsize} = $fontsize; 1389 $self->{fontsize} = $fontsize;
1252 delete $self->{texture}; 1390 delete $self->{texture};
1391
1392 $self->update;
1253 $self->check_size; 1393 $self->check_size;
1254 $self->update;
1255} 1394}
1256 1395
1257sub _draw { 1396sub _draw {
1258 my ($self) = @_; 1397 my ($self) = @_;
1259 1398
1743} 1882}
1744 1883
1745sub set_max { 1884sub set_max {
1746 my ($self, $max) = @_; 1885 my ($self, $max) = @_;
1747 1886
1887 return if $self->{max_val} == $max;
1888
1748 $self->{max_val} = $max; 1889 $self->{max_val} = $max;
1890 $self->update;
1749} 1891}
1750 1892
1751sub set_value { 1893sub set_value {
1752 my ($self, $val, $max) = @_; 1894 my ($self, $val, $max) = @_;
1753 1895
1754 $self->set_max ($max) 1896 $self->set_max ($max)
1755 if defined $max; 1897 if defined $max;
1756 1898
1757 $max = $self->{max_val}; 1899 return if $self->{val} == $val;
1900
1758 $self->{val} = $val; 1901 $self->{val} = $val;
1759
1760 $self->update; 1902 $self->update;
1761} 1903}
1762 1904
1763sub _draw { 1905sub _draw {
1764 my ($self) = @_; 1906 my ($self) = @_;
1847 1989
1848 $self->{value}->set_fontsize ($fsize); 1990 $self->{value}->set_fontsize ($fsize);
1849 $self->{max} ->set_fontsize ($fsize); 1991 $self->{max} ->set_fontsize ($fsize);
1850} 1992}
1851 1993
1994sub set_max {
1995 my ($self, $max) = @_;
1996
1997 $self->{gauge}->set_max ($max);
1998 $self->{max}->set_text ($max);
1999}
2000
1852sub set_value { 2001sub set_value {
1853 my ($self, $val, $max) = @_; 2002 my ($self, $val, $max) = @_;
1854 2003
1855 $self->set_max ($max) 2004 $self->set_max ($max)
1856 if defined $max; 2005 if defined $max;
1857 2006
1858 $self->{gauge}->set_value ($val, $max); 2007 $self->{gauge}->set_value ($val, $max);
1859 $self->{value}->set_text ($val); 2008 $self->{value}->set_text ($val);
1860}
1861
1862sub set_max {
1863 my ($self, $max) = @_;
1864
1865 $self->{gauge}->set_max ($max);
1866 $self->{max}->set_text ($max);
1867} 2009}
1868 2010
1869############################################################################# 2011#############################################################################
1870 2012
1871package CFClient::UI::Slider; 2013package CFClient::UI::Slider;
1890 # TODO: calculations are off 2032 # TODO: calculations are off
1891 my $self = $class->SUPER::new ( 2033 my $self = $class->SUPER::new (
1892 fg => [1, 1, 1], 2034 fg => [1, 1, 1],
1893 active_fg => [0, 0, 0], 2035 active_fg => [0, 0, 0],
1894 range => [0, 0, 100, 10], 2036 range => [0, 0, 100, 10],
1895 req_w => 20, 2037 req_w => $::WIDTH / 80,
1896 req_h => 20, 2038 req_h => $::WIDTH / 80,
1897 vertical => 0, 2039 vertical => 0,
1898 can_hover => 1, 2040 can_hover => 1,
1899 inner_pad => 5, 2041 inner_pad => 5,
1900 @_ 2042 @_
1901 ); 2043 );
2025 (new CFClient::UI::Empty expand => 1), 2167 (new CFClient::UI::Empty expand => 1),
2026 (new CFClient::UI::Slider vertical => 1), 2168 (new CFClient::UI::Slider vertical => 1),
2027 ], 2169 ],
2028 ); 2170 );
2029 2171
2030 $self->{children}[1]->connect (changed => sub { 2172 $self->{children}[1]->connect (changed => sub { $self->update });
2031 $self->update;
2032 });
2033 2173
2034 $self 2174 $self
2035} 2175}
2036 2176
2037sub set_fontsize { 2177sub set_fontsize {
2045 my ($self, $text) = @_; 2185 my ($self, $text) = @_;
2046 2186
2047 my $layout = $self->{layout}; 2187 my $layout = $self->{layout};
2048 2188
2049 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2050 $layout->set_width ($self->{w}); 2190 $layout->set_width ($self->{children}[0]{w});
2051 $layout->set_text ($text); 2191 $layout->set_text ($text);
2052 2192
2053 ($layout->size)[1] 2193 ($layout->size)[1]
2054} 2194}
2055 2195
2108 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2248 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2109 2249
2110 delete $self->{texture}; 2250 delete $self->{texture};
2111 } 2251 }
2112 2252
2113 $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 {
2114 glClearColor 0, 0, 0, 1; 2254 glClearColor 0, 0, 0, 0;
2115 glClear GL_COLOR_BUFFER_BIT; 2255 glClear GL_COLOR_BUFFER_BIT;
2116 2256
2117 glEnable GL_BLEND; 2257 glEnable GL_BLEND;
2118 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2119 glEnable GL_TEXTURE_2D; 2259 glEnable GL_TEXTURE_2D;
2151} 2291}
2152 2292
2153sub _draw { 2293sub _draw {
2154 my ($self) = @_; 2294 my ($self) = @_;
2155 2295
2156 if ($self->{texture}) { 2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2157 glEnable GL_TEXTURE_2D; 2298 glEnable GL_TEXTURE_2D;
2158 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2299 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2300 glColor 1, 1, 1, 1;
2159 $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});
2160 glDisable GL_TEXTURE_2D; 2302 glDisable GL_TEXTURE_2D;
2161 } 2303 glDisable GL_BLEND;
2162 2304
2163 $self->{children}[1]->draw; 2305 $self->{children}[1]->draw;
2164 2306
2165} 2307}
2166 2308
2335 2477
2336sub size_request { 2478sub size_request {
2337 (32, 8) 2479 (32, 8)
2338} 2480}
2339 2481
2340sub draw { 2482sub _draw {
2341 my ($self) = @_; 2483 my ($self) = @_;
2342 2484
2485 return unless $::CONN;#d# manage and cache textures differently
2343 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2344 2487
2488 # TODO animation
2345 if ($tex) { 2489 if ($tex) {
2346 glEnable GL_BLEND; 2490 glEnable GL_BLEND;
2347 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2348 glEnable GL_TEXTURE_2D; 2492 glEnable GL_TEXTURE_2D;
2349 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2354 } 2498 }
2355} 2499}
2356 2500
2357############################################################################# 2501#############################################################################
2358 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
2359package CFClient::UI::Root; 2680package CFClient::UI::Root;
2360 2681
2361our @ISA = CFClient::UI::Container::; 2682our @ISA = CFClient::UI::Container::;
2362 2683
2363use CFClient::OpenGL; 2684use CFClient::OpenGL;
2364 2685
2365sub check_size { 2686sub check_size {
2366 my ($self) = @_; 2687 my ($self) = @_;
2367 2688
2368 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2689 $self->configure (0, 0, $self->{w}, $self->{h});
2369} 2690}
2370 2691
2371sub size_request { 2692sub size_request {
2372 ($::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;
2373} 2719}
2374 2720
2375sub configure { 2721sub configure {
2376 my ($self, $x, $y, $w, $h) = @_; 2722 my ($self, $x, $y, $w, $h) = @_;
2377 2723
2378 $self->SUPER::configure ($x, $y, $w, $h); 2724 $self->SUPER::configure ($x, $y, $w, $h);
2379 2725
2380 for my $child (@{$self->{children}}) { 2726 for my $child ($self->children) {
2381 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)};
2382 2728
2383 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2729 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2384 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2385 $child->configure ($X, $Y, $W,$H); 2731 $child->configure ($X, $Y, $W, $H);
2386 } 2732 }
2387} 2733}
2388 2734
2389sub _topleft { 2735sub _topleft {
2390 my ($self, $x, $y) = @_; 2736 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines