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.47 by root, Sun Apr 9 22:24:43 2006 UTC vs.
Revision 1.55 by root, Mon Apr 10 19:34:04 2006 UTC

7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use Crossfire::Client;
11 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
16
12# class methods for events 17# class methods for events
13sub feed_sdl_key_down_event { $::FOCUS->key_down ($_[0]) if $::FOCUS } 18sub feed_sdl_key_down_event {
14sub feed_sdl_key_up_event { $::FOCUS->key_up ($_[0]) if $::FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
15sub feed_sdl_button_down_event { } 26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev) if $GRAB;
40}
41
16sub feed_sdl_button_up_event { } 42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev) if $HOVER;
71}
17 72
18sub new { 73sub new {
19 my $class = shift; 74 my $class = shift;
20 75
21 bless { @_ }, $class 76 bless { @_ }, $class
43 $self->{w} = $w; 98 $self->{w} = $w;
44 $self->{h} = $h; 99 $self->{h} = $h;
45} 100}
46 101
47sub focus_in { 102sub focus_in {
48 my ($widget) = @_; 103 my ($self) = @_;
49 $::FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
50} 108}
51 109
52sub focus_out { 110sub focus_out {
53 my ($widget) = @_; 111 my ($self) = @_;
54}
55 112
56sub key_down { 113 return unless $FOCUS == $self;
57 my ($widget, $sdlev) = @_;
58}
59 114
60sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
61 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
62} 117}
63 118
119sub mouse_motion { }
120sub button_up { }
64sub button_down { 121sub button_down { }
65 my ($widget, $sdlev) = @_; 122sub key_down { }
66} 123sub key_up { }
67 124
68sub button_up {
69 my ($widget, $sdlev) = @_;
70}
71
72sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
73sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
74sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
75sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
76sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
77 130
78sub draw { 131sub draw {
79 my ($self) = @_; 132 my ($self) = @_;
80 133
81 glPushMatrix; 134 glPushMatrix;
82 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
83 $self->_draw; 136 $self->_draw;
84 if ($self == $::HOVER) { 137 if ($self == $HOVER) {
85 glColor 1, 1, 1, 1; 138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
86 glBegin GL_LINES; 140 glBegin GL_QUADS;
87 glVertex 0, 0; 141 glVertex 0 , 0;
88 glVertex $self->{w} - 1, 0; 142 glVertex $self->{w}, 0;
89 glVertex $self->{w} - 1, $self->{h} - 1; 143 glVertex $self->{w}, $self->{h};
90 glVertex 0, $self->{h} - 1; 144 glVertex 0 , $self->{h};
91 glVertex 0, 0;
92 glEnd; 145 glEnd;
146 glDisable GL_BLEND;
93 } 147 }
94 glPopMatrix; 148 glPopMatrix;
95} 149}
96 150
97sub _draw { 151sub _draw {
222 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
223 277
224 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
225 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
226 if $self->{children}[0] 280 if $self->{children}[0]
227}
228
229#############################################################################
230
231package Crossfire::Client::Widget::Toplevel;
232
233our @ISA = Crossfire::Client::Widget::Container::;
234
235sub update {
236 my ($self) = @_;
237
238 ::refresh ();
239}
240
241sub add {
242 my ($self, $widget) = @_;
243
244 $self->SUPER::add ($widget);
245
246 $widget->size_allocate ($widget->size_request);
247} 281}
248 282
249############################################################################# 283#############################################################################
250 284
251package Crossfire::Client::Widget::Window; 285package Crossfire::Client::Widget::Window;
631use SDL::OpenGL; 665use SDL::OpenGL;
632 666
633sub new { 667sub new {
634 my ($class, $x, $y, $z, $height, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
635 669
670 $height ||= $::FONTSIZE;
671
636 # TODO: color, and make height, xyz etc. optional 672 # TODO: color, and make height, xyz etc. optional
637 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
638 674
639 $self->set_text ($text); 675 $self->set_text ($text);
640 676
657} 693}
658 694
659sub size_request { 695sub size_request {
660 my ($self) = @_; 696 my ($self) = @_;
661 697
662 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
663 $self->{texture}{width}, 700 $self->{texture}{width},
664 $self->{texture}{height}, 701 $self->{texture}{height},
702 )
703 } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705
706 ($w, $h)
665 ) 707 }
708
666} 709}
667 710
668sub _draw { 711sub _draw {
669 my ($self) = @_; 712 my ($self) = @_;
670 713
689 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
690} 733}
691 734
692############################################################################# 735#############################################################################
693 736
694package Crossfire::Client::Widget::TextEntry; 737package Crossfire::Client::Widget::Entry;
695 738
696our @ISA = Crossfire::Client::Widget::Label::; 739our @ISA = Crossfire::Client::Widget::Label::;
697 740
698use SDL; 741use SDL;
699use SDL::OpenGL; 742use SDL::OpenGL;
702 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
703 746
704 my $mod = $ev->key_mod; 747 my $mod = $ev->key_mod;
705 my $sym = $ev->key_sym; 748 my $sym = $ev->key_sym;
706 749
707 $ev->set_unicode (1);
708 my $uni = $ev->key_unicode; 750 my $uni = $ev->key_unicode;
709 751
710 my $text = $self->get_text; 752 my $text = $self->get_text;
711 753
712 if ($sym == SDLK_BACKSPACE) { 754 if ($sym == SDLK_BACKSPACE) {
713 substr $text, -1, 1, ''; 755 substr $text, -1, 1, '';
714
715 } elsif ($uni) { 756 } elsif ($uni) {
716 $text .= chr $uni; 757 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
717 } 759 }
760
718 $self->set_text ($text); 761 $self->set_text ($text);
762}
763
764sub button_down {
765 my ($self, $ev) = @_;
766
767 $self->focus_in;
768}
769
770sub _draw {
771 my ($self) = @_;
772
773 if ($FOCUS == $self) {
774 glColor 1, 1, 1;
775 } else {
776 glColor 0.7, 0.7, 0.7;
777 }
778
779 glBegin GL_QUADS;
780 glVertex 0 , 0;
781 glVertex 0 , $self->{h} - 1;
782 glVertex $self->{w} - 1, $self->{h} - 1;
783 glVertex $self->{w} - 1, 0;
784 glEnd;
785
786 $self->SUPER::_draw;
719} 787}
720 788
721############################################################################# 789#############################################################################
722 790
723package Crossfire::Client::Widget::MapWidget; 791package Crossfire::Client::Widget::MapWidget;
738 806
739sub key_up { 807sub key_up {
740} 808}
741 809
742sub size_request { 810sub size_request {
743 811 (
744} 812 1 + int $::WIDTH / 32,
745 813 1 + int $::HEIGHT / 32,
746sub size_allocate { 814 )
747} 815}
748 816
749sub _draw { 817sub _draw {
750 my ($self) = @_; 818 my ($self) = @_;
751 819
784 my $cell = $map->[$x + $xofs][$y + $yofs] 852 my $cell = $map->[$x + $xofs][$y + $yofs]
785 or next; 853 or next;
786 854
787 my $darkness = $cell->[0] * (1 / 255); 855 my $darkness = $cell->[0] * (1 / 255);
788 if ($darkness < 0) { 856 if ($darkness < 0) {
789 $darkness = 0.15; 857 $darkness = $cell->[1] ? 0.1 : 0;
790 } 858 }
791 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
792 860
793 for my $num (grep $_, @$cell[1,2,3]) { 861 for my $num (grep $_, @$cell[1,2,3]) {
794 my $tex = $::CONN->{face}[$num]{texture} || next; 862 my $tex = $::CONN->{face}[$num]{texture} || next;
823 891
824 $lighting = new Crossfire::Client::Texture 892 $lighting = new Crossfire::Client::Texture
825 width => $sw4, 893 width => $sw4,
826 height => $sh, 894 height => $sh,
827 data => $lighting, 895 data => $lighting,
828 internalformat => GL_ALPHA4, 896 internalformat => GL_ALPHA,
829 format => GL_ALPHA; 897 format => GL_ALPHA;
830 898
831 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
832 glColor 0, 0, 0, 0.75; 900 glColor 0.45, 0.45, 0.45, 1;
833 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
834 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
835 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
836 glBegin GL_QUADS; 904 glBegin GL_QUADS;
837 glTexCoord 0, 0; glVertex 0 , 0; 905 glTexCoord 0, 0; glVertex 0 , 0;
905 973
906sub moveto { 974sub moveto {
907 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
908 976
909 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
910 $self->{speed} = 0.2; 978 $self->{speed} = 2;
911 $self->{time} = 1; 979 $self->{time} = 1;
912 980
913 ::animation_start $self; 981 ::animation_start $self;
914} 982}
915 983
930 998
931sub _draw { 999sub _draw {
932 my ($self) = @_; 1000 my ($self) = @_;
933 1001
934 glPushMatrix; 1002 glPushMatrix;
935 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
936 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
937 glPopMatrix; 1005 glPopMatrix;
938} 1006}
939 1007
9401; 1008#############################################################################
941 1009
1010package Crossfire::Client::Widget::Toplevel;
1011
1012our @ISA = Crossfire::Client::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub size_allocate {
1019 my ($self, $w, $h) = @_;
1020
1021 $self->SUPER::size_allocate ($w, $h);
1022
1023 $_->size_allocate ($_->size_request)
1024 for @{$self->{children}};
1025}
1026
1027sub update {
1028 my ($self) = @_;
1029
1030 $self->size_allocate ($self->size_request);
1031 ::refresh ();
1032}
1033
1034sub add {
1035 my ($self, $widget) = @_;
1036
1037 $self->SUPER::add ($widget);
1038
1039 $widget->size_allocate ($widget->size_request);
1040}
1041
1042sub draw {
1043 my ($self) = @_;
1044
1045 $self->_draw;
1046}
1047
1048#############################################################################
1049
1050package Crossfire::Client::Widget;
1051
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1053
10541
1055

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines