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