--- deliantra/Deliantra-Client/DC/Macro.pm 2006/12/09 21:44:43 1.2 +++ deliantra/Deliantra-Client/DC/Macro.pm 2007/07/21 20:05:37 1.8 @@ -159,20 +159,17 @@ } # find macro by event -# maybe return multiple results? sub match_event($) { my ($ev) = @_; - for my $macro (@{ $::PROFILE->{macro} || [] }) { - my $key = $macro->{accelkey} - or next; - - $key->[1] == $ev->{sym} - && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) - && return $macro; - } - - () + grep { + if (my $key = $_->{accelkey}) { + $key->[1] == $ev->{sym} + && $key->[0] == ($ev->{mod} & $MODIFIER_MASK) + } else { + 0 + } + } @{ $::PROFILE->{macro} || [] } } sub keyboard_setup { @@ -197,8 +194,8 @@ ); $list->add (new CFPlus::UI::FancyFrame - label => "Bindings", - child => (my $macros = new CFPlus::UI::Table), + label => "Macros", + child => (my $macros = new CFPlus::UI::VBox), ); my $refresh; @@ -226,20 +223,20 @@ child => (my $editor = new CFPlus::UI::Table col_expand => [0, 1]), ); - $editor->add (0, 1, new CFPlus::UI::Label + $editor->add_at (0, 1, new CFPlus::UI::Label text => "Trigger", tooltip => $tooltip_trigger, can_hover => 1, can_events => 1, ); - $editor->add (0, 2, new CFPlus::UI::Label + $editor->add_at (0, 2, new CFPlus::UI::Label text => "Actions", tooltip => $tooltip_commands, can_hover => 1, can_events => 1, ); - $editor->add (1, 2, my $textedit = new CFPlus::UI::TextEdit + $editor->add_at (1, 2, my $textedit = new CFPlus::UI::TextEdit text => macro_to_text $macro, tooltip => $tooltip_commands, on_changed => sub { @@ -247,7 +244,7 @@ }, ); - $editor->add (1, 1, my $accel = new CFPlus::UI::Button + $editor->add_at (1, 1, my $accel = new CFPlus::UI::Button text => trigger_to_string $macro, tooltip => "To change the trigger for a macro, activate this button.", on_activate => sub { @@ -260,7 +257,7 @@ ); my $recording; - $editor->add (1, 3, new CFPlus::UI::Button + $editor->add_at (1, 3, new CFPlus::UI::Button text => "Start Recording", tooltip => "Start/Stop command recording: when recording, " . "actions and commands you invoke are appended to this macro. " @@ -274,9 +271,8 @@ $recording = $::CONN && !$recording; if ($recording) { $widget->set_text ("Stop Recording"); - my $action = $macro->{action} ||= []; $::CONN->record (sub { - push @$action, $_[0]; + push @{ $macro->{action} }, $_[0]; $textedit->set_text (macro_to_text $macro); }) if $::CONN; } else { @@ -287,22 +283,34 @@ ); }; + $macros->add (new CFPlus::UI::Button + text => "New Macro", + tooltip => "Creates a new, empty, macro you can edit.", + on_activate => sub { + my $macro = { }; + push @{ $::PROFILE->{macro} }, $macro; + $edit_macro->($macro); + }, + ); + + $macros->add (my $macrolist = new CFPlus::UI::Table col_expand => [0, 1]); + $REFRESH_MACRO_LIST = $refresh = sub { - $macros->clear; + $macrolist->clear; - $macros->add (0, 0, new CFPlus::UI::Label + $macrolist->add_at (0, 1, new CFPlus::UI::Label text => "Trigger", align => 0, tooltip => $tooltip_trigger . $tooltip_common, ); - $macros->add (1, 0, new CFPlus::UI::Label + $macrolist->add_at (1, 1, new CFPlus::UI::Label text => "Commands", tooltip => $tooltip_commands . $tooltip_common, ); for my $idx (0 .. $#{$::PROFILE->{macro} || []}) { my $macro = $::PROFILE->{macro}[$idx]; - my $y = $idx + 1; + my $y = $idx + 2; my $macro_cb = sub { my ($widget, $ev) = @_; @@ -330,7 +338,7 @@ 1 }; - $macros->add (0, $y, new CFPlus::UI::Label + $macrolist->add_at (0, $y, new CFPlus::UI::Label text => trigger_to_string $macro, tooltip => $tooltip_trigger . $tooltip_common, align => 0, @@ -339,8 +347,8 @@ on_button_down => $macro_cb, ); - $macros->add (1, $y, new CFPlus::UI::Label - text => (join "; ", @{ $macro->{action} }), + $macrolist->add_at (1, $y, new CFPlus::UI::Label + text => (join "; ", @{ $macro->{action} || [] }), tooltip => $tooltip_commands . $tooltip_common, expand => 1, ellipsise => 3, @@ -359,14 +367,13 @@ # this is a shortcut method that asks for a binding # and then just binds it. sub quick_macro { - my ($self, $cmds, $end_cb) = @_; + my ($cmds, $end_cb) = @_; my $macro = { action => $cmds, }; trigger_edit $macro, sub { - if ($_[0]) { push @{ $::PROFILE->{macro} }, $macro; $REFRESH_MACRO_LIST->();