1 | package CFPlus::Macro; |
1 | package CFPlus::Macro; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
|
|
5 | use List::Util (); |
5 | use CFPlus::UI; |
6 | use CFPlus::UI; |
6 | |
7 | |
7 | our $REFRESH_MACRO_LIST; |
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-e" => "examine", |
|
|
37 | "LAlt-s" => "ready_skill find traps", |
|
|
38 | "LAlt-d" => "ready_skill disarm traps", |
|
|
39 | "LAlt-p" => "ready_skill praying", |
|
|
40 | ); |
8 | |
41 | |
9 | # allowed modifiers |
42 | # allowed modifiers |
10 | our %MODIFIER = ( |
43 | our %MODIFIER = ( |
11 | "LShift" => CFPlus::KMOD_LSHIFT, |
44 | "LShift" => CFPlus::KMOD_LSHIFT, |
12 | "RShift" => CFPlus::KMOD_RSHIFT, |
45 | "RShift" => CFPlus::KMOD_RSHIFT, |
|
|
46 | # "Shift" => CFPlus::KMOD_LSHIFT | CFPlus::KMOD_RSHIFT, |
13 | "LCtrl" => CFPlus::KMOD_LCTRL, |
47 | "LCtrl" => CFPlus::KMOD_LCTRL, |
14 | "RCtrl" => CFPlus::KMOD_RCTRL, |
48 | "RCtrl" => CFPlus::KMOD_RCTRL, |
|
|
49 | # "Ctrl" => CFPlus::KMOD_LCTRL | CFPlus::KMOD_RCTRL, |
15 | "LAlt" => CFPlus::KMOD_LALT, |
50 | "LAlt" => CFPlus::KMOD_LALT, |
16 | "RAlt" => CFPlus::KMOD_RALT, |
51 | "RAlt" => CFPlus::KMOD_RALT, |
|
|
52 | # "Alt" => CFPlus::KMOD_LALT | CFPlus::KMOD_RALT, |
17 | "LMeta" => CFPlus::KMOD_LMETA, |
53 | "LMeta" => CFPlus::KMOD_LMETA, |
18 | "RMeta" => CFPlus::KMOD_RMETA, |
54 | "RMeta" => CFPlus::KMOD_RMETA, |
|
|
55 | # "Meta" => CFPlus::KMOD_LMETA | CFPlus::KMOD_RMETA, |
19 | ); |
56 | ); |
20 | |
57 | |
21 | # allowed modifiers |
58 | # allowed modifiers |
22 | our $MODIFIER_MASK |= $_ for values %MODIFIER; |
59 | our $MODIFIER_MASK |= $_ for values %MODIFIER; |
23 | |
60 | |
… | |
… | |
40 | CFPlus::SDLK_F13, |
77 | CFPlus::SDLK_F13, |
41 | CFPlus::SDLK_F14, |
78 | CFPlus::SDLK_F14, |
42 | CFPlus::SDLK_F15, |
79 | CFPlus::SDLK_F15, |
43 | ); |
80 | ); |
44 | |
81 | |
|
|
82 | our %MACRO_FUNCTION = ( |
|
|
83 | "toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility }, |
|
|
84 | "toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility }, |
|
|
85 | "toggle-help" => sub { $::HELP_WINDOW->toggle_visibility }, |
|
|
86 | "toggle-stats" => sub { ::toggle_player_page ($::STATS_PAGE) }, |
|
|
87 | "toggle-skills" => sub { ::toggle_player_page ($::SKILL_PAGE) }, |
|
|
88 | "toggle-spells" => sub { ::toggle_player_page ($::SPELL_PAGE) }, |
|
|
89 | "toggle-inventory" => sub { ::toggle_player_page ($::INVENTORY_PAGE) }, |
|
|
90 | "toggle-pickup" => sub { ::toggle_player_page ($::PICKUP_PAGE) }, |
|
|
91 | "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility }, |
|
|
92 | "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility }, |
|
|
93 | "switch-tab" => sub { $::MESSAGE_WINDOW->user_switch_to_page (0 + shift) }, |
|
|
94 | "close-current-tab" => sub { $::MESSAGE_WINDOW->close_current_tab }, |
|
|
95 | "activate-chat" => sub { $::MESSAGE_WINDOW->activate_current }, |
|
|
96 | "repeat-command" => sub { |
|
|
97 | $::CONN->user_send ($::COMPLETER->{last_command}) |
|
|
98 | if $::CONN && exists $::COMPLETER->{last_command}; |
|
|
99 | }, |
|
|
100 | "completer" => sub { |
|
|
101 | if ($::CONN) { |
|
|
102 | $::COMPLETER->set_prefix (shift); |
|
|
103 | $::COMPLETER->show; |
|
|
104 | } |
|
|
105 | }, |
|
|
106 | ); |
|
|
107 | |
|
|
108 | our $DEFAULT_KEYMAP; |
|
|
109 | |
|
|
110 | sub init { |
|
|
111 | $DEFAULT_KEYMAP ||= do { |
|
|
112 | my %sym = map +(CFPlus::SDL_GetKeyName $_, $_), CFPlus::SDLK_FIRST .. CFPlus::SDLK_LAST; |
|
|
113 | my $map; |
|
|
114 | |
|
|
115 | while (my ($k, $v) = each %DEFAULT_KEYMAP) { |
|
|
116 | if ($k =~ /^\((.)\)$/) { |
|
|
117 | $map->{U}{ord $1} = $v; |
|
|
118 | } else { |
|
|
119 | my @mod = split /-/, $k; |
|
|
120 | my $sym = $sym{pop @mod} |
|
|
121 | or warn "unknown keysym $k\n"; |
|
|
122 | |
|
|
123 | my $mod = 0; $mod |= $MODIFIER{$_} for @mod; |
|
|
124 | |
|
|
125 | $map->{K}[CFPlus::popcount $mod]{$mod}{$sym} = $v; |
|
|
126 | } |
|
|
127 | } |
|
|
128 | |
|
|
129 | %DEFAULT_KEYMAP = (); |
|
|
130 | $map |
|
|
131 | }; |
|
|
132 | } |
|
|
133 | |
45 | sub accelkey_to_string($) { |
134 | sub accelkey_to_string($) { |
46 | join "-", |
135 | join "-", |
47 | (grep $_[0][0] & $MODIFIER{$_}, |
136 | (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER), |
48 | keys %MODIFIER), |
|
|
49 | CFPlus::SDL_GetKeyName $_[0][1] |
137 | CFPlus::SDL_GetKeyName $_[0][1] |
50 | } |
138 | } |
51 | |
139 | |
52 | sub trigger_to_string($) { |
140 | sub trigger_to_string($) { |
53 | my ($macro) = @_; |
141 | my ($macro) = @_; |
… | |
… | |
156 | |
244 | |
157 | $window->grab_focus; |
245 | $window->grab_focus; |
158 | $window->show; |
246 | $window->show; |
159 | } |
247 | } |
160 | |
248 | |
|
|
249 | sub find_default($) { |
|
|
250 | my ($ev) = @_; |
|
|
251 | |
|
|
252 | for my $m (reverse grep $_, @{ $DEFAULT_KEYMAP->{K} }) { |
|
|
253 | for (keys %$m) { |
|
|
254 | if ($_ == ($ev->{mod} & $_)) { |
|
|
255 | if (defined (my $cmd = $m->{$_}{$ev->{sym}})) { |
|
|
256 | return $cmd; |
|
|
257 | } |
|
|
258 | } |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
|
|
262 | if (my $cmd = $DEFAULT_KEYMAP->{U}{$ev->{unicode}}) { |
|
|
263 | return $cmd; |
|
|
264 | } |
|
|
265 | |
|
|
266 | () |
|
|
267 | } |
|
|
268 | |
161 | # find macro by event |
269 | # find macro by event |
162 | # maybe return multiple results? |
270 | sub find($) { |
163 | sub match_event($) { |
|
|
164 | my ($ev) = @_; |
271 | my ($ev) = @_; |
165 | |
272 | |
166 | for my $macro (@{ $::PROFILE->{macro} || [] }) { |
273 | # try user-defined macros |
|
|
274 | if (my @user = |
|
|
275 | grep { |
167 | my $key = $macro->{accelkey} |
276 | if (my $key = $_->{accelkey}) { |
168 | or next; |
|
|
169 | |
|
|
170 | $key->[1] == $ev->{sym} |
277 | $key->[1] == $ev->{sym} |
171 | && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) |
278 | && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) |
172 | && return $macro; |
279 | } else { |
|
|
280 | 0 |
|
|
281 | } |
|
|
282 | } @{ $::PROFILE->{macro} || [] } |
|
|
283 | ) { |
|
|
284 | return @user; |
173 | } |
285 | } |
174 | |
286 | |
|
|
287 | # now try default keymap |
|
|
288 | if (defined (my $def = find_default $ev)) { |
|
|
289 | return { |
|
|
290 | action => [$def], |
|
|
291 | }; |
|
|
292 | } |
|
|
293 | |
175 | () |
294 | () |
|
|
295 | } |
|
|
296 | |
|
|
297 | sub execute { |
|
|
298 | my ($macro) = @_; |
|
|
299 | |
|
|
300 | for (@{ $macro->{action} }) { |
|
|
301 | if (/^\!(\S+)\s?(.*)$/) { |
|
|
302 | $MACRO_FUNCTION{$1}->($2) |
|
|
303 | if exists $MACRO_FUNCTION{$1}; |
|
|
304 | } else { |
|
|
305 | $::CONN->send_command ($_) |
|
|
306 | if $::CONN; |
|
|
307 | } |
|
|
308 | } |
176 | } |
309 | } |
177 | |
310 | |
178 | sub keyboard_setup { |
311 | sub keyboard_setup { |
179 | my $kbd_setup = new CFPlus::UI::VBox; |
312 | my $kbd_setup = new CFPlus::UI::VBox; |
180 | |
313 | |
… | |
… | |
195 | 0 |
328 | 0 |
196 | }, |
329 | }, |
197 | ); |
330 | ); |
198 | |
331 | |
199 | $list->add (new CFPlus::UI::FancyFrame |
332 | $list->add (new CFPlus::UI::FancyFrame |
200 | label => "Bindings", |
333 | label => "Macros", |
201 | child => (my $macros = new CFPlus::UI::Table), |
334 | child => (my $macros = new CFPlus::UI::VBox), |
202 | ); |
335 | ); |
203 | |
336 | |
204 | my $refresh; |
337 | my $refresh; |
205 | |
338 | |
206 | my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; |
339 | my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; |
… | |
… | |
224 | $kbd_setup->add (new CFPlus::UI::FancyFrame |
357 | $kbd_setup->add (new CFPlus::UI::FancyFrame |
225 | label => "Edit Macro", |
358 | label => "Edit Macro", |
226 | child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), |
359 | child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), |
227 | ); |
360 | ); |
228 | |
361 | |
229 | $editor->add (0, 1, new CFPlus::UI::Label |
362 | $editor->add_at (0, 1, new CFPlus::UI::Label |
230 | text => "Trigger", |
363 | text => "Trigger", |
231 | tooltip => $tooltip_trigger, |
364 | tooltip => $tooltip_trigger, |
232 | can_hover => 1, |
365 | can_hover => 1, |
233 | can_events => 1, |
366 | can_events => 1, |
234 | ); |
367 | ); |
235 | $editor->add (0, 2, new CFPlus::UI::Label |
368 | $editor->add_at (0, 2, new CFPlus::UI::Label |
236 | text => "Actions", |
369 | text => "Actions", |
237 | tooltip => $tooltip_commands, |
370 | tooltip => $tooltip_commands, |
238 | can_hover => 1, |
371 | can_hover => 1, |
239 | can_events => 1, |
372 | can_events => 1, |
240 | ); |
373 | ); |
241 | |
374 | |
242 | $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit |
375 | $editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit |
243 | text => macro_to_text $macro, |
376 | text => macro_to_text $macro, |
244 | tooltip => $tooltip_commands, |
377 | tooltip => $tooltip_commands, |
245 | on_changed => sub { |
378 | on_changed => sub { |
246 | $macro->{action} = macro_from_text $macro, $_[1]; |
379 | $macro->{action} = macro_from_text $macro, $_[1]; |
247 | }, |
380 | }, |
248 | ); |
381 | ); |
249 | |
382 | |
250 | $editor->add (1, 1, my $accel = new CFPlus::UI::Button |
383 | $editor->add_at (1, 1, my $accel = new CFPlus::UI::Button |
251 | text => trigger_to_string $macro, |
384 | text => trigger_to_string $macro, |
252 | tooltip => "To change the trigger for a macro, activate this button.", |
385 | tooltip => "To change the trigger for a macro, activate this button.", |
253 | on_activate => sub { |
386 | on_activate => sub { |
254 | my ($accel) = @_; |
387 | my ($accel) = @_; |
255 | trigger_edit $macro, sub { |
388 | trigger_edit $macro, sub { |
… | |
… | |
258 | 1 |
391 | 1 |
259 | }, |
392 | }, |
260 | ); |
393 | ); |
261 | |
394 | |
262 | my $recording; |
395 | my $recording; |
263 | $editor->add (1, 3, new CFPlus::UI::Button |
396 | $editor->add_at (1, 3, new CFPlus::UI::Button |
264 | text => "Start Recording", |
397 | text => "Start Recording", |
265 | tooltip => "Start/Stop command recording: when recording, " |
398 | tooltip => "Start/Stop command recording: when recording, " |
266 | . "actions and commands you invoke are appended to this macro. " |
399 | . "actions and commands you invoke are appended to this macro. " |
267 | . "You can only record when you are logged in.", |
400 | . "You can only record when you are logged in.", |
268 | on_destroy => sub { |
401 | on_destroy => sub { |
… | |
… | |
272 | my ($widget) = @_; |
405 | my ($widget) = @_; |
273 | |
406 | |
274 | $recording = $::CONN && !$recording; |
407 | $recording = $::CONN && !$recording; |
275 | if ($recording) { |
408 | if ($recording) { |
276 | $widget->set_text ("Stop Recording"); |
409 | $widget->set_text ("Stop Recording"); |
277 | my $action = $macro->{action} ||= []; |
|
|
278 | $::CONN->record (sub { |
410 | $::CONN->record (sub { |
279 | push @$action, $_[0]; |
411 | push @{ $macro->{action} }, $_[0]; |
280 | $textedit->set_text (macro_to_text $macro); |
412 | $textedit->set_text (macro_to_text $macro); |
281 | }) if $::CONN; |
413 | }) if $::CONN; |
282 | } else { |
414 | } else { |
283 | $widget->set_text ("Start Recording"); |
415 | $widget->set_text ("Start Recording"); |
284 | $::CONN->record if $::CONN; |
416 | $::CONN->record if $::CONN; |
285 | } |
417 | } |
286 | }, |
418 | }, |
287 | ); |
419 | ); |
288 | }; |
420 | }; |
289 | |
421 | |
|
|
422 | $macros->add (new CFPlus::UI::Button |
|
|
423 | text => "New Macro", |
|
|
424 | tooltip => "Creates a new, empty, macro you can edit.", |
|
|
425 | on_activate => sub { |
|
|
426 | my $macro = { }; |
|
|
427 | push @{ $::PROFILE->{macro} }, $macro; |
|
|
428 | $edit_macro->($macro); |
|
|
429 | }, |
|
|
430 | ); |
|
|
431 | |
|
|
432 | $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]); |
|
|
433 | |
290 | $REFRESH_MACRO_LIST = $refresh = sub { |
434 | $REFRESH_MACRO_LIST = $refresh = sub { |
291 | $macros->clear; |
435 | $macrolist->clear; |
292 | |
436 | |
293 | $macros->add (0, 0, new CFPlus::UI::Label |
437 | $macrolist->add_at (0, 1, new CFPlus::UI::Label |
294 | text => "Trigger", |
438 | text => "Trigger", |
295 | align => 0, |
439 | align => 0, |
296 | tooltip => $tooltip_trigger . $tooltip_common, |
440 | tooltip => $tooltip_trigger . $tooltip_common, |
297 | ); |
441 | ); |
298 | $macros->add (1, 0, new CFPlus::UI::Label |
442 | $macrolist->add_at (1, 1, new CFPlus::UI::Label |
299 | text => "Commands", |
443 | text => "Commands", |
300 | tooltip => $tooltip_commands . $tooltip_common, |
444 | tooltip => $tooltip_commands . $tooltip_common, |
301 | ); |
445 | ); |
302 | |
446 | |
303 | for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { |
447 | for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { |
304 | my $macro = $::PROFILE->{macro}[$idx]; |
448 | my $macro = $::PROFILE->{macro}[$idx]; |
305 | my $y = $idx + 1; |
449 | my $y = $idx + 2; |
306 | |
450 | |
307 | my $macro_cb = sub { |
451 | my $macro_cb = sub { |
308 | my ($widget, $ev) = @_; |
452 | my ($widget, $ev) = @_; |
309 | |
453 | |
310 | if ($ev->{button} == 1) { |
454 | if ($ev->{button} == 1) { |
… | |
… | |
328 | } |
472 | } |
329 | |
473 | |
330 | 1 |
474 | 1 |
331 | }; |
475 | }; |
332 | |
476 | |
333 | $macros->add (0, $y, new CFPlus::UI::Label |
477 | $macrolist->add_at (0, $y, new CFPlus::UI::Label |
334 | text => trigger_to_string $macro, |
478 | text => trigger_to_string $macro, |
335 | tooltip => $tooltip_trigger . $tooltip_common, |
479 | tooltip => $tooltip_trigger . $tooltip_common, |
336 | align => 0, |
480 | align => 0, |
337 | can_hover => 1, |
481 | can_hover => 1, |
338 | can_events => 1, |
482 | can_events => 1, |
339 | on_button_down => $macro_cb, |
483 | on_button_down => $macro_cb, |
340 | ); |
484 | ); |
341 | |
485 | |
342 | $macros->add (1, $y, new CFPlus::UI::Label |
486 | $macrolist->add_at (1, $y, new CFPlus::UI::Label |
343 | text => (join "; ", @{ $macro->{action} }), |
487 | text => (join "; ", @{ $macro->{action} || [] }), |
344 | tooltip => $tooltip_commands . $tooltip_common, |
488 | tooltip => $tooltip_commands . $tooltip_common, |
345 | expand => 1, |
489 | expand => 1, |
346 | ellipsise => 3, |
490 | ellipsise => 3, |
347 | can_hover => 1, |
491 | can_hover => 1, |
348 | can_events => 1, |
492 | can_events => 1, |
… | |
… | |
357 | } |
501 | } |
358 | |
502 | |
359 | # this is a shortcut method that asks for a binding |
503 | # this is a shortcut method that asks for a binding |
360 | # and then just binds it. |
504 | # and then just binds it. |
361 | sub quick_macro { |
505 | sub quick_macro { |
362 | my ($self, $cmds, $end_cb) = @_; |
506 | my ($cmds, $end_cb) = @_; |
363 | |
507 | |
364 | my $macro = { |
508 | my $macro = { |
365 | action => $cmds, |
509 | action => $cmds, |
366 | }; |
510 | }; |
367 | |
511 | |
368 | trigger_edit $macro, sub { |
512 | trigger_edit $macro, sub { |
369 | |
|
|
370 | if ($_[0]) { |
513 | if ($_[0]) { |
371 | push @{ $::PROFILE->{macro} }, $macro; |
514 | push @{ $::PROFILE->{macro} }, $macro; |
372 | $REFRESH_MACRO_LIST->(); |
515 | $REFRESH_MACRO_LIST->(); |
373 | } |
516 | } |
374 | |
517 | |
375 | &$end_cb if $end_cb; |
518 | &$end_cb if $end_cb; |
376 | }; |
519 | }; |
377 | } |
520 | } |
378 | |
521 | |
|
|
522 | 1 |