ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.4
Committed: Sat Dec 9 22:28:11 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.3: +8 -11 lines
Log Message:
Support multiple macros bound on the same accelkey.

File Contents

# User Rev Content
1 root 1.1 package CFPlus::Macro;
2    
3     use strict;
4    
5     use CFPlus::UI;
6    
7     our $REFRESH_MACRO_LIST;
8    
9     # allowed modifiers
10     our %MODIFIER = (
11     "LShift" => CFPlus::KMOD_LSHIFT,
12     "RShift" => CFPlus::KMOD_RSHIFT,
13     "LCtrl" => CFPlus::KMOD_LCTRL,
14     "RCtrl" => CFPlus::KMOD_RCTRL,
15     "LAlt" => CFPlus::KMOD_LALT,
16     "RAlt" => CFPlus::KMOD_RALT,
17     "LMeta" => CFPlus::KMOD_LMETA,
18     "RMeta" => CFPlus::KMOD_RMETA,
19     );
20    
21     # allowed modifiers
22     our $MODIFIER_MASK |= $_ for values %MODIFIER;
23    
24     # can bind to these without any modifier
25     our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9);
26    
27     our @DIRECT_KEYS = (
28     CFPlus::SDLK_F1,
29     CFPlus::SDLK_F2,
30     CFPlus::SDLK_F3,
31     CFPlus::SDLK_F4,
32     CFPlus::SDLK_F5,
33     CFPlus::SDLK_F6,
34     CFPlus::SDLK_F7,
35     CFPlus::SDLK_F8,
36     CFPlus::SDLK_F9,
37     CFPlus::SDLK_F10,
38     CFPlus::SDLK_F11,
39     CFPlus::SDLK_F12,
40     CFPlus::SDLK_F13,
41     CFPlus::SDLK_F14,
42     CFPlus::SDLK_F15,
43     );
44    
45     sub accelkey_to_string($) {
46     join "-",
47     (grep $_[0][0] & $MODIFIER{$_},
48     keys %MODIFIER),
49     CFPlus::SDL_GetKeyName $_[0][1]
50     }
51    
52     sub trigger_to_string($) {
53     my ($macro) = @_;
54    
55     $macro->{accelkey}
56     ? accelkey_to_string $macro->{accelkey}
57     : "(none)"
58     }
59    
60     sub macro_to_text($) {
61     my ($macro) = @_;
62    
63     join "", map "$_\n", @{ $macro->{action} }
64     }
65    
66     sub macro_from_text($$) {
67     my ($macro, $text) = @_;
68    
69     $macro->{action} = [
70     grep /\S/, $text =~ /^\s*(.*?)\s*$/mg
71     ];
72     }
73    
74     sub trigger_edit {
75     my ($macro, $end_cb) = @_;
76    
77     my $window;
78    
79     my $done = sub {
80     $window->disconnect_all ("delete");
81     $window->disconnect_all ("focus_out");
82     $window->destroy;
83     &$end_cb;
84     };
85    
86     $window = new CFPlus::UI::Toplevel
87     title => "Edit Macro Trigger",
88     x => "center",
89     y => "center",
90     z => 1000,
91     can_events => 1,
92     can_focus => 1,
93     has_close_button => 1,
94     on_delete => sub {
95     $done->(0);
96     1
97     },
98     on_focus_out => sub {
99     $done->(0);
100     1
101     },
102     ;
103    
104     $window->add (my $vb = new CFPlus::UI::VBox);
105    
106     $vb->add (new CFPlus::UI::Label
107     text => "To bind the macro to a key,\n"
108     . "press a modifier (Ctrl, Alt\n"
109     . "and/or Shift) and a key, or\n"
110     . "0-9 and F1-F15 without any modifier\n\n"
111     . "To cancel press Escape or close this.\n\n"
112     . "Accelerator key combo:",
113     ellipsise => 0,
114     );
115    
116     $vb->add (my $entry = new CFPlus::UI::Label
117     fg => [0, 0, 0, 1],
118     bg => [1, 1, 0, 1],
119     );
120    
121     my $key_cb = sub {
122     my (undef, $ev) = @_;
123    
124     my $mod = $ev->{cmod} & $MODIFIER_MASK;
125     my $sym = $ev->{sym};
126    
127     if ($sym == 27) {
128     $done->(0);
129     return 1;
130     }
131    
132     $entry->set_text (
133     join "",
134     map "$_-",
135     grep $mod & $MODIFIER{$_},
136     keys %MODIFIER
137     );
138    
139     return if $sym >= CFPlus::SDLK_MODIFIER_MIN
140     && $sym <= CFPlus::SDLK_MODIFIER_MAX;
141    
142     if ($mod
143     || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS)
144     || (grep $_ == $sym, @DIRECT_KEYS)))
145     {
146     $macro->{accelkey} = [$mod, $sym];
147     $done->(1);
148     } else {
149     $entry->set_text ("cannot bind " . (CFPlus::SDL_GetKeyName $sym) . " without modifier.");
150     }
151     1
152     };
153    
154     $window->connect (key_up => $key_cb);
155     $window->connect (key_down => $key_cb);
156    
157     $window->grab_focus;
158     $window->show;
159     }
160    
161 root 1.2 # find macro by event
162     sub match_event($) {
163     my ($ev) = @_;
164    
165 root 1.4 grep {
166     if (my $key = $_->{accelkey}) {
167     $key->[1] == $ev->{sym}
168     && $key->[0] == ($ev->{mod} & $MODIFIER_MASK)
169     } else {
170     0
171     }
172     } @{ $::PROFILE->{macro} || [] }
173 root 1.2 }
174    
175 root 1.1 sub keyboard_setup {
176     my $kbd_setup = new CFPlus::UI::VBox;
177    
178     $kbd_setup->add (my $list = new CFPlus::UI::VBox);
179    
180     $list->add (new CFPlus::UI::FancyFrame
181     label => "Options",
182     child => (my $hb = new CFPlus::UI::HBox),
183     );
184     $hb->add (new CFPlus::UI::Label text => "only shift-up stops fire");
185     $hb->add (new CFPlus::UI::CheckBox
186     expand => 1,
187     state => $::CFG->{shift_fire_stop},
188     tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.",
189     on_changed => sub {
190     my ($cbox, $value) = @_;
191     $::CFG->{shift_fire_stop} = $value;
192     0
193     },
194     );
195    
196     $list->add (new CFPlus::UI::FancyFrame
197     label => "Bindings",
198     child => (my $macros = new CFPlus::UI::Table),
199     );
200    
201     my $refresh;
202    
203     my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
204     my $tooltip_trigger = "The event that triggers execution of this macro, usually a key combination.";
205     my $tooltip_commands = "The commands that comprise the macro.";
206    
207     my $edit_macro = sub {
208     my ($macro) = @_;
209    
210     $kbd_setup->clear;
211     $kbd_setup->add (new CFPlus::UI::Button
212     text => "Return",
213     tooltip => "Return to the macro list.",
214     on_activate => sub {
215     $kbd_setup->clear;
216     $kbd_setup->add ($list);
217     $refresh->();
218     1
219     },
220     );
221     $kbd_setup->add (new CFPlus::UI::FancyFrame
222     label => "Edit Macro",
223     child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]),
224     );
225    
226     $editor->add (0, 1, new CFPlus::UI::Label
227     text => "Trigger",
228     tooltip => $tooltip_trigger,
229     can_hover => 1,
230     can_events => 1,
231     );
232     $editor->add (0, 2, new CFPlus::UI::Label
233     text => "Actions",
234     tooltip => $tooltip_commands,
235     can_hover => 1,
236     can_events => 1,
237     );
238    
239     $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit
240     text => macro_to_text $macro,
241     tooltip => $tooltip_commands,
242     on_changed => sub {
243     $macro->{action} = macro_from_text $macro, $_[1];
244     },
245     );
246    
247     $editor->add (1, 1, my $accel = new CFPlus::UI::Button
248     text => trigger_to_string $macro,
249     tooltip => "To change the trigger for a macro, activate this button.",
250     on_activate => sub {
251     my ($accel) = @_;
252     trigger_edit $macro, sub {
253     $accel->set_text (trigger_to_string $macro);
254     };
255     1
256     },
257     );
258    
259     my $recording;
260     $editor->add (1, 3, new CFPlus::UI::Button
261     text => "Start Recording",
262     tooltip => "Start/Stop command recording: when recording, "
263     . "actions and commands you invoke are appended to this macro. "
264     . "You can only record when you are logged in.",
265     on_destroy => sub {
266     $::CONN->record if $::CONN;
267     },
268     on_activate => sub {
269     my ($widget) = @_;
270    
271     $recording = $::CONN && !$recording;
272     if ($recording) {
273     $widget->set_text ("Stop Recording");
274     my $action = $macro->{action} ||= [];
275     $::CONN->record (sub {
276     push @$action, $_[0];
277     $textedit->set_text (macro_to_text $macro);
278     }) if $::CONN;
279     } else {
280     $widget->set_text ("Start Recording");
281     $::CONN->record if $::CONN;
282     }
283     },
284     );
285     };
286    
287     $REFRESH_MACRO_LIST = $refresh = sub {
288     $macros->clear;
289    
290     $macros->add (0, 0, new CFPlus::UI::Label
291     text => "Trigger",
292     align => 0,
293     tooltip => $tooltip_trigger . $tooltip_common,
294     );
295     $macros->add (1, 0, new CFPlus::UI::Label
296     text => "Commands",
297     tooltip => $tooltip_commands . $tooltip_common,
298     );
299    
300     for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
301     my $macro = $::PROFILE->{macro}[$idx];
302     my $y = $idx + 1;
303    
304     my $macro_cb = sub {
305     my ($widget, $ev) = @_;
306    
307     if ($ev->{button} == 1) {
308     $edit_macro->($macro),
309     } elsif ($ev->{button} == 2) {
310     $::CONN->macro_send ($macro) if $::CONN;
311     } elsif ($ev->{button} == 3) {
312     (new CFPlus::UI::Menu
313     items => [
314     ["Edit" => sub { $edit_macro->($macro) }],
315     ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }],
316     ["Delete" => sub {
317     # might want to use grep instead
318     splice @{$::PROFILE->{macro}}, $idx, 1, ();
319     $refresh->();
320     }],
321     ],
322     )->popup ($ev);
323     } else {
324     return 0;
325     }
326    
327     1
328     };
329    
330     $macros->add (0, $y, new CFPlus::UI::Label
331     text => trigger_to_string $macro,
332     tooltip => $tooltip_trigger . $tooltip_common,
333     align => 0,
334     can_hover => 1,
335     can_events => 1,
336     on_button_down => $macro_cb,
337     );
338    
339     $macros->add (1, $y, new CFPlus::UI::Label
340 root 1.3 text => (join "; ", @{ $macro->{action} || [] }),
341 root 1.1 tooltip => $tooltip_commands . $tooltip_common,
342     expand => 1,
343     ellipsise => 3,
344     can_hover => 1,
345     can_events => 1,
346     on_button_down => $macro_cb,
347     );
348     }
349     };
350    
351     $refresh->();
352    
353     $kbd_setup
354     }
355    
356     # this is a shortcut method that asks for a binding
357     # and then just binds it.
358     sub quick_macro {
359 root 1.3 my ($cmds, $end_cb) = @_;
360 root 1.1
361     my $macro = {
362     action => $cmds,
363     };
364    
365     trigger_edit $macro, sub {
366     if ($_[0]) {
367     push @{ $::PROFILE->{macro} }, $macro;
368     $REFRESH_MACRO_LIST->();
369     }
370    
371     &$end_cb if $end_cb;
372     };
373     }
374