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.163 by root, Mon Apr 24 06:40:29 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 }
137 145
138sub new { 146sub new {
139 my $class = shift; 147 my $class = shift;
140 148
141 my $self = bless { 149 my $self = bless {
142 x => 0, 150 x => 0,
143 y => 0, 151 y => 0,
144 z => 0, 152 z => 0,
145 can_events => 1, 153 can_events => 1,
146 @_ 154 @_
147 }, $class; 155 }, $class;
148 156
149 for (keys %$self) { 157 for (keys %$self) {
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
492sub new { 519sub new {
493 my ($class, %arg) = @_; 520 my ($class, %arg) = @_;
494 521
495 my $children = delete $arg{children} || []; 522 my $children = delete $arg{children} || [];
496 523
497 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 524 my $self = $class->SUPER::new (
525 children => [],
526 can_events => 0,
527 %arg,
528 );
498 $self->add ($_) for @$children; 529 $self->add ($_) for @$children;
499 530
500 $self 531 $self
501} 532}
502 533
511 sort { $a->{z} <=> $b->{z} } 542 sort { $a->{z} <=> $b->{z} }
512 @{$self->{children}}, $child 543 @{$self->{children}}, $child
513 ]; 544 ];
514 545
515 $child->check_size; 546 $child->check_size;
547 $self->update;
548}
549
550sub children {
551 @{ $_[0]{children} }
516} 552}
517 553
518sub remove { 554sub remove {
519 my ($self, $child) = @_; 555 my ($self, $child) = @_;
520 556
535 571
536 for (@$children) { 572 for (@$children) {
537 delete $_->{parent}; 573 delete $_->{parent};
538 $_->hide; 574 $_->hide;
539 } 575 }
576
577 $self->check_size;
578 $self->update;
540} 579}
541 580
542sub find_widget { 581sub find_widget {
543 my ($self, $x, $y) = @_; 582 my ($self, $x, $y) = @_;
544 583
619} 658}
620 659
621sub update { 660sub update {
622 my ($self) = @_; 661 my ($self) = @_;
623 662
624 # we want to do this delayed... 663 $ROOT->on_refresh ($self => sub { $self->render_child });
625 $self->render_chld;
626 $self->SUPER::update; 664 $self->SUPER::update;
627}
628
629sub render_chld {
630 my ($self) = @_;
631
632 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
633 glClearColor 0, 0, 0, 1;
634 glClear GL_COLOR_BUFFER_BIT;
635 $self->child->draw;
636 };
637} 665}
638 666
639sub size_allocate { 667sub size_allocate {
640 my ($self, $w, $h) = @_; 668 my ($self, $w, $h) = @_;
641 669
642 $self->child->configure (0, 0, $w, $h); 670 $self->SUPER::size_allocate ($w, $h);
671 $self->update;
672}
643 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
644 $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 };
645} 694}
646 695
647sub _draw { 696sub _draw {
648 my ($self) = @_; 697 my ($self) = @_;
649 698
651 700
652 my $tex = $self->{texture} 701 my $tex = $self->{texture}
653 or return; 702 or return;
654 703
655 glEnable GL_BLEND; 704 glEnable GL_BLEND;
656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
657 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
658 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;
659 709
660 $tex->draw_quad (0, 0, $w, $h); 710 $tex->draw_quad (0, 0, $w, $h);
661 711
662 glDisable GL_BLEND; 712 glDisable GL_BLEND;
663 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
667 717
668package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
669 719
670our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
671 721
672sub new { die }
673
674sub size_request { 722sub size_request {
675 my ($self) = @_; 723 my ($self) = @_;
676 724
677 @$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)};
678 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
679 727
680 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
681} 729}
682 730
683sub _draw { 731sub size_allocate {
684 my ($self) = @_; 732 my ($self, $w, $h) = @_;
685 733
686 $self->{children}[1]->draw; 734 $self->update;
687} 735}
688 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
689 807
690############################################################################# 808#############################################################################
691 809
692package CFClient::UI::Frame; 810package CFClient::UI::Frame;
693 811
748 my $class = shift; 866 my $class = shift;
749 867
750 # TODO: user_x, user_y, overwrite moveto? 868 # TODO: user_x, user_y, overwrite moveto?
751 869
752 my $self = $class->SUPER::new ( 870 my $self = $class->SUPER::new (
753 bg => [1, 1, 1, 1], 871 bg => [1, 1, 1, 1],
754 border_bg => [1, 1, 1, 1], 872 border_bg => [1, 1, 1, 1],
755 border => 0.8, 873 border => 0.6,
756 can_events => 1, 874 can_events => 1,
757 @_ 875 @_
758 ); 876 );
759 877
760 $self->{title} &&= new CFClient::UI::Label 878 $self->{title} &&= new CFClient::UI::Label
761 align => 0, 879 align => 0,
762 valign => 1, 880 valign => 1,
763 text => $self->{title}, 881 text => $self->{title},
764 fontsize => 1; 882 fontsize => $self->{border};
765 883
766 $self 884 $self
767} 885}
768 886
769sub border { 887sub border {
794} 912}
795 913
796sub button_down { 914sub button_down {
797 my ($self, $ev, $x, $y) = @_; 915 my ($self, $ev, $x, $y) = @_;
798 916
917 my ($w, $h) = @$self{qw(w h)};
799 my $border = $self->border; 918 my $border = $self->border;
800 919
801 if ($x < $self->{w} && $x >= $self->{w} - $border 920 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
802 && $y < $self->{h} && $y >= $self->{h} - $border) { 921 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
803 922
923 if ($lr & $td) {
924 my ($wx, $wy) = ($self->{x}, $self->{y});
804 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 925 my ($ox, $oy) = ($ev->{x}, $ev->{y});
805 my ($bw, $bh) = ($self->{w}, $self->{h}); 926 my ($bw, $bh) = ($self->{w}, $self->{h});
806 927
928 my $mx = $x < $border;
929 my $my = $y < $border;
930
807 $self->{motion} = sub { 931 $self->{motion} = sub {
808 my ($ev, $x, $y) = @_; 932 my ($ev, $x, $y) = @_;
809 933
810 ($x, $y) = ($ev->{x}, $ev->{y}); 934 my $dx = $ev->{x} - $ox;
935 my $dy = $ev->{y} - $oy;
811 936
812 $self->{user_w} = $bw + $x - $ox; 937 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
813 $self->{user_h} = $bh + $y - $oy; 938 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
939 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
814 $self->check_size; 940 $self->check_size;
815 }; 941 };
816 942
817 } elsif ($x >= 0 && $x < $self->{w} 943 } elsif ($lr ^ $td) {
818 && $y >= 0 && $y < $border) {
819
820 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 944 my ($ox, $oy) = ($ev->{x}, $ev->{y});
821 my ($bx, $by) = ($self->{x}, $self->{y}); 945 my ($bx, $by) = ($self->{x}, $self->{y});
822 946
823 $self->{motion} = sub { 947 $self->{motion} = sub {
824 my ($ev, $x, $y) = @_; 948 my ($ev, $x, $y) = @_;
860 $tex[1]->draw_quad (0, 0, $w, $border); 984 $tex[1]->draw_quad (0, 0, $w, $border);
861 $tex[3]->draw_quad (0, $border, $border, $ch); 985 $tex[3]->draw_quad (0, $border, $border, $ch);
862 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 986 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
863 $tex[4]->draw_quad (0, $h - $border, $w, $border); 987 $tex[4]->draw_quad (0, $h - $border, $w, $border);
864 988
989 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
865 my $bg = $tex[0]; 990 my $bg = $tex[0];
866 991
867 # TODO: repeat texture not scale 992 # TODO: repeat texture not scale
868 my $rep_x = $cw / $bg->{w}; 993 my $rep_x = $cw / $bg->{w};
869 my $rep_y = $ch / $bg->{h}; 994 my $rep_y = $ch / $bg->{h};
870 995
871 glColor @{ $self->{bg} }; 996 glColor @{ $self->{bg} };
872 997
873 $bg->{s} = $rep_x; 998 $bg->{s} = $rep_x;
874 $bg->{t} = $rep_y; 999 $bg->{t} = $rep_y;
875 $bg->{wrap_mode} = 1; 1000 $bg->{wrap_mode} = 1;
876 $bg->draw_quad ($border, $border, $cw, $ch); 1001 $bg->draw_quad ($border, $border, $cw, $ch);
877 1002
878 glDisable GL_TEXTURE_2D; 1003 glDisable GL_TEXTURE_2D;
879 glDisable GL_BLEND; 1004 glDisable GL_BLEND;
1005 }
880 1006
881 $self->{title}->draw if $self->{title}; 1007 $self->{title}->draw if $self->{title};
1008
882 $self->child->draw; 1009 $self->child->draw;
883} 1010}
884 1011
885############################################################################# 1012#############################################################################
886 1013
908 $self->{children}[$y][$x] = $child; 1035 $self->{children}[$y][$x] = $child;
909 1036
910 $child->check_size; 1037 $child->check_size;
911} 1038}
912 1039
1040sub children {
1041 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1042}
1043
913# 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?
914sub clear { 1045sub clear {
915 my ($self) = @_; 1046 my ($self) = @_;
916 1047
917 my $children = delete $self->{children}; 1048 my @children = $self->children;
1049 delete $self->{children};
918 1050
919 for (grep $_, map @$_, grep $_, @$children) { 1051 for (@children) {
920 delete $_->{parent}; 1052 delete $_->{parent};
921 $_->hide; 1053 $_->hide;
922 } 1054 }
923 1055
924 $self->update; 1056 $self->update;
1151 1283
1152sub new { 1284sub new {
1153 my ($class, %arg) = @_; 1285 my ($class, %arg) = @_;
1154 1286
1155 my $self = $class->SUPER::new ( 1287 my $self = $class->SUPER::new (
1156 fg => [1, 1, 1], 1288 fg => [1, 1, 1],
1157 #font => default_font 1289 #font => default_font
1158 fontsize => 1, 1290 fontsize => 1,
1159 text => "", 1291 text => "",
1160 align => -1, 1292 align => -1,
1161 valign => -1, 1293 valign => -1,
1162 padding => 2, 1294 padding => 2,
1163 layout => new CFClient::Layout, 1295 layout => new CFClient::Layout,
1164 can_events => 0, 1296 can_events => 0,
1165 %arg 1297 %arg
1166 ); 1298 );
1167 1299
1168 if (exists $self->{template}) { 1300 if (exists $self->{template}) {
1185 s/</&lt;/g; 1317 s/</&lt;/g;
1186 1318
1187 $_[1] 1319 $_[1]
1188} 1320}
1189 1321
1322sub update {
1323 my ($self) = @_;
1324
1325 delete $self->{texture};
1326 $self->SUPER::update;
1327}
1328
1190sub set_text { 1329sub set_text {
1191 my ($self, $text) = @_; 1330 my ($self, $text) = @_;
1192 1331
1332 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text";
1334
1193 $self->{layout}->set_text ($text); 1335 $self->{layout}->set_text ($text);
1194 1336
1195 delete $self->{texture}; 1337 delete $self->{texture};
1338 $self->update;
1196 $self->check_size; 1339 $self->check_size;
1197 $self->update;
1198} 1340}
1199 1341
1200sub set_markup { 1342sub set_markup {
1201 my ($self, $markup) = @_; 1343 my ($self, $markup) = @_;
1202 1344
1345 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup";
1347
1203 $self->{layout}->set_markup ($markup); 1348 $self->{layout}->set_markup ($markup);
1204 1349
1205 delete $self->{texture}; 1350 delete $self->{texture};
1351 $self->update;
1206 $self->check_size; 1352 $self->check_size;
1207 $self->update;
1208} 1353}
1209 1354
1210sub size_request { 1355sub size_request {
1211 my ($self) = @_; 1356 my ($self) = @_;
1212 1357
1241sub set_fontsize { 1386sub set_fontsize {
1242 my ($self, $fontsize) = @_; 1387 my ($self, $fontsize) = @_;
1243 1388
1244 $self->{fontsize} = $fontsize; 1389 $self->{fontsize} = $fontsize;
1245 delete $self->{texture}; 1390 delete $self->{texture};
1391
1392 $self->update;
1246 $self->check_size; 1393 $self->check_size;
1247 $self->update;
1248} 1394}
1249 1395
1250sub _draw { 1396sub _draw {
1251 my ($self) = @_; 1397 my ($self) = @_;
1252 1398
1292 1438
1293sub new { 1439sub new {
1294 my $class = shift; 1440 my $class = shift;
1295 1441
1296 $class->SUPER::new ( 1442 $class->SUPER::new (
1297 fg => [1, 1, 1], 1443 fg => [1, 1, 1],
1298 bg => [0, 0, 0, 0.2], 1444 bg => [0, 0, 0, 0.2],
1299 active_bg => [1, 1, 1, 0.5], 1445 active_bg => [1, 1, 1, 0.5],
1300 active_fg => [0, 0, 0], 1446 active_fg => [0, 0, 0],
1301 can_hover => 1, 1447 can_hover => 1,
1302 can_focus => 1, 1448 can_focus => 1,
1303 valign => 0, 1449 valign => 0,
1304 can_events => 1, 1450 can_events => 1,
1305 @_ 1451 @_
1306 ) 1452 )
1307} 1453}
1308 1454
1462 my ($self, $ev) = @_; 1608 my ($self, $ev) = @_;
1463 1609
1464 my $sym = $ev->{sym}; 1610 my $sym = $ev->{sym};
1465 1611
1466 if ($sym == 13) { 1612 if ($sym == 13) {
1613 unshift @{$self->{history}},
1614 my $txt = $self->get_text;
1615 $self->{history_pointer} = -1;
1616 $self->{history_saveback} = '';
1467 $self->emit (activate => $self->get_text); 1617 $self->emit (activate => $txt);
1468 $self->update; 1618 $self->update;
1619
1620 } elsif ($sym == CFClient::SDLK_UP) {
1621 if ($self->{history_pointer} < 0) {
1622 $self->{history_saveback} = $self->get_text;
1623 }
1624 if (@{$self->{history} || []} > 0) {
1625 $self->{history_pointer}++;
1626 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1627 $self->{history_pointer} = @{$self->{history} || []} - 1;
1628 }
1629 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1630 }
1631
1632 } elsif ($sym == CFClient::SDLK_DOWN) {
1633 $self->{history_pointer}--;
1634 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1635
1636 if ($self->{history_pointer} >= 0) {
1637 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1638 } else {
1639 $self->set_text ($self->{history_saveback});
1640 }
1469 1641
1470 } else { 1642 } else {
1471 $self->SUPER::key_down ($ev); 1643 $self->SUPER::key_down ($ev);
1472 } 1644 }
1473 1645
1487 1659
1488sub new { 1660sub new {
1489 my $class = shift; 1661 my $class = shift;
1490 1662
1491 $class->SUPER::new ( 1663 $class->SUPER::new (
1492 padding => 4, 1664 padding => 4,
1493 fg => [1, 1, 1], 1665 fg => [1, 1, 1],
1494 bg => [1, 1, 1, 0.2], 1666 bg => [1, 1, 1, 0.2],
1495 active_fg => [0, 0, 1], 1667 active_fg => [0, 0, 1],
1496 can_hover => 1, 1668 can_hover => 1,
1497 align => 0, 1669 align => 0,
1498 valign => 0, 1670 valign => 0,
1499 can_events => 1, 1671 can_events => 1,
1500 @_ 1672 @_
1501 ) 1673 )
1502} 1674}
1503 1675
1710} 1882}
1711 1883
1712sub set_max { 1884sub set_max {
1713 my ($self, $max) = @_; 1885 my ($self, $max) = @_;
1714 1886
1887 return if $self->{max_val} == $max;
1888
1715 $self->{max_val} = $max; 1889 $self->{max_val} = $max;
1890 $self->update;
1716} 1891}
1717 1892
1718sub set_value { 1893sub set_value {
1719 my ($self, $val, $max) = @_; 1894 my ($self, $val, $max) = @_;
1720 1895
1721 $self->set_max ($max) 1896 $self->set_max ($max)
1722 if defined $max; 1897 if defined $max;
1723 1898
1724 $max = $self->{max_val}; 1899 return if $self->{val} == $val;
1900
1725 $self->{val} = $val; 1901 $self->{val} = $val;
1726
1727 $self->update; 1902 $self->update;
1728} 1903}
1729 1904
1730sub _draw { 1905sub _draw {
1731 my ($self) = @_; 1906 my ($self) = @_;
1794 1969
1795sub new { 1970sub new {
1796 my ($class, %arg) = @_; 1971 my ($class, %arg) = @_;
1797 1972
1798 my $self = $class->SUPER::new ( 1973 my $self = $class->SUPER::new (
1799 tooltip => $arg{type}, 1974 tooltip => $arg{type},
1975 can_hover => 1,
1800 can_events => 1, 1976 can_events => 1,
1801 can_hover => 1,
1802 %arg, 1977 %arg,
1803 ); 1978 );
1804 1979
1805 $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");
1806 $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);
1814 1989
1815 $self->{value}->set_fontsize ($fsize); 1990 $self->{value}->set_fontsize ($fsize);
1816 $self->{max} ->set_fontsize ($fsize); 1991 $self->{max} ->set_fontsize ($fsize);
1817} 1992}
1818 1993
1994sub set_max {
1995 my ($self, $max) = @_;
1996
1997 $self->{gauge}->set_max ($max);
1998 $self->{max}->set_text ($max);
1999}
2000
1819sub set_value { 2001sub set_value {
1820 my ($self, $val, $max) = @_; 2002 my ($self, $val, $max) = @_;
1821 2003
1822 $self->set_max ($max) 2004 $self->set_max ($max)
1823 if defined $max; 2005 if defined $max;
1824 2006
1825 $self->{gauge}->set_value ($val, $max); 2007 $self->{gauge}->set_value ($val, $max);
1826 $self->{value}->set_text ($val); 2008 $self->{value}->set_text ($val);
1827}
1828
1829sub set_max {
1830 my ($self, $max) = @_;
1831
1832 $self->{gauge}->set_max ($max);
1833 $self->{max}->set_text ($max);
1834} 2009}
1835 2010
1836############################################################################# 2011#############################################################################
1837 2012
1838package CFClient::UI::Slider; 2013package CFClient::UI::Slider;
1857 # TODO: calculations are off 2032 # TODO: calculations are off
1858 my $self = $class->SUPER::new ( 2033 my $self = $class->SUPER::new (
1859 fg => [1, 1, 1], 2034 fg => [1, 1, 1],
1860 active_fg => [0, 0, 0], 2035 active_fg => [0, 0, 0],
1861 range => [0, 0, 100, 10], 2036 range => [0, 0, 100, 10],
1862 req_w => 20, 2037 req_w => $::WIDTH / 80,
1863 req_h => 20, 2038 req_h => $::WIDTH / 80,
1864 vertical => 0, 2039 vertical => 0,
1865 can_hover => 1, 2040 can_hover => 1,
1866 inner_pad => 5, 2041 inner_pad => 5,
1867 @_ 2042 @_
1868 ); 2043 );
1978 2153
1979sub new { 2154sub new {
1980 my $class = shift; 2155 my $class = shift;
1981 2156
1982 my $self = $class->SUPER::new ( 2157 my $self = $class->SUPER::new (
1983 fontsize => 1, 2158 fontsize => 1,
2159 can_events => 0,
1984 #font => default_font 2160 #font => default_font
1985 @_, 2161 @_,
1986 2162
1987 layout => (new CFClient::Layout), 2163 layout => (new CFClient::Layout),
1988 par => [], 2164 par => [],
1989 height => 0, 2165 height => 0,
1990 children => [ 2166 children => [
1991 (new CFClient::UI::Empty expand => 1), 2167 (new CFClient::UI::Empty expand => 1),
1992 (new CFClient::UI::Slider vertical => 1), 2168 (new CFClient::UI::Slider vertical => 1),
1993 ], 2169 ],
1994 ); 2170 );
1995 2171
1996 $self->{children}[1]->connect (changed => sub { 2172 $self->{children}[1]->connect (changed => sub { $self->update });
1997 $self->update;
1998 });
1999 2173
2000 $self 2174 $self
2001} 2175}
2002 2176
2003sub set_fontsize { 2177sub set_fontsize {
2011 my ($self, $text) = @_; 2185 my ($self, $text) = @_;
2012 2186
2013 my $layout = $self->{layout}; 2187 my $layout = $self->{layout};
2014 2188
2015 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2016 $layout->set_width ($self->{w}); 2190 $layout->set_width ($self->{children}[0]{w});
2017 $layout->set_text ($text); 2191 $layout->set_text ($text);
2018 2192
2019 ($layout->size)[1] 2193 ($layout->size)[1]
2020} 2194}
2021 2195
2074 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2248 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2075 2249
2076 delete $self->{texture}; 2250 delete $self->{texture};
2077 } 2251 }
2078 2252
2079 $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 {
2080 glClearColor 0, 0, 0, 1; 2254 glClearColor 0, 0, 0, 0;
2081 glClear GL_COLOR_BUFFER_BIT; 2255 glClear GL_COLOR_BUFFER_BIT;
2082 2256
2083 glEnable GL_BLEND; 2257 glEnable GL_BLEND;
2084 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2085 glEnable GL_TEXTURE_2D; 2259 glEnable GL_TEXTURE_2D;
2117} 2291}
2118 2292
2119sub _draw { 2293sub _draw {
2120 my ($self) = @_; 2294 my ($self) = @_;
2121 2295
2122 if ($self->{texture}) { 2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2123 glEnable GL_TEXTURE_2D; 2298 glEnable GL_TEXTURE_2D;
2124 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;
2125 $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});
2126 glDisable GL_TEXTURE_2D; 2302 glDisable GL_TEXTURE_2D;
2127 } 2303 glDisable GL_BLEND;
2128 2304
2129 $self->{children}[1]->draw; 2305 $self->{children}[1]->draw;
2130 2306
2131} 2307}
2132 2308
2180 2356
2181sub new { 2357sub new {
2182 my $class = shift; 2358 my $class = shift;
2183 2359
2184 my $self = $class->SUPER::new ( 2360 my $self = $class->SUPER::new (
2185 state => 0, 2361 state => 0,
2186 connect_activate => \&toggle_flopper, 2362 connect_activate => \&toggle_flopper,
2187 can_events => 1,
2188 @_ 2363 @_
2189 ); 2364 );
2190 2365
2191 if ($self->{state}) { 2366 if ($self->{state}) {
2192 $self->{state} = 0; 2367 $self->{state} = 0;
2302 2477
2303sub size_request { 2478sub size_request {
2304 (32, 8) 2479 (32, 8)
2305} 2480}
2306 2481
2307sub draw { 2482sub _draw {
2308 my ($self) = @_; 2483 my ($self) = @_;
2309 2484
2485 return unless $::CONN;#d# manage and cache textures differently
2310 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2311 2487
2488 # TODO animation
2312 if ($tex) { 2489 if ($tex) {
2313 glEnable GL_BLEND; 2490 glEnable GL_BLEND;
2314 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2315 glEnable GL_TEXTURE_2D; 2492 glEnable GL_TEXTURE_2D;
2316 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2321 } 2498 }
2322} 2499}
2323 2500
2324############################################################################# 2501#############################################################################
2325 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
2326package CFClient::UI::Root; 2680package CFClient::UI::Root;
2327 2681
2328our @ISA = CFClient::UI::Container::; 2682our @ISA = CFClient::UI::Container::;
2329 2683
2330use CFClient::OpenGL; 2684use CFClient::OpenGL;
2331 2685
2332sub check_size { 2686sub check_size {
2333 my ($self) = @_; 2687 my ($self) = @_;
2334 2688
2335 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2689 $self->configure (0, 0, $self->{w}, $self->{h});
2336} 2690}
2337 2691
2338sub size_request { 2692sub size_request {
2339 ($::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;
2340} 2719}
2341 2720
2342sub configure { 2721sub configure {
2343 my ($self, $x, $y, $w, $h) = @_; 2722 my ($self, $x, $y, $w, $h) = @_;
2344 2723
2345 $self->SUPER::configure ($x, $y, $w, $h); 2724 $self->SUPER::configure ($x, $y, $w, $h);
2346 2725
2347 for my $child (@{$self->{children}}) { 2726 for my $child ($self->children) {
2348 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)};
2349 2728
2350 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2729 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2351 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2352 $child->configure ($X, $Y, $W,$H); 2731 $child->configure ($X, $Y, $W, $H);
2353 } 2732 }
2354} 2733}
2355 2734
2356sub _topleft { 2735sub _topleft {
2357 my ($self, $x, $y) = @_; 2736 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines