ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.80
Committed: Sun Jul 30 13:16:44 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-0_2
Changes since 1.79: +66 -66 lines
Log Message:
the big rename

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