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.127 by root, Mon Apr 17 21:03:31 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) = @_;
284} 315}
285 316
286sub connect { 317sub connect {
287 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
288 319
289 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 321}
291 322
292sub emit { 323sub emit {
293 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
294 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
295 $_->($self, @args) 327 $cb->($self, @args);
296 for @{$self->{cb}{$signal} || []}; 328 }
297} 329}
298 330
299sub DESTROY { 331sub DESTROY {
300 my ($self) = @_; 332 my ($self) = @_;
301 333
307package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
308 340
309our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
310 342
311use strict; 343use strict;
312use SDL::OpenGL; 344use CFClient::OpenGL;
313 345
314sub new { 346sub new {
315 my $class = shift; 347 my $class = shift;
316 348
317 # range [value, low, high, page] 349 # range [value, low, high, page]
381 $self->{children} = [ 413 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 415 @{$self->{children}}, $child
384 ]; 416 ];
385 417
386 $self->{w} = $self->{h} = -1;
387
388 $child->check_size; 418 $child->check_size;
389} 419}
390 420
391sub remove { 421sub remove {
392 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
393 423
424 delete $child->{parent};
425
394 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
395 427
396 $self->check_size; 428 $self->check_size;
397} 429}
398 430
399sub find_widget { 431sub find_widget {
431 463
432 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
433} 465}
434 466
435sub add { 467sub add {
436 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
437 469
438 $self->{children} = []; 470 $self->{children} = [];
439 471
440 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
441} 473}
442 474
443sub remove { 475sub remove {
444 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
445 477
454sub size_request { 486sub size_request {
455 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
456} 488}
457 489
458sub size_allocate { 490sub size_allocate {
459 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
460 492
461 $self->_size_allocate ($x, $y, $w, $h) or return;
462
463 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
464} 494}
465 495
466############################################################################# 496#############################################################################
467 497
468package CFClient::UI::Window; 498package CFClient::UI::Window;
469 499
470our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
471 501
472use SDL::OpenGL; 502use CFClient::OpenGL;
473 503
474sub new { 504sub new {
475 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
476 506
477 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
494 $self->child->draw; 524 $self->child->draw;
495 }; 525 };
496} 526}
497 527
498sub size_allocate { 528sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
500 530
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
504 532
505 $self->render_chld; 533 $self->render_chld;
506} 534}
507 535
508sub _draw { 536sub _draw {
539 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540 568
541 @$self{qw(child_w child_h)} 569 @$self{qw(child_w child_h)}
542} 570}
543 571
544sub size_allocate {
545 my ($self, $x, $y, $w, $h) = @_;
546
547 $self->_size_allocate ($x, $y, $w, $h) or return;
548}
549
550sub _draw { 572sub _draw {
551 my ($self) = @_; 573 my ($self) = @_;
552 574
553 $self->{children}[1]->draw; 575 $self->{children}[1]->draw;
554} 576}
558 580
559package CFClient::UI::Frame; 581package CFClient::UI::Frame;
560 582
561our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
562 584
563use SDL::OpenGL; 585use CFClient::OpenGL;
564 586
565sub size_request { 587sub size_request {
566 my ($self) = @_; 588 my ($self) = @_;
567 my $chld = $self->child 589 my $chld = $self->child
568 or return (0, 0); 590 or return (0, 0);
573} 595}
574 596
575sub size_allocate { 597sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 598 my ($self, $x, $y, $w, $h) = @_;
577 599
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 600 $self->child->configure (2, 2, $w - 4, $h - 4);
581} 601}
582 602
583sub _draw { 603sub _draw {
584 my ($self) = @_; 604 my ($self) = @_;
585 605
602 622
603package CFClient::UI::FancyFrame; 623package CFClient::UI::FancyFrame;
604 624
605our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
606 626
607use SDL::OpenGL; 627use CFClient::OpenGL;
608 628
609my @tex = 629my @tex =
610 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
611 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);
612 632
613sub new { 633sub new {
614 my $class = shift; 634 my $class = shift;
615 635
616 # TODO: user_x, user_y, overwrite moveto? 636 # TODO: user_x, user_y, overwrite moveto?
617 637
618 $class->SUPER::new ( 638 my $self = $class->SUPER::new (
619 bg => [1, 1, 1, 1], 639 bg => [1, 1, 1, 1],
620 border_bg => [1, 1, 1, 1], 640 border_bg => [1, 1, 1, 1],
621 border => int $::FONTSIZE * 0.8, 641 border => 0.8,
622 @_ 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,
623 ) 668 )
624} 669}
625 670
626sub size_request {
627 my ($self) = @_;
628
629 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
630
631 my ($w, $h) = $self->SUPER::size_request;
632
633 (
634 $w + $self->{border} * 2,
635 $h + $self->{border} * 2,
636 )
637}
638
639sub size_allocate { 671sub size_allocate {
640 my ($self, $x, $y, $w, $h) = @_; 672 my ($self, $w, $h) = @_;
641 673
642 $self->_size_allocate ($x, $y, $w, $h) or return;
643
644 $h -= List::Util::max 0, $self->{border} * 2; 674 $h -= List::Util::max 0, $self->border * 2;
645 $w -= List::Util::max 0, $self->{border} * 2; 675 $w -= List::Util::max 0, $self->border * 2;
646 676
677 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
678 if $self->{title};
679
647 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 680 $self->child->configure ($self->border, $self->border, $w, $h);
648} 681}
649 682
650sub button_down { 683sub button_down {
651 my ($self, $ev, $x, $y) = @_; 684 my ($self, $ev, $x, $y) = @_;
652 685
686 my $border = $self->border;
687
653 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 688 if ($x < $self->{w} && $x >= $self->{w} - $border
654 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 689 && $y < $self->{h} && $y >= $self->{h} - $border) {
655 690
656 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 691 my ($ox, $oy) = ($ev->{x}, $ev->{y});
657 my ($bw, $bh) = ($self->{w}, $self->{h}); 692 my ($bw, $bh) = ($self->{w}, $self->{h});
658 693
659 $self->{motion} = sub { 694 $self->{motion} = sub {
660 my ($ev, $x, $y) = @_; 695 my ($ev, $x, $y) = @_;
661 696
662 ($x, $y) = ($ev->motion_x, $ev->motion_y); 697 ($x, $y) = ($ev->{x}, $ev->{y});
663 698
664 $self->{user_w} = $bw + $x - $ox; 699 $self->{user_w} = $bw + $x - $ox;
665 $self->{user_h} = $bh + $y - $oy; 700 $self->{user_h} = $bh + $y - $oy;
666 $self->update; 701 $self->check_size;
667 }; 702 };
668 703
669 } elsif ($x >= 0 && $x < $self->{w} 704 } elsif ($x >= 0 && $x < $self->{w}
670 && $y >= 0 && $y < $self->{border}) { 705 && $y >= 0 && $y < $border) {
671 706
672 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 707 my ($ox, $oy) = ($ev->{x}, $ev->{y});
673 my ($bx, $by) = ($self->{x}, $self->{y}); 708 my ($bx, $by) = ($self->{x}, $self->{y});
674 709
675 $self->{motion} = sub { 710 $self->{motion} = sub {
676 my ($ev, $x, $y) = @_; 711 my ($ev, $x, $y) = @_;
677 712
678 ($x, $y) = ($ev->motion_x, $ev->motion_y); 713 ($x, $y) = ($ev->{x}, $ev->{y});
679 714
680 $self->move ($bx + $x - $ox, $by + $y - $oy); 715 $self->move ($bx + $x - $ox, $by + $y - $oy);
681 $self->update; 716 $self->update;
682 }; 717 };
683 } 718 }
704 glEnable GL_BLEND; 739 glEnable GL_BLEND;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 740 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
708 743
744 my $border = $self->border;
745
709 glColor @{ $self->{border_bg} }; 746 glColor @{ $self->{border_bg} };
710 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 747 $tex[1]->draw_quad (0, 0, $w, $border);
711 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 748 $tex[3]->draw_quad (0, $border, $border, $ch);
712 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 749 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
713 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 750 $tex[4]->draw_quad (0, $h - $border, $w, $border);
714 751
715 my $bg = $tex[0]; 752 my $bg = $tex[0];
716 753
717 # TODO: repeat texture not scale 754 # TODO: repeat texture not scale
718 my $rep_x = $cw / $bg->{w}; 755 my $rep_x = $cw / $bg->{w};
721 glColor @{ $self->{bg} }; 758 glColor @{ $self->{bg} };
722 759
723 $bg->{s} = $rep_x; 760 $bg->{s} = $rep_x;
724 $bg->{t} = $rep_y; 761 $bg->{t} = $rep_y;
725 $bg->{wrap_mode} = 1; 762 $bg->{wrap_mode} = 1;
726 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 763 $bg->draw_quad ($border, $border, $cw, $ch);
727 764
728 glDisable GL_TEXTURE_2D; 765 glDisable GL_TEXTURE_2D;
729 glDisable GL_BLEND; 766 glDisable GL_BLEND;
730 767
768 $self->{title}->draw if $self->{title};
731 $self->child->draw; 769 $self->child->draw;
732} 770}
733 771
734############################################################################# 772#############################################################################
735 773
737 775
738our @ISA = CFClient::UI::Base::; 776our @ISA = CFClient::UI::Base::;
739 777
740use List::Util qw(max sum); 778use List::Util qw(max sum);
741 779
742use SDL::OpenGL; 780use CFClient::OpenGL;
743 781
744sub new { 782sub new {
745 my $class = shift; 783 my $class = shift;
746 784
747 $class->SUPER::new ( 785 $class->SUPER::new (
754 my ($self, $x, $y, $child) = @_; 792 my ($self, $x, $y, $child) = @_;
755 793
756 $child->set_parent ($self); 794 $child->set_parent ($self);
757 $self->{children}[$y][$x] = $child; 795 $self->{children}[$y][$x] = $child;
758 796
759 $self->{w} = $self->{h} = -1;
760
761 $child->check_size; 797 $child->check_size;
762} 798}
763 799
764# 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?
765sub clear { 801sub clear {
801 (sum @$hs), 837 (sum @$hs),
802 ) 838 )
803} 839}
804 840
805sub size_allocate { 841sub size_allocate {
806 my ($self, $x, $y, $w, $h) = @_; 842 my ($self, $w, $h) = @_;
807
808 $self->_size_allocate ($x, $y, $w, $h) or return;
809 843
810 my ($ws, $hs) = $self->get_wh; 844 my ($ws, $hs) = $self->get_wh;
811 845
812 my $req_w = sum @$ws; 846 my $req_w = sum @$ws;
813 my $req_h = sum @$hs; 847 my $req_h = sum @$hs;
835 869
836 for my $c (0 .. $#$row) { 870 for my $c (0 .. $#$row) {
837 my $col_w = $ws->[$c]; 871 my $col_w = $ws->[$c];
838 872
839 if (my $widget = $row->[$c]) { 873 if (my $widget = $row->[$c]) {
840 $widget->size_allocate ($x, $y, $col_w, $row_h); 874 $widget->configure ($x, $y, $col_w, $row_h);
841 } 875 }
842 876
843 $x += $col_w; 877 $x += $col_w;
844 } 878 }
845 879
890 (List::Util::max map $_->[1], @alloc), 924 (List::Util::max map $_->[1], @alloc),
891 ) 925 )
892} 926}
893 927
894sub size_allocate { 928sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 929 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h) or return;
898 930
899 ($h, $w) = ($w, $h); 931 ($h, $w) = ($w, $h);
900 932
901 my $children = $self->{children}; 933 my $children = $self->{children};
902 934
924 956
925 my $y = 0; 957 my $y = 0;
926 for (0 .. $#$children) { 958 for (0 .. $#$children) {
927 my $child = $children->[$_]; 959 my $child = $children->[$_];
928 my $h = $h[$_]; 960 my $h = $h[$_];
929 $child->size_allocate ($y, 0, $h, $w); 961 $child->configure ($y, 0, $h, $w);
930 962
931 $y += $h; 963 $y += $h;
932 } 964 }
933 965
934 1 966 1
952 (List::Util::sum map $_->[1], @alloc), 984 (List::Util::sum map $_->[1], @alloc),
953 ) 985 )
954} 986}
955 987
956sub size_allocate { 988sub size_allocate {
957 my ($self, $x, $y, $w, $h) = @_; 989 my ($self, $w, $h) = @_;
958
959 $self->_size_allocate ($x, $y, $w, $h) or return;
960 990
961 my $children = $self->{children}; 991 my $children = $self->{children};
962 992
963 my @h = map +($_->size_request)[1], @$children; 993 my @h = map +($_->size_request)[1], @$children;
964 994
982 1012
983 my $y = 0; 1013 my $y = 0;
984 for (0 .. $#$children) { 1014 for (0 .. $#$children) {
985 my $child = $children->[$_]; 1015 my $child = $children->[$_];
986 my $h = $h[$_]; 1016 my $h = $h[$_];
987 $child->size_allocate (0, $y, $w, $h); 1017 $child->configure (0, $y, $w, $h);
988 1018
989 $y += $h; 1019 $y += $h;
990 } 1020 }
991 1021
992 1 1022 1
996 1026
997package CFClient::UI::Label; 1027package CFClient::UI::Label;
998 1028
999our @ISA = CFClient::UI::Base::; 1029our @ISA = CFClient::UI::Base::;
1000 1030
1001use SDL::OpenGL; 1031use CFClient::OpenGL;
1002 1032
1003sub new { 1033sub new {
1004 my ($class, %arg) = @_; 1034 my ($class, %arg) = @_;
1005 1035
1006 my $self = $class->SUPER::new ( 1036 my $self = $class->SUPER::new (
1007 fg => [1, 1, 1], 1037 fg => [1, 1, 1],
1008 fontsize => $::FONTSIZE, 1038 fontsize => 1,
1009 text => "", 1039 text => "",
1010 align => -1, 1040 align => -1,
1011 valign => -1, 1041 valign => -1,
1012 padding => 2, 1042 padding => 2,
1013 layout => new CFClient::Layout, 1043 layout => new CFClient::Layout,
1014 %arg 1044 %arg
1015 ); 1045 );
1016 1046
1017 $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 }
1018 1052
1019 $self->set_text (delete $self->{text}) if exists $self->{text}; 1053 $self->set_text (delete $self->{text}) if exists $self->{text};
1020 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1054 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1021 1055
1022 $self 1056 $self
1052 1086
1053sub size_request { 1087sub size_request {
1054 my ($self) = @_; 1088 my ($self) = @_;
1055 1089
1056 $self->{layout}->set_width; 1090 $self->{layout}->set_width;
1057 $self->{layout}->set_height ($self->{fontsize}); 1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1058 1092
1059 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 }
1060 1103
1061 ( 1104 (
1062 $w + $self->{padding} * 2, 1105 $w + $self->{padding} * 2,
1063 $h + $self->{padding} * 2, 1106 $h + $self->{padding} * 2,
1064 ) 1107 )
1065} 1108}
1066 1109
1067sub size_allocate { 1110sub size_allocate {
1068 my ($self, $x, $y, $w, $h) = @_; 1111 my ($self, $w, $h) = @_;
1069
1070 $self->_size_allocate ($x, $y, $w, $h) or return;
1071 1112
1072 delete $self->{texture}; 1113 delete $self->{texture};
1073}
1074
1075sub update {
1076 my ($self) = @_;
1077
1078 delete $self->{texture};
1079 $self->SUPER::update;
1080} 1114}
1081 1115
1082sub _draw { 1116sub _draw {
1083 my ($self) = @_; 1117 my ($self) = @_;
1084 1118
1085 my $tex = $self->{texture} ||= do { 1119 my $tex = $self->{texture} ||= do {
1086 $self->{layout}->set_width ($self->{w}); 1120 $self->{layout}->set_width ($self->{w});
1087 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1121 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1088 new_from_layout CFClient::Texture $self->{layout} 1122 new_from_layout CFClient::Texture $self->{layout}
1089 }; 1123 };
1090 1124
1091 glEnable GL_BLEND; 1125 glEnable GL_BLEND;
1092 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1126 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1117 1151
1118package CFClient::UI::EntryBase; 1152package CFClient::UI::EntryBase;
1119 1153
1120our @ISA = CFClient::UI::Label::; 1154our @ISA = CFClient::UI::Label::;
1121 1155
1122use SDL; 1156use CFClient::OpenGL;
1123use SDL::OpenGL;
1124 1157
1125sub new { 1158sub new {
1126 my $class = shift; 1159 my $class = shift;
1127 1160
1128 $class->SUPER::new ( 1161 $class->SUPER::new (
1142 1175
1143 delete $self->{cur_h}; 1176 delete $self->{cur_h};
1144 1177
1145 return if $self->{text} eq $text; 1178 return if $self->{text} eq $text;
1146 1179
1180 delete $self->{texture};
1181
1147 $self->{last_activity} = $::NOW; 1182 $self->{last_activity} = $::NOW;
1148 $self->{text} = $text; 1183 $self->{text} = $text;
1149 1184
1150 $text =~ s/./*/g if $self->{hidden}; 1185 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text "); 1186 $self->{layout}->set_text ("$text ");
1164 1199
1165 ($w + 1, $h) # add 1 for cursor 1200 ($w + 1, $h) # add 1 for cursor
1166} 1201}
1167 1202
1168sub size_allocate { 1203sub size_allocate {
1169 my ($self, $x, $y, $w, $h) = @_; 1204 my ($self, $w, $h) = @_;
1170
1171 $self->SUPER::size_allocate ($x, $y, $w, $h) or return;
1172 1205
1173 $self->_set_text ($self->{text}); 1206 $self->_set_text ($self->{text});
1174} 1207}
1175 1208
1176sub set_text { 1209sub set_text {
1182} 1215}
1183 1216
1184sub key_down { 1217sub key_down {
1185 my ($self, $ev) = @_; 1218 my ($self, $ev) = @_;
1186 1219
1187 my $mod = $ev->key_mod; 1220 my $mod = $ev->{mod};
1188 my $sym = $ev->key_sym; 1221 my $sym = $ev->{sym};
1189
1190 my $uni = $ev->key_unicode; 1222 my $uni = $ev->{unicode};
1191 1223
1192 my $text = $self->get_text; 1224 my $text = $self->get_text;
1193 1225
1194 if ($sym == SDLK_BACKSPACE) { 1226 if ($sym == 8) {
1195 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1227 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1196 } elsif ($sym == SDLK_DELETE) { 1228 } elsif ($sym == 127) {
1197 substr $text, $self->{cursor}, 1, ""; 1229 substr $text, $self->{cursor}, 1, "";
1198 } elsif ($sym == SDLK_LEFT) { 1230 } elsif ($sym == CFClient::SDLK_LEFT) {
1199 --$self->{cursor} if $self->{cursor}; 1231 --$self->{cursor} if $self->{cursor};
1200 } elsif ($sym == SDLK_RIGHT) { 1232 } elsif ($sym == CFClient::SDLK_RIGHT) {
1201 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1233 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1202 } elsif ($sym == SDLK_HOME) { 1234 } elsif ($sym == CFClient::SDLK_HOME) {
1203 $self->{cursor} = 0; 1235 $self->{cursor} = 0;
1204 } elsif ($sym == SDLK_END) { 1236 } elsif ($sym == CFClient::SDLK_END) {
1205 $self->{cursor} = length $text; 1237 $self->{cursor} = length $text;
1206 } elsif ($sym == SDLK_ESCAPE) { 1238 } elsif ($sym == 27) {
1207 $self->emit ('escape'); 1239 $self->emit ('escape');
1208 } elsif ($uni) { 1240 } elsif ($uni) {
1209 substr $text, $self->{cursor}++, 0, chr $uni; 1241 substr $text, $self->{cursor}++, 0, chr $uni;
1210 } 1242 }
1211 1243
1286 1318
1287package CFClient::UI::Entry; 1319package CFClient::UI::Entry;
1288 1320
1289our @ISA = CFClient::UI::EntryBase::; 1321our @ISA = CFClient::UI::EntryBase::;
1290 1322
1291use SDL; 1323use CFClient::OpenGL;
1292use SDL::OpenGL;
1293 1324
1294sub key_down { 1325sub key_down {
1295 my ($self, $ev) = @_; 1326 my ($self, $ev) = @_;
1296 1327
1297 my $sym = $ev->key_sym; 1328 my $sym = $ev->{sym};
1298 1329
1299 if ($sym == SDLK_RETURN) { 1330 if ($sym == 13) {
1300 $self->emit (activate => $self->get_text); 1331 $self->emit (activate => $self->get_text);
1301 $self->update; 1332 $self->update;
1302 1333
1303 } else { 1334 } else {
1304 $self->SUPER::key_down ($ev); 1335 $self->SUPER::key_down ($ev);
1310 1341
1311package CFClient::UI::Button; 1342package CFClient::UI::Button;
1312 1343
1313our @ISA = CFClient::UI::Label::; 1344our @ISA = CFClient::UI::Label::;
1314 1345
1315use SDL; 1346use CFClient::OpenGL;
1316use SDL::OpenGL;
1317 1347
1318my @tex = 1348my @tex =
1319 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1320 qw(b1_button_active.png); 1350 qw(b1_button_active.png);
1321 1351
1374 1404
1375my @tex = 1405my @tex =
1376 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1377 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1407 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1378 1408
1379use SDL; 1409use CFClient::OpenGL;
1380use SDL::OpenGL;
1381 1410
1382sub new { 1411sub new {
1383 my $class = shift; 1412 my $class = shift;
1384 1413
1385 $class->SUPER::new ( 1414 $class->SUPER::new (
1396 my ($self) = @_; 1425 my ($self) = @_;
1397 1426
1398 ($self->{padding} * 2 + 6) x 2 1427 ($self->{padding} * 2 + 6) x 2
1399} 1428}
1400 1429
1401sub size_allocate {
1402 my ($self, $x, $y, $w, $h) = @_;
1403
1404 $self->_size_allocate ($x, $y, $w, $h) or return;
1405}
1406
1407sub button_down { 1430sub button_down {
1408 my ($self, $ev, $x, $y) = @_; 1431 my ($self, $ev, $x, $y) = @_;
1409 1432
1410 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1433 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1411 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1434 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1441 1464
1442package CFClient::UI::VGauge; 1465package CFClient::UI::VGauge;
1443 1466
1444our @ISA = CFClient::UI::Base::; 1467our @ISA = CFClient::UI::Base::;
1445 1468
1446use SDL::OpenGL; 1469use CFClient::OpenGL;
1447 1470
1448my %tex = ( 1471my %tex = (
1449 food => [ 1472 food => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1466 1489
1467# eg. VGauge->new (gauge => 'food'), default gauge: food 1490# eg. VGauge->new (gauge => 'food'), default gauge: food
1468sub new { 1491sub new {
1469 my $class = shift; 1492 my $class = shift;
1470 1493
1471 my $self = $class->SUPER::new (gauge => 'food', @_); 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};
1472 1500
1473 $self 1501 $self
1474} 1502}
1475 1503
1476sub size_request { 1504sub size_request {
1477 my ($self) = @_; 1505 my ($self) = @_;
1478 1506
1479 my $tex = $tex{$self->{gauge}}[0]; 1507 #my $tex = $tex{$self->{type}}[0];
1480
1481 @$tex{qw(w h)} 1508 #@$tex{qw(w h)}
1509 (0, 0)
1482} 1510}
1483 1511
1484sub set_max { 1512sub set_max {
1485 my ($self, $max) = @_; 1513 my ($self, $max) = @_;
1486 1514
1500} 1528}
1501 1529
1502sub _draw { 1530sub _draw {
1503 my ($self) = @_; 1531 my ($self) = @_;
1504 1532
1505 my $tex = $tex{$self->{gauge}}; 1533 my $tex = $tex{$self->{type}};
1506 1534
1507 my ($w, $h) = ($self->{w}, $self->{h}); 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 }
1508 1543
1509 my $ycut = $self->{val} / ($self->{max_val} || 1); 1544 my $ycut = $self->{val} / ($self->{max_val} || 1);
1510 $ycut = 1 if $self->{val} > $self->{max_val}; 1545 $ycut = 1 if $self->{val} > $self->{max_val};
1511 1546
1512 my $t1 = $tex->[0]; 1547 my $t1 = $tex->[0];
1518 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1519 1554
1520 my $h1 = $self->{h} - $ycut * $self->{h}; 1555 my $h1 = $self->{h} - $ycut * $self->{h};
1521 my $h2 = $ycut * $self->{h}; 1556 my $h2 = $ycut * $self->{h};
1522 1557
1523 my $yp = 0;
1524
1525 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1558 glBindTexture GL_TEXTURE_2D, $t1->{name};
1526 glBegin (GL_QUADS); 1559 glBegin GL_QUADS;
1527 glTexCoord (0, 0); glVertex (0 , $yp); 1560 glTexCoord 0 , 0; glVertex 0 , 0;
1528 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1561 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1529 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1562 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1530 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1563 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1531 glEnd (); 1564 glEnd;
1532 1565
1533 $yp += $h1;
1534
1535 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1566 glBindTexture GL_TEXTURE_2D, $t2->{name};
1536 glBegin (GL_QUADS); 1567 glBegin GL_QUADS;
1537 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1568 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1538 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1569 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1539 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1570 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1540 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1571 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1541 glEnd (); 1572 glEnd;
1542 1573
1543 glDisable GL_BLEND; 1574 glDisable GL_BLEND;
1544 glDisable GL_TEXTURE_2D; 1575 glDisable GL_TEXTURE_2D;
1545} 1576}
1546 1577
1547############################################################################# 1578#############################################################################
1548 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
1549package CFClient::UI::Slider; 1617package CFClient::UI::Slider;
1550 1618
1551use strict; 1619use strict;
1552 1620
1553use SDL::OpenGL; 1621use CFClient::OpenGL;
1554 1622
1555our @ISA = CFClient::UI::DrawBG::; 1623our @ISA = CFClient::UI::DrawBG::;
1556 1624
1557my @tex = 1625my @tex =
1558 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1568 # TODO: calculations are off 1636 # TODO: calculations are off
1569 my $self = $class->SUPER::new ( 1637 my $self = $class->SUPER::new (
1570 fg => [1, 1, 1], 1638 fg => [1, 1, 1],
1571 active_fg => [0, 0, 0], 1639 active_fg => [0, 0, 0],
1572 range => [0, 0, 100, 10], 1640 range => [0, 0, 100, 10],
1573 req_w => 40, 1641 req_w => 20,
1574 req_h => 13, 1642 req_h => 20,
1575 vertical => 0, 1643 vertical => 0,
1576 can_hover => 1, 1644 can_hover => 1,
1577 inner_pad => 5, 1645 inner_pad => 5,
1578 @_ 1646 @_
1579 ); 1647 );
1683 1751
1684package CFClient::UI::TextView; 1752package CFClient::UI::TextView;
1685 1753
1686our @ISA = CFClient::UI::HBox::; 1754our @ISA = CFClient::UI::HBox::;
1687 1755
1688use SDL::OpenGL; 1756use CFClient::OpenGL;
1689 1757
1690sub new { 1758sub new {
1691 my $class = shift; 1759 my $class = shift;
1692 1760
1693 my $self = $class->SUPER::new ( 1761 my $self = $class->SUPER::new (
1694 req_w => $::WIDTH / 6,
1695 req_h => $::HEIGHT / 6,
1696 fontsize => $::FONTSIZE, 1762 fontsize => 1,
1697 @_, 1763 @_,
1698 1764
1699 layout => (new CFClient::Layout), 1765 layout => (new CFClient::Layout),
1700 par => [], 1766 par => [],
1701 height => 0, 1767 height => 0,
1722sub text_height { 1788sub text_height {
1723 my ($self, $text) = @_; 1789 my ($self, $text) = @_;
1724 1790
1725 my $layout = $self->{layout}; 1791 my $layout = $self->{layout};
1726 1792
1727 $layout->set_height ($self->{fontsize}); 1793 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1728 $layout->set_width ($self->{w}); 1794 $layout->set_width ($self->{w});
1729 $layout->set_text ($text); 1795 $layout->set_text ($text);
1730 1796
1731 ($layout->size)[1] 1797 ($layout->size)[1]
1732} 1798}
1736 1802
1737 $self->{need_reflow}++; 1803 $self->{need_reflow}++;
1738 $self->update; 1804 $self->update;
1739} 1805}
1740 1806
1741sub size_request {
1742 my ($self) = @_;
1743
1744 ($self->{req_w}, $self->{req_h})
1745}
1746
1747sub size_allocate { 1807sub size_allocate {
1748 my ($self, $x, $y, $w, $h) = @_; 1808 my ($self, $w, $h) = @_;
1749 1809
1750 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1810 $self->SUPER::size_allocate ($w, $h);
1751 1811
1752 $self->{layout}->set_height ($self->{fontsize}); 1812 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1753 $self->{layout}->set_width ($self->{w}); 1813 $self->{layout}->set_width ($self->{children}[0]{w});
1754 1814
1755 $self->reflow; 1815 $self->reflow;
1756 $self->update;
1757} 1816}
1758 1817
1759sub add_paragraph { 1818sub add_paragraph {
1760 my ($self, $color, $text) = @_; 1819 my ($self, $color, $text) = @_;
1761 1820
1846 1905
1847} 1906}
1848 1907
1849############################################################################# 1908#############################################################################
1850 1909
1851package CFClient::UI::MapWidget;
1852
1853use strict;
1854
1855use List::Util qw(min max);
1856
1857use SDL;
1858use SDL::OpenGL;
1859
1860our @ISA = CFClient::UI::Base::;
1861
1862sub new {
1863 my $class = shift;
1864
1865 $class->SUPER::new (
1866 z => -1,
1867 can_focus => 1,
1868 list => (glGenLists 1),
1869 @_
1870 )
1871}
1872
1873sub key_down {
1874 print "MAPKEYDOWN\n";
1875}
1876
1877sub key_up {
1878}
1879
1880sub button_down {
1881 my ($self, $ev, $x, $y) = @_;
1882
1883 $self->focus_in;
1884
1885 if ($ev->button == 2) {
1886 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1887 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1888
1889 $self->{motion} = sub {
1890 my ($ev, $x, $y) = @_;
1891
1892 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1893
1894 $::CFG->{map_shift_x} = $bw + $x - $ox;
1895 $::CFG->{map_shift_y} = $bh + $y - $oy;
1896
1897 $self->update;
1898 };
1899 }
1900}
1901
1902sub button_up {
1903 my ($self, $ev, $x, $y) = @_;
1904
1905 delete $self->{motion};
1906}
1907
1908sub mouse_motion {
1909 my ($self, $ev, $x, $y) = @_;
1910
1911 $self->{motion}->($ev, $x, $y) if $self->{motion};
1912}
1913
1914sub size_request {
1915 (
1916 1 + 32 * int $::WIDTH / 32,
1917 1 + 32 * int $::HEIGHT / 32,
1918 )
1919}
1920
1921sub update {
1922 my ($self) = @_;
1923
1924 $self->{need_update} = 1;
1925 $self->SUPER::update;
1926}
1927
1928sub draw {
1929 my ($self) = @_;
1930
1931 if (delete $self->{need_update}) {
1932 glNewList $self->{list}, GL_COMPILE;
1933
1934 if ($::MAP) {
1935 my $sw = int $::WIDTH / 32;
1936 my $sh = int $::HEIGHT / 32;
1937
1938 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1939 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1940
1941 glTranslate $sx0 - 32, $sy0 - 32, 0;
1942
1943 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1944
1945 if ($::CFG->{fow_enable}) {
1946 if ($::CFG->{fow_smooth}) { # smooth fog of war
1947 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1948 glConvolutionFilter2D
1949 GL_CONVOLUTION_2D,
1950 GL_ALPHA,
1951 3, 3,
1952 GL_ALPHA, GL_FLOAT,
1953 pack "f*",
1954 0.1, 0.1, 0.1,
1955 0.1, 0.2, 0.1,
1956 0.1, 0.1, 0.1,
1957 ;
1958 glEnable GL_CONVOLUTION_2D;
1959 }
1960
1961 $self->{fow_texture} = new CFClient::Texture
1962 w => $w,
1963 h => $h,
1964 data => $data,
1965 internalformat => GL_ALPHA,
1966 format => GL_ALPHA;
1967
1968 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1969
1970 glEnable GL_BLEND;
1971 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1972 glEnable GL_TEXTURE_2D;
1973 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1974
1975 glColor +($::CFG->{fow_intensity}) x 3, 1;
1976 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1977
1978 glDisable GL_TEXTURE_2D;
1979 glDisable GL_BLEND;
1980 }
1981
1982 # HACK BEGIN
1983 {
1984 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1985 my ($w, $h) = (250, 250);
1986
1987 glEnable GL_BLEND;
1988 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1989 glEnable GL_TEXTURE_2D;
1990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1991
1992 $self->{mapmap_texture} =
1993 new CFClient::Texture
1994 w => $w,
1995 h => $h,
1996 data => $::MAP->mapmap ($w, $h),
1997 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1998
1999 $self->{mapmap_texture}->draw_quad (100, 100);
2000
2001 glDisable GL_TEXTURE_2D;
2002 glDisable GL_BLEND;
2003 }
2004 # HACK END
2005 }
2006
2007 glEndList;
2008 }
2009
2010 glPushMatrix;
2011 glCallList $self->{list};
2012 glPopMatrix;
2013
2014 if ($FOCUS != $self) {
2015 glColor 64/255, 64/255, 64/255;
2016 glLogicOp GL_AND;
2017 glEnable GL_COLOR_LOGIC_OP;
2018 glBegin GL_QUADS;
2019 glVertex 0, 0;
2020 glVertex 0, $::HEIGHT;
2021 glVertex $::WIDTH, $::HEIGHT;
2022 glVertex $::WIDTH, 0;
2023 glEnd;
2024 glDisable GL_COLOR_LOGIC_OP;
2025 }
2026}
2027
2028my %DIR = (
2029 SDLK_KP8, [1, "north"],
2030 SDLK_KP9, [2, "northeast"],
2031 SDLK_KP6, [3, "east"],
2032 SDLK_KP3, [4, "southeast"],
2033 SDLK_KP2, [5, "south"],
2034 SDLK_KP1, [6, "southwest"],
2035 SDLK_KP4, [7, "west"],
2036 SDLK_KP7, [8, "northwest"],
2037
2038 SDLK_UP, [1, "north"],
2039 SDLK_RIGHT, [3, "east"],
2040 SDLK_DOWN, [5, "south"],
2041 SDLK_LEFT, [7, "west"],
2042);
2043
2044sub key_down {
2045 my ($self, $ev) = @_;
2046
2047 my $mod = $ev->key_mod;
2048 my $sym = $ev->key_sym;
2049
2050 if ($sym == SDLK_KP5) {
2051 $::CONN->user_send ("stay fire");
2052 } elsif ($sym == SDLK_a) {
2053 $::CONN->user_send ("apply");
2054 } elsif ($sym == SDLK_QUOTE) {
2055 $self->emit ('activate_console');
2056 } elsif ($sym == SDLK_SLASH) {
2057 $self->emit ('activate_console' => '/');
2058 } elsif (exists $DIR{$sym}) {
2059 if ($mod & KMOD_SHIFT) {
2060 $self->{shft}++;
2061 $::CONN->user_send ("fire $DIR{$sym}[0]");
2062 } elsif ($mod & KMOD_CTRL) {
2063 $self->{ctrl}++;
2064 $::CONN->user_send ("run $DIR{$sym}[0]");
2065 } else {
2066 $::CONN->user_send ("$DIR{$sym}[1]");
2067 }
2068 }
2069}
2070
2071sub key_up {
2072 my ($self, $ev) = @_;
2073
2074 my $mod = $ev->key_mod;
2075 my $sym = $ev->key_sym;
2076
2077 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2078 $::CONN->user_send ("fire_stop");
2079 }
2080 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2081 $::CONN->user_send ("run_stop");
2082 }
2083}
2084
2085#############################################################################
2086
2087package CFClient::UI::Animator; 1910package CFClient::UI::Animator;
2088 1911
2089use SDL::OpenGL; 1912use CFClient::OpenGL;
2090 1913
2091our @ISA = CFClient::UI::Bin::; 1914our @ISA = CFClient::UI::Bin::;
2092 1915
2093sub moveto { 1916sub moveto {
2094 my ($self, $x, $y) = @_; 1917 my ($self, $x, $y) = @_;
2167 1990
2168package CFClient::UI::Root; 1991package CFClient::UI::Root;
2169 1992
2170our @ISA = CFClient::UI::Container::; 1993our @ISA = CFClient::UI::Container::;
2171 1994
2172use SDL::OpenGL; 1995use CFClient::OpenGL;
2173 1996
2174sub check_size { 1997sub check_size {
2175 my ($self) = @_; 1998 my ($self) = @_;
2176 1999
2177 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 2000 $self->configure (0, 0, $::WITH, $::HEIGHT);
2178} 2001}
2179 2002
2180sub size_request { 2003sub size_request {
2181 ($::WIDTH, $::HEIGHT) 2004 ($::WIDTH, $::HEIGHT)
2182} 2005}
2183 2006
2184sub size_allocate { 2007sub configure {
2185 my ($self, $x, $y, $w, $h) = @_; 2008 my ($self, $x, $y, $w, $h) = @_;
2186 2009
2187 $self->_size_allocate ($x, $y, $w, $h); 2010 $self->SUPER::configure ($x, $y, $w, $h);
2188 2011
2189 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2190 for @{$self->{children}}; 2013 for @{$self->{children}};
2191} 2014}
2192 2015
2193sub _topleft { 2016sub _topleft {
2194 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
2202 $self->check_size; 2025 $self->check_size;
2203 ::refresh (); 2026 ::refresh ();
2204} 2027}
2205 2028
2206sub add { 2029sub add {
2207 my ($self, $widget) = @_; 2030 my ($self, $child) = @_;
2208 2031
2209 $self->SUPER::add ($widget); 2032 $self->SUPER::add ($child);
2210
2211 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2212} 2033}
2213 2034
2214sub on_refresh { 2035sub on_refresh {
2215 my ($self, $id, $cb) = @_; 2036 my ($self, $id, $cb) = @_;
2216 2037

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines