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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines