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.50 by root, Sun Apr 9 22:27:24 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, 0.4; 138 glColor 1, 1, 1, 0.4;
86 glEnable GL_BLEND; 139 glEnable GL_BLEND;
87 glBegin GL_QUADS; 140 glBegin GL_QUADS;
88 glVertex 0, 0; 141 glVertex 0 , 0;
89 glVertex $self->{w} - 1, 0; 142 glVertex $self->{w}, 0;
90 glVertex $self->{w} - 1, $self->{h} - 1; 143 glVertex $self->{w}, $self->{h};
91 glVertex 0, $self->{h} - 1; 144 glVertex 0 , $self->{h};
92 glEnd; 145 glEnd;
93 glDisable GL_BLEND; 146 glDisable GL_BLEND;
94 } 147 }
95 glPopMatrix; 148 glPopMatrix;
96} 149}
223 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
224 277
225 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
226 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
227 if $self->{children}[0] 280 if $self->{children}[0]
228}
229
230#############################################################################
231
232package Crossfire::Client::Widget::Toplevel;
233
234our @ISA = Crossfire::Client::Widget::Container::;
235
236sub update {
237 my ($self) = @_;
238
239 ::refresh ();
240}
241
242sub add {
243 my ($self, $widget) = @_;
244
245 $self->SUPER::add ($widget);
246
247 $widget->size_allocate ($widget->size_request);
248} 281}
249 282
250############################################################################# 283#############################################################################
251 284
252package Crossfire::Client::Widget::Window; 285package Crossfire::Client::Widget::Window;
632use SDL::OpenGL; 665use SDL::OpenGL;
633 666
634sub new { 667sub new {
635 my ($class, $x, $y, $z, $height, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
636 669
670 $height ||= $::FONTSIZE;
671
637 # TODO: color, and make height, xyz etc. optional 672 # TODO: color, and make height, xyz etc. optional
638 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);
639 674
640 $self->set_text ($text); 675 $self->set_text ($text);
641 676
658} 693}
659 694
660sub size_request { 695sub size_request {
661 my ($self) = @_; 696 my ($self) = @_;
662 697
663 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
664 $self->{texture}{width}, 700 $self->{texture}{width},
665 $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)
666 ) 707 }
708
667} 709}
668 710
669sub _draw { 711sub _draw {
670 my ($self) = @_; 712 my ($self) = @_;
671 713
690 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
691} 733}
692 734
693############################################################################# 735#############################################################################
694 736
695package Crossfire::Client::Widget::TextEntry; 737package Crossfire::Client::Widget::Entry;
696 738
697our @ISA = Crossfire::Client::Widget::Label::; 739our @ISA = Crossfire::Client::Widget::Label::;
698 740
699use SDL; 741use SDL;
700use SDL::OpenGL; 742use SDL::OpenGL;
703 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
704 746
705 my $mod = $ev->key_mod; 747 my $mod = $ev->key_mod;
706 my $sym = $ev->key_sym; 748 my $sym = $ev->key_sym;
707 749
708 $ev->set_unicode (1);
709 my $uni = $ev->key_unicode; 750 my $uni = $ev->key_unicode;
710 751
711 my $text = $self->get_text; 752 my $text = $self->get_text;
712 753
713 if ($sym == SDLK_BACKSPACE) { 754 if ($sym == SDLK_BACKSPACE) {
714 substr $text, -1, 1, ''; 755 substr $text, -1, 1, '';
715
716 } elsif ($uni) { 756 } elsif ($uni) {
717 $text .= chr $uni; 757 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
718 } 759 }
760
719 $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;
720} 787}
721 788
722############################################################################# 789#############################################################################
723 790
724package Crossfire::Client::Widget::MapWidget; 791package Crossfire::Client::Widget::MapWidget;
739 806
740sub key_up { 807sub key_up {
741} 808}
742 809
743sub size_request { 810sub size_request {
744 811 (
745} 812 1 + int $::WIDTH / 32,
746 813 1 + int $::HEIGHT / 32,
747sub size_allocate { 814 )
748} 815}
749 816
750sub _draw { 817sub _draw {
751 my ($self) = @_; 818 my ($self) = @_;
752 819
785 my $cell = $map->[$x + $xofs][$y + $yofs] 852 my $cell = $map->[$x + $xofs][$y + $yofs]
786 or next; 853 or next;
787 854
788 my $darkness = $cell->[0] * (1 / 255); 855 my $darkness = $cell->[0] * (1 / 255);
789 if ($darkness < 0) { 856 if ($darkness < 0) {
790 $darkness = 0.15; 857 $darkness = $cell->[1] ? 0.1 : 0;
791 } 858 }
792 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
793 860
794 for my $num (grep $_, @$cell[1,2,3]) { 861 for my $num (grep $_, @$cell[1,2,3]) {
795 my $tex = $::CONN->{face}[$num]{texture} || next; 862 my $tex = $::CONN->{face}[$num]{texture} || next;
824 891
825 $lighting = new Crossfire::Client::Texture 892 $lighting = new Crossfire::Client::Texture
826 width => $sw4, 893 width => $sw4,
827 height => $sh, 894 height => $sh,
828 data => $lighting, 895 data => $lighting,
829 internalformat => GL_ALPHA4, 896 internalformat => GL_ALPHA,
830 format => GL_ALPHA; 897 format => GL_ALPHA;
831 898
832 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
833 glColor 0, 0, 0, 0.75; 900 glColor 0.45, 0.45, 0.45, 1;
834 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
835 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
836 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
837 glBegin GL_QUADS; 904 glBegin GL_QUADS;
838 glTexCoord 0, 0; glVertex 0 , 0; 905 glTexCoord 0, 0; glVertex 0 , 0;
906 973
907sub moveto { 974sub moveto {
908 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
909 976
910 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
911 $self->{speed} = 0.2; 978 $self->{speed} = 2;
912 $self->{time} = 1; 979 $self->{time} = 1;
913 980
914 ::animation_start $self; 981 ::animation_start $self;
915} 982}
916 983
931 998
932sub _draw { 999sub _draw {
933 my ($self) = @_; 1000 my ($self) = @_;
934 1001
935 glPushMatrix; 1002 glPushMatrix;
936 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
937 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
938 glPopMatrix; 1005 glPopMatrix;
939} 1006}
940 1007
9411; 1008#############################################################################
942 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