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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines