ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.19
Committed: Sat Aug 30 08:04:01 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
Changes since 1.18: +10 -7 lines
Log Message:
*** empty log message ***

File Contents

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