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.31 by root, Fri May 19 23:18:42 2006 UTC vs.
Revision 1.79 by elmex, Mon Jul 24 12:25:09 2006 UTC

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 CFClient;
8use CFClient::OpenGL; 9use CFClient::OpenGL;
10use CFClient::UI;
9 11
10our @ISA = CFClient::UI::Base::; 12our @ISA = CFClient::UI::Base::;
13
14my $magicmap_tex =
15 new_from_file CFClient::Texture CFClient::find_rcfile "magicmap.png",
16 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
11 17
12sub new { 18sub new {
13 my $class = shift; 19 my $class = shift;
14 20
15 my $self = $class->SUPER::new ( 21 my $self = $class->SUPER::new (
16 z => -1, 22 z => -1,
17 can_focus => 1, 23 can_focus => 1,
18 list => glGenList, 24 list => glGenList,
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
19 @_ 32 @_
20 ); 33 );
21 34
35 $self->{completer} = new CFClient::MapWidget::Command::
36 command => $self->{command},
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 . "<b>Right-Click</b> opens a menu where you cna select further options, sich as redefining key bindings.",
45 ;
46
22 $self 47 $self
23} 48}
24 49
25sub DESTROY { 50sub 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
62sub clr_commands {
26 my $self = shift; 63 my ($self) = @_;
27 64
28 glDeleteList $self->{list}; 65 %{$self->{completer}{command}} = ();
29 66
30 $self->SUPER::DESTROY; 67 $self->{completer}->hide
68 if $self->{completer};
31} 69}
32 70
33sub button_down { 71sub invoke_button_down {
34 my ($self, $ev, $x, $y) = @_; 72 my ($self, $ev, $x, $y) = @_;
35 73
36 $self->focus_in;
37
38 if ($ev->{button} == 2) { 74 if ($ev->{button} == 1) {
75 $self->grab_focus;
76 return unless $::CONN;
77
78 my $x = 1 + CFClient::floor +($ev->{x} - $self->{sx0}) / $self->{tilesize} - $self->{sx};
79 my $y = 1 + CFClient::floor +($ev->{y} - $self->{sy0}) / $self->{tilesize} - $self->{sy};
80
81 $x -= int 0.5 * $self->{sw};
82 $y -= int 0.5 * $self->{sh};
83
84 $::CONN->lookat ($x, $y)
85 if $::CONN;
86
87 } elsif ($ev->{button} == 2) {
88 $self->grab_focus;
89 return unless $::CONN;
90
39 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 91 my ($ox, $oy) = ($ev->{x}, $ev->{y});
40 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); 92 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
41 93
42 $self->{motion} = sub { 94 $self->{motion} = sub {
43 my ($ev, $x, $y) = @_; 95 my ($ev, $x, $y) = @_;
47 $::CFG->{map_shift_x} = $bw + $x - $ox; 99 $::CFG->{map_shift_x} = $bw + $x - $ox;
48 $::CFG->{map_shift_y} = $bh + $y - $oy; 100 $::CFG->{map_shift_y} = $bh + $y - $oy;
49 101
50 $self->update; 102 $self->update;
51 }; 103 };
104 } elsif ($ev->{button} == 3) {
105 (new CFClient::UI::Menu
106 items => [
107 ["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 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
111 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
112 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
113 ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
114 [
115 $::PICKUP_ENABLE->{state}
116 ? "Disable automatic pickup"
117 : "Enable automatic pickup",
118 sub { $::PICKUP_ENABLE->toggle }
119 ],
120 ["Quit",
121 sub {
122 if ($::CONN) {
123 &::open_quit_dialog;
124 } else {
125 exit;
126 }
127 }
128 ],
129 ],
130 )->popup ($ev);
131 }
132
52 } 133 1
53} 134}
54 135
55sub button_up { 136sub invoke_button_up {
56 my ($self, $ev, $x, $y) = @_; 137 my ($self, $ev, $x, $y) = @_;
57 138
58 delete $self->{motion}; 139 delete $self->{motion};
59}
60 140
141 1
142}
143
61sub mouse_motion { 144sub invoke_mouse_motion {
62 my ($self, $ev, $x, $y) = @_; 145 my ($self, $ev, $x, $y) = @_;
63 146
147 if ($self->{motion}) {
64 $self->{motion}->($ev, $x, $y) if $self->{motion}; 148 $self->{motion}->($ev, $x, $y);
149 } else {
150 return 0;
151 }
152
153 1
65} 154}
66 155
67sub size_request { 156sub size_request {
68 ( 157 (
69 1 + 32 * int $::WIDTH / 32, 158 32 * CFClient::ceil $::WIDTH / 32,
70 1 + 32 * int $::HEIGHT / 32, 159 32 * CFClient::ceil $::HEIGHT / 32,
71 ) 160 )
72} 161}
73 162
74sub update { 163sub update {
75 my ($self) = @_; 164 my ($self) = @_;
76 165
77 $self->{need_update} = 1; 166 $self->{need_update} = 1;
78 $self->SUPER::update; 167 $self->SUPER::update;
79} 168}
80 169
170my %DIR = (
171 CFClient::SDLK_KP8, [1, "north"],
172 CFClient::SDLK_KP9, [2, "northeast"],
173 CFClient::SDLK_KP6, [3, "east"],
174 CFClient::SDLK_KP3, [4, "southeast"],
175 CFClient::SDLK_KP2, [5, "south"],
176 CFClient::SDLK_KP1, [6, "southwest"],
177 CFClient::SDLK_KP4, [7, "west"],
178 CFClient::SDLK_KP7, [8, "northwest"],
179
180 CFClient::SDLK_UP, [1, "north"],
181 CFClient::SDLK_RIGHT, [3, "east"],
182 CFClient::SDLK_DOWN, [5, "south"],
183 CFClient::SDLK_LEFT, [7, "west"],
184);
185
186sub invoke_key_down {
187 my ($self, $ev) = @_;
188
189 my $mod = $ev->{mod};
190 my $sym = $ev->{sym};
191 my $uni = $ev->{unicode};
192
193 $mod &= CFClient::KMOD_CTRL | CFClient::KMOD_ALT | CFClient::KMOD_SHIFT;
194
195 if ($uni == ord "\t") {
196 $::PL_WINDOW->toggle_visibility;
197 } elsif ($sym == CFClient::SDLK_F1 && !$mod) {
198 $::HELP_WINDOW->toggle_visibility;
199 } elsif ($sym == CFClient::SDLK_F2 && !$mod) {
200 ::toggle_player_page ($::STATS_PAGE);
201 } elsif ($sym == CFClient::SDLK_F3 && !$mod) {
202 ::toggle_player_page ($::SKILL_PAGE);
203 } elsif ($sym == CFClient::SDLK_F4 && !$mod) {
204 ::toggle_player_page ($::SPELL_PAGE);
205 } elsif ($sym == CFClient::SDLK_F5 && !$mod) {
206 ::toggle_player_page ($::INVENTORY_PAGE);
207 } elsif ($sym == CFClient::SDLK_F9 && !$mod) {
208 $::SETUP_DIALOG->toggle_visibility;
209 } elsif ($sym == CFClient::SDLK_INSERT && $mod & CFClient::KMOD_CTRL) {
210 $::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 } elsif ($sym == CFClient::SDLK_INSERT && not ($mod & CFClient::KMOD_CTRL)) {
218 $::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 } elsif ($sym == CFClient::SDLK_KP5 && !$mod) {
225 $::CONN->user_send ("stay fire");
226 } elsif ($uni == ord ",") {
227 $::CONN->user_send ("take");
228 } elsif ($uni == ord " ") {
229 $::CONN->user_send ("apply");
230 } elsif ($uni == ord ".") {
231 $::CONN->user_send ($self->{completer}{last_command})
232 if exists $self->{completer}{last_command};
233 } elsif (my $bind_cmd = $::CFG->{profile}{default}{bindings}{$mod}{$sym}) {
234 $::CONN->user_send ($_) for @$bind_cmd;
235 } elsif (($sym == CFClient::SDLK_KP_PLUS && !$mod) || $uni == ord "+") {
236 $::CONN->user_send ("rotateshoottype +");
237 } elsif (($sym == CFClient::SDLK_KP_MINUS && !$mod) || $uni == ord "-") {
238 $::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 if ($mod & CFClient::KMOD_SHIFT) {
247 $self->{shft}++;
248 if ($DIR{$sym}[0] != $self->{fire_dir}) {
249 $::CONN->user_send ("fire $DIR{$sym}[0]");
250 }
251 $self->{fire_dir} = $DIR{$sym}[0];
252 } elsif ($mod & CFClient::KMOD_CTRL) {
253 $self->{ctrl}++;
254 $::CONN->user_send ("run $DIR{$sym}[0]");
255 } else {
256 $::CONN->user_send ("$DIR{$sym}[1]");
257 }
258 } elsif ((ord 'a') <= $uni && $uni <= (ord 'z')) {
259 $self->{completer}->inject_key_down ($ev);
260 $self->{completer}->show;
261 } else {
262 return 0;
263 }
264
265 1
266}
267
268sub invoke_key_up {
269 my ($self, $ev) = @_;
270
271 my $res = 0;
272 my $mod = $ev->{mod};
273 my $sym = $ev->{sym};
274
275 if ($::CFG->{shift_fire_stop}) {
276 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
277 $::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 } elsif (($sym == CFClient::SDLK_LSHIFT || $sym == CFClient::SDLK_RSHIFT) && delete $self->{shft}) { # XXX: is RSHIFT ok?
287 $::CONN->user_send ("fire_stop");
288 delete $self->{fire_dir};
289 $res = 1;
290 }
291 }
292
293 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
294 $::CONN->user_send ("run_stop");
295 $res = 1;
296 }
297
298 $res
299}
300
301sub set_magicmap {
302 my ($self, $w, $h, $x, $y, $data) = @_;
303
304 $x -= $::MAP->ox + int 0.5 * $::MAP->w;
305 $y -= $::MAP->oy + int 0.5 * $::MAP->h;
306
307 $self->{magicmap} = [$x, $y, $w, $h, $data];
308
309 $self->update;
310}
311
81sub draw { 312sub draw {
82 my ($self) = @_; 313 my ($self) = @_;
83 314
315 return unless $::MAP;
316
317 my $focused = $CFClient::UI::FOCUS == $self
318 || $CFClient::UI::FOCUS == $self->{completer}{entry};
319
84 return 320 return
85 unless $CFClient::UI::FOCUS == $self || !$::FAST; 321 unless $focused || !$::FAST;
86 322
87 if (delete $self->{need_update}) { 323 if (delete $self->{need_update}) {
324 my $tilesize = $self->{tilesize} = int 32 * $::CFG->{map_scale};
325
326 my $sx = $self->{sx} = CFClient::ceil $::CFG->{map_shift_x} / $tilesize;
327 my $sy = $self->{sy} = CFClient::ceil $::CFG->{map_shift_y} / $tilesize;
328
329 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
330 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
331
332 my $sw = $self->{sw} = 1 + CFClient::ceil $self->{w} / $tilesize;
333 my $sh = $self->{sh} = 1 + CFClient::ceil $self->{h} / $tilesize;
334
335 if ($::CFG->{fow_enable}) {
336 my ($w, $h, $data) = $::MAP->fow_texture ($sx, $sy, 0, 0, $sw, $sh);
337
338 if ($::CFG->{fow_smooth} && $CFClient::OpenGL::GL_VERSION >= 1.2) { # smooth fog of war
339 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 $self->{fow_texture} = new CFClient::Texture
351 w => $w,
352 h => $h,
353 data => $data,
354 internalformat => GL_ALPHA,
355 format => GL_ALPHA;
356
357 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
358 } else {
359 delete $self->{fow_texture};
360 }
361
88 glNewList $self->{list}; 362 glNewList $self->{list};
89 363
90 if ($::MAP) {
91 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
92 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
93
94 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
95 my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
96
97 glPushMatrix; 364 glPushMatrix;
365 glTranslate $sx0, $sy0;
98 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 366 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
99 367
100 glTranslate $sx0 - 32, $sy0 - 32, 0; 368 $::MAP->draw ($sx, $sy, 0, 0, $sw, $sh);
101 369
102 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); 370 glScale 32, 32;
103 371
104 if ($::CFG->{fow_enable}) { 372 if (my $tex = $self->{fow_texture}) {
105 if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war
106 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
107 glConvolutionFilter2D (
108 GL_CONVOLUTION_2D,
109 GL_ALPHA,
110 3, 3,
111 GL_ALPHA, GL_FLOAT,
112 pack "f*",
113 0.05, 0.13, 0.05,
114 0.13, 0.30, 0.13,
115 0.05, 0.13, 0.05,
116 );
117 glEnable GL_CONVOLUTION_2D;
118 }
119
120 # try to re-use the texture name: TODO improve texture class instead
121 my $prev_name = ((delete $self->{fow_texture}) || {})->{name};
122
123 $self->{fow_texture} = new CFClient::Texture
124 w => $w,
125 h => $h,
126 data => $data,
127 name => $prev_name, # a bit hackish
128 internalformat => GL_ALPHA,
129 format => GL_ALPHA;
130
131 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
132
133 glEnable GL_TEXTURE_2D; 373 glEnable GL_TEXTURE_2D;
134 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 374 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
135 375
136 glColor +($::CFG->{fow_intensity}) x 3, 0.8; 376 glColor +($::CFG->{fow_intensity}) x 3, 0.9;
137 $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32); 377 $self->{fow_texture}->draw_quad_alpha (0, 0);
138 378
139 glDisable GL_TEXTURE_2D; 379 glDisable GL_TEXTURE_2D;
140 } 380 }
141 381
382 if ($self->{magicmap}) {
383 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
384
385 $x += $::MAP->ox - $sx + int 0.5 * ($::MAP->w - $sw + 1);
386 $y += $::MAP->oy - $sy + int 0.5 * ($::MAP->h - $sh + 1);
387
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
142 glPopMatrix; 393 glPopMatrix;
143 }
144
145 glEndList; 394 glEndList;
146 } 395 }
147 396
148 glPushMatrix;
149 glCallList $self->{list}; 397 glCallList $self->{list};
150 glPopMatrix;
151 398
152 # TNT2 emulates logops in software (or worse :) 399 # TNT2 emulates logops in software (or worse :)
153 if ($CFClient::UI::FOCUS != $self) { 400 unless ($focused) {
154 glColor 0.4, 0.2, 0.2, 0.9; 401 glColor 0.4, 0.2, 0.2, 0.6;
155 glEnable GL_BLEND; 402 glEnable GL_BLEND;
156 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 403 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
157 glBegin GL_QUADS; 404 glBegin GL_QUADS;
158 glVertex 0, 0; 405 glVertex 0, 0;
159 glVertex 0, $::HEIGHT; 406 glVertex 0, $::HEIGHT;
162 glEnd; 409 glEnd;
163 glDisable GL_BLEND; 410 glDisable GL_BLEND;
164 } 411 }
165} 412}
166 413
167my %DIR = ( 414sub DESTROY {
168 CFClient::SDLK_KP8, [1, "north"],
169 CFClient::SDLK_KP9, [2, "northeast"],
170 CFClient::SDLK_KP6, [3, "east"],
171 CFClient::SDLK_KP3, [4, "southeast"],
172 CFClient::SDLK_KP2, [5, "south"],
173 CFClient::SDLK_KP1, [6, "southwest"],
174 CFClient::SDLK_KP4, [7, "west"],
175 CFClient::SDLK_KP7, [8, "northwest"],
176
177 CFClient::SDLK_UP, [1, "north"],
178 CFClient::SDLK_RIGHT, [3, "east"],
179 CFClient::SDLK_DOWN, [5, "south"],
180 CFClient::SDLK_LEFT, [7, "west"],
181);
182
183sub key_down {
184 my ($self, $ev) = @_;
185
186 return unless $::CONN;
187
188 my $mod = $ev->{mod};
189 my $sym = $ev->{sym};
190 my $uni = $ev->{unicode};
191
192 if ($sym == CFClient::SDLK_KP5) {
193 $::CONN->user_send ("stay fire");
194 } elsif ($uni == ord ",") {
195 $::CONN->user_send ("take");
196 } elsif ($uni == ord "\t" or $uni == ord " ") {
197 $::CONN->user_send ("apply");
198 } elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") {
199 $::CONN->user_send ("rotateshoottype +");
200 } elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") {
201 $::CONN->user_send ("rotateshoottype -");
202 } elsif ($uni == ord "'") {
203 $self->emit ('activate_console');
204 } elsif ($uni == ord "/") {
205 $self->emit (activate_console => '/');
206 } elsif (exists $DIR{$sym}) {
207 if ($mod & CFClient::KMOD_SHIFT) {
208 $self->{shft}++;
209 $::CONN->user_send ("fire $DIR{$sym}[0]");
210 } elsif ($mod & CFClient::KMOD_CTRL) {
211 $self->{ctrl}++;
212 $::CONN->user_send ("run $DIR{$sym}[0]");
213 } else {
214 $::CONN->user_send ("$DIR{$sym}[1]");
215 }
216 } elsif ($ev->{unicode}) {
217 $self->{command_widget} ||=
218 new CFClient::MapWidget::Command::
219 command => $self->{command},
220 can_focus => 1,
221 connect_execute => sub {
222 # todo: support callback instead of user_send
223 $::CONN->user_send ($_[1]);
224 },
225 connect_close => sub {
226 (delete $self->{command_widget})->hide;
227 $self->focus_in;
228 },
229 ;
230 $self->{command_widget}->key_down ($ev)
231 unless $ev->{unicode} == 20;
232 return unless $self->{command_widget};
233 $self->{command_widget}->show;
234 $self->{command_widget}->focus_in;
235 }
236}
237
238sub key_up {
239 my ($self, $ev) = @_;
240
241 my $mod = $ev->{mod};
242 my $sym = $ev->{sym};
243
244 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
245 $::CONN->user_send ("fire_stop");
246 }
247 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
248 $::CONN->user_send ("run_stop");
249 }
250}
251
252sub add_command {
253 my ($self, $command, $tooltip, $widget, $cb) = @_;
254
255 (my $data = $command) =~ s/\\//g;
256
257 $tooltip =~ s/^\s+//;
258
259 $tooltip = "<big>$data</big>\n\n$tooltip";
260
261 $tooltip =~ s/\s+$//;
262
263 $self->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
264}
265
266sub clr_commands {
267 my ($self) = @_; 415 my $self = shift;
268 416
269 %{$self->{command}} = (); 417 glDeleteList $self->{list};
418
419 $self->SUPER::DESTROY;
270} 420}
271 421
272package CFClient::MapWidget::MapMap; 422package CFClient::MapWidget::MapMap;
273 423
274our @ISA = CFClient::UI::Base::; 424our @ISA = CFClient::UI::Base::;
278 428
279sub size_request { 429sub size_request {
280 ($::HEIGHT * 0.25, $::HEIGHT * 0.25) 430 ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
281} 431}
282 432
283sub size_allocate { 433sub invoke_size_allocate {
284 my ($self, $w, $h) = @_; 434 my ($self, $w, $h) = @_;
285 435
286 $self->SUPER::size_allocate ($w, $h);
287 $self->update; 436 $self->update;
437
438 1
288} 439}
289 440
290sub update { 441sub update {
291 my ($self) = @_; 442 my ($self) = @_;
292 443
375 fontsize => $_, 526 fontsize => $_,
376 ), (0.8) x 16 527 ), (0.8) x 16
377 ]; 528 ];
378 529
379 $self->{entry} = new CFClient::UI::Entry 530 $self->{entry} = new CFClient::UI::Entry
380 connect_changed => sub { 531 on_changed => sub {
381 $self->update_labels; 532 $self->update_labels;
533 0
534 },
535 on_button_down => sub {
536 my ($entry, $ev, $x, $y) = @_;
537
538 if ($ev->{button} == 3) {
539 (new CFClient::UI::Menu
540 items => [
541 ["bind <i>" . (CFClient::asxml $self->{select}) . "</i> to a key"
542 => sub { $::BIND_EDITOR->do_quick_binding ([$self->{select}], sub { $entry->grab_focus }) }]
543 ],
544 )->popup ($ev);
545 return 1;
546 }
547 0
548 },
549 on_key_down => sub {
550 my ($entry, $ev) = @_;
551
552 my $self = $entry->{parent}{parent};
553
554 if ($ev->{sym} == 13) {
555 if (exists $self->{select}) {
556 $self->{last_command} = $self->{select};
557 $::CONN->user_send ($self->{select});
558
559 unshift @{$self->{history}}, $self->{entry}->get_text;
560 $self->{hist_ptr} = 0;
561
562 $self->hide;
563 }
564 } elsif ($ev->{sym} == 27) {
565 $self->{hist_ptr} = 0;
566 $self->hide;
567 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
568 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 $self->update_labels;
579 } elsif ($ev->{sym} == CFClient::SDLK_UP) {
580 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 $self->update_labels;
593 } else {
594 return 0;
595 }
596
597 1
382 }; 598 }
599 ;
383 600
384 $self->{vbox}->add ( 601 $self->{vbox}->add (
385 $self->{entry}, 602 $self->{entry},
386 @{$self->{label}}, 603 @{$self->{label}},
387 ); 604 );
388 605
389 $self 606 $self
390} 607}
391 608
609sub set_prefix {
610 my ($self, $prefix) = @_;
611
612 $self->{entry}->set_text ($prefix);
613 $self->show;
614}
615
392sub size_allocate { 616sub invoke_size_allocate {
393 my ($self, $w, $h) = @_; 617 my ($self, $w, $h) = @_;
394 618
395 $self->SUPER::size_allocate ($w, $h);
396 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 619 $self->move_abs (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
397}
398 620
621 $self->SUPER::invoke_size_allocate ($w, $h)
622}
623
624sub show {
625 my ($self) = @_;
626
627 $self->SUPER::show;
628 $self->{entry}->grab_focus;
629}
630
631sub hide {
632 my ($self) = @_;
633
634 $self->{hist_ptr} = 0;
635
636 $self->SUPER::hide;
637 $self->{entry}->set_text ("");
638}
639
399sub key_down { 640sub inject_key_down {
400 my ($self, $ev) = @_; 641 my ($self, $ev) = @_;
401 642
402 if ($ev->{sym} == 13) { 643 $self->{entry}->grab_focus;
403 if (exists $self->{select}) {
404 $self->emit (execute => $self->{select});
405 $self->emit ("close");
406 }
407 } elsif ($ev->{sym} == 27) {
408 $self->{entry}->set_text ("");
409 $self->emit ("close");
410 return;
411 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
412 ++$self->{select_offset}
413 if $self->{select_offset} < $#{ $self->{last_match} || [] };
414 $self->update_labels;
415 } elsif ($ev->{sym} == CFClient::SDLK_UP) {
416 --$self->{select_offset}
417 if $self->{select_offset};
418 $self->update_labels;
419 } else {
420 #$self->{entry}{force_alloc} = 1;
421 $self->{entry}->key_down ($ev); 644 $self->{entry}->emit (key_down => $ev);
422 }
423} 645}
424 646
425sub update_labels { 647sub update_labels {
426 my ($self) = @_; 648 my ($self) = @_;
427 649
428 my $text = $self->{entry}->get_text; 650 my $text = $self->{entry}->get_text;
429 651
430 length $text 652 length $text
431 or return $self->emit ("close"); 653 or return $self->hide;
432 654
433 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; 655 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
434 656
435 if ($cmd ne $self->{last_search}) { 657 if ($text ne $self->{last_search}) {
436 my $regexp = do {
437 my ($beg, @chr) = split //, lc $cmd;
438
439 # the following regex is used to match our "completion entry"
440 # to an actual command - the parentheses match kind of "overhead"
441 # - the more characters the parentheses match, the less attractive
442 # is the match.
443 my $regexp = "^\Q$beg\E"
444 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
445 qr<$regexp>
446 };
447
448 my @match; 658 my @match;
659
660 if ($text =~ /^(.*?)\s+$/) {
661 @match = [$cmd, "(appended whitespace suppresses completion)"];
662 } else {
663 my $regexp = do {
664 my ($beg, @chr) = split //, lc $cmd;
665
666 # 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
449 my @penalty; 675 my @penalty;
450 676
451 for (keys %{$self->{command}}) { 677 for (keys %{$self->{command}}) {
452 if (@penalty = $_ =~ $regexp) { 678 if (@penalty = $_ =~ $regexp) {
453 push @match, [$_, length join "", map "::$_", grep defined, @penalty]; 679 push @match, [$_, length join "", map "::$_", grep defined, @penalty];
680 }
454 } 681 }
455 }
456 682
457 @match = map $self->{command}{$_->[0]}, 683 @match = map $self->{command}{$_->[0]},
458 sort { 684 sort {
459 $a->[1] <=> $b->[1] 685 $a->[1] <=> $b->[1]
460 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] 686 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
687 or (length $b->[0]) <=> (length $a->[0])
461 } @match; 688 } @match;
689 }
462 690
463 $self->{last_search} = $cmd; 691 $self->{last_search} = $text;
464 $self->{last_match} = \@match; 692 $self->{last_match} = \@match;
465 693
466 $self->{select_offset} = 0; 694 $self->{select_offset} = 0;
467 } 695 }
468 696
508 $label->set_text (""); 736 $label->set_text ("");
509 $label->set_tooltip (""); 737 $label->set_tooltip ("");
510 } 738 }
511 739
512 $self->update; 740 $self->update;
513 ###
514} 741}
515 742
516sub _draw { 743sub _draw {
517 my ($self) = @_; 744 my ($self) = @_;
518 745

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines