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.188 by root, Mon May 8 21:16:09 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
503sub add { 534sub add {
504 my ($self, $child) = @_; 535 my ($self, @widgets) = @_;
505 536
506 $child->set_parent ($self); 537 $_->set_parent ($self)
538 for @widgets;
507 539
508 use sort 'stable'; 540 use sort 'stable';
509 541
510 $self->{children} = [ 542 $self->{children} = [
511 sort { $a->{z} <=> $b->{z} } 543 sort { $a->{z} <=> $b->{z} }
512 @{$self->{children}}, $child 544 @{$self->{children}}, @widgets
513 ]; 545 ];
514 546
515 $child->check_size; 547 $_->check_size
548 for @widgets;
549
550 $self->update;
551}
552
553sub children {
554 @{ $_[0]{children} }
516} 555}
517 556
518sub remove { 557sub remove {
519 my ($self, $child) = @_; 558 my ($self, $child) = @_;
520 559
535 574
536 for (@$children) { 575 for (@$children) {
537 delete $_->{parent}; 576 delete $_->{parent};
538 $_->hide; 577 $_->hide;
539 } 578 }
579
580 $self->check_size;
581 $self->update;
540} 582}
541 583
542sub find_widget { 584sub find_widget {
543 my ($self, $x, $y) = @_; 585 my ($self, $x, $y) = @_;
544 586
619} 661}
620 662
621sub update { 663sub update {
622 my ($self) = @_; 664 my ($self) = @_;
623 665
624 # we want to do this delayed... 666 $ROOT->on_refresh ($self => sub { $self->render_child });
625 $self->render_chld;
626 $self->SUPER::update; 667 $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} 668}
638 669
639sub size_allocate { 670sub size_allocate {
640 my ($self, $w, $h) = @_; 671 my ($self, $w, $h) = @_;
641 672
642 $self->child->configure (0, 0, $w, $h); 673 $self->SUPER::size_allocate ($w, $h);
674 $self->update;
675}
643 676
677sub _render {
678 $_[0]{children}[0]->draw;
679}
680
681sub render_child {
682 my ($self) = @_;
683
684 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
685 glClearColor 0, 0, 0, 0;
686 glClear GL_COLOR_BUFFER_BIT;
687
644 $self->render_chld; 688 $self->_render;
689# glColorMask 1, 1, 1, 0;
690# glEnable GL_BLEND;
691# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
692# glRasterPos 0, 0;
693# glCopyPixels 0, 0, $self->{w}, $self->{h};
694# glDisable GL_BLEND;
695# glColorMask 1, 1, 1, 1;
696 };
645} 697}
646 698
647sub _draw { 699sub _draw {
648 my ($self) = @_; 700 my ($self) = @_;
649 701
651 703
652 my $tex = $self->{texture} 704 my $tex = $self->{texture}
653 or return; 705 or return;
654 706
655 glEnable GL_BLEND; 707 glEnable GL_BLEND;
656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 708 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
657 glEnable GL_TEXTURE_2D; 709 glEnable GL_TEXTURE_2D;
658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 710 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
711 glColor 0, 0, 0, 1;
659 712
660 $tex->draw_quad (0, 0, $w, $h); 713 $tex->draw_quad (0, 0, $w, $h);
661 714
662 glDisable GL_BLEND; 715 glDisable GL_BLEND;
663 glDisable GL_TEXTURE_2D; 716 glDisable GL_TEXTURE_2D;
667 720
668package CFClient::UI::ViewPort; 721package CFClient::UI::ViewPort;
669 722
670our @ISA = CFClient::UI::Window::; 723our @ISA = CFClient::UI::Window::;
671 724
672sub new { die }
673
674sub size_request { 725sub size_request {
675 my ($self) = @_; 726 my ($self) = @_;
676 727
677 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 728 @$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)}); 729 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
679 730
680 @$self{qw(child_w child_h)} 731 @$self{qw(child_w child_h)}
681} 732}
682 733
683sub _draw { 734sub size_allocate {
684 my ($self) = @_; 735 my ($self, $w, $h) = @_;
685 736
686 $self->{children}[1]->draw; 737 $self->update;
687} 738}
688 739
740sub set_offset {
741 my ($self, $x, $y) = @_;
742
743 $self->{view_x} = int $x;
744 $self->{view_y} = int $y;
745
746 $self->update;
747}
748
749# hmm, this does not work for topleft of $self... but we should not aks for that
750sub _topleft {
751 my ($self, $x, $y) = @_;
752
753 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y})
754}
755
756sub find_widget {
757 my ($self, $x, $y) = @_;
758
759 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
760 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
761 ) {
762 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
763 } else {
764 $self->CFClient::UI::Base::find_widget ($x, $y)
765 }
766}
767
768sub _render {
769 my ($self) = @_;
770
771 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
772
773 $self->SUPER::_render;
774}
775
776#############################################################################
777
778package CFClient::UI::ScrolledWindow;
779
780our @ISA = CFClient::UI::HBox::;
781
782sub new {
783 my $class = shift;
784
785 my $self;
786
787 my $slider = new CFClient::UI::Slider
788 vertical => 1,
789 range => [0, 0, 1, 0.01], # HACK fix
790 connect_changed => sub {
791 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
792 },
793 ;
794
795 $self = $class->SUPER::new (
796 vp => (new CFClient::UI::ViewPort),
797 slider => $slider,
798 @_,
799 );
800
801 $self->{vp}->add ($self->{scrolled});
802 $self->add ($self->{vp});
803 $self->add ($self->{slider});
804
805 $self
806}
807
808#TODO# update range on size_allocate depeneing on child
809# update viewport offset on scroll
689 810
690############################################################################# 811#############################################################################
691 812
692package CFClient::UI::Frame; 813package CFClient::UI::Frame;
693 814
748 my $class = shift; 869 my $class = shift;
749 870
750 # TODO: user_x, user_y, overwrite moveto? 871 # TODO: user_x, user_y, overwrite moveto?
751 872
752 my $self = $class->SUPER::new ( 873 my $self = $class->SUPER::new (
753 bg => [1, 1, 1, 1], 874 bg => [1, 1, 1, 1],
754 border_bg => [1, 1, 1, 1], 875 border_bg => [1, 1, 1, 1],
755 border => 0.8, 876 border => 0.6,
756 can_events => 1, 877 can_events => 1,
757 @_ 878 @_
758 ); 879 );
759 880
760 $self->{title} &&= new CFClient::UI::Label 881 $self->{title} &&= new CFClient::UI::Label
761 align => 0, 882 align => 0,
762 valign => 1, 883 valign => 1,
763 text => $self->{title}, 884 text => $self->{title},
764 fontsize => 1; 885 fontsize => $self->{border};
765 886
766 $self 887 $self
767} 888}
768 889
769sub border { 890sub border {
794} 915}
795 916
796sub button_down { 917sub button_down {
797 my ($self, $ev, $x, $y) = @_; 918 my ($self, $ev, $x, $y) = @_;
798 919
920 my ($w, $h) = @$self{qw(w h)};
799 my $border = $self->border; 921 my $border = $self->border;
800 922
801 if ($x < $self->{w} && $x >= $self->{w} - $border 923 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
802 && $y < $self->{h} && $y >= $self->{h} - $border) { 924 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
803 925
926 if ($lr & $td) {
927 my ($wx, $wy) = ($self->{x}, $self->{y});
804 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 928 my ($ox, $oy) = ($ev->{x}, $ev->{y});
805 my ($bw, $bh) = ($self->{w}, $self->{h}); 929 my ($bw, $bh) = ($self->{w}, $self->{h});
806 930
931 my $mx = $x < $border;
932 my $my = $y < $border;
933
807 $self->{motion} = sub { 934 $self->{motion} = sub {
808 my ($ev, $x, $y) = @_; 935 my ($ev, $x, $y) = @_;
809 936
810 ($x, $y) = ($ev->{x}, $ev->{y}); 937 my $dx = $ev->{x} - $ox;
938 my $dy = $ev->{y} - $oy;
811 939
812 $self->{user_w} = $bw + $x - $ox; 940 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
813 $self->{user_h} = $bh + $y - $oy; 941 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
942 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
814 $self->check_size; 943 $self->check_size;
815 }; 944 };
816 945
817 } elsif ($x >= 0 && $x < $self->{w} 946 } elsif ($lr ^ $td) {
818 && $y >= 0 && $y < $border) {
819
820 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 947 my ($ox, $oy) = ($ev->{x}, $ev->{y});
821 my ($bx, $by) = ($self->{x}, $self->{y}); 948 my ($bx, $by) = ($self->{x}, $self->{y});
822 949
823 $self->{motion} = sub { 950 $self->{motion} = sub {
824 my ($ev, $x, $y) = @_; 951 my ($ev, $x, $y) = @_;
860 $tex[1]->draw_quad (0, 0, $w, $border); 987 $tex[1]->draw_quad (0, 0, $w, $border);
861 $tex[3]->draw_quad (0, $border, $border, $ch); 988 $tex[3]->draw_quad (0, $border, $border, $ch);
862 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 989 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
863 $tex[4]->draw_quad (0, $h - $border, $w, $border); 990 $tex[4]->draw_quad (0, $h - $border, $w, $border);
864 991
992 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
865 my $bg = $tex[0]; 993 my $bg = $tex[0];
866 994
867 # TODO: repeat texture not scale 995 # TODO: repeat texture not scale
868 my $rep_x = $cw / $bg->{w}; 996 my $rep_x = $cw / $bg->{w};
869 my $rep_y = $ch / $bg->{h}; 997 my $rep_y = $ch / $bg->{h};
870 998
871 glColor @{ $self->{bg} }; 999 glColor @{ $self->{bg} };
872 1000
873 $bg->{s} = $rep_x; 1001 $bg->{s} = $rep_x;
874 $bg->{t} = $rep_y; 1002 $bg->{t} = $rep_y;
875 $bg->{wrap_mode} = 1; 1003 $bg->{wrap_mode} = 1;
876 $bg->draw_quad ($border, $border, $cw, $ch); 1004 $bg->draw_quad ($border, $border, $cw, $ch);
877 1005
878 glDisable GL_TEXTURE_2D; 1006 glDisable GL_TEXTURE_2D;
879 glDisable GL_BLEND; 1007 glDisable GL_BLEND;
1008 }
880 1009
881 $self->{title}->draw if $self->{title}; 1010 $self->{title}->draw if $self->{title};
1011
882 $self->child->draw; 1012 $self->child->draw;
883} 1013}
884 1014
885############################################################################# 1015#############################################################################
886 1016
908 $self->{children}[$y][$x] = $child; 1038 $self->{children}[$y][$x] = $child;
909 1039
910 $child->check_size; 1040 $child->check_size;
911} 1041}
912 1042
1043sub children {
1044 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1045}
1046
913# TODO: move to container class maybe? send childs a signal on removal? 1047# TODO: move to container class maybe? send childs a signal on removal?
914sub clear { 1048sub clear {
915 my ($self) = @_; 1049 my ($self) = @_;
916 1050
917 my $children = delete $self->{children}; 1051 my @children = $self->children;
1052 delete $self->{children};
918 1053
919 for (grep $_, map @$_, grep $_, @$children) { 1054 for (@children) {
920 delete $_->{parent}; 1055 delete $_->{parent};
921 $_->hide; 1056 $_->hide;
922 } 1057 }
923 1058
924 $self->update; 1059 $self->update;
1151 1286
1152sub new { 1287sub new {
1153 my ($class, %arg) = @_; 1288 my ($class, %arg) = @_;
1154 1289
1155 my $self = $class->SUPER::new ( 1290 my $self = $class->SUPER::new (
1156 fg => [1, 1, 1], 1291 fg => [1, 1, 1],
1157 #font => default_font 1292 #font => default_font
1158 fontsize => 1, 1293 fontsize => 1,
1159 text => "", 1294 text => "",
1160 align => -1, 1295 align => -1,
1161 valign => -1, 1296 valign => -1,
1162 padding => 2, 1297 padding => 2,
1163 layout => new CFClient::Layout, 1298 layout => new CFClient::Layout,
1164 can_events => 0, 1299 can_events => 0,
1165 %arg 1300 %arg
1166 ); 1301 );
1167 1302
1168 if (exists $self->{template}) { 1303 if (exists $self->{template}) {
1185 s/</&lt;/g; 1320 s/</&lt;/g;
1186 1321
1187 $_[1] 1322 $_[1]
1188} 1323}
1189 1324
1325sub update {
1326 my ($self) = @_;
1327
1328 delete $self->{texture};
1329 $self->SUPER::update;
1330}
1331
1190sub set_text { 1332sub set_text {
1191 my ($self, $text) = @_; 1333 my ($self, $text) = @_;
1192 1334
1335 return if $self->{text} eq "T$text";
1336 $self->{text} = "T$text";
1337
1193 $self->{layout}->set_text ($text); 1338 $self->{layout}->set_text ($text);
1194 1339
1195 delete $self->{texture}; 1340 delete $self->{texture};
1341 $self->update;
1196 $self->check_size; 1342 $self->check_size;
1197 $self->update;
1198} 1343}
1199 1344
1200sub set_markup { 1345sub set_markup {
1201 my ($self, $markup) = @_; 1346 my ($self, $markup) = @_;
1202 1347
1348 return if $self->{text} eq "M$markup";
1349 $self->{text} = "M$markup";
1350
1203 $self->{layout}->set_markup ($markup); 1351 $self->{layout}->set_markup ($markup);
1204 1352
1205 delete $self->{texture}; 1353 delete $self->{texture};
1354 $self->update;
1206 $self->check_size; 1355 $self->check_size;
1207 $self->update;
1208} 1356}
1209 1357
1210sub size_request { 1358sub size_request {
1211 my ($self) = @_; 1359 my ($self) = @_;
1212 1360
1241sub set_fontsize { 1389sub set_fontsize {
1242 my ($self, $fontsize) = @_; 1390 my ($self, $fontsize) = @_;
1243 1391
1244 $self->{fontsize} = $fontsize; 1392 $self->{fontsize} = $fontsize;
1245 delete $self->{texture}; 1393 delete $self->{texture};
1394
1395 $self->update;
1246 $self->check_size; 1396 $self->check_size;
1247 $self->update;
1248} 1397}
1249 1398
1250sub _draw { 1399sub _draw {
1251 my ($self) = @_; 1400 my ($self) = @_;
1252 1401
1292 1441
1293sub new { 1442sub new {
1294 my $class = shift; 1443 my $class = shift;
1295 1444
1296 $class->SUPER::new ( 1445 $class->SUPER::new (
1297 fg => [1, 1, 1], 1446 fg => [1, 1, 1],
1298 bg => [0, 0, 0, 0.2], 1447 bg => [0, 0, 0, 0.2],
1299 active_bg => [1, 1, 1, 0.5], 1448 active_bg => [1, 1, 1, 0.5],
1300 active_fg => [0, 0, 0], 1449 active_fg => [0, 0, 0],
1301 can_hover => 1, 1450 can_hover => 1,
1302 can_focus => 1, 1451 can_focus => 1,
1303 valign => 0, 1452 valign => 0,
1304 can_events => 1, 1453 can_events => 1,
1305 @_ 1454 @_
1306 ) 1455 )
1307} 1456}
1308 1457
1462 my ($self, $ev) = @_; 1611 my ($self, $ev) = @_;
1463 1612
1464 my $sym = $ev->{sym}; 1613 my $sym = $ev->{sym};
1465 1614
1466 if ($sym == 13) { 1615 if ($sym == 13) {
1616 unshift @{$self->{history}},
1617 my $txt = $self->get_text;
1618 $self->{history_pointer} = -1;
1619 $self->{history_saveback} = '';
1467 $self->emit (activate => $self->get_text); 1620 $self->emit (activate => $txt);
1468 $self->update; 1621 $self->update;
1622
1623 } elsif ($sym == CFClient::SDLK_UP) {
1624 if ($self->{history_pointer} < 0) {
1625 $self->{history_saveback} = $self->get_text;
1626 }
1627 if (@{$self->{history} || []} > 0) {
1628 $self->{history_pointer}++;
1629 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1630 $self->{history_pointer} = @{$self->{history} || []} - 1;
1631 }
1632 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1633 }
1634
1635 } elsif ($sym == CFClient::SDLK_DOWN) {
1636 $self->{history_pointer}--;
1637 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1638
1639 if ($self->{history_pointer} >= 0) {
1640 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1641 } else {
1642 $self->set_text ($self->{history_saveback});
1643 }
1469 1644
1470 } else { 1645 } else {
1471 $self->SUPER::key_down ($ev); 1646 $self->SUPER::key_down ($ev);
1472 } 1647 }
1473 1648
1487 1662
1488sub new { 1663sub new {
1489 my $class = shift; 1664 my $class = shift;
1490 1665
1491 $class->SUPER::new ( 1666 $class->SUPER::new (
1492 padding => 4, 1667 padding => 4,
1493 fg => [1, 1, 1], 1668 fg => [1, 1, 1],
1494 bg => [1, 1, 1, 0.2], 1669 bg => [1, 1, 1, 0.2],
1495 active_fg => [0, 0, 1], 1670 active_fg => [0, 0, 1],
1496 can_hover => 1, 1671 can_hover => 1,
1497 align => 0, 1672 align => 0,
1498 valign => 0, 1673 valign => 0,
1499 can_events => 1, 1674 can_events => 1,
1500 @_ 1675 @_
1501 ) 1676 )
1502} 1677}
1503 1678
1710} 1885}
1711 1886
1712sub set_max { 1887sub set_max {
1713 my ($self, $max) = @_; 1888 my ($self, $max) = @_;
1714 1889
1890 return if $self->{max_val} == $max;
1891
1715 $self->{max_val} = $max; 1892 $self->{max_val} = $max;
1893 $self->update;
1716} 1894}
1717 1895
1718sub set_value { 1896sub set_value {
1719 my ($self, $val, $max) = @_; 1897 my ($self, $val, $max) = @_;
1720 1898
1721 $self->set_max ($max) 1899 $self->set_max ($max)
1722 if defined $max; 1900 if defined $max;
1723 1901
1724 $max = $self->{max_val}; 1902 return if $self->{val} == $val;
1903
1725 $self->{val} = $val; 1904 $self->{val} = $val;
1726
1727 $self->update; 1905 $self->update;
1728} 1906}
1729 1907
1730sub _draw { 1908sub _draw {
1731 my ($self) = @_; 1909 my ($self) = @_;
1794 1972
1795sub new { 1973sub new {
1796 my ($class, %arg) = @_; 1974 my ($class, %arg) = @_;
1797 1975
1798 my $self = $class->SUPER::new ( 1976 my $self = $class->SUPER::new (
1799 tooltip => $arg{type}, 1977 tooltip => $arg{type},
1978 can_hover => 1,
1800 can_events => 1, 1979 can_events => 1,
1801 can_hover => 1,
1802 %arg, 1980 %arg,
1803 ); 1981 );
1804 1982
1805 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1983 $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); 1984 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1814 1992
1815 $self->{value}->set_fontsize ($fsize); 1993 $self->{value}->set_fontsize ($fsize);
1816 $self->{max} ->set_fontsize ($fsize); 1994 $self->{max} ->set_fontsize ($fsize);
1817} 1995}
1818 1996
1997sub set_max {
1998 my ($self, $max) = @_;
1999
2000 $self->{gauge}->set_max ($max);
2001 $self->{max}->set_text ($max);
2002}
2003
1819sub set_value { 2004sub set_value {
1820 my ($self, $val, $max) = @_; 2005 my ($self, $val, $max) = @_;
1821 2006
1822 $self->set_max ($max) 2007 $self->set_max ($max)
1823 if defined $max; 2008 if defined $max;
1824 2009
1825 $self->{gauge}->set_value ($val, $max); 2010 $self->{gauge}->set_value ($val, $max);
1826 $self->{value}->set_text ($val); 2011 $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} 2012}
1835 2013
1836############################################################################# 2014#############################################################################
1837 2015
1838package CFClient::UI::Slider; 2016package CFClient::UI::Slider;
1857 # TODO: calculations are off 2035 # TODO: calculations are off
1858 my $self = $class->SUPER::new ( 2036 my $self = $class->SUPER::new (
1859 fg => [1, 1, 1], 2037 fg => [1, 1, 1],
1860 active_fg => [0, 0, 0], 2038 active_fg => [0, 0, 0],
1861 range => [0, 0, 100, 10], 2039 range => [0, 0, 100, 10],
1862 req_w => 20, 2040 req_w => $::WIDTH / 80,
1863 req_h => 20, 2041 req_h => $::WIDTH / 80,
1864 vertical => 0, 2042 vertical => 0,
1865 can_hover => 1, 2043 can_hover => 1,
1866 inner_pad => 5, 2044 inner_pad => 5,
1867 @_ 2045 @_
1868 ); 2046 );
1978 2156
1979sub new { 2157sub new {
1980 my $class = shift; 2158 my $class = shift;
1981 2159
1982 my $self = $class->SUPER::new ( 2160 my $self = $class->SUPER::new (
1983 fontsize => 1, 2161 fontsize => 1,
2162 can_events => 0,
1984 #font => default_font 2163 #font => default_font
1985 @_, 2164 @_,
1986 2165
1987 layout => (new CFClient::Layout), 2166 layout => (new CFClient::Layout),
1988 par => [], 2167 par => [],
1989 height => 0, 2168 height => 0,
1990 children => [ 2169 children => [
1991 (new CFClient::UI::Empty expand => 1), 2170 (new CFClient::UI::Empty expand => 1),
1992 (new CFClient::UI::Slider vertical => 1), 2171 (new CFClient::UI::Slider vertical => 1),
1993 ], 2172 ],
1994 ); 2173 );
1995 2174
1996 $self->{children}[1]->connect (changed => sub { 2175 $self->{children}[1]->connect (changed => sub { $self->update });
1997 $self->update;
1998 });
1999 2176
2000 $self 2177 $self
2001} 2178}
2002 2179
2003sub set_fontsize { 2180sub set_fontsize {
2011 my ($self, $text) = @_; 2188 my ($self, $text) = @_;
2012 2189
2013 my $layout = $self->{layout}; 2190 my $layout = $self->{layout};
2014 2191
2015 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2192 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2016 $layout->set_width ($self->{w}); 2193 $layout->set_width ($self->{children}[0]{w});
2017 $layout->set_text ($text); 2194 $layout->set_text ($text);
2018 2195
2019 ($layout->size)[1] 2196 ($layout->size)[1]
2020} 2197}
2021 2198
2074 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2251 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2075 2252
2076 delete $self->{texture}; 2253 delete $self->{texture};
2077 } 2254 }
2078 2255
2079 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2256 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2080 glClearColor 0, 0, 0, 1; 2257 glClearColor 0, 0, 0, 0;
2081 glClear GL_COLOR_BUFFER_BIT; 2258 glClear GL_COLOR_BUFFER_BIT;
2082 2259
2083 glEnable GL_BLEND; 2260 glEnable GL_BLEND;
2084 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2261 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2085 glEnable GL_TEXTURE_2D; 2262 glEnable GL_TEXTURE_2D;
2117} 2294}
2118 2295
2119sub _draw { 2296sub _draw {
2120 my ($self) = @_; 2297 my ($self) = @_;
2121 2298
2122 if ($self->{texture}) { 2299 glEnable GL_BLEND;
2300 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2123 glEnable GL_TEXTURE_2D; 2301 glEnable GL_TEXTURE_2D;
2124 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2302 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2303 glColor 1, 1, 1, 1;
2125 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2304 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2126 glDisable GL_TEXTURE_2D; 2305 glDisable GL_TEXTURE_2D;
2127 } 2306 glDisable GL_BLEND;
2128 2307
2129 $self->{children}[1]->draw; 2308 $self->{children}[1]->draw;
2130 2309
2131} 2310}
2132 2311
2180 2359
2181sub new { 2360sub new {
2182 my $class = shift; 2361 my $class = shift;
2183 2362
2184 my $self = $class->SUPER::new ( 2363 my $self = $class->SUPER::new (
2185 state => 0, 2364 state => 0,
2186 connect_activate => \&toggle_flopper, 2365 connect_activate => \&toggle_flopper,
2187 can_events => 1,
2188 @_ 2366 @_
2189 ); 2367 );
2190 2368
2191 if ($self->{state}) { 2369 if ($self->{state}) {
2192 $self->{state} = 0; 2370 $self->{state} = 0;
2302 2480
2303sub size_request { 2481sub size_request {
2304 (32, 8) 2482 (32, 8)
2305} 2483}
2306 2484
2307sub draw { 2485sub _draw {
2308 my ($self) = @_; 2486 my ($self) = @_;
2309 2487
2488 return unless $::CONN;#d# manage and cache textures differently
2310 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2489 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2311 2490
2491 # TODO animation
2312 if ($tex) { 2492 if ($tex) {
2313 glEnable GL_BLEND; 2493 glEnable GL_BLEND;
2314 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2494 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2315 glEnable GL_TEXTURE_2D; 2495 glEnable GL_TEXTURE_2D;
2316 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2321 } 2501 }
2322} 2502}
2323 2503
2324############################################################################# 2504#############################################################################
2325 2505
2506package CFClient::UI::InventoryItem;
2507
2508our @ISA = CFClient::UI::HBox::;
2509
2510sub new {
2511 my $class = shift;
2512
2513 my %args = @_;
2514
2515 my $item = $args{item};
2516
2517 my $desc = $item->{nrof} < 2
2518 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}";
2520
2521
2522 my $self = $class->SUPER::new (
2523 can_hover => 1,
2524 can_events => 1,
2525 tooltip => (CFClient::UI::Label->escape ($desc)
2526 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2527 connect_button_down => sub {
2528 my ($self, $ev, $x, $y) = @_;
2529
2530 # todo: maybe put examine on 1? but should just be a tooltip :(
2531 if ($ev->{button} == 1) {
2532 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2533 } elsif ($ev->{button} == 2) {
2534 $::CONN->send ("apply $item->{tag}");
2535 } elsif ($ev->{button} == 3) {
2536 CFClient::UI::Menu->new (
2537 items => [
2538 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2539 [
2540 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2541 sub { $::CONN->send ("lock $item->{tag}") },
2542 ],
2543 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2544 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2545 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2546 ],
2547 )->popup ($ev);
2548 }
2549
2550 1
2551 },
2552 %args
2553 );
2554 $self->add(new CFClient::UI::Face
2555 can_events => 0,
2556 face => $item->{face},
2557 anim => $item->{anim},
2558 animspeed => $item->{animspeed});
2559 $self->add(new CFClient::UI::Label
2560 can_events => 0,
2561 text => $desc);
2562
2563 $self
2564}
2565
2566#############################################################################
2567
2568package CFClient::UI::Inventory;
2569
2570our @ISA = CFClient::UI::ScrolledWindow::;
2571
2572sub new {
2573 my $class = shift;
2574
2575 my $self = $class->SUPER::new (
2576 scrolled => (new CFClient::UI::VBox),
2577 @_,
2578 );
2579
2580 $self
2581}
2582
2583sub set_items {
2584 my ($self, $items) = @_;
2585
2586 $self->{scrolled}->clear;
2587 return unless $items;
2588
2589 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2590
2591 $self->{real_items} = \@items;
2592
2593 for my $item (@items) {
2594 my $desc = $item->{nrof} < 2
2595 ? $item->{name}
2596 : "$item->{nrof} $item->{name_pl}";
2597
2598 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2599 }
2600
2601# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2602}
2603
2604sub size_request {
2605 my ($self) = @_;
2606 ($self->{req_w}, $self->{req_h});
2607}
2608
2609#############################################################################
2610
2611package CFClient::UI::Menu;
2612
2613our @ISA = CFClient::UI::FancyFrame::;
2614
2615use CFClient::OpenGL;
2616
2617sub new {
2618 my $class = shift;
2619
2620 my $self = $class->SUPER::new (
2621 items => [],
2622 z => 100,
2623 @_,
2624 );
2625
2626 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2627
2628 for my $item (@{ $self->{items} }) {
2629 my ($widget, $cb) = @$item;
2630
2631 # handle various types of items, only text for now
2632 if (!ref $widget) {
2633 $widget = new CFClient::UI::Label
2634 can_hover => 1,
2635 can_events => 1,
2636 text => $widget;
2637 }
2638
2639 $self->{item}{$widget} = $item;
2640
2641 $self->{vbox}->add ($widget);
2642 }
2643
2644 $self
2645}
2646
2647# popup given the event (must be a mouse button down event currently)
2648sub popup {
2649 my ($self, $ev) = @_;
2650
2651 $self->emit ("popdown");
2652
2653 # maybe save $GRAB? must be careful about events...
2654 $GRAB = $self;
2655 $self->{button} = $ev->{button};
2656
2657 $self->show;
2658 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2659}
2660
2661sub mouse_motion {
2662 my ($self, $ev, $x, $y) = @_;
2663
2664 # TODO: should use vbox->find_widget or so
2665 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2666 $self->{hover} = $self->{item}{$HOVER};
2667}
2668
2669sub button_up {
2670 my ($self, $ev, $x, $y) = @_;
2671
2672 if ($ev->{button} == $self->{button}) {
2673 undef $GRAB;
2674 $self->hide;
2675
2676 $self->emit ("popdown");
2677 $self->{hover}[1]->() if $self->{hover};
2678 }
2679}
2680
2681#############################################################################
2682
2326package CFClient::UI::Root; 2683package CFClient::UI::Root;
2327 2684
2328our @ISA = CFClient::UI::Container::; 2685our @ISA = CFClient::UI::Container::;
2329 2686
2330use CFClient::OpenGL; 2687use CFClient::OpenGL;
2331 2688
2332sub check_size { 2689sub check_size {
2333 my ($self) = @_; 2690 my ($self) = @_;
2334 2691
2335 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2692 $self->configure (0, 0, $self->{w}, $self->{h});
2336} 2693}
2337 2694
2338sub size_request { 2695sub size_request {
2339 ($::WIDTH, $::HEIGHT) 2696 my ($self) = @_;
2697
2698 ($self->{w}, $self->{h})
2699}
2700
2701sub size_allocate {
2702 my ($self, $w, $h) = @_;
2703
2704 my $old_w = $self->{old_w};
2705 my $old_h = $self->{old_h};
2706
2707 if ($old_w && $old_h) {
2708 for my $child ($self->children) {
2709 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2710 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2711 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2712 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2713 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2714 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2715 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2716 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2717 }
2718 }
2719
2720 $self->{old_w} = $w;
2721 $self->{old_h} = $h;
2340} 2722}
2341 2723
2342sub configure { 2724sub configure {
2343 my ($self, $x, $y, $w, $h) = @_; 2725 my ($self, $x, $y, $w, $h) = @_;
2344 2726
2345 $self->SUPER::configure ($x, $y, $w, $h); 2727 $self->SUPER::configure ($x, $y, $w, $h);
2346 2728
2347 for my $child (@{$self->{children}}) { 2729 for my $child ($self->children) {
2348 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2730 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2349 2731
2350 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2732 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2351 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2733 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2352 $child->configure ($X, $Y, $W,$H); 2734 $child->configure ($X, $Y, $W, $H);
2353 } 2735 }
2354} 2736}
2355 2737
2356sub _topleft { 2738sub _topleft {
2357 my ($self, $x, $y) = @_; 2739 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines