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.39 by root, Sun Apr 9 21:39:08 2006 UTC vs.
Revision 1.56 by root, Mon Apr 10 22:16:34 2006 UTC

5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10our $FOCUS; # the widget with current focus 10use Crossfire::Client;
11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
11 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
37 Carp::confess "size_request is abtract"; 92 Carp::confess "size_request is abtract";
38} 93}
39 94
40sub size_allocate { 95sub size_allocate {
41 my ($self, $w, $h) = @_; 96 my ($self, $w, $h) = @_;
97
42 $self->w ($w); 98 $self->{w} = $w;
43 $self->h ($h); 99 $self->{h} = $h;
44} 100}
45 101
46sub focus_in { 102sub focus_in {
47 my ($widget) = @_; 103 my ($self) = @_;
48 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
49} 108}
50 109
51sub focus_out { 110sub focus_out {
52 my ($widget) = @_; 111 my ($self) = @_;
53}
54 112
55sub key_down { 113 return unless $FOCUS == $self;
56 my ($widget, $sdlev) = @_;
57}
58 114
59sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
60 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
61} 117}
62 118
119sub mouse_motion { }
120sub button_up { }
63sub button_down { 121sub button_down { }
64 my ($widget, $sdlev) = @_; 122sub key_down { }
65} 123sub key_up { }
66 124
67sub button_up {
68 my ($widget, $sdlev) = @_;
69}
70
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 130
77sub draw { 131sub draw {
78 my ($self) = @_; 132 my ($self) = @_;
79 133
80 glPushMatrix; 134 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
83 glPopMatrix; 148 glPopMatrix;
84} 149}
85 150
86sub _draw { 151sub _draw {
87 my ($self) = @_; 152 my ($self) = @_;
159 224
160 @{$self->{children}} = 225 @{$self->{children}} =
161 sort { $a->{z} <=> $b->{z} } 226 sort { $a->{z} <=> $b->{z} }
162 @{$self->{children}}, $chld; 227 @{$self->{children}}, $chld;
163 228
164 $self->size_allocate ($self->{w}, $self->{h}); 229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
165} 231}
166 232
167sub remove { 233sub remove {
168 my ($self, $widget) = @_; 234 my ($self, $widget) = @_;
169 235
173} 239}
174 240
175sub find_widget { 241sub find_widget {
176 my ($self, $x, $y) = @_; 242 my ($self, $x, $y) = @_;
177 243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
178 my $res; 247 my $res;
179 248
180 for (@{ $self->{children} }) { 249 for (reverse @{ $self->{children} }) {
181 $res = $_->find_widget ($x, $y) 250 $res = $_->find_widget ($x, $y)
182 and return $res; 251 and return $res;
183 } 252 }
184 253
185 () 254 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
186}
187
188sub size_request {
189 my ($self) = @_;
190
191 my ($hs, $ws) = (0, 0);
192 for (@{$self->{children} || []}) {
193 my ($w, $h) = $_->size_request;
194 $hs += $h;
195 if ($ws < $w) { $ws = $w }
196 }
197
198 return ($ws, $hs);
199} 255}
200 256
201sub _draw { 257sub _draw {
202 my ($self) = @_; 258 my ($self) = @_;
203 259
216 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
217} 273}
218 274
219sub size_allocate { 275sub size_allocate {
220 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
277
221 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
222 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
223 if $self->{children}[0] 280 if $self->{children}[0]
224} 281}
225 282
226############################################################################# 283#############################################################################
227 284
228package Crossfire::Client::Widget::Toplevel; 285package Crossfire::Client::Widget::Window;
229 286
230our @ISA = Crossfire::Client::Widget::Container::; 287our @ISA = Crossfire::Client::Widget::Bin::;
288
289use SDL::OpenGL;
290
291sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_;
293
294 my $self = $class->SUPER::new;
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297}
231 298
232sub update { 299sub update {
233 my ($self) = @_; 300 my ($self) = @_;
234 301
235 ::refresh ();
236}
237
238#############################################################################
239
240package Crossfire::Client::Widget::Window;
241
242our @ISA = Crossfire::Client::Widget::Bin::;
243
244use SDL::OpenGL;
245
246sub add {
247 my ($self, $chld) = @_;
248 warn "ADD $chld\n";
249 $self->SUPER::add ($chld);
250 $chld->set_parent ($self);
251}
252
253sub remove {
254 my ($self) = @_;
255 # TODO FIXME: removing a child from a window will crash, see render_chld
256 # $self->update;
257}
258
259sub update {
260 my ($self) = @_;
261 $self->render_chld; 302 $self->render_chld;
303 $self->SUPER::update;
262} 304}
263 305
264sub render_chld { 306sub render_chld {
265 my ($self) = @_; 307 my ($self) = @_;
266 my $chld = $self->get;
267 my ($w, $h) = $self->size_request;
268 308
269 require Carp;
270 Carp::cluck "RENDERCHI $w $h";
271 warn "RENDERCHI $w $h\n";
272 $self->{texture} = 309 $self->{texture} =
273 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
274 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
275 ); 312 );
276 $self->{texture}->upload;
277}
278
279sub size_request {
280 my ($self) = @_;
281 ($self->w, $self->h)
282} 313}
283 314
284sub size_allocate { 315sub size_allocate {
285 my ($self, $w, $h) = @_; 316 my ($self, $w, $h) = @_;
286 317
287 $self->w ($w); 318 $self->{w} = $w;
288 $self->h ($h); 319 $self->{h} = $h;
320
289 $self->get->size_allocate ($w, $h); 321 $self->child->size_allocate ($w, $h);
290 322
291 $self->update; #TODO: Move this to the size_request event propably? 323 $self->render_chld;
292} 324}
293 325
294sub _draw { 326sub _draw {
295 my ($self) = @_; 327 my ($self) = @_;
296 328
300 or return; 332 or return;
301 333
302 glEnable GL_BLEND; 334 glEnable GL_BLEND;
303 glEnable GL_TEXTURE_2D; 335 glEnable GL_TEXTURE_2D;
304 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
305 glBindTexture GL_TEXTURE_2D, $tex->{name};
306 337
307 glBegin GL_QUADS; 338 $tex->draw_quad (0, 0, $w, $h);
308 glTexCoord 0, 0; glVertex 0, 0;
309 glTexCoord 0, 1; glVertex 0, $h;
310 glTexCoord 1, 1; glVertex $w, $h;
311 glTexCoord 1, 0; glVertex $w, 0;
312 glEnd;
313 339
314 glDisable GL_BLEND; 340 glDisable GL_BLEND;
315 glDisable GL_TEXTURE_2D; 341 glDisable GL_TEXTURE_2D;
316} 342}
317 343
333 map { $_ + 4 } $chld->size_request; 359 map { $_ + 4 } $chld->size_request;
334} 360}
335 361
336sub size_allocate { 362sub size_allocate {
337 my ($self, $w, $h) = @_; 363 my ($self, $w, $h) = @_;
338 364
339 $self->w ($w); 365 $self->{w} = $w;
340 $self->h ($h); 366 $self->{h} = $h;
341 367
342 $self->child->size_allocate ($w - 4, $h - 4); 368 $self->child->size_allocate ($w - 4, $h - 4);
343 $self->child->move (2, 2); 369 $self->child->move (2, 2);
344} 370}
345 371
350 376
351 my ($w, $h) = $chld->size_request; 377 my ($w, $h) = $chld->size_request;
352 378
353 glBegin GL_QUADS; 379 glBegin GL_QUADS;
354 glColor 0, 0, 0; 380 glColor 0, 0, 0;
355 glTexCoord 0, 0; glVertex 0 , 0; 381 glVertex 0 , 0;
356 glTexCoord 0, 1; glVertex 0 , $h + 4; 382 glVertex 0 , $h + 4;
357 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 383 glVertex $w + 4 , $h + 4;
358 glTexCoord 1, 0; glVertex $w + 4 , 0; 384 glVertex $w + 4 , 0;
359 glEnd; 385 glEnd;
360 386
361 $chld->draw; 387 $chld->draw;
362} 388}
363 389
364############################################################################# 390#############################################################################
365 391
366package Crossfire::Client::Widget::FancyFrame; 392package Crossfire::Client::Widget::FancyFrame;
367 393
368our @ISA = Crossfire::Client::Widget::Frame::; 394our @ISA = Crossfire::Client::Widget::Bin::;
369 395
370use SDL::OpenGL; 396use SDL::OpenGL;
371 397
372sub new { 398my @tex =
373 my ($self, $theme) = @_;
374 $self = $self->SUPER::new;
375
376 $self->{txts} = [
377 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 399 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
378 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 400 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
379 ];
380 $self
381}
382 401
383sub size_request { 402sub size_request {
384 my ($self) = @_; 403 my ($self) = @_;
385 404
386 my ($w, $h) = $self->SUPER::size_request; 405 my ($w, $h) = $self->SUPER::size_request;
387 406
388 $h += $self->{txts}->[1]->{height}; 407 $h += $tex[1]->{height};
389 $h += $self->{txts}->[4]->{height}; 408 $h += $tex[4]->{height};
390 $w += $self->{txts}->[2]->{width}; 409 $w += $tex[2]->{width};
391 $w += $self->{txts}->[3]->{width}; 410 $w += $tex[3]->{width};
392 411
393 ($w, $h) 412 ($w, $h)
394} 413}
395 414
396sub size_allocate { 415sub size_allocate {
397 my ($self, $w, $h) = @_; 416 my ($self, $w, $h) = @_;
398 417
399 $self->w ($w); 418 $self->SUPER::size_allocate ($w, $h);
400 $self->h ($h); 419
401 $h -= $self->{txts}->[1]->{height}; 420 $h -= $tex[1]->{height};
402 $h -= $self->{txts}->[4]->{height}; 421 $h -= $tex[4]->{height};
403 $w -= $self->{txts}->[2]->{width}; 422 $w -= $tex[2]->{width};
404 $w -= $self->{txts}->[3]->{width}; 423 $w -= $tex[3]->{width};
405 424
406 $h = $h < 0 ? 0 : $h; 425 $h = $h < 0 ? 0 : $h;
407 $w = $w < 0 ? 0 : $w; 426 $w = $w < 0 ? 0 : $w;
408 warn "CHILD:$w $h\n"; 427
409 $self->child->size_allocate ($w, $h); 428 $self->child->size_allocate ($w, $h);
410 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 429 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
411} 430}
412 431
413sub _draw { 432sub _draw {
414 my ($self) = @_; 433 my ($self) = @_;
415 434
416 my ($w, $h) = ($self->w, $self->h); 435 my ($w, $h) = ($self->{w}, $self->{h});
417 my ($cw, $ch) = ($self->child->w, $self->child->h); 436 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
418 437
419 glEnable GL_BLEND; 438 glEnable GL_BLEND;
420 glEnable GL_TEXTURE_2D; 439 glEnable GL_TEXTURE_2D;
421 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 440 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
422 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 441 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
423 442
424 my $top = $self->{txts}->[1]; 443 my $top = $tex[1];
425 glBindTexture GL_TEXTURE_2D, $top->{name}; 444 $top->draw_quad (0, 0, $w, $top->{height});
426 445
427 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex 0 , 0;
429 glTexCoord 0, 1; glVertex 0 , $top->{height};
430 glTexCoord 1, 1; glVertex $w , $top->{height};
431 glTexCoord 1, 0; glVertex $w , 0;
432 glEnd;
433
434 my $left = $self->{txts}->[3]; 446 my $left = $tex[3];
435 glBindTexture GL_TEXTURE_2D, $left->{name}; 447 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
436 448
437 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex 0 , $top->{height};
439 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
440 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
441 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
442 glEnd;
443
444 my $right = $self->{txts}->[2]; 449 my $right = $tex[2];
445 glBindTexture GL_TEXTURE_2D, $right->{name}; 450 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
446 451
447 glBegin GL_QUADS;
448 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
449 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
450 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
451 glTexCoord 1, 0; glVertex $w , $top->{height};
452 glEnd;
453
454 my $bottom = $self->{txts}->[4]; 452 my $bottom = $tex[4];
455 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 453 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
456 454
457 glBegin GL_QUADS; 455 my $bg = $tex[0];
458 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
459 glTexCoord 0, 1; glVertex 0 , $h;
460 glTexCoord 1, 1; glVertex $w , $h;
461 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
462 glEnd;
463
464 my $bg = $self->{txts}->[0];
465 glBindTexture GL_TEXTURE_2D, $bg->{name}; 456 glBindTexture GL_TEXTURE_2D, $bg->{name};
466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 458 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
469 460
470 my $rep_x = $cw / $bg->{width}; 461 my $rep_x = $cw / $bg->{width};
471 my $rep_y = $ch / $bg->{height}; 462 my $rep_y = $ch / $bg->{height};
472 463
473 glBegin GL_QUADS; 464 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
474 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
475 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
476 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
477 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
478 glEnd;
479 465
480 glDisable GL_BLEND; 466 glDisable GL_BLEND;
481 glDisable GL_TEXTURE_2D; 467 glDisable GL_TEXTURE_2D;
482 468
483 $self->child->draw; 469 $self->child->draw;
576package Crossfire::Client::Widget::VBox; 562package Crossfire::Client::Widget::VBox;
577 563
578our @ISA = Crossfire::Client::Widget::Container::; 564our @ISA = Crossfire::Client::Widget::Container::;
579 565
580use SDL::OpenGL; 566use SDL::OpenGL;
567
568sub size_request {
569 my ($self) = @_;
570
571 my @alloc = map [$_->size_request], @{$self->{children}};
572
573 (
574 (List::Util::max map $_->[0], @alloc),
575 (List::Util::sum map $_->[1], @alloc),
576 )
577}
581 578
582sub size_allocate { 579sub size_allocate {
583 my ($self, $w, $h) = @_; 580 my ($self, $w, $h) = @_;
584 581
585 $self->w ($w); 582 $self->w ($w);
618 $y += $h; 615 $y += $h;
619 } 616 }
620 } 617 }
621} 618}
622 619
623sub _draw {
624 my ($self) = @_;
625
626 my ($x, $y);
627 for (@{$self->{children} || []}) {
628 $_->draw;
629 $y += $_->h;
630 }
631}
632
633############################################################################# 620#############################################################################
634 621
635package Crossfire::Client::Widget::Label; 622package Crossfire::Client::Widget::Label;
636 623
637our @ISA = Crossfire::Client::Widget::; 624our @ISA = Crossfire::Client::Widget::;
639use SDL::OpenGL; 626use SDL::OpenGL;
640 627
641sub new { 628sub new {
642 my ($class, $x, $y, $z, $height, $text) = @_; 629 my ($class, $x, $y, $z, $height, $text) = @_;
643 630
631 $height ||= $::FONTSIZE;
632
644 # TODO: color, and make height, xyz etc. optional 633 # TODO: color, and make height, xyz etc. optional
645 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
646 635
647 $self->set_text ($text); 636 $self->set_text ($text);
648 637
665} 654}
666 655
667sub size_request { 656sub size_request {
668 my ($self) = @_; 657 my ($self) = @_;
669 658
670 ( 659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 (
671 $self->{texture}{width}, 661 $self->{texture}{width},
672 $self->{texture}{height}, 662 $self->{texture}{height},
663 )
664 } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
666
667 ($w, $h)
673 ) 668 }
669
674} 670}
675 671
676sub _draw { 672sub _draw {
677 my ($self) = @_; 673 my ($self) = @_;
678 674
680 676
681 glEnable GL_BLEND; 677 glEnable GL_BLEND;
682 glEnable GL_TEXTURE_2D; 678 glEnable GL_TEXTURE_2D;
683 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
684 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
685 glBindTexture GL_TEXTURE_2D, $tex->{name};
686 681
687 glColor 1, 0, 0, 1; # TODO color 682 glColor 1, 0, 0, 1; # TODO color
688 683
689 glBegin GL_QUADS; 684 $tex->draw_quad (0, 0);
690 glTexCoord 0, 0; glVertex 0 , 0;
691 glTexCoord 0, 1; glVertex 0 , $tex->{height};
692 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
693 glTexCoord 1, 0; glVertex $tex->{width}, 0;
694 glEnd;
695 685
696 glDisable GL_BLEND; 686 glDisable GL_BLEND;
697 glDisable GL_TEXTURE_2D; 687 glDisable GL_TEXTURE_2D;
698} 688}
699 689
700############################################################################# 690#############################################################################
701 691
702package Crossfire::Client::Widget::TextEntry; 692package Crossfire::Client::Widget::Entry;
703 693
704our @ISA = Crossfire::Client::Widget::Label::; 694our @ISA = Crossfire::Client::Widget::Label::;
705 695
706use SDL; 696use SDL;
707use SDL::OpenGL; 697use SDL::OpenGL;
710 my ($self, $ev) = @_; 700 my ($self, $ev) = @_;
711 701
712 my $mod = $ev->key_mod; 702 my $mod = $ev->key_mod;
713 my $sym = $ev->key_sym; 703 my $sym = $ev->key_sym;
714 704
715 $ev->set_unicode (1);
716 my $uni = $ev->key_unicode; 705 my $uni = $ev->key_unicode;
717 706
718 my $text = $self->get_text; 707 my $text = $self->get_text;
719 708
720 if ($sym == SDLK_BACKSPACE) { 709 if ($sym == SDLK_BACKSPACE) {
721 substr $text, -1, 1, ''; 710 substr $text, -1, 1, '';
722
723 } elsif ($uni) { 711 } elsif ($uni) {
724 $text .= chr $uni; 712 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
725 } 714 }
715
726 $self->set_text ($text); 716 $self->set_text ($text);
717}
718
719sub button_down {
720 my ($self, $ev) = @_;
721
722 $self->focus_in;
723}
724
725sub _draw {
726 my ($self) = @_;
727
728 if ($FOCUS == $self) {
729 glColor 1, 1, 1;
730 } else {
731 glColor 0.7, 0.7, 0.7;
732 }
733
734 glBegin GL_QUADS;
735 glVertex 0 , 0;
736 glVertex 0 , $self->{h} - 1;
737 glVertex $self->{w} - 1, $self->{h} - 1;
738 glVertex $self->{w} - 1, 0;
739 glEnd;
740
741 $self->SUPER::_draw;
727} 742}
728 743
729############################################################################# 744#############################################################################
730 745
731package Crossfire::Client::Widget::MapWidget; 746package Crossfire::Client::Widget::MapWidget;
746 761
747sub key_up { 762sub key_up {
748} 763}
749 764
750sub size_request { 765sub size_request {
751 766 (
752} 767 1 + int $::WIDTH / 32,
753 768 1 + int $::HEIGHT / 32,
754sub size_allocate { 769 )
755} 770}
756 771
757sub _draw { 772sub _draw {
758 my ($self) = @_; 773 my ($self) = @_;
759 774
792 my $cell = $map->[$x + $xofs][$y + $yofs] 807 my $cell = $map->[$x + $xofs][$y + $yofs]
793 or next; 808 or next;
794 809
795 my $darkness = $cell->[0] * (1 / 255); 810 my $darkness = $cell->[0] * (1 / 255);
796 if ($darkness < 0) { 811 if ($darkness < 0) {
797 $darkness = 0.15; 812 $darkness = $cell->[1] ? 0.1 : 0;
798 } 813 }
799 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
800 815
801 for my $num (grep $_, @$cell[1,2,3]) { 816 for my $num (grep $_, @$cell[1,2,3]) {
802 my $tex = $::CONN->{face}[$num]{texture} || next; 817 my $tex = $::CONN->{face}[$num]{texture} || next;
803 818
804 glBindTexture GL_TEXTURE_2D, $tex->{name};
805
806 my $w = $tex->{width}; 819 my $w = $tex->{width};
807 my $h = $tex->{height}; 820 my $h = $tex->{height};
808 821
809 my $px = ($x + 1) * 32 - $w; 822 my $px = ($x + 1) * 32 - $w;
810 my $py = ($y + 1) * 32 - $h; 823 my $py = ($y + 1) * 32 - $h;
811 824
812 glBegin GL_QUADS; 825 $tex->draw_quad ($px, $py, $w, $h);
813 glTexCoord 0, 0; glVertex $px , $py;
814 glTexCoord 0, 1; glVertex $px , $py + $h;
815 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
816 glTexCoord 1, 0; glVertex $px + $w, $py;
817 glEnd;
818 } 826 }
819 } 827 }
820 } 828 }
821 829
822# if (1) { # higher quality darkness 830# if (1) { # higher quality darkness
831 839
832 $lighting = new Crossfire::Client::Texture 840 $lighting = new Crossfire::Client::Texture
833 width => $sw4, 841 width => $sw4,
834 height => $sh, 842 height => $sh,
835 data => $lighting, 843 data => $lighting,
836 internalformat => GL_ALPHA4, 844 internalformat => GL_ALPHA,
837 format => GL_ALPHA; 845 format => GL_ALPHA;
838 846
839 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
840 glColor 0, 0, 0, 0.75; 848 glColor 0.45, 0.45, 0.45, 1;
841 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 849 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
842 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 850 glBindTexture GL_TEXTURE_2D, $lighting->{name};
843 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 851 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
844 glBegin GL_QUADS; 852
845 glTexCoord 0, 0; glVertex 0 , 0; 853 $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32);
846 glTexCoord 0, 1; glVertex 0 , $sh * 32;
847 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
848 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
849 glEnd;
850 854
851 glDisable GL_TEXTURE_2D; 855 glDisable GL_TEXTURE_2D;
852 glDisable GL_BLEND; 856 glDisable GL_BLEND;
853} 857}
854 858
913 917
914sub moveto { 918sub moveto {
915 my ($self, $x, $y) = @_; 919 my ($self, $x, $y) = @_;
916 920
917 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
918 $self->{speed} = 0.2; 922 $self->{speed} = 0.001;
919 $self->{time} = 1; 923 $self->{time} = 1;
920 924
921 ::animation_start $self; 925 ::animation_start $self;
922} 926}
923 927
924sub animate { 928sub animate {
925 my ($self, $interval) = @_; 929 my ($self, $interval) = @_;
930
931 printf "%5.2f\n", 1 / $interval;#d#
926 932
927 $self->{time} -= $interval * $self->{speed}; 933 $self->{time} -= $interval * $self->{speed};
928 if ($self->{time} <= 0) { 934 if ($self->{time} <= 0) {
929 $self->{time} = 0; 935 $self->{time} = 0;
930 ::animation_stop $self; 936 ::animation_stop $self;
938 944
939sub _draw { 945sub _draw {
940 my ($self) = @_; 946 my ($self) = @_;
941 947
942 glPushMatrix; 948 glPushMatrix;
943 glRotate $self->{time} * 10000, 0, 1, 0; 949 glRotate $self->{time} * 1000, 0, 1, 0;
944 $self->{children}[0]->draw; 950 $self->{children}[0]->draw;
945 glPopMatrix; 951 glPopMatrix;
946} 952}
947 953
9481; 954#############################################################################
949 955
956package Crossfire::Client::Widget::Toplevel;
957
958our @ISA = Crossfire::Client::Widget::Container::;
959
960sub size_request {
961 ($::WIDTH, $::HEIGHT)
962}
963
964sub size_allocate {
965 my ($self, $w, $h) = @_;
966
967 $self->SUPER::size_allocate ($w, $h);
968
969 $_->size_allocate ($_->size_request)
970 for @{$self->{children}};
971}
972
973sub update {
974 my ($self) = @_;
975
976 $self->size_allocate ($self->size_request);
977 ::refresh ();
978}
979
980sub add {
981 my ($self, $widget) = @_;
982
983 $self->SUPER::add ($widget);
984
985 $widget->size_allocate ($widget->size_request);
986}
987
988sub draw {
989 my ($self) = @_;
990
991 $self->_draw;
992}
993
994#############################################################################
995
996package Crossfire::Client::Widget;
997
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
999
10001
1001

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines