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.162 by root, Mon Apr 24 06:05:35 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 }
39 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
40} 48}
41 49
42# class methods for events 50# class methods for events
43sub feed_sdl_key_down_event { 51sub feed_sdl_key_down_event {
44 $FOCUS->key_down ($_[0]) if $FOCUS; 52 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
53 if $FOCUS;
45} 54}
46 55
47sub feed_sdl_key_up_event { 56sub feed_sdl_key_up_event {
48 $FOCUS->key_up ($_[0]) if $FOCUS; 57 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
58 if $FOCUS;
49} 59}
50 60
51sub feed_sdl_button_down_event { 61sub feed_sdl_button_down_event {
52 my ($ev) = @_; 62 my ($ev) = @_;
53 my ($x, $y) = ($ev->{x}, $ev->{y}); 63 my ($x, $y) = ($ev->{x}, $ev->{y});
61 check_tooltip; 71 check_tooltip;
62 } 72 }
63 73
64 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 74 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
65 75
66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 76 if ($GRAB) {
77 ($x, $y) = $GRAB->coord2local ($x, $y);
78 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
79 }
67} 80}
68 81
69sub feed_sdl_button_up_event { 82sub feed_sdl_button_up_event {
70 my ($ev) = @_; 83 my ($ev) = @_;
71 my ($x, $y) = ($ev->{x}, $ev->{y}); 84 my ($x, $y) = ($ev->{x}, $ev->{y});
72 85
73 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 86 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
74 87
75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 88 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
76 89
77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 90 if ($GRAB) {
91 ($x, $y) = $GRAB->coord2local ($x, $y);
92 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
93 }
78 94
79 if (!$BUTTON_STATE) { 95 if (!$BUTTON_STATE) {
80 my $grab = $GRAB; undef $GRAB; 96 my $grab = $GRAB; undef $GRAB;
81 $grab->update if $grab; 97 $grab->update if $grab;
82 $GRAB->update if $GRAB; 98 $GRAB->update if $GRAB;
98 $HOVER->update if $HOVER && $HOVER->{can_hover}; 114 $HOVER->update if $HOVER && $HOVER->{can_hover};
99 115
100 check_tooltip; 116 check_tooltip;
101 } 117 }
102 118
103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 119 if ($HOVER) {
120 ($x, $y) = $HOVER->coord2local ($x, $y);
121 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
122 }
104} 123}
105 124
106# convert position array to integers 125# convert position array to integers
107sub harmonize { 126sub harmonize {
108 my ($vals) = @_; 127 my ($vals) = @_;
126 145
127sub new { 146sub new {
128 my $class = shift; 147 my $class = shift;
129 148
130 my $self = bless { 149 my $self = bless {
131 x => 0, 150 x => 0,
132 y => 0, 151 y => 0,
133 z => 0, 152 z => 0,
134 can_events => 1, 153 can_events => 1,
135 @_ 154 @_
136 }, $class; 155 }, $class;
137 156
138 for (keys %$self) { 157 for (keys %$self) {
160} 179}
161 180
162sub hide { 181sub hide {
163 my ($self) = @_; 182 my ($self) = @_;
164 183
165 return unless $self->{parent}; 184 undef $GRAB if $GRAB == $self;
185 undef $HOVER if $HOVER == $self;
166 186
167 $self->{parent}->remove ($self); 187 $self->{parent}->remove ($self)
188 if $self->{parent};
168} 189}
169 190
170sub move { 191sub move {
171 my ($self, $x, $y, $z) = @_; 192 my ($self, $x, $y, $z) = @_;
172 193
216 $self->{w} = $w; 237 $self->{w} = $w;
217 $self->{h} = $h; 238 $self->{h} = $h;
218 239
219 $self->size_allocate ($w, $h); 240 $self->size_allocate ($w, $h);
220 $self->update; 241 $self->update;
242 $self->emit (size_allocate => $w, $h);
221 } 243 }
222} 244}
223 245
224sub size_allocate { 246sub size_allocate {
225 # 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;
226} 263}
227 264
228sub set_max_size { 265sub set_max_size {
229 my ($self, $w, $h) = @_; 266 my ($self, $w, $h) = @_;
230 267
233} 270}
234 271
235# return top left coordinates 272# return top left coordinates
236sub _topleft { 273sub _topleft {
237 my ($self, $x, $y) = @_; 274 my ($self, $x, $y) = @_;
275
276 $self->{parent}
277 or Carp::confess "no parent widget in _topleft\n";#d#
238 278
239 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 279 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
240} 280}
241 281
242# translate global coordinates to local coordinate system 282# translate global coordinates to local coordinate system
402} 442}
403 443
404sub emit { 444sub emit {
405 my ($self, $signal, @args) = @_; 445 my ($self, $signal, @args) = @_;
406 446
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 447 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
408 $cb->($self, @args);
409 }
410} 448}
411 449
412sub DESTROY { 450sub DESTROY {
413 my ($self) = @_; 451 my ($self) = @_;
414 452
481sub new { 519sub new {
482 my ($class, %arg) = @_; 520 my ($class, %arg) = @_;
483 521
484 my $children = delete $arg{children} || []; 522 my $children = delete $arg{children} || [];
485 523
486 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 );
487 $self->add ($_) for @$children; 529 $self->add ($_) for @$children;
488 530
489 $self 531 $self
490} 532}
491 533
500 sort { $a->{z} <=> $b->{z} } 542 sort { $a->{z} <=> $b->{z} }
501 @{$self->{children}}, $child 543 @{$self->{children}}, $child
502 ]; 544 ];
503 545
504 $child->check_size; 546 $child->check_size;
547 $self->update;
548}
549
550sub children {
551 @{ $_[0]{children} }
505} 552}
506 553
507sub remove { 554sub remove {
508 my ($self, $child) = @_; 555 my ($self, $child) = @_;
509 556
510 delete $child->{parent}; 557 delete $child->{parent};
558 $child->hide;
511 559
512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 560 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
513 561
514 $self->check_size; 562 $self->check_size;
515 $self->update; 563 $self->update;
516} 564}
517 565
518sub clear { 566sub clear {
519 my ($self) = @_; 567 my ($self) = @_;
520 568
521 delete $_->{parent} 569 my $children = delete $self->{children};
522 for @{ delete $self->{children} };
523
524 $self->{children} = []; 570 $self->{children} = [];
571
572 for (@$children) {
573 delete $_->{parent};
574 $_->hide;
575 }
576
577 $self->check_size;
578 $self->update;
525} 579}
526 580
527sub find_widget { 581sub find_widget {
528 my ($self, $x, $y) = @_; 582 my ($self, $x, $y) = @_;
529 583
604} 658}
605 659
606sub update { 660sub update {
607 my ($self) = @_; 661 my ($self) = @_;
608 662
609 # we want to do this delayed... 663 $ROOT->on_refresh ($self => sub { $self->render_child });
610 $self->render_chld;
611 $self->SUPER::update; 664 $self->SUPER::update;
612}
613
614sub render_chld {
615 my ($self) = @_;
616
617 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
618 glClearColor 0, 0, 0, 1;
619 glClear GL_COLOR_BUFFER_BIT;
620 $self->child->draw;
621 };
622} 665}
623 666
624sub size_allocate { 667sub size_allocate {
625 my ($self, $w, $h) = @_; 668 my ($self, $w, $h) = @_;
626 669
627 $self->child->configure (0, 0, $w, $h); 670 $self->SUPER::size_allocate ($w, $h);
671 $self->update;
672}
628 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
629 $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 };
630} 694}
631 695
632sub _draw { 696sub _draw {
633 my ($self) = @_; 697 my ($self) = @_;
634 698
636 700
637 my $tex = $self->{texture} 701 my $tex = $self->{texture}
638 or return; 702 or return;
639 703
640 glEnable GL_BLEND; 704 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
643 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;
644 709
645 $tex->draw_quad (0, 0, $w, $h); 710 $tex->draw_quad (0, 0, $w, $h);
646 711
647 glDisable GL_BLEND; 712 glDisable GL_BLEND;
648 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
652 717
653package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
654 719
655our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
656 721
657sub new { die }
658
659sub size_request { 722sub size_request {
660 my ($self) = @_; 723 my ($self) = @_;
661 724
662 @$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)};
663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
664 727
665 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
666} 729}
667 730
668sub _draw { 731sub size_allocate {
669 my ($self) = @_; 732 my ($self, $w, $h) = @_;
670 733
671 $self->{children}[1]->draw; 734 $self->update;
672} 735}
673 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
674 807
675############################################################################# 808#############################################################################
676 809
677package CFClient::UI::Frame; 810package CFClient::UI::Frame;
678 811
733 my $class = shift; 866 my $class = shift;
734 867
735 # TODO: user_x, user_y, overwrite moveto? 868 # TODO: user_x, user_y, overwrite moveto?
736 869
737 my $self = $class->SUPER::new ( 870 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1], 871 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1], 872 border_bg => [1, 1, 1, 1],
740 border => 0.8, 873 border => 0.6,
741 can_events => 1, 874 can_events => 1,
742 @_ 875 @_
743 ); 876 );
744 877
745 $self->{title} &&= new CFClient::UI::Label 878 $self->{title} &&= new CFClient::UI::Label
746 align => 0, 879 align => 0,
747 valign => 1, 880 valign => 1,
748 text => $self->{title}, 881 text => $self->{title},
749 fontsize => 1; 882 fontsize => $self->{border};
750 883
751 $self 884 $self
752} 885}
753 886
754sub border { 887sub border {
779} 912}
780 913
781sub button_down { 914sub button_down {
782 my ($self, $ev, $x, $y) = @_; 915 my ($self, $ev, $x, $y) = @_;
783 916
917 my ($w, $h) = @$self{qw(w h)};
784 my $border = $self->border; 918 my $border = $self->border;
785 919
786 if ($x < $self->{w} && $x >= $self->{w} - $border 920 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
787 && $y < $self->{h} && $y >= $self->{h} - $border) { 921 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
788 922
923 if ($lr & $td) {
924 my ($wx, $wy) = ($self->{x}, $self->{y});
789 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 925 my ($ox, $oy) = ($ev->{x}, $ev->{y});
790 my ($bw, $bh) = ($self->{w}, $self->{h}); 926 my ($bw, $bh) = ($self->{w}, $self->{h});
791 927
928 my $mx = $x < $border;
929 my $my = $y < $border;
930
792 $self->{motion} = sub { 931 $self->{motion} = sub {
793 my ($ev, $x, $y) = @_; 932 my ($ev, $x, $y) = @_;
794 933
795 ($x, $y) = ($ev->{x}, $ev->{y}); 934 my $dx = $ev->{x} - $ox;
935 my $dy = $ev->{y} - $oy;
796 936
797 $self->{user_w} = $bw + $x - $ox; 937 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
798 $self->{user_h} = $bh + $y - $oy; 938 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
939 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
799 $self->check_size; 940 $self->check_size;
800 }; 941 };
801 942
802 } elsif ($x >= 0 && $x < $self->{w} 943 } elsif ($lr ^ $td) {
803 && $y >= 0 && $y < $border) {
804
805 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 944 my ($ox, $oy) = ($ev->{x}, $ev->{y});
806 my ($bx, $by) = ($self->{x}, $self->{y}); 945 my ($bx, $by) = ($self->{x}, $self->{y});
807 946
808 $self->{motion} = sub { 947 $self->{motion} = sub {
809 my ($ev, $x, $y) = @_; 948 my ($ev, $x, $y) = @_;
845 $tex[1]->draw_quad (0, 0, $w, $border); 984 $tex[1]->draw_quad (0, 0, $w, $border);
846 $tex[3]->draw_quad (0, $border, $border, $ch); 985 $tex[3]->draw_quad (0, $border, $border, $ch);
847 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 986 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
848 $tex[4]->draw_quad (0, $h - $border, $w, $border); 987 $tex[4]->draw_quad (0, $h - $border, $w, $border);
849 988
989 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
850 my $bg = $tex[0]; 990 my $bg = $tex[0];
851 991
852 # TODO: repeat texture not scale 992 # TODO: repeat texture not scale
853 my $rep_x = $cw / $bg->{w}; 993 my $rep_x = $cw / $bg->{w};
854 my $rep_y = $ch / $bg->{h}; 994 my $rep_y = $ch / $bg->{h};
855 995
856 glColor @{ $self->{bg} }; 996 glColor @{ $self->{bg} };
857 997
858 $bg->{s} = $rep_x; 998 $bg->{s} = $rep_x;
859 $bg->{t} = $rep_y; 999 $bg->{t} = $rep_y;
860 $bg->{wrap_mode} = 1; 1000 $bg->{wrap_mode} = 1;
861 $bg->draw_quad ($border, $border, $cw, $ch); 1001 $bg->draw_quad ($border, $border, $cw, $ch);
862 1002
863 glDisable GL_TEXTURE_2D; 1003 glDisable GL_TEXTURE_2D;
864 glDisable GL_BLEND; 1004 glDisable GL_BLEND;
1005 }
865 1006
866 $self->{title}->draw if $self->{title}; 1007 $self->{title}->draw if $self->{title};
1008
867 $self->child->draw; 1009 $self->child->draw;
868} 1010}
869 1011
870############################################################################# 1012#############################################################################
871 1013
893 $self->{children}[$y][$x] = $child; 1035 $self->{children}[$y][$x] = $child;
894 1036
895 $child->check_size; 1037 $child->check_size;
896} 1038}
897 1039
1040sub children {
1041 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1042}
1043
898# 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?
899sub clear { 1045sub clear {
900 my ($self) = @_; 1046 my ($self) = @_;
901 1047
1048 my @children = $self->children;
902 delete $self->{children}; 1049 delete $self->{children};
1050
1051 for (@children) {
1052 delete $_->{parent};
1053 $_->hide;
1054 }
1055
903 $self->update; 1056 $self->update;
904} 1057}
905 1058
906sub get_wh { 1059sub get_wh {
907 my ($self) = @_; 1060 my ($self) = @_;
1130 1283
1131sub new { 1284sub new {
1132 my ($class, %arg) = @_; 1285 my ($class, %arg) = @_;
1133 1286
1134 my $self = $class->SUPER::new ( 1287 my $self = $class->SUPER::new (
1135 fg => [1, 1, 1], 1288 fg => [1, 1, 1],
1136 #font => default_font 1289 #font => default_font
1137 fontsize => 1, 1290 fontsize => 1,
1138 text => "", 1291 text => "",
1139 align => -1, 1292 align => -1,
1140 valign => -1, 1293 valign => -1,
1141 padding => 2, 1294 padding => 2,
1142 layout => new CFClient::Layout, 1295 layout => new CFClient::Layout,
1143 can_events => 0, 1296 can_events => 0,
1144 %arg 1297 %arg
1145 ); 1298 );
1146 1299
1147 if (exists $self->{template}) { 1300 if (exists $self->{template}) {
1164 s/</&lt;/g; 1317 s/</&lt;/g;
1165 1318
1166 $_[1] 1319 $_[1]
1167} 1320}
1168 1321
1322sub update {
1323 my ($self) = @_;
1324
1325 delete $self->{texture};
1326 $self->SUPER::update;
1327}
1328
1169sub set_text { 1329sub set_text {
1170 my ($self, $text) = @_; 1330 my ($self, $text) = @_;
1171 1331
1332 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text";
1334
1172 $self->{layout}->set_text ($text); 1335 $self->{layout}->set_text ($text);
1173 1336
1174 delete $self->{texture}; 1337 delete $self->{texture};
1338 $self->update;
1175 $self->check_size; 1339 $self->check_size;
1176 $self->update;
1177} 1340}
1178 1341
1179sub set_markup { 1342sub set_markup {
1180 my ($self, $markup) = @_; 1343 my ($self, $markup) = @_;
1181 1344
1345 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup";
1347
1182 $self->{layout}->set_markup ($markup); 1348 $self->{layout}->set_markup ($markup);
1183 1349
1184 delete $self->{texture}; 1350 delete $self->{texture};
1351 $self->update;
1185 $self->check_size; 1352 $self->check_size;
1186 $self->update;
1187} 1353}
1188 1354
1189sub size_request { 1355sub size_request {
1190 my ($self) = @_; 1356 my ($self) = @_;
1191 1357
1220sub set_fontsize { 1386sub set_fontsize {
1221 my ($self, $fontsize) = @_; 1387 my ($self, $fontsize) = @_;
1222 1388
1223 $self->{fontsize} = $fontsize; 1389 $self->{fontsize} = $fontsize;
1224 delete $self->{texture}; 1390 delete $self->{texture};
1391
1392 $self->update;
1225 $self->check_size; 1393 $self->check_size;
1226 $self->update;
1227} 1394}
1228 1395
1229sub _draw { 1396sub _draw {
1230 my ($self) = @_; 1397 my ($self) = @_;
1231 1398
1271 1438
1272sub new { 1439sub new {
1273 my $class = shift; 1440 my $class = shift;
1274 1441
1275 $class->SUPER::new ( 1442 $class->SUPER::new (
1276 fg => [1, 1, 1], 1443 fg => [1, 1, 1],
1277 bg => [0, 0, 0, 0.2], 1444 bg => [0, 0, 0, 0.2],
1278 active_bg => [1, 1, 1, 0.5], 1445 active_bg => [1, 1, 1, 0.5],
1279 active_fg => [0, 0, 0], 1446 active_fg => [0, 0, 0],
1280 can_hover => 1, 1447 can_hover => 1,
1281 can_focus => 1, 1448 can_focus => 1,
1282 valign => 0, 1449 valign => 0,
1283 can_events => 1, 1450 can_events => 1,
1284 @_ 1451 @_
1285 ) 1452 )
1286} 1453}
1287 1454
1441 my ($self, $ev) = @_; 1608 my ($self, $ev) = @_;
1442 1609
1443 my $sym = $ev->{sym}; 1610 my $sym = $ev->{sym};
1444 1611
1445 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} = '';
1446 $self->emit (activate => $self->get_text); 1617 $self->emit (activate => $txt);
1447 $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 }
1448 1641
1449 } else { 1642 } else {
1450 $self->SUPER::key_down ($ev); 1643 $self->SUPER::key_down ($ev);
1451 } 1644 }
1452 1645
1466 1659
1467sub new { 1660sub new {
1468 my $class = shift; 1661 my $class = shift;
1469 1662
1470 $class->SUPER::new ( 1663 $class->SUPER::new (
1471 padding => 4, 1664 padding => 4,
1472 fg => [1, 1, 1], 1665 fg => [1, 1, 1],
1473 bg => [1, 1, 1, 0.2], 1666 bg => [1, 1, 1, 0.2],
1474 active_fg => [0, 0, 1], 1667 active_fg => [0, 0, 1],
1475 can_hover => 1, 1668 can_hover => 1,
1476 align => 0, 1669 align => 0,
1477 valign => 0, 1670 valign => 0,
1478 can_events => 1, 1671 can_events => 1,
1479 @_ 1672 @_
1480 ) 1673 )
1481} 1674}
1482 1675
1689} 1882}
1690 1883
1691sub set_max { 1884sub set_max {
1692 my ($self, $max) = @_; 1885 my ($self, $max) = @_;
1693 1886
1887 return if $self->{max_val} == $max;
1888
1694 $self->{max_val} = $max; 1889 $self->{max_val} = $max;
1890 $self->update;
1695} 1891}
1696 1892
1697sub set_value { 1893sub set_value {
1698 my ($self, $val, $max) = @_; 1894 my ($self, $val, $max) = @_;
1699 1895
1700 $self->set_max ($max) 1896 $self->set_max ($max)
1701 if defined $max; 1897 if defined $max;
1702 1898
1703 $max = $self->{max_val}; 1899 return if $self->{val} == $val;
1900
1704 $self->{val} = $val; 1901 $self->{val} = $val;
1705
1706 $self->update; 1902 $self->update;
1707} 1903}
1708 1904
1709sub _draw { 1905sub _draw {
1710 my ($self) = @_; 1906 my ($self) = @_;
1773 1969
1774sub new { 1970sub new {
1775 my ($class, %arg) = @_; 1971 my ($class, %arg) = @_;
1776 1972
1777 my $self = $class->SUPER::new ( 1973 my $self = $class->SUPER::new (
1778 tooltip => $arg{type}, 1974 tooltip => $arg{type},
1975 can_hover => 1,
1779 can_events => 1, 1976 can_events => 1,
1780 can_hover => 1,
1781 %arg, 1977 %arg,
1782 ); 1978 );
1783 1979
1784 $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");
1785 $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);
1793 1989
1794 $self->{value}->set_fontsize ($fsize); 1990 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize); 1991 $self->{max} ->set_fontsize ($fsize);
1796} 1992}
1797 1993
1994sub set_max {
1995 my ($self, $max) = @_;
1996
1997 $self->{gauge}->set_max ($max);
1998 $self->{max}->set_text ($max);
1999}
2000
1798sub set_value { 2001sub set_value {
1799 my ($self, $val, $max) = @_; 2002 my ($self, $val, $max) = @_;
1800 2003
1801 $self->set_max ($max) 2004 $self->set_max ($max)
1802 if defined $max; 2005 if defined $max;
1803 2006
1804 $self->{gauge}->set_value ($val, $max); 2007 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val); 2008 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1813} 2009}
1814 2010
1815############################################################################# 2011#############################################################################
1816 2012
1817package CFClient::UI::Slider; 2013package CFClient::UI::Slider;
1836 # TODO: calculations are off 2032 # TODO: calculations are off
1837 my $self = $class->SUPER::new ( 2033 my $self = $class->SUPER::new (
1838 fg => [1, 1, 1], 2034 fg => [1, 1, 1],
1839 active_fg => [0, 0, 0], 2035 active_fg => [0, 0, 0],
1840 range => [0, 0, 100, 10], 2036 range => [0, 0, 100, 10],
1841 req_w => 20, 2037 req_w => $::WIDTH / 80,
1842 req_h => 20, 2038 req_h => $::WIDTH / 80,
1843 vertical => 0, 2039 vertical => 0,
1844 can_hover => 1, 2040 can_hover => 1,
1845 inner_pad => 5, 2041 inner_pad => 5,
1846 @_ 2042 @_
1847 ); 2043 );
1957 2153
1958sub new { 2154sub new {
1959 my $class = shift; 2155 my $class = shift;
1960 2156
1961 my $self = $class->SUPER::new ( 2157 my $self = $class->SUPER::new (
1962 fontsize => 1, 2158 fontsize => 1,
2159 can_events => 0,
1963 #font => default_font 2160 #font => default_font
1964 @_, 2161 @_,
1965 2162
1966 layout => (new CFClient::Layout), 2163 layout => (new CFClient::Layout),
1967 par => [], 2164 par => [],
1968 height => 0, 2165 height => 0,
1969 children => [ 2166 children => [
1970 (new CFClient::UI::Empty expand => 1), 2167 (new CFClient::UI::Empty expand => 1),
1971 (new CFClient::UI::Slider vertical => 1), 2168 (new CFClient::UI::Slider vertical => 1),
1972 ], 2169 ],
1973 ); 2170 );
1974 2171
1975 $self->{children}[1]->connect (changed => sub { 2172 $self->{children}[1]->connect (changed => sub { $self->update });
1976 $self->update;
1977 });
1978 2173
1979 $self 2174 $self
1980} 2175}
1981 2176
1982sub set_fontsize { 2177sub set_fontsize {
1990 my ($self, $text) = @_; 2185 my ($self, $text) = @_;
1991 2186
1992 my $layout = $self->{layout}; 2187 my $layout = $self->{layout};
1993 2188
1994 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1995 $layout->set_width ($self->{w}); 2190 $layout->set_width ($self->{children}[0]{w});
1996 $layout->set_text ($text); 2191 $layout->set_text ($text);
1997 2192
1998 ($layout->size)[1] 2193 ($layout->size)[1]
1999} 2194}
2000 2195
2053 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2248 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2054 2249
2055 delete $self->{texture}; 2250 delete $self->{texture};
2056 } 2251 }
2057 2252
2058 $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 {
2059 glClearColor 0, 0, 0, 1; 2254 glClearColor 0, 0, 0, 0;
2060 glClear GL_COLOR_BUFFER_BIT; 2255 glClear GL_COLOR_BUFFER_BIT;
2061 2256
2062 glEnable GL_BLEND; 2257 glEnable GL_BLEND;
2063 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2064 glEnable GL_TEXTURE_2D; 2259 glEnable GL_TEXTURE_2D;
2096} 2291}
2097 2292
2098sub _draw { 2293sub _draw {
2099 my ($self) = @_; 2294 my ($self) = @_;
2100 2295
2101 if ($self->{texture}) { 2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2102 glEnable GL_TEXTURE_2D; 2298 glEnable GL_TEXTURE_2D;
2103 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;
2104 $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});
2105 glDisable GL_TEXTURE_2D; 2302 glDisable GL_TEXTURE_2D;
2106 } 2303 glDisable GL_BLEND;
2107 2304
2108 $self->{children}[1]->draw; 2305 $self->{children}[1]->draw;
2109 2306
2110} 2307}
2111 2308
2159 2356
2160sub new { 2357sub new {
2161 my $class = shift; 2358 my $class = shift;
2162 2359
2163 my $self = $class->SUPER::new ( 2360 my $self = $class->SUPER::new (
2164 state => 0, 2361 state => 0,
2165 connect_activate => \&toggle_flopper, 2362 connect_activate => \&toggle_flopper,
2166 can_events => 1,
2167 @_ 2363 @_
2168 ); 2364 );
2169 2365
2170 if ($self->{state}) { 2366 if ($self->{state}) {
2171 $self->{state} = 0; 2367 $self->{state} = 0;
2281 2477
2282sub size_request { 2478sub size_request {
2283 (32, 8) 2479 (32, 8)
2284} 2480}
2285 2481
2286sub draw { 2482sub _draw {
2287 my ($self) = @_; 2483 my ($self) = @_;
2288 2484
2485 return unless $::CONN;#d# manage and cache textures differently
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290 2487
2488 # TODO animation
2291 if ($tex) { 2489 if ($tex) {
2292 glEnable GL_BLEND; 2490 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D; 2492 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2300 } 2498 }
2301} 2499}
2302 2500
2303############################################################################# 2501#############################################################################
2304 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
2305package CFClient::UI::Root; 2680package CFClient::UI::Root;
2306 2681
2307our @ISA = CFClient::UI::Container::; 2682our @ISA = CFClient::UI::Container::;
2308 2683
2309use CFClient::OpenGL; 2684use CFClient::OpenGL;
2310 2685
2311sub check_size { 2686sub check_size {
2312 my ($self) = @_; 2687 my ($self) = @_;
2313 2688
2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2689 $self->configure (0, 0, $self->{w}, $self->{h});
2315} 2690}
2316 2691
2317sub size_request { 2692sub size_request {
2318 ($::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;
2319} 2719}
2320 2720
2321sub configure { 2721sub configure {
2322 my ($self, $x, $y, $w, $h) = @_; 2722 my ($self, $x, $y, $w, $h) = @_;
2323 2723
2324 $self->SUPER::configure ($x, $y, $w, $h); 2724 $self->SUPER::configure ($x, $y, $w, $h);
2325 2725
2326 for my $child (@{$self->{children}}) { 2726 for my $child ($self->children) {
2327 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)};
2328 2728
2329 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2729 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H); 2731 $child->configure ($X, $Y, $W, $H);
2332 } 2732 }
2333} 2733}
2334 2734
2335sub _topleft { 2735sub _topleft {
2336 my ($self, $x, $y) = @_; 2736 my ($self, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines