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