ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.6
Committed: Sat Dec 16 16:14:34 2006 UTC (17 years, 6 months ago) by elmex
Branch: MAIN
CVS Tags: rel-0_97, rel-0_98
Changes since 1.5: +1 -2 lines
Log Message:
fixed a small bug in macro recording

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 root 1.5 label => "Macros",
198     child => (my $macros = new CFPlus::UI::VBox),
199 root 1.1 );
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     $::CONN->record (sub {
275 elmex 1.6 push @{ $macro->{action} }, $_[0];
276 root 1.1 $textedit->set_text (macro_to_text $macro);
277     }) if $::CONN;
278     } else {
279     $widget->set_text ("Start Recording");
280     $::CONN->record if $::CONN;
281     }
282     },
283     );
284     };
285    
286 root 1.5 $macros->add (new CFPlus::UI::Button
287     text => "New Macro",
288     tooltip => "Creates a new, empty, macro you can edit.",
289     on_activate => sub {
290     my $macro = { };
291     push @{ $::PROFILE->{macro} }, $macro;
292     $edit_macro->($macro);
293     },
294     );
295    
296     $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]);
297    
298 root 1.1 $REFRESH_MACRO_LIST = $refresh = sub {
299 root 1.5 $macrolist->clear;
300 root 1.1
301 root 1.5 $macrolist->add (0, 1, new CFPlus::UI::Label
302 root 1.1 text => "Trigger",
303     align => 0,
304     tooltip => $tooltip_trigger . $tooltip_common,
305     );
306 root 1.5 $macrolist->add (1, 1, new CFPlus::UI::Label
307 root 1.1 text => "Commands",
308     tooltip => $tooltip_commands . $tooltip_common,
309     );
310    
311     for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
312     my $macro = $::PROFILE->{macro}[$idx];
313 root 1.5 my $y = $idx + 2;
314 root 1.1
315     my $macro_cb = sub {
316     my ($widget, $ev) = @_;
317    
318     if ($ev->{button} == 1) {
319     $edit_macro->($macro),
320     } elsif ($ev->{button} == 2) {
321     $::CONN->macro_send ($macro) if $::CONN;
322     } elsif ($ev->{button} == 3) {
323     (new CFPlus::UI::Menu
324     items => [
325     ["Edit" => sub { $edit_macro->($macro) }],
326     ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }],
327     ["Delete" => sub {
328     # might want to use grep instead
329     splice @{$::PROFILE->{macro}}, $idx, 1, ();
330     $refresh->();
331     }],
332     ],
333     )->popup ($ev);
334     } else {
335     return 0;
336     }
337    
338     1
339     };
340    
341 root 1.5 $macrolist->add (0, $y, new CFPlus::UI::Label
342 root 1.1 text => trigger_to_string $macro,
343     tooltip => $tooltip_trigger . $tooltip_common,
344     align => 0,
345     can_hover => 1,
346     can_events => 1,
347     on_button_down => $macro_cb,
348     );
349    
350 root 1.5 $macrolist->add (1, $y, new CFPlus::UI::Label
351 root 1.3 text => (join "; ", @{ $macro->{action} || [] }),
352 root 1.1 tooltip => $tooltip_commands . $tooltip_common,
353     expand => 1,
354     ellipsise => 3,
355     can_hover => 1,
356     can_events => 1,
357     on_button_down => $macro_cb,
358     );
359     }
360     };
361    
362     $refresh->();
363    
364     $kbd_setup
365     }
366    
367     # this is a shortcut method that asks for a binding
368     # and then just binds it.
369     sub quick_macro {
370 root 1.3 my ($cmds, $end_cb) = @_;
371 root 1.1
372     my $macro = {
373     action => $cmds,
374     };
375    
376     trigger_edit $macro, sub {
377     if ($_[0]) {
378     push @{ $::PROFILE->{macro} }, $macro;
379     $REFRESH_MACRO_LIST->();
380     }
381    
382     &$end_cb if $end_cb;
383     };
384     }
385