ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.123 by root, Mon Apr 17 19:50:22 2006 UTC vs.
Revision 1.136 by root, Wed Apr 19 20:46:44 2006 UTC

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) = @_;
260 my ($self, $parent) = @_; 276 my ($self, $parent) = @_;
261 277
262 Scalar::Util::weaken ($self->{parent} = $parent); 278 Scalar::Util::weaken ($self->{parent} = $parent);
263} 279}
264 280
281sub check_size {
282 my ($self) = @_;
283
284 my ($w, $h) = $self->size_request;
285
286 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w;
288 $self->{req_h} = $h;
289
290 $self->{parent}->check_size
291 if $self->{parent};
292 }
293}
294
265sub update { 295sub update {
266 my ($self) = @_; 296 my ($self) = @_;
267 297
268 $self->{parent}->update 298 $self->{parent}->update
269 if $self->{parent}; 299 if $self->{parent};
270} 300}
271 301
272sub connect { 302sub connect {
273 my ($self, $signal, $cb) = @_; 303 my ($self, $signal, $cb) = @_;
274 304
275 push @{ $self->{cb}{$signal} }, $cb; 305 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 306}
277 307
278sub emit { 308sub emit {
279 my ($self, $signal, @args) = @_; 309 my ($self, $signal, @args) = @_;
280 310
311 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 312 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 313 }
283} 314}
284 315
285sub DESTROY { 316sub DESTROY {
286 my ($self) = @_; 317 my ($self) = @_;
287 318
367 $self->{children} = [ 398 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 399 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 400 @{$self->{children}}, $child
370 ]; 401 ];
371 402
372 $self->{w} = $self->{h} = -1; 403 $child->check_size;
373 $self->update;
374} 404}
375 405
376sub remove { 406sub remove {
377 my ($self, $widget) = @_; 407 my ($self, $child) = @_;
378 408
409 delete $child->{parent};
410
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
380 412
381 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 413 $self->check_size;
382} 414}
383 415
384sub find_widget { 416sub find_widget {
385 my ($self, $x, $y) = @_; 417 my ($self, $x, $y) = @_;
386 418
416 448
417 $class->SUPER::new (children => [$child], %arg) 449 $class->SUPER::new (children => [$child], %arg)
418} 450}
419 451
420sub add { 452sub add {
421 my ($self, $widget) = @_; 453 my ($self, $child) = @_;
422 454
423 $self->{children} = []; 455 $self->{children} = [];
424 456
425 $self->SUPER::add ($widget); 457 $self->SUPER::add ($child);
426} 458}
427 459
428sub remove { 460sub remove {
429 my ($self, $widget) = @_; 461 my ($self, $widget) = @_;
430 462
439sub size_request { 471sub size_request {
440 $_[0]{children}[0]->size_request 472 $_[0]{children}[0]->size_request
441} 473}
442 474
443sub size_allocate { 475sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 476 my ($self, $w, $h) = @_;
445 477
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 478 $self->{children}[0]->configure (0, 0, $w, $h);
449} 479}
450 480
451############################################################################# 481#############################################################################
452 482
453package CFClient::UI::Window; 483package CFClient::UI::Window;
479 $self->child->draw; 509 $self->child->draw;
480 }; 510 };
481} 511}
482 512
483sub size_allocate { 513sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 514 my ($self, $w, $h) = @_;
485 515
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 516 $self->child->configure (0, 0, $w, $h);
489 517
490 $self->render_chld; 518 $self->render_chld;
491} 519}
492 520
493sub _draw { 521sub _draw {
509 glDisable GL_TEXTURE_2D; 537 glDisable GL_TEXTURE_2D;
510} 538}
511 539
512############################################################################# 540#############################################################################
513 541
542package CFClient::UI::ViewPort;
543
544our @ISA = CFClient::UI::Window::;
545
546sub new { die }
547
548sub size_request {
549 my ($self) = @_;
550
551 @$self{qw(child_w child_h)} = $self->child->size_request;
552 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553
554 @$self{qw(child_w child_h)}
555}
556
557sub _draw {
558 my ($self) = @_;
559
560 $self->{children}[1]->draw;
561}
562
563
564#############################################################################
565
514package CFClient::UI::Frame; 566package CFClient::UI::Frame;
515 567
516our @ISA = CFClient::UI::Bin::; 568our @ISA = CFClient::UI::Bin::;
517 569
518use SDL::OpenGL; 570use SDL::OpenGL;
528} 580}
529 581
530sub size_allocate { 582sub size_allocate {
531 my ($self, $x, $y, $w, $h) = @_; 583 my ($self, $x, $y, $w, $h) = @_;
532 584
533 $self->_size_allocate ($x, $y, $w, $h) or return;
534
535 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 585 $self->child->configure (2, 2, $w - 4, $h - 4);
536} 586}
537 587
538sub _draw { 588sub _draw {
539 my ($self) = @_; 589 my ($self) = @_;
540 590
571 # TODO: user_x, user_y, overwrite moveto? 621 # TODO: user_x, user_y, overwrite moveto?
572 622
573 $class->SUPER::new ( 623 $class->SUPER::new (
574 bg => [1, 1, 1, 1], 624 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 625 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 626 border => 0.8,
577 @_ 627 @_
578 ) 628 )
579} 629}
580 630
631sub border {
632 int $_[0]{border} * $::FONTSIZE
633}
634
581sub size_request { 635sub size_request {
582 my ($self) = @_; 636 my ($self) = @_;
583 637
584 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};
585 639
586 my ($w, $h) = $self->SUPER::size_request; 640 my ($w, $h) = $self->SUPER::size_request;
587 641
588 ( 642 (
589 $w + $self->{border} * 2, 643 $w + $self->border * 2,
590 $h + $self->{border} * 2, 644 $h + $self->border * 2,
591 ) 645 )
592} 646}
593 647
594sub size_allocate { 648sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 649 my ($self, $w, $h) = @_;
596 650
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 651 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 652 $w -= List::Util::max 0, $self->border * 2;
601 653
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 654 $self->child->configure ($self->border, $self->border, $w, $h);
603} 655}
604 656
605sub button_down { 657sub button_down {
606 my ($self, $ev, $x, $y) = @_; 658 my ($self, $ev, $x, $y) = @_;
607 659
660 my $border = $self->border;
661
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 662 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 663 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 664
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 665 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 666 my ($bw, $bh) = ($self->{w}, $self->{h});
613 667
614 $self->{motion} = sub { 668 $self->{motion} = sub {
616 670
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 671 ($x, $y) = ($ev->motion_x, $ev->motion_y);
618 672
619 $self->{user_w} = $bw + $x - $ox; 673 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 674 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 675 $self->check_size;
622 }; 676 };
623 677
624 } elsif ($x >= 0 && $x < $self->{w} 678 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 679 && $y >= 0 && $y < $border) {
626 680
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 681 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
628 my ($bx, $by) = ($self->{x}, $self->{y}); 682 my ($bx, $by) = ($self->{x}, $self->{y});
629 683
630 $self->{motion} = sub { 684 $self->{motion} = sub {
659 glEnable GL_BLEND; 713 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 714 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 717
718 my $border = $self->border;
719
664 glColor @{ $self->{border_bg} }; 720 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 721 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 722 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 723 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 724 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 725
670 my $bg = $tex[0]; 726 my $bg = $tex[0];
671 727
672 # TODO: repeat texture not scale 728 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 729 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 732 glColor @{ $self->{bg} };
677 733
678 $bg->{s} = $rep_x; 734 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 735 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 736 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 737 $bg->draw_quad ($border, $border, $cw, $ch);
682 738
683 glDisable GL_TEXTURE_2D; 739 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 740 glDisable GL_BLEND;
685 741
686 $self->child->draw; 742 $self->child->draw;
709 my ($self, $x, $y, $child) = @_; 765 my ($self, $x, $y, $child) = @_;
710 766
711 $child->set_parent ($self); 767 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 768 $self->{children}[$y][$x] = $child;
713 769
714 $self->{w} = $self->{h} = -1; 770 $child->check_size;
715 $self->update;
716} 771}
717 772
718# 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?
719sub clear { 774sub clear {
720 my ($self) = @_; 775 my ($self) = @_;
755 (sum @$hs), 810 (sum @$hs),
756 ) 811 )
757} 812}
758 813
759sub size_allocate { 814sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 815 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 816
764 my ($ws, $hs) = $self->get_wh; 817 my ($ws, $hs) = $self->get_wh;
765 818
766 my $req_w = sum @$ws; 819 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 820 my $req_h = sum @$hs;
789 842
790 for my $c (0 .. $#$row) { 843 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 844 my $col_w = $ws->[$c];
792 845
793 if (my $widget = $row->[$c]) { 846 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 847 $widget->configure ($x, $y, $col_w, $row_h);
795 } 848 }
796 849
797 $x += $col_w; 850 $x += $col_w;
798 } 851 }
799 852
844 (List::Util::max map $_->[1], @alloc), 897 (List::Util::max map $_->[1], @alloc),
845 ) 898 )
846} 899}
847 900
848sub size_allocate { 901sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 902 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 903
853 ($h, $w) = ($w, $h); 904 ($h, $w) = ($w, $h);
854 905
855 my $children = $self->{children}; 906 my $children = $self->{children};
856 907
878 929
879 my $y = 0; 930 my $y = 0;
880 for (0 .. $#$children) { 931 for (0 .. $#$children) {
881 my $child = $children->[$_]; 932 my $child = $children->[$_];
882 my $h = $h[$_]; 933 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 934 $child->configure ($y, 0, $h, $w);
884 935
885 $y += $h; 936 $y += $h;
886 } 937 }
938
939 1
887} 940}
888 941
889############################################################################# 942#############################################################################
890 943
891package CFClient::UI::VBox; 944package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 957 (List::Util::sum map $_->[1], @alloc),
905 ) 958 )
906} 959}
907 960
908sub size_allocate { 961sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 962 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 963
913 my $children = $self->{children}; 964 my $children = $self->{children};
914 965
915 my @h = map +($_->size_request)[1], @$children; 966 my @h = map +($_->size_request)[1], @$children;
916 967
934 985
935 my $y = 0; 986 my $y = 0;
936 for (0 .. $#$children) { 987 for (0 .. $#$children) {
937 my $child = $children->[$_]; 988 my $child = $children->[$_];
938 my $h = $h[$_]; 989 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 990 $child->configure (0, $y, $w, $h);
940 991
941 $y += $h; 992 $y += $h;
942 } 993 }
994
995 1
943} 996}
944 997
945############################################################################# 998#############################################################################
946 999
947package CFClient::UI::Label; 1000package CFClient::UI::Label;
953sub new { 1006sub new {
954 my ($class, %arg) = @_; 1007 my ($class, %arg) = @_;
955 1008
956 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1010 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1011 fontsize => 1,
959 text => "", 1012 text => "",
960 align => -1, 1013 align => -1,
961 valign => -1, 1014 valign => -1,
962 padding => 2, 1015 padding => 2,
963 layout => new CFClient::Layout, 1016 layout => new CFClient::Layout,
1002 1055
1003sub size_request { 1056sub size_request {
1004 my ($self) = @_; 1057 my ($self) = @_;
1005 1058
1006 $self->{layout}->set_width; 1059 $self->{layout}->set_width;
1007 $self->{layout}->set_height ($self->{fontsize}); 1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1008 1061
1009 my ($w, $h) = $self->{layout}->size; 1062 my ($w, $h) = $self->{layout}->size;
1010 1063
1011 ( 1064 (
1012 $w + $self->{padding} * 2, 1065 $w + $self->{padding} * 2,
1013 $h + $self->{padding} * 2, 1066 $h + $self->{padding} * 2,
1014 ) 1067 )
1015} 1068}
1016 1069
1017sub size_allocate { 1070sub size_allocate {
1018 my ($self, $x, $y, $w, $h) = @_; 1071 my ($self, $w, $h) = @_;
1019
1020 $self->_size_allocate ($x, $y, $w, $h) or return;
1021 1072
1022 delete $self->{texture}; 1073 delete $self->{texture};
1023}
1024
1025sub update {
1026 my ($self) = @_;
1027
1028 delete $self->{texture};
1029 $self->SUPER::update;
1030} 1074}
1031 1075
1032sub _draw { 1076sub _draw {
1033 my ($self) = @_; 1077 my ($self) = @_;
1034 1078
1035 my $tex = $self->{texture} ||= do { 1079 my $tex = $self->{texture} ||= do {
1036 $self->{layout}->set_width ($self->{w}); 1080 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1038 new_from_layout CFClient::Texture $self->{layout} 1082 new_from_layout CFClient::Texture $self->{layout}
1039 }; 1083 };
1040 1084
1041 glEnable GL_BLEND; 1085 glEnable GL_BLEND;
1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1086 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1067 1111
1068package CFClient::UI::EntryBase; 1112package CFClient::UI::EntryBase;
1069 1113
1070our @ISA = CFClient::UI::Label::; 1114our @ISA = CFClient::UI::Label::;
1071 1115
1072use SDL;
1073use SDL::OpenGL; 1116use SDL::OpenGL;
1074 1117
1075sub new { 1118sub new {
1076 my $class = shift; 1119 my $class = shift;
1077 1120
1092 1135
1093 delete $self->{cur_h}; 1136 delete $self->{cur_h};
1094 1137
1095 return if $self->{text} eq $text; 1138 return if $self->{text} eq $text;
1096 1139
1140 delete $self->{texture};
1141
1097 $self->{last_activity} = $::NOW; 1142 $self->{last_activity} = $::NOW;
1098 $self->{text} = $text; 1143 $self->{text} = $text;
1099 1144
1100 $text =~ s/./*/g if $self->{hidden}; 1145 $text =~ s/./*/g if $self->{hidden};
1101 $self->{layout}->set_text ("$text "); 1146 $self->{layout}->set_text ("$text ");
1114 1159
1115 ($w + 1, $h) # add 1 for cursor 1160 ($w + 1, $h) # add 1 for cursor
1116} 1161}
1117 1162
1118sub size_allocate { 1163sub size_allocate {
1119 my ($self, $x, $y, $w, $h) = @_; 1164 my ($self, $w, $h) = @_;
1120
1121 $self->SUPER::size_allocate ($x, $y, $w, $h);
1122 1165
1123 $self->_set_text ($self->{text}); 1166 $self->_set_text ($self->{text});
1124} 1167}
1125 1168
1126sub set_text { 1169sub set_text {
1139 1182
1140 my $uni = $ev->key_unicode; 1183 my $uni = $ev->key_unicode;
1141 1184
1142 my $text = $self->get_text; 1185 my $text = $self->get_text;
1143 1186
1144 if ($sym == SDLK_BACKSPACE) { 1187 if ($sym == 8) {
1145 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1188 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1146 } elsif ($sym == SDLK_DELETE) { 1189 } elsif ($sym == 127) {
1147 substr $text, $self->{cursor}, 1, ""; 1190 substr $text, $self->{cursor}, 1, "";
1148 } elsif ($sym == SDLK_LEFT) { 1191 } elsif ($sym == CFClient::SDLK_LEFT) {
1149 --$self->{cursor} if $self->{cursor}; 1192 --$self->{cursor} if $self->{cursor};
1150 } elsif ($sym == SDLK_RIGHT) { 1193 } elsif ($sym == CFClient::SDLK_RIGHT) {
1151 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1194 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1152 } elsif ($sym == SDLK_HOME) { 1195 } elsif ($sym == CFClient::SDLK_HOME) {
1153 $self->{cursor} = 0; 1196 $self->{cursor} = 0;
1154 } elsif ($sym == SDLK_END) { 1197 } elsif ($sym == CFClient::SDLK_END) {
1155 $self->{cursor} = length $text; 1198 $self->{cursor} = length $text;
1156 } elsif ($sym == SDLK_ESCAPE) { 1199 } elsif ($sym == 27) {
1157 $self->emit ('escape'); 1200 $self->emit ('escape');
1158 } elsif ($uni) { 1201 } elsif ($uni) {
1159 substr $text, $self->{cursor}++, 0, chr $uni; 1202 substr $text, $self->{cursor}++, 0, chr $uni;
1160 } 1203 }
1161 1204
1236 1279
1237package CFClient::UI::Entry; 1280package CFClient::UI::Entry;
1238 1281
1239our @ISA = CFClient::UI::EntryBase::; 1282our @ISA = CFClient::UI::EntryBase::;
1240 1283
1241use SDL;
1242use SDL::OpenGL; 1284use SDL::OpenGL;
1243 1285
1244sub key_down { 1286sub key_down {
1245 my ($self, $ev) = @_; 1287 my ($self, $ev) = @_;
1246 1288
1247 my $sym = $ev->key_sym; 1289 my $sym = $ev->key_sym;
1248 1290
1249 if ($sym == SDLK_RETURN) { 1291 if ($sym == 13) {
1250 $self->emit (activate => $self->get_text); 1292 $self->emit (activate => $self->get_text);
1251 $self->update; 1293 $self->update;
1252 1294
1253 } else { 1295 } else {
1254 $self->SUPER::key_down ($ev); 1296 $self->SUPER::key_down ($ev);
1260 1302
1261package CFClient::UI::Button; 1303package CFClient::UI::Button;
1262 1304
1263our @ISA = CFClient::UI::Label::; 1305our @ISA = CFClient::UI::Label::;
1264 1306
1265use SDL;
1266use SDL::OpenGL; 1307use SDL::OpenGL;
1267 1308
1268my @tex = 1309my @tex =
1269 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1310 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1270 qw(b1_button_active.png); 1311 qw(b1_button_active.png);
1276 padding => 4, 1317 padding => 4,
1277 fg => [1, 1, 1], 1318 fg => [1, 1, 1],
1278 bg => [1, 1, 1, 0.2], 1319 bg => [1, 1, 1, 0.2],
1279 active_fg => [0, 0, 1], 1320 active_fg => [0, 0, 1],
1280 can_hover => 1, 1321 can_hover => 1,
1322 align => 0,
1323 valign => 0,
1281 @_ 1324 @_
1282 ) 1325 )
1283} 1326}
1284 1327
1285sub button_up { 1328sub button_up {
1322 1365
1323my @tex = 1366my @tex =
1324 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1367 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1325 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1368 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1326 1369
1327use SDL;
1328use SDL::OpenGL; 1370use SDL::OpenGL;
1329 1371
1330sub new { 1372sub new {
1331 my $class = shift; 1373 my $class = shift;
1332 1374
1344 my ($self) = @_; 1386 my ($self) = @_;
1345 1387
1346 ($self->{padding} * 2 + 6) x 2 1388 ($self->{padding} * 2 + 6) x 2
1347} 1389}
1348 1390
1349sub size_allocate {
1350 my ($self, $x, $y, $w, $h) = @_;
1351
1352 $self->_size_allocate ($x, $y, $w, $h);
1353}
1354
1355sub button_down { 1391sub button_down {
1356 my ($self, $ev, $x, $y) = @_; 1392 my ($self, $ev, $x, $y) = @_;
1357 1393
1358 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1394 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1359 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1395 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1381 1417
1382 $tex->draw_quad (0, 0, $s, $s); 1418 $tex->draw_quad (0, 0, $s, $s);
1383 1419
1384 glDisable GL_TEXTURE_2D; 1420 glDisable GL_TEXTURE_2D;
1385 glDisable GL_BLEND; 1421 glDisable GL_BLEND;
1422}
1423
1424#############################################################################
1425
1426package CFClient::UI::VGauge;
1427
1428our @ISA = CFClient::UI::Base::;
1429
1430use SDL::OpenGL;
1431
1432my %tex = (
1433 food => [
1434 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1435 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1436 ],
1437 grace => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1440 ],
1441 hp => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1444 ],
1445 mana => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1448 ],
1449);
1450
1451# eg. VGauge->new (gauge => 'food'), default gauge: food
1452sub new {
1453 my $class = shift;
1454
1455 my $self = $class->SUPER::new (gauge => 'food', @_);
1456
1457 $self
1458}
1459
1460sub size_request {
1461 my ($self) = @_;
1462
1463 my $tex = $tex{$self->{gauge}}[0];
1464
1465 @$tex{qw(w h)}
1466}
1467
1468sub set_max {
1469 my ($self, $max) = @_;
1470
1471 $self->{max_val} = $max;
1472}
1473
1474sub set_value {
1475 my ($self, $val, $max) = @_;
1476
1477 $self->set_max ($max)
1478 if defined $max;
1479
1480 $max = $self->{max_val};
1481 $self->{val} = $val;
1482
1483 $self->update;
1484}
1485
1486sub _draw {
1487 my ($self) = @_;
1488
1489 my $tex = $tex{$self->{gauge}};
1490
1491 my ($w, $h) = ($self->{w}, $self->{h});
1492
1493 my $ycut = $self->{val} / ($self->{max_val} || 1);
1494 $ycut = 1 if $self->{val} > $self->{max_val};
1495
1496 my $t1 = $tex->[0];
1497 my $t2 = $tex->[1];
1498
1499 glEnable GL_BLEND;
1500 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1501 glEnable GL_TEXTURE_2D;
1502 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1503
1504 my $h1 = $self->{h} - $ycut * $self->{h};
1505 my $h2 = $ycut * $self->{h};
1506
1507 my $yp = 0;
1508
1509 glBindTexture GL_TEXTURE_2D, $t1->{name};
1510 glBegin GL_QUADS;
1511 glTexCoord 0 , 0; glVertex 0 , $yp;
1512 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1513 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1514 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1515 glEnd;
1516
1517 $yp += $h1;
1518
1519 glBindTexture GL_TEXTURE_2D, $t2->{name};
1520 glBegin GL_QUADS;
1521 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1522 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1523 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1524 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1525 glEnd;
1526
1527 glDisable GL_BLEND;
1528 glDisable GL_TEXTURE_2D;
1386} 1529}
1387 1530
1388############################################################################# 1531#############################################################################
1389 1532
1390package CFClient::UI::Slider; 1533package CFClient::UI::Slider;
1409 # TODO: calculations are off 1552 # TODO: calculations are off
1410 my $self = $class->SUPER::new ( 1553 my $self = $class->SUPER::new (
1411 fg => [1, 1, 1], 1554 fg => [1, 1, 1],
1412 active_fg => [0, 0, 0], 1555 active_fg => [0, 0, 0],
1413 range => [0, 0, 100, 10], 1556 range => [0, 0, 100, 10],
1414 req_w => 40, 1557 req_w => 20,
1415 req_h => 13, 1558 req_h => 20,
1416 vertical => 0, 1559 vertical => 0,
1417 can_hover => 1, 1560 can_hover => 1,
1418 inner_pad => 5, 1561 inner_pad => 5,
1419 @_ 1562 @_
1420 ); 1563 );
1530 1673
1531sub new { 1674sub new {
1532 my $class = shift; 1675 my $class = shift;
1533 1676
1534 my $self = $class->SUPER::new ( 1677 my $self = $class->SUPER::new (
1535 req_w => $::WIDTH / 6,
1536 req_h => $::HEIGHT / 6,
1537 fontsize => $::FONTSIZE, 1678 fontsize => 1,
1538 @_, 1679 @_,
1539 1680
1540 layout => (new CFClient::Layout), 1681 layout => (new CFClient::Layout),
1541 par => [], 1682 par => [],
1542 height => 0, 1683 height => 0,
1563sub text_height { 1704sub text_height {
1564 my ($self, $text) = @_; 1705 my ($self, $text) = @_;
1565 1706
1566 my $layout = $self->{layout}; 1707 my $layout = $self->{layout};
1567 1708
1568 $layout->set_height ($self->{fontsize}); 1709 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1569 $layout->set_width ($self->{w}); 1710 $layout->set_width ($self->{w});
1570 $layout->set_text ($text); 1711 $layout->set_text ($text);
1571 1712
1572 ($layout->size)[1] 1713 ($layout->size)[1]
1573} 1714}
1577 1718
1578 $self->{need_reflow}++; 1719 $self->{need_reflow}++;
1579 $self->update; 1720 $self->update;
1580} 1721}
1581 1722
1582sub size_request {
1583 my ($self) = @_;
1584
1585 ($self->{req_w}, $self->{req_h})
1586}
1587
1588sub size_allocate { 1723sub size_allocate {
1589 my ($self, $x, $y, $w, $h) = @_; 1724 my ($self, $w, $h) = @_;
1590 1725
1591 $self->SUPER::size_allocate ($x, $y, $w, $h); 1726 $self->SUPER::size_allocate ($w, $h);
1592 1727
1593 $self->{layout}->set_height ($self->{fontsize}); 1728 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1594 $self->{layout}->set_width ($self->{w}); 1729 $self->{layout}->set_width ($self->{children}[0]{w});
1595 1730
1596 $self->reflow; 1731 $self->reflow;
1597} 1732}
1598 1733
1599sub add_paragraph { 1734sub add_paragraph {
1682 glDisable GL_TEXTURE_2D; 1817 glDisable GL_TEXTURE_2D;
1683 } 1818 }
1684 1819
1685 $self->{children}[1]->draw; 1820 $self->{children}[1]->draw;
1686 1821
1687}
1688
1689#############################################################################
1690
1691package CFClient::UI::MapWidget;
1692
1693use strict;
1694
1695use List::Util qw(min max);
1696
1697use SDL;
1698use SDL::OpenGL;
1699
1700our @ISA = CFClient::UI::Base::;
1701
1702sub new {
1703 my $class = shift;
1704
1705 $class->SUPER::new (
1706 z => -1,
1707 can_focus => 1,
1708 list => (glGenLists 1),
1709 @_
1710 )
1711}
1712
1713sub key_down {
1714 print "MAPKEYDOWN\n";
1715}
1716
1717sub key_up {
1718}
1719
1720sub button_down {
1721 my ($self, $ev, $x, $y) = @_;
1722
1723 $self->focus_in;
1724
1725 if ($ev->button == 2) {
1726 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1727 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1728
1729 $self->{motion} = sub {
1730 my ($ev, $x, $y) = @_;
1731
1732 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1733
1734 $::CFG->{map_shift_x} = $bw + $x - $ox;
1735 $::CFG->{map_shift_y} = $bh + $y - $oy;
1736
1737 $self->update;
1738 };
1739 }
1740}
1741
1742sub button_up {
1743 my ($self, $ev, $x, $y) = @_;
1744
1745 delete $self->{motion};
1746}
1747
1748sub mouse_motion {
1749 my ($self, $ev, $x, $y) = @_;
1750
1751 $self->{motion}->($ev, $x, $y) if $self->{motion};
1752}
1753
1754sub size_request {
1755 (
1756 1 + 32 * int $::WIDTH / 32,
1757 1 + 32 * int $::HEIGHT / 32,
1758 )
1759}
1760
1761sub update {
1762 my ($self) = @_;
1763
1764 $self->{need_update} = 1;
1765 $self->SUPER::update;
1766}
1767
1768sub draw {
1769 my ($self) = @_;
1770
1771 if (delete $self->{need_update}) {
1772 glNewList $self->{list}, GL_COMPILE;
1773
1774 if ($::MAP) {
1775 my $sw = int $::WIDTH / 32;
1776 my $sh = int $::HEIGHT / 32;
1777
1778 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1779 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1780
1781 glTranslate $sx0 - 32, $sy0 - 32, 0;
1782
1783 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1784
1785 if ($::CFG->{fow_enable}) {
1786 if ($::CFG->{fow_smooth}) { # smooth fog of war
1787 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1788 glConvolutionFilter2D
1789 GL_CONVOLUTION_2D,
1790 GL_ALPHA,
1791 3, 3,
1792 GL_ALPHA, GL_FLOAT,
1793 pack "f*",
1794 0.1, 0.1, 0.1,
1795 0.1, 0.2, 0.1,
1796 0.1, 0.1, 0.1,
1797 ;
1798 glEnable GL_CONVOLUTION_2D;
1799 }
1800
1801 $self->{fow_texture} = new CFClient::Texture
1802 w => $w,
1803 h => $h,
1804 data => $data,
1805 internalformat => GL_ALPHA,
1806 format => GL_ALPHA;
1807
1808 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1809
1810 glEnable GL_BLEND;
1811 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1812 glEnable GL_TEXTURE_2D;
1813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1814
1815 glColor +($::CFG->{fow_intensity}) x 3, 1;
1816 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1817
1818 glDisable GL_TEXTURE_2D;
1819 glDisable GL_BLEND;
1820 }
1821
1822 # HACK BEGIN
1823 {
1824 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1825 my ($w, $h) = (250, 250);
1826
1827 glEnable GL_BLEND;
1828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1829 glEnable GL_TEXTURE_2D;
1830 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1831
1832 $self->{mapmap_texture} =
1833 new CFClient::Texture
1834 w => $w,
1835 h => $h,
1836 data => $::MAP->mapmap ($w, $h),
1837 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1838
1839 $self->{mapmap_texture}->draw_quad (100, 100);
1840
1841 glDisable GL_TEXTURE_2D;
1842 glDisable GL_BLEND;
1843 }
1844 # HACK END
1845 }
1846
1847 glEndList;
1848 }
1849
1850 glPushMatrix;
1851 glCallList $self->{list};
1852 glPopMatrix;
1853
1854 if ($FOCUS != $self) {
1855 glColor 64/255, 64/255, 64/255;
1856 glLogicOp GL_AND;
1857 glEnable GL_COLOR_LOGIC_OP;
1858 glBegin GL_QUADS;
1859 glVertex 0, 0;
1860 glVertex 0, $::HEIGHT;
1861 glVertex $::WIDTH, $::HEIGHT;
1862 glVertex $::WIDTH, 0;
1863 glEnd;
1864 glDisable GL_COLOR_LOGIC_OP;
1865 }
1866}
1867
1868my %DIR = (
1869 SDLK_KP8, [1, "north"],
1870 SDLK_KP9, [2, "northeast"],
1871 SDLK_KP6, [3, "east"],
1872 SDLK_KP3, [4, "southeast"],
1873 SDLK_KP2, [5, "south"],
1874 SDLK_KP1, [6, "southwest"],
1875 SDLK_KP4, [7, "west"],
1876 SDLK_KP7, [8, "northwest"],
1877
1878 SDLK_UP, [1, "north"],
1879 SDLK_RIGHT, [3, "east"],
1880 SDLK_DOWN, [5, "south"],
1881 SDLK_LEFT, [7, "west"],
1882);
1883
1884sub key_down {
1885 my ($self, $ev) = @_;
1886
1887 my $mod = $ev->key_mod;
1888 my $sym = $ev->key_sym;
1889
1890 if ($sym == SDLK_KP5) {
1891 $::CONN->user_send ("stay fire");
1892 } elsif ($sym == SDLK_a) {
1893 $::CONN->user_send ("apply");
1894 } elsif ($sym == SDLK_QUOTE) {
1895 $self->emit ('activate_console');
1896 } elsif ($sym == SDLK_SLASH) {
1897 $self->emit ('activate_console' => '/');
1898 } elsif (exists $DIR{$sym}) {
1899 if ($mod & KMOD_SHIFT) {
1900 $self->{shft}++;
1901 $::CONN->user_send ("fire $DIR{$sym}[0]");
1902 } elsif ($mod & KMOD_CTRL) {
1903 $self->{ctrl}++;
1904 $::CONN->user_send ("run $DIR{$sym}[0]");
1905 } else {
1906 $::CONN->user_send ("$DIR{$sym}[1]");
1907 }
1908 }
1909}
1910
1911sub key_up {
1912 my ($self, $ev) = @_;
1913
1914 my $mod = $ev->key_mod;
1915 my $sym = $ev->key_sym;
1916
1917 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1918 $::CONN->user_send ("fire_stop");
1919 }
1920 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1921 $::CONN->user_send ("run_stop");
1922 }
1923} 1822}
1924 1823
1925############################################################################# 1824#############################################################################
1926 1825
1927package CFClient::UI::Animator; 1826package CFClient::UI::Animator;
2009 1908
2010our @ISA = CFClient::UI::Container::; 1909our @ISA = CFClient::UI::Container::;
2011 1910
2012use SDL::OpenGL; 1911use SDL::OpenGL;
2013 1912
1913sub check_size {
1914 my ($self) = @_;
1915
1916 $self->configure (0, 0, $::WITH, $::HEIGHT);
1917}
1918
2014sub size_request { 1919sub size_request {
2015 ($::WIDTH, $::HEIGHT) 1920 ($::WIDTH, $::HEIGHT)
2016} 1921}
2017 1922
2018sub size_allocate { 1923sub configure {
2019 my ($self, $x, $y, $w, $h) = @_; 1924 my ($self, $x, $y, $w, $h) = @_;
2020 1925
2021 $self->_size_allocate ($x, $y, $w, $h); 1926 $self->SUPER::configure ($x, $y, $w, $h);
2022 1927
2023 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1928 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2024 for @{$self->{children}}; 1929 for @{$self->{children}};
2025} 1930}
2026 1931
2027sub _topleft { 1932sub _topleft {
2028 my ($self, $x, $y) = @_; 1933 my ($self, $x, $y) = @_;
2031} 1936}
2032 1937
2033sub update { 1938sub update {
2034 my ($self) = @_; 1939 my ($self) = @_;
2035 1940
2036 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1941 $self->check_size;
2037 ::refresh (); 1942 ::refresh ();
2038} 1943}
2039 1944
2040sub add { 1945sub add {
2041 my ($self, $widget) = @_; 1946 my ($self, $child) = @_;
2042 1947
2043 $self->SUPER::add ($widget); 1948 $self->SUPER::add ($child);
2044
2045 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2046} 1949}
2047 1950
2048sub on_refresh { 1951sub on_refresh {
2049 my ($self, $id, $cb) = @_; 1952 my ($self, $id, $cb) = @_;
2050 1953

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines