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.8 by root, Sat Jul 21 20:05:37 2007 UTC vs.
Revision 1.22 by root, Sat Apr 3 02:58:24 2010 UTC

1package CFPlus::Macro; 1package DC::Macro;
2 2
3use strict; 3use common::sense;
4 4
5use List::Util ();
5use CFPlus::UI; 6use DC::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
26 (map +("LRAM-$_" => "!switch-tab $_"), 0..9),
27 "LRAM-x" => "!close-current-tab",
28
29 "return" => "!activate-chat",
30 "." => "!repeat-command",
31
32 "," => "take",
33 "space" => "apply",
34 "enter" => "examine",
35 "[+]" => "rotateshoottype +",
36 "[-]" => "rotateshoottype -",
37
38 "LRAM-e" => "examine",
39 "LRAM-s" => "ready_skill find traps",
40 "LRAM-d" => "ready_skill disarm traps",
41 "LRAM-p" => "ready_skill praying",
42);
8 43
9# allowed modifiers 44# allowed modifiers
10our %MODIFIER = ( 45our %MODIFIER = (
11 "LShift" => CFPlus::KMOD_LSHIFT, 46 "LShift" => DC::KMOD_LSHIFT,
12 "RShift" => CFPlus::KMOD_RSHIFT, 47 "RShift" => DC::KMOD_RSHIFT,
48# "Shift" => DC::KMOD_LSHIFT | DC::KMOD_RSHIFT,
13 "LCtrl" => CFPlus::KMOD_LCTRL, 49 "LCtrl" => DC::KMOD_LCTRL,
14 "RCtrl" => CFPlus::KMOD_RCTRL, 50 "RCtrl" => DC::KMOD_RCTRL,
51# "Ctrl" => DC::KMOD_LCTRL | DC::KMOD_RCTRL,
15 "LAlt" => CFPlus::KMOD_LALT, 52 "LAlt" => DC::KMOD_LALT,
16 "RAlt" => CFPlus::KMOD_RALT, 53 "RAlt" => DC::KMOD_RALT,
54# "Alt" => DC::KMOD_LALT | DC::KMOD_RALT,
17 "LMeta" => CFPlus::KMOD_LMETA, 55 "LMeta" => DC::KMOD_LMETA,
18 "RMeta" => CFPlus::KMOD_RMETA, 56 "RMeta" => DC::KMOD_RMETA,
57# "Meta" => DC::KMOD_LMETA | DC::KMOD_RMETA,
19); 58);
20 59
21# allowed modifiers 60# allowed modifiers
22our $MODIFIER_MASK |= $_ for values %MODIFIER; 61our $MODIFIER_MASK |= $_ for values %MODIFIER;
23 62
24# can bind to these without any modifier 63# can bind to these without any modifier
25our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9); 64our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9);
26 65
27our @DIRECT_KEYS = ( 66our @DIRECT_KEYS = (
28 CFPlus::SDLK_F1, 67 DC::SDLK_F1,
29 CFPlus::SDLK_F2, 68 DC::SDLK_F2,
30 CFPlus::SDLK_F3, 69 DC::SDLK_F3,
31 CFPlus::SDLK_F4, 70 DC::SDLK_F4,
32 CFPlus::SDLK_F5, 71 DC::SDLK_F5,
33 CFPlus::SDLK_F6, 72 DC::SDLK_F6,
34 CFPlus::SDLK_F7, 73 DC::SDLK_F7,
35 CFPlus::SDLK_F8, 74 DC::SDLK_F8,
36 CFPlus::SDLK_F9, 75 DC::SDLK_F9,
37 CFPlus::SDLK_F10, 76 DC::SDLK_F10,
38 CFPlus::SDLK_F11, 77 DC::SDLK_F11,
39 CFPlus::SDLK_F12, 78 DC::SDLK_F12,
40 CFPlus::SDLK_F13, 79 DC::SDLK_F13,
41 CFPlus::SDLK_F14, 80 DC::SDLK_F14,
42 CFPlus::SDLK_F15, 81 DC::SDLK_F15,
43); 82);
83
84our %MACRO_FUNCTION = (
85 "toggle-messagewindow" => sub { $::MESSAGE_WINDOW->toggle_visibility },
86 "toggle-playerbook" => sub { $::PL_WINDOW->toggle_visibility },
87 "toggle-help" => sub { $::HELP_WINDOW->toggle_visibility },
88 "toggle-stats" => sub { ::toggle_player_page ($::STATS_PAGE) },
89 "toggle-skills" => sub { ::toggle_player_page ($::SKILL_PAGE) },
90 "toggle-spells" => sub { ::toggle_player_page ($::SPELL_PAGE) },
91 "toggle-inventory" => sub { ::toggle_player_page ($::INVENTORY_PAGE) },
92 "toggle-pickup" => sub { ::toggle_player_page ($::PICKUP_PAGE) },
93 "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility },
94 "toggle-setup" => sub { $::SETUP_DIALOG->toggle_visibility },
95 "switch-tab" => sub { $::MESSAGE_WINDOW->user_switch_to_page (0 + shift) },
96 "close-current-tab" => sub { $::MESSAGE_WINDOW->close_current_tab },
97 "activate-chat" => sub { $::MESSAGE_WINDOW->activate_current },
98 "repeat-command" => sub {
99 $::CONN->user_send ($::COMPLETER->{last_command})
100 if $::CONN && exists $::COMPLETER->{last_command};
101 },
102 "completer" => sub {
103 if ($::CONN) {
104 $::COMPLETER->set_prefix (shift);
105 $::COMPLETER->show;
106 }
107 },
108);
109
110our $DEFAULT_KEYMAP;
111
112sub init {
113 $DEFAULT_KEYMAP ||= do {
114 local $MODIFIER{LRAM} = DC::KMOD_LRAM; # hack to enable internal LRAM modifer
115
116 my %sym = map +(DC::SDL_GetKeyName $_, $_), DC::SDLK_FIRST .. DC::SDLK_LAST;
117 my $map;
118
119 while (my ($k, $v) = each %DEFAULT_KEYMAP) {
120 if ($k =~ /^\((.)\)$/) {
121 $map->{U}{ord $1} = $v;
122 } else {
123 my @mod = split /-/, $k;
124 my $sym = $sym{pop @mod}
125 or warn "unknown keysym $k\n";
126
127 my $mod = 0; $mod |= $MODIFIER{$_} for @mod;
128
129 $map->{K}[DC::popcount $mod]{$mod}{$sym} = $v;
130 }
131 }
132
133 %DEFAULT_KEYMAP = ();
134 $map
135 };
136}
44 137
45sub accelkey_to_string($) { 138sub accelkey_to_string($) {
46 join "-", 139 join "-",
47 (grep $_[0][0] & $MODIFIER{$_}, 140 (grep $_[0][0] & $MODIFIER{$_}, keys %MODIFIER),
48 keys %MODIFIER),
49 CFPlus::SDL_GetKeyName $_[0][1] 141 DC::SDL_GetKeyName $_[0][1]
50} 142}
51 143
52sub trigger_to_string($) { 144sub trigger_to_string($) {
53 my ($macro) = @_; 145 my ($macro) = @_;
54 146
81 $window->disconnect_all ("focus_out"); 173 $window->disconnect_all ("focus_out");
82 $window->destroy; 174 $window->destroy;
83 &$end_cb; 175 &$end_cb;
84 }; 176 };
85 177
86 $window = new CFPlus::UI::Toplevel 178 $window = new DC::UI::Toplevel
87 title => "Edit Macro Trigger", 179 title => "Edit Macro Trigger",
88 x => "center", 180 x => "center",
89 y => "center", 181 y => "center",
90 z => 1000, 182 z => 1000,
91 can_events => 1, 183 can_events => 1,
99 $done->(0); 191 $done->(0);
100 1 192 1
101 }, 193 },
102 ; 194 ;
103 195
104 $window->add (my $vb = new CFPlus::UI::VBox); 196 $window->add (my $vb = new DC::UI::VBox);
105 197
106 $vb->add (new CFPlus::UI::Label 198 $vb->add (new DC::UI::Label
107 text => "To bind the macro to a key,\n" 199 text => "To bind the macro to a key,\n"
108 . "press a modifier (Ctrl, Alt\n" 200 . "press a modifier (Ctrl, Alt\n"
109 . "and/or Shift) and a key, or\n" 201 . "and/or Shift) and a key, or\n"
110 . "0-9 and F1-F15 without any modifier\n\n" 202 . "0-9 and F1-F15 without any modifier\n\n"
111 . "To cancel press Escape or close this.\n\n" 203 . "To cancel press Escape or close this.\n\n"
112 . "Accelerator key combo:", 204 . "Accelerator key combo:",
113 ellipsise => 0, 205 ellipsise => 0,
114 ); 206 );
115 207
116 $vb->add (my $entry = new CFPlus::UI::Label 208 $vb->add (my $entry = new DC::UI::Label
117 fg => [0, 0, 0, 1], 209 fg => [0, 0, 0, 1],
118 bg => [1, 1, 0, 1], 210 bg => [1, 1, 0, 1],
119 ); 211 );
120 212
121 my $key_cb = sub { 213 my $key_cb = sub {
134 map "$_-", 226 map "$_-",
135 grep $mod & $MODIFIER{$_}, 227 grep $mod & $MODIFIER{$_},
136 keys %MODIFIER 228 keys %MODIFIER
137 ); 229 );
138 230
139 return if $sym >= CFPlus::SDLK_MODIFIER_MIN 231 return if $sym >= DC::SDLK_MODIFIER_MIN
140 && $sym <= CFPlus::SDLK_MODIFIER_MAX; 232 && $sym <= DC::SDLK_MODIFIER_MAX;
141 233
142 if ($mod 234 if ($mod
143 || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS) 235 || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS)
144 || (grep $_ == $sym, @DIRECT_KEYS))) 236 || (grep $_ == $sym, @DIRECT_KEYS)))
145 { 237 {
146 $macro->{accelkey} = [$mod, $sym]; 238 $macro->{accelkey} = [$mod, $sym];
147 $done->(1); 239 $done->(1);
148 } else { 240 } else {
149 $entry->set_text ("cannot bind " . (CFPlus::SDL_GetKeyName $sym) . " without modifier."); 241 $entry->set_text ("cannot bind " . (DC::SDL_GetKeyName $sym) . " without modifier.");
150 } 242 }
151 1 243 1
152 }; 244 };
153 245
154 $window->connect (key_up => $key_cb); 246 $window->connect (key_up => $key_cb);
156 248
157 $window->grab_focus; 249 $window->grab_focus;
158 $window->show; 250 $window->show;
159} 251}
160 252
253sub find_default($) {
254 my ($ev) = @_;
255
256 for my $m (reverse grep $_, @{ $DEFAULT_KEYMAP->{K} }) {
257 for (keys %$m) {
258 if ($_ == ($ev->{mod} & $_)) {
259 if (defined (my $cmd = $m->{$_}{$ev->{sym}})) {
260 return $cmd;
261 }
262 }
263 }
264 }
265
266 if (my $cmd = $DEFAULT_KEYMAP->{U}{$ev->{unicode}}) {
267 return $cmd;
268 }
269
270 ()
271}
272
161# find macro by event 273# find macro by event
162sub match_event($) { 274sub find($) {
163 my ($ev) = @_; 275 my ($ev) = @_;
164 276
277 # try user-defined macros
278 if (my @user =
165 grep { 279 grep {
166 if (my $key = $_->{accelkey}) { 280 if (my $key = $_->{accelkey}) {
167 $key->[1] == $ev->{sym} 281 $key->[1] == $ev->{sym}
168 && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) 282 && $key->[0] == ($ev->{mod} & $MODIFIER_MASK)
283 } else {
284 0
285 }
286 } @{ $::PROFILE->{macro} || [] }
287 ) {
288 return @user;
289 }
290
291 # now try default keymap
292 if (defined (my $def = find_default $ev)) {
293 return {
294 action => [$def],
295 };
296 }
297
298 ()
299}
300
301sub execute {
302 my ($macro) = @_;
303
304 for (@{ $macro->{action} }) {
305 if (/^\!(\S+)\s?(.*)$/) {
306 $MACRO_FUNCTION{$1}->($2)
307 if exists $MACRO_FUNCTION{$1};
169 } else { 308 } else {
170 0 309 $::CONN->send_command ($_)
310 if $::CONN;
171 } 311 }
172 } @{ $::PROFILE->{macro} || [] } 312 }
173} 313}
174 314
175sub keyboard_setup { 315sub keyboard_setup {
176 my $kbd_setup = new CFPlus::UI::VBox; 316 my $kbd_setup = new DC::UI::VBox;
177 317
178 $kbd_setup->add (my $list = new CFPlus::UI::VBox); 318 $kbd_setup->add (my $list = new DC::UI::VBox);
179 319
180 $list->add (new CFPlus::UI::FancyFrame 320 $list->add (new DC::UI::FancyFrame
181 label => "Options", 321 label => "Options",
182 child => (my $hb = new CFPlus::UI::HBox), 322 child => (my $hb = new DC::UI::HBox),
183 ); 323 );
184 $hb->add (new CFPlus::UI::Label text => "only shift-up stops fire"); 324 $hb->add (new DC::UI::Label text => "only shift-up stops fire");
185 $hb->add (new CFPlus::UI::CheckBox 325 $hb->add (new DC::UI::CheckBox
186 expand => 1, 326 expand => 1,
187 state => $::CFG->{shift_fire_stop}, 327 state => $::CFG->{shift_fire_stop},
188 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.", 328 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.",
189 on_changed => sub { 329 on_changed => sub {
190 my ($cbox, $value) = @_; 330 my ($cbox, $value) = @_;
191 $::CFG->{shift_fire_stop} = $value; 331 $::CFG->{shift_fire_stop} = $value;
192 0 332 0
193 }, 333 },
194 ); 334 );
195 335
196 $list->add (new CFPlus::UI::FancyFrame 336 $list->add (new DC::UI::FancyFrame
197 label => "Macros", 337 label => "Macros",
198 child => (my $macros = new CFPlus::UI::VBox), 338 child => (my $macros = new DC::UI::VBox),
199 ); 339 );
200 340
201 my $refresh; 341 my $refresh;
202 342
203 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; 343 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
206 346
207 my $edit_macro = sub { 347 my $edit_macro = sub {
208 my ($macro) = @_; 348 my ($macro) = @_;
209 349
210 $kbd_setup->clear; 350 $kbd_setup->clear;
211 $kbd_setup->add (new CFPlus::UI::Button 351 $kbd_setup->add (new DC::UI::Button
212 text => "Return", 352 text => "Return",
213 tooltip => "Return to the macro list.", 353 tooltip => "Return to the macro list.",
214 on_activate => sub { 354 on_activate => sub {
215 $kbd_setup->clear; 355 $kbd_setup->clear;
216 $kbd_setup->add ($list); 356 $kbd_setup->add ($list);
217 $refresh->(); 357 $refresh->();
218 1 358 1
219 }, 359 },
220 ); 360 );
221 $kbd_setup->add (new CFPlus::UI::FancyFrame 361 $kbd_setup->add (new DC::UI::FancyFrame
222 label => "Edit Macro", 362 label => "Edit Macro",
223 child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), 363 child => (my $editor = new DC::UI::Table col_expand => [0, 1]),
224 ); 364 );
225 365
226 $editor->add_at (0, 1, new CFPlus::UI::Label 366 $editor->add_at (0, 1, new DC::UI::Label
227 text => "Trigger", 367 text => "Trigger",
228 tooltip => $tooltip_trigger, 368 tooltip => $tooltip_trigger,
229 can_hover => 1, 369 can_hover => 1,
230 can_events => 1, 370 can_events => 1,
231 ); 371 );
232 $editor->add_at (0, 2, new CFPlus::UI::Label 372 $editor->add_at (0, 2, new DC::UI::Label
233 text => "Actions", 373 text => "Actions",
234 tooltip => $tooltip_commands, 374 tooltip => $tooltip_commands,
235 can_hover => 1, 375 can_hover => 1,
236 can_events => 1, 376 can_events => 1,
237 ); 377 );
238 378
239 $editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit 379 $editor->add_at (1, 2, my $textedit = new DC::UI::TextEdit
240 text => macro_to_text $macro, 380 text => macro_to_text $macro,
241 tooltip => $tooltip_commands, 381 tooltip => $tooltip_commands,
242 on_changed => sub { 382 on_changed => sub {
243 $macro->{action} = macro_from_text $macro, $_[1]; 383 $macro->{action} = macro_from_text $macro, $_[1];
244 }, 384 },
245 ); 385 );
246 386
247 $editor->add_at (1, 1, my $accel = new CFPlus::UI::Button 387 $editor->add_at (1, 1, my $accel = new DC::UI::Button
248 text => trigger_to_string $macro, 388 text => trigger_to_string $macro,
249 tooltip => "To change the trigger for a macro, activate this button.", 389 tooltip => "To change the trigger for a macro, activate this button.",
250 on_activate => sub { 390 on_activate => sub {
251 my ($accel) = @_; 391 my ($accel) = @_;
252 trigger_edit $macro, sub { 392 trigger_edit $macro, sub {
255 1 395 1
256 }, 396 },
257 ); 397 );
258 398
259 my $recording; 399 my $recording;
260 $editor->add_at (1, 3, new CFPlus::UI::Button 400 $editor->add_at (1, 3, new DC::UI::Button
261 text => "Start Recording", 401 text => "Start Recording",
262 tooltip => "Start/Stop command recording: when recording, " 402 tooltip => "Start/Stop command recording: when recording, "
263 . "actions and commands you invoke are appended to this macro. " 403 . "actions and commands you invoke are appended to this macro. "
264 . "You can only record when you are logged in.", 404 . "You can only record when you are logged in.",
265 on_destroy => sub { 405 on_destroy => sub {
281 } 421 }
282 }, 422 },
283 ); 423 );
284 }; 424 };
285 425
286 $macros->add (new CFPlus::UI::Button 426 $macros->add (new DC::UI::Button
287 text => "New Macro", 427 text => "New Macro",
288 tooltip => "Creates a new, empty, macro you can edit.", 428 tooltip => "Creates a new, empty, macro you can edit.",
289 on_activate => sub { 429 on_activate => sub {
290 my $macro = { }; 430 my $macro = { };
291 push @{ $::PROFILE->{macro} }, $macro; 431 push @{ $::PROFILE->{macro} }, $macro;
292 $edit_macro->($macro); 432 $edit_macro->($macro);
293 }, 433 },
294 ); 434 );
295 435
296 $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]); 436 $macros->add (my $macrolist = new DC::UI::Table col_expand => [0, 1]);
297 437
298 $REFRESH_MACRO_LIST = $refresh = sub { 438 $REFRESH_MACRO_LIST = $refresh = sub {
299 $macrolist->clear; 439 $macrolist->clear;
300 440
301 $macrolist->add_at (0, 1, new CFPlus::UI::Label 441 $macrolist->add_at (0, 1, new DC::UI::Label
302 text => "Trigger", 442 text => "Trigger",
443 tooltip => $tooltip_trigger . $tooltip_common,
444 );
445 $macrolist->add_at (1, 1, new DC::UI::Label
446 text => "Actions",
303 align => 0, 447 align => 0,
304 tooltip => $tooltip_trigger . $tooltip_common,
305 );
306 $macrolist->add_at (1, 1, new CFPlus::UI::Label
307 text => "Commands",
308 tooltip => $tooltip_commands . $tooltip_common, 448 tooltip => $tooltip_commands . $tooltip_common,
309 ); 449 );
310 450
311 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { 451 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
312 my $macro = $::PROFILE->{macro}[$idx]; 452 my $macro = $::PROFILE->{macro}[$idx];
316 my ($widget, $ev) = @_; 456 my ($widget, $ev) = @_;
317 457
318 if ($ev->{button} == 1) { 458 if ($ev->{button} == 1) {
319 $edit_macro->($macro), 459 $edit_macro->($macro),
320 } elsif ($ev->{button} == 2) { 460 } elsif ($ev->{button} == 2) {
321 $::CONN->macro_send ($macro) if $::CONN; 461 execute ($macro);
322 } elsif ($ev->{button} == 3) { 462 } elsif ($ev->{button} == 3) {
323 (new CFPlus::UI::Menu 463 (new DC::UI::Menu
324 items => [ 464 items => [
325 ["Edit" => sub { $edit_macro->($macro) }], 465 ["Edit" => sub { $edit_macro->($macro) }],
326 ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }], 466 ["Invoke" => sub { execute ($macro) }],
327 ["Delete" => sub { 467 ["Delete" => sub {
328 # might want to use grep instead 468 # might want to use grep instead
329 splice @{$::PROFILE->{macro}}, $idx, 1, (); 469 splice @{$::PROFILE->{macro}}, $idx, 1, ();
330 $refresh->(); 470 $refresh->();
331 }], 471 }],
336 } 476 }
337 477
338 1 478 1
339 }; 479 };
340 480
341 $macrolist->add_at (0, $y, new CFPlus::UI::Label 481 $macrolist->add_at (0, $y, new DC::UI::Label
342 text => trigger_to_string $macro, 482 text => trigger_to_string $macro,
343 tooltip => $tooltip_trigger . $tooltip_common, 483 tooltip => $tooltip_trigger . $tooltip_common,
344 align => 0, 484 fg => [1, 0.8, 0.8],
345 can_hover => 1, 485 can_hover => 1,
346 can_events => 1, 486 can_events => 1,
347 on_button_down => $macro_cb, 487 on_button_down => $macro_cb,
348 ); 488 );
349 489
350 $macrolist->add_at (1, $y, new CFPlus::UI::Label 490 $macrolist->add_at (1, $y, new DC::UI::Label
351 text => (join "; ", @{ $macro->{action} || [] }), 491 text => (join "; ", @{ $macro->{action} || [] }),
352 tooltip => $tooltip_commands . $tooltip_common, 492 tooltip => $tooltip_commands . $tooltip_common,
493 fg => [0.9, 0.9, 0.9],
494 align => 0,
353 expand => 1, 495 expand => 1,
354 ellipsise => 3, 496 ellipsise => 3,
355 can_hover => 1, 497 can_hover => 1,
356 can_events => 1, 498 can_events => 1,
357 on_button_down => $macro_cb, 499 on_button_down => $macro_cb,
381 523
382 &$end_cb if $end_cb; 524 &$end_cb if $end_cb;
383 }; 525 };
384} 526}
385 527
5281

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines