ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.10
Committed: Sun Sep 2 03:55:03 2007 UTC (16 years, 9 months ago) by root
Branch: MAIN
Changes since 1.9: +4 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 "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 );
38
39 # allowed modifiers
40 our %MODIFIER = (
41 "LShift" => CFPlus::KMOD_LSHIFT,
42 "RShift" => CFPlus::KMOD_RSHIFT,
43 # "Shift" => CFPlus::KMOD_LSHIFT | CFPlus::KMOD_RSHIFT,
44 "LCtrl" => CFPlus::KMOD_LCTRL,
45 "RCtrl" => CFPlus::KMOD_RCTRL,
46 # "Ctrl" => CFPlus::KMOD_LCTRL | CFPlus::KMOD_RCTRL,
47 "LAlt" => CFPlus::KMOD_LALT,
48 "RAlt" => CFPlus::KMOD_RALT,
49 # "Alt" => CFPlus::KMOD_LALT | CFPlus::KMOD_RALT,
50 "LMeta" => CFPlus::KMOD_LMETA,
51 "RMeta" => CFPlus::KMOD_RMETA,
52 # "Meta" => CFPlus::KMOD_LMETA | CFPlus::KMOD_RMETA,
53 );
54
55 # allowed modifiers
56 our $MODIFIER_MASK |= $_ for values %MODIFIER;
57
58 # can bind to these without any modifier
59 our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9);
60
61 our @DIRECT_KEYS = (
62 CFPlus::SDLK_F1,
63 CFPlus::SDLK_F2,
64 CFPlus::SDLK_F3,
65 CFPlus::SDLK_F4,
66 CFPlus::SDLK_F5,
67 CFPlus::SDLK_F6,
68 CFPlus::SDLK_F7,
69 CFPlus::SDLK_F8,
70 CFPlus::SDLK_F9,
71 CFPlus::SDLK_F10,
72 CFPlus::SDLK_F11,
73 CFPlus::SDLK_F12,
74 CFPlus::SDLK_F13,
75 CFPlus::SDLK_F14,
76 CFPlus::SDLK_F15,
77 );
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
130 sub accelkey_to_string($) {
131 join "-",
132 (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER),
133 CFPlus::SDL_GetKeyName $_[0][1]
134 }
135
136 sub trigger_to_string($) {
137 my ($macro) = @_;
138
139 $macro->{accelkey}
140 ? accelkey_to_string $macro->{accelkey}
141 : "(none)"
142 }
143
144 sub macro_to_text($) {
145 my ($macro) = @_;
146
147 join "", map "$_\n", @{ $macro->{action} }
148 }
149
150 sub macro_from_text($$) {
151 my ($macro, $text) = @_;
152
153 $macro->{action} = [
154 grep /\S/, $text =~ /^\s*(.*?)\s*$/mg
155 ];
156 }
157
158 sub trigger_edit {
159 my ($macro, $end_cb) = @_;
160
161 my $window;
162
163 my $done = sub {
164 $window->disconnect_all ("delete");
165 $window->disconnect_all ("focus_out");
166 $window->destroy;
167 &$end_cb;
168 };
169
170 $window = new CFPlus::UI::Toplevel
171 title => "Edit Macro Trigger",
172 x => "center",
173 y => "center",
174 z => 1000,
175 can_events => 1,
176 can_focus => 1,
177 has_close_button => 1,
178 on_delete => sub {
179 $done->(0);
180 1
181 },
182 on_focus_out => sub {
183 $done->(0);
184 1
185 },
186 ;
187
188 $window->add (my $vb = new CFPlus::UI::VBox);
189
190 $vb->add (new CFPlus::UI::Label
191 text => "To bind the macro to a key,\n"
192 . "press a modifier (Ctrl, Alt\n"
193 . "and/or Shift) and a key, or\n"
194 . "0-9 and F1-F15 without any modifier\n\n"
195 . "To cancel press Escape or close this.\n\n"
196 . "Accelerator key combo:",
197 ellipsise => 0,
198 );
199
200 $vb->add (my $entry = new CFPlus::UI::Label
201 fg => [0, 0, 0, 1],
202 bg => [1, 1, 0, 1],
203 );
204
205 my $key_cb = sub {
206 my (undef, $ev) = @_;
207
208 my $mod = $ev->{cmod} & $MODIFIER_MASK;
209 my $sym = $ev->{sym};
210
211 if ($sym == 27) {
212 $done->(0);
213 return 1;
214 }
215
216 $entry->set_text (
217 join "",
218 map "$_-",
219 grep $mod & $MODIFIER{$_},
220 keys %MODIFIER
221 );
222
223 return if $sym >= CFPlus::SDLK_MODIFIER_MIN
224 && $sym <= CFPlus::SDLK_MODIFIER_MAX;
225
226 if ($mod
227 || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS)
228 || (grep $_ == $sym, @DIRECT_KEYS)))
229 {
230 $macro->{accelkey} = [$mod, $sym];
231 $done->(1);
232 } else {
233 $entry->set_text ("cannot bind " . (CFPlus::SDL_GetKeyName $sym) . " without modifier.");
234 }
235 1
236 };
237
238 $window->connect (key_up => $key_cb);
239 $window->connect (key_down => $key_cb);
240
241 $window->grab_focus;
242 $window->show;
243 }
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
265 # find macro by event
266 sub find($) {
267 my ($ev) = @_;
268
269 # try user-defined macros
270 if (my @user =
271 grep {
272 if (my $key = $_->{accelkey}) {
273 $key->[1] == $ev->{sym}
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};
300 } else {
301 $::CONN->send_command ($_)
302 if $::CONN;
303 }
304 }
305 }
306
307 sub keyboard_setup {
308 my $kbd_setup = new CFPlus::UI::VBox;
309
310 $kbd_setup->add (my $list = new CFPlus::UI::VBox);
311
312 $list->add (new CFPlus::UI::FancyFrame
313 label => "Options",
314 child => (my $hb = new CFPlus::UI::HBox),
315 );
316 $hb->add (new CFPlus::UI::Label text => "only shift-up stops fire");
317 $hb->add (new CFPlus::UI::CheckBox
318 expand => 1,
319 state => $::CFG->{shift_fire_stop},
320 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.",
321 on_changed => sub {
322 my ($cbox, $value) = @_;
323 $::CFG->{shift_fire_stop} = $value;
324 0
325 },
326 );
327
328 $list->add (new CFPlus::UI::FancyFrame
329 label => "Macros",
330 child => (my $macros = new CFPlus::UI::VBox),
331 );
332
333 my $refresh;
334
335 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
336 my $tooltip_trigger = "The event that triggers execution of this macro, usually a key combination.";
337 my $tooltip_commands = "The commands that comprise the macro.";
338
339 my $edit_macro = sub {
340 my ($macro) = @_;
341
342 $kbd_setup->clear;
343 $kbd_setup->add (new CFPlus::UI::Button
344 text => "Return",
345 tooltip => "Return to the macro list.",
346 on_activate => sub {
347 $kbd_setup->clear;
348 $kbd_setup->add ($list);
349 $refresh->();
350 1
351 },
352 );
353 $kbd_setup->add (new CFPlus::UI::FancyFrame
354 label => "Edit Macro",
355 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]),
356 );
357
358 $editor->add_at (0, 1, new CFPlus::UI::Label
359 text => "Trigger",
360 tooltip => $tooltip_trigger,
361 can_hover => 1,
362 can_events => 1,
363 );
364 $editor->add_at (0, 2, new CFPlus::UI::Label
365 text => "Actions",
366 tooltip => $tooltip_commands,
367 can_hover => 1,
368 can_events => 1,
369 );
370
371 $editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit
372 text => macro_to_text $macro,
373 tooltip => $tooltip_commands,
374 on_changed => sub {
375 $macro->{action} = macro_from_text $macro, $_[1];
376 },
377 );
378
379 $editor->add_at (1, 1, my $accel = new CFPlus::UI::Button
380 text => trigger_to_string $macro,
381 tooltip => "To change the trigger for a macro, activate this button.",
382 on_activate => sub {
383 my ($accel) = @_;
384 trigger_edit $macro, sub {
385 $accel->set_text (trigger_to_string $macro);
386 };
387 1
388 },
389 );
390
391 my $recording;
392 $editor->add_at (1, 3, new CFPlus::UI::Button
393 text => "Start Recording",
394 tooltip => "Start/Stop command recording: when recording, "
395 . "actions and commands you invoke are appended to this macro. "
396 . "You can only record when you are logged in.",
397 on_destroy => sub {
398 $::CONN->record if $::CONN;
399 },
400 on_activate => sub {
401 my ($widget) = @_;
402
403 $recording = $::CONN && !$recording;
404 if ($recording) {
405 $widget->set_text ("Stop Recording");
406 $::CONN->record (sub {
407 push @{ $macro->{action} }, $_[0];
408 $textedit->set_text (macro_to_text $macro);
409 }) if $::CONN;
410 } else {
411 $widget->set_text ("Start Recording");
412 $::CONN->record if $::CONN;
413 }
414 },
415 );
416 };
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
430 $REFRESH_MACRO_LIST = $refresh = sub {
431 $macrolist->clear;
432
433 $macrolist->add_at (0, 1, new CFPlus::UI::Label
434 text => "Trigger",
435 align => 0,
436 tooltip => $tooltip_trigger . $tooltip_common,
437 );
438 $macrolist->add_at (1, 1, new CFPlus::UI::Label
439 text => "Commands",
440 tooltip => $tooltip_commands . $tooltip_common,
441 );
442
443 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
444 my $macro = $::PROFILE->{macro}[$idx];
445 my $y = $idx + 2;
446
447 my $macro_cb = sub {
448 my ($widget, $ev) = @_;
449
450 if ($ev->{button} == 1) {
451 $edit_macro->($macro),
452 } elsif ($ev->{button} == 2) {
453 $::CONN->macro_send ($macro) if $::CONN;
454 } elsif ($ev->{button} == 3) {
455 (new CFPlus::UI::Menu
456 items => [
457 ["Edit" => sub { $edit_macro->($macro) }],
458 ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }],
459 ["Delete" => sub {
460 # might want to use grep instead
461 splice @{$::PROFILE->{macro}}, $idx, 1, ();
462 $refresh->();
463 }],
464 ],
465 )->popup ($ev);
466 } else {
467 return 0;
468 }
469
470 1
471 };
472
473 $macrolist->add_at (0, $y, new CFPlus::UI::Label
474 text => trigger_to_string $macro,
475 tooltip => $tooltip_trigger . $tooltip_common,
476 align => 0,
477 can_hover => 1,
478 can_events => 1,
479 on_button_down => $macro_cb,
480 );
481
482 $macrolist->add_at (1, $y, new CFPlus::UI::Label
483 text => (join "; ", @{ $macro->{action} || [] }),
484 tooltip => $tooltip_commands . $tooltip_common,
485 expand => 1,
486 ellipsise => 3,
487 can_hover => 1,
488 can_events => 1,
489 on_button_down => $macro_cb,
490 );
491 }
492 };
493
494 $refresh->();
495
496 $kbd_setup
497 }
498
499 # this is a shortcut method that asks for a binding
500 # and then just binds it.
501 sub quick_macro {
502 my ($cmds, $end_cb) = @_;
503
504 my $macro = {
505 action => $cmds,
506 };
507
508 trigger_edit $macro, sub {
509 if ($_[0]) {
510 push @{ $::PROFILE->{macro} }, $macro;
511 $REFRESH_MACRO_LIST->();
512 }
513
514 &$end_cb if $end_cb;
515 };
516 }
517
518 1