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.126 by root, Mon Apr 17 20:59:20 2006 UTC vs.
Revision 1.137 by root, Wed Apr 19 21:38:05 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;
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
137 return unless $self->{w} != $w || $self->{h} != $h; 154 return unless $self->{w} != $w || $self->{h} != $h;
138 155
139 $self->{w} = $w; 156 $self->{w} = $w;
140 $self->{h} = $h; 157 $self->{h} = $h;
141 158
142 1 159 $self->size_allocate ($w, $h);
160 $self->update;
143} 161}
144 162
145sub size_allocate { 163sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 164 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 165}
150 166
151# return top left coordinates 167# return top left coordinates
152sub _topleft { 168sub _topleft {
153 my ($self, $x, $y) = @_; 169 my ($self, $x, $y) = @_;
284} 300}
285 301
286sub connect { 302sub connect {
287 my ($self, $signal, $cb) = @_; 303 my ($self, $signal, $cb) = @_;
288 304
289 push @{ $self->{cb}{$signal} }, $cb; 305 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 306}
291 307
292sub emit { 308sub emit {
293 my ($self, $signal, @args) = @_; 309 my ($self, $signal, @args) = @_;
294 310
311 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
295 $_->($self, @args) 312 $cb->($self, @args);
296 for @{$self->{cb}{$signal} || []}; 313 }
297} 314}
298 315
299sub DESTROY { 316sub DESTROY {
300 my ($self) = @_; 317 my ($self) = @_;
301 318
381 $self->{children} = [ 398 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 399 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 400 @{$self->{children}}, $child
384 ]; 401 ];
385 402
386 $self->{w} = $self->{h} = -1;
387
388 $child->check_size; 403 $child->check_size;
389} 404}
390 405
391sub remove { 406sub remove {
392 my ($self, $widget) = @_; 407 my ($self, $child) = @_;
393 408
409 delete $child->{parent};
410
394 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
395 412
396 $self->check_size; 413 $self->check_size;
397} 414}
398 415
399sub find_widget { 416sub find_widget {
431 448
432 $class->SUPER::new (children => [$child], %arg) 449 $class->SUPER::new (children => [$child], %arg)
433} 450}
434 451
435sub add { 452sub add {
436 my ($self, $widget) = @_; 453 my ($self, $child) = @_;
437 454
438 $self->{children} = []; 455 $self->{children} = [];
439 456
440 $self->SUPER::add ($widget); 457 $self->SUPER::add ($child);
441} 458}
442 459
443sub remove { 460sub remove {
444 my ($self, $widget) = @_; 461 my ($self, $widget) = @_;
445 462
454sub size_request { 471sub size_request {
455 $_[0]{children}[0]->size_request 472 $_[0]{children}[0]->size_request
456} 473}
457 474
458sub size_allocate { 475sub size_allocate {
459 my ($self, $x, $y, $w, $h) = @_; 476 my ($self, $w, $h) = @_;
460 477
461 $self->_size_allocate ($x, $y, $w, $h) or return;
462
463 $self->{children}[0]->size_allocate (0, 0, $w, $h); 478 $self->{children}[0]->configure (0, 0, $w, $h);
464} 479}
465 480
466############################################################################# 481#############################################################################
467 482
468package CFClient::UI::Window; 483package CFClient::UI::Window;
494 $self->child->draw; 509 $self->child->draw;
495 }; 510 };
496} 511}
497 512
498sub size_allocate { 513sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_; 514 my ($self, $w, $h) = @_;
500 515
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (0, 0, $w, $h); 516 $self->child->configure (0, 0, $w, $h);
504 517
505 $self->render_chld; 518 $self->render_chld;
506} 519}
507 520
508sub _draw { 521sub _draw {
539 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 552 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540 553
541 @$self{qw(child_w child_h)} 554 @$self{qw(child_w child_h)}
542} 555}
543 556
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 { 557sub _draw {
551 my ($self) = @_; 558 my ($self) = @_;
552 559
553 $self->{children}[1]->draw; 560 $self->{children}[1]->draw;
554} 561}
573} 580}
574 581
575sub size_allocate { 582sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 583 my ($self, $x, $y, $w, $h) = @_;
577 584
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 585 $self->child->configure (2, 2, $w - 4, $h - 4);
581} 586}
582 587
583sub _draw { 588sub _draw {
584 my ($self) = @_; 589 my ($self) = @_;
585 590
616 # TODO: user_x, user_y, overwrite moveto? 621 # TODO: user_x, user_y, overwrite moveto?
617 622
618 $class->SUPER::new ( 623 $class->SUPER::new (
619 bg => [1, 1, 1, 1], 624 bg => [1, 1, 1, 1],
620 border_bg => [1, 1, 1, 1], 625 border_bg => [1, 1, 1, 1],
621 border => int $::FONTSIZE * 0.8, 626 border => 0.8,
622 @_ 627 @_
623 ) 628 )
624} 629}
625 630
631sub border {
632 int $_[0]{border} * $::FONTSIZE
633}
634
626sub size_request { 635sub size_request {
627 my ($self) = @_; 636 my ($self) = @_;
628 637
629 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; 638 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
630 639
631 my ($w, $h) = $self->SUPER::size_request; 640 my ($w, $h) = $self->SUPER::size_request;
632 641
633 ( 642 (
634 $w + $self->{border} * 2, 643 $w + $self->border * 2,
635 $h + $self->{border} * 2, 644 $h + $self->border * 2,
636 ) 645 )
637} 646}
638 647
639sub size_allocate { 648sub size_allocate {
640 my ($self, $x, $y, $w, $h) = @_; 649 my ($self, $w, $h) = @_;
641 650
642 $self->_size_allocate ($x, $y, $w, $h) or return;
643
644 $h -= List::Util::max 0, $self->{border} * 2; 651 $h -= List::Util::max 0, $self->border * 2;
645 $w -= List::Util::max 0, $self->{border} * 2; 652 $w -= List::Util::max 0, $self->border * 2;
646 653
647 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 654 $self->child->configure ($self->border, $self->border, $w, $h);
648} 655}
649 656
650sub button_down { 657sub button_down {
651 my ($self, $ev, $x, $y) = @_; 658 my ($self, $ev, $x, $y) = @_;
652 659
660 my $border = $self->border;
661
653 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 662 if ($x < $self->{w} && $x >= $self->{w} - $border
654 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 663 && $y < $self->{h} && $y >= $self->{h} - $border) {
655 664
656 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 665 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
657 my ($bw, $bh) = ($self->{w}, $self->{h}); 666 my ($bw, $bh) = ($self->{w}, $self->{h});
658 667
659 $self->{motion} = sub { 668 $self->{motion} = sub {
661 670
662 ($x, $y) = ($ev->motion_x, $ev->motion_y); 671 ($x, $y) = ($ev->motion_x, $ev->motion_y);
663 672
664 $self->{user_w} = $bw + $x - $ox; 673 $self->{user_w} = $bw + $x - $ox;
665 $self->{user_h} = $bh + $y - $oy; 674 $self->{user_h} = $bh + $y - $oy;
666 $self->update; 675 $self->check_size;
667 }; 676 };
668 677
669 } elsif ($x >= 0 && $x < $self->{w} 678 } elsif ($x >= 0 && $x < $self->{w}
670 && $y >= 0 && $y < $self->{border}) { 679 && $y >= 0 && $y < $border) {
671 680
672 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 681 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
673 my ($bx, $by) = ($self->{x}, $self->{y}); 682 my ($bx, $by) = ($self->{x}, $self->{y});
674 683
675 $self->{motion} = sub { 684 $self->{motion} = sub {
704 glEnable GL_BLEND; 713 glEnable GL_BLEND;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 714 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
708 717
718 my $border = $self->border;
719
709 glColor @{ $self->{border_bg} }; 720 glColor @{ $self->{border_bg} };
710 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 721 $tex[1]->draw_quad (0, 0, $w, $border);
711 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 722 $tex[3]->draw_quad (0, $border, $border, $ch);
712 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 723 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
713 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 724 $tex[4]->draw_quad (0, $h - $border, $w, $border);
714 725
715 my $bg = $tex[0]; 726 my $bg = $tex[0];
716 727
717 # TODO: repeat texture not scale 728 # TODO: repeat texture not scale
718 my $rep_x = $cw / $bg->{w}; 729 my $rep_x = $cw / $bg->{w};
721 glColor @{ $self->{bg} }; 732 glColor @{ $self->{bg} };
722 733
723 $bg->{s} = $rep_x; 734 $bg->{s} = $rep_x;
724 $bg->{t} = $rep_y; 735 $bg->{t} = $rep_y;
725 $bg->{wrap_mode} = 1; 736 $bg->{wrap_mode} = 1;
726 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 737 $bg->draw_quad ($border, $border, $cw, $ch);
727 738
728 glDisable GL_TEXTURE_2D; 739 glDisable GL_TEXTURE_2D;
729 glDisable GL_BLEND; 740 glDisable GL_BLEND;
730 741
731 $self->child->draw; 742 $self->child->draw;
754 my ($self, $x, $y, $child) = @_; 765 my ($self, $x, $y, $child) = @_;
755 766
756 $child->set_parent ($self); 767 $child->set_parent ($self);
757 $self->{children}[$y][$x] = $child; 768 $self->{children}[$y][$x] = $child;
758 769
759 $self->{w} = $self->{h} = -1;
760
761 $child->check_size; 770 $child->check_size;
762} 771}
763 772
764# TODO: move to container class maybe? send childs a signal on removal? 773# TODO: move to container class maybe? send childs a signal on removal?
765sub clear { 774sub clear {
801 (sum @$hs), 810 (sum @$hs),
802 ) 811 )
803} 812}
804 813
805sub size_allocate { 814sub size_allocate {
806 my ($self, $x, $y, $w, $h) = @_; 815 my ($self, $w, $h) = @_;
807
808 $self->_size_allocate ($x, $y, $w, $h) or return;
809 816
810 my ($ws, $hs) = $self->get_wh; 817 my ($ws, $hs) = $self->get_wh;
811 818
812 my $req_w = sum @$ws; 819 my $req_w = sum @$ws;
813 my $req_h = sum @$hs; 820 my $req_h = sum @$hs;
835 842
836 for my $c (0 .. $#$row) { 843 for my $c (0 .. $#$row) {
837 my $col_w = $ws->[$c]; 844 my $col_w = $ws->[$c];
838 845
839 if (my $widget = $row->[$c]) { 846 if (my $widget = $row->[$c]) {
840 $widget->size_allocate ($x, $y, $col_w, $row_h); 847 $widget->configure ($x, $y, $col_w, $row_h);
841 } 848 }
842 849
843 $x += $col_w; 850 $x += $col_w;
844 } 851 }
845 852
890 (List::Util::max map $_->[1], @alloc), 897 (List::Util::max map $_->[1], @alloc),
891 ) 898 )
892} 899}
893 900
894sub size_allocate { 901sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 902 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h) or return;
898 903
899 ($h, $w) = ($w, $h); 904 ($h, $w) = ($w, $h);
900 905
901 my $children = $self->{children}; 906 my $children = $self->{children};
902 907
924 929
925 my $y = 0; 930 my $y = 0;
926 for (0 .. $#$children) { 931 for (0 .. $#$children) {
927 my $child = $children->[$_]; 932 my $child = $children->[$_];
928 my $h = $h[$_]; 933 my $h = $h[$_];
929 $child->size_allocate ($y, 0, $h, $w); 934 $child->configure ($y, 0, $h, $w);
930 935
931 $y += $h; 936 $y += $h;
932 } 937 }
933 938
934 1 939 1
952 (List::Util::sum map $_->[1], @alloc), 957 (List::Util::sum map $_->[1], @alloc),
953 ) 958 )
954} 959}
955 960
956sub size_allocate { 961sub size_allocate {
957 my ($self, $x, $y, $w, $h) = @_; 962 my ($self, $w, $h) = @_;
958
959 $self->_size_allocate ($x, $y, $w, $h) or return;
960 963
961 my $children = $self->{children}; 964 my $children = $self->{children};
962 965
963 my @h = map +($_->size_request)[1], @$children; 966 my @h = map +($_->size_request)[1], @$children;
964 967
982 985
983 my $y = 0; 986 my $y = 0;
984 for (0 .. $#$children) { 987 for (0 .. $#$children) {
985 my $child = $children->[$_]; 988 my $child = $children->[$_];
986 my $h = $h[$_]; 989 my $h = $h[$_];
987 $child->size_allocate (0, $y, $w, $h); 990 $child->configure (0, $y, $w, $h);
988 991
989 $y += $h; 992 $y += $h;
990 } 993 }
991 994
992 1 995 1
1003sub new { 1006sub new {
1004 my ($class, %arg) = @_; 1007 my ($class, %arg) = @_;
1005 1008
1006 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
1007 fg => [1, 1, 1], 1010 fg => [1, 1, 1],
1008 fontsize => $::FONTSIZE, 1011 fontsize => 1,
1009 text => "", 1012 text => "",
1010 align => -1, 1013 align => -1,
1011 valign => -1, 1014 valign => -1,
1012 padding => 2, 1015 padding => 2,
1013 layout => new CFClient::Layout, 1016 layout => new CFClient::Layout,
1052 1055
1053sub size_request { 1056sub size_request {
1054 my ($self) = @_; 1057 my ($self) = @_;
1055 1058
1056 $self->{layout}->set_width; 1059 $self->{layout}->set_width;
1057 $self->{layout}->set_height ($self->{fontsize}); 1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1058 1061
1059 my ($w, $h) = $self->{layout}->size; 1062 my ($w, $h) = $self->{layout}->size;
1060 1063
1061 ( 1064 (
1062 $w + $self->{padding} * 2, 1065 $w + $self->{padding} * 2,
1063 $h + $self->{padding} * 2, 1066 $h + $self->{padding} * 2,
1064 ) 1067 )
1065} 1068}
1066 1069
1067sub size_allocate { 1070sub size_allocate {
1068 my ($self, $x, $y, $w, $h) = @_; 1071 my ($self, $w, $h) = @_;
1069
1070 $self->_size_allocate ($x, $y, $w, $h) or return;
1071 1072
1072 delete $self->{texture}; 1073 delete $self->{texture};
1073}
1074
1075sub update {
1076 my ($self) = @_;
1077
1078 delete $self->{texture};
1079 $self->SUPER::update;
1080} 1074}
1081 1075
1082sub _draw { 1076sub _draw {
1083 my ($self) = @_; 1077 my ($self) = @_;
1084 1078
1085 my $tex = $self->{texture} ||= do { 1079 my $tex = $self->{texture} ||= do {
1086 $self->{layout}->set_width ($self->{w}); 1080 $self->{layout}->set_width ($self->{w});
1087 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1088 new_from_layout CFClient::Texture $self->{layout} 1082 new_from_layout CFClient::Texture $self->{layout}
1089 }; 1083 };
1090 1084
1091 glEnable GL_BLEND; 1085 glEnable GL_BLEND;
1092 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1086 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1117 1111
1118package CFClient::UI::EntryBase; 1112package CFClient::UI::EntryBase;
1119 1113
1120our @ISA = CFClient::UI::Label::; 1114our @ISA = CFClient::UI::Label::;
1121 1115
1122use SDL;
1123use SDL::OpenGL; 1116use SDL::OpenGL;
1124 1117
1125sub new { 1118sub new {
1126 my $class = shift; 1119 my $class = shift;
1127 1120
1142 1135
1143 delete $self->{cur_h}; 1136 delete $self->{cur_h};
1144 1137
1145 return if $self->{text} eq $text; 1138 return if $self->{text} eq $text;
1146 1139
1140 delete $self->{texture};
1141
1147 $self->{last_activity} = $::NOW; 1142 $self->{last_activity} = $::NOW;
1148 $self->{text} = $text; 1143 $self->{text} = $text;
1149 1144
1150 $text =~ s/./*/g if $self->{hidden}; 1145 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text "); 1146 $self->{layout}->set_text ("$text ");
1164 1159
1165 ($w + 1, $h) # add 1 for cursor 1160 ($w + 1, $h) # add 1 for cursor
1166} 1161}
1167 1162
1168sub size_allocate { 1163sub size_allocate {
1169 my ($self, $x, $y, $w, $h) = @_; 1164 my ($self, $w, $h) = @_;
1170
1171 $self->SUPER::size_allocate ($x, $y, $w, $h) or return;
1172 1165
1173 $self->_set_text ($self->{text}); 1166 $self->_set_text ($self->{text});
1174} 1167}
1175 1168
1176sub set_text { 1169sub set_text {
1182} 1175}
1183 1176
1184sub key_down { 1177sub key_down {
1185 my ($self, $ev) = @_; 1178 my ($self, $ev) = @_;
1186 1179
1187 my $mod = $ev->key_mod; 1180 my $mod = $ev->{mod};
1188 my $sym = $ev->key_sym; 1181 my $sym = $ev->{sym};
1189
1190 my $uni = $ev->key_unicode; 1182 my $uni = $ev->{unicode};
1191 1183
1192 my $text = $self->get_text; 1184 my $text = $self->get_text;
1193 1185
1194 if ($sym == SDLK_BACKSPACE) { 1186 if ($sym == 8) {
1195 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1187 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1196 } elsif ($sym == SDLK_DELETE) { 1188 } elsif ($sym == 127) {
1197 substr $text, $self->{cursor}, 1, ""; 1189 substr $text, $self->{cursor}, 1, "";
1198 } elsif ($sym == SDLK_LEFT) { 1190 } elsif ($sym == CFClient::SDLK_LEFT) {
1199 --$self->{cursor} if $self->{cursor}; 1191 --$self->{cursor} if $self->{cursor};
1200 } elsif ($sym == SDLK_RIGHT) { 1192 } elsif ($sym == CFClient::SDLK_RIGHT) {
1201 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1193 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1202 } elsif ($sym == SDLK_HOME) { 1194 } elsif ($sym == CFClient::SDLK_HOME) {
1203 $self->{cursor} = 0; 1195 $self->{cursor} = 0;
1204 } elsif ($sym == SDLK_END) { 1196 } elsif ($sym == CFClient::SDLK_END) {
1205 $self->{cursor} = length $text; 1197 $self->{cursor} = length $text;
1206 } elsif ($sym == SDLK_ESCAPE) { 1198 } elsif ($sym == 27) {
1207 $self->emit ('escape'); 1199 $self->emit ('escape');
1208 } elsif ($uni) { 1200 } elsif ($uni) {
1209 substr $text, $self->{cursor}++, 0, chr $uni; 1201 substr $text, $self->{cursor}++, 0, chr $uni;
1210 } 1202 }
1211 1203
1286 1278
1287package CFClient::UI::Entry; 1279package CFClient::UI::Entry;
1288 1280
1289our @ISA = CFClient::UI::EntryBase::; 1281our @ISA = CFClient::UI::EntryBase::;
1290 1282
1291use SDL;
1292use SDL::OpenGL; 1283use SDL::OpenGL;
1293 1284
1294sub key_down { 1285sub key_down {
1295 my ($self, $ev) = @_; 1286 my ($self, $ev) = @_;
1296 1287
1297 my $sym = $ev->key_sym; 1288 my $sym = $ev->{sym};
1298 1289
1299 if ($sym == SDLK_RETURN) { 1290 if ($sym == 13) {
1300 $self->emit (activate => $self->get_text); 1291 $self->emit (activate => $self->get_text);
1301 $self->update; 1292 $self->update;
1302 1293
1303 } else { 1294 } else {
1304 $self->SUPER::key_down ($ev); 1295 $self->SUPER::key_down ($ev);
1310 1301
1311package CFClient::UI::Button; 1302package CFClient::UI::Button;
1312 1303
1313our @ISA = CFClient::UI::Label::; 1304our @ISA = CFClient::UI::Label::;
1314 1305
1315use SDL;
1316use SDL::OpenGL; 1306use SDL::OpenGL;
1317 1307
1318my @tex = 1308my @tex =
1319 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1320 qw(b1_button_active.png); 1310 qw(b1_button_active.png);
1374 1364
1375my @tex = 1365my @tex =
1376 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1377 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1367 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1378 1368
1379use SDL;
1380use SDL::OpenGL; 1369use SDL::OpenGL;
1381 1370
1382sub new { 1371sub new {
1383 my $class = shift; 1372 my $class = shift;
1384 1373
1394 1383
1395sub size_request { 1384sub size_request {
1396 my ($self) = @_; 1385 my ($self) = @_;
1397 1386
1398 ($self->{padding} * 2 + 6) x 2 1387 ($self->{padding} * 2 + 6) x 2
1399}
1400
1401sub size_allocate {
1402 my ($self, $x, $y, $w, $h) = @_;
1403
1404 $self->_size_allocate ($x, $y, $w, $h) or return;
1405} 1388}
1406 1389
1407sub button_down { 1390sub button_down {
1408 my ($self, $ev, $x, $y) = @_; 1391 my ($self, $ev, $x, $y) = @_;
1409 1392
1474} 1457}
1475 1458
1476sub size_request { 1459sub size_request {
1477 my ($self) = @_; 1460 my ($self) = @_;
1478 1461
1479 (30, 100) 1462 my $tex = $tex{$self->{gauge}}[0];
1463
1464 @$tex{qw(w h)}
1480} 1465}
1481 1466
1482sub set_max { 1467sub set_max {
1483 my ($self, $max) = @_; 1468 my ($self, $max) = @_;
1469
1484 $self->{max_val} = $max; 1470 $self->{max_val} = $max;
1485} 1471}
1486 1472
1487sub set_value { 1473sub set_value {
1488 my ($self, $val, $max) = @_; 1474 my ($self, $val, $max) = @_;
1517 my $h1 = $self->{h} - $ycut * $self->{h}; 1503 my $h1 = $self->{h} - $ycut * $self->{h};
1518 my $h2 = $ycut * $self->{h}; 1504 my $h2 = $ycut * $self->{h};
1519 1505
1520 my $yp = 0; 1506 my $yp = 0;
1521 1507
1522 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1508 glBindTexture GL_TEXTURE_2D, $t1->{name};
1523 glBegin (GL_QUADS); 1509 glBegin GL_QUADS;
1524 glTexCoord (0, 0); glVertex (0 , $yp); 1510 glTexCoord 0 , 0; glVertex 0 , $yp;
1525 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1511 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1526 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1512 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1527 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1513 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1528 glEnd (); 1514 glEnd;
1529 1515
1530 $yp += $h1; 1516 $yp += $h1;
1531 1517
1532 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1518 glBindTexture GL_TEXTURE_2D, $t2->{name};
1533 glBegin (GL_QUADS); 1519 glBegin GL_QUADS;
1534 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1520 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1535 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1521 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1536 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1522 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1537 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1523 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1538 glEnd (); 1524 glEnd;
1539 1525
1540 glDisable GL_BLEND; 1526 glDisable GL_BLEND;
1541 glDisable GL_TEXTURE_2D; 1527 glDisable GL_TEXTURE_2D;
1542} 1528}
1543 1529
1565 # TODO: calculations are off 1551 # TODO: calculations are off
1566 my $self = $class->SUPER::new ( 1552 my $self = $class->SUPER::new (
1567 fg => [1, 1, 1], 1553 fg => [1, 1, 1],
1568 active_fg => [0, 0, 0], 1554 active_fg => [0, 0, 0],
1569 range => [0, 0, 100, 10], 1555 range => [0, 0, 100, 10],
1570 req_w => 40, 1556 req_w => 20,
1571 req_h => 13, 1557 req_h => 20,
1572 vertical => 0, 1558 vertical => 0,
1573 can_hover => 1, 1559 can_hover => 1,
1574 inner_pad => 5, 1560 inner_pad => 5,
1575 @_ 1561 @_
1576 ); 1562 );
1686 1672
1687sub new { 1673sub new {
1688 my $class = shift; 1674 my $class = shift;
1689 1675
1690 my $self = $class->SUPER::new ( 1676 my $self = $class->SUPER::new (
1691 req_w => $::WIDTH / 6,
1692 req_h => $::HEIGHT / 6,
1693 fontsize => $::FONTSIZE, 1677 fontsize => 1,
1694 @_, 1678 @_,
1695 1679
1696 layout => (new CFClient::Layout), 1680 layout => (new CFClient::Layout),
1697 par => [], 1681 par => [],
1698 height => 0, 1682 height => 0,
1719sub text_height { 1703sub text_height {
1720 my ($self, $text) = @_; 1704 my ($self, $text) = @_;
1721 1705
1722 my $layout = $self->{layout}; 1706 my $layout = $self->{layout};
1723 1707
1724 $layout->set_height ($self->{fontsize}); 1708 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1725 $layout->set_width ($self->{w}); 1709 $layout->set_width ($self->{w});
1726 $layout->set_text ($text); 1710 $layout->set_text ($text);
1727 1711
1728 ($layout->size)[1] 1712 ($layout->size)[1]
1729} 1713}
1733 1717
1734 $self->{need_reflow}++; 1718 $self->{need_reflow}++;
1735 $self->update; 1719 $self->update;
1736} 1720}
1737 1721
1738sub size_request {
1739 my ($self) = @_;
1740
1741 ($self->{req_w}, $self->{req_h})
1742}
1743
1744sub size_allocate { 1722sub size_allocate {
1745 my ($self, $x, $y, $w, $h) = @_; 1723 my ($self, $w, $h) = @_;
1746 1724
1747 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1725 $self->SUPER::size_allocate ($w, $h);
1748 1726
1749 $self->{layout}->set_height ($self->{fontsize}); 1727 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1750 $self->{layout}->set_width ($self->{w}); 1728 $self->{layout}->set_width ($self->{children}[0]{w});
1751 1729
1752 $self->reflow; 1730 $self->reflow;
1753 $self->update;
1754} 1731}
1755 1732
1756sub add_paragraph { 1733sub add_paragraph {
1757 my ($self, $color, $text) = @_; 1734 my ($self, $color, $text) = @_;
1758 1735
1839 glDisable GL_TEXTURE_2D; 1816 glDisable GL_TEXTURE_2D;
1840 } 1817 }
1841 1818
1842 $self->{children}[1]->draw; 1819 $self->{children}[1]->draw;
1843 1820
1844}
1845
1846#############################################################################
1847
1848package CFClient::UI::MapWidget;
1849
1850use strict;
1851
1852use List::Util qw(min max);
1853
1854use SDL;
1855use SDL::OpenGL;
1856
1857our @ISA = CFClient::UI::Base::;
1858
1859sub new {
1860 my $class = shift;
1861
1862 $class->SUPER::new (
1863 z => -1,
1864 can_focus => 1,
1865 list => (glGenLists 1),
1866 @_
1867 )
1868}
1869
1870sub key_down {
1871 print "MAPKEYDOWN\n";
1872}
1873
1874sub key_up {
1875}
1876
1877sub button_down {
1878 my ($self, $ev, $x, $y) = @_;
1879
1880 $self->focus_in;
1881
1882 if ($ev->button == 2) {
1883 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1884 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1885
1886 $self->{motion} = sub {
1887 my ($ev, $x, $y) = @_;
1888
1889 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1890
1891 $::CFG->{map_shift_x} = $bw + $x - $ox;
1892 $::CFG->{map_shift_y} = $bh + $y - $oy;
1893
1894 $self->update;
1895 };
1896 }
1897}
1898
1899sub button_up {
1900 my ($self, $ev, $x, $y) = @_;
1901
1902 delete $self->{motion};
1903}
1904
1905sub mouse_motion {
1906 my ($self, $ev, $x, $y) = @_;
1907
1908 $self->{motion}->($ev, $x, $y) if $self->{motion};
1909}
1910
1911sub size_request {
1912 (
1913 1 + 32 * int $::WIDTH / 32,
1914 1 + 32 * int $::HEIGHT / 32,
1915 )
1916}
1917
1918sub update {
1919 my ($self) = @_;
1920
1921 $self->{need_update} = 1;
1922 $self->SUPER::update;
1923}
1924
1925sub draw {
1926 my ($self) = @_;
1927
1928 if (delete $self->{need_update}) {
1929 glNewList $self->{list}, GL_COMPILE;
1930
1931 if ($::MAP) {
1932 my $sw = int $::WIDTH / 32;
1933 my $sh = int $::HEIGHT / 32;
1934
1935 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1936 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1937
1938 glTranslate $sx0 - 32, $sy0 - 32, 0;
1939
1940 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1941
1942 if ($::CFG->{fow_enable}) {
1943 if ($::CFG->{fow_smooth}) { # smooth fog of war
1944 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1945 glConvolutionFilter2D
1946 GL_CONVOLUTION_2D,
1947 GL_ALPHA,
1948 3, 3,
1949 GL_ALPHA, GL_FLOAT,
1950 pack "f*",
1951 0.1, 0.1, 0.1,
1952 0.1, 0.2, 0.1,
1953 0.1, 0.1, 0.1,
1954 ;
1955 glEnable GL_CONVOLUTION_2D;
1956 }
1957
1958 $self->{fow_texture} = new CFClient::Texture
1959 w => $w,
1960 h => $h,
1961 data => $data,
1962 internalformat => GL_ALPHA,
1963 format => GL_ALPHA;
1964
1965 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1966
1967 glEnable GL_BLEND;
1968 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1969 glEnable GL_TEXTURE_2D;
1970 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1971
1972 glColor +($::CFG->{fow_intensity}) x 3, 1;
1973 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1974
1975 glDisable GL_TEXTURE_2D;
1976 glDisable GL_BLEND;
1977 }
1978
1979 # HACK BEGIN
1980 {
1981 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1982 my ($w, $h) = (250, 250);
1983
1984 glEnable GL_BLEND;
1985 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1986 glEnable GL_TEXTURE_2D;
1987 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1988
1989 $self->{mapmap_texture} =
1990 new CFClient::Texture
1991 w => $w,
1992 h => $h,
1993 data => $::MAP->mapmap ($w, $h),
1994 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1995
1996 $self->{mapmap_texture}->draw_quad (100, 100);
1997
1998 glDisable GL_TEXTURE_2D;
1999 glDisable GL_BLEND;
2000 }
2001 # HACK END
2002 }
2003
2004 glEndList;
2005 }
2006
2007 glPushMatrix;
2008 glCallList $self->{list};
2009 glPopMatrix;
2010
2011 if ($FOCUS != $self) {
2012 glColor 64/255, 64/255, 64/255;
2013 glLogicOp GL_AND;
2014 glEnable GL_COLOR_LOGIC_OP;
2015 glBegin GL_QUADS;
2016 glVertex 0, 0;
2017 glVertex 0, $::HEIGHT;
2018 glVertex $::WIDTH, $::HEIGHT;
2019 glVertex $::WIDTH, 0;
2020 glEnd;
2021 glDisable GL_COLOR_LOGIC_OP;
2022 }
2023}
2024
2025my %DIR = (
2026 SDLK_KP8, [1, "north"],
2027 SDLK_KP9, [2, "northeast"],
2028 SDLK_KP6, [3, "east"],
2029 SDLK_KP3, [4, "southeast"],
2030 SDLK_KP2, [5, "south"],
2031 SDLK_KP1, [6, "southwest"],
2032 SDLK_KP4, [7, "west"],
2033 SDLK_KP7, [8, "northwest"],
2034
2035 SDLK_UP, [1, "north"],
2036 SDLK_RIGHT, [3, "east"],
2037 SDLK_DOWN, [5, "south"],
2038 SDLK_LEFT, [7, "west"],
2039);
2040
2041sub key_down {
2042 my ($self, $ev) = @_;
2043
2044 my $mod = $ev->key_mod;
2045 my $sym = $ev->key_sym;
2046
2047 if ($sym == SDLK_KP5) {
2048 $::CONN->user_send ("stay fire");
2049 } elsif ($sym == SDLK_a) {
2050 $::CONN->user_send ("apply");
2051 } elsif ($sym == SDLK_QUOTE) {
2052 $self->emit ('activate_console');
2053 } elsif ($sym == SDLK_SLASH) {
2054 $self->emit ('activate_console' => '/');
2055 } elsif (exists $DIR{$sym}) {
2056 if ($mod & KMOD_SHIFT) {
2057 $self->{shft}++;
2058 $::CONN->user_send ("fire $DIR{$sym}[0]");
2059 } elsif ($mod & KMOD_CTRL) {
2060 $self->{ctrl}++;
2061 $::CONN->user_send ("run $DIR{$sym}[0]");
2062 } else {
2063 $::CONN->user_send ("$DIR{$sym}[1]");
2064 }
2065 }
2066}
2067
2068sub key_up {
2069 my ($self, $ev) = @_;
2070
2071 my $mod = $ev->key_mod;
2072 my $sym = $ev->key_sym;
2073
2074 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2075 $::CONN->user_send ("fire_stop");
2076 }
2077 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2078 $::CONN->user_send ("run_stop");
2079 }
2080} 1821}
2081 1822
2082############################################################################# 1823#############################################################################
2083 1824
2084package CFClient::UI::Animator; 1825package CFClient::UI::Animator;
2169use SDL::OpenGL; 1910use SDL::OpenGL;
2170 1911
2171sub check_size { 1912sub check_size {
2172 my ($self) = @_; 1913 my ($self) = @_;
2173 1914
2174 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 1915 $self->configure (0, 0, $::WITH, $::HEIGHT);
2175} 1916}
2176 1917
2177sub size_request { 1918sub size_request {
2178 ($::WIDTH, $::HEIGHT) 1919 ($::WIDTH, $::HEIGHT)
2179} 1920}
2180 1921
2181sub size_allocate { 1922sub configure {
2182 my ($self, $x, $y, $w, $h) = @_; 1923 my ($self, $x, $y, $w, $h) = @_;
2183 1924
2184 $self->_size_allocate ($x, $y, $w, $h); 1925 $self->SUPER::configure ($x, $y, $w, $h);
2185 1926
2186 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1927 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2187 for @{$self->{children}}; 1928 for @{$self->{children}};
2188} 1929}
2189 1930
2190sub _topleft { 1931sub _topleft {
2191 my ($self, $x, $y) = @_; 1932 my ($self, $x, $y) = @_;
2199 $self->check_size; 1940 $self->check_size;
2200 ::refresh (); 1941 ::refresh ();
2201} 1942}
2202 1943
2203sub add { 1944sub add {
2204 my ($self, $widget) = @_; 1945 my ($self, $child) = @_;
2205 1946
2206 $self->SUPER::add ($widget); 1947 $self->SUPER::add ($child);
2207
2208 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2209} 1948}
2210 1949
2211sub on_refresh { 1950sub on_refresh {
2212 my ($self, $id, $cb) = @_; 1951 my ($self, $id, $cb) = @_;
2213 1952

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines