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