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

File Contents

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