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.164 by root, Mon Apr 24 06:57:39 2006 UTC vs.
Revision 1.189 by root, Mon May 8 22:17:24 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
376} 403}
377 404
378sub check_size { 405sub check_size {
379 my ($self) = @_; 406 my ($self) = @_;
380 407
408 $self->_check_size;
409 return;
410 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
411
412 $check_size->{$self} = $self;
413
414 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
415 while (%$check_size) {
416 my @widgets = values %$check_size;
417 $_->_check_size
418 for @widgets;
419 }
420 });
421}
422
423sub _check_size {
424 my ($self) = @_;
425
426 delete $CFClient::UI::ROOT->{_check_size}{$self};
427
381 $self->{parent} 428 $self->{parent}
382 or return 1; 429 or return 1;
383 430
384 my ($w, $h) = $self->{user_w} && $self->{user_h} 431 my ($w, $h) = $self->{user_w} && $self->{user_h}
385 ? @$self{qw(user_w user_h)} 432 ? @$self{qw(user_w user_h)}
387 434
388 if ($w != $self->{req_w} || $h != $self->{req_h}) { 435 if ($w != $self->{req_w} || $h != $self->{req_h}) {
389 $self->{req_w} = $w; 436 $self->{req_w} = $w;
390 $self->{req_h} = $h; 437 $self->{req_h} = $h;
391 438
392 $self->{parent}->check_size 439 $self->{parent}->_check_size
393 or $self->size_allocate ( 440 or $self->size_allocate (
394 (List::Util::max $self->{w}, $w), 441 (List::Util::max $self->{w}, $w),
395 (List::Util::max $self->{h}, $h), 442 (List::Util::max $self->{h}, $h),
396 ); 443 );
397 444
402} 449}
403 450
404sub update { 451sub update {
405 my ($self) = @_; 452 my ($self) = @_;
406 453
407 $self->{parent}->update 454 my $update = $CFClient::UI::ROOT->{_update} ||= {};
408 if $self->{parent}; 455
456 $update->{$self} = $self;
457
458 $CFClient::UI::ROOT->on_refresh (_update => sub {
459 while (%$update) {
460 my @widgets = values %$update;
461 %$update = ();
462
463 $_->{parent} && $_->{parent}->update
464 for @widgets;
465 }
466 });
409} 467}
410 468
411sub connect { 469sub connect {
412 my ($self, $signal, $cb) = @_; 470 my ($self, $signal, $cb) = @_;
413 471
492sub new { 550sub new {
493 my ($class, %arg) = @_; 551 my ($class, %arg) = @_;
494 552
495 my $children = delete $arg{children} || []; 553 my $children = delete $arg{children} || [];
496 554
497 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 555 my $self = $class->SUPER::new (
556 children => [],
557 can_events => 0,
558 %arg,
559 );
498 $self->add ($_) for @$children; 560 $self->add ($_) for @$children;
499 561
500 $self 562 $self
501} 563}
502 564
503sub add { 565sub add {
504 my ($self, $child) = @_; 566 my ($self, @widgets) = @_;
505 567
506 $child->set_parent ($self); 568 $_->set_parent ($self)
569 for @widgets;
507 570
508 use sort 'stable'; 571 use sort 'stable';
509 572
510 $self->{children} = [ 573 $self->{children} = [
511 sort { $a->{z} <=> $b->{z} } 574 sort { $a->{z} <=> $b->{z} }
512 @{$self->{children}}, $child 575 @{$self->{children}}, @widgets
513 ]; 576 ];
514 577
515 $child->check_size; 578 $_->check_size
579 for @widgets;
580
581 $self->update;
582}
583
584sub children {
585 @{ $_[0]{children} }
516} 586}
517 587
518sub remove { 588sub remove {
519 my ($self, $child) = @_; 589 my ($self, $child) = @_;
520 590
535 605
536 for (@$children) { 606 for (@$children) {
537 delete $_->{parent}; 607 delete $_->{parent};
538 $_->hide; 608 $_->hide;
539 } 609 }
610
611 $self->check_size;
612 $self->update;
540} 613}
541 614
542sub find_widget { 615sub find_widget {
543 my ($self, $x, $y) = @_; 616 my ($self, $x, $y) = @_;
544 617
619} 692}
620 693
621sub update { 694sub update {
622 my ($self) = @_; 695 my ($self) = @_;
623 696
624 # we want to do this delayed... 697 $ROOT->on_refresh ($self => sub { $self->render_child });
625 $self->render_chld;
626 $self->SUPER::update; 698 $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} 699}
638 700
639sub size_allocate { 701sub size_allocate {
640 my ($self, $w, $h) = @_; 702 my ($self, $w, $h) = @_;
641 703
642 $self->child->configure (0, 0, $w, $h); 704 $self->SUPER::size_allocate ($w, $h);
705 $self->update;
706}
643 707
708sub _render {
709 $_[0]{children}[0]->draw;
710}
711
712sub render_child {
713 my ($self) = @_;
714
715 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
716 glClearColor 0, 0, 0, 0;
717 glClear GL_COLOR_BUFFER_BIT;
718
644 $self->render_chld; 719 $self->_render;
720# glColorMask 1, 1, 1, 0;
721# glEnable GL_BLEND;
722# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
723# glRasterPos 0, 0;
724# glCopyPixels 0, 0, $self->{w}, $self->{h};
725# glDisable GL_BLEND;
726# glColorMask 1, 1, 1, 1;
727 };
645} 728}
646 729
647sub _draw { 730sub _draw {
648 my ($self) = @_; 731 my ($self) = @_;
649 732
651 734
652 my $tex = $self->{texture} 735 my $tex = $self->{texture}
653 or return; 736 or return;
654 737
655 glEnable GL_BLEND; 738 glEnable GL_BLEND;
656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 739 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
657 glEnable GL_TEXTURE_2D; 740 glEnable GL_TEXTURE_2D;
658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 741 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
742 glColor 0, 0, 0, 1;
659 743
660 $tex->draw_quad (0, 0, $w, $h); 744 $tex->draw_quad (0, 0, $w, $h);
661 745
662 glDisable GL_BLEND; 746 glDisable GL_BLEND;
663 glDisable GL_TEXTURE_2D; 747 glDisable GL_TEXTURE_2D;
667 751
668package CFClient::UI::ViewPort; 752package CFClient::UI::ViewPort;
669 753
670our @ISA = CFClient::UI::Window::; 754our @ISA = CFClient::UI::Window::;
671 755
672sub new { die }
673
674sub size_request { 756sub size_request {
675 my ($self) = @_; 757 my ($self) = @_;
676 758
677 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 759 @$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)}); 760 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
679 761
680 @$self{qw(child_w child_h)} 762 @$self{qw(child_w child_h)}
681} 763}
682 764
683sub _draw { 765sub size_allocate {
684 my ($self) = @_; 766 my ($self, $w, $h) = @_;
685 767
686 $self->{children}[1]->draw; 768 $self->update;
687} 769}
688 770
771sub set_offset {
772 my ($self, $x, $y) = @_;
773
774 $self->{view_x} = int $x;
775 $self->{view_y} = int $y;
776
777 $self->update;
778}
779
780# hmm, this does not work for topleft of $self... but we should not aks for that
781sub _topleft {
782 my ($self, $x, $y) = @_;
783
784 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y})
785}
786
787sub find_widget {
788 my ($self, $x, $y) = @_;
789
790 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
791 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
792 ) {
793 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
794 } else {
795 $self->CFClient::UI::Base::find_widget ($x, $y)
796 }
797}
798
799sub _render {
800 my ($self) = @_;
801
802 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
803
804 $self->SUPER::_render;
805}
806
807#############################################################################
808
809package CFClient::UI::ScrolledWindow;
810
811our @ISA = CFClient::UI::HBox::;
812
813sub new {
814 my $class = shift;
815
816 my $self;
817
818 my $slider = new CFClient::UI::Slider
819 vertical => 1,
820 range => [0, 0, 1, 0.01], # HACK fix
821 connect_changed => sub {
822 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
823 },
824 ;
825
826 $self = $class->SUPER::new (
827 vp => (new CFClient::UI::ViewPort),
828 slider => $slider,
829 @_,
830 );
831
832 $self->{vp}->add ($self->{scrolled});
833 $self->add ($self->{vp});
834 $self->add ($self->{slider});
835
836 $self
837}
838
839#TODO# update range on size_allocate depeneing on child
840# update viewport offset on scroll
689 841
690############################################################################# 842#############################################################################
691 843
692package CFClient::UI::Frame; 844package CFClient::UI::Frame;
693 845
750 # TODO: user_x, user_y, overwrite moveto? 902 # TODO: user_x, user_y, overwrite moveto?
751 903
752 my $self = $class->SUPER::new ( 904 my $self = $class->SUPER::new (
753 bg => [1, 1, 1, 1], 905 bg => [1, 1, 1, 1],
754 border_bg => [1, 1, 1, 1], 906 border_bg => [1, 1, 1, 1],
755 border => 0.8, 907 border => 0.6,
756 can_events => 0, 908 can_events => 1,
757 @_ 909 @_
758 ); 910 );
759 911
760 $self->{title} &&= new CFClient::UI::Label 912 $self->{title} &&= new CFClient::UI::Label
761 align => 0, 913 align => 0,
762 valign => 1, 914 valign => 1,
763 text => $self->{title}, 915 text => $self->{title},
764 fontsize => 1; 916 fontsize => $self->{border};
765 917
766 $self 918 $self
767} 919}
768 920
769sub border { 921sub border {
794} 946}
795 947
796sub button_down { 948sub button_down {
797 my ($self, $ev, $x, $y) = @_; 949 my ($self, $ev, $x, $y) = @_;
798 950
951 my ($w, $h) = @$self{qw(w h)};
799 my $border = $self->border; 952 my $border = $self->border;
800 953
801 if ($x < $self->{w} && $x >= $self->{w} - $border 954 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
802 && $y < $self->{h} && $y >= $self->{h} - $border) { 955 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
803 956
957 if ($lr & $td) {
958 my ($wx, $wy) = ($self->{x}, $self->{y});
804 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 959 my ($ox, $oy) = ($ev->{x}, $ev->{y});
805 my ($bw, $bh) = ($self->{w}, $self->{h}); 960 my ($bw, $bh) = ($self->{w}, $self->{h});
806 961
962 my $mx = $x < $border;
963 my $my = $y < $border;
964
807 $self->{motion} = sub { 965 $self->{motion} = sub {
808 my ($ev, $x, $y) = @_; 966 my ($ev, $x, $y) = @_;
809 967
810 ($x, $y) = ($ev->{x}, $ev->{y}); 968 my $dx = $ev->{x} - $ox;
969 my $dy = $ev->{y} - $oy;
811 970
812 $self->{user_w} = $bw + $x - $ox; 971 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
813 $self->{user_h} = $bh + $y - $oy; 972 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
973 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
814 $self->check_size; 974 $self->check_size;
815 }; 975 };
816 976
817 } elsif ($x >= 0 && $x < $self->{w} 977 } elsif ($lr ^ $td) {
818 && $y >= 0 && $y < $border) {
819
820 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 978 my ($ox, $oy) = ($ev->{x}, $ev->{y});
821 my ($bx, $by) = ($self->{x}, $self->{y}); 979 my ($bx, $by) = ($self->{x}, $self->{y});
822 980
823 $self->{motion} = sub { 981 $self->{motion} = sub {
824 my ($ev, $x, $y) = @_; 982 my ($ev, $x, $y) = @_;
860 $tex[1]->draw_quad (0, 0, $w, $border); 1018 $tex[1]->draw_quad (0, 0, $w, $border);
861 $tex[3]->draw_quad (0, $border, $border, $ch); 1019 $tex[3]->draw_quad (0, $border, $border, $ch);
862 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 1020 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
863 $tex[4]->draw_quad (0, $h - $border, $w, $border); 1021 $tex[4]->draw_quad (0, $h - $border, $w, $border);
864 1022
1023 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
865 my $bg = $tex[0]; 1024 my $bg = $tex[0];
866 1025
867 # TODO: repeat texture not scale 1026 # TODO: repeat texture not scale
868 my $rep_x = $cw / $bg->{w}; 1027 my $rep_x = $cw / $bg->{w};
869 my $rep_y = $ch / $bg->{h}; 1028 my $rep_y = $ch / $bg->{h};
870 1029
871 glColor @{ $self->{bg} }; 1030 glColor @{ $self->{bg} };
872 1031
873 $bg->{s} = $rep_x; 1032 $bg->{s} = $rep_x;
874 $bg->{t} = $rep_y; 1033 $bg->{t} = $rep_y;
875 $bg->{wrap_mode} = 1; 1034 $bg->{wrap_mode} = 1;
876 $bg->draw_quad ($border, $border, $cw, $ch); 1035 $bg->draw_quad ($border, $border, $cw, $ch);
877 1036
878 glDisable GL_TEXTURE_2D; 1037 glDisable GL_TEXTURE_2D;
879 glDisable GL_BLEND; 1038 glDisable GL_BLEND;
1039 }
880 1040
881 $self->{title}->draw if $self->{title}; 1041 $self->{title}->draw if $self->{title};
1042
882 $self->child->draw; 1043 $self->child->draw;
883} 1044}
884 1045
885############################################################################# 1046#############################################################################
886 1047
908 $self->{children}[$y][$x] = $child; 1069 $self->{children}[$y][$x] = $child;
909 1070
910 $child->check_size; 1071 $child->check_size;
911} 1072}
912 1073
1074sub children {
1075 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1076}
1077
913# TODO: move to container class maybe? send childs a signal on removal? 1078# TODO: move to container class maybe? send childs a signal on removal?
914sub clear { 1079sub clear {
915 my ($self) = @_; 1080 my ($self) = @_;
916 1081
917 my $children = delete $self->{children}; 1082 my @children = $self->children;
1083 delete $self->{children};
918 1084
919 for (grep $_, map @$_, grep $_, @$children) { 1085 for (@children) {
920 delete $_->{parent}; 1086 delete $_->{parent};
921 $_->hide; 1087 $_->hide;
922 } 1088 }
923 1089
924 $self->update; 1090 $self->update;
1185 s/</&lt;/g; 1351 s/</&lt;/g;
1186 1352
1187 $_[1] 1353 $_[1]
1188} 1354}
1189 1355
1356sub update {
1357 my ($self) = @_;
1358
1359 delete $self->{texture};
1360 $self->SUPER::update;
1361}
1362
1190sub set_text { 1363sub set_text {
1191 my ($self, $text) = @_; 1364 my ($self, $text) = @_;
1192 1365
1366 return if $self->{text} eq "T$text";
1367 $self->{text} = "T$text";
1368
1193 $self->{layout}->set_text ($text); 1369 $self->{layout}->set_text ($text);
1194 1370
1195 delete $self->{texture}; 1371 delete $self->{texture};
1372 $self->update;
1196 $self->check_size; 1373 $self->check_size;
1197 $self->update;
1198} 1374}
1199 1375
1200sub set_markup { 1376sub set_markup {
1201 my ($self, $markup) = @_; 1377 my ($self, $markup) = @_;
1202 1378
1379 return if $self->{text} eq "M$markup";
1380 $self->{text} = "M$markup";
1381
1203 $self->{layout}->set_markup ($markup); 1382 $self->{layout}->set_markup ($markup);
1204 1383
1205 delete $self->{texture}; 1384 delete $self->{texture};
1385 $self->update;
1206 $self->check_size; 1386 $self->check_size;
1207 $self->update;
1208} 1387}
1209 1388
1210sub size_request { 1389sub size_request {
1211 my ($self) = @_; 1390 my ($self) = @_;
1212 1391
1241sub set_fontsize { 1420sub set_fontsize {
1242 my ($self, $fontsize) = @_; 1421 my ($self, $fontsize) = @_;
1243 1422
1244 $self->{fontsize} = $fontsize; 1423 $self->{fontsize} = $fontsize;
1245 delete $self->{texture}; 1424 delete $self->{texture};
1425
1426 $self->update;
1246 $self->check_size; 1427 $self->check_size;
1247 $self->update;
1248} 1428}
1249 1429
1250sub _draw { 1430sub _draw {
1251 my ($self) = @_; 1431 my ($self) = @_;
1252 1432
1462 my ($self, $ev) = @_; 1642 my ($self, $ev) = @_;
1463 1643
1464 my $sym = $ev->{sym}; 1644 my $sym = $ev->{sym};
1465 1645
1466 if ($sym == 13) { 1646 if ($sym == 13) {
1647 unshift @{$self->{history}},
1648 my $txt = $self->get_text;
1649 $self->{history_pointer} = -1;
1650 $self->{history_saveback} = '';
1467 $self->emit (activate => $self->get_text); 1651 $self->emit (activate => $txt);
1468 $self->update; 1652 $self->update;
1653
1654 } elsif ($sym == CFClient::SDLK_UP) {
1655 if ($self->{history_pointer} < 0) {
1656 $self->{history_saveback} = $self->get_text;
1657 }
1658 if (@{$self->{history} || []} > 0) {
1659 $self->{history_pointer}++;
1660 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1661 $self->{history_pointer} = @{$self->{history} || []} - 1;
1662 }
1663 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1664 }
1665
1666 } elsif ($sym == CFClient::SDLK_DOWN) {
1667 $self->{history_pointer}--;
1668 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1669
1670 if ($self->{history_pointer} >= 0) {
1671 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1672 } else {
1673 $self->set_text ($self->{history_saveback});
1674 }
1469 1675
1470 } else { 1676 } else {
1471 $self->SUPER::key_down ($ev); 1677 $self->SUPER::key_down ($ev);
1472 } 1678 }
1473 1679
1710} 1916}
1711 1917
1712sub set_max { 1918sub set_max {
1713 my ($self, $max) = @_; 1919 my ($self, $max) = @_;
1714 1920
1921 return if $self->{max_val} == $max;
1922
1715 $self->{max_val} = $max; 1923 $self->{max_val} = $max;
1924 $self->update;
1716} 1925}
1717 1926
1718sub set_value { 1927sub set_value {
1719 my ($self, $val, $max) = @_; 1928 my ($self, $val, $max) = @_;
1720 1929
1721 $self->set_max ($max) 1930 $self->set_max ($max)
1722 if defined $max; 1931 if defined $max;
1723 1932
1724 $max = $self->{max_val}; 1933 return if $self->{val} == $val;
1934
1725 $self->{val} = $val; 1935 $self->{val} = $val;
1726
1727 $self->update; 1936 $self->update;
1728} 1937}
1729 1938
1730sub _draw { 1939sub _draw {
1731 my ($self) = @_; 1940 my ($self) = @_;
1794 2003
1795sub new { 2004sub new {
1796 my ($class, %arg) = @_; 2005 my ($class, %arg) = @_;
1797 2006
1798 my $self = $class->SUPER::new ( 2007 my $self = $class->SUPER::new (
1799 tooltip => $arg{type}, 2008 tooltip => $arg{type},
1800 can_hover => 1, 2009 can_hover => 1,
2010 can_events => 1,
1801 %arg, 2011 %arg,
1802 ); 2012 );
1803 2013
1804 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 2014 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1805 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2015 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1813 2023
1814 $self->{value}->set_fontsize ($fsize); 2024 $self->{value}->set_fontsize ($fsize);
1815 $self->{max} ->set_fontsize ($fsize); 2025 $self->{max} ->set_fontsize ($fsize);
1816} 2026}
1817 2027
2028sub set_max {
2029 my ($self, $max) = @_;
2030
2031 $self->{gauge}->set_max ($max);
2032 $self->{max}->set_text ($max);
2033}
2034
1818sub set_value { 2035sub set_value {
1819 my ($self, $val, $max) = @_; 2036 my ($self, $val, $max) = @_;
1820 2037
1821 $self->set_max ($max) 2038 $self->set_max ($max)
1822 if defined $max; 2039 if defined $max;
1823 2040
1824 $self->{gauge}->set_value ($val, $max); 2041 $self->{gauge}->set_value ($val, $max);
1825 $self->{value}->set_text ($val); 2042 $self->{value}->set_text ($val);
1826}
1827
1828sub set_max {
1829 my ($self, $max) = @_;
1830
1831 $self->{gauge}->set_max ($max);
1832 $self->{max}->set_text ($max);
1833} 2043}
1834 2044
1835############################################################################# 2045#############################################################################
1836 2046
1837package CFClient::UI::Slider; 2047package CFClient::UI::Slider;
1856 # TODO: calculations are off 2066 # TODO: calculations are off
1857 my $self = $class->SUPER::new ( 2067 my $self = $class->SUPER::new (
1858 fg => [1, 1, 1], 2068 fg => [1, 1, 1],
1859 active_fg => [0, 0, 0], 2069 active_fg => [0, 0, 0],
1860 range => [0, 0, 100, 10], 2070 range => [0, 0, 100, 10],
1861 req_w => 20, 2071 req_w => $::WIDTH / 80,
1862 req_h => 20, 2072 req_h => $::WIDTH / 80,
1863 vertical => 0, 2073 vertical => 0,
1864 can_hover => 1, 2074 can_hover => 1,
1865 inner_pad => 5, 2075 inner_pad => 5,
1866 @_ 2076 @_
1867 ); 2077 );
1991 (new CFClient::UI::Empty expand => 1), 2201 (new CFClient::UI::Empty expand => 1),
1992 (new CFClient::UI::Slider vertical => 1), 2202 (new CFClient::UI::Slider vertical => 1),
1993 ], 2203 ],
1994 ); 2204 );
1995 2205
1996 $self->{children}[1]->connect (changed => sub { 2206 $self->{children}[1]->connect (changed => sub { $self->update });
1997 $self->update;
1998 });
1999 2207
2000 $self 2208 $self
2001} 2209}
2002 2210
2003sub set_fontsize { 2211sub set_fontsize {
2011 my ($self, $text) = @_; 2219 my ($self, $text) = @_;
2012 2220
2013 my $layout = $self->{layout}; 2221 my $layout = $self->{layout};
2014 2222
2015 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2223 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2016 $layout->set_width ($self->{w}); 2224 $layout->set_width ($self->{children}[0]{w});
2017 $layout->set_text ($text); 2225 $layout->set_text ($text);
2018 2226
2019 ($layout->size)[1] 2227 ($layout->size)[1]
2020} 2228}
2021 2229
2074 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2282 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2075 2283
2076 delete $self->{texture}; 2284 delete $self->{texture};
2077 } 2285 }
2078 2286
2079 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2287 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2080 glClearColor 0, 0, 0, 1; 2288 glClearColor 0, 0, 0, 0;
2081 glClear GL_COLOR_BUFFER_BIT; 2289 glClear GL_COLOR_BUFFER_BIT;
2082 2290
2083 glEnable GL_BLEND; 2291 glEnable GL_BLEND;
2084 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2085 glEnable GL_TEXTURE_2D; 2293 glEnable GL_TEXTURE_2D;
2117} 2325}
2118 2326
2119sub _draw { 2327sub _draw {
2120 my ($self) = @_; 2328 my ($self) = @_;
2121 2329
2122 if ($self->{texture}) { 2330 glEnable GL_BLEND;
2331 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2123 glEnable GL_TEXTURE_2D; 2332 glEnable GL_TEXTURE_2D;
2124 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2333 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2334 glColor 1, 1, 1, 1;
2125 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2335 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2126 glDisable GL_TEXTURE_2D; 2336 glDisable GL_TEXTURE_2D;
2127 } 2337 glDisable GL_BLEND;
2128 2338
2129 $self->{children}[1]->draw; 2339 $self->{children}[1]->draw;
2130 2340
2131} 2341}
2132 2342
2301 2511
2302sub size_request { 2512sub size_request {
2303 (32, 8) 2513 (32, 8)
2304} 2514}
2305 2515
2306sub draw { 2516sub _draw {
2307 my ($self) = @_; 2517 my ($self) = @_;
2308 2518
2519 return unless $::CONN;#d# manage and cache textures differently
2309 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2520 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2310 2521
2522 # TODO animation
2311 if ($tex) { 2523 if ($tex) {
2312 glEnable GL_BLEND; 2524 glEnable GL_BLEND;
2313 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2525 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2314 glEnable GL_TEXTURE_2D; 2526 glEnable GL_TEXTURE_2D;
2315 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2527 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2320 } 2532 }
2321} 2533}
2322 2534
2323############################################################################# 2535#############################################################################
2324 2536
2537package CFClient::UI::InventoryItem;
2538
2539our @ISA = CFClient::UI::HBox::;
2540
2541sub new {
2542 my $class = shift;
2543
2544 my %args = @_;
2545
2546 my $item = $args{item};
2547
2548 my $desc = $item->{nrof} < 2
2549 ? $item->{name}
2550 : "$item->{nrof} $item->{name_pl}";
2551
2552
2553 my $self = $class->SUPER::new (
2554 can_hover => 1,
2555 can_events => 1,
2556 tooltip => (CFClient::UI::Label->escape ($desc)
2557 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2558 connect_button_down => sub {
2559 my ($self, $ev, $x, $y) = @_;
2560
2561 # todo: maybe put examine on 1? but should just be a tooltip :(
2562 if ($ev->{button} == 1) {
2563 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2564 } elsif ($ev->{button} == 2) {
2565 $::CONN->send ("apply $item->{tag}");
2566 } elsif ($ev->{button} == 3) {
2567 CFClient::UI::Menu->new (
2568 items => [
2569 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2570 [
2571 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2572 sub { $::CONN->send ("lock $item->{tag}") },
2573 ],
2574 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2575 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2576 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2577 ],
2578 )->popup ($ev);
2579 }
2580
2581 1
2582 },
2583 %args
2584 );
2585 $self->add(new CFClient::UI::Face
2586 can_events => 0,
2587 face => $item->{face},
2588 anim => $item->{anim},
2589 animspeed => $item->{animspeed});
2590 $self->add(new CFClient::UI::Label
2591 can_events => 0,
2592 text => $desc);
2593
2594 $self
2595}
2596
2597#############################################################################
2598
2599package CFClient::UI::Inventory;
2600
2601our @ISA = CFClient::UI::ScrolledWindow::;
2602
2603sub new {
2604 my $class = shift;
2605
2606 my $self = $class->SUPER::new (
2607 scrolled => (new CFClient::UI::VBox),
2608 @_,
2609 );
2610
2611 $self
2612}
2613
2614sub set_items {
2615 my ($self, $items) = @_;
2616
2617 $self->{scrolled}->clear;
2618 return unless $items;
2619
2620 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2621
2622 $self->{real_items} = \@items;
2623
2624 for my $item (@items) {
2625 my $desc = $item->{nrof} < 2
2626 ? $item->{name}
2627 : "$item->{nrof} $item->{name_pl}";
2628
2629 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2630 }
2631
2632# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2633}
2634
2635sub size_request {
2636 my ($self) = @_;
2637 ($self->{req_w}, $self->{req_h});
2638}
2639
2640#############################################################################
2641
2642package CFClient::UI::Menu;
2643
2644our @ISA = CFClient::UI::FancyFrame::;
2645
2646use CFClient::OpenGL;
2647
2648sub new {
2649 my $class = shift;
2650
2651 my $self = $class->SUPER::new (
2652 items => [],
2653 z => 100,
2654 @_,
2655 );
2656
2657 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2658
2659 for my $item (@{ $self->{items} }) {
2660 my ($widget, $cb) = @$item;
2661
2662 # handle various types of items, only text for now
2663 if (!ref $widget) {
2664 $widget = new CFClient::UI::Label
2665 can_hover => 1,
2666 can_events => 1,
2667 text => $widget;
2668 }
2669
2670 $self->{item}{$widget} = $item;
2671
2672 $self->{vbox}->add ($widget);
2673 }
2674
2675 $self
2676}
2677
2678# popup given the event (must be a mouse button down event currently)
2679sub popup {
2680 my ($self, $ev) = @_;
2681
2682 $self->emit ("popdown");
2683
2684 # maybe save $GRAB? must be careful about events...
2685 $GRAB = $self;
2686 $self->{button} = $ev->{button};
2687
2688 $self->show;
2689 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2690}
2691
2692sub mouse_motion {
2693 my ($self, $ev, $x, $y) = @_;
2694
2695 # TODO: should use vbox->find_widget or so
2696 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2697 $self->{hover} = $self->{item}{$HOVER};
2698}
2699
2700sub button_up {
2701 my ($self, $ev, $x, $y) = @_;
2702
2703 if ($ev->{button} == $self->{button}) {
2704 undef $GRAB;
2705 $self->hide;
2706
2707 $self->emit ("popdown");
2708 $self->{hover}[1]->() if $self->{hover};
2709 }
2710}
2711
2712#############################################################################
2713
2325package CFClient::UI::Root; 2714package CFClient::UI::Root;
2326 2715
2327our @ISA = CFClient::UI::Container::; 2716our @ISA = CFClient::UI::Container::;
2328 2717
2329use CFClient::OpenGL; 2718use CFClient::OpenGL;
2330 2719
2331sub check_size { 2720sub check_size {
2332 my ($self) = @_; 2721 my ($self) = @_;
2333 2722
2334 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2723 $self->configure (0, 0, $self->{w}, $self->{h});
2335} 2724}
2336 2725
2337sub size_request { 2726sub size_request {
2338 ($::WIDTH, $::HEIGHT) 2727 my ($self) = @_;
2728
2729 ($self->{w}, $self->{h})
2730}
2731
2732sub size_allocate {
2733 my ($self, $w, $h) = @_;
2734
2735 my $old_w = $self->{old_w};
2736 my $old_h = $self->{old_h};
2737
2738 if ($old_w && $old_h) {
2739 for my $child ($self->children) {
2740 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2741 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2742 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2743 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2744 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2745 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2746 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2747 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2748 }
2749 }
2750
2751 $self->{old_w} = $w;
2752 $self->{old_h} = $h;
2339} 2753}
2340 2754
2341sub configure { 2755sub configure {
2342 my ($self, $x, $y, $w, $h) = @_; 2756 my ($self, $x, $y, $w, $h) = @_;
2343 2757
2344 $self->SUPER::configure ($x, $y, $w, $h); 2758 $self->SUPER::configure ($x, $y, $w, $h);
2345 2759
2346 for my $child (@{$self->{children}}) { 2760 for my $child ($self->children) {
2347 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2761 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2348 2762
2349 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2763 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2350 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2764 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2351 $child->configure ($X, $Y, $W,$H); 2765 $child->configure ($X, $Y, $W, $H);
2352 } 2766 }
2353} 2767}
2354 2768
2355sub _topleft { 2769sub _topleft {
2356 my ($self, $x, $y) = @_; 2770 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines