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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines