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.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
246} 270}
247 271
248# return top left coordinates 272# return top left coordinates
249sub _topleft { 273sub _topleft {
250 my ($self, $x, $y) = @_; 274 my ($self, $x, $y) = @_;
275
276 $self->{parent}
277 or Carp::confess "no parent widget in _topleft\n";#d#
251 278
252 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 279 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
253} 280}
254 281
255# translate global coordinates to local coordinate system 282# translate global coordinates to local coordinate system
515 sort { $a->{z} <=> $b->{z} } 542 sort { $a->{z} <=> $b->{z} }
516 @{$self->{children}}, $child 543 @{$self->{children}}, $child
517 ]; 544 ];
518 545
519 $child->check_size; 546 $child->check_size;
547 $self->update;
548}
549
550sub children {
551 @{ $_[0]{children} }
520} 552}
521 553
522sub remove { 554sub remove {
523 my ($self, $child) = @_; 555 my ($self, $child) = @_;
524 556
539 571
540 for (@$children) { 572 for (@$children) {
541 delete $_->{parent}; 573 delete $_->{parent};
542 $_->hide; 574 $_->hide;
543 } 575 }
576
577 $self->check_size;
578 $self->update;
544} 579}
545 580
546sub find_widget { 581sub find_widget {
547 my ($self, $x, $y) = @_; 582 my ($self, $x, $y) = @_;
548 583
623} 658}
624 659
625sub update { 660sub update {
626 my ($self) = @_; 661 my ($self) = @_;
627 662
628 # we want to do this delayed... 663 $ROOT->on_refresh ($self => sub { $self->render_child });
629 $self->render_chld;
630 $self->SUPER::update; 664 $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} 665}
642 666
643sub size_allocate { 667sub size_allocate {
644 my ($self, $w, $h) = @_; 668 my ($self, $w, $h) = @_;
645 669
646 $self->child->configure (0, 0, $w, $h); 670 $self->SUPER::size_allocate ($w, $h);
671 $self->update;
672}
647 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
648 $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 };
649} 694}
650 695
651sub _draw { 696sub _draw {
652 my ($self) = @_; 697 my ($self) = @_;
653 698
655 700
656 my $tex = $self->{texture} 701 my $tex = $self->{texture}
657 or return; 702 or return;
658 703
659 glEnable GL_BLEND; 704 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
662 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;
663 709
664 $tex->draw_quad (0, 0, $w, $h); 710 $tex->draw_quad (0, 0, $w, $h);
665 711
666 glDisable GL_BLEND; 712 glDisable GL_BLEND;
667 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
671 717
672package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
673 719
674our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
675 721
676sub new { die }
677
678sub size_request { 722sub size_request {
679 my ($self) = @_; 723 my ($self) = @_;
680 724
681 @$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)};
682 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
683 727
684 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
685} 729}
686 730
687sub _draw { 731sub size_allocate {
688 my ($self) = @_; 732 my ($self, $w, $h) = @_;
689 733
690 $self->{children}[1]->draw; 734 $self->update;
691} 735}
692 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
693 807
694############################################################################# 808#############################################################################
695 809
696package CFClient::UI::Frame; 810package CFClient::UI::Frame;
697 811
754 # TODO: user_x, user_y, overwrite moveto? 868 # TODO: user_x, user_y, overwrite moveto?
755 869
756 my $self = $class->SUPER::new ( 870 my $self = $class->SUPER::new (
757 bg => [1, 1, 1, 1], 871 bg => [1, 1, 1, 1],
758 border_bg => [1, 1, 1, 1], 872 border_bg => [1, 1, 1, 1],
759 border => 0.8, 873 border => 0.6,
760 can_events => 1, 874 can_events => 1,
761 @_ 875 @_
762 ); 876 );
763 877
764 $self->{title} &&= new CFClient::UI::Label 878 $self->{title} &&= new CFClient::UI::Label
765 align => 0, 879 align => 0,
766 valign => 1, 880 valign => 1,
767 text => $self->{title}, 881 text => $self->{title},
768 fontsize => 1; 882 fontsize => $self->{border};
769 883
770 $self 884 $self
771} 885}
772 886
773sub border { 887sub border {
798} 912}
799 913
800sub button_down { 914sub button_down {
801 my ($self, $ev, $x, $y) = @_; 915 my ($self, $ev, $x, $y) = @_;
802 916
917 my ($w, $h) = @$self{qw(w h)};
803 my $border = $self->border; 918 my $border = $self->border;
804 919
805 if ($x < $self->{w} && $x >= $self->{w} - $border 920 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
806 && $y < $self->{h} && $y >= $self->{h} - $border) { 921 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
807 922
923 if ($lr & $td) {
924 my ($wx, $wy) = ($self->{x}, $self->{y});
808 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 925 my ($ox, $oy) = ($ev->{x}, $ev->{y});
809 my ($bw, $bh) = ($self->{w}, $self->{h}); 926 my ($bw, $bh) = ($self->{w}, $self->{h});
810 927
928 my $mx = $x < $border;
929 my $my = $y < $border;
930
811 $self->{motion} = sub { 931 $self->{motion} = sub {
812 my ($ev, $x, $y) = @_; 932 my ($ev, $x, $y) = @_;
813 933
814 ($x, $y) = ($ev->{x}, $ev->{y}); 934 my $dx = $ev->{x} - $ox;
935 my $dy = $ev->{y} - $oy;
815 936
816 $self->{user_w} = $bw + $x - $ox; 937 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
817 $self->{user_h} = $bh + $y - $oy; 938 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
939 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
818 $self->check_size; 940 $self->check_size;
819 }; 941 };
820 942
821 } elsif ($x >= 0 && $x < $self->{w} 943 } elsif ($lr ^ $td) {
822 && $y >= 0 && $y < $border) {
823
824 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 944 my ($ox, $oy) = ($ev->{x}, $ev->{y});
825 my ($bx, $by) = ($self->{x}, $self->{y}); 945 my ($bx, $by) = ($self->{x}, $self->{y});
826 946
827 $self->{motion} = sub { 947 $self->{motion} = sub {
828 my ($ev, $x, $y) = @_; 948 my ($ev, $x, $y) = @_;
864 $tex[1]->draw_quad (0, 0, $w, $border); 984 $tex[1]->draw_quad (0, 0, $w, $border);
865 $tex[3]->draw_quad (0, $border, $border, $ch); 985 $tex[3]->draw_quad (0, $border, $border, $ch);
866 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 986 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
867 $tex[4]->draw_quad (0, $h - $border, $w, $border); 987 $tex[4]->draw_quad (0, $h - $border, $w, $border);
868 988
989 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
869 my $bg = $tex[0]; 990 my $bg = $tex[0];
870 991
871 # TODO: repeat texture not scale 992 # TODO: repeat texture not scale
872 my $rep_x = $cw / $bg->{w}; 993 my $rep_x = $cw / $bg->{w};
873 my $rep_y = $ch / $bg->{h}; 994 my $rep_y = $ch / $bg->{h};
874 995
875 glColor @{ $self->{bg} }; 996 glColor @{ $self->{bg} };
876 997
877 $bg->{s} = $rep_x; 998 $bg->{s} = $rep_x;
878 $bg->{t} = $rep_y; 999 $bg->{t} = $rep_y;
879 $bg->{wrap_mode} = 1; 1000 $bg->{wrap_mode} = 1;
880 $bg->draw_quad ($border, $border, $cw, $ch); 1001 $bg->draw_quad ($border, $border, $cw, $ch);
881 1002
882 glDisable GL_TEXTURE_2D; 1003 glDisable GL_TEXTURE_2D;
883 glDisable GL_BLEND; 1004 glDisable GL_BLEND;
1005 }
884 1006
885 $self->{title}->draw if $self->{title}; 1007 $self->{title}->draw if $self->{title};
1008
886 $self->child->draw; 1009 $self->child->draw;
887} 1010}
888 1011
889############################################################################# 1012#############################################################################
890 1013
912 $self->{children}[$y][$x] = $child; 1035 $self->{children}[$y][$x] = $child;
913 1036
914 $child->check_size; 1037 $child->check_size;
915} 1038}
916 1039
1040sub children {
1041 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1042}
1043
917# 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?
918sub clear { 1045sub clear {
919 my ($self) = @_; 1046 my ($self) = @_;
920 1047
921 my $children = delete $self->{children}; 1048 my @children = $self->children;
1049 delete $self->{children};
922 1050
923 for (grep $_, map @$_, grep $_, @$children) { 1051 for (@children) {
924 delete $_->{parent}; 1052 delete $_->{parent};
925 $_->hide; 1053 $_->hide;
926 } 1054 }
927 1055
928 $self->update; 1056 $self->update;
1189 s/</&lt;/g; 1317 s/</&lt;/g;
1190 1318
1191 $_[1] 1319 $_[1]
1192} 1320}
1193 1321
1322sub update {
1323 my ($self) = @_;
1324
1325 delete $self->{texture};
1326 $self->SUPER::update;
1327}
1328
1194sub set_text { 1329sub set_text {
1195 my ($self, $text) = @_; 1330 my ($self, $text) = @_;
1196 1331
1332 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text";
1334
1197 $self->{layout}->set_text ($text); 1335 $self->{layout}->set_text ($text);
1198 1336
1199 delete $self->{texture}; 1337 delete $self->{texture};
1338 $self->update;
1200 $self->check_size; 1339 $self->check_size;
1201 $self->update;
1202} 1340}
1203 1341
1204sub set_markup { 1342sub set_markup {
1205 my ($self, $markup) = @_; 1343 my ($self, $markup) = @_;
1206 1344
1345 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup";
1347
1207 $self->{layout}->set_markup ($markup); 1348 $self->{layout}->set_markup ($markup);
1208 1349
1209 delete $self->{texture}; 1350 delete $self->{texture};
1351 $self->update;
1210 $self->check_size; 1352 $self->check_size;
1211 $self->update;
1212} 1353}
1213 1354
1214sub size_request { 1355sub size_request {
1215 my ($self) = @_; 1356 my ($self) = @_;
1216 1357
1245sub set_fontsize { 1386sub set_fontsize {
1246 my ($self, $fontsize) = @_; 1387 my ($self, $fontsize) = @_;
1247 1388
1248 $self->{fontsize} = $fontsize; 1389 $self->{fontsize} = $fontsize;
1249 delete $self->{texture}; 1390 delete $self->{texture};
1391
1392 $self->update;
1250 $self->check_size; 1393 $self->check_size;
1251 $self->update;
1252} 1394}
1253 1395
1254sub _draw { 1396sub _draw {
1255 my ($self) = @_; 1397 my ($self) = @_;
1256 1398
1469 1611
1470 if ($sym == 13) { 1612 if ($sym == 13) {
1471 unshift @{$self->{history}}, 1613 unshift @{$self->{history}},
1472 my $txt = $self->get_text; 1614 my $txt = $self->get_text;
1473 $self->{history_pointer} = -1; 1615 $self->{history_pointer} = -1;
1616 $self->{history_saveback} = '';
1474 $self->emit (activate => $txt); 1617 $self->emit (activate => $txt);
1475 $self->update; 1618 $self->update;
1476 1619
1477 } elsif ($sym == CFClient::SDLK_UP) { 1620 } elsif ($sym == CFClient::SDLK_UP) {
1478 if ($self->{history_pointer} < 0) { 1621 if ($self->{history_pointer} < 0) {
1479 $self->{history_saveback} = $self->get_text; 1622 $self->{history_saveback} = $self->get_text;
1480 } 1623 }
1624 if (@{$self->{history} || []} > 0) {
1481 $self->{history_pointer}++; 1625 $self->{history_pointer}++;
1482 if ($self->{history_pointer} >= @{$self->{history}}) { 1626 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1483 $self->{history_pointer} = @{$self->{history}} - 1; 1627 $self->{history_pointer} = @{$self->{history} || []} - 1;
1628 }
1629 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1484 } 1630 }
1485 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1486 1631
1487 } elsif ($sym == CFClient::SDLK_DOWN) { 1632 } elsif ($sym == CFClient::SDLK_DOWN) {
1488 $self->{history_pointer}--; 1633 $self->{history_pointer}--;
1489 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 1634 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1490 1635
1737} 1882}
1738 1883
1739sub set_max { 1884sub set_max {
1740 my ($self, $max) = @_; 1885 my ($self, $max) = @_;
1741 1886
1887 return if $self->{max_val} == $max;
1888
1742 $self->{max_val} = $max; 1889 $self->{max_val} = $max;
1890 $self->update;
1743} 1891}
1744 1892
1745sub set_value { 1893sub set_value {
1746 my ($self, $val, $max) = @_; 1894 my ($self, $val, $max) = @_;
1747 1895
1748 $self->set_max ($max) 1896 $self->set_max ($max)
1749 if defined $max; 1897 if defined $max;
1750 1898
1751 $max = $self->{max_val}; 1899 return if $self->{val} == $val;
1900
1752 $self->{val} = $val; 1901 $self->{val} = $val;
1753
1754 $self->update; 1902 $self->update;
1755} 1903}
1756 1904
1757sub _draw { 1905sub _draw {
1758 my ($self) = @_; 1906 my ($self) = @_;
1821 1969
1822sub new { 1970sub new {
1823 my ($class, %arg) = @_; 1971 my ($class, %arg) = @_;
1824 1972
1825 my $self = $class->SUPER::new ( 1973 my $self = $class->SUPER::new (
1826 tooltip => $arg{type}, 1974 tooltip => $arg{type},
1827 can_hover => 1, 1975 can_hover => 1,
1976 can_events => 1,
1828 %arg, 1977 %arg,
1829 ); 1978 );
1830 1979
1831 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1980 $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); 1981 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1840 1989
1841 $self->{value}->set_fontsize ($fsize); 1990 $self->{value}->set_fontsize ($fsize);
1842 $self->{max} ->set_fontsize ($fsize); 1991 $self->{max} ->set_fontsize ($fsize);
1843} 1992}
1844 1993
1994sub set_max {
1995 my ($self, $max) = @_;
1996
1997 $self->{gauge}->set_max ($max);
1998 $self->{max}->set_text ($max);
1999}
2000
1845sub set_value { 2001sub set_value {
1846 my ($self, $val, $max) = @_; 2002 my ($self, $val, $max) = @_;
1847 2003
1848 $self->set_max ($max) 2004 $self->set_max ($max)
1849 if defined $max; 2005 if defined $max;
1850 2006
1851 $self->{gauge}->set_value ($val, $max); 2007 $self->{gauge}->set_value ($val, $max);
1852 $self->{value}->set_text ($val); 2008 $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} 2009}
1861 2010
1862############################################################################# 2011#############################################################################
1863 2012
1864package CFClient::UI::Slider; 2013package CFClient::UI::Slider;
1883 # TODO: calculations are off 2032 # TODO: calculations are off
1884 my $self = $class->SUPER::new ( 2033 my $self = $class->SUPER::new (
1885 fg => [1, 1, 1], 2034 fg => [1, 1, 1],
1886 active_fg => [0, 0, 0], 2035 active_fg => [0, 0, 0],
1887 range => [0, 0, 100, 10], 2036 range => [0, 0, 100, 10],
1888 req_w => 20, 2037 req_w => $::WIDTH / 80,
1889 req_h => 20, 2038 req_h => $::WIDTH / 80,
1890 vertical => 0, 2039 vertical => 0,
1891 can_hover => 1, 2040 can_hover => 1,
1892 inner_pad => 5, 2041 inner_pad => 5,
1893 @_ 2042 @_
1894 ); 2043 );
2018 (new CFClient::UI::Empty expand => 1), 2167 (new CFClient::UI::Empty expand => 1),
2019 (new CFClient::UI::Slider vertical => 1), 2168 (new CFClient::UI::Slider vertical => 1),
2020 ], 2169 ],
2021 ); 2170 );
2022 2171
2023 $self->{children}[1]->connect (changed => sub { 2172 $self->{children}[1]->connect (changed => sub { $self->update });
2024 $self->update;
2025 });
2026 2173
2027 $self 2174 $self
2028} 2175}
2029 2176
2030sub set_fontsize { 2177sub set_fontsize {
2038 my ($self, $text) = @_; 2185 my ($self, $text) = @_;
2039 2186
2040 my $layout = $self->{layout}; 2187 my $layout = $self->{layout};
2041 2188
2042 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2043 $layout->set_width ($self->{w}); 2190 $layout->set_width ($self->{children}[0]{w});
2044 $layout->set_text ($text); 2191 $layout->set_text ($text);
2045 2192
2046 ($layout->size)[1] 2193 ($layout->size)[1]
2047} 2194}
2048 2195
2101 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2248 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2102 2249
2103 delete $self->{texture}; 2250 delete $self->{texture};
2104 } 2251 }
2105 2252
2106 $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 {
2107 glClearColor 0, 0, 0, 1; 2254 glClearColor 0, 0, 0, 0;
2108 glClear GL_COLOR_BUFFER_BIT; 2255 glClear GL_COLOR_BUFFER_BIT;
2109 2256
2110 glEnable GL_BLEND; 2257 glEnable GL_BLEND;
2111 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2112 glEnable GL_TEXTURE_2D; 2259 glEnable GL_TEXTURE_2D;
2144} 2291}
2145 2292
2146sub _draw { 2293sub _draw {
2147 my ($self) = @_; 2294 my ($self) = @_;
2148 2295
2149 if ($self->{texture}) { 2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2150 glEnable GL_TEXTURE_2D; 2298 glEnable GL_TEXTURE_2D;
2151 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;
2152 $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});
2153 glDisable GL_TEXTURE_2D; 2302 glDisable GL_TEXTURE_2D;
2154 } 2303 glDisable GL_BLEND;
2155 2304
2156 $self->{children}[1]->draw; 2305 $self->{children}[1]->draw;
2157 2306
2158} 2307}
2159 2308
2328 2477
2329sub size_request { 2478sub size_request {
2330 (32, 8) 2479 (32, 8)
2331} 2480}
2332 2481
2333sub draw { 2482sub _draw {
2334 my ($self) = @_; 2483 my ($self) = @_;
2335 2484
2485 return unless $::CONN;#d# manage and cache textures differently
2336 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2337 2487
2488 # TODO animation
2338 if ($tex) { 2489 if ($tex) {
2339 glEnable GL_BLEND; 2490 glEnable GL_BLEND;
2340 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2341 glEnable GL_TEXTURE_2D; 2492 glEnable GL_TEXTURE_2D;
2342 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2347 } 2498 }
2348} 2499}
2349 2500
2350############################################################################# 2501#############################################################################
2351 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
2352package CFClient::UI::Root; 2680package CFClient::UI::Root;
2353 2681
2354our @ISA = CFClient::UI::Container::; 2682our @ISA = CFClient::UI::Container::;
2355 2683
2356use CFClient::OpenGL; 2684use CFClient::OpenGL;
2357 2685
2358sub check_size { 2686sub check_size {
2359 my ($self) = @_; 2687 my ($self) = @_;
2360 2688
2361 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2689 $self->configure (0, 0, $self->{w}, $self->{h});
2362} 2690}
2363 2691
2364sub size_request { 2692sub size_request {
2365 ($::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;
2366} 2719}
2367 2720
2368sub configure { 2721sub configure {
2369 my ($self, $x, $y, $w, $h) = @_; 2722 my ($self, $x, $y, $w, $h) = @_;
2370 2723
2371 $self->SUPER::configure ($x, $y, $w, $h); 2724 $self->SUPER::configure ($x, $y, $w, $h);
2372 2725
2373 for my $child (@{$self->{children}}) { 2726 for my $child ($self->children) {
2374 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)};
2375 2728
2376 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2729 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2377 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2378 $child->configure ($X, $Y, $W,$H); 2731 $child->configure ($X, $Y, $W, $H);
2379 } 2732 }
2380} 2733}
2381 2734
2382sub _topleft { 2735sub _topleft {
2383 my ($self, $x, $y) = @_; 2736 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines