ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.83
Committed: Sun Oct 1 14:48:51 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.82: +32 -1 lines
Log Message:
support for grabbing win32 proxy

File Contents

# User Rev Content
1 root 1.80 package CFPlus::MapWidget;
2 root 1.1
3     use strict;
4 root 1.14 use utf8;
5 root 1.1
6     use List::Util qw(min max);
7    
8 root 1.80 use CFPlus;
9     use CFPlus::OpenGL;
10     use CFPlus::UI;
11 root 1.1
12 root 1.80 our @ISA = CFPlus::UI::Base::;
13 root 1.1
14 root 1.61 my $magicmap_tex =
15 root 1.80 new_from_file CFPlus::Texture CFPlus::find_rcfile "magicmap.png",
16 root 1.61 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
17    
18 root 1.1 sub new {
19     my $class = shift;
20    
21 root 1.20 my $self = $class->SUPER::new (
22 root 1.1 z => -1,
23     can_focus => 1,
24 root 1.4 list => glGenList,
25 root 1.51
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 root 1.1 @_
33 root 1.20 );
34    
35 root 1.80 $self->{completer} = new CFPlus::MapWidget::Command::
36 root 1.36 command => $self->{command},
37 root 1.81 tooltip => "#completer_help",
38 root 1.36 ;
39    
40 root 1.20 $self
41 root 1.1 }
42    
43 root 1.36 sub add_command {
44     my ($self, $command, $tooltip, $widget, $cb) = @_;
45    
46     (my $data = $command) =~ s/\\//g;
47    
48     $tooltip =~ s/^\s+//;
49     $tooltip = "<big>$data</big>\n\n$tooltip";
50     $tooltip =~ s/\s+$//;
51    
52     $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
53     }
54 root 1.4
55 root 1.36 sub clr_commands {
56     my ($self) = @_;
57 root 1.4
58 root 1.36 %{$self->{completer}{command}} = ();
59 elmex 1.76
60     $self->{completer}->hide
61     if $self->{completer};
62 root 1.4 }
63    
64 root 1.83 sub test_server_login {
65     return unless
66     $::CONN && length $::CONN->{test_server};
67    
68     my $server = $::CONN->{test_server};
69     ::stop_game ();
70     local $::PROFILE->{host} = $server;
71     ::start_game ();
72     }
73    
74     sub editor_invoke {
75     return unless $::CONN;
76    
77     # putting the password into the env is somewhat tasteless
78     local $ENV{CFPLUS_LOGIN} = $::PROFILE->{user};
79     local $ENV{CFPLUS_PASSWORD} = $::PROFILE->{password};
80     local $ENV{CFPLUS_UPLOAD} = $::CONN->{upload};
81     local $ENV{CROSSFIRE_MAPDIR} = $::CONN->{cvs_root};
82    
83     # CFPlus::set_proxy;
84    
85     # TODO: move into editor
86     require LWP::Simple;
87     }
88    
89 root 1.63 sub invoke_button_down {
90 root 1.1 my ($self, $ev, $x, $y) = @_;
91    
92 root 1.71 if ($ev->{button} == 1) {
93     $self->grab_focus;
94     return unless $::CONN;
95 root 1.1
96 root 1.80 my $x = 1 + CFPlus::floor +($ev->{x} - $self->{sx0}) / $self->{tilesize} - $self->{sx};
97     my $y = 1 + CFPlus::floor +($ev->{y} - $self->{sy0}) / $self->{tilesize} - $self->{sy};
98 root 1.49
99 root 1.60 $x -= int 0.5 * $self->{sw};
100     $y -= int 0.5 * $self->{sh};
101 root 1.54
102 root 1.56 $::CONN->lookat ($x, $y)
103 root 1.53 if $::CONN;
104 root 1.49
105     } elsif ($ev->{button} == 2) {
106 root 1.71 $self->grab_focus;
107     return unless $::CONN;
108    
109 root 1.5 my ($ox, $oy) = ($ev->{x}, $ev->{y});
110 root 1.1 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
111    
112     $self->{motion} = sub {
113     my ($ev, $x, $y) = @_;
114    
115 root 1.5 ($x, $y) = ($ev->{x}, $ev->{y});
116 root 1.1
117     $::CFG->{map_shift_x} = $bw + $x - $ox;
118     $::CFG->{map_shift_y} = $bh + $y - $oy;
119    
120     $self->update;
121     };
122 root 1.71 } elsif ($ev->{button} == 3) {
123 root 1.80 (new CFPlus::UI::Menu
124 root 1.71 items => [
125 root 1.74 ["Help Browser…\tF1", sub { $::HELP_WINDOW->toggle_visibility }],
126     ["Statistics\tF2", sub { ::toggle_player_page ($::STATS_PAGE) }],
127     ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
128 root 1.75 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
129     ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
130 root 1.74 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
131     ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
132 root 1.71 [
133     $::PICKUP_ENABLE->{state}
134     ? "Disable automatic pickup"
135     : "Enable automatic pickup",
136     sub { $::PICKUP_ENABLE->toggle }
137     ],
138 root 1.83 $::CONN && length $::CONN->{cvs_root}
139     ? ["Edit this map <span size='xx-small'>(" . (CFPlus::asxml $::CONN->{map_info}[0]) . ")</span>", \&editor_invoke]
140     : (),
141     $::CONN && length $::CONN->{test_server}
142     ? ["Login on Test Server", \&test_server_login]
143     : (),
144 elmex 1.79 ["Quit",
145     sub {
146     if ($::CONN) {
147     &::open_quit_dialog;
148     } else {
149     exit;
150     }
151     }
152     ],
153 root 1.71 ],
154     )->popup ($ev);
155 root 1.1 }
156 root 1.47
157     1
158 root 1.1 }
159    
160 root 1.63 sub invoke_button_up {
161 root 1.1 my ($self, $ev, $x, $y) = @_;
162    
163     delete $self->{motion};
164 root 1.47
165     1
166 root 1.1 }
167    
168 root 1.63 sub invoke_mouse_motion {
169 root 1.1 my ($self, $ev, $x, $y) = @_;
170    
171 root 1.47 if ($self->{motion}) {
172     $self->{motion}->($ev, $x, $y);
173     } else {
174     return 0;
175     }
176    
177     1
178 root 1.1 }
179    
180     sub size_request {
181     (
182 root 1.80 32 * CFPlus::ceil $::WIDTH / 32,
183     32 * CFPlus::ceil $::HEIGHT / 32,
184 root 1.1 )
185     }
186    
187     sub update {
188     my ($self) = @_;
189    
190     $self->{need_update} = 1;
191     $self->SUPER::update;
192     }
193    
194 root 1.36 my %DIR = (
195 root 1.80 CFPlus::SDLK_KP8, [1, "north"],
196     CFPlus::SDLK_KP9, [2, "northeast"],
197     CFPlus::SDLK_KP6, [3, "east"],
198     CFPlus::SDLK_KP3, [4, "southeast"],
199     CFPlus::SDLK_KP2, [5, "south"],
200     CFPlus::SDLK_KP1, [6, "southwest"],
201     CFPlus::SDLK_KP4, [7, "west"],
202     CFPlus::SDLK_KP7, [8, "northwest"],
203    
204     CFPlus::SDLK_UP, [1, "north"],
205     CFPlus::SDLK_RIGHT, [3, "east"],
206     CFPlus::SDLK_DOWN, [5, "south"],
207     CFPlus::SDLK_LEFT, [7, "west"],
208 root 1.36 );
209    
210 root 1.63 sub invoke_key_down {
211 root 1.36 my ($self, $ev) = @_;
212    
213     my $mod = $ev->{mod};
214     my $sym = $ev->{sym};
215     my $uni = $ev->{unicode};
216    
217 root 1.80 $mod &= CFPlus::KMOD_CTRL | CFPlus::KMOD_ALT | CFPlus::KMOD_SHIFT;
218 root 1.71
219     if ($uni == ord "\t") {
220     $::PL_WINDOW->toggle_visibility;
221 root 1.80 } elsif ($sym == CFPlus::SDLK_F1 && !$mod) {
222 root 1.71 $::HELP_WINDOW->toggle_visibility;
223 root 1.80 } elsif ($sym == CFPlus::SDLK_F2 && !$mod) {
224 root 1.71 ::toggle_player_page ($::STATS_PAGE);
225 root 1.80 } elsif ($sym == CFPlus::SDLK_F3 && !$mod) {
226 root 1.74 ::toggle_player_page ($::SKILL_PAGE);
227 root 1.80 } elsif ($sym == CFPlus::SDLK_F4 && !$mod) {
228 root 1.75 ::toggle_player_page ($::SPELL_PAGE);
229 root 1.80 } elsif ($sym == CFPlus::SDLK_F5 && !$mod) {
230 root 1.71 ::toggle_player_page ($::INVENTORY_PAGE);
231 root 1.80 } elsif ($sym == CFPlus::SDLK_F9 && !$mod) {
232 root 1.71 $::SETUP_DIALOG->toggle_visibility;
233 root 1.80 } elsif ($sym == CFPlus::SDLK_INSERT && $mod & CFPlus::KMOD_CTRL) {
234 root 1.71 $::BIND_EDITOR->set_binding (undef, undef, [],
235     sub {
236     my ($mod, $sym, $cmds) = @_;
237     $::BIND_EDITOR->cfg_bind ($mod, $sym, $cmds);
238     });
239     $::BIND_EDITOR->start;
240     $::BIND_EDITOR->show;
241 root 1.82 #TODO: elmex, what was this supposed to do? it currently crashes the client.
242     # } elsif ($sym == CFPlus::SDLK_INSERT && not ($mod & CFPlus::KMOD_CTRL)) {
243     # $::BIND_EDITOR->stop;
244     # $::BIND_EDITOR->ask_for_bind_and_commit;
245     # $::BIND_EDITOR->hide;
246 root 1.71 } elsif (!$::CONN) {
247     return 0; # bindings further down need a valid connection
248    
249 root 1.80 } elsif ($sym == CFPlus::SDLK_KP5 && !$mod) {
250 root 1.36 $::CONN->user_send ("stay fire");
251     } elsif ($uni == ord ",") {
252     $::CONN->user_send ("take");
253     } elsif ($uni == ord " ") {
254     $::CONN->user_send ("apply");
255 root 1.82 } elsif ($uni == 13) {
256     $::CONN->user_send ("examine");
257 root 1.37 } elsif ($uni == ord ".") {
258     $::CONN->user_send ($self->{completer}{last_command})
259     if exists $self->{completer}{last_command};
260 root 1.83 } elsif (my $bind_cmd = $::PROFILE->{bindings}{$mod}{$sym}) {
261 root 1.71 $::CONN->user_send ($_) for @$bind_cmd;
262 root 1.80 } elsif (($sym == CFPlus::SDLK_KP_PLUS && !$mod) || $uni == ord "+") {
263 root 1.36 $::CONN->user_send ("rotateshoottype +");
264 root 1.80 } elsif (($sym == CFPlus::SDLK_KP_MINUS && !$mod) || $uni == ord "-") {
265 root 1.36 $::CONN->user_send ("rotateshoottype -");
266     } elsif ($uni == ord '"') {
267     $self->{completer}->set_prefix ("$::CFG->{say_command} ");
268     $self->{completer}->show;
269     } elsif ($uni == ord "'") {
270     $self->{completer}->set_prefix ("");
271     $self->{completer}->show;
272     } elsif (exists $DIR{$sym}) {
273 root 1.80 if ($mod & CFPlus::KMOD_SHIFT) {
274 root 1.36 $self->{shft}++;
275 elmex 1.58 if ($DIR{$sym}[0] != $self->{fire_dir}) {
276     $::CONN->user_send ("fire $DIR{$sym}[0]");
277     }
278     $self->{fire_dir} = $DIR{$sym}[0];
279 root 1.80 } elsif ($mod & CFPlus::KMOD_CTRL) {
280 root 1.36 $self->{ctrl}++;
281     $::CONN->user_send ("run $DIR{$sym}[0]");
282     } else {
283     $::CONN->user_send ("$DIR{$sym}[1]");
284     }
285 root 1.42 } elsif ((ord 'a') <= $uni && $uni <= (ord 'z')) {
286 root 1.77 $self->{completer}->inject_key_down ($ev);
287 root 1.36 $self->{completer}->show;
288 root 1.47 } else {
289     return 0;
290 root 1.36 }
291 root 1.47
292     1
293 root 1.36 }
294    
295 root 1.63 sub invoke_key_up {
296 root 1.36 my ($self, $ev) = @_;
297    
298 root 1.47 my $res = 0;
299 root 1.36 my $mod = $ev->{mod};
300     my $sym = $ev->{sym};
301    
302 elmex 1.58 if ($::CFG->{shift_fire_stop}) {
303 root 1.80 if (!($mod & CFPlus::KMOD_SHIFT) && delete $self->{shft}) {
304 elmex 1.58 $::CONN->user_send ("fire_stop");
305     delete $self->{fire_dir};
306     $res = 1;
307     }
308     } else {
309     if (exists $DIR{$sym} && delete $self->{shft}) {
310     $::CONN->user_send ("fire_stop");
311     delete $self->{fire_dir};
312     $res = 1;
313 root 1.80 } elsif (($sym == CFPlus::SDLK_LSHIFT || $sym == CFPlus::SDLK_RSHIFT) && delete $self->{shft}) { # XXX: is RSHIFT ok?
314 elmex 1.58 $::CONN->user_send ("fire_stop");
315     delete $self->{fire_dir};
316     $res = 1;
317     }
318 root 1.36 }
319 root 1.47
320 root 1.80 if (!($mod & CFPlus::KMOD_CTRL ) && delete $self->{ctrl}) {
321 root 1.36 $::CONN->user_send ("run_stop");
322 root 1.47 $res = 1;
323 root 1.36 }
324 root 1.47
325     $res
326 root 1.36 }
327    
328 root 1.61 sub set_magicmap {
329     my ($self, $w, $h, $x, $y, $data) = @_;
330    
331 root 1.62 $x -= $::MAP->ox + int 0.5 * $::MAP->w;
332     $y -= $::MAP->oy + int 0.5 * $::MAP->h;
333 root 1.61
334     $self->{magicmap} = [$x, $y, $w, $h, $data];
335    
336     $self->update;
337     }
338    
339 root 1.1 sub draw {
340     my ($self) = @_;
341    
342 root 1.60 return unless $::MAP;
343    
344 root 1.80 my $focused = $CFPlus::UI::FOCUS == $self
345     || $CFPlus::UI::FOCUS == $self->{completer}{entry};
346 root 1.36
347 root 1.26 return
348 root 1.60 unless $focused || !$::FAST;
349 root 1.26
350 root 1.1 if (delete $self->{need_update}) {
351 root 1.60 my $tilesize = $self->{tilesize} = int 32 * $::CFG->{map_scale};
352    
353 root 1.80 my $sx = $self->{sx} = CFPlus::ceil $::CFG->{map_shift_x} / $tilesize;
354     my $sy = $self->{sy} = CFPlus::ceil $::CFG->{map_shift_y} / $tilesize;
355 root 1.60
356     my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
357     my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
358 root 1.59
359 root 1.80 my $sw = $self->{sw} = 1 + CFPlus::ceil $self->{w} / $tilesize;
360     my $sh = $self->{sh} = 1 + CFPlus::ceil $self->{h} / $tilesize;
361 root 1.60
362     if ($::CFG->{fow_enable}) {
363     my ($w, $h, $data) = $::MAP->fow_texture ($sx, $sy, 0, 0, $sw, $sh);
364    
365 root 1.80 if ($::CFG->{fow_smooth} && $CFPlus::OpenGL::GL_VERSION >= 1.2) { # smooth fog of war
366 root 1.60 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
367     glConvolutionFilter2D (
368     GL_CONVOLUTION_2D,
369     GL_ALPHA,
370     3, 3,
371     GL_ALPHA, GL_FLOAT,
372     (pack "f*", @{ $self->{smooth_matrix} }),
373     );
374     glEnable GL_CONVOLUTION_2D;
375     }
376    
377 root 1.80 $self->{fow_texture} = new CFPlus::Texture
378 root 1.60 w => $w,
379     h => $h,
380     data => $data,
381     internalformat => GL_ALPHA,
382     format => GL_ALPHA;
383 root 1.1
384 root 1.60 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
385     } else {
386     delete $self->{fow_texture};
387     }
388 root 1.1
389 root 1.60 glNewList $self->{list};
390 root 1.31
391 root 1.60 glPushMatrix;
392     glTranslate $sx0, $sy0;
393     glScale $::CFG->{map_scale}, $::CFG->{map_scale};
394 root 1.1
395 root 1.60 $::MAP->draw ($sx, $sy, 0, 0, $sw, $sh);
396 root 1.1
397 root 1.61 glScale 32, 32;
398    
399 root 1.60 if (my $tex = $self->{fow_texture}) {
400     glEnable GL_TEXTURE_2D;
401     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
402 root 1.1
403 root 1.60 glColor +($::CFG->{fow_intensity}) x 3, 0.9;
404     $self->{fow_texture}->draw_quad_alpha (0, 0);
405 root 1.1
406 root 1.60 glDisable GL_TEXTURE_2D;
407 root 1.1 }
408    
409 root 1.61 if ($self->{magicmap}) {
410     my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
411    
412 root 1.62 $x += $::MAP->ox - $sx + int 0.5 * ($::MAP->w - $sw + 1);
413     $y += $::MAP->oy - $sy + int 0.5 * ($::MAP->h - $sh + 1);
414 root 1.61
415     glTranslate - $x - 1, - $y - 1;
416     glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
417     $::MAP->draw_magicmap ($x, $y, $w, $h, $data);
418     }
419    
420 root 1.60 glPopMatrix;
421 root 1.1 glEndList;
422     }
423    
424     glCallList $self->{list};
425    
426 root 1.29 # TNT2 emulates logops in software (or worse :)
427 root 1.60 unless ($focused) {
428 root 1.32 glColor 0.4, 0.2, 0.2, 0.6;
429 root 1.29 glEnable GL_BLEND;
430     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
431 root 1.1 glBegin GL_QUADS;
432     glVertex 0, 0;
433     glVertex 0, $::HEIGHT;
434     glVertex $::WIDTH, $::HEIGHT;
435     glVertex $::WIDTH, 0;
436     glEnd;
437 root 1.29 glDisable GL_BLEND;
438 root 1.1 }
439     }
440    
441 root 1.36 sub DESTROY {
442     my $self = shift;
443 root 1.3
444 root 1.36 glDeleteList $self->{list};
445 root 1.1
446 root 1.36 $self->SUPER::DESTROY;
447 root 1.8 }
448    
449 root 1.80 package CFPlus::MapWidget::MapMap;
450 root 1.18
451 root 1.80 our @ISA = CFPlus::UI::Base::;
452 root 1.18
453     use Time::HiRes qw(time);
454 root 1.80 use CFPlus::OpenGL;
455 root 1.18
456     sub size_request {
457     ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
458     }
459    
460 root 1.63 sub invoke_size_allocate {
461 root 1.18 my ($self, $w, $h) = @_;
462    
463     $self->update;
464 root 1.63
465     1
466 root 1.18 }
467    
468     sub update {
469     my ($self) = @_;
470    
471     delete $self->{texture_atime};
472     $self->SUPER::update;
473     }
474    
475     sub _draw {
476     my ($self) = @_;
477    
478     $::MAP or return;
479    
480     my ($w, $h) = @$self{qw(w h)};
481    
482     my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
483     my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
484    
485     my $sx = int $::CFG->{map_shift_x} / 32;
486     my $sy = int $::CFG->{map_shift_y} / 32;
487    
488     my $ox = 0.5 * ($w - $sw);
489     my $oy = 0.5 * ($h - $sh);
490    
491     glEnable GL_BLEND;
492     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
493     glEnable GL_TEXTURE_2D;
494     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
495    
496     if ($self->{texture_atime} < time) {
497     $self->{texture_atime} = time + 1/3;
498    
499     $self->{texture} =
500 root 1.80 new CFPlus::Texture
501 root 1.18 w => $w,
502     h => $h,
503     data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
504 root 1.80 type => $CFPlus::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
505 root 1.18 }
506    
507     $self->{texture}->draw_quad (0, 0);
508    
509     glDisable GL_TEXTURE_2D;
510    
511     glTranslate 0.375, 0.375;
512    
513     #TODO: map scale is completely borked
514    
515     my $x0 = int $ox - $sx + 0.5;
516     my $y0 = int $oy - $sy + 0.5;
517    
518     glColor 1, 1, 0, 1;
519     glBegin GL_LINE_LOOP;
520     glVertex $x0 , $y0 ;
521     glVertex $x0 , $y0 + $sh;
522     glVertex $x0 + $sw, $y0 + $sh;
523     glVertex $x0 + $sw, $y0 ;
524     glEnd;
525    
526     glDisable GL_BLEND;
527     }
528    
529 root 1.80 package CFPlus::MapWidget::Command;
530 root 1.8
531     use strict;
532    
533 root 1.80 use CFPlus::OpenGL;
534 root 1.8
535 root 1.80 our @ISA = CFPlus::UI::Frame::;
536 root 1.8
537     sub new {
538     my $class = shift;
539    
540     my $self = $class->SUPER::new (
541 root 1.23 bg => [0, 0, 0, 0.8],
542 root 1.8 @_,
543 root 1.23 );
544    
545 root 1.80 $self->add ($self->{vbox} = new CFPlus::UI::VBox);
546 root 1.23
547     $self->{label} = [
548     map
549 root 1.80 CFPlus::UI::Label->new (
550 root 1.25 can_hover => 1,
551     can_events => 1,
552     tooltip_width => 0.33,
553     fontsize => $_,
554 root 1.27 ), (0.8) x 16
555 root 1.23 ];
556    
557 root 1.80 $self->{entry} = new CFPlus::UI::Entry
558 root 1.40 on_changed => sub {
559 root 1.23 $self->update_labels;
560 root 1.63 0
561 root 1.36 },
562 elmex 1.57 on_button_down => sub {
563     my ($entry, $ev, $x, $y) = @_;
564    
565     if ($ev->{button} == 3) {
566 root 1.80 (new CFPlus::UI::Menu
567 elmex 1.57 items => [
568 root 1.80 ["bind <i>" . (CFPlus::asxml $self->{select}) . "</i> to a key"
569 root 1.69 => sub { $::BIND_EDITOR->do_quick_binding ([$self->{select}], sub { $entry->grab_focus }) }]
570 elmex 1.57 ],
571     )->popup ($ev);
572     return 1;
573     }
574     0
575     },
576 root 1.40 on_key_down => sub {
577 root 1.36 my ($entry, $ev) = @_;
578    
579     my $self = $entry->{parent}{parent};
580    
581     if ($ev->{sym} == 13) {
582     if (exists $self->{select}) {
583 root 1.37 $self->{last_command} = $self->{select};
584 root 1.36 $::CONN->user_send ($self->{select});
585 elmex 1.52
586 elmex 1.68 unshift @{$self->{history}}, $self->{entry}->get_text;
587 elmex 1.52 $self->{hist_ptr} = 0;
588    
589 root 1.36 $self->hide;
590     }
591     } elsif ($ev->{sym} == 27) {
592 elmex 1.52 $self->{hist_ptr} = 0;
593 root 1.36 $self->hide;
594 root 1.80 } elsif ($ev->{sym} == CFPlus::SDLK_DOWN) {
595 elmex 1.52 if ($self->{hist_ptr} > 1) {
596     $self->{hist_ptr}--;
597     $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1]);
598     } elsif ($self->{hist_ptr} > 0) {
599     $self->{hist_ptr}--;
600     $self->{entry}->set_text ($self->{hist_saveback});
601     } else {
602     ++$self->{select_offset}
603     if $self->{select_offset} < $#{ $self->{last_match} || [] };
604     }
605 root 1.36 $self->update_labels;
606 root 1.80 } elsif ($ev->{sym} == CFPlus::SDLK_UP) {
607 elmex 1.52 if ($self->{select_offset}) {
608     --$self->{select_offset}
609     } else {
610     unless ($self->{hist_ptr}) {
611     $self->{hist_saveback} = $self->{entry}->get_text;
612     }
613     if ($self->{hist_ptr} <= $#{$self->{history}}) {
614     $self->{hist_ptr}++;
615     }
616     $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1])
617     if exists $self->{history}->[$self->{hist_ptr} - 1];
618     }
619 root 1.36 $self->update_labels;
620     } else {
621     return 0;
622     }
623    
624     1
625     }
626     ;
627 root 1.23
628     $self->{vbox}->add (
629     $self->{entry},
630     @{$self->{label}},
631 root 1.8 );
632    
633     $self
634     }
635    
636 root 1.36 sub set_prefix {
637     my ($self, $prefix) = @_;
638    
639     $self->{entry}->set_text ($prefix);
640     $self->show;
641     }
642    
643 root 1.63 sub invoke_size_allocate {
644 root 1.8 my ($self, $w, $h) = @_;
645    
646 root 1.44 $self->move_abs (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
647 root 1.63
648     $self->SUPER::invoke_size_allocate ($w, $h)
649 root 1.8 }
650    
651 root 1.36 sub show {
652     my ($self) = @_;
653    
654     $self->SUPER::show;
655 root 1.63 $self->{entry}->grab_focus;
656 root 1.36 }
657    
658     sub hide {
659     my ($self) = @_;
660    
661 elmex 1.76 $self->{hist_ptr} = 0;
662    
663 root 1.36 $self->SUPER::hide;
664     $self->{entry}->set_text ("");
665     }
666    
667 root 1.77 sub inject_key_down {
668 root 1.23 my ($self, $ev) = @_;
669    
670 root 1.77 $self->{entry}->grab_focus;
671     $self->{entry}->emit (key_down => $ev);
672 root 1.23 }
673    
674 root 1.8 sub update_labels {
675     my ($self) = @_;
676    
677 root 1.23 my $text = $self->{entry}->get_text;
678    
679     length $text
680 root 1.36 or return $self->hide;
681 root 1.23
682     my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
683    
684 root 1.36 if ($text ne $self->{last_search}) {
685     my @match;
686 root 1.23
687 root 1.36 if ($text =~ /^(.*?)\s+$/) {
688     @match = [$cmd, "(appended whitespace suppresses completion)"];
689     } else {
690     my $regexp = do {
691     my ($beg, @chr) = split //, lc $cmd;
692 root 1.23
693 root 1.36 # the following regex is used to match our "completion entry"
694     # to an actual command - the parentheses match kind of "overhead"
695     # - the more characters the parentheses match, the less attractive
696     # is the match.
697     my $regexp = "^\Q$beg\E"
698     . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
699     qr<$regexp>
700     };
701    
702     my @penalty;
703    
704     for (keys %{$self->{command}}) {
705     if (@penalty = $_ =~ $regexp) {
706     push @match, [$_, length join "", map "::$_", grep defined, @penalty];
707     }
708 root 1.23 }
709 root 1.36
710     @match = map $self->{command}{$_->[0]},
711     sort {
712     $a->[1] <=> $b->[1]
713     or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
714 root 1.39 or (length $b->[0]) <=> (length $a->[0])
715 root 1.36 } @match;
716 root 1.8 }
717 root 1.23
718 root 1.39 $self->{last_search} = $text;
719 root 1.23 $self->{last_match} = \@match;
720    
721     $self->{select_offset} = 0;
722 root 1.8 }
723    
724 root 1.23 my @labels = @{ $self->{label} };
725     my @matches = @{ $self->{last_match} || [] };
726 root 1.8
727 root 1.23 if ($self->{select_offset}) {
728     splice @matches, 0, $self->{select_offset}, ();
729 root 1.8
730 root 1.23 my $label = shift @labels;
731     $label->set_text ("...");
732     $label->set_tooltip ("Use Cursor-Up to view previous matches");
733 root 1.8 }
734    
735 root 1.23 for my $label (@labels) {
736     $label->{fg} = [1, 1, 1, 1];
737     $label->{bg} = [0, 0, 0, 0];
738     }
739    
740     if (@matches) {
741     $self->{select} = "$matches[0][0]$arg";
742    
743     $labels[0]->{fg} = [0, 0, 0, 1];
744     $labels[0]->{bg} = [1, 1, 1, 0.8];
745     } else {
746 root 1.24 $self->{select} = "$cmd$arg";
747 root 1.23 }
748    
749     for my $match (@matches) {
750     my $label = shift @labels;
751    
752     if (@labels) {
753     $label->set_text ("$match->[0]$arg");
754     $label->set_tooltip ($match->[1]);
755     } else {
756     $label->set_text ("...");
757     $label->set_tooltip ("Use Cursor-Down to view more matches");
758     last;
759     }
760     }
761 root 1.8
762 root 1.23 for my $label (@labels) {
763     $label->set_text ("");
764     $label->set_tooltip ("");
765 root 1.8 }
766    
767 root 1.23 $self->update;
768 root 1.8 }
769    
770 root 1.23 sub _draw {
771     my ($self) = @_;
772 root 1.8
773 root 1.23 # hack
774 root 1.80 local $CFPlus::UI::FOCUS = $self->{entry};
775 root 1.10
776 root 1.23 $self->SUPER::_draw;
777 root 1.2 }
778    
779 root 1.1 1