ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Macro.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Macro.pm (file contents):
Revision 1.1 by root, Sat Dec 9 02:21:25 2006 UTC vs.
Revision 1.11 by root, Sun Sep 2 08:31:53 2007 UTC

1package CFPlus::Macro; 1package CFPlus::Macro;
2 2
3use strict; 3use strict;
4 4
5use List::Util ();
5use CFPlus::UI; 6use CFPlus::UI;
6 7
7our $REFRESH_MACRO_LIST; 8our $REFRESH_MACRO_LIST;
9
10our %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 "return" => "!activate-chat",
28 "." => "!repeat-command",
29
30 "," => "take",
31 "space" => "apply",
32 "enter" => "examine",
33 "[+]" => "rotateshoottype +",
34 "[-]" => "rotateshoottype -",
35 "LAlt-s" => "ready_skill find traps",
36 "LAlt-d" => "ready_skill disarm traps",
37 "LAlt-p" => "ready_skill praying",
38);
8 39
9# allowed modifiers 40# allowed modifiers
10our %MODIFIER = ( 41our %MODIFIER = (
11 "LShift" => CFPlus::KMOD_LSHIFT, 42 "LShift" => CFPlus::KMOD_LSHIFT,
12 "RShift" => CFPlus::KMOD_RSHIFT, 43 "RShift" => CFPlus::KMOD_RSHIFT,
44# "Shift" => CFPlus::KMOD_LSHIFT | CFPlus::KMOD_RSHIFT,
13 "LCtrl" => CFPlus::KMOD_LCTRL, 45 "LCtrl" => CFPlus::KMOD_LCTRL,
14 "RCtrl" => CFPlus::KMOD_RCTRL, 46 "RCtrl" => CFPlus::KMOD_RCTRL,
47# "Ctrl" => CFPlus::KMOD_LCTRL | CFPlus::KMOD_RCTRL,
15 "LAlt" => CFPlus::KMOD_LALT, 48 "LAlt" => CFPlus::KMOD_LALT,
16 "RAlt" => CFPlus::KMOD_RALT, 49 "RAlt" => CFPlus::KMOD_RALT,
50# "Alt" => CFPlus::KMOD_LALT | CFPlus::KMOD_RALT,
17 "LMeta" => CFPlus::KMOD_LMETA, 51 "LMeta" => CFPlus::KMOD_LMETA,
18 "RMeta" => CFPlus::KMOD_RMETA, 52 "RMeta" => CFPlus::KMOD_RMETA,
53# "Meta" => CFPlus::KMOD_LMETA | CFPlus::KMOD_RMETA,
19); 54);
20 55
21# allowed modifiers 56# allowed modifiers
22our $MODIFIER_MASK |= $_ for values %MODIFIER; 57our $MODIFIER_MASK |= $_ for values %MODIFIER;
23 58
40 CFPlus::SDLK_F13, 75 CFPlus::SDLK_F13,
41 CFPlus::SDLK_F14, 76 CFPlus::SDLK_F14,
42 CFPlus::SDLK_F15, 77 CFPlus::SDLK_F15,
43); 78);
44 79
80our %MACRO_FUNCTION = (
81 "toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility },
82 "toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility },
83 "toggle-help" => sub { $::HELP_WINDOW->toggle_visibility },
84 "toggle-stats" => sub { ::toggle_player_page ($::STATS_PAGE) },
85 "toggle-skills" => sub { ::toggle_player_page ($::SKILL_PAGE) },
86 "toggle-spells" => sub { ::toggle_player_page ($::SPELL_PAGE) },
87 "toggle-inventory" => sub { ::toggle_player_page ($::INVENTORY_PAGE) },
88 "toggle-pickup" => sub { ::toggle_player_page ($::PICKUP_PAGE) },
89 "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility },
90 "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility },
91 "switch-tab" => sub { $::MESSAGE_WINDOW->user_switch_to_page (0 + shift) },
92 "activate-chat" => sub { $::MESSAGE_WINDOW->activate_current },
93 "repeat-command" => sub {
94 $::CONN->user_send ($::COMPLETER->{last_command})
95 if $::CONN && exists $::COMPLETER->{last_command};
96 },
97 "completer" => sub {
98 if ($::CONN) {
99 $::COMPLETER->set_prefix (shift);
100 $::COMPLETER->show;
101 }
102 },
103);
104
105our $DEFAULT_KEYMAP;
106
107sub init {
108 $DEFAULT_KEYMAP ||= do {
109 my %sym = map +(CFPlus::SDL_GetKeyName $_, $_), CFPlus::SDLK_FIRST .. CFPlus::SDLK_LAST;
110 my $map;
111
112 while (my ($k, $v) = each %DEFAULT_KEYMAP) {
113 if ($k =~ /^\((.)\)$/) {
114 $map->{U}{ord $1} = $v;
115 } else {
116 my @mod = split /-/, $k;
117 my $sym = $sym{pop @mod}
118 or warn "unknown keysym $k\n";
119
120 my $mod = 0; $mod |= $MODIFIER{$_} for @mod;
121
122 $map->{K}[CFPlus::popcount $mod]{$mod}{$sym} = $v;
123 }
124 }
125
126 %DEFAULT_KEYMAP = ();
127 $map
128 };
129}
130
45sub accelkey_to_string($) { 131sub accelkey_to_string($) {
46 join "-", 132 join "-",
47 (grep $_[0][0] & $MODIFIER{$_}, 133 (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER),
48 keys %MODIFIER),
49 CFPlus::SDL_GetKeyName $_[0][1] 134 CFPlus::SDL_GetKeyName $_[0][1]
50} 135}
51 136
52sub trigger_to_string($) { 137sub trigger_to_string($) {
53 my ($macro) = @_; 138 my ($macro) = @_;
156 241
157 $window->grab_focus; 242 $window->grab_focus;
158 $window->show; 243 $window->show;
159} 244}
160 245
246sub find_default($) {
247 my ($ev) = @_;
248
249 if (my $cmd = $DEFAULT_KEYMAP->{U}{$ev->{unicode}}) {
250 return $cmd;
251 }
252
253 for my $m (reverse grep $_, @{ $DEFAULT_KEYMAP->{K} }) {
254 for (keys %$m) {
255 if ($_ == ($ev->{mod} & $_)) {
256 if (defined (my $cmd = $m->{$_}{$ev->{sym}})) {
257 return $cmd;
258 }
259 }
260 }
261 }
262
263 ()
264}
265
266# find macro by event
267sub find($) {
268 my ($ev) = @_;
269
270 # try user-defined macros
271 if (my @user =
272 grep {
273 if (my $key = $_->{accelkey}) {
274 $key->[1] == $ev->{sym}
275 && $key->[0] == ($ev->{mod} & $MODIFIER_MASK)
276 } else {
277 0
278 }
279 } @{ $::PROFILE->{macro} || [] }
280 ) {
281 return @user;
282 }
283
284 # now try default keymap
285 if (defined (my $def = find_default $ev)) {
286 return {
287 action => [$def],
288 };
289 }
290
291 ()
292}
293
294sub execute {
295 my ($macro) = @_;
296
297 for (@{ $macro->{action} }) {
298 if (/^\!(\S+)\s?(.*)$/) {
299 $MACRO_FUNCTION{$1}->($2)
300 if exists $MACRO_FUNCTION{$1};
301 } else {
302 $::CONN->send_command ($_)
303 if $::CONN;
304 }
305 }
306}
307
161sub keyboard_setup { 308sub keyboard_setup {
162 my $kbd_setup = new CFPlus::UI::VBox; 309 my $kbd_setup = new CFPlus::UI::VBox;
163 310
164 $kbd_setup->add (my $list = new CFPlus::UI::VBox); 311 $kbd_setup->add (my $list = new CFPlus::UI::VBox);
165 312
178 0 325 0
179 }, 326 },
180 ); 327 );
181 328
182 $list->add (new CFPlus::UI::FancyFrame 329 $list->add (new CFPlus::UI::FancyFrame
183 label => "Bindings", 330 label => "Macros",
184 child => (my $macros = new CFPlus::UI::Table), 331 child => (my $macros = new CFPlus::UI::VBox),
185 ); 332 );
186 333
187 my $refresh; 334 my $refresh;
188 335
189 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; 336 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
207 $kbd_setup->add (new CFPlus::UI::FancyFrame 354 $kbd_setup->add (new CFPlus::UI::FancyFrame
208 label => "Edit Macro", 355 label => "Edit Macro",
209 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), 356 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]),
210 ); 357 );
211 358
212 $editor->add (0, 1, new CFPlus::UI::Label 359 $editor->add_at (0, 1, new CFPlus::UI::Label
213 text => "Trigger", 360 text => "Trigger",
214 tooltip => $tooltip_trigger, 361 tooltip => $tooltip_trigger,
215 can_hover => 1, 362 can_hover => 1,
216 can_events => 1, 363 can_events => 1,
217 ); 364 );
218 $editor->add (0, 2, new CFPlus::UI::Label 365 $editor->add_at (0, 2, new CFPlus::UI::Label
219 text => "Actions", 366 text => "Actions",
220 tooltip => $tooltip_commands, 367 tooltip => $tooltip_commands,
221 can_hover => 1, 368 can_hover => 1,
222 can_events => 1, 369 can_events => 1,
223 ); 370 );
224 371
225 $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit 372 $editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit
226 text => macro_to_text $macro, 373 text => macro_to_text $macro,
227 tooltip => $tooltip_commands, 374 tooltip => $tooltip_commands,
228 on_changed => sub { 375 on_changed => sub {
229 $macro->{action} = macro_from_text $macro, $_[1]; 376 $macro->{action} = macro_from_text $macro, $_[1];
230 }, 377 },
231 ); 378 );
232 379
233 $editor->add (1, 1, my $accel = new CFPlus::UI::Button 380 $editor->add_at (1, 1, my $accel = new CFPlus::UI::Button
234 text => trigger_to_string $macro, 381 text => trigger_to_string $macro,
235 tooltip => "To change the trigger for a macro, activate this button.", 382 tooltip => "To change the trigger for a macro, activate this button.",
236 on_activate => sub { 383 on_activate => sub {
237 my ($accel) = @_; 384 my ($accel) = @_;
238 trigger_edit $macro, sub { 385 trigger_edit $macro, sub {
241 1 388 1
242 }, 389 },
243 ); 390 );
244 391
245 my $recording; 392 my $recording;
246 $editor->add (1, 3, new CFPlus::UI::Button 393 $editor->add_at (1, 3, new CFPlus::UI::Button
247 text => "Start Recording", 394 text => "Start Recording",
248 tooltip => "Start/Stop command recording: when recording, " 395 tooltip => "Start/Stop command recording: when recording, "
249 . "actions and commands you invoke are appended to this macro. " 396 . "actions and commands you invoke are appended to this macro. "
250 . "You can only record when you are logged in.", 397 . "You can only record when you are logged in.",
251 on_destroy => sub { 398 on_destroy => sub {
255 my ($widget) = @_; 402 my ($widget) = @_;
256 403
257 $recording = $::CONN && !$recording; 404 $recording = $::CONN && !$recording;
258 if ($recording) { 405 if ($recording) {
259 $widget->set_text ("Stop Recording"); 406 $widget->set_text ("Stop Recording");
260 my $action = $macro->{action} ||= [];
261 $::CONN->record (sub { 407 $::CONN->record (sub {
262 push @$action, $_[0]; 408 push @{ $macro->{action} }, $_[0];
263 $textedit->set_text (macro_to_text $macro); 409 $textedit->set_text (macro_to_text $macro);
264 }) if $::CONN; 410 }) if $::CONN;
265 } else { 411 } else {
266 $widget->set_text ("Start Recording"); 412 $widget->set_text ("Start Recording");
267 $::CONN->record if $::CONN; 413 $::CONN->record if $::CONN;
268 } 414 }
269 }, 415 },
270 ); 416 );
271 }; 417 };
272 418
419 $macros->add (new CFPlus::UI::Button
420 text => "New Macro",
421 tooltip => "Creates a new, empty, macro you can edit.",
422 on_activate => sub {
423 my $macro = { };
424 push @{ $::PROFILE->{macro} }, $macro;
425 $edit_macro->($macro);
426 },
427 );
428
429 $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]);
430
273 $REFRESH_MACRO_LIST = $refresh = sub { 431 $REFRESH_MACRO_LIST = $refresh = sub {
274 $macros->clear; 432 $macrolist->clear;
275 433
276 $macros->add (0, 0, new CFPlus::UI::Label 434 $macrolist->add_at (0, 1, new CFPlus::UI::Label
277 text => "Trigger", 435 text => "Trigger",
278 align => 0, 436 align => 0,
279 tooltip => $tooltip_trigger . $tooltip_common, 437 tooltip => $tooltip_trigger . $tooltip_common,
280 ); 438 );
281 $macros->add (1, 0, new CFPlus::UI::Label 439 $macrolist->add_at (1, 1, new CFPlus::UI::Label
282 text => "Commands", 440 text => "Commands",
283 tooltip => $tooltip_commands . $tooltip_common, 441 tooltip => $tooltip_commands . $tooltip_common,
284 ); 442 );
285 443
286 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { 444 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
287 my $macro = $::PROFILE->{macro}[$idx]; 445 my $macro = $::PROFILE->{macro}[$idx];
288 my $y = $idx + 1; 446 my $y = $idx + 2;
289 447
290 my $macro_cb = sub { 448 my $macro_cb = sub {
291 my ($widget, $ev) = @_; 449 my ($widget, $ev) = @_;
292 450
293 if ($ev->{button} == 1) { 451 if ($ev->{button} == 1) {
311 } 469 }
312 470
313 1 471 1
314 }; 472 };
315 473
316 $macros->add (0, $y, new CFPlus::UI::Label 474 $macrolist->add_at (0, $y, new CFPlus::UI::Label
317 text => trigger_to_string $macro, 475 text => trigger_to_string $macro,
318 tooltip => $tooltip_trigger . $tooltip_common, 476 tooltip => $tooltip_trigger . $tooltip_common,
319 align => 0, 477 align => 0,
320 can_hover => 1, 478 can_hover => 1,
321 can_events => 1, 479 can_events => 1,
322 on_button_down => $macro_cb, 480 on_button_down => $macro_cb,
323 ); 481 );
324 482
325 $macros->add (1, $y, new CFPlus::UI::Label 483 $macrolist->add_at (1, $y, new CFPlus::UI::Label
326 text => (join "; ", @{ $macro->{action} }), 484 text => (join "; ", @{ $macro->{action} || [] }),
327 tooltip => $tooltip_commands . $tooltip_common, 485 tooltip => $tooltip_commands . $tooltip_common,
328 expand => 1, 486 expand => 1,
329 ellipsise => 3, 487 ellipsise => 3,
330 can_hover => 1, 488 can_hover => 1,
331 can_events => 1, 489 can_events => 1,
340} 498}
341 499
342# this is a shortcut method that asks for a binding 500# this is a shortcut method that asks for a binding
343# and then just binds it. 501# and then just binds it.
344sub quick_macro { 502sub quick_macro {
345 my ($self, $cmds, $end_cb) = @_; 503 my ($cmds, $end_cb) = @_;
346 504
347 my $macro = { 505 my $macro = {
348 action => $cmds, 506 action => $cmds,
349 }; 507 };
350 508
351 trigger_edit $macro, sub { 509 trigger_edit $macro, sub {
352
353 if ($_[0]) { 510 if ($_[0]) {
354 push @{ $::PROFILE->{macro} }, $macro; 511 push @{ $::PROFILE->{macro} }, $macro;
355 $REFRESH_MACRO_LIST->(); 512 $REFRESH_MACRO_LIST->();
356 } 513 }
357 514
358 &$end_cb if $end_cb; 515 &$end_cb if $end_cb;
359 }; 516 };
360} 517}
361 518
5191

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines