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.19 by root, Sat Aug 30 08:04:01 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines