ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.9
Committed: Tue Aug 28 01:23:47 2007 UTC (16 years, 9 months ago) by root
Branch: MAIN
Changes since 1.8: +139 -9 lines
Log Message:
- generalise keyboard handling a bit into a default
  keymapping + macro functions (such as "!completer shout").
- default keymappings ahve a lower priority, so its possible to
  (in theory) override them. a UI for this has not yet been created.
- re-enable NPOT texture usage, if available.

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