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.15 by root, Wed Dec 26 20:46:39 2007 UTC vs.
Revision 1.19 by root, Sat Aug 30 08:04:01 2008 UTC

1package dc::Macro; 1package DC::Macro;
2 2
3use strict; 3use strict;
4 4
5use List::Util (); 5use List::Util ();
6use dc::UI; 6use DC::UI;
7 7
8our $REFRESH_MACRO_LIST; 8our $REFRESH_MACRO_LIST;
9 9
10our %DEFAULT_KEYMAP = ( 10our %DEFAULT_KEYMAP = (
11 (map +("($_)" => "!completer $_"), "a" .. "z"), 11 (map +("($_)" => "!completer $_"), "a" .. "z"),
12 "(!)" => "!completer shout ", 12 "(!)" => "!completer shout ",
13 "(\")" => "!completer say ", 13 "(\")" => "!completer say ",
14 "(')" => "!completer", 14 "(')" => "!completer",
15 15
16 "LShift-tab" => "!toggle-messagewindow", 16# "LShift-tab" => "!toggle-messagewindow",
17 "RShift-tab" => "!toggle-messagewindow", 17# "RShift-tab" => "!toggle-messagewindow",
18 "tab" => "!toggle-playerbook", 18 "tab" => "!toggle-playerbook",
19 "f1" => "!toggle-help", 19 "f1" => "!toggle-help",
20 "f2" => "!toggle-stats", 20 "f2" => "!toggle-stats",
21 "f3" => "!toggle-skills", 21 "f3" => "!toggle-skills",
22 "f4" => "!toggle-spells", 22 "f4" => "!toggle-spells",
23 "f5" => "!toggle-inventory", 23 "f5" => "!toggle-inventory",
24 "f9" => "!toggle-setup", 24 "f9" => "!toggle-setup",
25
25 (map +("LAlt-$_" => "!switch-tab $_"), 0..9), 26 (map +("LRAM-$_" => "!switch-tab $_"), 0..9),
26 (map +("RAlt-$_" => "!switch-tab $_"), 0..9),
27 "LAlt-x" => "!close-current-tab", 27 "LRAM-x" => "!close-current-tab",
28
28 "return" => "!activate-chat", 29 "return" => "!activate-chat",
29 "." => "!repeat-command", 30 "." => "!repeat-command",
30 31
31 "," => "take", 32 "," => "take",
32 "space" => "apply", 33 "space" => "apply",
33 "enter" => "examine", 34 "enter" => "examine",
34 "[+]" => "rotateshoottype +", 35 "[+]" => "rotateshoottype +",
35 "[-]" => "rotateshoottype -", 36 "[-]" => "rotateshoottype -",
37
36 "LAlt-e" => "examine", 38 "LRAM-e" => "examine",
37 "LAlt-s" => "ready_skill find traps", 39 "LRAM-s" => "ready_skill find traps",
38 "LAlt-d" => "ready_skill disarm traps", 40 "LRAM-d" => "ready_skill disarm traps",
39 "LAlt-p" => "ready_skill praying", 41 "LRAM-p" => "ready_skill praying",
40); 42);
41 43
42# allowed modifiers 44# allowed modifiers
43our %MODIFIER = ( 45our %MODIFIER = (
44 "LShift" => dc::KMOD_LSHIFT, 46 "LShift" => DC::KMOD_LSHIFT,
45 "RShift" => dc::KMOD_RSHIFT, 47 "RShift" => DC::KMOD_RSHIFT,
46# "Shift" => dc::KMOD_LSHIFT | dc::KMOD_RSHIFT, 48# "Shift" => DC::KMOD_LSHIFT | DC::KMOD_RSHIFT,
47 "LCtrl" => dc::KMOD_LCTRL, 49 "LCtrl" => DC::KMOD_LCTRL,
48 "RCtrl" => dc::KMOD_RCTRL, 50 "RCtrl" => DC::KMOD_RCTRL,
49# "Ctrl" => dc::KMOD_LCTRL | dc::KMOD_RCTRL, 51# "Ctrl" => DC::KMOD_LCTRL | DC::KMOD_RCTRL,
50 "LAlt" => dc::KMOD_LALT, 52 "LAlt" => DC::KMOD_LALT,
51 "RAlt" => dc::KMOD_RALT, 53 "RAlt" => DC::KMOD_RALT,
52# "Alt" => dc::KMOD_LALT | dc::KMOD_RALT, 54# "Alt" => DC::KMOD_LALT | DC::KMOD_RALT,
53 "LMeta" => dc::KMOD_LMETA, 55 "LMeta" => DC::KMOD_LMETA,
54 "RMeta" => dc::KMOD_RMETA, 56 "RMeta" => DC::KMOD_RMETA,
55# "Meta" => dc::KMOD_LMETA | dc::KMOD_RMETA, 57# "Meta" => DC::KMOD_LMETA | DC::KMOD_RMETA,
58 "LRAM" => DC::KMOD_LRAM, # left/right/alt/meta
56); 59);
57 60
58# allowed modifiers 61# allowed modifiers
59our $MODIFIER_MASK |= $_ for values %MODIFIER; 62our $MODIFIER_MASK |= $_ for values %MODIFIER;
60 63
61# can bind to these without any modifier 64# can bind to these without any modifier
62our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9); 65our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9);
63 66
64our @DIRECT_KEYS = ( 67our @DIRECT_KEYS = (
65 dc::SDLK_F1, 68 DC::SDLK_F1,
66 dc::SDLK_F2, 69 DC::SDLK_F2,
67 dc::SDLK_F3, 70 DC::SDLK_F3,
68 dc::SDLK_F4, 71 DC::SDLK_F4,
69 dc::SDLK_F5, 72 DC::SDLK_F5,
70 dc::SDLK_F6, 73 DC::SDLK_F6,
71 dc::SDLK_F7, 74 DC::SDLK_F7,
72 dc::SDLK_F8, 75 DC::SDLK_F8,
73 dc::SDLK_F9, 76 DC::SDLK_F9,
74 dc::SDLK_F10, 77 DC::SDLK_F10,
75 dc::SDLK_F11, 78 DC::SDLK_F11,
76 dc::SDLK_F12, 79 DC::SDLK_F12,
77 dc::SDLK_F13, 80 DC::SDLK_F13,
78 dc::SDLK_F14, 81 DC::SDLK_F14,
79 dc::SDLK_F15, 82 DC::SDLK_F15,
80); 83);
81 84
82our %MACRO_FUNCTION = ( 85our %MACRO_FUNCTION = (
83 "toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility }, 86 "toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility },
84 "toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility }, 87 "toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility },
107 110
108our $DEFAULT_KEYMAP; 111our $DEFAULT_KEYMAP;
109 112
110sub init { 113sub init {
111 $DEFAULT_KEYMAP ||= do { 114 $DEFAULT_KEYMAP ||= do {
112 my %sym = map +(dc::SDL_GetKeyName $_, $_), dc::SDLK_FIRST .. dc::SDLK_LAST; 115 my %sym = map +(DC::SDL_GetKeyName $_, $_), DC::SDLK_FIRST .. DC::SDLK_LAST;
113 my $map; 116 my $map;
114 117
115 while (my ($k, $v) = each %DEFAULT_KEYMAP) { 118 while (my ($k, $v) = each %DEFAULT_KEYMAP) {
116 if ($k =~ /^\((.)\)$/) { 119 if ($k =~ /^\((.)\)$/) {
117 $map->{U}{ord $1} = $v; 120 $map->{U}{ord $1} = $v;
120 my $sym = $sym{pop @mod} 123 my $sym = $sym{pop @mod}
121 or warn "unknown keysym $k\n"; 124 or warn "unknown keysym $k\n";
122 125
123 my $mod = 0; $mod |= $MODIFIER{$_} for @mod; 126 my $mod = 0; $mod |= $MODIFIER{$_} for @mod;
124 127
125 $map->{K}[dc::popcount $mod]{$mod}{$sym} = $v; 128 $map->{K}[DC::popcount $mod]{$mod}{$sym} = $v;
126 } 129 }
127 } 130 }
128 131
129 %DEFAULT_KEYMAP = (); 132 %DEFAULT_KEYMAP = ();
130 $map 133 $map
132} 135}
133 136
134sub accelkey_to_string($) { 137sub accelkey_to_string($) {
135 join "-", 138 join "-",
136 (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER), 139 (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER),
137 dc::SDL_GetKeyName $_[0][1] 140 DC::SDL_GetKeyName $_[0][1]
138} 141}
139 142
140sub trigger_to_string($) { 143sub trigger_to_string($) {
141 my ($macro) = @_; 144 my ($macro) = @_;
142 145
169 $window->disconnect_all ("focus_out"); 172 $window->disconnect_all ("focus_out");
170 $window->destroy; 173 $window->destroy;
171 &$end_cb; 174 &$end_cb;
172 }; 175 };
173 176
174 $window = new dc::UI::Toplevel 177 $window = new DC::UI::Toplevel
175 title => "Edit Macro Trigger", 178 title => "Edit Macro Trigger",
176 x => "center", 179 x => "center",
177 y => "center", 180 y => "center",
178 z => 1000, 181 z => 1000,
179 can_events => 1, 182 can_events => 1,
187 $done->(0); 190 $done->(0);
188 1 191 1
189 }, 192 },
190 ; 193 ;
191 194
192 $window->add (my $vb = new dc::UI::VBox); 195 $window->add (my $vb = new DC::UI::VBox);
193 196
194 $vb->add (new dc::UI::Label 197 $vb->add (new DC::UI::Label
195 text => "To bind the macro to a key,\n" 198 text => "To bind the macro to a key,\n"
196 . "press a modifier (Ctrl, Alt\n" 199 . "press a modifier (Ctrl, Alt\n"
197 . "and/or Shift) and a key, or\n" 200 . "and/or Shift) and a key, or\n"
198 . "0-9 and F1-F15 without any modifier\n\n" 201 . "0-9 and F1-F15 without any modifier\n\n"
199 . "To cancel press Escape or close this.\n\n" 202 . "To cancel press Escape or close this.\n\n"
200 . "Accelerator key combo:", 203 . "Accelerator key combo:",
201 ellipsise => 0, 204 ellipsise => 0,
202 ); 205 );
203 206
204 $vb->add (my $entry = new dc::UI::Label 207 $vb->add (my $entry = new DC::UI::Label
205 fg => [0, 0, 0, 1], 208 fg => [0, 0, 0, 1],
206 bg => [1, 1, 0, 1], 209 bg => [1, 1, 0, 1],
207 ); 210 );
208 211
209 my $key_cb = sub { 212 my $key_cb = sub {
222 map "$_-", 225 map "$_-",
223 grep $mod & $MODIFIER{$_}, 226 grep $mod & $MODIFIER{$_},
224 keys %MODIFIER 227 keys %MODIFIER
225 ); 228 );
226 229
227 return if $sym >= dc::SDLK_MODIFIER_MIN 230 return if $sym >= DC::SDLK_MODIFIER_MIN
228 && $sym <= dc::SDLK_MODIFIER_MAX; 231 && $sym <= DC::SDLK_MODIFIER_MAX;
229 232
230 if ($mod 233 if ($mod
231 || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS) 234 || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS)
232 || (grep $_ == $sym, @DIRECT_KEYS))) 235 || (grep $_ == $sym, @DIRECT_KEYS)))
233 { 236 {
234 $macro->{accelkey} = [$mod, $sym]; 237 $macro->{accelkey} = [$mod, $sym];
235 $done->(1); 238 $done->(1);
236 } else { 239 } else {
237 $entry->set_text ("cannot bind " . (dc::SDL_GetKeyName $sym) . " without modifier."); 240 $entry->set_text ("cannot bind " . (DC::SDL_GetKeyName $sym) . " without modifier.");
238 } 241 }
239 1 242 1
240 }; 243 };
241 244
242 $window->connect (key_up => $key_cb); 245 $window->connect (key_up => $key_cb);
307 } 310 }
308 } 311 }
309} 312}
310 313
311sub keyboard_setup { 314sub keyboard_setup {
312 my $kbd_setup = new dc::UI::VBox; 315 my $kbd_setup = new DC::UI::VBox;
313 316
314 $kbd_setup->add (my $list = new dc::UI::VBox); 317 $kbd_setup->add (my $list = new DC::UI::VBox);
315 318
316 $list->add (new dc::UI::FancyFrame 319 $list->add (new DC::UI::FancyFrame
317 label => "Options", 320 label => "Options",
318 child => (my $hb = new dc::UI::HBox), 321 child => (my $hb = new DC::UI::HBox),
319 ); 322 );
320 $hb->add (new dc::UI::Label text => "only shift-up stops fire"); 323 $hb->add (new DC::UI::Label text => "only shift-up stops fire");
321 $hb->add (new dc::UI::CheckBox 324 $hb->add (new DC::UI::CheckBox
322 expand => 1, 325 expand => 1,
323 state => $::CFG->{shift_fire_stop}, 326 state => $::CFG->{shift_fire_stop},
324 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.", 327 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.",
325 on_changed => sub { 328 on_changed => sub {
326 my ($cbox, $value) = @_; 329 my ($cbox, $value) = @_;
327 $::CFG->{shift_fire_stop} = $value; 330 $::CFG->{shift_fire_stop} = $value;
328 0 331 0
329 }, 332 },
330 ); 333 );
331 334
332 $list->add (new dc::UI::FancyFrame 335 $list->add (new DC::UI::FancyFrame
333 label => "Macros", 336 label => "Macros",
334 child => (my $macros = new dc::UI::VBox), 337 child => (my $macros = new DC::UI::VBox),
335 ); 338 );
336 339
337 my $refresh; 340 my $refresh;
338 341
339 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; 342 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
342 345
343 my $edit_macro = sub { 346 my $edit_macro = sub {
344 my ($macro) = @_; 347 my ($macro) = @_;
345 348
346 $kbd_setup->clear; 349 $kbd_setup->clear;
347 $kbd_setup->add (new dc::UI::Button 350 $kbd_setup->add (new DC::UI::Button
348 text => "Return", 351 text => "Return",
349 tooltip => "Return to the macro list.", 352 tooltip => "Return to the macro list.",
350 on_activate => sub { 353 on_activate => sub {
351 $kbd_setup->clear; 354 $kbd_setup->clear;
352 $kbd_setup->add ($list); 355 $kbd_setup->add ($list);
353 $refresh->(); 356 $refresh->();
354 1 357 1
355 }, 358 },
356 ); 359 );
357 $kbd_setup->add (new dc::UI::FancyFrame 360 $kbd_setup->add (new DC::UI::FancyFrame
358 label => "Edit Macro", 361 label => "Edit Macro",
359 child => (my $editor = new dc::UI::Table col_expand => [0, 1]), 362 child => (my $editor = new DC::UI::Table col_expand => [0, 1]),
360 ); 363 );
361 364
362 $editor->add_at (0, 1, new dc::UI::Label 365 $editor->add_at (0, 1, new DC::UI::Label
363 text => "Trigger", 366 text => "Trigger",
364 tooltip => $tooltip_trigger, 367 tooltip => $tooltip_trigger,
365 can_hover => 1, 368 can_hover => 1,
366 can_events => 1, 369 can_events => 1,
367 ); 370 );
368 $editor->add_at (0, 2, new dc::UI::Label 371 $editor->add_at (0, 2, new DC::UI::Label
369 text => "Actions", 372 text => "Actions",
370 tooltip => $tooltip_commands, 373 tooltip => $tooltip_commands,
371 can_hover => 1, 374 can_hover => 1,
372 can_events => 1, 375 can_events => 1,
373 ); 376 );
374 377
375 $editor->add_at (1, 2, my $textedit = new dc::UI::TextEdit 378 $editor->add_at (1, 2, my $textedit = new DC::UI::TextEdit
376 text => macro_to_text $macro, 379 text => macro_to_text $macro,
377 tooltip => $tooltip_commands, 380 tooltip => $tooltip_commands,
378 on_changed => sub { 381 on_changed => sub {
379 $macro->{action} = macro_from_text $macro, $_[1]; 382 $macro->{action} = macro_from_text $macro, $_[1];
380 }, 383 },
381 ); 384 );
382 385
383 $editor->add_at (1, 1, my $accel = new dc::UI::Button 386 $editor->add_at (1, 1, my $accel = new DC::UI::Button
384 text => trigger_to_string $macro, 387 text => trigger_to_string $macro,
385 tooltip => "To change the trigger for a macro, activate this button.", 388 tooltip => "To change the trigger for a macro, activate this button.",
386 on_activate => sub { 389 on_activate => sub {
387 my ($accel) = @_; 390 my ($accel) = @_;
388 trigger_edit $macro, sub { 391 trigger_edit $macro, sub {
391 1 394 1
392 }, 395 },
393 ); 396 );
394 397
395 my $recording; 398 my $recording;
396 $editor->add_at (1, 3, new dc::UI::Button 399 $editor->add_at (1, 3, new DC::UI::Button
397 text => "Start Recording", 400 text => "Start Recording",
398 tooltip => "Start/Stop command recording: when recording, " 401 tooltip => "Start/Stop command recording: when recording, "
399 . "actions and commands you invoke are appended to this macro. " 402 . "actions and commands you invoke are appended to this macro. "
400 . "You can only record when you are logged in.", 403 . "You can only record when you are logged in.",
401 on_destroy => sub { 404 on_destroy => sub {
417 } 420 }
418 }, 421 },
419 ); 422 );
420 }; 423 };
421 424
422 $macros->add (new dc::UI::Button 425 $macros->add (new DC::UI::Button
423 text => "New Macro", 426 text => "New Macro",
424 tooltip => "Creates a new, empty, macro you can edit.", 427 tooltip => "Creates a new, empty, macro you can edit.",
425 on_activate => sub { 428 on_activate => sub {
426 my $macro = { }; 429 my $macro = { };
427 push @{ $::PROFILE->{macro} }, $macro; 430 push @{ $::PROFILE->{macro} }, $macro;
428 $edit_macro->($macro); 431 $edit_macro->($macro);
429 }, 432 },
430 ); 433 );
431 434
432 $macros->add (my $macrolist = new dc::UI::Table col_expand => [0, 1]); 435 $macros->add (my $macrolist = new DC::UI::Table col_expand => [0, 1]);
433 436
434 $REFRESH_MACRO_LIST = $refresh = sub { 437 $REFRESH_MACRO_LIST = $refresh = sub {
435 $macrolist->clear; 438 $macrolist->clear;
436 439
437 $macrolist->add_at (0, 1, new dc::UI::Label 440 $macrolist->add_at (0, 1, new DC::UI::Label
438 text => "Trigger", 441 text => "Trigger",
442 tooltip => $tooltip_trigger . $tooltip_common,
443 );
444 $macrolist->add_at (1, 1, new DC::UI::Label
445 text => "Actions",
439 align => 0, 446 align => 0,
440 tooltip => $tooltip_trigger . $tooltip_common,
441 );
442 $macrolist->add_at (1, 1, new dc::UI::Label
443 text => "Commands",
444 tooltip => $tooltip_commands . $tooltip_common, 447 tooltip => $tooltip_commands . $tooltip_common,
445 ); 448 );
446 449
447 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { 450 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
448 my $macro = $::PROFILE->{macro}[$idx]; 451 my $macro = $::PROFILE->{macro}[$idx];
454 if ($ev->{button} == 1) { 457 if ($ev->{button} == 1) {
455 $edit_macro->($macro), 458 $edit_macro->($macro),
456 } elsif ($ev->{button} == 2) { 459 } elsif ($ev->{button} == 2) {
457 $::CONN->macro_send ($macro) if $::CONN; 460 $::CONN->macro_send ($macro) if $::CONN;
458 } elsif ($ev->{button} == 3) { 461 } elsif ($ev->{button} == 3) {
459 (new dc::UI::Menu 462 (new DC::UI::Menu
460 items => [ 463 items => [
461 ["Edit" => sub { $edit_macro->($macro) }], 464 ["Edit" => sub { $edit_macro->($macro) }],
462 ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }], 465 ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }],
463 ["Delete" => sub { 466 ["Delete" => sub {
464 # might want to use grep instead 467 # might want to use grep instead
472 } 475 }
473 476
474 1 477 1
475 }; 478 };
476 479
477 $macrolist->add_at (0, $y, new dc::UI::Label 480 $macrolist->add_at (0, $y, new DC::UI::Label
478 text => trigger_to_string $macro, 481 text => trigger_to_string $macro,
479 tooltip => $tooltip_trigger . $tooltip_common, 482 tooltip => $tooltip_trigger . $tooltip_common,
480 align => 0, 483 fg => [1, 0.8, 0.8],
481 can_hover => 1, 484 can_hover => 1,
482 can_events => 1, 485 can_events => 1,
483 on_button_down => $macro_cb, 486 on_button_down => $macro_cb,
484 ); 487 );
485 488
486 $macrolist->add_at (1, $y, new dc::UI::Label 489 $macrolist->add_at (1, $y, new DC::UI::Label
487 text => (join "; ", @{ $macro->{action} || [] }), 490 text => (join "; ", @{ $macro->{action} || [] }),
488 tooltip => $tooltip_commands . $tooltip_common, 491 tooltip => $tooltip_commands . $tooltip_common,
492 fg => [0.9, 0.9, 0.9],
493 align => 0,
489 expand => 1, 494 expand => 1,
490 ellipsise => 3, 495 ellipsise => 3,
491 can_hover => 1, 496 can_hover => 1,
492 can_events => 1, 497 can_events => 1,
493 on_button_down => $macro_cb, 498 on_button_down => $macro_cb,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines