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.6 by elmex, Sat Dec 16 16:14:34 2006 UTC vs.
Revision 1.13 by elmex, Sun Sep 9 17:47:00 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 "LAlt-x" => "!close-current-tab",
28 "return" => "!activate-chat",
29 "." => "!repeat-command",
30
31 "," => "take",
32 "space" => "apply",
33 "enter" => "examine",
34 "[+]" => "rotateshoottype +",
35 "[-]" => "rotateshoottype -",
36 "LAlt-s" => "ready_skill find traps",
37 "LAlt-d" => "ready_skill disarm traps",
38 "LAlt-p" => "ready_skill praying",
39);
8 40
9# allowed modifiers 41# allowed modifiers
10our %MODIFIER = ( 42our %MODIFIER = (
11 "LShift" => CFPlus::KMOD_LSHIFT, 43 "LShift" => CFPlus::KMOD_LSHIFT,
12 "RShift" => CFPlus::KMOD_RSHIFT, 44 "RShift" => CFPlus::KMOD_RSHIFT,
45# "Shift" => CFPlus::KMOD_LSHIFT | CFPlus::KMOD_RSHIFT,
13 "LCtrl" => CFPlus::KMOD_LCTRL, 46 "LCtrl" => CFPlus::KMOD_LCTRL,
14 "RCtrl" => CFPlus::KMOD_RCTRL, 47 "RCtrl" => CFPlus::KMOD_RCTRL,
48# "Ctrl" => CFPlus::KMOD_LCTRL | CFPlus::KMOD_RCTRL,
15 "LAlt" => CFPlus::KMOD_LALT, 49 "LAlt" => CFPlus::KMOD_LALT,
16 "RAlt" => CFPlus::KMOD_RALT, 50 "RAlt" => CFPlus::KMOD_RALT,
51# "Alt" => CFPlus::KMOD_LALT | CFPlus::KMOD_RALT,
17 "LMeta" => CFPlus::KMOD_LMETA, 52 "LMeta" => CFPlus::KMOD_LMETA,
18 "RMeta" => CFPlus::KMOD_RMETA, 53 "RMeta" => CFPlus::KMOD_RMETA,
54# "Meta" => CFPlus::KMOD_LMETA | CFPlus::KMOD_RMETA,
19); 55);
20 56
21# allowed modifiers 57# allowed modifiers
22our $MODIFIER_MASK |= $_ for values %MODIFIER; 58our $MODIFIER_MASK |= $_ for values %MODIFIER;
23 59
40 CFPlus::SDLK_F13, 76 CFPlus::SDLK_F13,
41 CFPlus::SDLK_F14, 77 CFPlus::SDLK_F14,
42 CFPlus::SDLK_F15, 78 CFPlus::SDLK_F15,
43); 79);
44 80
81our %MACRO_FUNCTION = (
82 "toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility },
83 "toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility },
84 "toggle-help" => sub { $::HELP_WINDOW->toggle_visibility },
85 "toggle-stats" => sub { ::toggle_player_page ($::STATS_PAGE) },
86 "toggle-skills" => sub { ::toggle_player_page ($::SKILL_PAGE) },
87 "toggle-spells" => sub { ::toggle_player_page ($::SPELL_PAGE) },
88 "toggle-inventory" => sub { ::toggle_player_page ($::INVENTORY_PAGE) },
89 "toggle-pickup" => sub { ::toggle_player_page ($::PICKUP_PAGE) },
90 "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility },
91 "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility },
92 "switch-tab" => sub { $::MESSAGE_WINDOW->user_switch_to_page (0 + shift) },
93 "close-current-tab" => sub { $::MESSAGE_WINDOW->close_current_tab },
94 "activate-chat" => sub { $::MESSAGE_WINDOW->activate_current },
95 "repeat-command" => sub {
96 $::CONN->user_send ($::COMPLETER->{last_command})
97 if $::CONN && exists $::COMPLETER->{last_command};
98 },
99 "completer" => sub {
100 if ($::CONN) {
101 $::COMPLETER->set_prefix (shift);
102 $::COMPLETER->show;
103 }
104 },
105);
106
107our $DEFAULT_KEYMAP;
108
109sub init {
110 $DEFAULT_KEYMAP ||= do {
111 my %sym = map +(CFPlus::SDL_GetKeyName $_, $_), CFPlus::SDLK_FIRST .. CFPlus::SDLK_LAST;
112 my $map;
113
114 while (my ($k, $v) = each %DEFAULT_KEYMAP) {
115 if ($k =~ /^\((.)\)$/) {
116 $map->{U}{ord $1} = $v;
117 } else {
118 my @mod = split /-/, $k;
119 my $sym = $sym{pop @mod}
120 or warn "unknown keysym $k\n";
121
122 my $mod = 0; $mod |= $MODIFIER{$_} for @mod;
123
124 $map->{K}[CFPlus::popcount $mod]{$mod}{$sym} = $v;
125 }
126 }
127
128 %DEFAULT_KEYMAP = ();
129 $map
130 };
131}
132
45sub accelkey_to_string($) { 133sub accelkey_to_string($) {
46 join "-", 134 join "-",
47 (grep $_[0][0] & $MODIFIER{$_}, 135 (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER),
48 keys %MODIFIER),
49 CFPlus::SDL_GetKeyName $_[0][1] 136 CFPlus::SDL_GetKeyName $_[0][1]
50} 137}
51 138
52sub trigger_to_string($) { 139sub trigger_to_string($) {
53 my ($macro) = @_; 140 my ($macro) = @_;
156 243
157 $window->grab_focus; 244 $window->grab_focus;
158 $window->show; 245 $window->show;
159} 246}
160 247
248sub find_default($) {
249 my ($ev) = @_;
250
251 for my $m (reverse grep $_, @{ $DEFAULT_KEYMAP->{K} }) {
252 for (keys %$m) {
253 if ($_ == ($ev->{mod} & $_)) {
254 if (defined (my $cmd = $m->{$_}{$ev->{sym}})) {
255 return $cmd;
256 }
257 }
258 }
259 }
260
261 if (my $cmd = $DEFAULT_KEYMAP->{U}{$ev->{unicode}}) {
262 return $cmd;
263 }
264
265 ()
266}
267
161# find macro by event 268# find macro by event
162sub match_event($) { 269sub find($) {
163 my ($ev) = @_; 270 my ($ev) = @_;
164 271
272 # try user-defined macros
273 if (my @user =
165 grep { 274 grep {
166 if (my $key = $_->{accelkey}) { 275 if (my $key = $_->{accelkey}) {
167 $key->[1] == $ev->{sym} 276 $key->[1] == $ev->{sym}
168 && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) 277 && $key->[0] == ($ev->{mod} & $MODIFIER_MASK)
278 } else {
279 0
280 }
281 } @{ $::PROFILE->{macro} || [] }
282 ) {
283 return @user;
284 }
285
286 # now try default keymap
287 if (defined (my $def = find_default $ev)) {
288 return {
289 action => [$def],
290 };
291 }
292
293 ()
294}
295
296sub execute {
297 my ($macro) = @_;
298
299 for (@{ $macro->{action} }) {
300 if (/^\!(\S+)\s?(.*)$/) {
301 $MACRO_FUNCTION{$1}->($2)
302 if exists $MACRO_FUNCTION{$1};
169 } else { 303 } else {
170 0 304 $::CONN->send_command ($_)
305 if $::CONN;
171 } 306 }
172 } @{ $::PROFILE->{macro} || [] } 307 }
173} 308}
174 309
175sub keyboard_setup { 310sub keyboard_setup {
176 my $kbd_setup = new CFPlus::UI::VBox; 311 my $kbd_setup = new CFPlus::UI::VBox;
177 312
221 $kbd_setup->add (new CFPlus::UI::FancyFrame 356 $kbd_setup->add (new CFPlus::UI::FancyFrame
222 label => "Edit Macro", 357 label => "Edit Macro",
223 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), 358 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]),
224 ); 359 );
225 360
226 $editor->add (0, 1, new CFPlus::UI::Label 361 $editor->add_at (0, 1, new CFPlus::UI::Label
227 text => "Trigger", 362 text => "Trigger",
228 tooltip => $tooltip_trigger, 363 tooltip => $tooltip_trigger,
229 can_hover => 1, 364 can_hover => 1,
230 can_events => 1, 365 can_events => 1,
231 ); 366 );
232 $editor->add (0, 2, new CFPlus::UI::Label 367 $editor->add_at (0, 2, new CFPlus::UI::Label
233 text => "Actions", 368 text => "Actions",
234 tooltip => $tooltip_commands, 369 tooltip => $tooltip_commands,
235 can_hover => 1, 370 can_hover => 1,
236 can_events => 1, 371 can_events => 1,
237 ); 372 );
238 373
239 $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit 374 $editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit
240 text => macro_to_text $macro, 375 text => macro_to_text $macro,
241 tooltip => $tooltip_commands, 376 tooltip => $tooltip_commands,
242 on_changed => sub { 377 on_changed => sub {
243 $macro->{action} = macro_from_text $macro, $_[1]; 378 $macro->{action} = macro_from_text $macro, $_[1];
244 }, 379 },
245 ); 380 );
246 381
247 $editor->add (1, 1, my $accel = new CFPlus::UI::Button 382 $editor->add_at (1, 1, my $accel = new CFPlus::UI::Button
248 text => trigger_to_string $macro, 383 text => trigger_to_string $macro,
249 tooltip => "To change the trigger for a macro, activate this button.", 384 tooltip => "To change the trigger for a macro, activate this button.",
250 on_activate => sub { 385 on_activate => sub {
251 my ($accel) = @_; 386 my ($accel) = @_;
252 trigger_edit $macro, sub { 387 trigger_edit $macro, sub {
255 1 390 1
256 }, 391 },
257 ); 392 );
258 393
259 my $recording; 394 my $recording;
260 $editor->add (1, 3, new CFPlus::UI::Button 395 $editor->add_at (1, 3, new CFPlus::UI::Button
261 text => "Start Recording", 396 text => "Start Recording",
262 tooltip => "Start/Stop command recording: when recording, " 397 tooltip => "Start/Stop command recording: when recording, "
263 . "actions and commands you invoke are appended to this macro. " 398 . "actions and commands you invoke are appended to this macro. "
264 . "You can only record when you are logged in.", 399 . "You can only record when you are logged in.",
265 on_destroy => sub { 400 on_destroy => sub {
296 $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]); 431 $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]);
297 432
298 $REFRESH_MACRO_LIST = $refresh = sub { 433 $REFRESH_MACRO_LIST = $refresh = sub {
299 $macrolist->clear; 434 $macrolist->clear;
300 435
301 $macrolist->add (0, 1, new CFPlus::UI::Label 436 $macrolist->add_at (0, 1, new CFPlus::UI::Label
302 text => "Trigger", 437 text => "Trigger",
303 align => 0, 438 align => 0,
304 tooltip => $tooltip_trigger . $tooltip_common, 439 tooltip => $tooltip_trigger . $tooltip_common,
305 ); 440 );
306 $macrolist->add (1, 1, new CFPlus::UI::Label 441 $macrolist->add_at (1, 1, new CFPlus::UI::Label
307 text => "Commands", 442 text => "Commands",
308 tooltip => $tooltip_commands . $tooltip_common, 443 tooltip => $tooltip_commands . $tooltip_common,
309 ); 444 );
310 445
311 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { 446 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
336 } 471 }
337 472
338 1 473 1
339 }; 474 };
340 475
341 $macrolist->add (0, $y, new CFPlus::UI::Label 476 $macrolist->add_at (0, $y, new CFPlus::UI::Label
342 text => trigger_to_string $macro, 477 text => trigger_to_string $macro,
343 tooltip => $tooltip_trigger . $tooltip_common, 478 tooltip => $tooltip_trigger . $tooltip_common,
344 align => 0, 479 align => 0,
345 can_hover => 1, 480 can_hover => 1,
346 can_events => 1, 481 can_events => 1,
347 on_button_down => $macro_cb, 482 on_button_down => $macro_cb,
348 ); 483 );
349 484
350 $macrolist->add (1, $y, new CFPlus::UI::Label 485 $macrolist->add_at (1, $y, new CFPlus::UI::Label
351 text => (join "; ", @{ $macro->{action} || [] }), 486 text => (join "; ", @{ $macro->{action} || [] }),
352 tooltip => $tooltip_commands . $tooltip_common, 487 tooltip => $tooltip_commands . $tooltip_common,
353 expand => 1, 488 expand => 1,
354 ellipsise => 3, 489 ellipsise => 3,
355 can_hover => 1, 490 can_hover => 1,
381 516
382 &$end_cb if $end_cb; 517 &$end_cb if $end_cb;
383 }; 518 };
384} 519}
385 520
5211

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines