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.167 by elmex, Mon Apr 24 08:44:23 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
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 $_->check_size;
239} 261}
240 262
241sub set_max_size { 263sub set_max_size {
242 my ($self, $w, $h) = @_; 264 my ($self, $w, $h) = @_;
243 265
246} 268}
247 269
248# return top left coordinates 270# return top left coordinates
249sub _topleft { 271sub _topleft {
250 my ($self, $x, $y) = @_; 272 my ($self, $x, $y) = @_;
273
274 $self->{parent}
275 or Carp::confess "no parent widget in _topleft\n";#d#
251 276
252 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 277 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
253} 278}
254 279
255# translate global coordinates to local coordinate system 280# translate global coordinates to local coordinate system
515 sort { $a->{z} <=> $b->{z} } 540 sort { $a->{z} <=> $b->{z} }
516 @{$self->{children}}, $child 541 @{$self->{children}}, $child
517 ]; 542 ];
518 543
519 $child->check_size; 544 $child->check_size;
545 $self->update;
546}
547
548sub children {
549 @{ $_[0]{children} }
520} 550}
521 551
522sub remove { 552sub remove {
523 my ($self, $child) = @_; 553 my ($self, $child) = @_;
524 554
539 569
540 for (@$children) { 570 for (@$children) {
541 delete $_->{parent}; 571 delete $_->{parent};
542 $_->hide; 572 $_->hide;
543 } 573 }
574
575 $self->check_size;
576 $self->update;
544} 577}
545 578
546sub find_widget { 579sub find_widget {
547 my ($self, $x, $y) = @_; 580 my ($self, $x, $y) = @_;
548 581
623} 656}
624 657
625sub update { 658sub update {
626 my ($self) = @_; 659 my ($self) = @_;
627 660
628 # we want to do this delayed... 661 $ROOT->on_refresh ($self => sub { $self->render_child });
629 $self->render_chld;
630 $self->SUPER::update; 662 $self->SUPER::update;
631}
632
633sub render_chld {
634 my ($self) = @_;
635
636 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
637 glClearColor 0, 0, 0, 1;
638 glClear GL_COLOR_BUFFER_BIT;
639 $self->child->draw;
640 };
641} 663}
642 664
643sub size_allocate { 665sub size_allocate {
644 my ($self, $w, $h) = @_; 666 my ($self, $w, $h) = @_;
645 667
646 $self->child->configure (0, 0, $w, $h); 668 $self->SUPER::size_allocate ($w, $h);
669 $self->update;
670}
647 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
648 $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 };
649} 692}
650 693
651sub _draw { 694sub _draw {
652 my ($self) = @_; 695 my ($self) = @_;
653 696
655 698
656 my $tex = $self->{texture} 699 my $tex = $self->{texture}
657 or return; 700 or return;
658 701
659 glEnable GL_BLEND; 702 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 703 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 704 glEnable GL_TEXTURE_2D;
662 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;
663 707
664 $tex->draw_quad (0, 0, $w, $h); 708 $tex->draw_quad (0, 0, $w, $h);
665 709
666 glDisable GL_BLEND; 710 glDisable GL_BLEND;
667 glDisable GL_TEXTURE_2D; 711 glDisable GL_TEXTURE_2D;
671 715
672package CFClient::UI::ViewPort; 716package CFClient::UI::ViewPort;
673 717
674our @ISA = CFClient::UI::Window::; 718our @ISA = CFClient::UI::Window::;
675 719
676sub new { die }
677
678sub size_request { 720sub size_request {
679 my ($self) = @_; 721 my ($self) = @_;
680 722
681 @$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)};
682 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 724 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
683 725
684 @$self{qw(child_w child_h)} 726 @$self{qw(child_w child_h)}
685} 727}
686 728
687sub _draw { 729sub size_allocate {
688 my ($self) = @_; 730 my ($self, $w, $h) = @_;
689 731
690 $self->{children}[1]->draw; 732 $self->update;
691} 733}
692 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
693 786
694############################################################################# 787#############################################################################
695 788
696package CFClient::UI::Frame; 789package CFClient::UI::Frame;
697 790
754 # TODO: user_x, user_y, overwrite moveto? 847 # TODO: user_x, user_y, overwrite moveto?
755 848
756 my $self = $class->SUPER::new ( 849 my $self = $class->SUPER::new (
757 bg => [1, 1, 1, 1], 850 bg => [1, 1, 1, 1],
758 border_bg => [1, 1, 1, 1], 851 border_bg => [1, 1, 1, 1],
759 border => 0.8, 852 border => 0.6,
760 can_events => 1, 853 can_events => 1,
761 @_ 854 @_
762 ); 855 );
763 856
764 $self->{title} &&= new CFClient::UI::Label 857 $self->{title} &&= new CFClient::UI::Label
765 align => 0, 858 align => 0,
766 valign => 1, 859 valign => 1,
767 text => $self->{title}, 860 text => $self->{title},
768 fontsize => 1; 861 fontsize => $self->{border};
769 862
770 $self 863 $self
771} 864}
772 865
773sub border { 866sub border {
798} 891}
799 892
800sub button_down { 893sub button_down {
801 my ($self, $ev, $x, $y) = @_; 894 my ($self, $ev, $x, $y) = @_;
802 895
896 my ($w, $h) = @$self{qw(w h)};
803 my $border = $self->border; 897 my $border = $self->border;
804 898
805 if ($x < $self->{w} && $x >= $self->{w} - $border 899 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
806 && $y < $self->{h} && $y >= $self->{h} - $border) { 900 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
807 901
902 if ($lr & $td) {
903 my ($wx, $wy) = ($self->{x}, $self->{y});
808 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 904 my ($ox, $oy) = ($ev->{x}, $ev->{y});
809 my ($bw, $bh) = ($self->{w}, $self->{h}); 905 my ($bw, $bh) = ($self->{w}, $self->{h});
810 906
907 my $mx = $x < $border;
908 my $my = $y < $border;
909
811 $self->{motion} = sub { 910 $self->{motion} = sub {
812 my ($ev, $x, $y) = @_; 911 my ($ev, $x, $y) = @_;
813 912
814 ($x, $y) = ($ev->{x}, $ev->{y}); 913 my $dx = $ev->{x} - $ox;
914 my $dy = $ev->{y} - $oy;
815 915
816 $self->{user_w} = $bw + $x - $ox; 916 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
817 $self->{user_h} = $bh + $y - $oy; 917 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
918 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
818 $self->check_size; 919 $self->check_size;
819 }; 920 };
820 921
821 } elsif ($x >= 0 && $x < $self->{w} 922 } elsif ($lr ^ $td) {
822 && $y >= 0 && $y < $border) {
823
824 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 923 my ($ox, $oy) = ($ev->{x}, $ev->{y});
825 my ($bx, $by) = ($self->{x}, $self->{y}); 924 my ($bx, $by) = ($self->{x}, $self->{y});
826 925
827 $self->{motion} = sub { 926 $self->{motion} = sub {
828 my ($ev, $x, $y) = @_; 927 my ($ev, $x, $y) = @_;
864 $tex[1]->draw_quad (0, 0, $w, $border); 963 $tex[1]->draw_quad (0, 0, $w, $border);
865 $tex[3]->draw_quad (0, $border, $border, $ch); 964 $tex[3]->draw_quad (0, $border, $border, $ch);
866 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 965 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
867 $tex[4]->draw_quad (0, $h - $border, $w, $border); 966 $tex[4]->draw_quad (0, $h - $border, $w, $border);
868 967
968 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
869 my $bg = $tex[0]; 969 my $bg = $tex[0];
870 970
871 # TODO: repeat texture not scale 971 # TODO: repeat texture not scale
872 my $rep_x = $cw / $bg->{w}; 972 my $rep_x = $cw / $bg->{w};
873 my $rep_y = $ch / $bg->{h}; 973 my $rep_y = $ch / $bg->{h};
874 974
875 glColor @{ $self->{bg} }; 975 glColor @{ $self->{bg} };
876 976
877 $bg->{s} = $rep_x; 977 $bg->{s} = $rep_x;
878 $bg->{t} = $rep_y; 978 $bg->{t} = $rep_y;
879 $bg->{wrap_mode} = 1; 979 $bg->{wrap_mode} = 1;
880 $bg->draw_quad ($border, $border, $cw, $ch); 980 $bg->draw_quad ($border, $border, $cw, $ch);
881 981
882 glDisable GL_TEXTURE_2D; 982 glDisable GL_TEXTURE_2D;
883 glDisable GL_BLEND; 983 glDisable GL_BLEND;
984 }
884 985
885 $self->{title}->draw if $self->{title}; 986 $self->{title}->draw if $self->{title};
987
886 $self->child->draw; 988 $self->child->draw;
887} 989}
888 990
889############################################################################# 991#############################################################################
890 992
912 $self->{children}[$y][$x] = $child; 1014 $self->{children}[$y][$x] = $child;
913 1015
914 $child->check_size; 1016 $child->check_size;
915} 1017}
916 1018
1019sub children {
1020 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1021}
1022
917# TODO: move to container class maybe? send childs a signal on removal? 1023# TODO: move to container class maybe? send childs a signal on removal?
918sub clear { 1024sub clear {
919 my ($self) = @_; 1025 my ($self) = @_;
920 1026
921 my $children = delete $self->{children}; 1027 my @children = $self->children;
1028 delete $self->{children};
922 1029
923 for (grep $_, map @$_, grep $_, @$children) { 1030 for (@children) {
924 delete $_->{parent}; 1031 delete $_->{parent};
925 $_->hide; 1032 $_->hide;
926 } 1033 }
927 1034
928 $self->update; 1035 $self->update;
1189 s/</&lt;/g; 1296 s/</&lt;/g;
1190 1297
1191 $_[1] 1298 $_[1]
1192} 1299}
1193 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
1194sub set_text { 1314sub set_text {
1195 my ($self, $text) = @_; 1315 my ($self, $text) = @_;
1196 1316
1317 return if $self->{text} eq "T$text";
1318 $self->{text} = "T$text";
1319
1197 $self->{layout}->set_text ($text); 1320 $self->{layout}->set_text ($text);
1198 1321
1199 delete $self->{texture}; 1322 delete $self->{texture};
1323 $self->update;
1200 $self->check_size; 1324 $self->check_size;
1201 $self->update;
1202} 1325}
1203 1326
1204sub set_markup { 1327sub set_markup {
1205 my ($self, $markup) = @_; 1328 my ($self, $markup) = @_;
1206 1329
1330 return if $self->{text} eq "M$markup";
1331 $self->{text} = "M$markup";
1332
1207 $self->{layout}->set_markup ($markup); 1333 $self->{layout}->set_markup ($markup);
1208 1334
1209 delete $self->{texture}; 1335 delete $self->{texture};
1336 $self->update;
1210 $self->check_size; 1337 $self->check_size;
1211 $self->update;
1212} 1338}
1213 1339
1214sub size_request { 1340sub size_request {
1215 my ($self) = @_; 1341 my ($self) = @_;
1216 1342
1469 1595
1470 if ($sym == 13) { 1596 if ($sym == 13) {
1471 unshift @{$self->{history}}, 1597 unshift @{$self->{history}},
1472 my $txt = $self->get_text; 1598 my $txt = $self->get_text;
1473 $self->{history_pointer} = -1; 1599 $self->{history_pointer} = -1;
1600 $self->{history_saveback} = '';
1474 $self->emit (activate => $txt); 1601 $self->emit (activate => $txt);
1475 $self->update; 1602 $self->update;
1476 1603
1477 } elsif ($sym == CFClient::SDLK_UP) { 1604 } elsif ($sym == CFClient::SDLK_UP) {
1478 if ($self->{history_pointer} < 0) { 1605 if ($self->{history_pointer} < 0) {
1479 $self->{history_saveback} = $self->get_text; 1606 $self->{history_saveback} = $self->get_text;
1480 } 1607 }
1608 if (@{$self->{history} || []} > 0) {
1481 $self->{history_pointer}++; 1609 $self->{history_pointer}++;
1482 if ($self->{history_pointer} >= @{$self->{history}}) { 1610 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1483 $self->{history_pointer} = @{$self->{history}} - 1; 1611 $self->{history_pointer} = @{$self->{history} || []} - 1;
1612 }
1613 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1484 } 1614 }
1485 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1486 1615
1487 } elsif ($sym == CFClient::SDLK_DOWN) { 1616 } elsif ($sym == CFClient::SDLK_DOWN) {
1488 $self->{history_pointer}--; 1617 $self->{history_pointer}--;
1489 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 1618 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1490 1619
1737} 1866}
1738 1867
1739sub set_max { 1868sub set_max {
1740 my ($self, $max) = @_; 1869 my ($self, $max) = @_;
1741 1870
1871 return if $self->{max_val} == $max;
1872
1742 $self->{max_val} = $max; 1873 $self->{max_val} = $max;
1874 $self->update;
1743} 1875}
1744 1876
1745sub set_value { 1877sub set_value {
1746 my ($self, $val, $max) = @_; 1878 my ($self, $val, $max) = @_;
1747 1879
1748 $self->set_max ($max) 1880 $self->set_max ($max)
1749 if defined $max; 1881 if defined $max;
1750 1882
1751 $max = $self->{max_val}; 1883 return if $self->{val} == $val;
1884
1752 $self->{val} = $val; 1885 $self->{val} = $val;
1753
1754 $self->update; 1886 $self->update;
1755} 1887}
1756 1888
1757sub _draw { 1889sub _draw {
1758 my ($self) = @_; 1890 my ($self) = @_;
1821 1953
1822sub new { 1954sub new {
1823 my ($class, %arg) = @_; 1955 my ($class, %arg) = @_;
1824 1956
1825 my $self = $class->SUPER::new ( 1957 my $self = $class->SUPER::new (
1826 tooltip => $arg{type}, 1958 tooltip => $arg{type},
1827 can_hover => 1, 1959 can_hover => 1,
1960 can_events => 1,
1828 %arg, 1961 %arg,
1829 ); 1962 );
1830 1963
1831 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1964 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1832 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 1965 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1840 1973
1841 $self->{value}->set_fontsize ($fsize); 1974 $self->{value}->set_fontsize ($fsize);
1842 $self->{max} ->set_fontsize ($fsize); 1975 $self->{max} ->set_fontsize ($fsize);
1843} 1976}
1844 1977
1978sub set_max {
1979 my ($self, $max) = @_;
1980
1981 $self->{gauge}->set_max ($max);
1982 $self->{max}->set_text ($max);
1983}
1984
1845sub set_value { 1985sub set_value {
1846 my ($self, $val, $max) = @_; 1986 my ($self, $val, $max) = @_;
1847 1987
1848 $self->set_max ($max) 1988 $self->set_max ($max)
1849 if defined $max; 1989 if defined $max;
1850 1990
1851 $self->{gauge}->set_value ($val, $max); 1991 $self->{gauge}->set_value ($val, $max);
1852 $self->{value}->set_text ($val); 1992 $self->{value}->set_text ($val);
1853}
1854
1855sub set_max {
1856 my ($self, $max) = @_;
1857
1858 $self->{gauge}->set_max ($max);
1859 $self->{max}->set_text ($max);
1860} 1993}
1861 1994
1862############################################################################# 1995#############################################################################
1863 1996
1864package CFClient::UI::Slider; 1997package CFClient::UI::Slider;
1883 # TODO: calculations are off 2016 # TODO: calculations are off
1884 my $self = $class->SUPER::new ( 2017 my $self = $class->SUPER::new (
1885 fg => [1, 1, 1], 2018 fg => [1, 1, 1],
1886 active_fg => [0, 0, 0], 2019 active_fg => [0, 0, 0],
1887 range => [0, 0, 100, 10], 2020 range => [0, 0, 100, 10],
1888 req_w => 20, 2021 req_w => $::WIDTH / 80,
1889 req_h => 20, 2022 req_h => $::WIDTH / 80,
1890 vertical => 0, 2023 vertical => 0,
1891 can_hover => 1, 2024 can_hover => 1,
1892 inner_pad => 5, 2025 inner_pad => 5,
1893 @_ 2026 @_
1894 ); 2027 );
2018 (new CFClient::UI::Empty expand => 1), 2151 (new CFClient::UI::Empty expand => 1),
2019 (new CFClient::UI::Slider vertical => 1), 2152 (new CFClient::UI::Slider vertical => 1),
2020 ], 2153 ],
2021 ); 2154 );
2022 2155
2023 $self->{children}[1]->connect (changed => sub { 2156 $self->{children}[1]->connect (changed => sub { $self->update });
2024 $self->update;
2025 });
2026 2157
2027 $self 2158 $self
2028} 2159}
2029 2160
2030sub set_fontsize { 2161sub set_fontsize {
2038 my ($self, $text) = @_; 2169 my ($self, $text) = @_;
2039 2170
2040 my $layout = $self->{layout}; 2171 my $layout = $self->{layout};
2041 2172
2042 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2173 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2043 $layout->set_width ($self->{w}); 2174 $layout->set_width ($self->{children}[0]{w});
2044 $layout->set_text ($text); 2175 $layout->set_text ($text);
2045 2176
2046 ($layout->size)[1] 2177 ($layout->size)[1]
2047} 2178}
2048 2179
2101 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2232 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2102 2233
2103 delete $self->{texture}; 2234 delete $self->{texture};
2104 } 2235 }
2105 2236
2106 $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 {
2107 glClearColor 0, 0, 0, 1; 2238 glClearColor 0, 0, 0, 0;
2108 glClear GL_COLOR_BUFFER_BIT; 2239 glClear GL_COLOR_BUFFER_BIT;
2109 2240
2110 glEnable GL_BLEND; 2241 glEnable GL_BLEND;
2111 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2242 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2112 glEnable GL_TEXTURE_2D; 2243 glEnable GL_TEXTURE_2D;
2144} 2275}
2145 2276
2146sub _draw { 2277sub _draw {
2147 my ($self) = @_; 2278 my ($self) = @_;
2148 2279
2149 if ($self->{texture}) { 2280 glEnable GL_BLEND;
2281 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2150 glEnable GL_TEXTURE_2D; 2282 glEnable GL_TEXTURE_2D;
2151 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2283 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2284 glColor 1, 1, 1, 1;
2152 $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});
2153 glDisable GL_TEXTURE_2D; 2286 glDisable GL_TEXTURE_2D;
2154 } 2287 glDisable GL_BLEND;
2155 2288
2156 $self->{children}[1]->draw; 2289 $self->{children}[1]->draw;
2157 2290
2158} 2291}
2159 2292
2328 2461
2329sub size_request { 2462sub size_request {
2330 (32, 8) 2463 (32, 8)
2331} 2464}
2332 2465
2333sub draw { 2466sub _draw {
2334 my ($self) = @_; 2467 my ($self) = @_;
2335 2468
2336 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2469 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2337 2470
2471 # TODO animation
2338 if ($tex) { 2472 if ($tex) {
2339 glEnable GL_BLEND; 2473 glEnable GL_BLEND;
2340 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2474 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2341 glEnable GL_TEXTURE_2D; 2475 glEnable GL_TEXTURE_2D;
2342 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2476 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2347 } 2481 }
2348} 2482}
2349 2483
2350############################################################################# 2484#############################################################################
2351 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
2352package CFClient::UI::Root; 2558package CFClient::UI::Root;
2353 2559
2354our @ISA = CFClient::UI::Container::; 2560our @ISA = CFClient::UI::Container::;
2355 2561
2356use CFClient::OpenGL; 2562use CFClient::OpenGL;
2428 $self->_draw; 2634 $self->_draw;
2429} 2635}
2430 2636
2431############################################################################# 2637#############################################################################
2432 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
2433package CFClient::UI; 2744package CFClient::UI;
2434 2745
2435$ROOT = new CFClient::UI::Root; 2746$ROOT = new CFClient::UI::Root;
2436$TOOLTIP = new CFClient::UI::Tooltip; 2747$TOOLTIP = new CFClient::UI::Tooltip;
2437 2748

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines