1 |
package CFPlus::Macro; |
2 |
|
3 |
use strict; |
4 |
|
5 |
use List::Util (); |
6 |
use CFPlus::UI; |
7 |
|
8 |
our $REFRESH_MACRO_LIST; |
9 |
|
10 |
our %DEFAULT_KEYMAP = ( |
11 |
(map +("($_)" => "!completer $_"), "a" .. "z"), |
12 |
"(!)" => "!completer shout ", |
13 |
"(\")" => "!completer say ", |
14 |
"(')" => "!completer", |
15 |
|
16 |
"LShift-tab" => "!toggle-messagewindow", |
17 |
"RShift-tab" => "!toggle-messagewindow", |
18 |
"tab" => "!toggle-playerbook", |
19 |
"f1" => "!toggle-help", |
20 |
"f2" => "!toggle-stats", |
21 |
"f3" => "!toggle-skills", |
22 |
"f4" => "!toggle-spells", |
23 |
"f5" => "!toggle-inventory", |
24 |
"f9" => "!toggle-setup", |
25 |
(map +("LAlt-$_" => "!switch-tab $_"), 0..9), |
26 |
(map +("RAlt-$_" => "!switch-tab $_"), 0..9), |
27 |
"LAlt-x" => "!close-current-tab", |
28 |
"return" => "!activate-chat", |
29 |
"." => "!repeat-command", |
30 |
|
31 |
"," => "take", |
32 |
"space" => "apply", |
33 |
"enter" => "examine", |
34 |
"[+]" => "rotateshoottype +", |
35 |
"[-]" => "rotateshoottype -", |
36 |
"LAlt-s" => "ready_skill find traps", |
37 |
"LAlt-d" => "ready_skill disarm traps", |
38 |
"LAlt-p" => "ready_skill praying", |
39 |
); |
40 |
|
41 |
# allowed modifiers |
42 |
our %MODIFIER = ( |
43 |
"LShift" => CFPlus::KMOD_LSHIFT, |
44 |
"RShift" => CFPlus::KMOD_RSHIFT, |
45 |
# "Shift" => CFPlus::KMOD_LSHIFT | CFPlus::KMOD_RSHIFT, |
46 |
"LCtrl" => CFPlus::KMOD_LCTRL, |
47 |
"RCtrl" => CFPlus::KMOD_RCTRL, |
48 |
# "Ctrl" => CFPlus::KMOD_LCTRL | CFPlus::KMOD_RCTRL, |
49 |
"LAlt" => CFPlus::KMOD_LALT, |
50 |
"RAlt" => CFPlus::KMOD_RALT, |
51 |
# "Alt" => CFPlus::KMOD_LALT | CFPlus::KMOD_RALT, |
52 |
"LMeta" => CFPlus::KMOD_LMETA, |
53 |
"RMeta" => CFPlus::KMOD_RMETA, |
54 |
# "Meta" => CFPlus::KMOD_LMETA | CFPlus::KMOD_RMETA, |
55 |
); |
56 |
|
57 |
# allowed modifiers |
58 |
our $MODIFIER_MASK |= $_ for values %MODIFIER; |
59 |
|
60 |
# can bind to these without any modifier |
61 |
our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9); |
62 |
|
63 |
our @DIRECT_KEYS = ( |
64 |
CFPlus::SDLK_F1, |
65 |
CFPlus::SDLK_F2, |
66 |
CFPlus::SDLK_F3, |
67 |
CFPlus::SDLK_F4, |
68 |
CFPlus::SDLK_F5, |
69 |
CFPlus::SDLK_F6, |
70 |
CFPlus::SDLK_F7, |
71 |
CFPlus::SDLK_F8, |
72 |
CFPlus::SDLK_F9, |
73 |
CFPlus::SDLK_F10, |
74 |
CFPlus::SDLK_F11, |
75 |
CFPlus::SDLK_F12, |
76 |
CFPlus::SDLK_F13, |
77 |
CFPlus::SDLK_F14, |
78 |
CFPlus::SDLK_F15, |
79 |
); |
80 |
|
81 |
our %MACRO_FUNCTION = ( |
82 |
"toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility }, |
83 |
"toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility }, |
84 |
"toggle-help" => sub { $::HELP_WINDOW->toggle_visibility }, |
85 |
"toggle-stats" => sub { ::toggle_player_page ($::STATS_PAGE) }, |
86 |
"toggle-skills" => sub { ::toggle_player_page ($::SKILL_PAGE) }, |
87 |
"toggle-spells" => sub { ::toggle_player_page ($::SPELL_PAGE) }, |
88 |
"toggle-inventory" => sub { ::toggle_player_page ($::INVENTORY_PAGE) }, |
89 |
"toggle-pickup" => sub { ::toggle_player_page ($::PICKUP_PAGE) }, |
90 |
"toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility }, |
91 |
"toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility }, |
92 |
"switch-tab" => sub { $::MESSAGE_WINDOW->user_switch_to_page (0 + shift) }, |
93 |
"close-current-tab" => sub { $::MESSAGE_WINDOW->close_current_tab }, |
94 |
"activate-chat" => sub { $::MESSAGE_WINDOW->activate_current }, |
95 |
"repeat-command" => sub { |
96 |
$::CONN->user_send ($::COMPLETER->{last_command}) |
97 |
if $::CONN && exists $::COMPLETER->{last_command}; |
98 |
}, |
99 |
"completer" => sub { |
100 |
if ($::CONN) { |
101 |
$::COMPLETER->set_prefix (shift); |
102 |
$::COMPLETER->show; |
103 |
} |
104 |
}, |
105 |
); |
106 |
|
107 |
our $DEFAULT_KEYMAP; |
108 |
|
109 |
sub init { |
110 |
$DEFAULT_KEYMAP ||= do { |
111 |
my %sym = map +(CFPlus::SDL_GetKeyName $_, $_), CFPlus::SDLK_FIRST .. CFPlus::SDLK_LAST; |
112 |
my $map; |
113 |
|
114 |
while (my ($k, $v) = each %DEFAULT_KEYMAP) { |
115 |
if ($k =~ /^\((.)\)$/) { |
116 |
$map->{U}{ord $1} = $v; |
117 |
} else { |
118 |
my @mod = split /-/, $k; |
119 |
my $sym = $sym{pop @mod} |
120 |
or warn "unknown keysym $k\n"; |
121 |
|
122 |
my $mod = 0; $mod |= $MODIFIER{$_} for @mod; |
123 |
|
124 |
$map->{K}[CFPlus::popcount $mod]{$mod}{$sym} = $v; |
125 |
} |
126 |
} |
127 |
|
128 |
%DEFAULT_KEYMAP = (); |
129 |
$map |
130 |
}; |
131 |
} |
132 |
|
133 |
sub accelkey_to_string($) { |
134 |
join "-", |
135 |
(grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER), |
136 |
CFPlus::SDL_GetKeyName $_[0][1] |
137 |
} |
138 |
|
139 |
sub trigger_to_string($) { |
140 |
my ($macro) = @_; |
141 |
|
142 |
$macro->{accelkey} |
143 |
? accelkey_to_string $macro->{accelkey} |
144 |
: "(none)" |
145 |
} |
146 |
|
147 |
sub macro_to_text($) { |
148 |
my ($macro) = @_; |
149 |
|
150 |
join "", map "$_\n", @{ $macro->{action} } |
151 |
} |
152 |
|
153 |
sub macro_from_text($$) { |
154 |
my ($macro, $text) = @_; |
155 |
|
156 |
$macro->{action} = [ |
157 |
grep /\S/, $text =~ /^\s*(.*?)\s*$/mg |
158 |
]; |
159 |
} |
160 |
|
161 |
sub trigger_edit { |
162 |
my ($macro, $end_cb) = @_; |
163 |
|
164 |
my $window; |
165 |
|
166 |
my $done = sub { |
167 |
$window->disconnect_all ("delete"); |
168 |
$window->disconnect_all ("focus_out"); |
169 |
$window->destroy; |
170 |
&$end_cb; |
171 |
}; |
172 |
|
173 |
$window = new CFPlus::UI::Toplevel |
174 |
title => "Edit Macro Trigger", |
175 |
x => "center", |
176 |
y => "center", |
177 |
z => 1000, |
178 |
can_events => 1, |
179 |
can_focus => 1, |
180 |
has_close_button => 1, |
181 |
on_delete => sub { |
182 |
$done->(0); |
183 |
1 |
184 |
}, |
185 |
on_focus_out => sub { |
186 |
$done->(0); |
187 |
1 |
188 |
}, |
189 |
; |
190 |
|
191 |
$window->add (my $vb = new CFPlus::UI::VBox); |
192 |
|
193 |
$vb->add (new CFPlus::UI::Label |
194 |
text => "To bind the macro to a key,\n" |
195 |
. "press a modifier (Ctrl, Alt\n" |
196 |
. "and/or Shift) and a key, or\n" |
197 |
. "0-9 and F1-F15 without any modifier\n\n" |
198 |
. "To cancel press Escape or close this.\n\n" |
199 |
. "Accelerator key combo:", |
200 |
ellipsise => 0, |
201 |
); |
202 |
|
203 |
$vb->add (my $entry = new CFPlus::UI::Label |
204 |
fg => [0, 0, 0, 1], |
205 |
bg => [1, 1, 0, 1], |
206 |
); |
207 |
|
208 |
my $key_cb = sub { |
209 |
my (undef, $ev) = @_; |
210 |
|
211 |
my $mod = $ev->{cmod} & $MODIFIER_MASK; |
212 |
my $sym = $ev->{sym}; |
213 |
|
214 |
if ($sym == 27) { |
215 |
$done->(0); |
216 |
return 1; |
217 |
} |
218 |
|
219 |
$entry->set_text ( |
220 |
join "", |
221 |
map "$_-", |
222 |
grep $mod & $MODIFIER{$_}, |
223 |
keys %MODIFIER |
224 |
); |
225 |
|
226 |
return if $sym >= CFPlus::SDLK_MODIFIER_MIN |
227 |
&& $sym <= CFPlus::SDLK_MODIFIER_MAX; |
228 |
|
229 |
if ($mod |
230 |
|| ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS) |
231 |
|| (grep $_ == $sym, @DIRECT_KEYS))) |
232 |
{ |
233 |
$macro->{accelkey} = [$mod, $sym]; |
234 |
$done->(1); |
235 |
} else { |
236 |
$entry->set_text ("cannot bind " . (CFPlus::SDL_GetKeyName $sym) . " without modifier."); |
237 |
} |
238 |
1 |
239 |
}; |
240 |
|
241 |
$window->connect (key_up => $key_cb); |
242 |
$window->connect (key_down => $key_cb); |
243 |
|
244 |
$window->grab_focus; |
245 |
$window->show; |
246 |
} |
247 |
|
248 |
sub find_default($) { |
249 |
my ($ev) = @_; |
250 |
|
251 |
for my $m (reverse grep $_, @{ $DEFAULT_KEYMAP->{K} }) { |
252 |
for (keys %$m) { |
253 |
if ($_ == ($ev->{mod} & $_)) { |
254 |
if (defined (my $cmd = $m->{$_}{$ev->{sym}})) { |
255 |
return $cmd; |
256 |
} |
257 |
} |
258 |
} |
259 |
} |
260 |
|
261 |
if (my $cmd = $DEFAULT_KEYMAP->{U}{$ev->{unicode}}) { |
262 |
return $cmd; |
263 |
} |
264 |
|
265 |
() |
266 |
} |
267 |
|
268 |
# find macro by event |
269 |
sub find($) { |
270 |
my ($ev) = @_; |
271 |
|
272 |
# try user-defined macros |
273 |
if (my @user = |
274 |
grep { |
275 |
if (my $key = $_->{accelkey}) { |
276 |
$key->[1] == $ev->{sym} |
277 |
&& $key->[0] == ($ev->{mod} & $MODIFIER_MASK) |
278 |
} else { |
279 |
0 |
280 |
} |
281 |
} @{ $::PROFILE->{macro} || [] } |
282 |
) { |
283 |
return @user; |
284 |
} |
285 |
|
286 |
# now try default keymap |
287 |
if (defined (my $def = find_default $ev)) { |
288 |
return { |
289 |
action => [$def], |
290 |
}; |
291 |
} |
292 |
|
293 |
() |
294 |
} |
295 |
|
296 |
sub execute { |
297 |
my ($macro) = @_; |
298 |
|
299 |
for (@{ $macro->{action} }) { |
300 |
if (/^\!(\S+)\s?(.*)$/) { |
301 |
$MACRO_FUNCTION{$1}->($2) |
302 |
if exists $MACRO_FUNCTION{$1}; |
303 |
} else { |
304 |
$::CONN->send_command ($_) |
305 |
if $::CONN; |
306 |
} |
307 |
} |
308 |
} |
309 |
|
310 |
sub keyboard_setup { |
311 |
my $kbd_setup = new CFPlus::UI::VBox; |
312 |
|
313 |
$kbd_setup->add (my $list = new CFPlus::UI::VBox); |
314 |
|
315 |
$list->add (new CFPlus::UI::FancyFrame |
316 |
label => "Options", |
317 |
child => (my $hb = new CFPlus::UI::HBox), |
318 |
); |
319 |
$hb->add (new CFPlus::UI::Label text => "only shift-up stops fire"); |
320 |
$hb->add (new CFPlus::UI::CheckBox |
321 |
expand => 1, |
322 |
state => $::CFG->{shift_fire_stop}, |
323 |
tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.", |
324 |
on_changed => sub { |
325 |
my ($cbox, $value) = @_; |
326 |
$::CFG->{shift_fire_stop} = $value; |
327 |
0 |
328 |
}, |
329 |
); |
330 |
|
331 |
$list->add (new CFPlus::UI::FancyFrame |
332 |
label => "Macros", |
333 |
child => (my $macros = new CFPlus::UI::VBox), |
334 |
); |
335 |
|
336 |
my $refresh; |
337 |
|
338 |
my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; |
339 |
my $tooltip_trigger = "The event that triggers execution of this macro, usually a key combination."; |
340 |
my $tooltip_commands = "The commands that comprise the macro."; |
341 |
|
342 |
my $edit_macro = sub { |
343 |
my ($macro) = @_; |
344 |
|
345 |
$kbd_setup->clear; |
346 |
$kbd_setup->add (new CFPlus::UI::Button |
347 |
text => "Return", |
348 |
tooltip => "Return to the macro list.", |
349 |
on_activate => sub { |
350 |
$kbd_setup->clear; |
351 |
$kbd_setup->add ($list); |
352 |
$refresh->(); |
353 |
1 |
354 |
}, |
355 |
); |
356 |
$kbd_setup->add (new CFPlus::UI::FancyFrame |
357 |
label => "Edit Macro", |
358 |
child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), |
359 |
); |
360 |
|
361 |
$editor->add_at (0, 1, new CFPlus::UI::Label |
362 |
text => "Trigger", |
363 |
tooltip => $tooltip_trigger, |
364 |
can_hover => 1, |
365 |
can_events => 1, |
366 |
); |
367 |
$editor->add_at (0, 2, new CFPlus::UI::Label |
368 |
text => "Actions", |
369 |
tooltip => $tooltip_commands, |
370 |
can_hover => 1, |
371 |
can_events => 1, |
372 |
); |
373 |
|
374 |
$editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit |
375 |
text => macro_to_text $macro, |
376 |
tooltip => $tooltip_commands, |
377 |
on_changed => sub { |
378 |
$macro->{action} = macro_from_text $macro, $_[1]; |
379 |
}, |
380 |
); |
381 |
|
382 |
$editor->add_at (1, 1, my $accel = new CFPlus::UI::Button |
383 |
text => trigger_to_string $macro, |
384 |
tooltip => "To change the trigger for a macro, activate this button.", |
385 |
on_activate => sub { |
386 |
my ($accel) = @_; |
387 |
trigger_edit $macro, sub { |
388 |
$accel->set_text (trigger_to_string $macro); |
389 |
}; |
390 |
1 |
391 |
}, |
392 |
); |
393 |
|
394 |
my $recording; |
395 |
$editor->add_at (1, 3, new CFPlus::UI::Button |
396 |
text => "Start Recording", |
397 |
tooltip => "Start/Stop command recording: when recording, " |
398 |
. "actions and commands you invoke are appended to this macro. " |
399 |
. "You can only record when you are logged in.", |
400 |
on_destroy => sub { |
401 |
$::CONN->record if $::CONN; |
402 |
}, |
403 |
on_activate => sub { |
404 |
my ($widget) = @_; |
405 |
|
406 |
$recording = $::CONN && !$recording; |
407 |
if ($recording) { |
408 |
$widget->set_text ("Stop Recording"); |
409 |
$::CONN->record (sub { |
410 |
push @{ $macro->{action} }, $_[0]; |
411 |
$textedit->set_text (macro_to_text $macro); |
412 |
}) if $::CONN; |
413 |
} else { |
414 |
$widget->set_text ("Start Recording"); |
415 |
$::CONN->record if $::CONN; |
416 |
} |
417 |
}, |
418 |
); |
419 |
}; |
420 |
|
421 |
$macros->add (new CFPlus::UI::Button |
422 |
text => "New Macro", |
423 |
tooltip => "Creates a new, empty, macro you can edit.", |
424 |
on_activate => sub { |
425 |
my $macro = { }; |
426 |
push @{ $::PROFILE->{macro} }, $macro; |
427 |
$edit_macro->($macro); |
428 |
}, |
429 |
); |
430 |
|
431 |
$macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]); |
432 |
|
433 |
$REFRESH_MACRO_LIST = $refresh = sub { |
434 |
$macrolist->clear; |
435 |
|
436 |
$macrolist->add_at (0, 1, new CFPlus::UI::Label |
437 |
text => "Trigger", |
438 |
align => 0, |
439 |
tooltip => $tooltip_trigger . $tooltip_common, |
440 |
); |
441 |
$macrolist->add_at (1, 1, new CFPlus::UI::Label |
442 |
text => "Commands", |
443 |
tooltip => $tooltip_commands . $tooltip_common, |
444 |
); |
445 |
|
446 |
for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { |
447 |
my $macro = $::PROFILE->{macro}[$idx]; |
448 |
my $y = $idx + 2; |
449 |
|
450 |
my $macro_cb = sub { |
451 |
my ($widget, $ev) = @_; |
452 |
|
453 |
if ($ev->{button} == 1) { |
454 |
$edit_macro->($macro), |
455 |
} elsif ($ev->{button} == 2) { |
456 |
$::CONN->macro_send ($macro) if $::CONN; |
457 |
} elsif ($ev->{button} == 3) { |
458 |
(new CFPlus::UI::Menu |
459 |
items => [ |
460 |
["Edit" => sub { $edit_macro->($macro) }], |
461 |
["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }], |
462 |
["Delete" => sub { |
463 |
# might want to use grep instead |
464 |
splice @{$::PROFILE->{macro}}, $idx, 1, (); |
465 |
$refresh->(); |
466 |
}], |
467 |
], |
468 |
)->popup ($ev); |
469 |
} else { |
470 |
return 0; |
471 |
} |
472 |
|
473 |
1 |
474 |
}; |
475 |
|
476 |
$macrolist->add_at (0, $y, new CFPlus::UI::Label |
477 |
text => trigger_to_string $macro, |
478 |
tooltip => $tooltip_trigger . $tooltip_common, |
479 |
align => 0, |
480 |
can_hover => 1, |
481 |
can_events => 1, |
482 |
on_button_down => $macro_cb, |
483 |
); |
484 |
|
485 |
$macrolist->add_at (1, $y, new CFPlus::UI::Label |
486 |
text => (join "; ", @{ $macro->{action} || [] }), |
487 |
tooltip => $tooltip_commands . $tooltip_common, |
488 |
expand => 1, |
489 |
ellipsise => 3, |
490 |
can_hover => 1, |
491 |
can_events => 1, |
492 |
on_button_down => $macro_cb, |
493 |
); |
494 |
} |
495 |
}; |
496 |
|
497 |
$refresh->(); |
498 |
|
499 |
$kbd_setup |
500 |
} |
501 |
|
502 |
# this is a shortcut method that asks for a binding |
503 |
# and then just binds it. |
504 |
sub quick_macro { |
505 |
my ($cmds, $end_cb) = @_; |
506 |
|
507 |
my $macro = { |
508 |
action => $cmds, |
509 |
}; |
510 |
|
511 |
trigger_edit $macro, sub { |
512 |
if ($_[0]) { |
513 |
push @{ $::PROFILE->{macro} }, $macro; |
514 |
$REFRESH_MACRO_LIST->(); |
515 |
} |
516 |
|
517 |
&$end_cb if $end_cb; |
518 |
}; |
519 |
} |
520 |
|
521 |
1 |