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.7 by root, Fri Jul 20 16:32:04 2007 UTC vs.
Revision 1.18 by root, Mon May 5 16:19:47 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 (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" => DC::KMOD_LSHIFT,
12 "RShift" => CFPlus::KMOD_RSHIFT, 45 "RShift" => DC::KMOD_RSHIFT,
46# "Shift" => DC::KMOD_LSHIFT | DC::KMOD_RSHIFT,
13 "LCtrl" => CFPlus::KMOD_LCTRL, 47 "LCtrl" => DC::KMOD_LCTRL,
14 "RCtrl" => CFPlus::KMOD_RCTRL, 48 "RCtrl" => DC::KMOD_RCTRL,
49# "Ctrl" => DC::KMOD_LCTRL | DC::KMOD_RCTRL,
15 "LAlt" => CFPlus::KMOD_LALT, 50 "LAlt" => DC::KMOD_LALT,
16 "RAlt" => CFPlus::KMOD_RALT, 51 "RAlt" => DC::KMOD_RALT,
52# "Alt" => DC::KMOD_LALT | DC::KMOD_RALT,
17 "LMeta" => CFPlus::KMOD_LMETA, 53 "LMeta" => DC::KMOD_LMETA,
18 "RMeta" => CFPlus::KMOD_RMETA, 54 "RMeta" => DC::KMOD_RMETA,
55# "Meta" => DC::KMOD_LMETA | DC::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
24# can bind to these without any modifier 61# can bind to these without any modifier
25our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9); 62our @DIRECT_CHARS = qw(0 1 2 3 4 5 6 7 8 9);
26 63
27our @DIRECT_KEYS = ( 64our @DIRECT_KEYS = (
28 CFPlus::SDLK_F1, 65 DC::SDLK_F1,
29 CFPlus::SDLK_F2, 66 DC::SDLK_F2,
30 CFPlus::SDLK_F3, 67 DC::SDLK_F3,
31 CFPlus::SDLK_F4, 68 DC::SDLK_F4,
32 CFPlus::SDLK_F5, 69 DC::SDLK_F5,
33 CFPlus::SDLK_F6, 70 DC::SDLK_F6,
34 CFPlus::SDLK_F7, 71 DC::SDLK_F7,
35 CFPlus::SDLK_F8, 72 DC::SDLK_F8,
36 CFPlus::SDLK_F9, 73 DC::SDLK_F9,
37 CFPlus::SDLK_F10, 74 DC::SDLK_F10,
38 CFPlus::SDLK_F11, 75 DC::SDLK_F11,
39 CFPlus::SDLK_F12, 76 DC::SDLK_F12,
40 CFPlus::SDLK_F13, 77 DC::SDLK_F13,
41 CFPlus::SDLK_F14, 78 DC::SDLK_F14,
42 CFPlus::SDLK_F15, 79 DC::SDLK_F15,
43); 80);
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 +(DC::SDL_GetKeyName $_, $_), DC::SDLK_FIRST .. DC::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}[DC::popcount $mod]{$mod}{$sym} = $v;
126 }
127 }
128
129 %DEFAULT_KEYMAP = ();
130 $map
131 };
132}
44 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 DC::SDL_GetKeyName $_[0][1]
50} 138}
51 139
52sub trigger_to_string($) { 140sub trigger_to_string($) {
53 my ($macro) = @_; 141 my ($macro) = @_;
54 142
81 $window->disconnect_all ("focus_out"); 169 $window->disconnect_all ("focus_out");
82 $window->destroy; 170 $window->destroy;
83 &$end_cb; 171 &$end_cb;
84 }; 172 };
85 173
86 $window = new CFPlus::UI::Toplevel 174 $window = new DC::UI::Toplevel
87 title => "Edit Macro Trigger", 175 title => "Edit Macro Trigger",
88 x => "center", 176 x => "center",
89 y => "center", 177 y => "center",
90 z => 1000, 178 z => 1000,
91 can_events => 1, 179 can_events => 1,
99 $done->(0); 187 $done->(0);
100 1 188 1
101 }, 189 },
102 ; 190 ;
103 191
104 $window->add (my $vb = new CFPlus::UI::VBox); 192 $window->add (my $vb = new DC::UI::VBox);
105 193
106 $vb->add (new CFPlus::UI::Label 194 $vb->add (new DC::UI::Label
107 text => "To bind the macro to a key,\n" 195 text => "To bind the macro to a key,\n"
108 . "press a modifier (Ctrl, Alt\n" 196 . "press a modifier (Ctrl, Alt\n"
109 . "and/or Shift) and a key, or\n" 197 . "and/or Shift) and a key, or\n"
110 . "0-9 and F1-F15 without any modifier\n\n" 198 . "0-9 and F1-F15 without any modifier\n\n"
111 . "To cancel press Escape or close this.\n\n" 199 . "To cancel press Escape or close this.\n\n"
112 . "Accelerator key combo:", 200 . "Accelerator key combo:",
113 ellipsise => 0, 201 ellipsise => 0,
114 ); 202 );
115 203
116 $vb->add (my $entry = new CFPlus::UI::Label 204 $vb->add (my $entry = new DC::UI::Label
117 fg => [0, 0, 0, 1], 205 fg => [0, 0, 0, 1],
118 bg => [1, 1, 0, 1], 206 bg => [1, 1, 0, 1],
119 ); 207 );
120 208
121 my $key_cb = sub { 209 my $key_cb = sub {
134 map "$_-", 222 map "$_-",
135 grep $mod & $MODIFIER{$_}, 223 grep $mod & $MODIFIER{$_},
136 keys %MODIFIER 224 keys %MODIFIER
137 ); 225 );
138 226
139 return if $sym >= CFPlus::SDLK_MODIFIER_MIN 227 return if $sym >= DC::SDLK_MODIFIER_MIN
140 && $sym <= CFPlus::SDLK_MODIFIER_MAX; 228 && $sym <= DC::SDLK_MODIFIER_MAX;
141 229
142 if ($mod 230 if ($mod
143 || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS) 231 || ((grep $_ eq chr $ev->{unicode}, @DIRECT_CHARS)
144 || (grep $_ == $sym, @DIRECT_KEYS))) 232 || (grep $_ == $sym, @DIRECT_KEYS)))
145 { 233 {
146 $macro->{accelkey} = [$mod, $sym]; 234 $macro->{accelkey} = [$mod, $sym];
147 $done->(1); 235 $done->(1);
148 } else { 236 } else {
149 $entry->set_text ("cannot bind " . (CFPlus::SDL_GetKeyName $sym) . " without modifier."); 237 $entry->set_text ("cannot bind " . (DC::SDL_GetKeyName $sym) . " without modifier.");
150 } 238 }
151 1 239 1
152 }; 240 };
153 241
154 $window->connect (key_up => $key_cb); 242 $window->connect (key_up => $key_cb);
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 DC::UI::VBox;
177 313
178 $kbd_setup->add (my $list = new CFPlus::UI::VBox); 314 $kbd_setup->add (my $list = new DC::UI::VBox);
179 315
180 $list->add (new CFPlus::UI::FancyFrame 316 $list->add (new DC::UI::FancyFrame
181 label => "Options", 317 label => "Options",
182 child => (my $hb = new CFPlus::UI::HBox), 318 child => (my $hb = new DC::UI::HBox),
183 ); 319 );
184 $hb->add (new CFPlus::UI::Label text => "only shift-up stops fire"); 320 $hb->add (new DC::UI::Label text => "only shift-up stops fire");
185 $hb->add (new CFPlus::UI::CheckBox 321 $hb->add (new DC::UI::CheckBox
186 expand => 1, 322 expand => 1,
187 state => $::CFG->{shift_fire_stop}, 323 state => $::CFG->{shift_fire_stop},
188 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.", 324 tooltip => "If this checkbox is enabled you will stop fire only if you stop pressing shift.",
189 on_changed => sub { 325 on_changed => sub {
190 my ($cbox, $value) = @_; 326 my ($cbox, $value) = @_;
191 $::CFG->{shift_fire_stop} = $value; 327 $::CFG->{shift_fire_stop} = $value;
192 0 328 0
193 }, 329 },
194 ); 330 );
195 331
196 $list->add (new CFPlus::UI::FancyFrame 332 $list->add (new DC::UI::FancyFrame
197 label => "Macros", 333 label => "Macros",
198 child => (my $macros = new CFPlus::UI::VBox), 334 child => (my $macros = new DC::UI::VBox),
199 ); 335 );
200 336
201 my $refresh; 337 my $refresh;
202 338
203 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>"; 339 my $tooltip_common = "\n\n<small>Left click - edit macro\nMiddle click - invoke macro\nRight click - further options</small>";
206 342
207 my $edit_macro = sub { 343 my $edit_macro = sub {
208 my ($macro) = @_; 344 my ($macro) = @_;
209 345
210 $kbd_setup->clear; 346 $kbd_setup->clear;
211 $kbd_setup->add (new CFPlus::UI::Button 347 $kbd_setup->add (new DC::UI::Button
212 text => "Return", 348 text => "Return",
213 tooltip => "Return to the macro list.", 349 tooltip => "Return to the macro list.",
214 on_activate => sub { 350 on_activate => sub {
215 $kbd_setup->clear; 351 $kbd_setup->clear;
216 $kbd_setup->add ($list); 352 $kbd_setup->add ($list);
217 $refresh->(); 353 $refresh->();
218 1 354 1
219 }, 355 },
220 ); 356 );
221 $kbd_setup->add (new CFPlus::UI::FancyFrame 357 $kbd_setup->add (new DC::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 DC::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 DC::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 DC::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 DC::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 DC::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 DC::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 {
281 } 417 }
282 }, 418 },
283 ); 419 );
284 }; 420 };
285 421
286 $macros->add (new CFPlus::UI::Button 422 $macros->add (new DC::UI::Button
287 text => "New Macro", 423 text => "New Macro",
288 tooltip => "Creates a new, empty, macro you can edit.", 424 tooltip => "Creates a new, empty, macro you can edit.",
289 on_activate => sub { 425 on_activate => sub {
290 my $macro = { }; 426 my $macro = { };
291 push @{ $::PROFILE->{macro} }, $macro; 427 push @{ $::PROFILE->{macro} }, $macro;
292 $edit_macro->($macro); 428 $edit_macro->($macro);
293 }, 429 },
294 ); 430 );
295 431
296 $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]); 432 $macros->add (my $macrolist = new DC::UI::Table col_expand => [0, 1]);
297 433
298 $REFRESH_MACRO_LIST = $refresh = sub { 434 $REFRESH_MACRO_LIST = $refresh = sub {
299 $macrolist->clear; 435 $macrolist->clear;
300 436
301 $macrolist->add_at (0, 1, new CFPlus::UI::Label 437 $macrolist->add_at (0, 1, new DC::UI::Label
302 text => "Trigger", 438 text => "Trigger",
439 tooltip => $tooltip_trigger . $tooltip_common,
440 );
441 $macrolist->add_at (1, 1, new DC::UI::Label
442 text => "Actions",
303 align => 0, 443 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, 444 tooltip => $tooltip_commands . $tooltip_common,
309 ); 445 );
310 446
311 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { 447 for my $idx (0 .. $#{$::PROFILE->{macro} || []}) {
312 my $macro = $::PROFILE->{macro}[$idx]; 448 my $macro = $::PROFILE->{macro}[$idx];
318 if ($ev->{button} == 1) { 454 if ($ev->{button} == 1) {
319 $edit_macro->($macro), 455 $edit_macro->($macro),
320 } elsif ($ev->{button} == 2) { 456 } elsif ($ev->{button} == 2) {
321 $::CONN->macro_send ($macro) if $::CONN; 457 $::CONN->macro_send ($macro) if $::CONN;
322 } elsif ($ev->{button} == 3) { 458 } elsif ($ev->{button} == 3) {
323 (new CFPlus::UI::Menu 459 (new DC::UI::Menu
324 items => [ 460 items => [
325 ["Edit" => sub { $edit_macro->($macro) }], 461 ["Edit" => sub { $edit_macro->($macro) }],
326 ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }], 462 ["Invoke" => sub { $::CONN->macro_send ($macro) if $::CONN }],
327 ["Delete" => sub { 463 ["Delete" => sub {
328 # might want to use grep instead 464 # might want to use grep instead
336 } 472 }
337 473
338 1 474 1
339 }; 475 };
340 476
341 $macrolist->add_at (0, $y, new CFPlus::UI::Label 477 $macrolist->add_at (0, $y, new DC::UI::Label
342 text => trigger_to_string $macro, 478 text => trigger_to_string $macro,
343 tooltip => $tooltip_trigger . $tooltip_common, 479 tooltip => $tooltip_trigger . $tooltip_common,
344 align => 0, 480 fg => [1, 0.8, 0.8],
345 can_hover => 1, 481 can_hover => 1,
346 can_events => 1, 482 can_events => 1,
347 on_button_down => $macro_cb, 483 on_button_down => $macro_cb,
348 ); 484 );
349 485
350 $macrolist->add_at (1, $y, new CFPlus::UI::Label 486 $macrolist->add_at (1, $y, new DC::UI::Label
351 text => (join "; ", @{ $macro->{action} || [] }), 487 text => (join "; ", @{ $macro->{action} || [] }),
352 tooltip => $tooltip_commands . $tooltip_common, 488 tooltip => $tooltip_commands . $tooltip_common,
489 fg => [0.9, 0.9, 0.9],
490 align => 0,
353 expand => 1, 491 expand => 1,
354 ellipsise => 3, 492 ellipsise => 3,
355 can_hover => 1, 493 can_hover => 1,
356 can_events => 1, 494 can_events => 1,
357 on_button_down => $macro_cb, 495 on_button_down => $macro_cb,
381 519
382 &$end_cb if $end_cb; 520 &$end_cb if $end_cb;
383 }; 521 };
384} 522}
385 523
5241

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines