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.13 by elmex, Sun Sep 9 17:47:00 2007 UTC vs.
Revision 1.18 by root, Mon May 5 16:19:47 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines