ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.21
Committed: Mon Oct 20 15:44:21 2008 UTC (15 years, 8 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_03, rel-2_02, rel-2_05, rel-2_04, rel-2_0, rel-2_10, rel-0_9978
Changes since 1.20: +2 -2 lines
Log Message:
fixed crash bug in macro dialogue.

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.1 );
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 root 1.16 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 root 1.1 );
83    
84 root 1.9 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 elmex 1.13 "close-current-tab" => sub { $::MESSAGE_WINDOW->close_current_tab },
97 root 1.9 "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 root 1.20 local $MODIFIER{LRAM} = DC::KMOD_LRAM; # hack to enable internal LRAM modifer
115    
116 root 1.16 my %sym = map +(DC::SDL_GetKeyName $_, $_), DC::SDLK_FIRST .. DC::SDLK_LAST;
117 root 1.9 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 root 1.16 $map->{K}[DC::popcount $mod]{$mod}{$sym} = $v;
130 root 1.9 }
131     }
132    
133     %DEFAULT_KEYMAP = ();
134     $map
135     };
136     }
137    
138 root 1.1 sub accelkey_to_string($) {
139     join "-",
140 root 1.9 (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER),
141 root 1.16 DC::SDL_GetKeyName $_[0][1]
142 root 1.1 }
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 root 1.16 $window = new DC::UI::Toplevel
179 root 1.1 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 root 1.16 $window->add (my $vb = new DC::UI::VBox);
197 root 1.1
198 root 1.16 $vb->add (new DC::UI::Label
199 root 1.1 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 root 1.16 $vb->add (my $entry = new DC::UI::Label
209 root 1.1 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 root 1.16 return if $sym >= DC::SDLK_MODIFIER_MIN
232     && $sym <= DC::SDLK_MODIFIER_MAX;
233 root 1.1
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 root 1.16 $entry->set_text ("cannot bind " . (DC::SDL_GetKeyName $sym) . " without modifier.");
242 root 1.1 }
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 root 1.9 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 root 1.12 if (my $cmd = $DEFAULT_KEYMAP->{U}{$ev->{unicode}}) {
267     return $cmd;
268     }
269    
270 root 1.9 ()
271     }
272    
273 root 1.2 # find macro by event
274 root 1.9 sub find($) {
275 root 1.2 my ($ev) = @_;
276    
277 root 1.9 # 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 root 1.4 } else {
309 root 1.9 $::CONN->send_command ($_)
310     if $::CONN;
311 root 1.4 }
312 root 1.9 }
313 root 1.2 }
314    
315 root 1.1 sub keyboard_setup {
316 root 1.16 my $kbd_setup = new DC::UI::VBox;
317 root 1.1
318 root 1.16 $kbd_setup->add (my $list = new DC::UI::VBox);
319 root 1.1
320 root 1.16 $list->add (new DC::UI::FancyFrame
321 root 1.1 label => "Options",
322 root 1.16 child => (my $hb = new DC::UI::HBox),
323 root 1.1 );
324 root 1.16 $hb->add (new DC::UI::Label text => "only shift-up stops fire");
325     $hb->add (new DC::UI::CheckBox
326 root 1.1 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 root 1.16 $list->add (new DC::UI::FancyFrame
337 root 1.5 label => "Macros",
338 root 1.16 child => (my $macros = new DC::UI::VBox),
339 root 1.1 );
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 root 1.16 $kbd_setup->add (new DC::UI::Button
352 root 1.1 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 root 1.16 $kbd_setup->add (new DC::UI::FancyFrame
362 root 1.1 label => "Edit Macro",
363 root 1.16 child => (my $editor = new DC::UI::Table col_expand => [0, 1]),
364 root 1.1 );
365    
366 root 1.16 $editor->add_at (0, 1, new DC::UI::Label
367 root 1.1 text => "Trigger",
368     tooltip => $tooltip_trigger,
369     can_hover => 1,
370     can_events => 1,
371     );
372 root 1.16 $editor->add_at (0, 2, new DC::UI::Label
373 root 1.1 text => "Actions",
374     tooltip => $tooltip_commands,
375     can_hover => 1,
376     can_events => 1,
377     );
378    
379 root 1.16 $editor->add_at (1, 2, my $textedit = new DC::UI::TextEdit
380 root 1.1 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 root 1.16 $editor->add_at (1, 1, my $accel = new DC::UI::Button
388 root 1.1 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 root 1.16 $editor->add_at (1, 3, new DC::UI::Button
401 root 1.1 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 elmex 1.6 push @{ $macro->{action} }, $_[0];
416 root 1.1 $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 root 1.16 $macros->add (new DC::UI::Button
427 root 1.5 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 root 1.16 $macros->add (my $macrolist = new DC::UI::Table col_expand => [0, 1]);
437 root 1.5
438 root 1.1 $REFRESH_MACRO_LIST = $refresh = sub {
439 root 1.5 $macrolist->clear;
440 root 1.1
441 root 1.16 $macrolist->add_at (0, 1, new DC::UI::Label
442 root 1.1 text => "Trigger",
443     tooltip => $tooltip_trigger . $tooltip_common,
444     );
445 root 1.16 $macrolist->add_at (1, 1, new DC::UI::Label
446 root 1.17 text => "Actions",
447     align => 0,
448 root 1.1 tooltip => $tooltip_commands . $tooltip_common,
449     );
450    
451     for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
452     my $macro = $::PROFILE->{macro}[$idx];
453 root 1.5 my $y = $idx + 2;
454 root 1.1
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 elmex 1.21 execute ($macro);
462 root 1.1 } elsif ($ev->{button} == 3) {
463 root 1.16 (new DC::UI::Menu
464 root 1.1 items => [
465     ["Edit" => sub { $edit_macro->($macro) }],
466 elmex 1.21 ["Invoke" => sub { execute ($macro) }],
467 root 1.1 ["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 root 1.16 $macrolist->add_at (0, $y, new DC::UI::Label
482 root 1.1 text => trigger_to_string $macro,
483     tooltip => $tooltip_trigger . $tooltip_common,
484 root 1.17 fg => [1, 0.8, 0.8],
485 root 1.1 can_hover => 1,
486     can_events => 1,
487     on_button_down => $macro_cb,
488     );
489    
490 root 1.16 $macrolist->add_at (1, $y, new DC::UI::Label
491 root 1.3 text => (join "; ", @{ $macro->{action} || [] }),
492 root 1.1 tooltip => $tooltip_commands . $tooltip_common,
493 root 1.17 fg => [0.9, 0.9, 0.9],
494     align => 0,
495 root 1.1 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 root 1.3 my ($cmds, $end_cb) = @_;
513 root 1.1
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 root 1.9 1