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.6 by elmex, Fri Apr 7 19:58:55 2006 UTC vs.
Revision 1.17 by elmex, Sat Apr 8 13:34:19 2006 UTC

1package Crossfire::Client::Widget; 1package Crossfire::Client::Widget;
2
2use strict; 3use strict;
4use SDL::OpenGL;
5use SDL::OpenGL::Constants;
3 6
4our $FOCUS; # the widget with current focus 7our $FOCUS; # the widget with current focus
5our %ACTIVE_WIDGETS; 8our @ACTIVE_WIDGETS;
6 9
7# class methods for events 10# class methods for events
8sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
9sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
10sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
11sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 14sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS }
12 15
13sub new { 16sub new {
14 my $class = shift; 17 my $class = shift;
15 my $self = { @_ }; 18
16 bless $self, $class; 19 bless { @_ }, $class
17 return $self;
18} 20}
19 21
20sub activate { 22sub activate {
21 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 23 push @ACTIVE_WIDGETS, $_[0];
22} 24}
23 25
24sub deactivate { 26sub deactivate {
25 delete $ACTIVE_WIDGETS{$_[0]}; 27 @ACTIVE_WIDGETS =
28 sort { $a->{z} <=> $b->{z} }
29 grep { $_ != $_[0] }
30 @ACTIVE_WIDGETS;
31}
32
33sub size_request {
34 die "size_request is abtract";
26} 35}
27 36
28sub focus_in { 37sub focus_in {
29 my ($widget) = @_; 38 my ($widget) = @_;
30 $FOCUS = $widget; 39 $FOCUS = $widget;
48 57
49sub button_up { 58sub button_up {
50 my ($widget, $sdlev) = @_; 59 my ($widget, $sdlev) = @_;
51} 60}
52 61
62sub w { $_[0]->{w} }
63sub h { $_[0]->{h} }
64sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
65sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
66sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
67
53sub draw { 68sub draw {
69 my ($self) = @_;
70
71 glPushMatrix;
72 glTranslate $self->{x}, $self->{y}, 0;
73 $self->_draw;
74 glPopMatrix;
75}
76
77sub _draw {
54 my ($widget) = @_; 78 my ($widget) = @_;
55} 79}
56 80
57sub bbox { 81sub bbox {
58 my ($widget) = @_; 82 my ($widget) = @_;
59} 83}
60 84
61package Client::TextView; 85package Crossfire::Client::Widget::Container;
86
87our @ISA = Crossfire::Client::Widget::;
88
89use SDL::OpenGL;
90
91sub add { $_[0]->{child} = $_[1] }
92sub get { $_[0]->{child} }
93
94sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
95
96sub _draw { die "Containers can't be drawn!" }
97
98package Crossfire::Client::Widget::Frame;
99
100our @ISA = Crossfire::Client::Widget::Container::;
101
102use SDL::OpenGL;
103
104sub size_request {
105 my ($self) = @_;
106 my $chld = $self->get
107 or return (0, 0);
108 map { $_ + 4 } $chld->size_request;
109}
110
111sub _draw {
112 my ($self) = @_;
113
114 my $chld = $self->get;
115
116 my ($w, $h) = $chld->size_request;
117
118 glColor 1, 0, 0;
119 glBegin GL_QUADS;
120 glTexCoord 0, 0; glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0;
124 glEnd;
125
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw;
129 glPopMatrix;
130}
131
132package Crossfire::Client::Widget::Table;
133
134our @ISA = Crossfire::Client::Widget::Container::;
135
136use SDL::OpenGL;
137
138sub add {
139 my ($self, $x, $y, $chld) = @_;
140 $self->{childs}[$y][$x] = $chld;
141}
142
143sub max_row_height {
144 my ($self, $row) = @_;
145
146 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi];
149 if ($c) {
150 my ($w, $h) = $c->size_request;
151 if ($hs < $h) { $hs = $h }
152 }
153 }
154 return $hs;
155}
156
157sub max_col_width {
158 my ($self, $col) = @_;
159
160 my $ws = 0;
161 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
162 my $c = ($self->{childs}->[$yi] || [])->[$col];
163 if ($c) {
164 my ($w, $h) = $c->size_request;
165 if ($ws < $w) { $ws = $w }
166 }
167 }
168 return $ws;
169}
170
171sub size_request {
172 my ($self) = @_;
173
174 my ($hs, $ws) = (0, 0);
175
176 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
177 $hs += $self->max_row_height ($yi);
178 }
179
180 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
181 my $wm = 0;
182 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
183 $wm += $self->max_col_width ($xi)
184 }
185 if ($ws < $wm) { $ws = $wm }
186 }
187
188 return ($ws, $hs);
189}
190
191sub _draw {
192 my ($self) = @_;
193
194 my $y = 0;
195 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
196 my $x = 0;
197
198 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
199
200 glPushMatrix;
201 glTranslate ($x, $y, 0);
202 my $c = $self->{childs}->[$yi]->[$xi];
203 $c->_draw if $c;
204 glPopMatrix;
205
206 $x += $self->max_col_width ($xi);
207 }
208
209 $y += $self->max_row_height ($yi);
210 }
211}
212
213package Crossfire::Client::Widget::VBox;
214
215our @ISA = Crossfire::Client::Widget::Container::;
216
217use SDL::OpenGL;
218
219sub add {
220 my ($self, $chld) = @_;
221 push @{$self->{childs}}, $chld;
222}
223
224sub size_request {
225 my ($self) = @_;
226
227 my ($hs, $ws) = (0, 0);
228 for (@{$self->{childs} || []}) {
229 my ($w, $h) = $_->size_request;
230 $hs += $h;
231 if ($ws < $w) { $ws = $w }
232 }
233
234 return ($ws, $hs);
235}
236
237sub _draw {
238 my ($self) = @_;
239
240 my ($x, $y);
241 for (@{$self->{childs} || []}) {
242 glPushMatrix;
243 glTranslate (0, $y, 0);
244 $_->_draw;
245 glPopMatrix;
246 my ($w, $h) = $_->size_request;
247 $y += $h;
248 }
249}
250
251package Crossfire::Client::Widget::Label;
252
253our @ISA = Crossfire::Client::Widget::;
254
255use SDL::OpenGL;
256
257sub new {
258 my ($class, $x, $y, $z, $ttf, $text) = @_;
259
260 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
261
262 $self->set_text ($text);
263
264 $self
265}
266
267sub set_text {
268 my ($self, $text) = @_;
269 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
270}
271
272sub get_text {
273 my ($self, $text) = @_;
274 $self->{text}
275}
276
277sub size_request {
278 my ($self) = @_;
279
280 (
281 $self->{texture}{width},
282 $self->{texture}{height},
283 )
284}
285
286sub _draw {
287 my ($self) = @_;
288
289 my $tex = $self->{texture};
290
291 $self->{x}--;
292
293 glEnable GL_BLEND;
294 glEnable GL_TEXTURE_2D;
295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
296 glBindTexture GL_TEXTURE_2D, $tex->{name};
297
298 glColor 1, 1, 1;
299
300 glBegin GL_QUADS;
301 glTexCoord 0, 0; glVertex 0 , 0;
302 glTexCoord 0, 1; glVertex 0 , $tex->{height};
303 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
304 glTexCoord 1, 0; glVertex $tex->{width}, 0;
305 glEnd;
306
307 glDisable GL_BLEND;
308 glDisable GL_TEXTURE_2D;
309}
310
311package Crossfire::Client::Widget::TextView;
62 312
63use strict; 313use strict;
314
64our @ISA = qw/Client::Widget/; 315our @ISA = qw/Crossfire::Client::Widget/;
65 316
66use SDL::OpenGL; 317use SDL::OpenGL;
67use SDL::OpenGL::Constants; 318use SDL::OpenGL::Constants;
68 319
69sub add_line { 320sub add_line {
70 my ($self, $line) = @_; 321 my ($self, $line) = @_;
71 push @{$self->{lines}}, $line; 322 push @{$self->{lines}}, $line;
72} 323}
73 324
74sub draw { 325sub _draw {
75 my ($self) = @_; 326 my ($self) = @_;
76 327
77} 328}
78 329
79package Client::MapWidget; 330package Crossfire::Client::Widget::MapWidget;
80 331
81use strict; 332use strict;
333
82our @ISA = qw/Client::Widget/; 334our @ISA = qw/Crossfire::Client::Widget/;
83 335
336use SDL;
84use SDL::OpenGL; 337use SDL::OpenGL;
85use SDL::OpenGL::Constants; 338use SDL::OpenGL::Constants;
86 339
87sub key_down { 340sub key_down {
88 print "MAPKEYDOWN\n"; 341 print "MAPKEYDOWN\n";
89} 342}
90 343
91sub key_up { 344sub key_up {
92} 345}
93 346
94my $x;
95
96sub draw { 347sub _draw {
97 glEnable GL_TEXTURE_2D; 348 glEnable GL_TEXTURE_2D;
98 glEnable GL_BLEND; 349 glEnable GL_BLEND;
99 350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
100 glPushMatrix;
101 351
102 my $map = $::CONN->{map}; 352 my $map = $::CONN->{map};
103 353
104 for my $x (0 .. $::CONN->{mapw} - 1) { 354 for my $x (0 .. $::CONN->{mapw} - 1) {
105 for my $y (0 .. $::CONN->{maph} - 1) { 355 for my $y (0 .. $::CONN->{maph} - 1) {
114 my $tex = $::CONN->{face}[$num]{texture} || next; 364 my $tex = $::CONN->{face}[$num]{texture} || next;
115 365
116 glBindTexture GL_TEXTURE_2D, $tex->{name}; 366 glBindTexture GL_TEXTURE_2D, $tex->{name};
117 367
118 glBegin GL_QUADS; 368 glBegin GL_QUADS;
119 glTexCoord 0, 0; glVertex $x, $y; 369 glTexCoord 0, 0; glVertex $x * 32 , $y * 32;
120 glTexCoord 0, 1; glVertex $x, $y + 1; 370 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32;
121 glTexCoord 1, 1; glVertex $x + 1, $y + 1; 371 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32;
122 glTexCoord 1, 0; glVertex $x + 1, $y; 372 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32;
123 glEnd; 373 glEnd;
124 } 374 }
125 } 375 }
126 } 376 }
127 377
128 glPopMatrix;
129
130 glDisable GL_TEXTURE_2D; 378 glDisable GL_TEXTURE_2D;
131 glDisable GL_BLEND; 379 glDisable GL_BLEND;
132} 380}
133 381
382my %DIR = (
383 SDLK_KP8, [1, "north"],
384 SDLK_KP9, [2, "northest"],
385 SDLK_KP6, [3, "east"],
386 SDLK_KP3, [4, "southeast"],
387 SDLK_KP2, [5, "south"],
388 SDLK_KP1, [6, "southwest"],
389 SDLK_KP4, [7, "west"],
390 SDLK_KP7, [8, "northwest"],
391);
392
393sub key_down {
394 my ($self, $ev) = @_;
395
396 my $mod = $ev->key_mod;
397 my $sym = $ev->key_sym;
398
399 if ($sym == SDLK_KP5) {
400 $::CONN->send ("command stay fire");
401 } elsif (exists $DIR{$sym}) {
402 if ($mod & KMOD_SHIFT) {
403 $::CONN->send ("command fire $DIR{$sym}[0]");
404 } elsif ($mod & KMOD_CTRL) {
405 $::CONN->send ("command run $DIR{$sym}[0]");
406 } else {
407 }
408 }
409}
410
411sub key_up {
412 my ($self, $ev) = @_;
413
414 my $mod = $ev->key_mod;
415 my $sym = $ev->key_sym;
416
417 if (exists $DIR{$sym}) {
418 if ($mod & KMOD_SHIFT) {
419 $::CONN->send ("command fire_stop");
420 } else {
421 $::CONN->send ("command run_stop");
422 }
423 }
424}
425
1341; 4261;
135 427

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines