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.123 by root, Mon Apr 17 19:50:22 2006 UTC vs.
Revision 1.143 by root, Thu Apr 20 21:28:51 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
151 if ($self->{aspect}) {
152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154
155 # use alignment to adjust x, y
156
157 $x += ($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 164 $self->{x} = $x;
135 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
136 168
137 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 170 $self->{w} = $w;
140 $self->{h} = $h; 171 $self->{h} = $h;
141 172
173 $self->size_allocate ($w, $h);
174 $self->update;
142 1 175 }
143} 176}
144 177
145sub size_allocate { 178sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 179 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 180}
150 181
151# return top left coordinates 182# return top left coordinates
152sub _topleft { 183sub _topleft {
153 my ($self, $x, $y) = @_; 184 my ($self, $x, $y) = @_;
260 my ($self, $parent) = @_; 291 my ($self, $parent) = @_;
261 292
262 Scalar::Util::weaken ($self->{parent} = $parent); 293 Scalar::Util::weaken ($self->{parent} = $parent);
263} 294}
264 295
296sub check_size {
297 my ($self) = @_;
298
299 my ($w, $h) = $self->size_request;
300
301 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w;
303 $self->{req_h} = $h;
304
305 $self->{parent}->check_size
306 if $self->{parent};
307 }
308}
309
265sub update { 310sub update {
266 my ($self) = @_; 311 my ($self) = @_;
267 312
268 $self->{parent}->update 313 $self->{parent}->update
269 if $self->{parent}; 314 if $self->{parent};
270} 315}
271 316
272sub connect { 317sub connect {
273 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
274 319
275 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 321}
277 322
278sub emit { 323sub emit {
279 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
280 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 327 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 328 }
283} 329}
284 330
285sub DESTROY { 331sub DESTROY {
286 my ($self) = @_; 332 my ($self) = @_;
287 333
293package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
294 340
295our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
296 342
297use strict; 343use strict;
298use SDL::OpenGL; 344use CFClient::OpenGL;
299 345
300sub new { 346sub new {
301 my $class = shift; 347 my $class = shift;
302 348
303 # range [value, low, high, page] 349 # range [value, low, high, page]
367 $self->{children} = [ 413 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 415 @{$self->{children}}, $child
370 ]; 416 ];
371 417
372 $self->{w} = $self->{h} = -1; 418 $child->check_size;
373 $self->update;
374} 419}
375 420
376sub remove { 421sub remove {
377 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
378 423
424 delete $child->{parent};
425
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
380 427
381 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 428 $self->check_size;
382} 429}
383 430
384sub find_widget { 431sub find_widget {
385 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
386 433
416 463
417 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
418} 465}
419 466
420sub add { 467sub add {
421 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
422 469
423 $self->{children} = []; 470 $self->{children} = [];
424 471
425 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
426} 473}
427 474
428sub remove { 475sub remove {
429 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
430 477
439sub size_request { 486sub size_request {
440 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
441} 488}
442 489
443sub size_allocate { 490sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
445 492
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
449} 494}
450 495
451############################################################################# 496#############################################################################
452 497
453package CFClient::UI::Window; 498package CFClient::UI::Window;
454 499
455our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
456 501
457use SDL::OpenGL; 502use CFClient::OpenGL;
458 503
459sub new { 504sub new {
460 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
461 506
462 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
479 $self->child->draw; 524 $self->child->draw;
480 }; 525 };
481} 526}
482 527
483sub size_allocate { 528sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
485 530
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
489 532
490 $self->render_chld; 533 $self->render_chld;
491} 534}
492 535
493sub _draw { 536sub _draw {
509 glDisable GL_TEXTURE_2D; 552 glDisable GL_TEXTURE_2D;
510} 553}
511 554
512############################################################################# 555#############################################################################
513 556
557package CFClient::UI::ViewPort;
558
559our @ISA = CFClient::UI::Window::;
560
561sub new { die }
562
563sub size_request {
564 my ($self) = @_;
565
566 @$self{qw(child_w child_h)} = $self->child->size_request;
567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568
569 @$self{qw(child_w child_h)}
570}
571
572sub _draw {
573 my ($self) = @_;
574
575 $self->{children}[1]->draw;
576}
577
578
579#############################################################################
580
514package CFClient::UI::Frame; 581package CFClient::UI::Frame;
515 582
516our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
517 584
518use SDL::OpenGL; 585use CFClient::OpenGL;
519 586
520sub size_request { 587sub size_request {
521 my ($self) = @_; 588 my ($self) = @_;
522 my $chld = $self->child 589 my $chld = $self->child
523 or return (0, 0); 590 or return (0, 0);
528} 595}
529 596
530sub size_allocate { 597sub size_allocate {
531 my ($self, $x, $y, $w, $h) = @_; 598 my ($self, $x, $y, $w, $h) = @_;
532 599
533 $self->_size_allocate ($x, $y, $w, $h) or return;
534
535 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 600 $self->child->configure (2, 2, $w - 4, $h - 4);
536} 601}
537 602
538sub _draw { 603sub _draw {
539 my ($self) = @_; 604 my ($self) = @_;
540 605
557 622
558package CFClient::UI::FancyFrame; 623package CFClient::UI::FancyFrame;
559 624
560our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
561 626
562use SDL::OpenGL; 627use CFClient::OpenGL;
563 628
564my @tex = 629my @tex =
565 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 630 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); 631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
567 632
568sub new { 633sub new {
569 my $class = shift; 634 my $class = shift;
570 635
571 # TODO: user_x, user_y, overwrite moveto? 636 # TODO: user_x, user_y, overwrite moveto?
572 637
573 $class->SUPER::new ( 638 my $self = $class->SUPER::new (
574 bg => [1, 1, 1, 1], 639 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 640 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 641 border => 0.8,
577 @_ 642 @_
643 );
644
645 $self->{title} &&= new CFClient::UI::Label
646 align => 0,
647 valign => 1,
648 text => $self->{title},
649 fontsize => 1;
650
651 $self
652}
653
654sub border {
655 int $_[0]{border} * $::FONTSIZE
656}
657
658sub size_request {
659 my ($self) = @_;
660
661 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
662
663 my ($w, $h) = $self->SUPER::size_request;
664
665 (
666 $w + $self->border * 2,
667 $h + $self->border * 2,
578 ) 668 )
579} 669}
580 670
581sub size_request {
582 my ($self) = @_;
583
584 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
585
586 my ($w, $h) = $self->SUPER::size_request;
587
588 (
589 $w + $self->{border} * 2,
590 $h + $self->{border} * 2,
591 )
592}
593
594sub size_allocate { 671sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 672 my ($self, $w, $h) = @_;
596 673
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 674 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 675 $w -= List::Util::max 0, $self->border * 2;
601 676
677 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
678 if $self->{title};
679
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 680 $self->child->configure ($self->border, $self->border, $w, $h);
603} 681}
604 682
605sub button_down { 683sub button_down {
606 my ($self, $ev, $x, $y) = @_; 684 my ($self, $ev, $x, $y) = @_;
607 685
686 my $border = $self->border;
687
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 688 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 689 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 690
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 691 my ($ox, $oy) = ($ev->{x}, $ev->{y});
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 692 my ($bw, $bh) = ($self->{w}, $self->{h});
613 693
614 $self->{motion} = sub { 694 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_; 695 my ($ev, $x, $y) = @_;
616 696
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 697 ($x, $y) = ($ev->{x}, $ev->{y});
618 698
619 $self->{user_w} = $bw + $x - $ox; 699 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 700 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 701 $self->check_size;
622 }; 702 };
623 703
624 } elsif ($x >= 0 && $x < $self->{w} 704 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 705 && $y >= 0 && $y < $border) {
626 706
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 707 my ($ox, $oy) = ($ev->{x}, $ev->{y});
628 my ($bx, $by) = ($self->{x}, $self->{y}); 708 my ($bx, $by) = ($self->{x}, $self->{y});
629 709
630 $self->{motion} = sub { 710 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_; 711 my ($ev, $x, $y) = @_;
632 712
633 ($x, $y) = ($ev->motion_x, $ev->motion_y); 713 ($x, $y) = ($ev->{x}, $ev->{y});
634 714
635 $self->move ($bx + $x - $ox, $by + $y - $oy); 715 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update; 716 $self->update;
637 }; 717 };
638 } 718 }
659 glEnable GL_BLEND; 739 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 740 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 743
744 my $border = $self->border;
745
664 glColor @{ $self->{border_bg} }; 746 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 747 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 748 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 749 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 750 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 751
670 my $bg = $tex[0]; 752 my $bg = $tex[0];
671 753
672 # TODO: repeat texture not scale 754 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 755 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 758 glColor @{ $self->{bg} };
677 759
678 $bg->{s} = $rep_x; 760 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 761 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 762 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 763 $bg->draw_quad ($border, $border, $cw, $ch);
682 764
683 glDisable GL_TEXTURE_2D; 765 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 766 glDisable GL_BLEND;
685 767
768 $self->{title}->draw if $self->{title};
686 $self->child->draw; 769 $self->child->draw;
687} 770}
688 771
689############################################################################# 772#############################################################################
690 773
692 775
693our @ISA = CFClient::UI::Base::; 776our @ISA = CFClient::UI::Base::;
694 777
695use List::Util qw(max sum); 778use List::Util qw(max sum);
696 779
697use SDL::OpenGL; 780use CFClient::OpenGL;
698 781
699sub new { 782sub new {
700 my $class = shift; 783 my $class = shift;
701 784
702 $class->SUPER::new ( 785 $class->SUPER::new (
709 my ($self, $x, $y, $child) = @_; 792 my ($self, $x, $y, $child) = @_;
710 793
711 $child->set_parent ($self); 794 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 795 $self->{children}[$y][$x] = $child;
713 796
714 $self->{w} = $self->{h} = -1; 797 $child->check_size;
715 $self->update;
716} 798}
717 799
718# TODO: move to container class maybe? send childs a signal on removal? 800# TODO: move to container class maybe? send childs a signal on removal?
719sub clear { 801sub clear {
720 my ($self) = @_; 802 my ($self) = @_;
755 (sum @$hs), 837 (sum @$hs),
756 ) 838 )
757} 839}
758 840
759sub size_allocate { 841sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 842 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 843
764 my ($ws, $hs) = $self->get_wh; 844 my ($ws, $hs) = $self->get_wh;
765 845
766 my $req_w = sum @$ws; 846 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 847 my $req_h = sum @$hs;
789 869
790 for my $c (0 .. $#$row) { 870 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 871 my $col_w = $ws->[$c];
792 872
793 if (my $widget = $row->[$c]) { 873 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 874 $widget->configure ($x, $y, $col_w, $row_h);
795 } 875 }
796 876
797 $x += $col_w; 877 $x += $col_w;
798 } 878 }
799 879
844 (List::Util::max map $_->[1], @alloc), 924 (List::Util::max map $_->[1], @alloc),
845 ) 925 )
846} 926}
847 927
848sub size_allocate { 928sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 929 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 930
853 ($h, $w) = ($w, $h); 931 ($h, $w) = ($w, $h);
854 932
855 my $children = $self->{children}; 933 my $children = $self->{children};
856 934
878 956
879 my $y = 0; 957 my $y = 0;
880 for (0 .. $#$children) { 958 for (0 .. $#$children) {
881 my $child = $children->[$_]; 959 my $child = $children->[$_];
882 my $h = $h[$_]; 960 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 961 $child->configure ($y, 0, $h, $w);
884 962
885 $y += $h; 963 $y += $h;
886 } 964 }
965
966 1
887} 967}
888 968
889############################################################################# 969#############################################################################
890 970
891package CFClient::UI::VBox; 971package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 984 (List::Util::sum map $_->[1], @alloc),
905 ) 985 )
906} 986}
907 987
908sub size_allocate { 988sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 989 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 990
913 my $children = $self->{children}; 991 my $children = $self->{children};
914 992
915 my @h = map +($_->size_request)[1], @$children; 993 my @h = map +($_->size_request)[1], @$children;
916 994
934 1012
935 my $y = 0; 1013 my $y = 0;
936 for (0 .. $#$children) { 1014 for (0 .. $#$children) {
937 my $child = $children->[$_]; 1015 my $child = $children->[$_];
938 my $h = $h[$_]; 1016 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 1017 $child->configure (0, $y, $w, $h);
940 1018
941 $y += $h; 1019 $y += $h;
942 } 1020 }
1021
1022 1
943} 1023}
944 1024
945############################################################################# 1025#############################################################################
946 1026
947package CFClient::UI::Label; 1027package CFClient::UI::Label;
948 1028
949our @ISA = CFClient::UI::Base::; 1029our @ISA = CFClient::UI::Base::;
950 1030
951use SDL::OpenGL; 1031use CFClient::OpenGL;
952 1032
953sub new { 1033sub new {
954 my ($class, %arg) = @_; 1034 my ($class, %arg) = @_;
955 1035
956 my $self = $class->SUPER::new ( 1036 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1037 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1038 fontsize => 1,
959 text => "", 1039 text => "",
960 align => -1, 1040 align => -1,
961 valign => -1, 1041 valign => -1,
962 padding => 2, 1042 padding => 2,
963 layout => new CFClient::Layout, 1043 layout => new CFClient::Layout,
964 %arg 1044 %arg
965 ); 1045 );
966 1046
967 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1047 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout;
1049 $layout->set_text (delete $self->{template});
1050 $self->{template} = $layout;
1051 }
968 1052
969 $self->set_text (delete $self->{text}) if exists $self->{text}; 1053 $self->set_text (delete $self->{text}) if exists $self->{text};
970 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1054 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
971 1055
972 $self 1056 $self
1002 1086
1003sub size_request { 1087sub size_request {
1004 my ($self) = @_; 1088 my ($self) = @_;
1005 1089
1006 $self->{layout}->set_width; 1090 $self->{layout}->set_width;
1007 $self->{layout}->set_height ($self->{fontsize}); 1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1008 1092
1009 my ($w, $h) = $self->{layout}->size; 1093 my ($w, $h) = $self->{layout}->size;
1094
1095 if (exists $self->{template}) {
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097
1098 my ($w2, $h2) = $self->{template}->size;
1099
1100 $w = List::Util::max $w, $w2;
1101 $h = List::Util::max $h, $h2;
1102 }
1010 1103
1011 ( 1104 (
1012 $w + $self->{padding} * 2, 1105 $w + $self->{padding} * 2,
1013 $h + $self->{padding} * 2, 1106 $h + $self->{padding} * 2,
1014 ) 1107 )
1015} 1108}
1016 1109
1017sub size_allocate { 1110sub size_allocate {
1018 my ($self, $x, $y, $w, $h) = @_; 1111 my ($self, $w, $h) = @_;
1019
1020 $self->_size_allocate ($x, $y, $w, $h) or return;
1021 1112
1022 delete $self->{texture}; 1113 delete $self->{texture};
1023}
1024
1025sub update {
1026 my ($self) = @_;
1027
1028 delete $self->{texture};
1029 $self->SUPER::update;
1030} 1114}
1031 1115
1032sub _draw { 1116sub _draw {
1033 my ($self) = @_; 1117 my ($self) = @_;
1034 1118
1035 my $tex = $self->{texture} ||= do { 1119 my $tex = $self->{texture} ||= do {
1036 $self->{layout}->set_width ($self->{w}); 1120 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1121 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1038 new_from_layout CFClient::Texture $self->{layout} 1122 new_from_layout CFClient::Texture $self->{layout}
1039 }; 1123 };
1040 1124
1041 glEnable GL_BLEND; 1125 glEnable GL_BLEND;
1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1126 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1067 1151
1068package CFClient::UI::EntryBase; 1152package CFClient::UI::EntryBase;
1069 1153
1070our @ISA = CFClient::UI::Label::; 1154our @ISA = CFClient::UI::Label::;
1071 1155
1072use SDL; 1156use CFClient::OpenGL;
1073use SDL::OpenGL;
1074 1157
1075sub new { 1158sub new {
1076 my $class = shift; 1159 my $class = shift;
1077 1160
1078 $class->SUPER::new ( 1161 $class->SUPER::new (
1092 1175
1093 delete $self->{cur_h}; 1176 delete $self->{cur_h};
1094 1177
1095 return if $self->{text} eq $text; 1178 return if $self->{text} eq $text;
1096 1179
1180 delete $self->{texture};
1181
1097 $self->{last_activity} = $::NOW; 1182 $self->{last_activity} = $::NOW;
1098 $self->{text} = $text; 1183 $self->{text} = $text;
1099 1184
1100 $text =~ s/./*/g if $self->{hidden}; 1185 $text =~ s/./*/g if $self->{hidden};
1101 $self->{layout}->set_text ("$text "); 1186 $self->{layout}->set_text ("$text ");
1114 1199
1115 ($w + 1, $h) # add 1 for cursor 1200 ($w + 1, $h) # add 1 for cursor
1116} 1201}
1117 1202
1118sub size_allocate { 1203sub size_allocate {
1119 my ($self, $x, $y, $w, $h) = @_; 1204 my ($self, $w, $h) = @_;
1120
1121 $self->SUPER::size_allocate ($x, $y, $w, $h);
1122 1205
1123 $self->_set_text ($self->{text}); 1206 $self->_set_text ($self->{text});
1124} 1207}
1125 1208
1126sub set_text { 1209sub set_text {
1132} 1215}
1133 1216
1134sub key_down { 1217sub key_down {
1135 my ($self, $ev) = @_; 1218 my ($self, $ev) = @_;
1136 1219
1137 my $mod = $ev->key_mod; 1220 my $mod = $ev->{mod};
1138 my $sym = $ev->key_sym; 1221 my $sym = $ev->{sym};
1139
1140 my $uni = $ev->key_unicode; 1222 my $uni = $ev->{unicode};
1141 1223
1142 my $text = $self->get_text; 1224 my $text = $self->get_text;
1143 1225
1144 if ($sym == SDLK_BACKSPACE) { 1226 if ($sym == 8) {
1145 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1227 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1146 } elsif ($sym == SDLK_DELETE) { 1228 } elsif ($sym == 127) {
1147 substr $text, $self->{cursor}, 1, ""; 1229 substr $text, $self->{cursor}, 1, "";
1148 } elsif ($sym == SDLK_LEFT) { 1230 } elsif ($sym == CFClient::SDLK_LEFT) {
1149 --$self->{cursor} if $self->{cursor}; 1231 --$self->{cursor} if $self->{cursor};
1150 } elsif ($sym == SDLK_RIGHT) { 1232 } elsif ($sym == CFClient::SDLK_RIGHT) {
1151 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1233 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1152 } elsif ($sym == SDLK_HOME) { 1234 } elsif ($sym == CFClient::SDLK_HOME) {
1153 $self->{cursor} = 0; 1235 $self->{cursor} = 0;
1154 } elsif ($sym == SDLK_END) { 1236 } elsif ($sym == CFClient::SDLK_END) {
1155 $self->{cursor} = length $text; 1237 $self->{cursor} = length $text;
1156 } elsif ($sym == SDLK_ESCAPE) { 1238 } elsif ($sym == 27) {
1157 $self->emit ('escape'); 1239 $self->emit ('escape');
1158 } elsif ($uni) { 1240 } elsif ($uni) {
1159 substr $text, $self->{cursor}++, 0, chr $uni; 1241 substr $text, $self->{cursor}++, 0, chr $uni;
1160 } 1242 }
1161 1243
1236 1318
1237package CFClient::UI::Entry; 1319package CFClient::UI::Entry;
1238 1320
1239our @ISA = CFClient::UI::EntryBase::; 1321our @ISA = CFClient::UI::EntryBase::;
1240 1322
1241use SDL; 1323use CFClient::OpenGL;
1242use SDL::OpenGL;
1243 1324
1244sub key_down { 1325sub key_down {
1245 my ($self, $ev) = @_; 1326 my ($self, $ev) = @_;
1246 1327
1247 my $sym = $ev->key_sym; 1328 my $sym = $ev->{sym};
1248 1329
1249 if ($sym == SDLK_RETURN) { 1330 if ($sym == 13) {
1250 $self->emit (activate => $self->get_text); 1331 $self->emit (activate => $self->get_text);
1251 $self->update; 1332 $self->update;
1252 1333
1253 } else { 1334 } else {
1254 $self->SUPER::key_down ($ev); 1335 $self->SUPER::key_down ($ev);
1260 1341
1261package CFClient::UI::Button; 1342package CFClient::UI::Button;
1262 1343
1263our @ISA = CFClient::UI::Label::; 1344our @ISA = CFClient::UI::Label::;
1264 1345
1265use SDL; 1346use CFClient::OpenGL;
1266use SDL::OpenGL;
1267 1347
1268my @tex = 1348my @tex =
1269 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1270 qw(b1_button_active.png); 1350 qw(b1_button_active.png);
1271 1351
1276 padding => 4, 1356 padding => 4,
1277 fg => [1, 1, 1], 1357 fg => [1, 1, 1],
1278 bg => [1, 1, 1, 0.2], 1358 bg => [1, 1, 1, 0.2],
1279 active_fg => [0, 0, 1], 1359 active_fg => [0, 0, 1],
1280 can_hover => 1, 1360 can_hover => 1,
1361 align => 0,
1362 valign => 0,
1281 @_ 1363 @_
1282 ) 1364 )
1283} 1365}
1284 1366
1285sub button_up { 1367sub button_up {
1322 1404
1323my @tex = 1405my @tex =
1324 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1325 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1407 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1326 1408
1327use SDL; 1409use CFClient::OpenGL;
1328use SDL::OpenGL;
1329 1410
1330sub new { 1411sub new {
1331 my $class = shift; 1412 my $class = shift;
1332 1413
1333 $class->SUPER::new ( 1414 $class->SUPER::new (
1344 my ($self) = @_; 1425 my ($self) = @_;
1345 1426
1346 ($self->{padding} * 2 + 6) x 2 1427 ($self->{padding} * 2 + 6) x 2
1347} 1428}
1348 1429
1349sub size_allocate {
1350 my ($self, $x, $y, $w, $h) = @_;
1351
1352 $self->_size_allocate ($x, $y, $w, $h);
1353}
1354
1355sub button_down { 1430sub button_down {
1356 my ($self, $ev, $x, $y) = @_; 1431 my ($self, $ev, $x, $y) = @_;
1357 1432
1358 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1433 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1359 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1434 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1385 glDisable GL_BLEND; 1460 glDisable GL_BLEND;
1386} 1461}
1387 1462
1388############################################################################# 1463#############################################################################
1389 1464
1465package CFClient::UI::VGauge;
1466
1467our @ISA = CFClient::UI::Base::;
1468
1469use CFClient::OpenGL;
1470
1471my %tex = (
1472 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ],
1476 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1479 ],
1480 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ],
1484 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1487 ],
1488);
1489
1490# eg. VGauge->new (gauge => 'food'), default gauge: food
1491sub new {
1492 my $class = shift;
1493
1494 my $self = $class->SUPER::new (
1495 type => 'food',
1496 @_
1497 );
1498
1499 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1500
1501 $self
1502}
1503
1504sub size_request {
1505 my ($self) = @_;
1506
1507 #my $tex = $tex{$self->{type}}[0];
1508 #@$tex{qw(w h)}
1509 (0, 0)
1510}
1511
1512sub set_max {
1513 my ($self, $max) = @_;
1514
1515 $self->{max_val} = $max;
1516}
1517
1518sub set_value {
1519 my ($self, $val, $max) = @_;
1520
1521 $self->set_max ($max)
1522 if defined $max;
1523
1524 $max = $self->{max_val};
1525 $self->{val} = $val;
1526
1527 $self->update;
1528}
1529
1530sub _draw {
1531 my ($self) = @_;
1532
1533 my $tex = $tex{$self->{type}};
1534
1535 my ($w, $h) = ($self->{w}, $self->{h});
1536
1537 if ($self->{vertical}) {
1538 glRotate 90, 0, 0, 1;
1539 glTranslate 0, -$self->{w}, 0;
1540
1541 ($w, $h) = ($h, $w);
1542 }
1543
1544 my $ycut = $self->{val} / ($self->{max_val} || 1);
1545 $ycut = 1 if $self->{val} > $self->{max_val};
1546
1547 my $t1 = $tex->[0];
1548 my $t2 = $tex->[1];
1549
1550 glEnable GL_BLEND;
1551 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1552 glEnable GL_TEXTURE_2D;
1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1554
1555 my $h1 = $self->{h} - $ycut * $self->{h};
1556 my $h2 = $ycut * $self->{h};
1557
1558 glBindTexture GL_TEXTURE_2D, $t1->{name};
1559 glBegin GL_QUADS;
1560 glTexCoord 0 , 0; glVertex 0 , 0;
1561 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1562 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1563 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1564 glEnd;
1565
1566 glBindTexture GL_TEXTURE_2D, $t2->{name};
1567 glBegin GL_QUADS;
1568 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1569 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1570 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1571 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1572 glEnd;
1573
1574 glDisable GL_BLEND;
1575 glDisable GL_TEXTURE_2D;
1576}
1577
1578#############################################################################
1579
1580package CFClient::UI::Gauge;
1581
1582our @ISA = CFClient::UI::VBox::;
1583
1584sub new {
1585 my ($class, %arg) = shift;
1586
1587 my $self = $class->SUPER::new (
1588 @_,
1589 );
1590
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1592 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1593 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1594
1595 $self
1596}
1597
1598sub set_value {
1599 my ($self, $val, $max) = @_;
1600
1601 $self->set_max ($max)
1602 if defined $max;
1603
1604 $self->{gauge}->set_value ($val, $max);
1605 $self->{value}->set_text ($val);
1606}
1607
1608sub set_max {
1609 my ($self, $max) = @_;
1610
1611 $self->{gauge}->set_max ($max);
1612 $self->{max}->set_text ($max);
1613}
1614
1615#############################################################################
1616
1390package CFClient::UI::Slider; 1617package CFClient::UI::Slider;
1391 1618
1392use strict; 1619use strict;
1393 1620
1394use SDL::OpenGL; 1621use CFClient::OpenGL;
1395 1622
1396our @ISA = CFClient::UI::DrawBG::; 1623our @ISA = CFClient::UI::DrawBG::;
1397 1624
1398my @tex = 1625my @tex =
1399 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1409 # TODO: calculations are off 1636 # TODO: calculations are off
1410 my $self = $class->SUPER::new ( 1637 my $self = $class->SUPER::new (
1411 fg => [1, 1, 1], 1638 fg => [1, 1, 1],
1412 active_fg => [0, 0, 0], 1639 active_fg => [0, 0, 0],
1413 range => [0, 0, 100, 10], 1640 range => [0, 0, 100, 10],
1414 req_w => 40, 1641 req_w => 20,
1415 req_h => 13, 1642 req_h => 20,
1416 vertical => 0, 1643 vertical => 0,
1417 can_hover => 1, 1644 can_hover => 1,
1418 inner_pad => 5, 1645 inner_pad => 5,
1419 @_ 1646 @_
1420 ); 1647 );
1524 1751
1525package CFClient::UI::TextView; 1752package CFClient::UI::TextView;
1526 1753
1527our @ISA = CFClient::UI::HBox::; 1754our @ISA = CFClient::UI::HBox::;
1528 1755
1529use SDL::OpenGL; 1756use CFClient::OpenGL;
1530 1757
1531sub new { 1758sub new {
1532 my $class = shift; 1759 my $class = shift;
1533 1760
1534 my $self = $class->SUPER::new ( 1761 my $self = $class->SUPER::new (
1535 req_w => $::WIDTH / 6,
1536 req_h => $::HEIGHT / 6,
1537 fontsize => $::FONTSIZE, 1762 fontsize => 1,
1538 @_, 1763 @_,
1539 1764
1540 layout => (new CFClient::Layout), 1765 layout => (new CFClient::Layout),
1541 par => [], 1766 par => [],
1542 height => 0, 1767 height => 0,
1563sub text_height { 1788sub text_height {
1564 my ($self, $text) = @_; 1789 my ($self, $text) = @_;
1565 1790
1566 my $layout = $self->{layout}; 1791 my $layout = $self->{layout};
1567 1792
1568 $layout->set_height ($self->{fontsize}); 1793 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1569 $layout->set_width ($self->{w}); 1794 $layout->set_width ($self->{w});
1570 $layout->set_text ($text); 1795 $layout->set_text ($text);
1571 1796
1572 ($layout->size)[1] 1797 ($layout->size)[1]
1573} 1798}
1577 1802
1578 $self->{need_reflow}++; 1803 $self->{need_reflow}++;
1579 $self->update; 1804 $self->update;
1580} 1805}
1581 1806
1582sub size_request {
1583 my ($self) = @_;
1584
1585 ($self->{req_w}, $self->{req_h})
1586}
1587
1588sub size_allocate { 1807sub size_allocate {
1589 my ($self, $x, $y, $w, $h) = @_; 1808 my ($self, $w, $h) = @_;
1590 1809
1591 $self->SUPER::size_allocate ($x, $y, $w, $h); 1810 $self->SUPER::size_allocate ($w, $h);
1592 1811
1593 $self->{layout}->set_height ($self->{fontsize}); 1812 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1594 $self->{layout}->set_width ($self->{w}); 1813 $self->{layout}->set_width ($self->{children}[0]{w});
1595 1814
1596 $self->reflow; 1815 $self->reflow;
1597} 1816}
1598 1817
1599sub add_paragraph { 1818sub add_paragraph {
1686 1905
1687} 1906}
1688 1907
1689############################################################################# 1908#############################################################################
1690 1909
1691package CFClient::UI::MapWidget;
1692
1693use strict;
1694
1695use List::Util qw(min max);
1696
1697use SDL;
1698use SDL::OpenGL;
1699
1700our @ISA = CFClient::UI::Base::;
1701
1702sub new {
1703 my $class = shift;
1704
1705 $class->SUPER::new (
1706 z => -1,
1707 can_focus => 1,
1708 list => (glGenLists 1),
1709 @_
1710 )
1711}
1712
1713sub key_down {
1714 print "MAPKEYDOWN\n";
1715}
1716
1717sub key_up {
1718}
1719
1720sub button_down {
1721 my ($self, $ev, $x, $y) = @_;
1722
1723 $self->focus_in;
1724
1725 if ($ev->button == 2) {
1726 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1727 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1728
1729 $self->{motion} = sub {
1730 my ($ev, $x, $y) = @_;
1731
1732 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1733
1734 $::CFG->{map_shift_x} = $bw + $x - $ox;
1735 $::CFG->{map_shift_y} = $bh + $y - $oy;
1736
1737 $self->update;
1738 };
1739 }
1740}
1741
1742sub button_up {
1743 my ($self, $ev, $x, $y) = @_;
1744
1745 delete $self->{motion};
1746}
1747
1748sub mouse_motion {
1749 my ($self, $ev, $x, $y) = @_;
1750
1751 $self->{motion}->($ev, $x, $y) if $self->{motion};
1752}
1753
1754sub size_request {
1755 (
1756 1 + 32 * int $::WIDTH / 32,
1757 1 + 32 * int $::HEIGHT / 32,
1758 )
1759}
1760
1761sub update {
1762 my ($self) = @_;
1763
1764 $self->{need_update} = 1;
1765 $self->SUPER::update;
1766}
1767
1768sub draw {
1769 my ($self) = @_;
1770
1771 if (delete $self->{need_update}) {
1772 glNewList $self->{list}, GL_COMPILE;
1773
1774 if ($::MAP) {
1775 my $sw = int $::WIDTH / 32;
1776 my $sh = int $::HEIGHT / 32;
1777
1778 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1779 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1780
1781 glTranslate $sx0 - 32, $sy0 - 32, 0;
1782
1783 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1784
1785 if ($::CFG->{fow_enable}) {
1786 if ($::CFG->{fow_smooth}) { # smooth fog of war
1787 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1788 glConvolutionFilter2D
1789 GL_CONVOLUTION_2D,
1790 GL_ALPHA,
1791 3, 3,
1792 GL_ALPHA, GL_FLOAT,
1793 pack "f*",
1794 0.1, 0.1, 0.1,
1795 0.1, 0.2, 0.1,
1796 0.1, 0.1, 0.1,
1797 ;
1798 glEnable GL_CONVOLUTION_2D;
1799 }
1800
1801 $self->{fow_texture} = new CFClient::Texture
1802 w => $w,
1803 h => $h,
1804 data => $data,
1805 internalformat => GL_ALPHA,
1806 format => GL_ALPHA;
1807
1808 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1809
1810 glEnable GL_BLEND;
1811 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1812 glEnable GL_TEXTURE_2D;
1813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1814
1815 glColor +($::CFG->{fow_intensity}) x 3, 1;
1816 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1817
1818 glDisable GL_TEXTURE_2D;
1819 glDisable GL_BLEND;
1820 }
1821
1822 # HACK BEGIN
1823 {
1824 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1825 my ($w, $h) = (250, 250);
1826
1827 glEnable GL_BLEND;
1828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1829 glEnable GL_TEXTURE_2D;
1830 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1831
1832 $self->{mapmap_texture} =
1833 new CFClient::Texture
1834 w => $w,
1835 h => $h,
1836 data => $::MAP->mapmap ($w, $h),
1837 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1838
1839 $self->{mapmap_texture}->draw_quad (100, 100);
1840
1841 glDisable GL_TEXTURE_2D;
1842 glDisable GL_BLEND;
1843 }
1844 # HACK END
1845 }
1846
1847 glEndList;
1848 }
1849
1850 glPushMatrix;
1851 glCallList $self->{list};
1852 glPopMatrix;
1853
1854 if ($FOCUS != $self) {
1855 glColor 64/255, 64/255, 64/255;
1856 glLogicOp GL_AND;
1857 glEnable GL_COLOR_LOGIC_OP;
1858 glBegin GL_QUADS;
1859 glVertex 0, 0;
1860 glVertex 0, $::HEIGHT;
1861 glVertex $::WIDTH, $::HEIGHT;
1862 glVertex $::WIDTH, 0;
1863 glEnd;
1864 glDisable GL_COLOR_LOGIC_OP;
1865 }
1866}
1867
1868my %DIR = (
1869 SDLK_KP8, [1, "north"],
1870 SDLK_KP9, [2, "northeast"],
1871 SDLK_KP6, [3, "east"],
1872 SDLK_KP3, [4, "southeast"],
1873 SDLK_KP2, [5, "south"],
1874 SDLK_KP1, [6, "southwest"],
1875 SDLK_KP4, [7, "west"],
1876 SDLK_KP7, [8, "northwest"],
1877
1878 SDLK_UP, [1, "north"],
1879 SDLK_RIGHT, [3, "east"],
1880 SDLK_DOWN, [5, "south"],
1881 SDLK_LEFT, [7, "west"],
1882);
1883
1884sub key_down {
1885 my ($self, $ev) = @_;
1886
1887 my $mod = $ev->key_mod;
1888 my $sym = $ev->key_sym;
1889
1890 if ($sym == SDLK_KP5) {
1891 $::CONN->user_send ("stay fire");
1892 } elsif ($sym == SDLK_a) {
1893 $::CONN->user_send ("apply");
1894 } elsif ($sym == SDLK_QUOTE) {
1895 $self->emit ('activate_console');
1896 } elsif ($sym == SDLK_SLASH) {
1897 $self->emit ('activate_console' => '/');
1898 } elsif (exists $DIR{$sym}) {
1899 if ($mod & KMOD_SHIFT) {
1900 $self->{shft}++;
1901 $::CONN->user_send ("fire $DIR{$sym}[0]");
1902 } elsif ($mod & KMOD_CTRL) {
1903 $self->{ctrl}++;
1904 $::CONN->user_send ("run $DIR{$sym}[0]");
1905 } else {
1906 $::CONN->user_send ("$DIR{$sym}[1]");
1907 }
1908 }
1909}
1910
1911sub key_up {
1912 my ($self, $ev) = @_;
1913
1914 my $mod = $ev->key_mod;
1915 my $sym = $ev->key_sym;
1916
1917 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1918 $::CONN->user_send ("fire_stop");
1919 }
1920 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1921 $::CONN->user_send ("run_stop");
1922 }
1923}
1924
1925#############################################################################
1926
1927package CFClient::UI::Animator; 1910package CFClient::UI::Animator;
1928 1911
1929use SDL::OpenGL; 1912use CFClient::OpenGL;
1930 1913
1931our @ISA = CFClient::UI::Bin::; 1914our @ISA = CFClient::UI::Bin::;
1932 1915
1933sub moveto { 1916sub moveto {
1934 my ($self, $x, $y) = @_; 1917 my ($self, $x, $y) = @_;
2007 1990
2008package CFClient::UI::Root; 1991package CFClient::UI::Root;
2009 1992
2010our @ISA = CFClient::UI::Container::; 1993our @ISA = CFClient::UI::Container::;
2011 1994
2012use SDL::OpenGL; 1995use CFClient::OpenGL;
1996
1997sub check_size {
1998 my ($self) = @_;
1999
2000 $self->configure (0, 0, $::WITH, $::HEIGHT);
2001}
2013 2002
2014sub size_request { 2003sub size_request {
2015 ($::WIDTH, $::HEIGHT) 2004 ($::WIDTH, $::HEIGHT)
2016} 2005}
2017 2006
2018sub size_allocate { 2007sub configure {
2019 my ($self, $x, $y, $w, $h) = @_; 2008 my ($self, $x, $y, $w, $h) = @_;
2020 2009
2021 $self->_size_allocate ($x, $y, $w, $h); 2010 $self->SUPER::configure ($x, $y, $w, $h);
2022 2011
2023 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2024 for @{$self->{children}}; 2013 for @{$self->{children}};
2025} 2014}
2026 2015
2027sub _topleft { 2016sub _topleft {
2028 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
2031} 2020}
2032 2021
2033sub update { 2022sub update {
2034 my ($self) = @_; 2023 my ($self) = @_;
2035 2024
2036 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2025 $self->check_size;
2037 ::refresh (); 2026 ::refresh ();
2038} 2027}
2039 2028
2040sub add { 2029sub add {
2041 my ($self, $widget) = @_; 2030 my ($self, $child) = @_;
2042 2031
2043 $self->SUPER::add ($widget); 2032 $self->SUPER::add ($child);
2044
2045 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2046} 2033}
2047 2034
2048sub on_refresh { 2035sub on_refresh {
2049 my ($self, $id, $cb) = @_; 2036 my ($self, $id, $cb) = @_;
2050 2037

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines