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.3 by root, Sat Dec 9 22:06:51 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
162# maybe return multiple results? 269sub find($) {
163sub match_event($) {
164 my ($ev) = @_; 270 my ($ev) = @_;
165 271
166 for my $macro (@{ $::PROFILE->{macro} || [] }) { 272 # try user-defined macros
273 if (my @user =
274 grep {
167 my $key = $macro->{accelkey} 275 if (my $key = $_->{accelkey}) {
168 or next;
169
170 $key->[1] == $ev->{sym} 276 $key->[1] == $ev->{sym}
171 && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) 277 && $key->[0] == ($ev->{mod} & $MODIFIER_MASK)
172 && return $macro; 278 } else {
279 0
280 }
281 } @{ $::PROFILE->{macro} || [] }
282 ) {
283 return @user;
173 } 284 }
174 285
286 # now try default keymap
287 if (defined (my $def = find_default $ev)) {
288 return {
289 action => [$def],
290 };
291 }
292
175 () 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};
303 } else {
304 $::CONN->send_command ($_)
305 if $::CONN;
306 }
307 }
176} 308}
177 309
178sub keyboard_setup { 310sub keyboard_setup {
179 my $kbd_setup = new CFPlus::UI::VBox; 311 my $kbd_setup = new CFPlus::UI::VBox;
180 312
195 0 327 0
196 }, 328 },
197 ); 329 );
198 330
199 $list->add (new CFPlus::UI::FancyFrame 331 $list->add (new CFPlus::UI::FancyFrame
200 label => "Bindings", 332 label => "Macros",
201 child => (my $macros = new CFPlus::UI::Table), 333 child => (my $macros = new CFPlus::UI::VBox),
202 ); 334 );
203 335
204 my $refresh; 336 my $refresh;
205 337
206 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; 338 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
224 $kbd_setup->add (new CFPlus::UI::FancyFrame 356 $kbd_setup->add (new CFPlus::UI::FancyFrame
225 label => "Edit Macro", 357 label => "Edit Macro",
226 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), 358 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]),
227 ); 359 );
228 360
229 $editor->add (0, 1, new CFPlus::UI::Label 361 $editor->add_at (0, 1, new CFPlus::UI::Label
230 text => "Trigger", 362 text => "Trigger",
231 tooltip => $tooltip_trigger, 363 tooltip => $tooltip_trigger,
232 can_hover => 1, 364 can_hover => 1,
233 can_events => 1, 365 can_events => 1,
234 ); 366 );
235 $editor->add (0, 2, new CFPlus::UI::Label 367 $editor->add_at (0, 2, new CFPlus::UI::Label
236 text => "Actions", 368 text => "Actions",
237 tooltip => $tooltip_commands, 369 tooltip => $tooltip_commands,
238 can_hover => 1, 370 can_hover => 1,
239 can_events => 1, 371 can_events => 1,
240 ); 372 );
241 373
242 $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit 374 $editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit
243 text => macro_to_text $macro, 375 text => macro_to_text $macro,
244 tooltip => $tooltip_commands, 376 tooltip => $tooltip_commands,
245 on_changed => sub { 377 on_changed => sub {
246 $macro->{action} = macro_from_text $macro, $_[1]; 378 $macro->{action} = macro_from_text $macro, $_[1];
247 }, 379 },
248 ); 380 );
249 381
250 $editor->add (1, 1, my $accel = new CFPlus::UI::Button 382 $editor->add_at (1, 1, my $accel = new CFPlus::UI::Button
251 text => trigger_to_string $macro, 383 text => trigger_to_string $macro,
252 tooltip => "To change the trigger for a macro, activate this button.", 384 tooltip => "To change the trigger for a macro, activate this button.",
253 on_activate => sub { 385 on_activate => sub {
254 my ($accel) = @_; 386 my ($accel) = @_;
255 trigger_edit $macro, sub { 387 trigger_edit $macro, sub {
258 1 390 1
259 }, 391 },
260 ); 392 );
261 393
262 my $recording; 394 my $recording;
263 $editor->add (1, 3, new CFPlus::UI::Button 395 $editor->add_at (1, 3, new CFPlus::UI::Button
264 text => "Start Recording", 396 text => "Start Recording",
265 tooltip => "Start/Stop command recording: when recording, " 397 tooltip => "Start/Stop command recording: when recording, "
266 . "actions and commands you invoke are appended to this macro. " 398 . "actions and commands you invoke are appended to this macro. "
267 . "You can only record when you are logged in.", 399 . "You can only record when you are logged in.",
268 on_destroy => sub { 400 on_destroy => sub {
272 my ($widget) = @_; 404 my ($widget) = @_;
273 405
274 $recording = $::CONN && !$recording; 406 $recording = $::CONN && !$recording;
275 if ($recording) { 407 if ($recording) {
276 $widget->set_text ("Stop Recording"); 408 $widget->set_text ("Stop Recording");
277 my $action = $macro->{action} ||= [];
278 $::CONN->record (sub { 409 $::CONN->record (sub {
279 push @$action, $_[0]; 410 push @{ $macro->{action} }, $_[0];
280 $textedit->set_text (macro_to_text $macro); 411 $textedit->set_text (macro_to_text $macro);
281 }) if $::CONN; 412 }) if $::CONN;
282 } else { 413 } else {
283 $widget->set_text ("Start Recording"); 414 $widget->set_text ("Start Recording");
284 $::CONN->record if $::CONN; 415 $::CONN->record if $::CONN;
285 } 416 }
286 }, 417 },
287 ); 418 );
288 }; 419 };
289 420
421 $macros->add (new CFPlus::UI::Button
422 text => "New Macro",
423 tooltip => "Creates a new, empty, macro you can edit.",
424 on_activate => sub {
425 my $macro = { };
426 push @{ $::PROFILE->{macro} }, $macro;
427 $edit_macro->($macro);
428 },
429 );
430
431 $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]);
432
290 $REFRESH_MACRO_LIST = $refresh = sub { 433 $REFRESH_MACRO_LIST = $refresh = sub {
291 $macros->clear; 434 $macrolist->clear;
292 435
293 $macros->add (0, 0, new CFPlus::UI::Label 436 $macrolist->add_at (0, 1, new CFPlus::UI::Label
294 text => "Trigger", 437 text => "Trigger",
295 align => 0, 438 align => 0,
296 tooltip => $tooltip_trigger . $tooltip_common, 439 tooltip => $tooltip_trigger . $tooltip_common,
297 ); 440 );
298 $macros->add (1, 0, new CFPlus::UI::Label 441 $macrolist->add_at (1, 1, new CFPlus::UI::Label
299 text => "Commands", 442 text => "Commands",
300 tooltip => $tooltip_commands . $tooltip_common, 443 tooltip => $tooltip_commands . $tooltip_common,
301 ); 444 );
302 445
303 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { 446 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
304 my $macro = $::PROFILE->{macro}[$idx]; 447 my $macro = $::PROFILE->{macro}[$idx];
305 my $y = $idx + 1; 448 my $y = $idx + 2;
306 449
307 my $macro_cb = sub { 450 my $macro_cb = sub {
308 my ($widget, $ev) = @_; 451 my ($widget, $ev) = @_;
309 452
310 if ($ev->{button} == 1) { 453 if ($ev->{button} == 1) {
328 } 471 }
329 472
330 1 473 1
331 }; 474 };
332 475
333 $macros->add (0, $y, new CFPlus::UI::Label 476 $macrolist->add_at (0, $y, new CFPlus::UI::Label
334 text => trigger_to_string $macro, 477 text => trigger_to_string $macro,
335 tooltip => $tooltip_trigger . $tooltip_common, 478 tooltip => $tooltip_trigger . $tooltip_common,
336 align => 0, 479 align => 0,
337 can_hover => 1, 480 can_hover => 1,
338 can_events => 1, 481 can_events => 1,
339 on_button_down => $macro_cb, 482 on_button_down => $macro_cb,
340 ); 483 );
341 484
342 $macros->add (1, $y, new CFPlus::UI::Label 485 $macrolist->add_at (1, $y, new CFPlus::UI::Label
343 text => (join "; ", @{ $macro->{action} || [] }), 486 text => (join "; ", @{ $macro->{action} || [] }),
344 tooltip => $tooltip_commands . $tooltip_common, 487 tooltip => $tooltip_commands . $tooltip_common,
345 expand => 1, 488 expand => 1,
346 ellipsise => 3, 489 ellipsise => 3,
347 can_hover => 1, 490 can_hover => 1,
373 516
374 &$end_cb if $end_cb; 517 &$end_cb if $end_cb;
375 }; 518 };
376} 519}
377 520
5211

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines