1 | package Crossfire::Client::Widget; |
1 | package CFClient::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
6 | |
6 | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | use Crossfire::Client; |
10 | use CFClient; |
11 | |
11 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
13 | |
14 | our $TOPLEVEL; |
14 | our $TOPLEVEL; |
15 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
… | |
… | |
34 | $GRAB->update if $GRAB; |
34 | $GRAB->update if $GRAB; |
35 | } |
35 | } |
36 | |
36 | |
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
38 | |
38 | |
39 | $GRAB->button_down ($ev) if $GRAB; |
39 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
40 | } |
40 | } |
41 | |
41 | |
42 | sub feed_sdl_button_up_event { |
42 | sub feed_sdl_button_up_event { |
43 | my ($ev) = @_; |
43 | my ($ev) = @_; |
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
45 | |
45 | |
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
47 | |
47 | |
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
49 | |
|
|
50 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
49 | |
51 | |
50 | if (!$BUTTON_STATE) { |
52 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
53 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
54 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
55 | $GRAB->update if $GRAB; |
… | |
… | |
65 | |
67 | |
66 | $hover->update if $hover; |
68 | $hover->update if $hover; |
67 | $HOVER->update if $HOVER; |
69 | $HOVER->update if $HOVER; |
68 | } |
70 | } |
69 | |
71 | |
70 | $HOVER->mouse_motion ($ev) if $HOVER; |
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
71 | } |
73 | } |
72 | |
74 | |
73 | sub new { |
75 | sub new { |
74 | my $class = shift; |
76 | my $class = shift; |
75 | |
77 | |
76 | bless { @_ }, $class |
78 | bless { |
|
|
79 | x => 0, |
|
|
80 | y => 0, |
|
|
81 | z => 0, |
|
|
82 | @_ |
|
|
83 | }, $class |
77 | } |
84 | } |
78 | |
85 | |
79 | sub move { |
86 | sub move { |
80 | my ($self, $x, $y, $z) = @_; |
87 | my ($self, $x, $y, $z) = @_; |
81 | $self->{x} = $x; |
88 | $self->{x} = $x; |
… | |
… | |
95 | sub size_allocate { |
102 | sub size_allocate { |
96 | my ($self, $w, $h) = @_; |
103 | my ($self, $w, $h) = @_; |
97 | |
104 | |
98 | $self->{w} = $w; |
105 | $self->{w} = $w; |
99 | $self->{h} = $h; |
106 | $self->{h} = $h; |
|
|
107 | } |
|
|
108 | |
|
|
109 | # translate global koordinates to local coordinate system |
|
|
110 | sub translate { |
|
|
111 | my ($self, $x, $y) = @_; |
|
|
112 | |
|
|
113 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
100 | } |
114 | } |
101 | |
115 | |
102 | sub focus_in { |
116 | sub focus_in { |
103 | my ($self) = @_; |
117 | my ($self) = @_; |
104 | |
118 | |
… | |
… | |
201 | #$self->deactivate; |
215 | #$self->deactivate; |
202 | } |
216 | } |
203 | |
217 | |
204 | ############################################################################# |
218 | ############################################################################# |
205 | |
219 | |
|
|
220 | package CFClient::Widget::Empty; |
|
|
221 | |
|
|
222 | our @ISA = CFClient::Widget::; |
|
|
223 | |
|
|
224 | sub size_request { |
|
|
225 | (0, 0) |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub draw { |
|
|
229 | } |
|
|
230 | |
|
|
231 | ############################################################################# |
|
|
232 | |
206 | package Crossfire::Client::Widget::Container; |
233 | package CFClient::Widget::Container; |
207 | |
234 | |
208 | our @ISA = Crossfire::Client::Widget::; |
235 | our @ISA = CFClient::Widget::; |
209 | |
236 | |
210 | sub new { |
237 | sub new { |
211 | my ($class, @widgets) = @_; |
238 | my ($class, %arg) = @_; |
212 | |
239 | |
|
|
240 | my $children = delete $arg{children} || []; |
|
|
241 | |
213 | my $self = $class->SUPER::new (children => []); |
242 | my $self = $class->SUPER::new (children => [], %arg); |
214 | $self->add ($_) for @widgets; |
243 | $self->add ($_) for @$children; |
215 | |
244 | |
216 | $self |
245 | $self |
217 | } |
246 | } |
218 | |
247 | |
219 | sub add { |
248 | sub add { |
220 | my ($self, $chld, $expand) = @_; |
249 | my ($self, $chld, $expand) = @_; |
221 | |
250 | |
222 | $chld->{expand} = $expand; |
251 | $chld->{expand} = $expand; |
223 | $chld->set_parent ($self); |
252 | $chld->set_parent ($self); |
224 | |
253 | |
225 | @{$self->{children}} = |
254 | $self->{children} = [ |
226 | sort { $a->{z} <=> $b->{z} } |
255 | sort { $a->{z} <=> $b->{z} } |
227 | @{$self->{children}}, $chld; |
256 | @{$self->{children}}, $chld |
|
|
257 | ]; |
228 | |
258 | |
229 | $self->size_allocate ($self->{w}, $self->{h}) |
259 | $self->size_allocate ($self->{w}, $self->{h}) |
230 | if $self->{w}; #TODO: check for "realised state" |
260 | if $self->{w}; #TODO: check for "realised state" |
231 | } |
261 | } |
232 | |
262 | |
… | |
… | |
260 | $_->draw for @{$self->{children}}; |
290 | $_->draw for @{$self->{children}}; |
261 | } |
291 | } |
262 | |
292 | |
263 | ############################################################################# |
293 | ############################################################################# |
264 | |
294 | |
265 | package Crossfire::Client::Widget::Bin; |
295 | package CFClient::Widget::Bin; |
266 | |
296 | |
267 | our @ISA = Crossfire::Client::Widget::Container::; |
297 | our @ISA = CFClient::Widget::Container::; |
|
|
298 | |
|
|
299 | sub new { |
|
|
300 | my ($class, %arg) = @_; |
|
|
301 | |
|
|
302 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
|
|
303 | |
|
|
304 | $class->SUPER::new (children => [$child], %arg) |
|
|
305 | } |
|
|
306 | |
|
|
307 | sub add { |
|
|
308 | my ($self, $widget) = @_; |
|
|
309 | |
|
|
310 | $self->{children} = []; |
|
|
311 | |
|
|
312 | $self->SUPER::add ($widget); |
|
|
313 | } |
|
|
314 | |
|
|
315 | sub remove { |
|
|
316 | my ($self, $widget) = @_; |
|
|
317 | |
|
|
318 | $self->SUPER::remove ($widget); |
|
|
319 | |
|
|
320 | $self->{children} = [new CFClient::Widget::Empty] |
|
|
321 | unless @{$self->{children}}; |
|
|
322 | } |
268 | |
323 | |
269 | sub child { $_[0]->{children}[0] } |
324 | sub child { $_[0]->{children}[0] } |
270 | |
325 | |
271 | sub size_request { |
326 | sub size_request { |
272 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
327 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
… | |
… | |
280 | if $self->{children}[0] |
335 | if $self->{children}[0] |
281 | } |
336 | } |
282 | |
337 | |
283 | ############################################################################# |
338 | ############################################################################# |
284 | |
339 | |
285 | package Crossfire::Client::Widget::Window; |
340 | package CFClient::Widget::Window; |
286 | |
341 | |
287 | our @ISA = Crossfire::Client::Widget::Bin::; |
342 | our @ISA = CFClient::Widget::Bin::; |
288 | |
343 | |
289 | use SDL::OpenGL; |
344 | use SDL::OpenGL; |
290 | |
345 | |
291 | sub new { |
346 | sub new { |
292 | my ($class, $x, $y, $z, $w, $h) = @_; |
347 | my ($class, %arg) = @_; |
293 | |
348 | |
294 | my $self = $class->SUPER::new; |
349 | my $self = $class->SUPER::new (%arg); |
295 | |
|
|
296 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
297 | } |
350 | } |
298 | |
351 | |
299 | sub update { |
352 | sub update { |
300 | my ($self) = @_; |
353 | my ($self) = @_; |
301 | |
354 | |
|
|
355 | # we want to do this delayed... |
302 | $self->render_chld; |
356 | $self->render_chld; |
303 | $self->SUPER::update; |
357 | $self->SUPER::update; |
304 | } |
358 | } |
305 | |
359 | |
306 | sub render_chld { |
360 | sub render_chld { |
307 | my ($self) = @_; |
361 | my ($self) = @_; |
308 | |
362 | |
309 | $self->{texture} = |
363 | $self->{texture} = |
310 | Crossfire::Client::Texture->new_from_opengl ( |
364 | CFClient::Texture->new_from_opengl ( |
311 | $self->{w}, $self->{h}, sub { $self->child->draw } |
365 | $self->{w}, $self->{h}, sub { $self->child->draw } |
312 | ); |
366 | ); |
313 | } |
367 | } |
314 | |
368 | |
315 | sub size_allocate { |
369 | sub size_allocate { |
… | |
… | |
332 | or return; |
386 | or return; |
333 | |
387 | |
334 | glEnable GL_BLEND; |
388 | glEnable GL_BLEND; |
335 | glEnable GL_TEXTURE_2D; |
389 | glEnable GL_TEXTURE_2D; |
336 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
390 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
337 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
338 | |
391 | |
339 | glBegin GL_QUADS; |
392 | $tex->draw_quad (0, 0, $w, $h); |
340 | glTexCoord 0, 0; glVertex 0, 0; |
|
|
341 | glTexCoord 0, 1; glVertex 0, $h; |
|
|
342 | glTexCoord 1, 1; glVertex $w, $h; |
|
|
343 | glTexCoord 1, 0; glVertex $w, 0; |
|
|
344 | glEnd; |
|
|
345 | |
393 | |
346 | glDisable GL_BLEND; |
394 | glDisable GL_BLEND; |
347 | glDisable GL_TEXTURE_2D; |
395 | glDisable GL_TEXTURE_2D; |
348 | } |
396 | } |
349 | |
397 | |
350 | ############################################################################# |
398 | ############################################################################# |
351 | |
399 | |
352 | package Crossfire::Client::Widget::Frame; |
400 | package CFClient::Widget::Frame; |
353 | |
401 | |
354 | our @ISA = Crossfire::Client::Widget::Bin::; |
402 | our @ISA = CFClient::Widget::Bin::; |
355 | |
403 | |
356 | use SDL::OpenGL; |
404 | use SDL::OpenGL; |
357 | |
405 | |
358 | sub size_request { |
406 | sub size_request { |
359 | my ($self) = @_; |
407 | my ($self) = @_; |
… | |
… | |
382 | |
430 | |
383 | my ($w, $h) = $chld->size_request; |
431 | my ($w, $h) = $chld->size_request; |
384 | |
432 | |
385 | glBegin GL_QUADS; |
433 | glBegin GL_QUADS; |
386 | glColor 0, 0, 0; |
434 | glColor 0, 0, 0; |
387 | glTexCoord 0, 0; glVertex 0 , 0; |
435 | glVertex 0 , 0; |
388 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
436 | glVertex 0 , $h + 4; |
389 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
437 | glVertex $w + 4 , $h + 4; |
390 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
438 | glVertex $w + 4 , 0; |
391 | glEnd; |
439 | glEnd; |
392 | |
440 | |
393 | $chld->draw; |
441 | $chld->draw; |
394 | } |
442 | } |
395 | |
443 | |
396 | ############################################################################# |
444 | ############################################################################# |
397 | |
445 | |
398 | package Crossfire::Client::Widget::FancyFrame; |
446 | package CFClient::Widget::FancyFrame; |
399 | |
447 | |
400 | our @ISA = Crossfire::Client::Widget::Bin::; |
448 | our @ISA = CFClient::Widget::Bin::; |
401 | |
449 | |
402 | use SDL::OpenGL; |
450 | use SDL::OpenGL; |
403 | |
451 | |
404 | my @tex = |
452 | my @tex = |
405 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
453 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
406 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
454 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
407 | |
455 | |
408 | sub size_request { |
456 | sub size_request { |
409 | my ($self) = @_; |
457 | my ($self) = @_; |
410 | |
458 | |
… | |
… | |
429 | $w -= $tex[3]->{width}; |
477 | $w -= $tex[3]->{width}; |
430 | |
478 | |
431 | $h = $h < 0 ? 0 : $h; |
479 | $h = $h < 0 ? 0 : $h; |
432 | $w = $w < 0 ? 0 : $w; |
480 | $w = $w < 0 ? 0 : $w; |
433 | |
481 | |
|
|
482 | my $child = $self->child; |
|
|
483 | |
434 | $self->child->size_allocate ($w, $h); |
484 | $child->size_allocate ($w, $h); |
435 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
485 | $child->move ($tex[3]->{width}, $tex[1]->{height}); |
436 | } |
486 | } |
437 | |
487 | |
438 | sub _draw { |
488 | sub _draw { |
439 | my ($self) = @_; |
489 | my ($self) = @_; |
440 | |
490 | |
… | |
… | |
445 | glEnable GL_TEXTURE_2D; |
495 | glEnable GL_TEXTURE_2D; |
446 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
496 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
447 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
497 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
448 | |
498 | |
449 | my $top = $tex[1]; |
499 | my $top = $tex[1]; |
450 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
500 | $top->draw_quad (0, 0, $w, $top->{height}); |
451 | |
|
|
452 | glBegin GL_QUADS; |
|
|
453 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
454 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
455 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
456 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
457 | glEnd; |
|
|
458 | |
501 | |
459 | my $left = $tex[3]; |
502 | my $left = $tex[3]; |
460 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
503 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
461 | |
|
|
462 | glBegin GL_QUADS; |
|
|
463 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
464 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
465 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
466 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
467 | glEnd; |
|
|
468 | |
504 | |
469 | my $right = $tex[2]; |
505 | my $right = $tex[2]; |
470 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
506 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
471 | |
|
|
472 | glBegin GL_QUADS; |
|
|
473 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
474 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
475 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
476 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
477 | glEnd; |
|
|
478 | |
507 | |
479 | my $bottom = $tex[4]; |
508 | my $bottom = $tex[4]; |
480 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
509 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
481 | |
|
|
482 | glBegin GL_QUADS; |
|
|
483 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
484 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
485 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
486 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
487 | glEnd; |
|
|
488 | |
510 | |
489 | my $bg = $tex[0]; |
511 | my $bg = $tex[0]; |
490 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
512 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
491 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
513 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
492 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
514 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
493 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
515 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
494 | |
516 | |
495 | my $rep_x = $cw / $bg->{width}; |
517 | my $rep_x = $cw / $bg->{width}; |
496 | my $rep_y = $ch / $bg->{height}; |
518 | my $rep_y = $ch / $bg->{height}; |
497 | |
519 | |
498 | glBegin GL_QUADS; |
520 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
499 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
500 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
501 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
502 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
503 | glEnd; |
|
|
504 | |
521 | |
505 | glDisable GL_BLEND; |
522 | glDisable GL_BLEND; |
506 | glDisable GL_TEXTURE_2D; |
523 | glDisable GL_TEXTURE_2D; |
507 | |
524 | |
508 | $self->child->draw; |
525 | $self->child->draw; |
509 | |
526 | |
510 | } |
527 | } |
511 | |
528 | |
512 | ############################################################################# |
529 | ############################################################################# |
513 | |
530 | |
514 | package Crossfire::Client::Widget::Table; |
531 | package CFClient::Widget::Table; |
515 | |
532 | |
516 | our @ISA = Crossfire::Client::Widget::Bin::; |
533 | our @ISA = CFClient::Widget::Bin::; |
517 | |
534 | |
518 | use SDL::OpenGL; |
535 | use SDL::OpenGL; |
519 | |
536 | |
520 | sub add { |
537 | sub add { |
521 | my ($self, $x, $y, $chld) = @_; |
538 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
596 | } |
613 | } |
597 | } |
614 | } |
598 | |
615 | |
599 | ############################################################################# |
616 | ############################################################################# |
600 | |
617 | |
601 | package Crossfire::Client::Widget::VBox; |
618 | package CFClient::Widget::VBox; |
602 | |
619 | |
603 | our @ISA = Crossfire::Client::Widget::Container::; |
620 | our @ISA = CFClient::Widget::Container::; |
604 | |
621 | |
605 | use SDL::OpenGL; |
622 | use SDL::OpenGL; |
606 | |
623 | |
607 | sub size_request { |
624 | sub size_request { |
608 | my ($self) = @_; |
625 | my ($self) = @_; |
… | |
… | |
656 | } |
673 | } |
657 | } |
674 | } |
658 | |
675 | |
659 | ############################################################################# |
676 | ############################################################################# |
660 | |
677 | |
661 | package Crossfire::Client::Widget::Label; |
678 | package CFClient::Widget::Label; |
662 | |
679 | |
663 | our @ISA = Crossfire::Client::Widget::; |
680 | our @ISA = CFClient::Widget::; |
664 | |
681 | |
665 | use SDL::OpenGL; |
682 | use SDL::OpenGL; |
666 | |
683 | |
667 | sub new { |
684 | sub new { |
668 | my ($class, $x, $y, $z, $height, $text) = @_; |
685 | my ($class, %arg) = @_; |
669 | |
|
|
670 | $height ||= $::FONTSIZE; |
|
|
671 | |
686 | |
672 | # TODO: color, and make height, xyz etc. optional |
687 | # TODO: color, and make height, xyz etc. optional |
673 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
688 | my $self = $class->SUPER::new ( |
|
|
689 | color => [1, 1, 1], |
|
|
690 | height => $::FONTSIZE, |
|
|
691 | text => "", |
|
|
692 | layout => new CFClient::Layout, |
|
|
693 | %arg |
|
|
694 | ); |
674 | |
695 | |
675 | $self->set_text ($text); |
696 | $self->set_text ($self->{text}); |
676 | |
697 | |
677 | $self |
698 | $self |
678 | } |
699 | } |
679 | |
700 | |
680 | sub set_text { |
701 | sub set_text { |
681 | my ($self, $text) = @_; |
702 | my ($self, $text) = @_; |
682 | |
703 | |
683 | $self->{text} = $text; |
704 | $self->{text} = $text; |
684 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
705 | $self->{layout}->set_markup ($text); |
685 | |
706 | |
686 | $self->update; |
707 | delete $self->{texture}; |
687 | } |
708 | } |
688 | |
709 | |
689 | sub get_text { |
710 | sub get_text { |
690 | my ($self, $text) = @_; |
711 | my ($self, $text) = @_; |
691 | |
712 | |
… | |
… | |
693 | } |
714 | } |
694 | |
715 | |
695 | sub size_request { |
716 | sub size_request { |
696 | my ($self) = @_; |
717 | my ($self) = @_; |
697 | |
718 | |
|
|
719 | $self->{layout}->set_width; |
|
|
720 | $self->{layout}->set_height ($self->{height}); |
|
|
721 | $self->{layout}->size |
698 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
722 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
699 | ( |
723 | # ( |
700 | $self->{texture}{width}, |
724 | # $self->{texture}{width}, |
701 | $self->{texture}{height}, |
725 | # $self->{texture}{height}, |
702 | ) |
726 | # ) |
703 | } else { |
727 | # } else { |
704 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
728 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
705 | |
729 | # |
706 | ($w, $h) |
730 | # ($w, $h) |
707 | } |
731 | # } |
|
|
732 | } |
708 | |
733 | |
|
|
734 | sub size_allocate { |
|
|
735 | my ($self, $w, $h) = @_; |
|
|
736 | |
|
|
737 | $self->SUPER::size_allocate ($w, $h); |
|
|
738 | delete $self->{texture}; |
709 | } |
739 | } |
710 | |
740 | |
711 | sub _draw { |
741 | sub _draw { |
712 | my ($self) = @_; |
742 | my ($self) = @_; |
713 | |
743 | |
714 | my $tex = $self->{texture}; |
744 | my $tex = $self->{texture} ||= do { |
|
|
745 | $self->{layout}->set_width ($self->{w}); |
|
|
746 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
747 | }; |
715 | |
748 | |
716 | glEnable GL_BLEND; |
749 | glEnable GL_BLEND; |
717 | glEnable GL_TEXTURE_2D; |
750 | glEnable GL_TEXTURE_2D; |
718 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
751 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
719 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
752 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
720 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
721 | |
753 | |
722 | glColor 1, 0, 0, 1; # TODO color |
754 | glColor @{$self->{color}}; |
723 | |
755 | |
724 | glBegin GL_QUADS; |
756 | $tex->draw_quad (0, 0); |
725 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
726 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
|
|
727 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
|
|
728 | glTexCoord 1, 0; glVertex $tex->{width}, 0; |
|
|
729 | glEnd; |
|
|
730 | |
757 | |
731 | glDisable GL_BLEND; |
758 | glDisable GL_BLEND; |
732 | glDisable GL_TEXTURE_2D; |
759 | glDisable GL_TEXTURE_2D; |
733 | } |
760 | } |
734 | |
761 | |
735 | ############################################################################# |
762 | ############################################################################# |
736 | |
763 | |
737 | package Crossfire::Client::Widget::Entry; |
764 | package CFClient::Widget::Entry; |
738 | |
765 | |
739 | our @ISA = Crossfire::Client::Widget::Label::; |
766 | our @ISA = CFClient::Widget::Label::; |
740 | |
767 | |
741 | use SDL; |
768 | use SDL; |
742 | use SDL::OpenGL; |
769 | use SDL::OpenGL; |
743 | |
770 | |
744 | sub key_down { |
771 | sub key_down { |
… | |
… | |
753 | |
780 | |
754 | if ($sym == SDLK_BACKSPACE) { |
781 | if ($sym == SDLK_BACKSPACE) { |
755 | substr $text, -1, 1, ''; |
782 | substr $text, -1, 1, ''; |
756 | } elsif ($uni) { |
783 | } elsif ($uni) { |
757 | $text .= chr $uni; |
784 | $text .= chr $uni; |
758 | print "$uni <$text>\n";#d# |
|
|
759 | } |
785 | } |
760 | |
786 | |
761 | $self->set_text ($text); |
787 | $self->set_text ($text); |
762 | } |
788 | } |
763 | |
789 | |
764 | sub button_down { |
790 | sub button_down { |
765 | my ($self, $ev) = @_; |
791 | my ($self, $ev) = @_; |
766 | |
792 | |
767 | $self->focus_in; |
793 | $self->focus_in; |
|
|
794 | } |
|
|
795 | |
|
|
796 | sub mouse_motion { |
|
|
797 | my ($self, $ev, $x, $y) = @_; |
|
|
798 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
768 | } |
799 | } |
769 | |
800 | |
770 | sub _draw { |
801 | sub _draw { |
771 | my ($self) = @_; |
802 | my ($self) = @_; |
772 | |
803 | |
… | |
… | |
786 | $self->SUPER::_draw; |
817 | $self->SUPER::_draw; |
787 | } |
818 | } |
788 | |
819 | |
789 | ############################################################################# |
820 | ############################################################################# |
790 | |
821 | |
791 | package Crossfire::Client::Widget::MapWidget; |
822 | package CFClient::Widget::MapWidget; |
792 | |
823 | |
793 | use strict; |
824 | use strict; |
794 | |
825 | |
795 | use List::Util qw(min max); |
826 | use List::Util qw(min max); |
796 | |
827 | |
797 | use SDL; |
828 | use SDL; |
798 | use SDL::OpenGL; |
829 | use SDL::OpenGL; |
799 | use SDL::OpenGL::Constants; |
830 | use SDL::OpenGL::Constants; |
800 | |
831 | |
801 | our @ISA = Crossfire::Client::Widget::; |
832 | our @ISA = CFClient::Widget::; |
|
|
833 | |
|
|
834 | sub new { |
|
|
835 | my $class = shift; |
|
|
836 | |
|
|
837 | $class->SUPER::new ( |
|
|
838 | z => -1, |
|
|
839 | list => (glGenLists 1), |
|
|
840 | @_ |
|
|
841 | ) |
|
|
842 | } |
802 | |
843 | |
803 | sub key_down { |
844 | sub key_down { |
804 | print "MAPKEYDOWN\n"; |
845 | print "MAPKEYDOWN\n"; |
805 | } |
846 | } |
806 | |
847 | |
… | |
… | |
812 | 1 + int $::WIDTH / 32, |
853 | 1 + int $::WIDTH / 32, |
813 | 1 + int $::HEIGHT / 32, |
854 | 1 + int $::HEIGHT / 32, |
814 | ) |
855 | ) |
815 | } |
856 | } |
816 | |
857 | |
|
|
858 | sub update { |
|
|
859 | my ($self) = @_; |
|
|
860 | |
|
|
861 | $self->{need_update} = 1; |
|
|
862 | } |
|
|
863 | |
817 | sub _draw { |
864 | sub _draw { |
818 | my ($self) = @_; |
865 | my ($self) = @_; |
819 | |
866 | |
|
|
867 | if (delete $self->{need_update}) { |
|
|
868 | glNewList $self->{list}, GL_COMPILE; |
|
|
869 | |
820 | my $mx = $::CONN->{mapx}; |
870 | my $mx = $::CONN->{mapx}; |
821 | my $my = $::CONN->{mapy}; |
871 | my $my = $::CONN->{mapy}; |
822 | |
872 | |
823 | my $map = $::CONN->{map}; |
873 | my $map = $::CONN->{map}; |
824 | |
874 | |
825 | my ($xofs, $yofs); |
875 | my ($xofs, $yofs); |
826 | |
876 | |
827 | my $sw = 1 + int $::WIDTH / 32; |
877 | my $sw = 1 + int $::WIDTH / 32; |
828 | my $sh = 1 + int $::HEIGHT / 32; |
878 | my $sh = 1 + int $::HEIGHT / 32; |
829 | |
879 | |
830 | if ($::CONN->{mapw} > $sw) { |
880 | if ($::CONN->{mapw} > $sw) { |
831 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
881 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
832 | } else { |
882 | } else { |
833 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
883 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
834 | } |
884 | } |
835 | |
885 | |
836 | if ($::CONN->{maph} > $sh) { |
886 | if ($::CONN->{maph} > $sh) { |
837 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
887 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
838 | } else { |
888 | } else { |
839 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
889 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
840 | } |
890 | } |
841 | |
891 | |
842 | glEnable GL_TEXTURE_2D; |
892 | glEnable GL_TEXTURE_2D; |
843 | glEnable GL_BLEND; |
893 | glEnable GL_BLEND; |
844 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
894 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
845 | |
895 | |
846 | my $sw4 = ($sw + 3) & ~3; |
896 | my $sw4 = ($sw + 3) & ~3; |
847 | my $lighting = "\x00" x ($sw4 * $sh); |
897 | my $darkness = "\x00" x ($sw4 * $sh); |
848 | |
898 | |
849 | for my $x (0 .. $sw - 1) { |
899 | for my $x (0 .. $sw - 1) { |
|
|
900 | my $row = $map->[$x + $xofs]; |
850 | for my $y (0 .. $sh - 1) { |
901 | for my $y (0 .. $sh - 1) { |
851 | |
902 | |
852 | my $cell = $map->[$x + $xofs][$y + $yofs] |
903 | my $cell = $row->[$y + $yofs] |
853 | or next; |
904 | or next; |
854 | |
905 | |
855 | my $darkness = $cell->[0] * (1 / 255); |
906 | my $dark = $cell->[0]; |
856 | if ($darkness < 0) { |
907 | if ($dark < 0) { |
857 | $darkness = $cell->[1] ? 0.1 : 0; |
908 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
909 | } else { |
|
|
910 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
858 | } |
911 | } |
859 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
860 | |
912 | |
861 | for my $num (grep $_, @$cell[1,2,3]) { |
913 | for my $num (grep $_, @$cell[1,2,3]) { |
862 | my $tex = $::CONN->{face}[$num]{texture} || next; |
914 | my $tex = $::CONN->{face}[$num]{texture} || next; |
863 | |
915 | |
864 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
865 | |
|
|
866 | my $w = $tex->{width}; |
916 | my $w = $tex->{width}; |
867 | my $h = $tex->{height}; |
917 | my $h = $tex->{height}; |
868 | |
918 | |
869 | my $px = ($x + 1) * 32 - $w; |
919 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
870 | my $py = ($y + 1) * 32 - $h; |
|
|
871 | |
|
|
872 | glBegin GL_QUADS; |
|
|
873 | glTexCoord 0, 0; glVertex $px , $py; |
|
|
874 | glTexCoord 0, 1; glVertex $px , $py + $h; |
|
|
875 | glTexCoord 1, 1; glVertex $px + $w, $py + $h; |
|
|
876 | glTexCoord 1, 0; glVertex $px + $w, $py; |
|
|
877 | glEnd; |
920 | } |
878 | } |
921 | } |
879 | } |
922 | } |
880 | } |
|
|
881 | |
923 | |
882 | # if (1) { # higher quality darkness |
924 | # if (1) { # higher quality darkness |
883 | # $lighting =~ s/(.)/$1$1$1/gs; |
925 | # $lighting =~ s/(.)/$1$1$1/gs; |
884 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
926 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
885 | # |
927 | # |
… | |
… | |
887 | # |
929 | # |
888 | # $lighting = $pb->get_pixels; |
930 | # $lighting = $pb->get_pixels; |
889 | # $lighting =~ s/(.)../$1/gs; |
931 | # $lighting =~ s/(.)../$1/gs; |
890 | # } |
932 | # } |
891 | |
933 | |
892 | $lighting = new Crossfire::Client::Texture |
|
|
893 | width => $sw4, |
|
|
894 | height => $sh, |
|
|
895 | data => $lighting, |
|
|
896 | internalformat => GL_ALPHA, |
|
|
897 | format => GL_ALPHA; |
|
|
898 | |
|
|
899 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
934 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
900 | glColor 0.45, 0.45, 0.45, 1; |
|
|
901 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
935 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
902 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
903 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
904 | glBegin GL_QUADS; |
|
|
905 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
906 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
907 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
908 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
909 | glEnd; |
|
|
910 | |
936 | |
|
|
937 | $darkness = new CFClient::Texture |
|
|
938 | width => $sw4, |
|
|
939 | height => $sh, |
|
|
940 | data => $darkness, |
|
|
941 | internalformat => GL_ALPHA, |
|
|
942 | format => GL_ALPHA; |
|
|
943 | |
|
|
944 | glColor 0.45, 0.45, 0.45, 1; |
|
|
945 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
946 | |
911 | glDisable GL_TEXTURE_2D; |
947 | glDisable GL_TEXTURE_2D; |
912 | glDisable GL_BLEND; |
948 | glDisable GL_BLEND; |
|
|
949 | |
|
|
950 | glEndList; |
|
|
951 | } |
|
|
952 | |
|
|
953 | glCallList $self->{list}; |
913 | } |
954 | } |
914 | |
955 | |
915 | my %DIR = ( |
956 | my %DIR = ( |
916 | SDLK_KP8, [1, "north"], |
957 | SDLK_KP8, [1, "north"], |
917 | SDLK_KP9, [2, "northeast"], |
958 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
963 | } |
1004 | } |
964 | } |
1005 | } |
965 | |
1006 | |
966 | ############################################################################# |
1007 | ############################################################################# |
967 | |
1008 | |
968 | package Crossfire::Client::Widget::Animator; |
1009 | package CFClient::Widget::Animator; |
969 | |
1010 | |
970 | use SDL::OpenGL; |
1011 | use SDL::OpenGL; |
971 | |
1012 | |
972 | our @ISA = Crossfire::Client::Widget::Bin::; |
1013 | our @ISA = CFClient::Widget::Bin::; |
973 | |
1014 | |
974 | sub moveto { |
1015 | sub moveto { |
975 | my ($self, $x, $y) = @_; |
1016 | my ($self, $x, $y) = @_; |
976 | |
1017 | |
977 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1018 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
978 | $self->{speed} = 2; |
1019 | $self->{speed} = 0.001; |
979 | $self->{time} = 1; |
1020 | $self->{time} = 1; |
980 | |
1021 | |
981 | ::animation_start $self; |
1022 | ::animation_start $self; |
982 | } |
1023 | } |
983 | |
1024 | |
… | |
… | |
1005 | glPopMatrix; |
1046 | glPopMatrix; |
1006 | } |
1047 | } |
1007 | |
1048 | |
1008 | ############################################################################# |
1049 | ############################################################################# |
1009 | |
1050 | |
1010 | package Crossfire::Client::Widget::Toplevel; |
1051 | package CFClient::Widget::Toplevel; |
1011 | |
1052 | |
1012 | our @ISA = Crossfire::Client::Widget::Container::; |
1053 | our @ISA = CFClient::Widget::Container::; |
1013 | |
1054 | |
1014 | sub size_request { |
1055 | sub size_request { |
1015 | ($::WIDTH, $::HEIGHT) |
1056 | ($::WIDTH, $::HEIGHT) |
1016 | } |
1057 | } |
1017 | |
1058 | |
… | |
… | |
1022 | |
1063 | |
1023 | $_->size_allocate ($_->size_request) |
1064 | $_->size_allocate ($_->size_request) |
1024 | for @{$self->{children}}; |
1065 | for @{$self->{children}}; |
1025 | } |
1066 | } |
1026 | |
1067 | |
|
|
1068 | sub translate { |
|
|
1069 | my ($self, $x, $y) = @_; |
|
|
1070 | |
|
|
1071 | ($x, $y) |
|
|
1072 | } |
|
|
1073 | |
1027 | sub update { |
1074 | sub update { |
1028 | my ($self) = @_; |
1075 | my ($self) = @_; |
1029 | |
1076 | |
1030 | $self->size_allocate ($self->size_request); |
1077 | $self->size_allocate ($self->size_request); |
1031 | ::refresh (); |
1078 | ::refresh (); |
… | |
… | |
1045 | $self->_draw; |
1092 | $self->_draw; |
1046 | } |
1093 | } |
1047 | |
1094 | |
1048 | ############################################################################# |
1095 | ############################################################################# |
1049 | |
1096 | |
1050 | package Crossfire::Client::Widget; |
1097 | package CFClient::Widget; |
1051 | |
1098 | |
1052 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
1099 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1053 | |
1100 | |
1054 | 1 |
1101 | 1 |
1055 | |
1102 | |