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