ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.168
Committed: Wed Nov 21 13:23:10 2012 UTC (11 years, 6 months ago) by root
Branch: MAIN
Changes since 1.167: +0 -12 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.126 package DC::MapWidget;
2 root 1.1
3 root 1.162 use common::sense;
4 root 1.1
5     use List::Util qw(min max);
6    
7 root 1.164 use AnyEvent ();
8    
9 root 1.126 use DC;
10     use DC::OpenGL;
11     use DC::UI;
12     use DC::Macro;
13 root 1.1
14 root 1.126 our @ISA = DC::UI::Base::;
15 root 1.1
16 root 1.159 our @TEX_HIDDEN = map {
17     new_from_resource DC::Texture # MUST be POT
18     "hidden-$_.png", mipmap => 1, wrap => 1
19     } 0, 1, 2;
20    
21 root 1.61 my $magicmap_tex =
22 root 1.143 new_from_resource DC::Texture "magicmap.png",
23 root 1.61 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
24    
25 root 1.1 sub new {
26     my $class = shift;
27    
28 root 1.20 my $self = $class->SUPER::new (
29 root 1.1 z => -1,
30     can_focus => 1,
31 root 1.104 tilesize => 32,
32 root 1.1 @_
33 root 1.20 );
34    
35     $self
36 root 1.1 }
37    
38 root 1.85 sub server_login {
39     my ($server) = @_;
40 root 1.83
41     ::stop_game ();
42     local $::PROFILE->{host} = $server;
43     ::start_game ();
44     }
45    
46     sub editor_invoke {
47 root 1.85 my $editsup = $::CONN && $::CONN->{editor_support}
48     or return;
49 root 1.83
50 root 1.126 DC::background {
51 root 1.85 print "preparing editor startup...\n";
52    
53     my $server = $editsup->{gameserver} || "default";
54     $server =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
55    
56 root 1.124 local $ENV{CROSSFIRE_MAPDIR} = my $mapdir = "$Deliantra::VARDIR/map.$server"; mkdir $mapdir;
57     local $ENV{CROSSFIRE_LIBDIR} = my $libdir = "$Deliantra::VARDIR/lib.$server"; mkdir $libdir;
58 root 1.85
59     print "map directory is $mapdir\n";
60     print "lib directory is $libdir\n";
61    
62 root 1.126 my $ua = DC::lwp_useragent;
63 root 1.84
64 root 1.85 for my $file (qw(archetypes crossfire.0)) {
65     my $url = "$editsup->{lib_root}$file";
66     print "mirroring $url...\n";
67 root 1.126 DC::lwp_check $ua->mirror ($url, "$libdir/$file");
68 root 1.85 printf "%s size %d octets\n", $file, -s "$libdir/$file";
69     }
70    
71     if (1) { # upload a map
72     my $mapname = $::CONN->{map_info}[0];
73    
74 root 1.87 my $mappath = "$mapdir/$mapname";
75    
76     -e $mappath and die "$mappath already exists\n";
77    
78 root 1.85 print "getting map revision for $mapname...\n";
79    
80     # try to get the most recent head revision, what a hack,
81     # this should have been returned while downloading *sigh*
82 root 1.126 my $log = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?view=log&logsort=rev"))->decoded_content;
83 root 1.84
84     if ($log =~ /\?rev=(\d+\.\d+)"/) {
85     my $rev = $1;
86    
87 root 1.85 print "downloading revision $rev...\n";
88 root 1.84
89 root 1.126 my $map = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?rev=$rev"))->decoded_content;
90 root 1.84
91 root 1.86 my $meta = {
92     %$editsup,
93     path => $mapname,
94     revision => $rev,
95     cf_login => $::PROFILE->{user},
96     };
97    
98     require File::Basename;
99     require File::Path;
100    
101     File::Path::mkpath (File::Basename::dirname ($mappath));
102     open my $fh, ">:raw:perlio", "$mappath.meta"
103 root 1.87 or die "$mappath.meta: $!\n";
104 root 1.126 print $fh DC::encode_json $meta;
105 root 1.86 close $fh;
106     open my $fh, ">:raw:perlio:utf8", $mappath
107 root 1.87 or die "$mappath: $!\n";
108 root 1.86 print $fh $map;
109     close $fh;
110    
111     print "saved as $mappath\n";
112    
113     print "invoking editor...\n";
114     exec "/root/s2/gce $mappath";#d#
115    
116 root 1.85 # now upload it
117     # require HTTP::Request::Common;
118     #
119     # my $res = $ua->post (
120     # $ENV{CFPLUS_UPLOAD},
121     # Content_Type => 'multipart/form-data',
122     # Content => [
123     # path => $mapname,
124     # mapdir => $ENV{CROSSFIRE_MAPDIR},
125     # map => $map,
126     # revision => $rev,
127     # cf_login => $ENV{CFPLUS_LOGIN},
128     # cf_password => $ENV{CFPLUS_PASSWORD},
129     # comment => "",
130     # ]
131     # );
132     #
133     # if ($res->is_error) {
134     # # fatal condition
135     # warn $res->status_line;
136     # } else {
137     # # script replies are marked as {{..}}
138     # my @msgs = $res->decoded_content =~ m/\{\{(.*?)\}\}/g;
139     # warn map "$_\n", @msgs;
140     # }
141 root 1.84 } else {
142 root 1.85 die "viewvc parse error, unable to detect revision\n";
143 root 1.84 }
144     }
145     }
146 root 1.83 }
147    
148 root 1.63 sub invoke_button_down {
149 root 1.1 my ($self, $ev, $x, $y) = @_;
150    
151 root 1.71 if ($ev->{button} == 1) {
152     $self->grab_focus;
153 root 1.147 return unless $::CONN && $self->{ctilesize};
154 root 1.1
155 root 1.126 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize};
156     my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize};
157 root 1.49
158 root 1.126 $x -= DC::floor $::MAP->w * 0.5;
159     $y -= DC::floor $::MAP->h * 0.5;
160 root 1.54
161 elmex 1.94 if ($::CONN) {
162 elmex 1.115 $::CONN->lookat ($x, $y)
163 elmex 1.94 }
164 root 1.49
165     } elsif ($ev->{button} == 2) {
166 root 1.71 $self->grab_focus;
167     return unless $::CONN;
168    
169 root 1.5 my ($ox, $oy) = ($ev->{x}, $ev->{y});
170 root 1.1 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
171    
172     $self->{motion} = sub {
173     my ($ev, $x, $y) = @_;
174    
175 root 1.5 ($x, $y) = ($ev->{x}, $ev->{y});
176 root 1.1
177     $::CFG->{map_shift_x} = $bw + $x - $ox;
178     $::CFG->{map_shift_y} = $bh + $y - $oy;
179    
180     $self->update;
181     };
182 root 1.71 } elsif ($ev->{button} == 3) {
183 root 1.85 my @items = (
184 root 1.74 ["Help Browser…\tF1", sub { $::HELP_WINDOW->toggle_visibility }],
185     ["Statistics\tF2", sub { ::toggle_player_page ($::STATS_PAGE) }],
186     ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
187 root 1.75 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
188     ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
189 root 1.74 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
190 root 1.129 # ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
191 root 1.85 );
192    
193 root 1.90 if ($::CONN && $::CONN->{editor_support}) {
194 root 1.128 # push @items, [
195     # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>",
196     # \&editor_invoke,
197     # ];
198 root 1.85
199 root 1.161 for my $type (@{ $::CONN->{editor_support}{servertypes} }) {
200     $::CONN->{editor_support}{servertype} ne $type
201 root 1.85 or next;
202     my $server = $::CONN->{editor_support}{"${type}server"}
203     or next;
204    
205     push @items, [
206 root 1.126 "Login on $type server <span size='xx-small'>(" . (DC::asxml $server) . ")</span>",
207 root 1.85 sub { server_login $server },
208     ];
209     }
210     }
211    
212     push @items,
213     ["Quit",
214     sub {
215     if ($::CONN) {
216     &::open_quit_dialog;
217     } else {
218     exit;
219 elmex 1.79 }
220 root 1.85 }
221 root 1.71 ],
222 root 1.85 ;
223    
224 root 1.126 (new DC::UI::Menu
225 root 1.85 items => \@items,
226 root 1.71 )->popup ($ev);
227 root 1.1 }
228 root 1.47
229     1
230 root 1.1 }
231    
232 root 1.63 sub invoke_button_up {
233 root 1.1 my ($self, $ev, $x, $y) = @_;
234    
235     delete $self->{motion};
236 root 1.47
237     1
238 root 1.1 }
239    
240 root 1.63 sub invoke_mouse_motion {
241 root 1.1 my ($self, $ev, $x, $y) = @_;
242    
243 root 1.47 if ($self->{motion}) {
244     $self->{motion}->($ev, $x, $y);
245     } else {
246     return 0;
247     }
248    
249     1
250 root 1.1 }
251    
252     sub size_request {
253 root 1.104 my ($self) = @_;
254    
255 root 1.1 (
256 root 1.126 $self->{tilesize} * DC::ceil $::WIDTH / $self->{tilesize},
257     $self->{tilesize} * DC::ceil $::HEIGHT / $self->{tilesize},
258 root 1.1 )
259     }
260    
261     sub update {
262     my ($self) = @_;
263    
264     $self->{need_update} = 1;
265     $self->SUPER::update;
266     }
267    
268 root 1.36 my %DIR = (
269 root 1.142 ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
270     ( "," . DC::SDLK_KP8 ), [1, "north"],
271     ( "," . DC::SDLK_KP9 ), [2, "northeast"],
272     ( "," . DC::SDLK_KP6 ), [3, "east"],
273     ( "," . DC::SDLK_KP3 ), [4, "southeast"],
274     ( "," . DC::SDLK_KP2 ), [5, "south"],
275     ( "," . DC::SDLK_KP1 ), [6, "southwest"],
276     ( "," . DC::SDLK_KP4 ), [7, "west"],
277     ( "," . DC::SDLK_KP7 ), [8, "northwest"],
278    
279     ( "," . DC::SDLK_PAGEUP ), [2, "northeast"],
280     ( "," . DC::SDLK_PAGEDOWN ), [4, "southeast"],
281     ( "," . DC::SDLK_END ), [6, "southwest"],
282     ( "," . DC::SDLK_HOME ), [8, "northwest"],
283    
284     ( "," . DC::SDLK_UP ), [1, "north"],
285     ("1," . DC::SDLK_UP ), [2, "northeast"],
286     ( "," . DC::SDLK_RIGHT ), [3, "east"],
287     ("1," . DC::SDLK_RIGHT ), [4, "southeast"],
288     ( "," . DC::SDLK_DOWN ), [5, "south"],
289     ("1," . DC::SDLK_DOWN ), [6, "southwest"],
290     ( "," . DC::SDLK_LEFT ), [7, "west"],
291     ("1," . DC::SDLK_LEFT ), [8, "northwest"],
292 root 1.36 );
293    
294 root 1.63 sub invoke_key_down {
295 root 1.36 my ($self, $ev) = @_;
296    
297     my $mod = $ev->{mod};
298     my $sym = $ev->{sym};
299     my $uni = $ev->{unicode};
300    
301 root 1.142 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT;
302 root 1.71
303 root 1.130 # ignore repeated keypresses
304     return if $self->{last_mod} == $mod && $self->{last_sym} == $sym;
305     $self->{last_mod} = $mod;
306     $self->{last_sym} = $sym;
307    
308 root 1.142 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
309    
310     if ($::CONN && $dir) {
311 root 1.126 if ($mod & DC::KMOD_SHIFT) {
312 root 1.36 $self->{shft}++;
313 root 1.112 if ($dir->[0] != $self->{fire_dir}) {
314     $::CONN->user_send ("fire $dir->[0]");
315 elmex 1.58 }
316 elmex 1.123 $self->{fire_dir} = $dir->[0];
317 root 1.126 } elsif ($mod & DC::KMOD_CTRL) {
318 root 1.36 $self->{ctrl}++;
319 root 1.112 $::CONN->user_send ("run $dir->[0]");
320 root 1.36 } else {
321 root 1.112 $::CONN->user_send ("$dir->[1]");
322 root 1.36 }
323 root 1.119
324     return 1;
325 root 1.36 }
326 root 1.47
327 root 1.119 0
328 root 1.36 }
329    
330 root 1.63 sub invoke_key_up {
331 root 1.36 my ($self, $ev) = @_;
332    
333 root 1.47 my $res = 0;
334 root 1.36 my $mod = $ev->{mod};
335     my $sym = $ev->{sym};
336    
337 root 1.131 delete $self->{last_mod};
338     delete $self->{last_sym};
339    
340 elmex 1.58 if ($::CFG->{shift_fire_stop}) {
341 root 1.126 if (!($mod & DC::KMOD_SHIFT) && delete $self->{shft}) {
342 elmex 1.58 $::CONN->user_send ("fire_stop");
343     delete $self->{fire_dir};
344     $res = 1;
345     }
346     } else {
347 root 1.142 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
348    
349     if ($dir && delete $self->{shft}) {
350 elmex 1.58 $::CONN->user_send ("fire_stop");
351     delete $self->{fire_dir};
352     $res = 1;
353 root 1.126 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT)
354 elmex 1.123 && delete $self->{shft}) { # XXX: is RSHIFT ok?
355 elmex 1.58 $::CONN->user_send ("fire_stop");
356     delete $self->{fire_dir};
357     $res = 1;
358     }
359 root 1.36 }
360 root 1.47
361 root 1.142 if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) {
362 root 1.36 $::CONN->user_send ("run_stop");
363 root 1.47 $res = 1;
364 root 1.36 }
365 root 1.47
366     $res
367 root 1.36 }
368    
369 root 1.110 sub invoke_visibility_change {
370     my ($self) = @_;
371    
372     $self->refresh_hook;
373    
374     0
375     }
376    
377 root 1.133 sub set_tilesize {
378     my ($self, $tilesize) = @_;
379    
380     $self->{tilesize} = $tilesize;
381     }
382    
383     sub scroll {
384     my ($self, $dx, $dy) = @_;
385    
386     $self->movement_update;
387    
388     $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
389     $self->{sdy} += $dy * $self->{tilesize};
390 root 1.156
391 root 1.157 # save old fow texture, if applicable
392 root 1.160 $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture};
393 root 1.157 $self->{lfdx} = $dx;
394     $self->{lfdy} = $dy;
395     $self->{lmdx} = $self->{dx};
396     $self->{lmdy} = $self->{dy};
397 root 1.156
398 root 1.157 $::MAP->scroll ($dx, $dy);
399 root 1.133 }
400    
401 root 1.61 sub set_magicmap {
402     my ($self, $w, $h, $x, $y, $data) = @_;
403    
404 root 1.92 $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w;
405     $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h;
406 root 1.61
407     $self->{magicmap} = [$x, $y, $w, $h, $data];
408    
409     $self->update;
410     }
411    
412 root 1.133 sub movement_update {
413     my ($self) = @_;
414    
415     if ($::CFG->{smooth_movement}) {
416     if ($self->{sdx} || $self->{sdy}) {
417 root 1.164 my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH);
418 root 1.133 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
419    
420     # the minimum time for a single tile movement
421 root 1.141 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
422 root 1.133
423 root 1.145 $spd *= $self->{tilesize};
424    
425 root 1.133 # jump if "impossibly high" speed
426     if (
427     (max abs $self->{sdx}, abs $self->{sdy})
428 root 1.145 > $spd * $mintime * 2.1
429 root 1.133 ) {
430 root 1.145 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d#
431 root 1.133 $self->{sdx} = $self->{sdy} = 0;
432     } else {
433 root 1.145 $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction
434 root 1.133
435     my $dx = $self->{sdx} < 0 ? -$spd : $spd;
436     my $dy = $self->{sdy} < 0 ? -$spd : $spd;
437    
438 root 1.135 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx }
439     if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy }
440 root 1.145 }
441 root 1.133
442 root 1.145 $self->update;
443 root 1.133 }
444     } else {
445     $self->{sdx} = $self->{sdy} = 0;
446     }
447    
448 root 1.164 $self->{last_update} = AE::time;
449 root 1.133 }
450    
451 root 1.110 sub refresh_hook {
452 root 1.1 my ($self) = @_;
453    
454 root 1.116 if ($::MAP && $::CONN) {
455 root 1.114 if (delete $self->{need_update}) {
456 root 1.133 $self->movement_update;
457    
458 root 1.114 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
459    
460 root 1.140 my $sdx_t = DC::ceil $self->{sdx} / $tilesize;
461     my $sdy_t = DC::ceil $self->{sdy} / $tilesize;
462 root 1.137
463 root 1.133 # width/height of map, in tiles
464 root 1.139 my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize;
465     my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize;
466 root 1.114
467 root 1.133 # the map displacement, in tiles
468 root 1.137 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t;
469     my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t;
470 root 1.114
471 root 1.133 # the upper left "visible" corner, in pixels
472 root 1.114 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
473     my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
474    
475 root 1.126 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
476     my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
477 root 1.114
478     if ($::CFG->{fow_enable}) {
479 root 1.158 # draw_fow_texture REQUIRES the fow texture to stay the same size.
480 root 1.157 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
481 root 1.114
482 root 1.126 $self->{fow_texture} = new DC::Texture
483 root 1.114 w => $w,
484     h => $h,
485     data => $data,
486 root 1.156 internalformat => GL_ALPHA,
487     format => GL_ALPHA;
488 root 1.114 } else {
489     delete $self->{fow_texture};
490     }
491 root 1.60
492 root 1.114 glNewList ($self->{list} ||= glGenList);
493 root 1.60
494 root 1.114 glPushMatrix;
495 root 1.134 glTranslate $sx0, $sy0;
496 root 1.114 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
497 root 1.149 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
498 root 1.91
499 root 1.132 $::MAP->draw ($dx, $dy, $sw, $sh,
500 root 1.163 ($self->{tilesize}) x 2,
501 root 1.133 $::CONN->{player}{tag},
502     -$self->{sdx}, -$self->{sdy});
503 root 1.60
504 root 1.114 glScale $self->{tilesize}, $self->{tilesize};
505 root 1.59
506 root 1.114 if (my $tex = $self->{fow_texture}) {
507 root 1.160 my @prev_fow_params;
508    
509     if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) {
510 root 1.157 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
511     my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
512 root 1.151
513     if ($d1 * $d2) {
514 root 1.160 @prev_fow_params = (
515 root 1.157 (min 1, $d1 / $d2),
516     $self->{lmdx} - $dx - $self->{lfdx},
517     $self->{lmdy} - $dy - $self->{lfdy},
518 root 1.160 @{$self->{prev_fow_texture}}{qw(name data)}
519     );
520 root 1.157 }
521     }
522 root 1.156
523 root 1.160 DC::Texture::draw_fow_texture
524     $::CFG->{fow_intensity},
525     $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
526     @{$self->{fow_texture}}{qw(name data s t w h)},
527     @prev_fow_params;
528 root 1.114 }
529 root 1.1
530 root 1.114 if ($self->{magicmap}) {
531     my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
532 root 1.31
533 root 1.114 $x += $::MAP->ox + $self->{dx};
534     $y += $::MAP->oy + $self->{dy};
535 root 1.1
536 root 1.114 glTranslate - $x - 1, - $y - 1;
537     glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
538 root 1.157 $::MAP->draw_magicmap ($w, $h, $data);
539 root 1.114 }
540 root 1.1
541 root 1.109 glPopMatrix;
542 root 1.114 glEndList;
543 root 1.1 }
544 root 1.114 } else {
545 root 1.158 delete $self->{last_fow_texture};
546     delete $self->{fow_texture};
547    
548 root 1.114 glDeleteList delete $self->{list}
549     if $self->{list};
550 root 1.1 }
551 root 1.110 }
552    
553     sub draw {
554     my ($self) = @_;
555    
556     $self->{root}->on_post_alloc (prepare => sub { $self->refresh_hook });
557    
558     return unless $self->{list};
559    
560 root 1.126 my $focused = $DC::UI::FOCUS == $self
561     || $DC::UI::FOCUS == $::COMPLETER->{entry};
562 root 1.110
563     return
564     unless $focused || !$::FAST;
565    
566 root 1.1 glCallList $self->{list};
567    
568 root 1.29 # TNT2 emulates logops in software (or worse :)
569 root 1.60 unless ($focused) {
570 root 1.120 glColor_premultiply 0, 0, 1, 0.25;
571 root 1.29 glEnable GL_BLEND;
572 root 1.120 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
573 root 1.1 glBegin GL_QUADS;
574     glVertex 0, 0;
575     glVertex 0, $::HEIGHT;
576     glVertex $::WIDTH, $::HEIGHT;
577     glVertex $::WIDTH, 0;
578     glEnd;
579 root 1.29 glDisable GL_BLEND;
580 root 1.1 }
581     }
582    
583 root 1.36 sub DESTROY {
584     my $self = shift;
585 root 1.3
586 root 1.36 glDeleteList $self->{list};
587 root 1.1
588 root 1.36 $self->SUPER::DESTROY;
589 root 1.8 }
590    
591 root 1.126 package DC::MapWidget::MapMap;
592 root 1.18
593 root 1.162 use common::sense;
594 root 1.105
595 root 1.126 our @ISA = DC::UI::Base::;
596 root 1.18
597 root 1.126 use DC::OpenGL;
598 root 1.18
599     sub size_request {
600 root 1.111 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
601 root 1.18 }
602    
603 root 1.110 sub refresh_hook {
604     my ($self) = @_;
605    
606 root 1.164 if ($::MAP && $self->{texture_atime} < AE::now) {
607 root 1.110 my ($w, $h) = @$self{qw(w h)};
608    
609 root 1.144 return unless $w && $h;
610    
611 root 1.110 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
612     my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
613    
614     my $ox = 0.5 * ($w - $sw);
615     my $oy = 0.5 * ($h - $sh);
616    
617     my $sx = int $::CFG->{map_shift_x} / $::MAPWIDGET->{tilesize};
618     my $sy = int $::CFG->{map_shift_y} / $::MAPWIDGET->{tilesize};
619    
620     #TODO: map scale is completely borked
621    
622     my $x0 = int $ox - $sx + 0.5;
623     my $y0 = int $oy - $sy + 0.5;
624    
625     $self->{sw} = $sw;
626     $self->{sh} = $sh;
627    
628     $self->{x0} = $x0;
629     $self->{y0} = $y0;
630    
631 root 1.164 $self->{texture_atime} = AE::now + 1/2;
632 root 1.110
633     $self->{texture} =
634 root 1.126 new DC::Texture
635 root 1.110 w => $w,
636     h => $h,
637     data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
638 root 1.126 type => $DC::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
639 root 1.110 }
640     }
641    
642     sub invoke_visibility_change {
643     my ($self) = @_;
644    
645     $self->refresh_hook;
646    
647     0
648     }
649    
650 root 1.63 sub invoke_size_allocate {
651 root 1.18 my ($self, $w, $h) = @_;
652    
653     $self->update;
654 root 1.63
655     1
656 root 1.18 }
657    
658     sub update {
659     my ($self) = @_;
660    
661     delete $self->{texture_atime};
662     $self->SUPER::update;
663     }
664    
665     sub _draw {
666     my ($self) = @_;
667    
668 root 1.110 $self->{root}->on_post_alloc (texture => sub { $self->refresh_hook });
669 root 1.18
670 root 1.110 $self->{texture} or return;
671 root 1.18
672     glEnable GL_BLEND;
673     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
674     glEnable GL_TEXTURE_2D;
675     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
676    
677     $self->{texture}->draw_quad (0, 0);
678    
679     glDisable GL_TEXTURE_2D;
680    
681     glTranslate 0.375, 0.375;
682    
683     glColor 1, 1, 0, 1;
684     glBegin GL_LINE_LOOP;
685 root 1.110 glVertex $self->{x0} , $self->{y0} ;
686     glVertex $self->{x0} , $self->{y0} + $self->{sh};
687     glVertex $self->{x0} + $self->{sw}, $self->{y0} + $self->{sh};
688     glVertex $self->{x0} + $self->{sw}, $self->{y0} ;
689 root 1.18 glEnd;
690    
691     glDisable GL_BLEND;
692     }
693    
694 root 1.126 package DC::MapWidget::Command;
695 root 1.8
696 root 1.162 use common::sense;
697 root 1.8
698 root 1.126 use DC::OpenGL;
699 root 1.8
700 root 1.126 our @ISA = DC::UI::Frame::;
701 root 1.8
702     sub new {
703     my $class = shift;
704    
705     my $self = $class->SUPER::new (
706 root 1.23 bg => [0, 0, 0, 0.8],
707 root 1.8 @_,
708 root 1.23 );
709    
710 root 1.126 $self->add ($self->{vbox} = new DC::UI::VBox);
711 root 1.23
712     $self->{label} = [
713     map
714 root 1.126 DC::UI::Label->new (
715 root 1.127 align => 0,
716 root 1.25 can_hover => 1,
717     can_events => 1,
718     tooltip_width => 0.33,
719     fontsize => $_,
720 root 1.27 ), (0.8) x 16
721 root 1.23 ];
722    
723 root 1.126 $self->{entry} = new DC::UI::Entry
724 root 1.40 on_changed => sub {
725 root 1.23 $self->update_labels;
726 root 1.63 0
727 root 1.36 },
728 elmex 1.57 on_button_down => sub {
729     my ($entry, $ev, $x, $y) = @_;
730    
731     if ($ev->{button} == 3) {
732 root 1.126 (new DC::UI::Menu
733 elmex 1.57 items => [
734 root 1.126 ["bind <i>" . (DC::asxml $self->{select}) . "</i> to a key"
735     => sub { DC::Macro::quick_macro [$self->{select}], sub { $entry->grab_focus } }]
736 elmex 1.57 ],
737     )->popup ($ev);
738     return 1;
739     }
740     0
741     },
742 root 1.40 on_key_down => sub {
743 root 1.36 my ($entry, $ev) = @_;
744    
745     my $self = $entry->{parent}{parent};
746    
747     if ($ev->{sym} == 13) {
748     if (exists $self->{select}) {
749 root 1.37 $self->{last_command} = $self->{select};
750 root 1.36 $::CONN->user_send ($self->{select});
751 elmex 1.52
752 elmex 1.68 unshift @{$self->{history}}, $self->{entry}->get_text;
753 elmex 1.52 $self->{hist_ptr} = 0;
754    
755 root 1.36 $self->hide;
756     }
757     } elsif ($ev->{sym} == 27) {
758 elmex 1.52 $self->{hist_ptr} = 0;
759 root 1.36 $self->hide;
760 root 1.126 } elsif ($ev->{sym} == DC::SDLK_DOWN) {
761 elmex 1.52 if ($self->{hist_ptr} > 1) {
762     $self->{hist_ptr}--;
763     $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1]);
764     } elsif ($self->{hist_ptr} > 0) {
765     $self->{hist_ptr}--;
766     $self->{entry}->set_text ($self->{hist_saveback});
767     } else {
768     ++$self->{select_offset}
769     if $self->{select_offset} < $#{ $self->{last_match} || [] };
770     }
771 root 1.36 $self->update_labels;
772 root 1.126 } elsif ($ev->{sym} == DC::SDLK_UP) {
773 elmex 1.52 if ($self->{select_offset}) {
774     --$self->{select_offset}
775     } else {
776     unless ($self->{hist_ptr}) {
777     $self->{hist_saveback} = $self->{entry}->get_text;
778     }
779     if ($self->{hist_ptr} <= $#{$self->{history}}) {
780     $self->{hist_ptr}++;
781     }
782     $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1])
783     if exists $self->{history}->[$self->{hist_ptr} - 1];
784     }
785 root 1.36 $self->update_labels;
786     } else {
787     return 0;
788     }
789    
790     1
791     }
792     ;
793 root 1.23
794     $self->{vbox}->add (
795     $self->{entry},
796     @{$self->{label}},
797 root 1.8 );
798    
799     $self
800     }
801    
802 root 1.167 sub reset {
803     my ($self) = @_;
804    
805     $self->hide;
806     delete $self->{command_list};
807     }
808    
809 root 1.36 sub set_prefix {
810     my ($self, $prefix) = @_;
811    
812     $self->{entry}->set_text ($prefix);
813     $self->show;
814     }
815    
816 root 1.63 sub invoke_size_allocate {
817 root 1.8 my ($self, $w, $h) = @_;
818    
819 root 1.44 $self->move_abs (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
820 root 1.63
821     $self->SUPER::invoke_size_allocate ($w, $h)
822 root 1.8 }
823    
824 root 1.36 sub show {
825     my ($self) = @_;
826    
827     $self->SUPER::show;
828 root 1.63 $self->{entry}->grab_focus;
829 root 1.36 }
830    
831     sub hide {
832     my ($self) = @_;
833    
834 elmex 1.76 $self->{hist_ptr} = 0;
835    
836 root 1.36 $self->SUPER::hide;
837     $self->{entry}->set_text ("");
838     }
839    
840 root 1.77 sub inject_key_down {
841 root 1.23 my ($self, $ev) = @_;
842    
843 root 1.77 $self->{entry}->grab_focus;
844     $self->{entry}->emit (key_down => $ev);
845 root 1.23 }
846    
847 root 1.8 sub update_labels {
848     my ($self) = @_;
849    
850 root 1.23 my $text = $self->{entry}->get_text;
851    
852     length $text
853 root 1.36 or return $self->hide;
854 root 1.23
855 root 1.167 return unless $::CONN;
856    
857     # regenerate spell list if necessary
858     $self->{command_list}{spells} ||= [
859     map { ("cast $_->{name}", "invoke $_->{name}") }
860     values %{ $::CONN->{spell} }
861     ];
862    
863 root 1.36 if ($text ne $self->{last_search}) {
864     my @match;
865 root 1.23
866 root 1.36 if ($text =~ /^(.*?)\s+$/) {
867 elmex 1.150 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
868     @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]);
869 root 1.36 } else {
870 elmex 1.150 # @match is [command, penalty, command with arguments] until sort
871    
872     my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
873    
874 root 1.165 my $first_char = substr $cmd, 0, 1;
875    
876 elmex 1.150 my $regexp_abbrev = do {
877 root 1.36 my ($beg, @chr) = split //, lc $cmd;
878 root 1.23
879 root 1.36 # the following regex is used to match our "completion entry"
880     # to an actual command - the parentheses match kind of "overhead"
881     # - the more characters the parentheses match, the less attractive
882     # is the match.
883     my $regexp = "^\Q$beg\E"
884 root 1.166 . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr;
885 root 1.36 qr<$regexp>
886     };
887    
888 elmex 1.150 my $regexp_partial = do {
889     my $regexp = "^\Q$text\E(.*)";
890     qr<$regexp>
891     };
892 root 1.36
893 root 1.167 for my $list (values %{ $self->{command_list} }) {
894     for (@$list) {
895     # we only match and score if the first character matches,
896     # so quickly rule out all others first.
897     next unless $first_char = substr $_, 0, 1;
898    
899     my @scores;
900    
901     # 1. Complete command [with args]
902     # command is a prefix of the text
903     # score is length of complete command matched
904     # e.g. "invoke summon pet monster bat"
905     # "invoke" "summon pet monster bat" = 6
906     # "invoke summon pet monster" "bat" = 25
907     if ((substr $text, 0, length $_) eq $_) {
908     push @scores, [$_, length $_, $text];
909     }
910 elmex 1.150
911 root 1.167 # 2. Partial command
912     # text is a prefix of the full command
913     # score is the length of the input text
914     # e.g. "invoke s"
915     # "invoke small fireball" = 8
916     # "invoke summon pet monster" = 8
917 elmex 1.150
918 root 1.167 if ($_ =~ $regexp_partial) {
919     push @scores, [$_, length $text, $_];
920     }
921    
922     # 3. Abbreviation match
923     # attempts to use first word of text as an abbreviated command
924     # score is length of word + 1 - 3 per non-word-initial character
925 elmex 1.150
926 root 1.167 if (my @penalty = $_ =~ $regexp_abbrev) {
927     push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
928     }
929 elmex 1.150
930 root 1.167 # Pick the best option for this command
931     push @match, (sort {
932     $b->[1] <=> $a->[1]
933     } @scores)[0];
934 elmex 1.150 }
935 root 1.23 }
936 root 1.36
937 elmex 1.150 # @match is now [command object, command with arguments]
938     @match = map [$self->{command}{$_->[0]}, $_->[2]],
939 root 1.36 sort {
940 elmex 1.150 $b->[1] <=> $a->[1]
941 root 1.36 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
942 root 1.39 or (length $b->[0]) <=> (length $a->[0])
943 root 1.36 } @match;
944 root 1.8 }
945 root 1.23
946 root 1.39 $self->{last_search} = $text;
947 root 1.23 $self->{last_match} = \@match;
948    
949     $self->{select_offset} = 0;
950 root 1.8 }
951    
952 root 1.23 my @labels = @{ $self->{label} };
953     my @matches = @{ $self->{last_match} || [] };
954 root 1.8
955 root 1.23 if ($self->{select_offset}) {
956     splice @matches, 0, $self->{select_offset}, ();
957 root 1.8
958 root 1.23 my $label = shift @labels;
959     $label->set_text ("...");
960     $label->set_tooltip ("Use Cursor-Up to view previous matches");
961 root 1.8 }
962    
963 root 1.23 for my $label (@labels) {
964     $label->{fg} = [1, 1, 1, 1];
965     $label->{bg} = [0, 0, 0, 0];
966     }
967    
968     if (@matches) {
969 elmex 1.150 $self->{select} = "$matches[0][1]";
970 root 1.23
971     $labels[0]->{fg} = [0, 0, 0, 1];
972     $labels[0]->{bg} = [1, 1, 1, 0.8];
973     } else {
974 elmex 1.150 $self->{select} = "$text";
975 root 1.23 }
976    
977     for my $match (@matches) {
978     my $label = shift @labels;
979    
980     if (@labels) {
981 elmex 1.150 $label->set_text ("$match->[1]");
982     $label->set_tooltip ("$match->[0][1]");
983 root 1.23 } else {
984     $label->set_text ("...");
985     $label->set_tooltip ("Use Cursor-Down to view more matches");
986     last;
987     }
988     }
989 root 1.8
990 root 1.23 for my $label (@labels) {
991     $label->set_text ("");
992     $label->set_tooltip ("");
993 root 1.8 }
994    
995 root 1.23 $self->update;
996 root 1.8 }
997    
998 root 1.23 sub _draw {
999     my ($self) = @_;
1000 root 1.8
1001 root 1.23 # hack
1002 root 1.126 local $DC::UI::FOCUS = $self->{entry};
1003 root 1.10
1004 root 1.23 $self->SUPER::_draw;
1005 root 1.2 }
1006    
1007 root 1.1 1
1008 root 1.85