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

Comparing deliantra/Deliantra-Client/DC/MapWidget.pm (file contents):
Revision 1.88 by root, Mon Oct 9 01:06:36 2006 UTC vs.
Revision 1.130 by root, Thu May 8 18:58:55 2008 UTC

1package CFPlus::MapWidget; 1package DC::MapWidget;
2 2
3use strict; 3use strict;
4use utf8; 4use utf8;
5 5
6use List::Util qw(min max); 6use List::Util qw(min max);
7 7
8use CFPlus; 8use DC;
9use CFPlus::OpenGL; 9use DC::OpenGL;
10use CFPlus::UI; 10use DC::UI;
11use DC::Macro;
11 12
12our @ISA = CFPlus::UI::Base::; 13our @ISA = DC::UI::Base::;
13 14
14my $magicmap_tex = 15my $magicmap_tex =
15 new_from_file CFPlus::Texture CFPlus::find_rcfile "magicmap.png", 16 new_from_file DC::Texture DC::find_rcfile "magicmap.png",
16 mipmap => 1, wrap => 0, internalformat => GL_ALPHA; 17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
17 18
18sub new { 19sub new {
19 my $class = shift; 20 my $class = shift;
20 21
21 my $self = $class->SUPER::new ( 22 my $self = $class->SUPER::new (
22 z => -1, 23 z => -1,
23 can_focus => 1, 24 can_focus => 1,
24 list => glGenList, 25 tilesize => 32,
25
26 smooth_matrix => [
27 0.05, 0.13, 0.05,
28 0.13, 0.30, 0.13,
29 0.05, 0.13, 0.05,
30 ],
31
32 @_ 26 @_
33 ); 27 );
34 28
35 $self->{completer} = new CFPlus::MapWidget::Command::
36 command => $self->{command},
37 tooltip => "#completer_help",
38 ;
39
40 $self 29 $self
30}
31
32sub set_tilesize {
33 my ($self, $tilesize) = @_;
34
35 $self->{tilesize} = $tilesize;
41} 36}
42 37
43sub add_command { 38sub add_command {
44 my ($self, $command, $tooltip, $widget, $cb) = @_; 39 my ($self, $command, $tooltip, $widget, $cb) = @_;
45 40
47 42
48 $tooltip =~ s/^\s+//; 43 $tooltip =~ s/^\s+//;
49 $tooltip = "<big>$data</big>\n\n$tooltip"; 44 $tooltip = "<big>$data</big>\n\n$tooltip";
50 $tooltip =~ s/\s+$//; 45 $tooltip =~ s/\s+$//;
51 46
52 $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; 47 $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
53} 48}
54 49
55sub clr_commands { 50sub clr_commands {
56 my ($self) = @_; 51 my ($self) = @_;
57 52
58 %{$self->{completer}{command}} = (); 53 %{$::COMPLETER->{command}} = ();
59 54
60 $self->{completer}->hide 55 $::COMPLETER->hide
61 if $self->{completer}; 56 if $::COMPLETER;
62} 57}
63 58
64sub server_login { 59sub server_login {
65 my ($server) = @_; 60 my ($server) = @_;
66 61
67 ::stop_game (); 62 ::stop_game ();
68 local $::PROFILE->{host} = $server; 63 local $::PROFILE->{host} = $server;
69 ::start_game (); 64 ::start_game ();
70} 65}
71 66
72sub check_lwp($) {
73 my ($res) = @_;
74
75 $res->is_error
76 and die $res->status_line;
77
78 $res
79}
80
81sub editor_invoke { 67sub editor_invoke {
82 my $editsup = $::CONN && $::CONN->{editor_support} 68 my $editsup = $::CONN && $::CONN->{editor_support}
83 or return; 69 or return;
84 70
85 CFPlus::background { 71 DC::background {
86 print "preparing editor startup...\n"; 72 print "preparing editor startup...\n";
87 CFPlus::set_proxy;
88 73
89 my $server = $editsup->{gameserver} || "default"; 74 my $server = $editsup->{gameserver} || "default";
90 $server =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 75 $server =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
91 76
92 local $ENV{CROSSFIRE_MAPDIR} = my $mapdir = "$Crossfire::VARDIR/map.$server"; mkdir $mapdir; 77 local $ENV{CROSSFIRE_MAPDIR} = my $mapdir = "$Deliantra::VARDIR/map.$server"; mkdir $mapdir;
93 local $ENV{CROSSFIRE_LIBDIR} = my $libdir = "$Crossfire::VARDIR/lib.$server"; mkdir $libdir; 78 local $ENV{CROSSFIRE_LIBDIR} = my $libdir = "$Deliantra::VARDIR/lib.$server"; mkdir $libdir;
94 79
95 print "map directory is $mapdir\n"; 80 print "map directory is $mapdir\n";
96 print "lib directory is $libdir\n"; 81 print "lib directory is $libdir\n";
97 82
98 require LWP::UserAgent; 83 my $ua = DC::lwp_useragent;
99
100 my $ua = LWP::UserAgent->new (
101 agent => "cfplus $CFPlus::VERSION",
102 keep_alive => 1,
103 env_proxy => 1,
104 timeout => 30,
105 );
106 84
107 for my $file (qw(archetypes crossfire.0)) { 85 for my $file (qw(archetypes crossfire.0)) {
108 my $url = "$editsup->{lib_root}$file"; 86 my $url = "$editsup->{lib_root}$file";
109 print "mirroring $url...\n"; 87 print "mirroring $url...\n";
110 check_lwp $ua->mirror ($url, "$libdir/$file"); 88 DC::lwp_check $ua->mirror ($url, "$libdir/$file");
111 printf "%s size %d octets\n", $file, -s "$libdir/$file"; 89 printf "%s size %d octets\n", $file, -s "$libdir/$file";
112 } 90 }
113 91
114 if (1) { # upload a map 92 if (1) { # upload a map
115 my $mapname = $::CONN->{map_info}[0]; 93 my $mapname = $::CONN->{map_info}[0];
120 98
121 print "getting map revision for $mapname...\n"; 99 print "getting map revision for $mapname...\n";
122 100
123 # try to get the most recent head revision, what a hack, 101 # try to get the most recent head revision, what a hack,
124 # this should have been returned while downloading *sigh* 102 # this should have been returned while downloading *sigh*
125 my $log = (check_lwp $ua->get ("$editsup->{cvs_root}/$mapname?view=log&logsort=rev"))->decoded_content; 103 my $log = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?view=log&logsort=rev"))->decoded_content;
126 104
127 if ($log =~ /\?rev=(\d+\.\d+)"/) { 105 if ($log =~ /\?rev=(\d+\.\d+)"/) {
128 my $rev = $1; 106 my $rev = $1;
129 107
130 print "downloading revision $rev...\n"; 108 print "downloading revision $rev...\n";
131 109
132 my $map = (check_lwp $ua->get ("$editsup->{cvs_root}/$mapname?rev=$rev"))->decoded_content; 110 my $map = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?rev=$rev"))->decoded_content;
133 111
134 my $meta = { 112 my $meta = {
135 %$editsup, 113 %$editsup,
136 path => $mapname, 114 path => $mapname,
137 revision => $rev, 115 revision => $rev,
142 require File::Path; 120 require File::Path;
143 121
144 File::Path::mkpath (File::Basename::dirname ($mappath)); 122 File::Path::mkpath (File::Basename::dirname ($mappath));
145 open my $fh, ">:raw:perlio", "$mappath.meta" 123 open my $fh, ">:raw:perlio", "$mappath.meta"
146 or die "$mappath.meta: $!\n"; 124 or die "$mappath.meta: $!\n";
147 print $fh CFPlus::to_json $meta; 125 print $fh DC::encode_json $meta;
148 close $fh; 126 close $fh;
149 open my $fh, ">:raw:perlio:utf8", $mappath 127 open my $fh, ">:raw:perlio:utf8", $mappath
150 or die "$mappath: $!\n"; 128 or die "$mappath: $!\n";
151 print $fh $map; 129 print $fh $map;
152 close $fh; 130 close $fh;
193 171
194 if ($ev->{button} == 1) { 172 if ($ev->{button} == 1) {
195 $self->grab_focus; 173 $self->grab_focus;
196 return unless $::CONN; 174 return unless $::CONN;
197 175
198 my $x = 1 + CFPlus::floor +($ev->{x} - $self->{sx0}) / $self->{tilesize} - $self->{sx}; 176 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize};
199 my $y = 1 + CFPlus::floor +($ev->{y} - $self->{sy0}) / $self->{tilesize} - $self->{sy}; 177 my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize};
200 178
201 $x -= int 0.5 * $self->{sw}; 179 $x -= DC::floor $::MAP->w * 0.5;
202 $y -= int 0.5 * $self->{sh}; 180 $y -= DC::floor $::MAP->h * 0.5;
203 181
182 if ($::CONN) {
204 $::CONN->lookat ($x, $y) 183 $::CONN->lookat ($x, $y)
205 if $::CONN; 184 }
206 185
207 } elsif ($ev->{button} == 2) { 186 } elsif ($ev->{button} == 2) {
208 $self->grab_focus; 187 $self->grab_focus;
209 return unless $::CONN; 188 return unless $::CONN;
210 189
227 ["Statistics\tF2", sub { ::toggle_player_page ($::STATS_PAGE) }], 206 ["Statistics\tF2", sub { ::toggle_player_page ($::STATS_PAGE) }],
228 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], 207 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
229 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], 208 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
230 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], 209 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
231 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], 210 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
232 ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], 211# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
233 [ 212 [
234 $::PICKUP_ENABLE->{state} 213 $::PICKUP_ENABLE->{state}
235 ? "Disable automatic pickup" 214 ? "Disable automatic pickup"
236 : "Enable automatic pickup", 215 : "Enable automatic pickup",
237 sub { $::PICKUP_ENABLE->toggle } 216 sub { $::PICKUP_ENABLE->toggle }
238 ], 217 ],
239 ); 218 );
240 219
241 if ($::CONN && $::CONN->{editor_support}) { 220 if ($::CONN && $::CONN->{editor_support}) {
242 push @items, [ 221# push @items, [
243 "Edit this map <span size='xx-small'>(" . (CFPlus::asxml $::CONN->{map_info}[0]) . ")</span>", 222# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>",
244 \&editor_invoke, 223# \&editor_invoke,
245 ]; 224# ];
246 225
247 for my $type (qw(test name)) { 226 for my $type (qw(test name)) {
248 $::CONN->{editor_support}{type} ne $type 227 $::CONN->{editor_support}{type} ne $type
249 or next; 228 or next;
250 my $server = $::CONN->{editor_support}{"${type}server"} 229 my $server = $::CONN->{editor_support}{"${type}server"}
251 or next; 230 or next;
252 231
253 push @items, [ 232 push @items, [
254 "Login on $type server <span size='xx-small'>(" . (CFPlus::asxml $server) . ")</span>", 233 "Login on $type server <span size='xx-small'>(" . (DC::asxml $server) . ")</span>",
255 sub { server_login $server }, 234 sub { server_login $server },
256 ]; 235 ];
257 } 236 }
258 } 237 }
259 238
267 } 246 }
268 } 247 }
269 ], 248 ],
270 ; 249 ;
271 250
272 (new CFPlus::UI::Menu 251 (new DC::UI::Menu
273 items => \@items, 252 items => \@items,
274 )->popup ($ev); 253 )->popup ($ev);
275 } 254 }
276 255
277 1 256 1
296 275
297 1 276 1
298} 277}
299 278
300sub size_request { 279sub size_request {
280 my ($self) = @_;
281
301 ( 282 (
302 32 * CFPlus::ceil $::WIDTH / 32, 283 $self->{tilesize} * DC::ceil $::WIDTH / $self->{tilesize},
303 32 * CFPlus::ceil $::HEIGHT / 32, 284 $self->{tilesize} * DC::ceil $::HEIGHT / $self->{tilesize},
304 ) 285 )
305} 286}
306 287
307sub update { 288sub update {
308 my ($self) = @_; 289 my ($self) = @_;
310 $self->{need_update} = 1; 291 $self->{need_update} = 1;
311 $self->SUPER::update; 292 $self->SUPER::update;
312} 293}
313 294
314my %DIR = ( 295my %DIR = (
296 ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
315 CFPlus::SDLK_KP8, [1, "north"], 297 ( "," . DC::SDLK_KP8 ), [1, "north"],
316 CFPlus::SDLK_KP9, [2, "northeast"], 298 ( "," . DC::SDLK_KP9 ), [2, "northeast"],
317 CFPlus::SDLK_KP6, [3, "east"], 299 ( "," . DC::SDLK_KP6 ), [3, "east"],
318 CFPlus::SDLK_KP3, [4, "southeast"], 300 ( "," . DC::SDLK_KP3 ), [4, "southeast"],
319 CFPlus::SDLK_KP2, [5, "south"], 301 ( "," . DC::SDLK_KP2 ), [5, "south"],
320 CFPlus::SDLK_KP1, [6, "southwest"], 302 ( "," . DC::SDLK_KP1 ), [6, "southwest"],
321 CFPlus::SDLK_KP4, [7, "west"], 303 ( "," . DC::SDLK_KP4 ), [7, "west"],
322 CFPlus::SDLK_KP7, [8, "northwest"], 304 ( "," . DC::SDLK_KP7 ), [8, "northwest"],
323 305
324 CFPlus::SDLK_UP, [1, "north"], 306 ( "," . DC::SDLK_UP ), [1, "north"],
307 ("1," . DC::SDLK_UP ), [2, "northeast"],
325 CFPlus::SDLK_RIGHT, [3, "east"], 308 ( "," . DC::SDLK_RIGHT), [3, "east"],
309 ("1," . DC::SDLK_RIGHT), [4, "southeast"],
326 CFPlus::SDLK_DOWN, [5, "south"], 310 ( "," . DC::SDLK_DOWN ), [5, "south"],
311 ("1," . DC::SDLK_DOWN ), [6, "southwest"],
327 CFPlus::SDLK_LEFT, [7, "west"], 312 ( "," . DC::SDLK_LEFT ), [7, "west"],
313 ("1," . DC::SDLK_LEFT ), [8, "northwest"],
328); 314);
329 315
330sub invoke_key_down { 316sub invoke_key_down {
331 my ($self, $ev) = @_; 317 my ($self, $ev) = @_;
332 318
333 my $mod = $ev->{mod}; 319 my $mod = $ev->{mod};
334 my $sym = $ev->{sym}; 320 my $sym = $ev->{sym};
335 my $uni = $ev->{unicode}; 321 my $uni = $ev->{unicode};
336 322
337 $mod &= CFPlus::KMOD_CTRL | CFPlus::KMOD_ALT | CFPlus::KMOD_SHIFT; 323 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_SHIFT;
338 324
339 if ($uni == ord "\t") { 325 # ignore repeated keypresses
340 $::PL_WINDOW->toggle_visibility; 326 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym;
341 } elsif ($sym == CFPlus::SDLK_F1 && !$mod) { 327 $self->{last_mod} = $mod;
342 $::HELP_WINDOW->toggle_visibility; 328 $self->{last_sym} = $sym;
343 } elsif ($sym == CFPlus::SDLK_F2 && !$mod) {
344 ::toggle_player_page ($::STATS_PAGE);
345 } elsif ($sym == CFPlus::SDLK_F3 && !$mod) {
346 ::toggle_player_page ($::SKILL_PAGE);
347 } elsif ($sym == CFPlus::SDLK_F4 && !$mod) {
348 ::toggle_player_page ($::SPELL_PAGE);
349 } elsif ($sym == CFPlus::SDLK_F5 && !$mod) {
350 ::toggle_player_page ($::INVENTORY_PAGE);
351 } elsif ($sym == CFPlus::SDLK_F9 && !$mod) {
352 $::SETUP_DIALOG->toggle_visibility;
353 } elsif ($sym == CFPlus::SDLK_INSERT && $mod & CFPlus::KMOD_CTRL) {
354 $::BIND_EDITOR->set_binding (undef, undef, [],
355 sub {
356 my ($mod, $sym, $cmds) = @_;
357 $::BIND_EDITOR->cfg_bind ($mod, $sym, $cmds);
358 });
359 $::BIND_EDITOR->start;
360 $::BIND_EDITOR->show;
361#TODO: elmex, what was this supposed to do? it currently crashes the client.
362# } elsif ($sym == CFPlus::SDLK_INSERT && not ($mod & CFPlus::KMOD_CTRL)) {
363# $::BIND_EDITOR->stop;
364# $::BIND_EDITOR->ask_for_bind_and_commit;
365# $::BIND_EDITOR->hide;
366 } elsif (!$::CONN) {
367 return 0; # bindings further down need a valid connection
368 329
369 } elsif ($sym == CFPlus::SDLK_KP5 && !$mod) { 330 if ($::CONN && (my $dir = $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"})) {
370 $::CONN->user_send ("stay fire");
371 } elsif ($uni == ord ",") {
372 $::CONN->user_send ("take");
373 } elsif ($uni == ord " ") {
374 $::CONN->user_send ("apply");
375 } elsif ($uni == 13) {
376 $::CONN->user_send ("examine");
377 } elsif ($uni == ord ".") {
378 $::CONN->user_send ($self->{completer}{last_command})
379 if exists $self->{completer}{last_command};
380 } elsif (my $bind_cmd = $::PROFILE->{bindings}{$mod}{$sym}) {
381 $::CONN->user_send ($_) for @$bind_cmd;
382 } elsif (($sym == CFPlus::SDLK_KP_PLUS && !$mod) || $uni == ord "+") {
383 $::CONN->user_send ("rotateshoottype +");
384 } elsif (($sym == CFPlus::SDLK_KP_MINUS && !$mod) || $uni == ord "-") {
385 $::CONN->user_send ("rotateshoottype -");
386 } elsif ($uni == ord '"') {
387 $self->{completer}->set_prefix ("$::CFG->{say_command} ");
388 $self->{completer}->show;
389 } elsif ($uni == ord "'") {
390 $self->{completer}->set_prefix ("");
391 $self->{completer}->show;
392 } elsif (exists $DIR{$sym}) {
393 if ($mod & CFPlus::KMOD_SHIFT) { 331 if ($mod & DC::KMOD_SHIFT) {
394 $self->{shft}++; 332 $self->{shft}++;
395 if ($DIR{$sym}[0] != $self->{fire_dir}) { 333 if ($dir->[0] != $self->{fire_dir}) {
396 $::CONN->user_send ("fire $DIR{$sym}[0]"); 334 $::CONN->user_send ("fire $dir->[0]");
397 } 335 }
398 $self->{fire_dir} = $DIR{$sym}[0]; 336 $self->{fire_dir} = $dir->[0];
399 } elsif ($mod & CFPlus::KMOD_CTRL) { 337 } elsif ($mod & DC::KMOD_CTRL) {
400 $self->{ctrl}++; 338 $self->{ctrl}++;
401 $::CONN->user_send ("run $DIR{$sym}[0]"); 339 $::CONN->user_send ("run $dir->[0]");
402 } else { 340 } else {
403 $::CONN->user_send ("$DIR{$sym}[1]"); 341 $::CONN->user_send ("$dir->[1]");
404 } 342 }
405 } elsif ((ord 'a') <= $uni && $uni <= (ord 'z')) { 343
406 $self->{completer}->inject_key_down ($ev);
407 $self->{completer}->show;
408 } else {
409 return 0; 344 return 1;
410 }
411
412 1 345 }
346
347 0
413} 348}
414 349
415sub invoke_key_up { 350sub invoke_key_up {
416 my ($self, $ev) = @_; 351 my ($self, $ev) = @_;
417 352
418 my $res = 0; 353 my $res = 0;
419 my $mod = $ev->{mod}; 354 my $mod = $ev->{mod};
420 my $sym = $ev->{sym}; 355 my $sym = $ev->{sym};
421 356
422 if ($::CFG->{shift_fire_stop}) { 357 if ($::CFG->{shift_fire_stop}) {
423 if (!($mod & CFPlus::KMOD_SHIFT) && delete $self->{shft}) { 358 if (!($mod & DC::KMOD_SHIFT) && delete $self->{shft}) {
424 $::CONN->user_send ("fire_stop"); 359 $::CONN->user_send ("fire_stop");
425 delete $self->{fire_dir}; 360 delete $self->{fire_dir};
426 $res = 1; 361 $res = 1;
427 } 362 }
428 } else { 363 } else {
429 if (exists $DIR{$sym} && delete $self->{shft}) { 364 if (exists $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"} && delete $self->{shft}) {
430 $::CONN->user_send ("fire_stop"); 365 $::CONN->user_send ("fire_stop");
431 delete $self->{fire_dir}; 366 delete $self->{fire_dir};
432 $res = 1; 367 $res = 1;
433 } elsif (($sym == CFPlus::SDLK_LSHIFT || $sym == CFPlus::SDLK_RSHIFT) && delete $self->{shft}) { # XXX: is RSHIFT ok? 368 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT)
369 && delete $self->{shft}) { # XXX: is RSHIFT ok?
434 $::CONN->user_send ("fire_stop"); 370 $::CONN->user_send ("fire_stop");
435 delete $self->{fire_dir}; 371 delete $self->{fire_dir};
436 $res = 1; 372 $res = 1;
437 } 373 }
438 } 374 }
439 375
440 if (!($mod & CFPlus::KMOD_CTRL ) && delete $self->{ctrl}) { 376 if (!($mod & DC::KMOD_CTRL ) && delete $self->{ctrl}) {
441 $::CONN->user_send ("run_stop"); 377 $::CONN->user_send ("run_stop");
442 $res = 1; 378 $res = 1;
443 } 379 }
444 380
445 $res 381 $res
446} 382}
447 383
384sub invoke_visibility_change {
385 my ($self) = @_;
386
387 $self->refresh_hook;
388
389 0
390}
391
448sub set_magicmap { 392sub set_magicmap {
449 my ($self, $w, $h, $x, $y, $data) = @_; 393 my ($self, $w, $h, $x, $y, $data) = @_;
450 394
451 $x -= $::MAP->ox + int 0.5 * $::MAP->w; 395 $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w;
452 $y -= $::MAP->oy + int 0.5 * $::MAP->h; 396 $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h;
453 397
454 $self->{magicmap} = [$x, $y, $w, $h, $data]; 398 $self->{magicmap} = [$x, $y, $w, $h, $data];
455 399
456 $self->update; 400 $self->update;
457} 401}
458 402
403sub refresh_hook {
404 my ($self) = @_;
405
406 if ($::MAP && $::CONN) {
407 if (delete $self->{need_update}) {
408 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
409
410 my $sw = $self->{sw} = 1 + DC::ceil $self->{w} / $tilesize;
411 my $sh = $self->{sh} = 1 + DC::ceil $self->{h} / $tilesize;
412
413 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize;
414 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize;
415
416 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
417 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
418
419 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
420 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
421
422 if ($::CFG->{fow_enable}) {
423 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
424
425 $self->{fow_texture} = new DC::Texture
426 w => $w,
427 h => $h,
428 data => $data,
429 internalformat => GL_ALPHA,
430 format => GL_ALPHA;
431 } else {
432 delete $self->{fow_texture};
433 }
434
435 glNewList ($self->{list} ||= glGenList);
436
437 glPushMatrix;
438 glTranslate $sx0, $sy0;
439 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
440
441 $::MAP->draw ($dx, $dy, $sw, $sh, $self->{tilesize});
442
443 glScale $self->{tilesize}, $self->{tilesize};
444
445 if (my $tex = $self->{fow_texture}) {
446 glPushMatrix;
447 glScale 1/3, 1/3;
448 glEnable GL_TEXTURE_2D;
449 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
450
451 glColor +($::CFG->{fow_intensity}) x 3, 0.9;
452 $self->{fow_texture}->draw_quad_alpha (0, 0);
453
454 glDisable GL_TEXTURE_2D;
455 glPopMatrix;
456 }
457
458 if ($self->{magicmap}) {
459 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
460
461 $x += $::MAP->ox + $self->{dx};
462 $y += $::MAP->oy + $self->{dy};
463
464 glTranslate - $x - 1, - $y - 1;
465 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
466 $::MAP->draw_magicmap ($x, $y, $w, $h, $data);
467 }
468
469 glPopMatrix;
470 glEndList;
471 }
472 } else {
473 glDeleteList delete $self->{list}
474 if $self->{list};
475 }
476}
477
459sub draw { 478sub draw {
460 my ($self) = @_; 479 my ($self) = @_;
461 480
462 return unless $::MAP; 481 $self->{root}->on_post_alloc (prepare => sub { $self->refresh_hook });
463 482
483 return unless $self->{list};
484
464 my $focused = $CFPlus::UI::FOCUS == $self 485 my $focused = $DC::UI::FOCUS == $self
465 || $CFPlus::UI::FOCUS == $self->{completer}{entry}; 486 || $DC::UI::FOCUS == $::COMPLETER->{entry};
466 487
467 return 488 return
468 unless $focused || !$::FAST; 489 unless $focused || !$::FAST;
469 490
470 if (delete $self->{need_update}) {
471 my $tilesize = $self->{tilesize} = int 32 * $::CFG->{map_scale};
472
473 my $sx = $self->{sx} = CFPlus::ceil $::CFG->{map_shift_x} / $tilesize;
474 my $sy = $self->{sy} = CFPlus::ceil $::CFG->{map_shift_y} / $tilesize;
475
476 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
477 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
478
479 my $sw = $self->{sw} = 1 + CFPlus::ceil $self->{w} / $tilesize;
480 my $sh = $self->{sh} = 1 + CFPlus::ceil $self->{h} / $tilesize;
481
482 if ($::CFG->{fow_enable}) {
483 my ($w, $h, $data) = $::MAP->fow_texture ($sx, $sy, 0, 0, $sw, $sh);
484
485 if ($::CFG->{fow_smooth} && $CFPlus::OpenGL::GL_VERSION >= 1.2) { # smooth fog of war
486 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
487 glConvolutionFilter2D (
488 GL_CONVOLUTION_2D,
489 GL_ALPHA,
490 3, 3,
491 GL_ALPHA, GL_FLOAT,
492 (pack "f*", @{ $self->{smooth_matrix} }),
493 );
494 glEnable GL_CONVOLUTION_2D;
495 }
496
497 $self->{fow_texture} = new CFPlus::Texture
498 w => $w,
499 h => $h,
500 data => $data,
501 internalformat => GL_ALPHA,
502 format => GL_ALPHA;
503
504 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
505 } else {
506 delete $self->{fow_texture};
507 }
508
509 glNewList $self->{list};
510
511 glPushMatrix;
512 glTranslate $sx0, $sy0;
513 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
514
515 $::MAP->draw ($sx, $sy, 0, 0, $sw, $sh);
516
517 glScale 32, 32;
518
519 if (my $tex = $self->{fow_texture}) {
520 glEnable GL_TEXTURE_2D;
521 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
522
523 glColor +($::CFG->{fow_intensity}) x 3, 0.9;
524 $self->{fow_texture}->draw_quad_alpha (0, 0);
525
526 glDisable GL_TEXTURE_2D;
527 }
528
529 if ($self->{magicmap}) {
530 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
531
532 $x += $::MAP->ox - $sx + int 0.5 * ($::MAP->w - $sw + 1);
533 $y += $::MAP->oy - $sy + int 0.5 * ($::MAP->h - $sh + 1);
534
535 glTranslate - $x - 1, - $y - 1;
536 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
537 $::MAP->draw_magicmap ($x, $y, $w, $h, $data);
538 }
539
540 glPopMatrix;
541 glEndList;
542 }
543
544 glCallList $self->{list}; 491 glCallList $self->{list};
545 492
546 # TNT2 emulates logops in software (or worse :) 493 # TNT2 emulates logops in software (or worse :)
547 unless ($focused) { 494 unless ($focused) {
548 glColor 0.4, 0.2, 0.2, 0.6; 495 glColor_premultiply 0, 0, 1, 0.25;
549 glEnable GL_BLEND; 496 glEnable GL_BLEND;
550 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 497 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
551 glBegin GL_QUADS; 498 glBegin GL_QUADS;
552 glVertex 0, 0; 499 glVertex 0, 0;
553 glVertex 0, $::HEIGHT; 500 glVertex 0, $::HEIGHT;
554 glVertex $::WIDTH, $::HEIGHT; 501 glVertex $::WIDTH, $::HEIGHT;
555 glVertex $::WIDTH, 0; 502 glVertex $::WIDTH, 0;
564 glDeleteList $self->{list}; 511 glDeleteList $self->{list};
565 512
566 $self->SUPER::DESTROY; 513 $self->SUPER::DESTROY;
567} 514}
568 515
569package CFPlus::MapWidget::MapMap; 516package DC::MapWidget::MapMap;
570 517
518use strict;
519use utf8;
520
571our @ISA = CFPlus::UI::Base::; 521our @ISA = DC::UI::Base::;
572 522
573use Time::HiRes qw(time); 523use Time::HiRes qw(time);
574use CFPlus::OpenGL; 524use DC::OpenGL;
575 525
576sub size_request { 526sub size_request {
577 ($::HEIGHT * 0.25, $::HEIGHT * 0.25) 527 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
528}
529
530sub refresh_hook {
531 my ($self) = @_;
532
533 if ($::MAP && $self->{texture_atime} < time) {
534 my ($w, $h) = @$self{qw(w h)};
535
536 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
537 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
538
539 my $ox = 0.5 * ($w - $sw);
540 my $oy = 0.5 * ($h - $sh);
541
542 my $sx = int $::CFG->{map_shift_x} / $::MAPWIDGET->{tilesize};
543 my $sy = int $::CFG->{map_shift_y} / $::MAPWIDGET->{tilesize};
544
545 #TODO: map scale is completely borked
546
547 my $x0 = int $ox - $sx + 0.5;
548 my $y0 = int $oy - $sy + 0.5;
549
550 $self->{sw} = $sw;
551 $self->{sh} = $sh;
552
553 $self->{x0} = $x0;
554 $self->{y0} = $y0;
555
556 $self->{texture_atime} = time + 1/3;
557
558 $self->{texture} =
559 new DC::Texture
560 w => $w,
561 h => $h,
562 data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
563 type => $DC::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
564 }
565}
566
567sub invoke_visibility_change {
568 my ($self) = @_;
569
570 $self->refresh_hook;
571
572 0
578} 573}
579 574
580sub invoke_size_allocate { 575sub invoke_size_allocate {
581 my ($self, $w, $h) = @_; 576 my ($self, $w, $h) = @_;
582 577
593} 588}
594 589
595sub _draw { 590sub _draw {
596 my ($self) = @_; 591 my ($self) = @_;
597 592
598 $::MAP or return; 593 $self->{root}->on_post_alloc (texture => sub { $self->refresh_hook });
599 594
600 my ($w, $h) = @$self{qw(w h)}; 595 $self->{texture} or return;
601
602 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
603 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
604
605 my $sx = int $::CFG->{map_shift_x} / 32;
606 my $sy = int $::CFG->{map_shift_y} / 32;
607
608 my $ox = 0.5 * ($w - $sw);
609 my $oy = 0.5 * ($h - $sh);
610 596
611 glEnable GL_BLEND; 597 glEnable GL_BLEND;
612 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 598 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
613 glEnable GL_TEXTURE_2D; 599 glEnable GL_TEXTURE_2D;
614 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 600 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
615 601
616 if ($self->{texture_atime} < time) {
617 $self->{texture_atime} = time + 1/3;
618
619 $self->{texture} =
620 new CFPlus::Texture
621 w => $w,
622 h => $h,
623 data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
624 type => $CFPlus::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
625 }
626
627 $self->{texture}->draw_quad (0, 0); 602 $self->{texture}->draw_quad (0, 0);
628 603
629 glDisable GL_TEXTURE_2D; 604 glDisable GL_TEXTURE_2D;
630 605
631 glTranslate 0.375, 0.375; 606 glTranslate 0.375, 0.375;
632
633 #TODO: map scale is completely borked
634
635 my $x0 = int $ox - $sx + 0.5;
636 my $y0 = int $oy - $sy + 0.5;
637 607
638 glColor 1, 1, 0, 1; 608 glColor 1, 1, 0, 1;
639 glBegin GL_LINE_LOOP; 609 glBegin GL_LINE_LOOP;
640 glVertex $x0 , $y0 ; 610 glVertex $self->{x0} , $self->{y0} ;
641 glVertex $x0 , $y0 + $sh; 611 glVertex $self->{x0} , $self->{y0} + $self->{sh};
642 glVertex $x0 + $sw, $y0 + $sh; 612 glVertex $self->{x0} + $self->{sw}, $self->{y0} + $self->{sh};
643 glVertex $x0 + $sw, $y0 ; 613 glVertex $self->{x0} + $self->{sw}, $self->{y0} ;
644 glEnd; 614 glEnd;
645 615
646 glDisable GL_BLEND; 616 glDisable GL_BLEND;
647} 617}
648 618
649package CFPlus::MapWidget::Command; 619package DC::MapWidget::Command;
650 620
651use strict; 621use strict;
652 622
653use CFPlus::OpenGL; 623use DC::OpenGL;
654 624
655our @ISA = CFPlus::UI::Frame::; 625our @ISA = DC::UI::Frame::;
656 626
657sub new { 627sub new {
658 my $class = shift; 628 my $class = shift;
659 629
660 my $self = $class->SUPER::new ( 630 my $self = $class->SUPER::new (
661 bg => [0, 0, 0, 0.8], 631 bg => [0, 0, 0, 0.8],
662 @_, 632 @_,
663 ); 633 );
664 634
665 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 635 $self->add ($self->{vbox} = new DC::UI::VBox);
666 636
667 $self->{label} = [ 637 $self->{label} = [
668 map 638 map
669 CFPlus::UI::Label->new ( 639 DC::UI::Label->new (
640 align => 0,
670 can_hover => 1, 641 can_hover => 1,
671 can_events => 1, 642 can_events => 1,
672 tooltip_width => 0.33, 643 tooltip_width => 0.33,
673 fontsize => $_, 644 fontsize => $_,
674 ), (0.8) x 16 645 ), (0.8) x 16
675 ]; 646 ];
676 647
677 $self->{entry} = new CFPlus::UI::Entry 648 $self->{entry} = new DC::UI::Entry
678 on_changed => sub { 649 on_changed => sub {
679 $self->update_labels; 650 $self->update_labels;
680 0 651 0
681 }, 652 },
682 on_button_down => sub { 653 on_button_down => sub {
683 my ($entry, $ev, $x, $y) = @_; 654 my ($entry, $ev, $x, $y) = @_;
684 655
685 if ($ev->{button} == 3) { 656 if ($ev->{button} == 3) {
686 (new CFPlus::UI::Menu 657 (new DC::UI::Menu
687 items => [ 658 items => [
688 ["bind <i>" . (CFPlus::asxml $self->{select}) . "</i> to a key" 659 ["bind <i>" . (DC::asxml $self->{select}) . "</i> to a key"
689 => sub { $::BIND_EDITOR->do_quick_binding ([$self->{select}], sub { $entry->grab_focus }) }] 660 => sub { DC::Macro::quick_macro [$self->{select}], sub { $entry->grab_focus } }]
690 ], 661 ],
691 )->popup ($ev); 662 )->popup ($ev);
692 return 1; 663 return 1;
693 } 664 }
694 0 665 0
709 $self->hide; 680 $self->hide;
710 } 681 }
711 } elsif ($ev->{sym} == 27) { 682 } elsif ($ev->{sym} == 27) {
712 $self->{hist_ptr} = 0; 683 $self->{hist_ptr} = 0;
713 $self->hide; 684 $self->hide;
714 } elsif ($ev->{sym} == CFPlus::SDLK_DOWN) { 685 } elsif ($ev->{sym} == DC::SDLK_DOWN) {
715 if ($self->{hist_ptr} > 1) { 686 if ($self->{hist_ptr} > 1) {
716 $self->{hist_ptr}--; 687 $self->{hist_ptr}--;
717 $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1]); 688 $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1]);
718 } elsif ($self->{hist_ptr} > 0) { 689 } elsif ($self->{hist_ptr} > 0) {
719 $self->{hist_ptr}--; 690 $self->{hist_ptr}--;
721 } else { 692 } else {
722 ++$self->{select_offset} 693 ++$self->{select_offset}
723 if $self->{select_offset} < $#{ $self->{last_match} || [] }; 694 if $self->{select_offset} < $#{ $self->{last_match} || [] };
724 } 695 }
725 $self->update_labels; 696 $self->update_labels;
726 } elsif ($ev->{sym} == CFPlus::SDLK_UP) { 697 } elsif ($ev->{sym} == DC::SDLK_UP) {
727 if ($self->{select_offset}) { 698 if ($self->{select_offset}) {
728 --$self->{select_offset} 699 --$self->{select_offset}
729 } else { 700 } else {
730 unless ($self->{hist_ptr}) { 701 unless ($self->{hist_ptr}) {
731 $self->{hist_saveback} = $self->{entry}->get_text; 702 $self->{hist_saveback} = $self->{entry}->get_text;
889 860
890sub _draw { 861sub _draw {
891 my ($self) = @_; 862 my ($self) = @_;
892 863
893 # hack 864 # hack
894 local $CFPlus::UI::FOCUS = $self->{entry}; 865 local $DC::UI::FOCUS = $self->{entry};
895 866
896 $self->SUPER::_draw; 867 $self->SUPER::_draw;
897} 868}
898 869
8991 8701

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines