ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.22
Committed: Sat Apr 3 02:58:24 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_11, HEAD
Changes since 1.21: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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