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.129 by root, Tue Apr 18 01:36:13 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) = @_;
117 131
118 $self->{x} = int $x; 132 $self->{x} = int $x;
119 $self->{y} = int $y; 133 $self->{y} = int $y;
141 155
142 $self->{w} = $w; 156 $self->{w} = $w;
143 $self->{h} = $h; 157 $self->{h} = $h;
144 158
145 $self->size_allocate ($w, $h); 159 $self->size_allocate ($w, $h);
160 $self->update;
146} 161}
147 162
148sub size_allocate { 163sub size_allocate {
149 # nothing to be done 164 # nothing to be done
150} 165}
272 $self->{req_w} = $w; 287 $self->{req_w} = $w;
273 $self->{req_h} = $h; 288 $self->{req_h} = $h;
274 289
275 $self->{parent}->check_size 290 $self->{parent}->check_size
276 if $self->{parent}; 291 if $self->{parent};
277
278 $self->size_allocate ($w, $h);
279 $self->update;
280 } 292 }
281} 293}
282 294
283sub update { 295sub update {
284 my ($self) = @_; 296 my ($self) = @_;
288} 300}
289 301
290sub connect { 302sub connect {
291 my ($self, $signal, $cb) = @_; 303 my ($self, $signal, $cb) = @_;
292 304
293 push @{ $self->{cb}{$signal} }, $cb; 305 push @{ $self->{signal_cb}{$signal} }, $cb;
294} 306}
295 307
296sub emit { 308sub emit {
297 my ($self, $signal, @args) = @_; 309 my ($self, $signal, @args) = @_;
298 310
311 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
299 $_->($self, @args) 312 $cb->($self, @args);
300 for @{$self->{cb}{$signal} || []}; 313 }
301} 314}
302 315
303sub DESTROY { 316sub DESTROY {
304 my ($self) = @_; 317 my ($self) = @_;
305 318
385 $self->{children} = [ 398 $self->{children} = [
386 sort { $a->{z} <=> $b->{z} } 399 sort { $a->{z} <=> $b->{z} }
387 @{$self->{children}}, $child 400 @{$self->{children}}, $child
388 ]; 401 ];
389 402
390 $self->{w} = $self->{h} = -1;
391
392 $child->check_size; 403 $child->check_size;
393} 404}
394 405
395sub remove { 406sub remove {
396 my ($self, $widget) = @_; 407 my ($self, $child) = @_;
397 408
409 delete $child->{parent};
410
398 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
399 412
400 $self->check_size; 413 $self->check_size;
401} 414}
402 415
403sub find_widget { 416sub find_widget {
435 448
436 $class->SUPER::new (children => [$child], %arg) 449 $class->SUPER::new (children => [$child], %arg)
437} 450}
438 451
439sub add { 452sub add {
440 my ($self, $widget) = @_; 453 my ($self, $child) = @_;
441 454
442 $self->{children} = []; 455 $self->{children} = [];
443 456
444 $self->SUPER::add ($widget); 457 $self->SUPER::add ($child);
445} 458}
446 459
447sub remove { 460sub remove {
448 my ($self, $widget) = @_; 461 my ($self, $widget) = @_;
449 462
608 # TODO: user_x, user_y, overwrite moveto? 621 # TODO: user_x, user_y, overwrite moveto?
609 622
610 $class->SUPER::new ( 623 $class->SUPER::new (
611 bg => [1, 1, 1, 1], 624 bg => [1, 1, 1, 1],
612 border_bg => [1, 1, 1, 1], 625 border_bg => [1, 1, 1, 1],
613 border => int $::FONTSIZE * 0.8, 626 border => 0.8,
614 @_ 627 @_
615 ) 628 )
616} 629}
617 630
631sub border {
632 int $_[0]{border} * $::FONTSIZE
633}
634
618sub size_request { 635sub size_request {
619 my ($self) = @_; 636 my ($self) = @_;
620 637
621 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};
622 639
623 my ($w, $h) = $self->SUPER::size_request; 640 my ($w, $h) = $self->SUPER::size_request;
624 641
625 ( 642 (
626 $w + $self->{border} * 2, 643 $w + $self->border * 2,
627 $h + $self->{border} * 2, 644 $h + $self->border * 2,
628 ) 645 )
629} 646}
630 647
631sub size_allocate { 648sub size_allocate {
632 my ($self, $w, $h) = @_; 649 my ($self, $w, $h) = @_;
633 650
634 $h -= List::Util::max 0, $self->{border} * 2; 651 $h -= List::Util::max 0, $self->border * 2;
635 $w -= List::Util::max 0, $self->{border} * 2; 652 $w -= List::Util::max 0, $self->border * 2;
636 653
637 $self->child->configure ($self->{border}, $self->{border}, $w, $h); 654 $self->child->configure ($self->border, $self->border, $w, $h);
638} 655}
639 656
640sub button_down { 657sub button_down {
641 my ($self, $ev, $x, $y) = @_; 658 my ($self, $ev, $x, $y) = @_;
642 659
660 my $border = $self->border;
661
643 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 662 if ($x < $self->{w} && $x >= $self->{w} - $border
644 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 663 && $y < $self->{h} && $y >= $self->{h} - $border) {
645 664
646 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 665 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
647 my ($bw, $bh) = ($self->{w}, $self->{h}); 666 my ($bw, $bh) = ($self->{w}, $self->{h});
648 667
649 $self->{motion} = sub { 668 $self->{motion} = sub {
655 $self->{user_h} = $bh + $y - $oy; 674 $self->{user_h} = $bh + $y - $oy;
656 $self->check_size; 675 $self->check_size;
657 }; 676 };
658 677
659 } elsif ($x >= 0 && $x < $self->{w} 678 } elsif ($x >= 0 && $x < $self->{w}
660 && $y >= 0 && $y < $self->{border}) { 679 && $y >= 0 && $y < $border) {
661 680
662 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 681 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
663 my ($bx, $by) = ($self->{x}, $self->{y}); 682 my ($bx, $by) = ($self->{x}, $self->{y});
664 683
665 $self->{motion} = sub { 684 $self->{motion} = sub {
694 glEnable GL_BLEND; 713 glEnable GL_BLEND;
695 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 714 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
696 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
698 717
718 my $border = $self->border;
719
699 glColor @{ $self->{border_bg} }; 720 glColor @{ $self->{border_bg} };
700 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 721 $tex[1]->draw_quad (0, 0, $w, $border);
701 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 722 $tex[3]->draw_quad (0, $border, $border, $ch);
702 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 723 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
703 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 724 $tex[4]->draw_quad (0, $h - $border, $w, $border);
704 725
705 my $bg = $tex[0]; 726 my $bg = $tex[0];
706 727
707 # TODO: repeat texture not scale 728 # TODO: repeat texture not scale
708 my $rep_x = $cw / $bg->{w}; 729 my $rep_x = $cw / $bg->{w};
711 glColor @{ $self->{bg} }; 732 glColor @{ $self->{bg} };
712 733
713 $bg->{s} = $rep_x; 734 $bg->{s} = $rep_x;
714 $bg->{t} = $rep_y; 735 $bg->{t} = $rep_y;
715 $bg->{wrap_mode} = 1; 736 $bg->{wrap_mode} = 1;
716 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 737 $bg->draw_quad ($border, $border, $cw, $ch);
717 738
718 glDisable GL_TEXTURE_2D; 739 glDisable GL_TEXTURE_2D;
719 glDisable GL_BLEND; 740 glDisable GL_BLEND;
720 741
721 $self->child->draw; 742 $self->child->draw;
743sub add { 764sub add {
744 my ($self, $x, $y, $child) = @_; 765 my ($self, $x, $y, $child) = @_;
745 766
746 $child->set_parent ($self); 767 $child->set_parent ($self);
747 $self->{children}[$y][$x] = $child; 768 $self->{children}[$y][$x] = $child;
748
749 $self->{w} = $self->{h} = -1;
750 769
751 $child->check_size; 770 $child->check_size;
752} 771}
753 772
754# 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?
987sub new { 1006sub new {
988 my ($class, %arg) = @_; 1007 my ($class, %arg) = @_;
989 1008
990 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
991 fg => [1, 1, 1], 1010 fg => [1, 1, 1],
992 fontsize => $::FONTSIZE, 1011 fontsize => 1,
993 text => "", 1012 text => "",
994 align => -1, 1013 align => -1,
995 valign => -1, 1014 valign => -1,
996 padding => 2, 1015 padding => 2,
997 layout => new CFClient::Layout, 1016 layout => new CFClient::Layout,
1036 1055
1037sub size_request { 1056sub size_request {
1038 my ($self) = @_; 1057 my ($self) = @_;
1039 1058
1040 $self->{layout}->set_width; 1059 $self->{layout}->set_width;
1041 $self->{layout}->set_height ($self->{fontsize}); 1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1042 1061
1043 my ($w, $h) = $self->{layout}->size; 1062 my ($w, $h) = $self->{layout}->size;
1044 1063
1045 ( 1064 (
1046 $w + $self->{padding} * 2, 1065 $w + $self->{padding} * 2,
1052 my ($self, $w, $h) = @_; 1071 my ($self, $w, $h) = @_;
1053 1072
1054 delete $self->{texture}; 1073 delete $self->{texture};
1055} 1074}
1056 1075
1057sub update {
1058 my ($self) = @_;
1059
1060 delete $self->{texture};
1061 $self->SUPER::update;
1062}
1063
1064sub _draw { 1076sub _draw {
1065 my ($self) = @_; 1077 my ($self) = @_;
1066 1078
1067 my $tex = $self->{texture} ||= do { 1079 my $tex = $self->{texture} ||= do {
1068 $self->{layout}->set_width ($self->{w}); 1080 $self->{layout}->set_width ($self->{w});
1069 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1070 new_from_layout CFClient::Texture $self->{layout} 1082 new_from_layout CFClient::Texture $self->{layout}
1071 }; 1083 };
1072 1084
1073 glEnable GL_BLEND; 1085 glEnable GL_BLEND;
1074 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1086 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1099 1111
1100package CFClient::UI::EntryBase; 1112package CFClient::UI::EntryBase;
1101 1113
1102our @ISA = CFClient::UI::Label::; 1114our @ISA = CFClient::UI::Label::;
1103 1115
1104use SDL;
1105use SDL::OpenGL; 1116use SDL::OpenGL;
1106 1117
1107sub new { 1118sub new {
1108 my $class = shift; 1119 my $class = shift;
1109 1120
1124 1135
1125 delete $self->{cur_h}; 1136 delete $self->{cur_h};
1126 1137
1127 return if $self->{text} eq $text; 1138 return if $self->{text} eq $text;
1128 1139
1140 delete $self->{texture};
1141
1129 $self->{last_activity} = $::NOW; 1142 $self->{last_activity} = $::NOW;
1130 $self->{text} = $text; 1143 $self->{text} = $text;
1131 1144
1132 $text =~ s/./*/g if $self->{hidden}; 1145 $text =~ s/./*/g if $self->{hidden};
1133 $self->{layout}->set_text ("$text "); 1146 $self->{layout}->set_text ("$text ");
1162} 1175}
1163 1176
1164sub key_down { 1177sub key_down {
1165 my ($self, $ev) = @_; 1178 my ($self, $ev) = @_;
1166 1179
1167 my $mod = $ev->key_mod; 1180 my $mod = $ev->{mod};
1168 my $sym = $ev->key_sym; 1181 my $sym = $ev->{sym};
1169
1170 my $uni = $ev->key_unicode; 1182 my $uni = $ev->{unicode};
1171 1183
1172 my $text = $self->get_text; 1184 my $text = $self->get_text;
1173 1185
1174 if ($sym == SDLK_BACKSPACE) { 1186 if ($sym == 8) {
1175 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1187 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1176 } elsif ($sym == SDLK_DELETE) { 1188 } elsif ($sym == 127) {
1177 substr $text, $self->{cursor}, 1, ""; 1189 substr $text, $self->{cursor}, 1, "";
1178 } elsif ($sym == SDLK_LEFT) { 1190 } elsif ($sym == CFClient::SDLK_LEFT) {
1179 --$self->{cursor} if $self->{cursor}; 1191 --$self->{cursor} if $self->{cursor};
1180 } elsif ($sym == SDLK_RIGHT) { 1192 } elsif ($sym == CFClient::SDLK_RIGHT) {
1181 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1193 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1182 } elsif ($sym == SDLK_HOME) { 1194 } elsif ($sym == CFClient::SDLK_HOME) {
1183 $self->{cursor} = 0; 1195 $self->{cursor} = 0;
1184 } elsif ($sym == SDLK_END) { 1196 } elsif ($sym == CFClient::SDLK_END) {
1185 $self->{cursor} = length $text; 1197 $self->{cursor} = length $text;
1186 } elsif ($sym == SDLK_ESCAPE) { 1198 } elsif ($sym == 27) {
1187 $self->emit ('escape'); 1199 $self->emit ('escape');
1188 } elsif ($uni) { 1200 } elsif ($uni) {
1189 substr $text, $self->{cursor}++, 0, chr $uni; 1201 substr $text, $self->{cursor}++, 0, chr $uni;
1190 } 1202 }
1191 1203
1266 1278
1267package CFClient::UI::Entry; 1279package CFClient::UI::Entry;
1268 1280
1269our @ISA = CFClient::UI::EntryBase::; 1281our @ISA = CFClient::UI::EntryBase::;
1270 1282
1271use SDL;
1272use SDL::OpenGL; 1283use SDL::OpenGL;
1273 1284
1274sub key_down { 1285sub key_down {
1275 my ($self, $ev) = @_; 1286 my ($self, $ev) = @_;
1276 1287
1277 my $sym = $ev->key_sym; 1288 my $sym = $ev->{sym};
1278 1289
1279 if ($sym == SDLK_RETURN) { 1290 if ($sym == 13) {
1280 $self->emit (activate => $self->get_text); 1291 $self->emit (activate => $self->get_text);
1281 $self->update; 1292 $self->update;
1282 1293
1283 } else { 1294 } else {
1284 $self->SUPER::key_down ($ev); 1295 $self->SUPER::key_down ($ev);
1290 1301
1291package CFClient::UI::Button; 1302package CFClient::UI::Button;
1292 1303
1293our @ISA = CFClient::UI::Label::; 1304our @ISA = CFClient::UI::Label::;
1294 1305
1295use SDL;
1296use SDL::OpenGL; 1306use SDL::OpenGL;
1297 1307
1298my @tex = 1308my @tex =
1299 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1300 qw(b1_button_active.png); 1310 qw(b1_button_active.png);
1354 1364
1355my @tex = 1365my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1357 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1367 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1358 1368
1359use SDL;
1360use SDL::OpenGL; 1369use SDL::OpenGL;
1361 1370
1362sub new { 1371sub new {
1363 my $class = shift; 1372 my $class = shift;
1364 1373
1494 my $h1 = $self->{h} - $ycut * $self->{h}; 1503 my $h1 = $self->{h} - $ycut * $self->{h};
1495 my $h2 = $ycut * $self->{h}; 1504 my $h2 = $ycut * $self->{h};
1496 1505
1497 my $yp = 0; 1506 my $yp = 0;
1498 1507
1499 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1508 glBindTexture GL_TEXTURE_2D, $t1->{name};
1500 glBegin (GL_QUADS); 1509 glBegin GL_QUADS;
1501 glTexCoord (0, 0); glVertex (0 , $yp); 1510 glTexCoord 0 , 0; glVertex 0 , $yp;
1502 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1511 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1503 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1512 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1504 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1513 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1505 glEnd (); 1514 glEnd;
1506 1515
1507 $yp += $h1; 1516 $yp += $h1;
1508 1517
1509 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1518 glBindTexture GL_TEXTURE_2D, $t2->{name};
1510 glBegin (GL_QUADS); 1519 glBegin GL_QUADS;
1511 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1520 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1512 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1521 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1513 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1522 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1514 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1523 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1515 glEnd (); 1524 glEnd;
1516 1525
1517 glDisable GL_BLEND; 1526 glDisable GL_BLEND;
1518 glDisable GL_TEXTURE_2D; 1527 glDisable GL_TEXTURE_2D;
1519} 1528}
1520 1529
1542 # TODO: calculations are off 1551 # TODO: calculations are off
1543 my $self = $class->SUPER::new ( 1552 my $self = $class->SUPER::new (
1544 fg => [1, 1, 1], 1553 fg => [1, 1, 1],
1545 active_fg => [0, 0, 0], 1554 active_fg => [0, 0, 0],
1546 range => [0, 0, 100, 10], 1555 range => [0, 0, 100, 10],
1547 req_w => 40, 1556 req_w => 20,
1548 req_h => 13, 1557 req_h => 20,
1549 vertical => 0, 1558 vertical => 0,
1550 can_hover => 1, 1559 can_hover => 1,
1551 inner_pad => 5, 1560 inner_pad => 5,
1552 @_ 1561 @_
1553 ); 1562 );
1663 1672
1664sub new { 1673sub new {
1665 my $class = shift; 1674 my $class = shift;
1666 1675
1667 my $self = $class->SUPER::new ( 1676 my $self = $class->SUPER::new (
1668 req_w => $::WIDTH / 6,
1669 req_h => $::HEIGHT / 6,
1670 fontsize => $::FONTSIZE, 1677 fontsize => 1,
1671 @_, 1678 @_,
1672 1679
1673 layout => (new CFClient::Layout), 1680 layout => (new CFClient::Layout),
1674 par => [], 1681 par => [],
1675 height => 0, 1682 height => 0,
1696sub text_height { 1703sub text_height {
1697 my ($self, $text) = @_; 1704 my ($self, $text) = @_;
1698 1705
1699 my $layout = $self->{layout}; 1706 my $layout = $self->{layout};
1700 1707
1701 $layout->set_height ($self->{fontsize}); 1708 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1702 $layout->set_width ($self->{w}); 1709 $layout->set_width ($self->{w});
1703 $layout->set_text ($text); 1710 $layout->set_text ($text);
1704 1711
1705 ($layout->size)[1] 1712 ($layout->size)[1]
1706} 1713}
1710 1717
1711 $self->{need_reflow}++; 1718 $self->{need_reflow}++;
1712 $self->update; 1719 $self->update;
1713} 1720}
1714 1721
1715sub size_request {
1716 my ($self) = @_;
1717
1718 ($self->{req_w}, $self->{req_h})
1719}
1720
1721sub size_allocate { 1722sub size_allocate {
1722 my ($self, $w, $h) = @_; 1723 my ($self, $w, $h) = @_;
1723 1724
1725 $self->SUPER::size_allocate ($w, $h);
1726
1724 $self->{layout}->set_height ($self->{fontsize}); 1727 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1725 $self->{layout}->set_width ($self->{w}); 1728 $self->{layout}->set_width ($self->{children}[0]{w});
1726 1729
1727 $self->reflow; 1730 $self->reflow;
1728 $self->update;
1729} 1731}
1730 1732
1731sub add_paragraph { 1733sub add_paragraph {
1732 my ($self, $color, $text) = @_; 1734 my ($self, $color, $text) = @_;
1733 1735
1814 glDisable GL_TEXTURE_2D; 1816 glDisable GL_TEXTURE_2D;
1815 } 1817 }
1816 1818
1817 $self->{children}[1]->draw; 1819 $self->{children}[1]->draw;
1818 1820
1819}
1820
1821#############################################################################
1822
1823package CFClient::UI::MapWidget;
1824
1825use strict;
1826
1827use List::Util qw(min max);
1828
1829use SDL;
1830use SDL::OpenGL;
1831
1832our @ISA = CFClient::UI::Base::;
1833
1834sub new {
1835 my $class = shift;
1836
1837 $class->SUPER::new (
1838 z => -1,
1839 can_focus => 1,
1840 list => (glGenLists 1),
1841 @_
1842 )
1843}
1844
1845sub key_down {
1846 print "MAPKEYDOWN\n";
1847}
1848
1849sub key_up {
1850}
1851
1852sub button_down {
1853 my ($self, $ev, $x, $y) = @_;
1854
1855 $self->focus_in;
1856
1857 if ($ev->button == 2) {
1858 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1859 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1860
1861 $self->{motion} = sub {
1862 my ($ev, $x, $y) = @_;
1863
1864 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1865
1866 $::CFG->{map_shift_x} = $bw + $x - $ox;
1867 $::CFG->{map_shift_y} = $bh + $y - $oy;
1868
1869 $self->update;
1870 };
1871 }
1872}
1873
1874sub button_up {
1875 my ($self, $ev, $x, $y) = @_;
1876
1877 delete $self->{motion};
1878}
1879
1880sub mouse_motion {
1881 my ($self, $ev, $x, $y) = @_;
1882
1883 $self->{motion}->($ev, $x, $y) if $self->{motion};
1884}
1885
1886sub size_request {
1887 (
1888 1 + 32 * int $::WIDTH / 32,
1889 1 + 32 * int $::HEIGHT / 32,
1890 )
1891}
1892
1893sub update {
1894 my ($self) = @_;
1895
1896 $self->{need_update} = 1;
1897 $self->SUPER::update;
1898}
1899
1900sub draw {
1901 my ($self) = @_;
1902
1903 if (delete $self->{need_update}) {
1904 glNewList $self->{list}, GL_COMPILE;
1905
1906 if ($::MAP) {
1907 my $sw = int $::WIDTH / 32;
1908 my $sh = int $::HEIGHT / 32;
1909
1910 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1911 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1912
1913 glTranslate $sx0 - 32, $sy0 - 32, 0;
1914
1915 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1916
1917 if ($::CFG->{fow_enable}) {
1918 if ($::CFG->{fow_smooth}) { # smooth fog of war
1919 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
1920 glConvolutionFilter2D (
1921 GL_CONVOLUTION_2D,
1922 GL_ALPHA,
1923 3, 3,
1924 GL_ALPHA, GL_FLOAT,
1925 pack "f*",
1926 0.1, 0.1, 0.1,
1927 0.1, 0.2, 0.1,
1928 0.1, 0.1, 0.1,
1929 );
1930 glEnable GL_CONVOLUTION_2D;
1931 }
1932
1933 $self->{fow_texture} = new CFClient::Texture
1934 w => $w,
1935 h => $h,
1936 data => $data,
1937 internalformat => GL_ALPHA,
1938 format => GL_ALPHA;
1939
1940 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1941
1942 glEnable GL_BLEND;
1943 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1944 glEnable GL_TEXTURE_2D;
1945 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1946
1947 glColor +($::CFG->{fow_intensity}) x 3, 1;
1948 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1949
1950 glDisable GL_TEXTURE_2D;
1951 glDisable GL_BLEND;
1952 }
1953
1954 # HACK BEGIN
1955 {
1956 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1957 my ($w, $h) = (250, 250);
1958
1959 glEnable GL_BLEND;
1960 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1961 glEnable GL_TEXTURE_2D;
1962 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1963
1964 $self->{mapmap_texture} =
1965 new CFClient::Texture
1966 w => $w,
1967 h => $h,
1968 data => $::MAP->mapmap ($w, $h),
1969 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1970
1971 $self->{mapmap_texture}->draw_quad (100, 100);
1972
1973 glDisable GL_TEXTURE_2D;
1974 glDisable GL_BLEND;
1975 }
1976 # HACK END
1977 }
1978
1979 glEndList;
1980 }
1981
1982 glPushMatrix;
1983 glCallList $self->{list};
1984 glPopMatrix;
1985
1986 if ($FOCUS != $self) {
1987 glColor 64/255, 64/255, 64/255;
1988 glLogicOp GL_AND;
1989 glEnable GL_COLOR_LOGIC_OP;
1990 glBegin GL_QUADS;
1991 glVertex 0, 0;
1992 glVertex 0, $::HEIGHT;
1993 glVertex $::WIDTH, $::HEIGHT;
1994 glVertex $::WIDTH, 0;
1995 glEnd;
1996 glDisable GL_COLOR_LOGIC_OP;
1997 }
1998}
1999
2000my %DIR = (
2001 SDLK_KP8, [1, "north"],
2002 SDLK_KP9, [2, "northeast"],
2003 SDLK_KP6, [3, "east"],
2004 SDLK_KP3, [4, "southeast"],
2005 SDLK_KP2, [5, "south"],
2006 SDLK_KP1, [6, "southwest"],
2007 SDLK_KP4, [7, "west"],
2008 SDLK_KP7, [8, "northwest"],
2009
2010 SDLK_UP, [1, "north"],
2011 SDLK_RIGHT, [3, "east"],
2012 SDLK_DOWN, [5, "south"],
2013 SDLK_LEFT, [7, "west"],
2014);
2015
2016sub key_down {
2017 my ($self, $ev) = @_;
2018
2019 my $mod = $ev->key_mod;
2020 my $sym = $ev->key_sym;
2021
2022 if ($sym == SDLK_KP5) {
2023 $::CONN->user_send ("stay fire");
2024 } elsif ($sym == SDLK_a) {
2025 $::CONN->user_send ("apply");
2026 } elsif ($sym == SDLK_QUOTE) {
2027 $self->emit ('activate_console');
2028 } elsif ($sym == SDLK_SLASH) {
2029 $self->emit ('activate_console' => '/');
2030 } elsif (exists $DIR{$sym}) {
2031 if ($mod & KMOD_SHIFT) {
2032 $self->{shft}++;
2033 $::CONN->user_send ("fire $DIR{$sym}[0]");
2034 } elsif ($mod & KMOD_CTRL) {
2035 $self->{ctrl}++;
2036 $::CONN->user_send ("run $DIR{$sym}[0]");
2037 } else {
2038 $::CONN->user_send ("$DIR{$sym}[1]");
2039 }
2040 }
2041}
2042
2043sub key_up {
2044 my ($self, $ev) = @_;
2045
2046 my $mod = $ev->key_mod;
2047 my $sym = $ev->key_sym;
2048
2049 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2050 $::CONN->user_send ("fire_stop");
2051 }
2052 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2053 $::CONN->user_send ("run_stop");
2054 }
2055} 1821}
2056 1822
2057############################################################################# 1823#############################################################################
2058 1824
2059package CFClient::UI::Animator; 1825package CFClient::UI::Animator;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines