Can now set the grid selected from a Lua macro. Should still work in Ruby too but can't test that. Don't blame me if it breaks.

Originally committed to SVN as r1384.
This commit is contained in:
Niels Martin Hansen 2007-07-06 14:26:04 +00:00
parent 70367a1c35
commit 9eb5ab4e25
7 changed files with 62 additions and 12 deletions

View File

@ -110,7 +110,7 @@ namespace Automation4 {
const wxString& GetDescription() const;
virtual bool Validate(AssFile *subs, const std::vector<int> &selected, int active) = 0;
virtual void Process(AssFile *subs, const std::vector<int> &selected, int active, wxWindow * const progress_parent) = 0;
virtual void Process(AssFile *subs, std::vector<int> &selected, int active, wxWindow * const progress_parent) = 0;
};

View File

@ -561,7 +561,7 @@ namespace Automation4 {
return result;
}
void LuaFeatureMacro::Process(AssFile *subs, const std::vector<int> &selected, int active, wxWindow * const progress_parent)
void LuaFeatureMacro::Process(AssFile *subs, std::vector<int> &selected, int active, wxWindow * const progress_parent)
{
GetFeatureFunction(1); // 1 = processing function
@ -575,13 +575,31 @@ namespace Automation4 {
ps->SetTitle(GetName());
// do call
LuaThreadedCall call(L, 3, 0);
// 3 args: subtitles, selected lines, active line
// 1 result: new selected lines
LuaThreadedCall call(L, 3, 1);
ps->ShowModal();
wxThread::ExitCode code = call.Wait();
(void) code;
(void) code; // ignore
//if (code) ThrowError();
// top of stack will be selected lines array, if any was returned
if (lua_istable(L, -1)) {
selected.clear();
selected.reserve(lua_objlen(L, -1));
lua_pushnil(L);
while (lua_next(L, -2)) {
if (lua_isnumber(L, -1)) {
selected.push_back(lua_tointeger(L, -1));
}
lua_pop(L, 1);
}
std::sort(selected.begin(), selected.end());
}
// either way, there will be something on the stack
lua_pop(L, 1);
delete ps;
}

View File

@ -222,7 +222,7 @@ namespace Automation4 {
virtual ~LuaFeatureMacro() { }
virtual bool Validate(AssFile *subs, const std::vector<int> &selected, int active);
virtual void Process(AssFile *subs, const std::vector<int> &selected, int active, wxWindow * const progress_parent);
virtual void Process(AssFile *subs, std::vector<int> &selected, int active, wxWindow * const progress_parent);
};

View File

@ -334,7 +334,7 @@ namespace Automation4 {
return false;
}
void RubyFeatureMacro::Process(AssFile *subs, const std::vector<int> &selected, int active, wxWindow * const progress_parent)
void RubyFeatureMacro::Process(AssFile *subs, std::vector<int> &selected, int active, wxWindow * const progress_parent)
{
delete RubyProgressSink::inst;
RubyProgressSink::inst = new RubyProgressSink(progress_parent, false);
@ -375,14 +375,13 @@ namespace Automation4 {
break;
case T_FIXNUM: // array of ints = selection
// i hope this works, can't test it -jfs
int num = RARRAY(p)->len;
std::vector<int> sel(num);
selected.clear();
selected.reserve(num);
for(int i = 0; i < num; ++i) {
sel[i] = FIX2INT(RARRAY(p)->ptr[i]);
selected.push_back(FIX2INT(RARRAY(p)->ptr[i]));
}
FrameMain *frame = AegisubApp::Get()->frame;
frame->SubsBox->LoadFromAss(AssFile::top, true, true);
frame->SubsBox->SetSelectionFromAbsolute(sel);
break;
}
}

View File

@ -195,7 +195,7 @@ namespace Automation4 {
virtual ~RubyFeatureMacro() { }
virtual bool Validate(AssFile *subs, const std::vector<int> &selected, int active);
virtual void Process(AssFile *subs, const std::vector<int> &selected, int active, wxWindow * const progress_parent);
virtual void Process(AssFile *subs, std::vector<int> &selected, int active, wxWindow * const progress_parent);
};

View File

@ -999,6 +999,7 @@ void FrameMain::OnAutomationMacro (wxCommandEvent &event) {
activeMacroItems[event.GetId()-Menu_Automation_Macro]->Process(SubsBox->ass, selected_lines, first_sel, this);
// Have the grid update its maps, this properly refreshes it to reflect the changed subs
SubsBox->UpdateMaps();
SubsBox->SetSelectionFromAbsolute(selected_lines);
SubsBox->EndBatch();
SubsBox->CommitChanges(true, false);
}

View File

@ -0,0 +1,32 @@
-- Automation 4 test file
-- Create a Macro feature, that displays some text
script_name = "Automation 4 set-selection test"
script_description = "Test setting the grid selection"
script_author = "Niels Martin Hansen"
script_version = "1"
function selecttest(subtitles, selected_lines, active_line)
-- get line-id of first selected line
local lid = selected_lines[1]
-- insert a copy of line 'lid' before itself
subtitles[-lid] = subtitles[lid]
-- append a copy of the copy of the copied line
subtitles[0] = subtitles[lid]
-- grab the copied line
local l = subtitles[lid]
-- modify it
l.text = "A4 was here!"
-- and store it back
subtitles[lid] = l
-- select some new lines
selected_lines = { lid-1, lid, lid+1 }
-- and set undo point (never forget!)
aegisub.set_undo_point("Insert+select Stuff")
-- return the new selection
return selected_lines
end
aegisub.register_macro("Insert+select stuff", "Inserts some lines near the active line and selects the new lines", selecttest, nil)