ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.1
Committed: Sat Dec 9 02:21:25 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Log Message:
- slightly cleaned up and "outsourced" components
- completely rewrote keybind bindings
  - not functional yet
  - buggy a shell
  - hits perlbug

File Contents

# Content
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 sub keyboard_setup {
162 my $kbd_setup = new CFPlus::UI::VBox;
163
164 $kbd_setup->add (my $list = new CFPlus::UI::VBox);
165
166 $list->add (new CFPlus::UI::FancyFrame
167 label => "Options",
168 child => (my $hb = new CFPlus::UI::HBox),
169 );
170 $hb->add (new CFPlus::UI::Label text => "only shift-up stops fire");
171 $hb->add (new CFPlus::UI::CheckBox
172 expand => 1,
173 state => $::CFG->{shift_fire_stop},
174 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.",
175 on_changed => sub {
176 my ($cbox, $value) = @_;
177 $::CFG->{shift_fire_stop} = $value;
178 0
179 },
180 );
181
182 $list->add (new CFPlus::UI::FancyFrame
183 label => "Bindings",
184 child => (my $macros = new CFPlus::UI::Table),
185 );
186
187 my $refresh;
188
189 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
190 my $tooltip_trigger = "The event that triggers execution of this macro, usually a key combination.";
191 my $tooltip_commands = "The commands that comprise the macro.";
192
193 my $edit_macro = sub {
194 my ($macro) = @_;
195
196 $kbd_setup->clear;
197 $kbd_setup->add (new CFPlus::UI::Button
198 text => "Return",
199 tooltip => "Return to the macro list.",
200 on_activate => sub {
201 $kbd_setup->clear;
202 $kbd_setup->add ($list);
203 $refresh->();
204 1
205 },
206 );
207 $kbd_setup->add (new CFPlus::UI::FancyFrame
208 label => "Edit Macro",
209 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]),
210 );
211
212 $editor->add (0, 1, new CFPlus::UI::Label
213 text => "Trigger",
214 tooltip => $tooltip_trigger,
215 can_hover => 1,
216 can_events => 1,
217 );
218 $editor->add (0, 2, new CFPlus::UI::Label
219 text => "Actions",
220 tooltip => $tooltip_commands,
221 can_hover => 1,
222 can_events => 1,
223 );
224
225 $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit
226 text => macro_to_text $macro,
227 tooltip => $tooltip_commands,
228 on_changed => sub {
229 $macro->{action} = macro_from_text $macro, $_[1];
230 },
231 );
232
233 $editor->add (1, 1, my $accel = new CFPlus::UI::Button
234 text => trigger_to_string $macro,
235 tooltip => "To change the trigger for a macro, activate this button.",
236 on_activate => sub {
237 my ($accel) = @_;
238 trigger_edit $macro, sub {
239 $accel->set_text (trigger_to_string $macro);
240 };
241 1
242 },
243 );
244
245 my $recording;
246 $editor->add (1, 3, new CFPlus::UI::Button
247 text => "Start Recording",
248 tooltip => "Start/Stop command recording: when recording, "
249 . "actions and commands you invoke are appended to this macro. "
250 . "You can only record when you are logged in.",
251 on_destroy => sub {
252 $::CONN->record if $::CONN;
253 },
254 on_activate => sub {
255 my ($widget) = @_;
256
257 $recording = $::CONN && !$recording;
258 if ($recording) {
259 $widget->set_text ("Stop Recording");
260 my $action = $macro->{action} ||= [];
261 $::CONN->record (sub {
262 push @$action, $_[0];
263 $textedit->set_text (macro_to_text $macro);
264 }) if $::CONN;
265 } else {
266 $widget->set_text ("Start Recording");
267 $::CONN->record if $::CONN;
268 }
269 },
270 );
271 };
272
273 $REFRESH_MACRO_LIST = $refresh = sub {
274 $macros->clear;
275
276 $macros->add (0, 0, new CFPlus::UI::Label
277 text => "Trigger",
278 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,
284 );
285
286 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
287 my $macro = $::PROFILE->{macro}[$idx];
288 my $y = $idx + 1;
289
290 my $macro_cb = sub {
291 my ($widget, $ev) = @_;
292
293 if ($ev->{button} == 1) {
294 $edit_macro->($macro),
295 } elsif ($ev->{button} == 2) {
296 $::CONN->macro_send ($macro) if $::CONN;
297 } elsif ($ev->{button} == 3) {
298 (new CFPlus::UI::Menu
299 items => [
300 ["Edit" => sub { $edit_macro->($macro) }],
301 ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }],
302 ["Delete" => sub {
303 # might want to use grep instead
304 splice @{$::PROFILE->{macro}}, $idx, 1, ();
305 $refresh->();
306 }],
307 ],
308 )->popup ($ev);
309 } else {
310 return 0;
311 }
312
313 1
314 };
315
316 $macros->add (0, $y, new CFPlus::UI::Label
317 text => trigger_to_string $macro,
318 tooltip => $tooltip_trigger . $tooltip_common,
319 align => 0,
320 can_hover => 1,
321 can_events => 1,
322 on_button_down => $macro_cb,
323 );
324
325 $macros->add (1, $y, new CFPlus::UI::Label
326 text => (join "; ", @{ $macro->{action} }),
327 tooltip => $tooltip_commands . $tooltip_common,
328 expand => 1,
329 ellipsise => 3,
330 can_hover => 1,
331 can_events => 1,
332 on_button_down => $macro_cb,
333 );
334 }
335 };
336
337 $refresh->();
338
339 $kbd_setup
340 }
341
342 # this is a shortcut method that asks for a binding
343 # and then just binds it.
344 sub quick_macro {
345 my ($self, $cmds, $end_cb) = @_;
346
347 my $macro = {
348 action => $cmds,
349 };
350
351 trigger_edit $macro, sub {
352
353 if ($_[0]) {
354 push @{ $::PROFILE->{macro} }, $macro;
355 $REFRESH_MACRO_LIST->();
356 }
357
358 &$end_cb if $end_cb;
359 };
360 }
361