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.67 by root, Tue Apr 11 14:36:59 2006 UTC vs.
Revision 1.68 by root, Tue Apr 11 17:02:36 2006 UTC

114} 114}
115 115
116sub focus_in { 116sub focus_in {
117 my ($self) = @_; 117 my ($self) = @_;
118 118
119 return if $FOCUS == $self;
120
119 my $focus = $FOCUS; $FOCUS = $self; 121 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus; 122 $focus->update if $focus;
121 $FOCUS->update; 123 $FOCUS->update;
122} 124}
123 125
130 $focus->update if $focus; #? 132 $focus->update if $focus; #?
131} 133}
132 134
133sub mouse_motion { } 135sub mouse_motion { }
134sub button_up { } 136sub button_up { }
135sub button_down { }
136sub key_down { } 137sub key_down { }
137sub key_up { } 138sub key_up { }
139
140sub button_down {
141 my ($self, $ev, $x, $y) = @_;
142
143 $self->focus_in;
144}
138 145
139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 146sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 147sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 148sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 149sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 150sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
144 151
145sub draw { 152sub draw {
146 my ($self) = @_; 153 my ($self) = @_;
154
155 return unless $self->{h} && $self->{w};
147 156
148 glPushMatrix; 157 glPushMatrix;
149 glTranslate $self->{x}, $self->{y}, 0; 158 glTranslate $self->{x}, $self->{y}, 0;
150 $self->_draw; 159 $self->_draw;
151 if ($self == $HOVER) { 160 if ($self == $HOVER) {
215 #$self->deactivate; 224 #$self->deactivate;
216} 225}
217 226
218############################################################################# 227#############################################################################
219 228
229package CFClient::Widget::DrawBG;
230
231our @ISA = CFClient::Widget::;
232
233use strict;
234use SDL::OpenGL;
235
236sub new {
237 my $class = shift;
238
239 # range [value, low, high, page]
240
241 $class->SUPER::new (
242 bg => [0, 0, 0, 0.4],
243 active_bg => [1, 1, 1],
244 @_
245 )
246}
247
248sub _draw {
249 my ($self) = @_;
250
251 my ($w, $h) = @$self{qw(w h)};
252
253 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
254 glBegin GL_QUADS;
255 glVertex 0 , 0;
256 glVertex 0 , $h;
257 glVertex $w, $h;
258 glVertex $w, 0;
259 glEnd;
260}
261
262#############################################################################
263
220package CFClient::Widget::Empty; 264package CFClient::Widget::Empty;
221 265
222our @ISA = CFClient::Widget::; 266our @ISA = CFClient::Widget::;
223 267
224sub size_request { 268sub size_request {
321} 365}
322 366
323sub child { $_[0]->{children}[0] } 367sub child { $_[0]->{children}[0] }
324 368
325sub size_request { 369sub size_request {
326 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 370 $_[0]{children}[0]->size_request
327} 371}
328 372
329sub size_allocate { 373sub size_allocate {
330 my ($self, $w, $h) = @_; 374 my ($self, $w, $h) = @_;
331 375
376 return unless $self->{w} != $w || $self->{h} != $h;
377
332 $self->SUPER::size_allocate ($w, $h); 378 $self->SUPER::size_allocate ($w, $h);
333 $self->{children}[0]->size_allocate ($w, $h) 379 $self->{children}[0]->size_allocate ($w, $h);
334 if $self->{children}[0]
335} 380}
336 381
337############################################################################# 382#############################################################################
338 383
339package CFClient::Widget::Window; 384package CFClient::Widget::Window;
366} 411}
367 412
368sub size_allocate { 413sub size_allocate {
369 my ($self, $w, $h) = @_; 414 my ($self, $w, $h) = @_;
370 415
416 return unless $self->{w} != $w || $self->{h} != $h;
417
371 $self->{w} = $w; 418 $self->{w} = $w;
372 $self->{h} = $h; 419 $self->{h} = $h;
373 420
374 $self->child->size_allocate ($w, $h); 421 $self->child->size_allocate ($w, $h);
375 422
413} 460}
414 461
415sub size_allocate { 462sub size_allocate {
416 my ($self, $w, $h) = @_; 463 my ($self, $w, $h) = @_;
417 464
465 return unless $self->{w} != $w || $self->{h} != $h;
466
418 $self->{w} = $w; 467 $self->{w} = $w;
419 $self->{h} = $h; 468 $self->{h} = $h;
420 469
421 $self->child->size_allocate ($w - 4, $h - 4); 470 $self->child->size_allocate ($w - 4, $h - 4);
422 $self->child->move (2, 2); 471 $self->child->move (2, 2);
466} 515}
467 516
468sub size_allocate { 517sub size_allocate {
469 my ($self, $w, $h) = @_; 518 my ($self, $w, $h) = @_;
470 519
520 return unless $self->{w} != $w || $self->{h} != $h;
521
471 $self->SUPER::size_allocate ($w, $h); 522 $self->SUPER::size_allocate ($w, $h);
472 523
473 $h -= $tex[1]->{height}; 524 $h -= $tex[1]->{height};
474 $h -= $tex[4]->{height}; 525 $h -= $tex[4]->{height};
475 $w -= $tex[2]->{width}; 526 $w -= $tex[2]->{width};
632} 683}
633 684
634sub size_allocate { 685sub size_allocate {
635 my ($self, $w, $h) = @_; 686 my ($self, $w, $h) = @_;
636 687
688 return unless $self->{w} != $w || $self->{h} != $h;
689
637 $self->w ($w); 690 $self->{w} = $w;
638 $self->h ($h); 691 $self->{h} = $h;
639 692
640 my $exp; 693 return unless $self->{h};
641 my @oth; 694
642 # find expand widget 695 my $children = $self->{children};
643 for (@{$self->{children}}) { 696
644 if ($_->{expand}) { 697 my @h = map +($_->size_request)[1], @$children;
645 $exp = $_; 698
646 last; 699 my $req_h = List::Util::sum @h;
700
701 if ($req_h > $h) {
702 # ah well, not enough space
703 $_ = $h[$_] * $h / $req_h for @h;
704 } else {
705 my @exp = grep $_->{expand}, @$children;
706 @exp = @$children unless @exp;
707
708 my %exp = map +($_ => 1), @exp;
709
710 for (0 .. $#$children) {
711 my $child = $children->[$_];
712
713 my $alloc_h = $h[$_];
714 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
715 $h[$_] = $alloc_h;
647 } 716 }
648 push @oth, $_;
649 }
650
651 my ($ow, $oh);
652
653 # get sizes of other widgets
654 for (@oth) {
655 my ($w, $h) = $_->size_request;
656 $oh += $h;
657 if ($ow < $w) { $ow = $w }
658 } 717 }
659 718
660 my $y = 0; 719 my $y = 0;
661 for (@{$self->{children}}) { 720 for (0 .. $#$children) {
721 my $child = $children->[$_];
722 my $h = $h[$_];
662 $_->move (0, $y); 723 $child->move (0, $y);
663
664 if ($_ == $exp) {
665 $_->size_allocate ($w, $h - $oh);
666 $y += $h - $oh;
667 } else {
668 my ($cw, $h) = $_->size_request;
669 $_->size_allocate ($w, $h); 724 $child->size_allocate ($w, $h);
725
670 $y += $h; 726 $y += $h;
671 }
672 } 727 }
673} 728}
674 729
675############################################################################# 730#############################################################################
676 731
681use SDL::OpenGL; 736use SDL::OpenGL;
682 737
683sub new { 738sub new {
684 my ($class, %arg) = @_; 739 my ($class, %arg) = @_;
685 740
686 # TODO: color, and make height, xyz etc. optional
687 my $self = $class->SUPER::new ( 741 my $self = $class->SUPER::new (
688 color => [1, 1, 1], 742 fg => [1, 1, 1],
689 height => $::FONTSIZE, 743 height => $::FONTSIZE,
690 text => "", 744 text => "",
691 layout => new CFClient::Layout, 745 layout => new CFClient::Layout,
692 %arg 746 %arg
693 ); 747 );
695 $self->set_text ($self->{text}); 749 $self->set_text ($self->{text});
696 750
697 $self 751 $self
698} 752}
699 753
754sub escape_text {
755 local $_ = $_[1];
756
757 s/&/&amp;/g;
758 s/>/&gt;/g;
759 s/</&lt;/g;
760
761 $_[1]
762}
763
700sub set_text { 764sub set_text {
701 my ($self, $text) = @_; 765 my ($self, $text) = @_;
702 766
703 $self->{text} = $text; 767 $self->{text} = $text;
704 $self->{layout}->set_markup ($text); 768 $self->{layout}->set_markup ($text);
705 769
706 delete $self->{texture}; 770 delete $self->{texture};
771 $self->update;
707} 772}
708 773
709sub get_text { 774sub get_text {
710 my ($self, $text) = @_; 775 my ($self, $text) = @_;
711 776
731} 796}
732 797
733sub size_allocate { 798sub size_allocate {
734 my ($self, $w, $h) = @_; 799 my ($self, $w, $h) = @_;
735 800
801 return unless $self->{w} != $w || $self->{h} != $h;
802
736 $self->SUPER::size_allocate ($w, $h); 803 $self->SUPER::size_allocate ($w, $h);
737 delete $self->{texture}; 804 delete $self->{texture};
805}
806
807sub update {
808 my ($self) = @_;
809
810 delete $self->{texture};
811 $self->SUPER::update;
738} 812}
739 813
740sub _draw { 814sub _draw {
741 my ($self) = @_; 815 my ($self) = @_;
742 816
748 glEnable GL_BLEND; 822 glEnable GL_BLEND;
749 glEnable GL_TEXTURE_2D; 823 glEnable GL_TEXTURE_2D;
750 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 824 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
751 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 825 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
752 826
753 glColor @{$self->{color}}; 827 glColor @{$self->{fg}};
754 828
755 $tex->draw_quad (0, 0); 829 $tex->draw_quad (0, 0);
756 830
757 glDisable GL_BLEND; 831 glDisable GL_BLEND;
758 glDisable GL_TEXTURE_2D; 832 glDisable GL_TEXTURE_2D;
765our @ISA = CFClient::Widget::Label::; 839our @ISA = CFClient::Widget::Label::;
766 840
767use SDL; 841use SDL;
768use SDL::OpenGL; 842use SDL::OpenGL;
769 843
844sub new {
845 my $class = shift;
846
847 $class->SUPER::new (
848 fg => [1, 1, 1],
849 bg => [0, 0, 0, 0.4],
850 active_bg => [1, 1, 1],
851 active_fg => [0, 0, 0],
852 @_
853 )
854}
855
856sub _set_text {
857 my ($self, $text) = @_;
858
859 $self->{last_activity} = $::NOW;
860
861 $self->{text} = $text;
862 $self->{layout}->set_width ($self->{w});
863 $self->{layout}->set_markup ($self->escape_text ($text));
864
865 $text = substr $text, 0, $self->{cursor};
866 utf8::encode $text;
867
868 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
869}
870
871sub size_request {
872 my ($self) = @_;
873
874 my ($w, $h) = $self->SUPER::size_request;
875
876 ($w + 1, $h) # add 1 for cursor
877}
878
879sub size_allocate {
880 my ($self, $w, $h) = @_;
881
882 return unless $self->{w} != $w || $self->{h} != $h;
883
884 $self->SUPER::size_allocate ($w, $h);
885
886 $self->_set_text ($self->{text});
887}
888
889sub set_text {
890 my ($self, $text) = @_;
891
892 $self->{cursor} = length $text;
893 $self->_set_text ($text);
894 $self->update;
895}
896
770sub key_down { 897sub key_down {
771 my ($self, $ev) = @_; 898 my ($self, $ev) = @_;
772 899
773 my $mod = $ev->key_mod; 900 my $mod = $ev->key_mod;
774 my $sym = $ev->key_sym; 901 my $sym = $ev->key_sym;
776 my $uni = $ev->key_unicode; 903 my $uni = $ev->key_unicode;
777 904
778 my $text = $self->get_text; 905 my $text = $self->get_text;
779 906
780 if ($sym == SDLK_BACKSPACE) { 907 if ($sym == SDLK_BACKSPACE) {
781 substr $text, -1, 1, ''; 908 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
909 } elsif ($sym == SDLK_DELETE) {
910 substr $text, $self->{cursor}, 1, "";
911 } elsif ($sym == SDLK_LEFT) {
912 --$self->{cursor} if $self->{cursor};
913 } elsif ($sym == SDLK_RIGHT) {
914 ++$self->{cursor} if $self->{cursor} < length $self->{text};
782 } elsif ($uni) { 915 } elsif ($uni) {
783 $text .= chr $uni; 916 substr $text, $self->{cursor}++, 0, chr $uni;
784 } 917 }
785 918
786 $self->set_text ($text); 919 $self->_set_text ($text);
920 $self->update;
921}
922
923sub focus_in {
924 my ($self) = @_;
925
926 $self->{last_activity} = $::NOW;
927
928 $self->SUPER::focus_in;
787} 929}
788 930
789sub button_down { 931sub button_down {
790 my ($self, $ev) = @_; 932 my ($self, $ev, $x, $y) = @_;
791 933
792 $self->focus_in; 934 $self->SUPER::button_down ($ev, $x, $y);
935
936 my $idx = $self->{layout}->xy_to_index ($x, $y);
937
938 # byte-index to char-index
939 my $text = $self->{layout};
940 utf8::encode $text;
941 $self->{cursor} = length substr $text, 0, $idx;
942
943 $self->_set_text ($self->{text});
944 $self->update;
793} 945}
794 946
795sub mouse_motion { 947sub mouse_motion {
796 my ($self, $ev, $x, $y) = @_; 948 my ($self, $ev, $x, $y) = @_;
797 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 949# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
798} 950}
799 951
800sub _draw { 952sub _draw {
801 my ($self) = @_; 953 my ($self) = @_;
802 954
955 local $self->{fg} = $self->{fg};
956
803 if ($FOCUS == $self) { 957 if ($FOCUS == $self) {
804 glColor 1, 1, 1; 958 glColor @{$self->{active_bg}};
959 $self->{fg} = $self->{active_fg};
805 } else { 960 } else {
806 glColor 0.7, 0.7, 0.7; 961 glColor @{$self->{bg}};
807 } 962 }
808 963
809 glBegin GL_QUADS; 964 glBegin GL_QUADS;
810 glVertex 0 , 0; 965 glVertex 0 , 0;
811 glVertex 0 , $self->{h} - 1; 966 glVertex 0 , $self->{h};
812 glVertex $self->{w} - 1, $self->{h} - 1; 967 glVertex $self->{w}, $self->{h};
813 glVertex $self->{w} - 1, 0; 968 glVertex $self->{w}, 0;
814 glEnd; 969 glEnd;
815 970
816 $self->SUPER::_draw; 971 $self->SUPER::_draw;
972
973 #TODO: force update every cursor change :(
974 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
975 glColor @{$self->{fg}};
976 glBegin GL_LINES;
977 glVertex $self->{cur_x}, $self->{cur_y};
978 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
979 glEnd;
980 }
981}
982
983#############################################################################
984
985package CFClient::Widget::Slider;
986
987use strict;
988
989use SDL::OpenGL;
990use SDL::OpenGL::Constants;
991
992our @ISA = CFClient::Widget::DrawBG::;
993
994sub size_request {
995 my ($self) = @_;
996
997 my $w =
998 my $h = 10;
999
1000 $self->{vertical} ? ($h, $w) : ($w, $h)
1001}
1002
1003sub new {
1004 my $class = shift;
1005
1006 # range [value, low, high, page]
1007
1008 $class->SUPER::new (
1009 fg => [1, 1, 1],
1010 active_fg => [0, 0, 0],
1011 range => [0, 0, 100, 10],
1012 vertical => 0,
1013 @_
1014 )
1015}
1016
1017sub _draw {
1018 my ($self) = @_;
1019
1020 $self->SUPER::_draw ();
1021
1022 my ($w, $h) = @$self{qw(w h)};
1023
1024 if ($self->{vertical}) {
1025 # draw a vertical slider like a rotated horizontal slider
1026
1027 glTranslate 0, $self->{w};
1028 glRotate 90, 0, 0, 1;
1029
1030 ($w, $h) = ($h, $w);
1031 }
1032
1033 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1034 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1035
1036 glColor @$fg;
1037 glBegin GL_LINES;
1038 glVertex 0, 0; glVertex 0, $h;
1039 glVertex $w - 1, 0; glVertex $w - 1, $h;
1040 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1041 glEnd;
817} 1042}
818 1043
819############################################################################# 1044#############################################################################
820 1045
821package CFClient::Widget::MapWidget; 1046package CFClient::Widget::MapWidget;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines