ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
Revision: 1.2
Committed: Sat Dec 9 21:44:43 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.1: +17 -0 lines
Log Message:
enable new macros

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