1 | package CFPlus::Macro; |
1 | package DC::Macro; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
|
|
5 | use List::Util (); |
5 | use CFPlus::UI; |
6 | use DC::UI; |
6 | |
7 | |
7 | our $REFRESH_MACRO_LIST; |
8 | our $REFRESH_MACRO_LIST; |
|
|
9 | |
|
|
10 | 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 | "LAlt-x" => "!close-current-tab", |
|
|
28 | "return" => "!activate-chat", |
|
|
29 | "." => "!repeat-command", |
|
|
30 | |
|
|
31 | "," => "take", |
|
|
32 | "space" => "apply", |
|
|
33 | "enter" => "examine", |
|
|
34 | "[+]" => "rotateshoottype +", |
|
|
35 | "[-]" => "rotateshoottype -", |
|
|
36 | "LAlt-e" => "examine", |
|
|
37 | "LAlt-s" => "ready_skill find traps", |
|
|
38 | "LAlt-d" => "ready_skill disarm traps", |
|
|
39 | "LAlt-p" => "ready_skill praying", |
|
|
40 | ); |
8 | |
41 | |
9 | # allowed modifiers |
42 | # allowed modifiers |
10 | our %MODIFIER = ( |
43 | our %MODIFIER = ( |
11 | "LShift" => CFPlus::KMOD_LSHIFT, |
44 | "LShift" => DC::KMOD_LSHIFT, |
12 | "RShift" => CFPlus::KMOD_RSHIFT, |
45 | "RShift" => DC::KMOD_RSHIFT, |
|
|
46 | # "Shift" => DC::KMOD_LSHIFT | DC::KMOD_RSHIFT, |
13 | "LCtrl" => CFPlus::KMOD_LCTRL, |
47 | "LCtrl" => DC::KMOD_LCTRL, |
14 | "RCtrl" => CFPlus::KMOD_RCTRL, |
48 | "RCtrl" => DC::KMOD_RCTRL, |
|
|
49 | # "Ctrl" => DC::KMOD_LCTRL | DC::KMOD_RCTRL, |
15 | "LAlt" => CFPlus::KMOD_LALT, |
50 | "LAlt" => DC::KMOD_LALT, |
16 | "RAlt" => CFPlus::KMOD_RALT, |
51 | "RAlt" => DC::KMOD_RALT, |
|
|
52 | # "Alt" => DC::KMOD_LALT | DC::KMOD_RALT, |
17 | "LMeta" => CFPlus::KMOD_LMETA, |
53 | "LMeta" => DC::KMOD_LMETA, |
18 | "RMeta" => CFPlus::KMOD_RMETA, |
54 | "RMeta" => DC::KMOD_RMETA, |
|
|
55 | # "Meta" => DC::KMOD_LMETA | DC::KMOD_RMETA, |
19 | ); |
56 | ); |
20 | |
57 | |
21 | # allowed modifiers |
58 | # allowed modifiers |
22 | our $MODIFIER_MASK |= $_ for values %MODIFIER; |
59 | our $MODIFIER_MASK |= $_ for values %MODIFIER; |
23 | |
60 | |
24 | # can bind to these without any modifier |
61 | # can bind to these without any modifier |
25 | our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9); |
62 | our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9); |
26 | |
63 | |
27 | our @DIRECT_KEYS = ( |
64 | our @DIRECT_KEYS = ( |
28 | CFPlus::SDLK_F1, |
65 | DC::SDLK_F1, |
29 | CFPlus::SDLK_F2, |
66 | DC::SDLK_F2, |
30 | CFPlus::SDLK_F3, |
67 | DC::SDLK_F3, |
31 | CFPlus::SDLK_F4, |
68 | DC::SDLK_F4, |
32 | CFPlus::SDLK_F5, |
69 | DC::SDLK_F5, |
33 | CFPlus::SDLK_F6, |
70 | DC::SDLK_F6, |
34 | CFPlus::SDLK_F7, |
71 | DC::SDLK_F7, |
35 | CFPlus::SDLK_F8, |
72 | DC::SDLK_F8, |
36 | CFPlus::SDLK_F9, |
73 | DC::SDLK_F9, |
37 | CFPlus::SDLK_F10, |
74 | DC::SDLK_F10, |
38 | CFPlus::SDLK_F11, |
75 | DC::SDLK_F11, |
39 | CFPlus::SDLK_F12, |
76 | DC::SDLK_F12, |
40 | CFPlus::SDLK_F13, |
77 | DC::SDLK_F13, |
41 | CFPlus::SDLK_F14, |
78 | DC::SDLK_F14, |
42 | CFPlus::SDLK_F15, |
79 | DC::SDLK_F15, |
43 | ); |
80 | ); |
|
|
81 | |
|
|
82 | our %MACRO_FUNCTION = ( |
|
|
83 | "toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility }, |
|
|
84 | "toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility }, |
|
|
85 | "toggle-help" => sub { $::HELP_WINDOW->toggle_visibility }, |
|
|
86 | "toggle-stats" => sub { ::toggle_player_page ($::STATS_PAGE) }, |
|
|
87 | "toggle-skills" => sub { ::toggle_player_page ($::SKILL_PAGE) }, |
|
|
88 | "toggle-spells" => sub { ::toggle_player_page ($::SPELL_PAGE) }, |
|
|
89 | "toggle-inventory" => sub { ::toggle_player_page ($::INVENTORY_PAGE) }, |
|
|
90 | "toggle-pickup" => sub { ::toggle_player_page ($::PICKUP_PAGE) }, |
|
|
91 | "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility }, |
|
|
92 | "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility }, |
|
|
93 | "switch-tab" => sub { $::MESSAGE_WINDOW->user_switch_to_page (0 + shift) }, |
|
|
94 | "close-current-tab" => sub { $::MESSAGE_WINDOW->close_current_tab }, |
|
|
95 | "activate-chat" => sub { $::MESSAGE_WINDOW->activate_current }, |
|
|
96 | "repeat-command" => sub { |
|
|
97 | $::CONN->user_send ($::COMPLETER->{last_command}) |
|
|
98 | if $::CONN && exists $::COMPLETER->{last_command}; |
|
|
99 | }, |
|
|
100 | "completer" => sub { |
|
|
101 | if ($::CONN) { |
|
|
102 | $::COMPLETER->set_prefix (shift); |
|
|
103 | $::COMPLETER->show; |
|
|
104 | } |
|
|
105 | }, |
|
|
106 | ); |
|
|
107 | |
|
|
108 | our $DEFAULT_KEYMAP; |
|
|
109 | |
|
|
110 | sub init { |
|
|
111 | $DEFAULT_KEYMAP ||= do { |
|
|
112 | my %sym = map +(DC::SDL_GetKeyName $_, $_), DC::SDLK_FIRST .. DC::SDLK_LAST; |
|
|
113 | my $map; |
|
|
114 | |
|
|
115 | while (my ($k, $v) = each %DEFAULT_KEYMAP) { |
|
|
116 | if ($k =~ /^\((.)\)$/) { |
|
|
117 | $map->{U}{ord $1} = $v; |
|
|
118 | } else { |
|
|
119 | my @mod = split /-/, $k; |
|
|
120 | my $sym = $sym{pop @mod} |
|
|
121 | or warn "unknown keysym $k\n"; |
|
|
122 | |
|
|
123 | my $mod = 0; $mod |= $MODIFIER{$_} for @mod; |
|
|
124 | |
|
|
125 | $map->{K}[DC::popcount $mod]{$mod}{$sym} = $v; |
|
|
126 | } |
|
|
127 | } |
|
|
128 | |
|
|
129 | %DEFAULT_KEYMAP = (); |
|
|
130 | $map |
|
|
131 | }; |
|
|
132 | } |
44 | |
133 | |
45 | sub accelkey_to_string($) { |
134 | sub accelkey_to_string($) { |
46 | join "-", |
135 | join "-", |
47 | (grep $_[0][0] & $MODIFIER{$_}, |
136 | (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER), |
48 | keys %MODIFIER), |
|
|
49 | CFPlus::SDL_GetKeyName $_[0][1] |
137 | DC::SDL_GetKeyName $_[0][1] |
50 | } |
138 | } |
51 | |
139 | |
52 | sub trigger_to_string($) { |
140 | sub trigger_to_string($) { |
53 | my ($macro) = @_; |
141 | my ($macro) = @_; |
54 | |
142 | |
… | |
… | |
81 | $window->disconnect_all ("focus_out"); |
169 | $window->disconnect_all ("focus_out"); |
82 | $window->destroy; |
170 | $window->destroy; |
83 | &$end_cb; |
171 | &$end_cb; |
84 | }; |
172 | }; |
85 | |
173 | |
86 | $window = new CFPlus::UI::Toplevel |
174 | $window = new DC::UI::Toplevel |
87 | title => "Edit Macro Trigger", |
175 | title => "Edit Macro Trigger", |
88 | x => "center", |
176 | x => "center", |
89 | y => "center", |
177 | y => "center", |
90 | z => 1000, |
178 | z => 1000, |
91 | can_events => 1, |
179 | can_events => 1, |
… | |
… | |
99 | $done->(0); |
187 | $done->(0); |
100 | 1 |
188 | 1 |
101 | }, |
189 | }, |
102 | ; |
190 | ; |
103 | |
191 | |
104 | $window->add (my $vb = new CFPlus::UI::VBox); |
192 | $window->add (my $vb = new DC::UI::VBox); |
105 | |
193 | |
106 | $vb->add (new CFPlus::UI::Label |
194 | $vb->add (new DC::UI::Label |
107 | text => "To bind the macro to a key,\n" |
195 | text => "To bind the macro to a key,\n" |
108 | . "press a modifier (Ctrl, Alt\n" |
196 | . "press a modifier (Ctrl, Alt\n" |
109 | . "and/or Shift) and a key, or\n" |
197 | . "and/or Shift) and a key, or\n" |
110 | . "0-9 and F1-F15 without any modifier\n\n" |
198 | . "0-9 and F1-F15 without any modifier\n\n" |
111 | . "To cancel press Escape or close this.\n\n" |
199 | . "To cancel press Escape or close this.\n\n" |
112 | . "Accelerator key combo:", |
200 | . "Accelerator key combo:", |
113 | ellipsise => 0, |
201 | ellipsise => 0, |
114 | ); |
202 | ); |
115 | |
203 | |
116 | $vb->add (my $entry = new CFPlus::UI::Label |
204 | $vb->add (my $entry = new DC::UI::Label |
117 | fg => [0, 0, 0, 1], |
205 | fg => [0, 0, 0, 1], |
118 | bg => [1, 1, 0, 1], |
206 | bg => [1, 1, 0, 1], |
119 | ); |
207 | ); |
120 | |
208 | |
121 | my $key_cb = sub { |
209 | my $key_cb = sub { |
… | |
… | |
134 | map "$_-", |
222 | map "$_-", |
135 | grep $mod & $MODIFIER{$_}, |
223 | grep $mod & $MODIFIER{$_}, |
136 | keys %MODIFIER |
224 | keys %MODIFIER |
137 | ); |
225 | ); |
138 | |
226 | |
139 | return if $sym >= CFPlus::SDLK_MODIFIER_MIN |
227 | return if $sym >= DC::SDLK_MODIFIER_MIN |
140 | && $sym <= CFPlus::SDLK_MODIFIER_MAX; |
228 | && $sym <= DC::SDLK_MODIFIER_MAX; |
141 | |
229 | |
142 | if ($mod |
230 | if ($mod |
143 | || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS) |
231 | || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS) |
144 | || (grep $_ == $sym, @DIRECT_KEYS))) |
232 | || (grep $_ == $sym, @DIRECT_KEYS))) |
145 | { |
233 | { |
146 | $macro->{accelkey} = [$mod, $sym]; |
234 | $macro->{accelkey} = [$mod, $sym]; |
147 | $done->(1); |
235 | $done->(1); |
148 | } else { |
236 | } else { |
149 | $entry->set_text ("cannot bind " . (CFPlus::SDL_GetKeyName $sym) . " without modifier."); |
237 | $entry->set_text ("cannot bind " . (DC::SDL_GetKeyName $sym) . " without modifier."); |
150 | } |
238 | } |
151 | 1 |
239 | 1 |
152 | }; |
240 | }; |
153 | |
241 | |
154 | $window->connect (key_up => $key_cb); |
242 | $window->connect (key_up => $key_cb); |
… | |
… | |
156 | |
244 | |
157 | $window->grab_focus; |
245 | $window->grab_focus; |
158 | $window->show; |
246 | $window->show; |
159 | } |
247 | } |
160 | |
248 | |
|
|
249 | sub find_default($) { |
|
|
250 | my ($ev) = @_; |
|
|
251 | |
|
|
252 | for my $m (reverse grep $_, @{ $DEFAULT_KEYMAP->{K} }) { |
|
|
253 | for (keys %$m) { |
|
|
254 | if ($_ == ($ev->{mod} & $_)) { |
|
|
255 | if (defined (my $cmd = $m->{$_}{$ev->{sym}})) { |
|
|
256 | return $cmd; |
|
|
257 | } |
|
|
258 | } |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
|
|
262 | if (my $cmd = $DEFAULT_KEYMAP->{U}{$ev->{unicode}}) { |
|
|
263 | return $cmd; |
|
|
264 | } |
|
|
265 | |
|
|
266 | () |
|
|
267 | } |
|
|
268 | |
|
|
269 | # find macro by event |
|
|
270 | sub find($) { |
|
|
271 | my ($ev) = @_; |
|
|
272 | |
|
|
273 | # try user-defined macros |
|
|
274 | if (my @user = |
|
|
275 | grep { |
|
|
276 | if (my $key = $_->{accelkey}) { |
|
|
277 | $key->[1] == $ev->{sym} |
|
|
278 | && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) |
|
|
279 | } else { |
|
|
280 | 0 |
|
|
281 | } |
|
|
282 | } @{ $::PROFILE->{macro} || [] } |
|
|
283 | ) { |
|
|
284 | return @user; |
|
|
285 | } |
|
|
286 | |
|
|
287 | # now try default keymap |
|
|
288 | if (defined (my $def = find_default $ev)) { |
|
|
289 | return { |
|
|
290 | action => [$def], |
|
|
291 | }; |
|
|
292 | } |
|
|
293 | |
|
|
294 | () |
|
|
295 | } |
|
|
296 | |
|
|
297 | sub execute { |
|
|
298 | my ($macro) = @_; |
|
|
299 | |
|
|
300 | for (@{ $macro->{action} }) { |
|
|
301 | if (/^\!(\S+)\s?(.*)$/) { |
|
|
302 | $MACRO_FUNCTION{$1}->($2) |
|
|
303 | if exists $MACRO_FUNCTION{$1}; |
|
|
304 | } else { |
|
|
305 | $::CONN->send_command ($_) |
|
|
306 | if $::CONN; |
|
|
307 | } |
|
|
308 | } |
|
|
309 | } |
|
|
310 | |
161 | sub keyboard_setup { |
311 | sub keyboard_setup { |
162 | my $kbd_setup = new CFPlus::UI::VBox; |
312 | my $kbd_setup = new DC::UI::VBox; |
163 | |
313 | |
164 | $kbd_setup->add (my $list = new CFPlus::UI::VBox); |
314 | $kbd_setup->add (my $list = new DC::UI::VBox); |
165 | |
315 | |
166 | $list->add (new CFPlus::UI::FancyFrame |
316 | $list->add (new DC::UI::FancyFrame |
167 | label => "Options", |
317 | label => "Options", |
168 | child => (my $hb = new CFPlus::UI::HBox), |
318 | child => (my $hb = new DC::UI::HBox), |
169 | ); |
319 | ); |
170 | $hb->add (new CFPlus::UI::Label text => "only shift-up stops fire"); |
320 | $hb->add (new DC::UI::Label text => "only shift-up stops fire"); |
171 | $hb->add (new CFPlus::UI::CheckBox |
321 | $hb->add (new DC::UI::CheckBox |
172 | expand => 1, |
322 | expand => 1, |
173 | state => $::CFG->{shift_fire_stop}, |
323 | state => $::CFG->{shift_fire_stop}, |
174 | tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.", |
324 | tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.", |
175 | on_changed => sub { |
325 | on_changed => sub { |
176 | my ($cbox, $value) = @_; |
326 | my ($cbox, $value) = @_; |
177 | $::CFG->{shift_fire_stop} = $value; |
327 | $::CFG->{shift_fire_stop} = $value; |
178 | 0 |
328 | 0 |
179 | }, |
329 | }, |
180 | ); |
330 | ); |
181 | |
331 | |
182 | $list->add (new CFPlus::UI::FancyFrame |
332 | $list->add (new DC::UI::FancyFrame |
183 | label => "Bindings", |
333 | label => "Macros", |
184 | child => (my $macros = new CFPlus::UI::Table), |
334 | child => (my $macros = new DC::UI::VBox), |
185 | ); |
335 | ); |
186 | |
336 | |
187 | my $refresh; |
337 | my $refresh; |
188 | |
338 | |
189 | my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; |
339 | my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; |
… | |
… | |
192 | |
342 | |
193 | my $edit_macro = sub { |
343 | my $edit_macro = sub { |
194 | my ($macro) = @_; |
344 | my ($macro) = @_; |
195 | |
345 | |
196 | $kbd_setup->clear; |
346 | $kbd_setup->clear; |
197 | $kbd_setup->add (new CFPlus::UI::Button |
347 | $kbd_setup->add (new DC::UI::Button |
198 | text => "Return", |
348 | text => "Return", |
199 | tooltip => "Return to the macro list.", |
349 | tooltip => "Return to the macro list.", |
200 | on_activate => sub { |
350 | on_activate => sub { |
201 | $kbd_setup->clear; |
351 | $kbd_setup->clear; |
202 | $kbd_setup->add ($list); |
352 | $kbd_setup->add ($list); |
203 | $refresh->(); |
353 | $refresh->(); |
204 | 1 |
354 | 1 |
205 | }, |
355 | }, |
206 | ); |
356 | ); |
207 | $kbd_setup->add (new CFPlus::UI::FancyFrame |
357 | $kbd_setup->add (new DC::UI::FancyFrame |
208 | label => "Edit Macro", |
358 | label => "Edit Macro", |
209 | child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), |
359 | child => (my $editor = new DC::UI::Table col_expand => [0, 1]), |
210 | ); |
360 | ); |
211 | |
361 | |
212 | $editor->add (0, 1, new CFPlus::UI::Label |
362 | $editor->add_at (0, 1, new DC::UI::Label |
213 | text => "Trigger", |
363 | text => "Trigger", |
214 | tooltip => $tooltip_trigger, |
364 | tooltip => $tooltip_trigger, |
215 | can_hover => 1, |
365 | can_hover => 1, |
216 | can_events => 1, |
366 | can_events => 1, |
217 | ); |
367 | ); |
218 | $editor->add (0, 2, new CFPlus::UI::Label |
368 | $editor->add_at (0, 2, new DC::UI::Label |
219 | text => "Actions", |
369 | text => "Actions", |
220 | tooltip => $tooltip_commands, |
370 | tooltip => $tooltip_commands, |
221 | can_hover => 1, |
371 | can_hover => 1, |
222 | can_events => 1, |
372 | can_events => 1, |
223 | ); |
373 | ); |
224 | |
374 | |
225 | $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit |
375 | $editor->add_at (1, 2, my $textedit = new DC::UI::TextEdit |
226 | text => macro_to_text $macro, |
376 | text => macro_to_text $macro, |
227 | tooltip => $tooltip_commands, |
377 | tooltip => $tooltip_commands, |
228 | on_changed => sub { |
378 | on_changed => sub { |
229 | $macro->{action} = macro_from_text $macro, $_[1]; |
379 | $macro->{action} = macro_from_text $macro, $_[1]; |
230 | }, |
380 | }, |
231 | ); |
381 | ); |
232 | |
382 | |
233 | $editor->add (1, 1, my $accel = new CFPlus::UI::Button |
383 | $editor->add_at (1, 1, my $accel = new DC::UI::Button |
234 | text => trigger_to_string $macro, |
384 | text => trigger_to_string $macro, |
235 | tooltip => "To change the trigger for a macro, activate this button.", |
385 | tooltip => "To change the trigger for a macro, activate this button.", |
236 | on_activate => sub { |
386 | on_activate => sub { |
237 | my ($accel) = @_; |
387 | my ($accel) = @_; |
238 | trigger_edit $macro, sub { |
388 | trigger_edit $macro, sub { |
… | |
… | |
241 | 1 |
391 | 1 |
242 | }, |
392 | }, |
243 | ); |
393 | ); |
244 | |
394 | |
245 | my $recording; |
395 | my $recording; |
246 | $editor->add (1, 3, new CFPlus::UI::Button |
396 | $editor->add_at (1, 3, new DC::UI::Button |
247 | text => "Start Recording", |
397 | text => "Start Recording", |
248 | tooltip => "Start/Stop command recording: when recording, " |
398 | tooltip => "Start/Stop command recording: when recording, " |
249 | . "actions and commands you invoke are appended to this macro. " |
399 | . "actions and commands you invoke are appended to this macro. " |
250 | . "You can only record when you are logged in.", |
400 | . "You can only record when you are logged in.", |
251 | on_destroy => sub { |
401 | on_destroy => sub { |
… | |
… | |
255 | my ($widget) = @_; |
405 | my ($widget) = @_; |
256 | |
406 | |
257 | $recording = $::CONN && !$recording; |
407 | $recording = $::CONN && !$recording; |
258 | if ($recording) { |
408 | if ($recording) { |
259 | $widget->set_text ("Stop Recording"); |
409 | $widget->set_text ("Stop Recording"); |
260 | my $action = $macro->{action} ||= []; |
|
|
261 | $::CONN->record (sub { |
410 | $::CONN->record (sub { |
262 | push @$action, $_[0]; |
411 | push @{ $macro->{action} }, $_[0]; |
263 | $textedit->set_text (macro_to_text $macro); |
412 | $textedit->set_text (macro_to_text $macro); |
264 | }) if $::CONN; |
413 | }) if $::CONN; |
265 | } else { |
414 | } else { |
266 | $widget->set_text ("Start Recording"); |
415 | $widget->set_text ("Start Recording"); |
267 | $::CONN->record if $::CONN; |
416 | $::CONN->record if $::CONN; |
268 | } |
417 | } |
269 | }, |
418 | }, |
270 | ); |
419 | ); |
271 | }; |
420 | }; |
272 | |
421 | |
|
|
422 | $macros->add (new DC::UI::Button |
|
|
423 | text => "New Macro", |
|
|
424 | tooltip => "Creates a new, empty, macro you can edit.", |
|
|
425 | on_activate => sub { |
|
|
426 | my $macro = { }; |
|
|
427 | push @{ $::PROFILE->{macro} }, $macro; |
|
|
428 | $edit_macro->($macro); |
|
|
429 | }, |
|
|
430 | ); |
|
|
431 | |
|
|
432 | $macros->add (my $macrolist = new DC::UI::Table col_expand => [0, 1]); |
|
|
433 | |
273 | $REFRESH_MACRO_LIST = $refresh = sub { |
434 | $REFRESH_MACRO_LIST = $refresh = sub { |
274 | $macros->clear; |
435 | $macrolist->clear; |
275 | |
436 | |
276 | $macros->add (0, 0, new CFPlus::UI::Label |
437 | $macrolist->add_at (0, 1, new DC::UI::Label |
277 | text => "Trigger", |
438 | text => "Trigger", |
|
|
439 | tooltip => $tooltip_trigger . $tooltip_common, |
|
|
440 | ); |
|
|
441 | $macrolist->add_at (1, 1, new DC::UI::Label |
|
|
442 | text => "Actions", |
278 | align => 0, |
443 | align => 0, |
279 | tooltip => $tooltip_trigger . $tooltip_common, |
|
|
280 | ); |
|
|
281 | $macros->add (1, 0, new CFPlus::UI::Label |
|
|
282 | text => "Commands", |
|
|
283 | tooltip => $tooltip_commands . $tooltip_common, |
444 | tooltip => $tooltip_commands . $tooltip_common, |
284 | ); |
445 | ); |
285 | |
446 | |
286 | for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { |
447 | for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { |
287 | my $macro = $::PROFILE->{macro}[$idx]; |
448 | my $macro = $::PROFILE->{macro}[$idx]; |
288 | my $y = $idx + 1; |
449 | my $y = $idx + 2; |
289 | |
450 | |
290 | my $macro_cb = sub { |
451 | my $macro_cb = sub { |
291 | my ($widget, $ev) = @_; |
452 | my ($widget, $ev) = @_; |
292 | |
453 | |
293 | if ($ev->{button} == 1) { |
454 | if ($ev->{button} == 1) { |
294 | $edit_macro->($macro), |
455 | $edit_macro->($macro), |
295 | } elsif ($ev->{button} == 2) { |
456 | } elsif ($ev->{button} == 2) { |
296 | $::CONN->macro_send ($macro) if $::CONN; |
457 | $::CONN->macro_send ($macro) if $::CONN; |
297 | } elsif ($ev->{button} == 3) { |
458 | } elsif ($ev->{button} == 3) { |
298 | (new CFPlus::UI::Menu |
459 | (new DC::UI::Menu |
299 | items => [ |
460 | items => [ |
300 | ["Edit" => sub { $edit_macro->($macro) }], |
461 | ["Edit" => sub { $edit_macro->($macro) }], |
301 | ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }], |
462 | ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }], |
302 | ["Delete" => sub { |
463 | ["Delete" => sub { |
303 | # might want to use grep instead |
464 | # might want to use grep instead |
… | |
… | |
311 | } |
472 | } |
312 | |
473 | |
313 | 1 |
474 | 1 |
314 | }; |
475 | }; |
315 | |
476 | |
316 | $macros->add (0, $y, new CFPlus::UI::Label |
477 | $macrolist->add_at (0, $y, new DC::UI::Label |
317 | text => trigger_to_string $macro, |
478 | text => trigger_to_string $macro, |
318 | tooltip => $tooltip_trigger . $tooltip_common, |
479 | tooltip => $tooltip_trigger . $tooltip_common, |
319 | align => 0, |
480 | fg => [1, 0.8, 0.8], |
320 | can_hover => 1, |
481 | can_hover => 1, |
321 | can_events => 1, |
482 | can_events => 1, |
322 | on_button_down => $macro_cb, |
483 | on_button_down => $macro_cb, |
323 | ); |
484 | ); |
324 | |
485 | |
325 | $macros->add (1, $y, new CFPlus::UI::Label |
486 | $macrolist->add_at (1, $y, new DC::UI::Label |
326 | text => (join "; ", @{ $macro->{action} }), |
487 | text => (join "; ", @{ $macro->{action} || [] }), |
327 | tooltip => $tooltip_commands . $tooltip_common, |
488 | tooltip => $tooltip_commands . $tooltip_common, |
|
|
489 | fg => [0.9, 0.9, 0.9], |
|
|
490 | align => 0, |
328 | expand => 1, |
491 | expand => 1, |
329 | ellipsise => 3, |
492 | ellipsise => 3, |
330 | can_hover => 1, |
493 | can_hover => 1, |
331 | can_events => 1, |
494 | can_events => 1, |
332 | on_button_down => $macro_cb, |
495 | on_button_down => $macro_cb, |
… | |
… | |
340 | } |
503 | } |
341 | |
504 | |
342 | # this is a shortcut method that asks for a binding |
505 | # this is a shortcut method that asks for a binding |
343 | # and then just binds it. |
506 | # and then just binds it. |
344 | sub quick_macro { |
507 | sub quick_macro { |
345 | my ($self, $cmds, $end_cb) = @_; |
508 | my ($cmds, $end_cb) = @_; |
346 | |
509 | |
347 | my $macro = { |
510 | my $macro = { |
348 | action => $cmds, |
511 | action => $cmds, |
349 | }; |
512 | }; |
350 | |
513 | |
351 | trigger_edit $macro, sub { |
514 | trigger_edit $macro, sub { |
352 | |
|
|
353 | if ($_[0]) { |
515 | if ($_[0]) { |
354 | push @{ $::PROFILE->{macro} }, $macro; |
516 | push @{ $::PROFILE->{macro} }, $macro; |
355 | $REFRESH_MACRO_LIST->(); |
517 | $REFRESH_MACRO_LIST->(); |
356 | } |
518 | } |
357 | |
519 | |
358 | &$end_cb if $end_cb; |
520 | &$end_cb if $end_cb; |
359 | }; |
521 | }; |
360 | } |
522 | } |
361 | |
523 | |
|
|
524 | 1 |