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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines