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.121 by root, Mon Apr 17 19:21:00 2006 UTC vs.
Revision 1.140 by root, Thu Apr 20 04:20:52 2006 UTC

21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
87 87
88package CFClient::UI::Base; 88package CFClient::UI::Base;
89 89
90use strict; 90use strict;
91 91
92use SDL::OpenGL; 92use CFClient::OpenGL;
93 93
94sub new { 94sub new {
95 my $class = shift; 95 my $class = shift;
96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 w => -1,
102 h => -1,
103 @_ 101 @_
104 }, $class; 102 }, $class;
105 103
106 for (keys %$self) { 104 for (keys %$self) {
107 if (/^connect_(.*)$/) { 105 if (/^connect_(.*)$/) {
110 } 108 }
111 109
112 $self 110 $self
113} 111}
114 112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
127}
128
115sub move { 129sub move {
116 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
117 $self->{x} = int $x; 132 $self->{x} = int $x;
118 $self->{y} = int $y; 133 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
120} 137}
121 138
122sub needs_redraw { 139sub needs_redraw {
123 0 140 0
124} 141}
126sub size_request { 143sub size_request {
127 require Carp; 144 require Carp;
128 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abtract";
129} 146}
130 147
131sub _size_allocate { 148sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
133 150
134 $self->{x} = $x; 151 $self->{x} = $x;
135 $self->{y} = $y; 152 $self->{y} = $y;
136 153
154 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect};
157 }
158
137 return unless $self->{w} != $w || $self->{h} != $h; 159 return unless $self->{w} != $w || $self->{h} != $h;
138 160
139 $self->{w} = $w; 161 $self->{w} = $w;
140 $self->{h} = $h; 162 $self->{h} = $h;
141 163
142 1 164 $self->size_allocate ($w, $h);
165 $self->update;
143} 166}
144 167
145sub size_allocate { 168sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 169 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 170}
150 171
151# return top left coordinates 172# return top left coordinates
152sub _topleft { 173sub _topleft {
153 my ($self, $x, $y) = @_; 174 my ($self, $x, $y) = @_;
225 glPopMatrix; 246 glPopMatrix;
226 247
227 if ($self == $HOVER && $self->{can_hover}) { 248 if ($self == $HOVER && $self->{can_hover}) {
228 my ($x, $y) = @$self{qw(x y)}; 249 my ($x, $y) = @$self{qw(x y)};
229 250
230 glColor 0, 0, 1, 0.2; 251 glColor 1, 0.8, 0.5, 0.2;
231 glEnable GL_BLEND; 252 glEnable GL_BLEND;
232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 253 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
233 glBegin GL_QUADS; 254 glBegin GL_QUADS;
234 glVertex $x , $y; 255 glVertex $x , $y;
235 glVertex $x + $self->{w}, $y; 256 glVertex $x + $self->{w}, $y;
260 my ($self, $parent) = @_; 281 my ($self, $parent) = @_;
261 282
262 Scalar::Util::weaken ($self->{parent} = $parent); 283 Scalar::Util::weaken ($self->{parent} = $parent);
263} 284}
264 285
286sub check_size {
287 my ($self) = @_;
288
289 my ($w, $h) = $self->size_request;
290
291 if ($w != $self->{req_w} || $h != $self->{req_h}) {
292 $self->{req_w} = $w;
293 $self->{req_h} = $h;
294
295 $self->{parent}->check_size
296 if $self->{parent};
297 }
298}
299
265sub update { 300sub update {
266 my ($self) = @_; 301 my ($self) = @_;
267 302
268 $self->{parent}->update 303 $self->{parent}->update
269 if $self->{parent}; 304 if $self->{parent};
270} 305}
271 306
272sub connect { 307sub connect {
273 my ($self, $signal, $cb) = @_; 308 my ($self, $signal, $cb) = @_;
274 309
275 push @{ $self->{cb}{$signal} }, $cb; 310 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 311}
277 312
278sub emit { 313sub emit {
279 my ($self, $signal, @args) = @_; 314 my ($self, $signal, @args) = @_;
280 315
316 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 317 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 318 }
283} 319}
284 320
285sub DESTROY { 321sub DESTROY {
286 my ($self) = @_; 322 my ($self) = @_;
287 323
293package CFClient::UI::DrawBG; 329package CFClient::UI::DrawBG;
294 330
295our @ISA = CFClient::UI::Base::; 331our @ISA = CFClient::UI::Base::;
296 332
297use strict; 333use strict;
298use SDL::OpenGL; 334use CFClient::OpenGL;
299 335
300sub new { 336sub new {
301 my $class = shift; 337 my $class = shift;
302 338
303 # range [value, low, high, page] 339 # range [value, low, high, page]
367 $self->{children} = [ 403 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 404 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 405 @{$self->{children}}, $child
370 ]; 406 ];
371 407
372 $self->{w} = $self->{h} = -1; 408 $child->check_size;
373 $self->update;
374} 409}
375 410
376sub remove { 411sub remove {
377 my ($self, $widget) = @_; 412 my ($self, $child) = @_;
378 413
414 delete $child->{parent};
415
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 416 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
380 417
381 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 418 $self->check_size;
382} 419}
383 420
384sub find_widget { 421sub find_widget {
385 my ($self, $x, $y) = @_; 422 my ($self, $x, $y) = @_;
386 423
416 453
417 $class->SUPER::new (children => [$child], %arg) 454 $class->SUPER::new (children => [$child], %arg)
418} 455}
419 456
420sub add { 457sub add {
421 my ($self, $widget) = @_; 458 my ($self, $child) = @_;
422 459
423 $self->{children} = []; 460 $self->{children} = [];
424 461
425 $self->SUPER::add ($widget); 462 $self->SUPER::add ($child);
426} 463}
427 464
428sub remove { 465sub remove {
429 my ($self, $widget) = @_; 466 my ($self, $widget) = @_;
430 467
439sub size_request { 476sub size_request {
440 $_[0]{children}[0]->size_request 477 $_[0]{children}[0]->size_request
441} 478}
442 479
443sub size_allocate { 480sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 481 my ($self, $w, $h) = @_;
445 482
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 483 $self->{children}[0]->configure (0, 0, $w, $h);
449} 484}
450 485
451############################################################################# 486#############################################################################
452 487
453package CFClient::UI::Window; 488package CFClient::UI::Window;
454 489
455our @ISA = CFClient::UI::Bin::; 490our @ISA = CFClient::UI::Bin::;
456 491
457use SDL::OpenGL; 492use CFClient::OpenGL;
458 493
459sub new { 494sub new {
460 my ($class, %arg) = @_; 495 my ($class, %arg) = @_;
461 496
462 my $self = $class->SUPER::new (%arg); 497 my $self = $class->SUPER::new (%arg);
479 $self->child->draw; 514 $self->child->draw;
480 }; 515 };
481} 516}
482 517
483sub size_allocate { 518sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 519 my ($self, $w, $h) = @_;
485 520
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 521 $self->child->configure (0, 0, $w, $h);
489 522
490 $self->render_chld; 523 $self->render_chld;
491} 524}
492 525
493sub _draw { 526sub _draw {
509 glDisable GL_TEXTURE_2D; 542 glDisable GL_TEXTURE_2D;
510} 543}
511 544
512############################################################################# 545#############################################################################
513 546
547package CFClient::UI::ViewPort;
548
549our @ISA = CFClient::UI::Window::;
550
551sub new { die }
552
553sub size_request {
554 my ($self) = @_;
555
556 @$self{qw(child_w child_h)} = $self->child->size_request;
557 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
558
559 @$self{qw(child_w child_h)}
560}
561
562sub _draw {
563 my ($self) = @_;
564
565 $self->{children}[1]->draw;
566}
567
568
569#############################################################################
570
514package CFClient::UI::Frame; 571package CFClient::UI::Frame;
515 572
516our @ISA = CFClient::UI::Bin::; 573our @ISA = CFClient::UI::Bin::;
517 574
518use SDL::OpenGL; 575use CFClient::OpenGL;
519 576
520sub size_request { 577sub size_request {
521 my ($self) = @_; 578 my ($self) = @_;
522 my $chld = $self->child 579 my $chld = $self->child
523 or return (0, 0); 580 or return (0, 0);
528} 585}
529 586
530sub size_allocate { 587sub size_allocate {
531 my ($self, $x, $y, $w, $h) = @_; 588 my ($self, $x, $y, $w, $h) = @_;
532 589
533 $self->_size_allocate ($x, $y, $w, $h) or return;
534
535 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 590 $self->child->configure (2, 2, $w - 4, $h - 4);
536} 591}
537 592
538sub _draw { 593sub _draw {
539 my ($self) = @_; 594 my ($self) = @_;
540 595
557 612
558package CFClient::UI::FancyFrame; 613package CFClient::UI::FancyFrame;
559 614
560our @ISA = CFClient::UI::Bin::; 615our @ISA = CFClient::UI::Bin::;
561 616
562use SDL::OpenGL; 617use CFClient::OpenGL;
563 618
564my @tex = 619my @tex =
565 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
566 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
567 622
571 # TODO: user_x, user_y, overwrite moveto? 626 # TODO: user_x, user_y, overwrite moveto?
572 627
573 $class->SUPER::new ( 628 $class->SUPER::new (
574 bg => [1, 1, 1, 1], 629 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 630 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 631 border => 0.8,
577 @_ 632 @_
578 ) 633 )
579} 634}
580 635
636sub border {
637 int $_[0]{border} * $::FONTSIZE
638}
639
581sub size_request { 640sub size_request {
582 my ($self) = @_; 641 my ($self) = @_;
583 642
584 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; 643 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
585 644
586 my ($w, $h) = $self->SUPER::size_request; 645 my ($w, $h) = $self->SUPER::size_request;
587 646
588 ( 647 (
589 $w + $self->{border} * 2, 648 $w + $self->border * 2,
590 $h + $self->{border} * 2, 649 $h + $self->border * 2,
591 ) 650 )
592} 651}
593 652
594sub size_allocate { 653sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
596 655
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 656 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 657 $w -= List::Util::max 0, $self->border * 2;
601 658
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 659 $self->child->configure ($self->border, $self->border, $w, $h);
603} 660}
604 661
605sub button_down { 662sub button_down {
606 my ($self, $ev, $x, $y) = @_; 663 my ($self, $ev, $x, $y) = @_;
607 664
665 my $border = $self->border;
666
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 667 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 668 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 669
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 670 my ($ox, $oy) = ($ev->{x}, $ev->{y});
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 671 my ($bw, $bh) = ($self->{w}, $self->{h});
613 672
614 $self->{motion} = sub { 673 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_; 674 my ($ev, $x, $y) = @_;
616 675
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 676 ($x, $y) = ($ev->{x}, $ev->{y});
618 677
619 $self->{user_w} = $bw + $x - $ox; 678 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 679 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 680 $self->check_size;
622 }; 681 };
623 682
624 } elsif ($x >= 0 && $x < $self->{w} 683 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 684 && $y >= 0 && $y < $border) {
626 685
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 686 my ($ox, $oy) = ($ev->{x}, $ev->{y});
628 my ($bx, $by) = ($self->{x}, $self->{y}); 687 my ($bx, $by) = ($self->{x}, $self->{y});
629 688
630 $self->{motion} = sub { 689 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_; 690 my ($ev, $x, $y) = @_;
632 691
633 ($x, $y) = ($ev->motion_x, $ev->motion_y); 692 ($x, $y) = ($ev->{x}, $ev->{y});
634 693
635 $self->move ($bx + $x - $ox, $by + $y - $oy); 694 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update; 695 $self->update;
637 }; 696 };
638 } 697 }
659 glEnable GL_BLEND; 718 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 719 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 720 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 721 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 722
723 my $border = $self->border;
724
664 glColor @{ $self->{border_bg} }; 725 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 726 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 727 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 728 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 729 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 730
670 my $bg = $tex[0]; 731 my $bg = $tex[0];
671 732
672 # TODO: repeat texture not scale 733 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 734 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 737 glColor @{ $self->{bg} };
677 738
678 $bg->{s} = $rep_x; 739 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 740 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 741 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 742 $bg->draw_quad ($border, $border, $cw, $ch);
682 743
683 glDisable GL_TEXTURE_2D; 744 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 745 glDisable GL_BLEND;
685 746
686 $self->child->draw; 747 $self->child->draw;
692 753
693our @ISA = CFClient::UI::Base::; 754our @ISA = CFClient::UI::Base::;
694 755
695use List::Util qw(max sum); 756use List::Util qw(max sum);
696 757
697use SDL::OpenGL; 758use CFClient::OpenGL;
698 759
699sub new { 760sub new {
700 my $class = shift; 761 my $class = shift;
701 762
702 $class->SUPER::new ( 763 $class->SUPER::new (
709 my ($self, $x, $y, $child) = @_; 770 my ($self, $x, $y, $child) = @_;
710 771
711 $child->set_parent ($self); 772 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 773 $self->{children}[$y][$x] = $child;
713 774
714 $self->{w} = $self->{h} = -1; 775 $child->check_size;
715 $self->update;
716} 776}
717 777
718# TODO: move to container class maybe? send childs a signal on removal? 778# TODO: move to container class maybe? send childs a signal on removal?
719sub clear { 779sub clear {
720 my ($self) = @_; 780 my ($self) = @_;
755 (sum @$hs), 815 (sum @$hs),
756 ) 816 )
757} 817}
758 818
759sub size_allocate { 819sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 820 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 821
764 my ($ws, $hs) = $self->get_wh; 822 my ($ws, $hs) = $self->get_wh;
765 823
766 my $req_w = sum @$ws; 824 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 825 my $req_h = sum @$hs;
789 847
790 for my $c (0 .. $#$row) { 848 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 849 my $col_w = $ws->[$c];
792 850
793 if (my $widget = $row->[$c]) { 851 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 852 $widget->configure ($x, $y, $col_w, $row_h);
795 } 853 }
796 854
797 $x += $col_w; 855 $x += $col_w;
798 } 856 }
799 857
844 (List::Util::max map $_->[1], @alloc), 902 (List::Util::max map $_->[1], @alloc),
845 ) 903 )
846} 904}
847 905
848sub size_allocate { 906sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 907 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 908
853 ($h, $w) = ($w, $h); 909 ($h, $w) = ($w, $h);
854 910
855 my $children = $self->{children}; 911 my $children = $self->{children};
856 912
878 934
879 my $y = 0; 935 my $y = 0;
880 for (0 .. $#$children) { 936 for (0 .. $#$children) {
881 my $child = $children->[$_]; 937 my $child = $children->[$_];
882 my $h = $h[$_]; 938 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 939 $child->configure ($y, 0, $h, $w);
884 940
885 $y += $h; 941 $y += $h;
886 } 942 }
943
944 1
887} 945}
888 946
889############################################################################# 947#############################################################################
890 948
891package CFClient::UI::VBox; 949package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 962 (List::Util::sum map $_->[1], @alloc),
905 ) 963 )
906} 964}
907 965
908sub size_allocate { 966sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 967 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 968
913 my $children = $self->{children}; 969 my $children = $self->{children};
914 970
915 my @h = map +($_->size_request)[1], @$children; 971 my @h = map +($_->size_request)[1], @$children;
916 972
934 990
935 my $y = 0; 991 my $y = 0;
936 for (0 .. $#$children) { 992 for (0 .. $#$children) {
937 my $child = $children->[$_]; 993 my $child = $children->[$_];
938 my $h = $h[$_]; 994 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 995 $child->configure (0, $y, $w, $h);
940 996
941 $y += $h; 997 $y += $h;
942 } 998 }
999
1000 1
943} 1001}
944 1002
945############################################################################# 1003#############################################################################
946 1004
947package CFClient::UI::Label; 1005package CFClient::UI::Label;
948 1006
949our @ISA = CFClient::UI::Base::; 1007our @ISA = CFClient::UI::Base::;
950 1008
951use SDL::OpenGL; 1009use CFClient::OpenGL;
952 1010
953sub new { 1011sub new {
954 my ($class, %arg) = @_; 1012 my ($class, %arg) = @_;
955 1013
956 my $self = $class->SUPER::new ( 1014 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1015 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1016 fontsize => 1,
959 text => "", 1017 text => "",
960 align => -1, 1018 align => -1,
961 valign => -1, 1019 valign => -1,
962 padding => 2, 1020 padding => 2,
963 layout => new CFClient::Layout, 1021 layout => new CFClient::Layout,
1002 1060
1003sub size_request { 1061sub size_request {
1004 my ($self) = @_; 1062 my ($self) = @_;
1005 1063
1006 $self->{layout}->set_width; 1064 $self->{layout}->set_width;
1007 $self->{layout}->set_height ($self->{fontsize}); 1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1008 1066
1009 my ($w, $h) = $self->{layout}->size; 1067 my ($w, $h) = $self->{layout}->size;
1010 1068
1011 ( 1069 (
1012 $w + $self->{padding} * 2, 1070 $w + $self->{padding} * 2,
1013 $h + $self->{padding} * 2, 1071 $h + $self->{padding} * 2,
1014 ) 1072 )
1015} 1073}
1016 1074
1017sub size_allocate { 1075sub size_allocate {
1018 my ($self, $x, $y, $w, $h) = @_; 1076 my ($self, $w, $h) = @_;
1019
1020 $self->_size_allocate ($x, $y, $w, $h) or return;
1021 1077
1022 delete $self->{texture}; 1078 delete $self->{texture};
1023}
1024
1025sub update {
1026 my ($self) = @_;
1027
1028 delete $self->{texture};
1029 $self->SUPER::update;
1030} 1079}
1031 1080
1032sub _draw { 1081sub _draw {
1033 my ($self) = @_; 1082 my ($self) = @_;
1034 1083
1035 my $tex = $self->{texture} ||= do { 1084 my $tex = $self->{texture} ||= do {
1036 $self->{layout}->set_width ($self->{w}); 1085 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1086 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1038 new_from_layout CFClient::Texture $self->{layout} 1087 new_from_layout CFClient::Texture $self->{layout}
1039 }; 1088 };
1040 1089
1041 glEnable GL_BLEND; 1090 glEnable GL_BLEND;
1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1091 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1043 glEnable GL_TEXTURE_2D; 1092 glEnable GL_TEXTURE_2D;
1044 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1093 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1045 1094
1046 glColor @{$self->{fg}}; 1095 glColor @{$self->{fg}};
1047 1096
1048 my $x = 1097 $self->{ox} = int (
1049 $self->{align} < 0 ? $self->{padding} 1098 $self->{align} < 0 ? $self->{padding}
1050 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1099 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1051 : ($self->{w} - $tex->{w}) * 0.5; 1100 : ($self->{w} - $tex->{w}) * 0.5
1101 );
1052 1102
1053 my $y = 1103 $self->{oy} = int (
1054 $self->{valign} < 0 ? $self->{padding} 1104 $self->{valign} < 0 ? $self->{padding}
1055 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1105 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1056 : ($self->{h} - $tex->{h}) * 0.5; 1106 : ($self->{h} - $tex->{h}) * 0.5
1107 );
1057 1108
1058 $tex->draw_quad (int $x, int $y); 1109 $tex->draw_quad ($self->{ox}, $self->{oy});
1059 1110
1060 glDisable GL_TEXTURE_2D; 1111 glDisable GL_TEXTURE_2D;
1061 glDisable GL_BLEND; 1112 glDisable GL_BLEND;
1062} 1113}
1063 1114
1065 1116
1066package CFClient::UI::EntryBase; 1117package CFClient::UI::EntryBase;
1067 1118
1068our @ISA = CFClient::UI::Label::; 1119our @ISA = CFClient::UI::Label::;
1069 1120
1070use SDL; 1121use CFClient::OpenGL;
1071use SDL::OpenGL;
1072 1122
1073sub new { 1123sub new {
1074 my $class = shift; 1124 my $class = shift;
1075 1125
1076 $class->SUPER::new ( 1126 $class->SUPER::new (
1090 1140
1091 delete $self->{cur_h}; 1141 delete $self->{cur_h};
1092 1142
1093 return if $self->{text} eq $text; 1143 return if $self->{text} eq $text;
1094 1144
1145 delete $self->{texture};
1146
1095 $self->{last_activity} = $::NOW; 1147 $self->{last_activity} = $::NOW;
1096 $self->{text} = $text; 1148 $self->{text} = $text;
1097 1149
1098 $text =~ s/./*/g if $self->{hidden}; 1150 $text =~ s/./*/g if $self->{hidden};
1099 $self->{layout}->set_text ("$text "); 1151 $self->{layout}->set_text ("$text ");
1112 1164
1113 ($w + 1, $h) # add 1 for cursor 1165 ($w + 1, $h) # add 1 for cursor
1114} 1166}
1115 1167
1116sub size_allocate { 1168sub size_allocate {
1117 my ($self, $x, $y, $w, $h) = @_; 1169 my ($self, $w, $h) = @_;
1118
1119 $self->SUPER::size_allocate ($x, $y, $w, $h);
1120 1170
1121 $self->_set_text ($self->{text}); 1171 $self->_set_text ($self->{text});
1122} 1172}
1123 1173
1124sub set_text { 1174sub set_text {
1130} 1180}
1131 1181
1132sub key_down { 1182sub key_down {
1133 my ($self, $ev) = @_; 1183 my ($self, $ev) = @_;
1134 1184
1135 my $mod = $ev->key_mod; 1185 my $mod = $ev->{mod};
1136 my $sym = $ev->key_sym; 1186 my $sym = $ev->{sym};
1137
1138 my $uni = $ev->key_unicode; 1187 my $uni = $ev->{unicode};
1139 1188
1140 my $text = $self->get_text; 1189 my $text = $self->get_text;
1141 1190
1142 if ($sym == SDLK_BACKSPACE) { 1191 if ($sym == 8) {
1143 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1192 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1144 } elsif ($sym == SDLK_DELETE) { 1193 } elsif ($sym == 127) {
1145 substr $text, $self->{cursor}, 1, ""; 1194 substr $text, $self->{cursor}, 1, "";
1146 } elsif ($sym == SDLK_LEFT) { 1195 } elsif ($sym == CFClient::SDLK_LEFT) {
1147 --$self->{cursor} if $self->{cursor}; 1196 --$self->{cursor} if $self->{cursor};
1148 } elsif ($sym == SDLK_RIGHT) { 1197 } elsif ($sym == CFClient::SDLK_RIGHT) {
1149 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1198 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1150 } elsif ($sym == SDLK_HOME) { 1199 } elsif ($sym == CFClient::SDLK_HOME) {
1151 $self->{cursor} = 0; 1200 $self->{cursor} = 0;
1152 } elsif ($sym == SDLK_END) { 1201 } elsif ($sym == CFClient::SDLK_END) {
1153 $self->{cursor} = length $text; 1202 $self->{cursor} = length $text;
1154 } elsif ($sym == SDLK_ESCAPE) { 1203 } elsif ($sym == 27) {
1155 $self->emit ('escape'); 1204 $self->emit ('escape');
1156 } elsif ($uni) { 1205 } elsif ($uni) {
1157 substr $text, $self->{cursor}++, 0, chr $uni; 1206 substr $text, $self->{cursor}++, 0, chr $uni;
1158 } 1207 }
1159 1208
1224 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 1273 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1225 } 1274 }
1226 1275
1227 glColor @{$self->{fg}}; 1276 glColor @{$self->{fg}};
1228 glBegin GL_LINES; 1277 glBegin GL_LINES;
1229 glVertex $self->{cur_x}, $self->{cur_y};
1230 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1278 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1279 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1231 glEnd; 1280 glEnd;
1232 } 1281 }
1233} 1282}
1234 1283
1235package CFClient::UI::Entry; 1284package CFClient::UI::Entry;
1236 1285
1237our @ISA = CFClient::UI::EntryBase::; 1286our @ISA = CFClient::UI::EntryBase::;
1238 1287
1239use SDL; 1288use CFClient::OpenGL;
1240use SDL::OpenGL;
1241 1289
1242sub key_down { 1290sub key_down {
1243 my ($self, $ev) = @_; 1291 my ($self, $ev) = @_;
1244 1292
1245 my $sym = $ev->key_sym; 1293 my $sym = $ev->{sym};
1246 1294
1247 if ($sym == SDLK_RETURN) { 1295 if ($sym == 13) {
1248 $self->emit (activate => $self->get_text); 1296 $self->emit (activate => $self->get_text);
1249 $self->update; 1297 $self->update;
1250 1298
1251 } else { 1299 } else {
1252 $self->SUPER::key_down ($ev); 1300 $self->SUPER::key_down ($ev);
1258 1306
1259package CFClient::UI::Button; 1307package CFClient::UI::Button;
1260 1308
1261our @ISA = CFClient::UI::Label::; 1309our @ISA = CFClient::UI::Label::;
1262 1310
1263use SDL; 1311use CFClient::OpenGL;
1264use SDL::OpenGL;
1265 1312
1266my @tex = 1313my @tex =
1267 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1268 qw(b1_button_active.png); 1315 qw(b1_button_active.png);
1269 1316
1272 1319
1273 $class->SUPER::new ( 1320 $class->SUPER::new (
1274 padding => 4, 1321 padding => 4,
1275 fg => [1, 1, 1], 1322 fg => [1, 1, 1],
1276 bg => [1, 1, 1, 0.2], 1323 bg => [1, 1, 1, 0.2],
1277 active_fg => [1, 1, 0], 1324 active_fg => [0, 0, 1],
1278 can_hover => 1, 1325 can_hover => 1,
1326 align => 0,
1327 valign => 0,
1279 @_ 1328 @_
1280 ) 1329 )
1281} 1330}
1282 1331
1283sub button_up { 1332sub button_up {
1320 1369
1321my @tex = 1370my @tex =
1322 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1323 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1372 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1324 1373
1325use SDL; 1374use CFClient::OpenGL;
1326use SDL::OpenGL;
1327 1375
1328sub new { 1376sub new {
1329 my $class = shift; 1377 my $class = shift;
1330 1378
1331 $class->SUPER::new ( 1379 $class->SUPER::new (
1342 my ($self) = @_; 1390 my ($self) = @_;
1343 1391
1344 ($self->{padding} * 2 + 6) x 2 1392 ($self->{padding} * 2 + 6) x 2
1345} 1393}
1346 1394
1347sub size_allocate {
1348 my ($self, $x, $y, $w, $h) = @_;
1349
1350 $self->_size_allocate ($x, $y, $w, $h);
1351}
1352
1353sub button_down { 1395sub button_down {
1354 my ($self, $ev, $x, $y) = @_; 1396 my ($self, $ev, $x, $y) = @_;
1355 1397
1356 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1398 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1357 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1399 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1383 glDisable GL_BLEND; 1425 glDisable GL_BLEND;
1384} 1426}
1385 1427
1386############################################################################# 1428#############################################################################
1387 1429
1430package CFClient::UI::VGauge;
1431
1432our @ISA = CFClient::UI::Base::;
1433
1434use CFClient::OpenGL;
1435
1436my %tex = (
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new {
1457 my $class = shift;
1458
1459 my $self = $class->SUPER::new (
1460 gauge => 'food',
1461 @_
1462 );
1463
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h};
1465
1466 $self
1467}
1468
1469sub size_request {
1470 my ($self) = @_;
1471
1472 my $tex = $tex{$self->{gauge}}[0];
1473
1474 @$tex{qw(w h)}
1475}
1476
1477sub set_max {
1478 my ($self, $max) = @_;
1479
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493}
1494
1495sub _draw {
1496 my ($self) = @_;
1497
1498 my $tex = $tex{$self->{gauge}};
1499
1500 my ($w, $h) = ($self->{w}, $self->{h});
1501
1502 my $ycut = $self->{val} / ($self->{max_val} || 1);
1503 $ycut = 1 if $self->{val} > $self->{max_val};
1504
1505 my $t1 = $tex->[0];
1506 my $t2 = $tex->[1];
1507
1508 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512
1513 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h};
1515
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1524 glEnd;
1525
1526 $yp += $h1;
1527
1528 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1534 glEnd;
1535
1536 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D;
1538}
1539
1540#############################################################################
1541
1388package CFClient::UI::Slider; 1542package CFClient::UI::Slider;
1389 1543
1390use strict; 1544use strict;
1391 1545
1392use SDL::OpenGL; 1546use CFClient::OpenGL;
1393 1547
1394our @ISA = CFClient::UI::DrawBG::; 1548our @ISA = CFClient::UI::DrawBG::;
1395 1549
1396my @tex = 1550my @tex =
1397 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1407 # TODO: calculations are off 1561 # TODO: calculations are off
1408 my $self = $class->SUPER::new ( 1562 my $self = $class->SUPER::new (
1409 fg => [1, 1, 1], 1563 fg => [1, 1, 1],
1410 active_fg => [0, 0, 0], 1564 active_fg => [0, 0, 0],
1411 range => [0, 0, 100, 10], 1565 range => [0, 0, 100, 10],
1412 req_w => 40, 1566 req_w => 20,
1413 req_h => 13, 1567 req_h => 20,
1414 vertical => 0, 1568 vertical => 0,
1415 can_hover => 1, 1569 can_hover => 1,
1416 inner_pad => 5, 1570 inner_pad => 5,
1417 @_ 1571 @_
1418 ); 1572 );
1522 1676
1523package CFClient::UI::TextView; 1677package CFClient::UI::TextView;
1524 1678
1525our @ISA = CFClient::UI::HBox::; 1679our @ISA = CFClient::UI::HBox::;
1526 1680
1527use SDL::OpenGL; 1681use CFClient::OpenGL;
1528 1682
1529sub new { 1683sub new {
1530 my $class = shift; 1684 my $class = shift;
1531 1685
1532 my $self = $class->SUPER::new ( 1686 my $self = $class->SUPER::new (
1533 req_w => $::WIDTH / 6,
1534 req_h => $::HEIGHT / 6,
1535 fontsize => $::FONTSIZE, 1687 fontsize => 1,
1536 @_, 1688 @_,
1537 1689
1538 layout => (new CFClient::Layout), 1690 layout => (new CFClient::Layout),
1539 par => [], 1691 par => [],
1540 height => 0, 1692 height => 0,
1561sub text_height { 1713sub text_height {
1562 my ($self, $text) = @_; 1714 my ($self, $text) = @_;
1563 1715
1564 my $layout = $self->{layout}; 1716 my $layout = $self->{layout};
1565 1717
1566 $layout->set_height ($self->{fontsize}); 1718 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1567 $layout->set_width ($self->{w}); 1719 $layout->set_width ($self->{w});
1568 $layout->set_text ($text); 1720 $layout->set_text ($text);
1569 1721
1570 ($layout->size)[1] 1722 ($layout->size)[1]
1571} 1723}
1575 1727
1576 $self->{need_reflow}++; 1728 $self->{need_reflow}++;
1577 $self->update; 1729 $self->update;
1578} 1730}
1579 1731
1580sub size_request {
1581 my ($self) = @_;
1582
1583 ($self->{req_w}, $self->{req_h})
1584}
1585
1586sub size_allocate { 1732sub size_allocate {
1587 my ($self, $x, $y, $w, $h) = @_; 1733 my ($self, $w, $h) = @_;
1588 1734
1589 $self->SUPER::size_allocate ($x, $y, $w, $h); 1735 $self->SUPER::size_allocate ($w, $h);
1590 1736
1591 $self->{layout}->set_height ($self->{fontsize}); 1737 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1592 $self->{layout}->set_width ($self->{w}); 1738 $self->{layout}->set_width ($self->{children}[0]{w});
1593 1739
1594 $self->reflow; 1740 $self->reflow;
1595} 1741}
1596 1742
1597sub add_paragraph { 1743sub add_paragraph {
1684 1830
1685} 1831}
1686 1832
1687############################################################################# 1833#############################################################################
1688 1834
1689package CFClient::UI::MapWidget;
1690
1691use strict;
1692
1693use List::Util qw(min max);
1694
1695use SDL;
1696use SDL::OpenGL;
1697
1698our @ISA = CFClient::UI::Base::;
1699
1700sub new {
1701 my $class = shift;
1702
1703 $class->SUPER::new (
1704 z => -1,
1705 can_focus => 1,
1706 list => (glGenLists 1),
1707 @_
1708 )
1709}
1710
1711sub key_down {
1712 print "MAPKEYDOWN\n";
1713}
1714
1715sub key_up {
1716}
1717
1718sub button_down {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->focus_in;
1722
1723 if ($ev->button == 2) {
1724 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1725 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1726
1727 $self->{motion} = sub {
1728 my ($ev, $x, $y) = @_;
1729
1730 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1731
1732 $::CFG->{map_shift_x} = $bw + $x - $ox;
1733 $::CFG->{map_shift_y} = $bh + $y - $oy;
1734
1735 $self->update;
1736 };
1737 }
1738}
1739
1740sub button_up {
1741 my ($self, $ev, $x, $y) = @_;
1742
1743 delete $self->{motion};
1744}
1745
1746sub mouse_motion {
1747 my ($self, $ev, $x, $y) = @_;
1748
1749 $self->{motion}->($ev, $x, $y) if $self->{motion};
1750}
1751
1752sub size_request {
1753 (
1754 1 + 32 * int $::WIDTH / 32,
1755 1 + 32 * int $::HEIGHT / 32,
1756 )
1757}
1758
1759sub update {
1760 my ($self) = @_;
1761
1762 $self->{need_update} = 1;
1763 $self->SUPER::update;
1764}
1765
1766sub draw {
1767 my ($self) = @_;
1768
1769 if (delete $self->{need_update}) {
1770 glNewList $self->{list}, GL_COMPILE;
1771
1772 if ($::MAP) {
1773 my $sw = int $::WIDTH / 32;
1774 my $sh = int $::HEIGHT / 32;
1775
1776 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1777 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1778
1779 glTranslate $sx0 - 32, $sy0 - 32, 0;
1780
1781 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1782
1783 if ($::CFG->{fow_enable}) {
1784 if ($::CFG->{fow_smooth}) { # smooth fog of war
1785 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1786 glConvolutionFilter2D
1787 GL_CONVOLUTION_2D,
1788 GL_ALPHA,
1789 3, 3,
1790 GL_ALPHA, GL_FLOAT,
1791 pack "f*",
1792 0.1, 0.1, 0.1,
1793 0.1, 0.2, 0.1,
1794 0.1, 0.1, 0.1,
1795 ;
1796 glEnable GL_CONVOLUTION_2D;
1797 }
1798
1799 $self->{fow_texture} = new CFClient::Texture
1800 w => $w,
1801 h => $h,
1802 data => $data,
1803 internalformat => GL_ALPHA,
1804 format => GL_ALPHA;
1805
1806 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1807
1808 glEnable GL_BLEND;
1809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1810 glEnable GL_TEXTURE_2D;
1811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1812
1813 glColor +($::CFG->{fow_intensity}) x 3, 1;
1814 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1815
1816 glDisable GL_TEXTURE_2D;
1817 glDisable GL_BLEND;
1818 }
1819
1820 # HACK BEGIN
1821 {
1822 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1823 my ($w, $h) = (250, 250);
1824
1825 glEnable GL_BLEND;
1826 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1827 glEnable GL_TEXTURE_2D;
1828 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1829
1830 $self->{mapmap_texture} =
1831 new CFClient::Texture
1832 w => $w,
1833 h => $h,
1834 data => $::MAP->mapmap ($w, $h),
1835 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1836
1837 $self->{mapmap_texture}->draw_quad (100, 100);
1838
1839 glDisable GL_TEXTURE_2D;
1840 glDisable GL_BLEND;
1841 }
1842 # HACK END
1843 }
1844
1845 glEndList;
1846 }
1847
1848 glPushMatrix;
1849 glCallList $self->{list};
1850 glPopMatrix;
1851
1852 if ($FOCUS != $self) {
1853 glColor 64/255, 64/255, 64/255;
1854 glLogicOp GL_AND;
1855 glEnable GL_COLOR_LOGIC_OP;
1856 glBegin GL_QUADS;
1857 glVertex 0, 0;
1858 glVertex 0, $::HEIGHT;
1859 glVertex $::WIDTH, $::HEIGHT;
1860 glVertex $::WIDTH, 0;
1861 glEnd;
1862 glDisable GL_COLOR_LOGIC_OP;
1863 }
1864}
1865
1866my %DIR = (
1867 SDLK_KP8, [1, "north"],
1868 SDLK_KP9, [2, "northeast"],
1869 SDLK_KP6, [3, "east"],
1870 SDLK_KP3, [4, "southeast"],
1871 SDLK_KP2, [5, "south"],
1872 SDLK_KP1, [6, "southwest"],
1873 SDLK_KP4, [7, "west"],
1874 SDLK_KP7, [8, "northwest"],
1875
1876 SDLK_UP, [1, "north"],
1877 SDLK_RIGHT, [3, "east"],
1878 SDLK_DOWN, [5, "south"],
1879 SDLK_LEFT, [7, "west"],
1880);
1881
1882sub key_down {
1883 my ($self, $ev) = @_;
1884
1885 my $mod = $ev->key_mod;
1886 my $sym = $ev->key_sym;
1887
1888 if ($sym == SDLK_KP5) {
1889 $::CONN->user_send ("command stay fire");
1890 } elsif ($sym == SDLK_a) {
1891 $::CONN->user_send ("command apply");
1892 } elsif ($sym == SDLK_QUOTE) {
1893 $self->emit ('activate_console');
1894 } elsif ($sym == SDLK_SLASH) {
1895 $self->emit ('activate_console' => '/');
1896 } elsif (exists $DIR{$sym}) {
1897 if ($mod & KMOD_SHIFT) {
1898 $self->{shft}++;
1899 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1900 } elsif ($mod & KMOD_CTRL) {
1901 $self->{ctrl}++;
1902 $::CONN->user_send ("command run $DIR{$sym}[0]");
1903 } else {
1904 $::CONN->user_send ("command $DIR{$sym}[1]");
1905 }
1906 }
1907}
1908
1909sub key_up {
1910 my ($self, $ev) = @_;
1911
1912 my $mod = $ev->key_mod;
1913 my $sym = $ev->key_sym;
1914
1915 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1916 $::CONN->user_send ("command fire_stop");
1917 }
1918 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1919 $::CONN->user_send ("command run_stop");
1920 }
1921}
1922
1923#############################################################################
1924
1925package CFClient::UI::Animator; 1835package CFClient::UI::Animator;
1926 1836
1927use SDL::OpenGL; 1837use CFClient::OpenGL;
1928 1838
1929our @ISA = CFClient::UI::Bin::; 1839our @ISA = CFClient::UI::Bin::;
1930 1840
1931sub moveto { 1841sub moveto {
1932 my ($self, $x, $y) = @_; 1842 my ($self, $x, $y) = @_;
2005 1915
2006package CFClient::UI::Root; 1916package CFClient::UI::Root;
2007 1917
2008our @ISA = CFClient::UI::Container::; 1918our @ISA = CFClient::UI::Container::;
2009 1919
2010use SDL::OpenGL; 1920use CFClient::OpenGL;
1921
1922sub check_size {
1923 my ($self) = @_;
1924
1925 $self->configure (0, 0, $::WITH, $::HEIGHT);
1926}
2011 1927
2012sub size_request { 1928sub size_request {
2013 ($::WIDTH, $::HEIGHT) 1929 ($::WIDTH, $::HEIGHT)
2014} 1930}
2015 1931
2016sub size_allocate { 1932sub configure {
2017 my ($self, $x, $y, $w, $h) = @_; 1933 my ($self, $x, $y, $w, $h) = @_;
2018 1934
2019 $self->_size_allocate ($x, $y, $w, $h); 1935 $self->SUPER::configure ($x, $y, $w, $h);
2020 1936
2021 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2022 for @{$self->{children}}; 1938 for @{$self->{children}};
2023} 1939}
2024 1940
2025sub _topleft { 1941sub _topleft {
2026 my ($self, $x, $y) = @_; 1942 my ($self, $x, $y) = @_;
2029} 1945}
2030 1946
2031sub update { 1947sub update {
2032 my ($self) = @_; 1948 my ($self) = @_;
2033 1949
2034 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1950 $self->check_size;
2035 ::refresh (); 1951 ::refresh ();
2036} 1952}
2037 1953
2038sub add { 1954sub add {
2039 my ($self, $widget) = @_; 1955 my ($self, $child) = @_;
2040 1956
2041 $self->SUPER::add ($widget); 1957 $self->SUPER::add ($child);
2042
2043 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2044} 1958}
2045 1959
2046sub on_refresh { 1960sub on_refresh {
2047 my ($self, $id, $cb) = @_; 1961 my ($self, $id, $cb) = @_;
2048 1962

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines