diff --git a/aegisub/src/command/time.cpp b/aegisub/src/command/time.cpp index 10afa8c1a..76aef03c1 100644 --- a/aegisub/src/command/time.cpp +++ b/aegisub/src/command/time.cpp @@ -69,7 +69,7 @@ namespace { CMD_TYPE(COMMAND_VALIDATE) bool Validate(const agi::Context *c) { SubtitleSelection sel = c->selectionController->GetSelectedSet(); - if (sel.size() < 2) return false; + if (sel.size() < 2) return !sel.empty(); size_t found = 0; for (auto diag : c->ass->Line | agi::of_type()) { @@ -87,6 +87,34 @@ namespace { /// @defgroup cmd-time Time manipulation commands. /// @{ +static void adjoin_lines(agi::Context *c, bool set_start) { + auto sel = c->selectionController->GetSelectedSet(); + AssDialogue *prev = nullptr; + size_t seen = 0; + bool prev_sel = false; + for (auto diag : c->ass->Line | agi::of_type()) { + bool cur_sel = !!sel.count(diag); + if (prev) { + // One row selections act as if the previous or next line was selected + if (set_start && cur_sel && (sel.size() == 1 || prev_sel)) + diag->Start = prev->End; + else if (!set_start && prev_sel && (cur_sel || sel.size() == 1)) + prev->End = diag->Start; + } + + if (seen == sel.size()) + break; + + if (cur_sel) + ++seen; + + prev = diag; + prev_sel = cur_sel; + } + + c->ass->Commit(_("adjoin"), AssFile::COMMIT_DIAG_TIME); +} + /// Changes times of subs so end times begin on next's start time. struct time_continuous_end : public validate_adjoinable { CMD_NAME("time/continuous/end") @@ -95,12 +123,10 @@ struct time_continuous_end : public validate_adjoinable { STR_HELP("Changes times of subs so end times begin on next's start time") void operator()(agi::Context *c) { - wxArrayInt sels = c->subsGrid->GetSelection(); - c->subsGrid->AdjoinLines(sels.front(), sels.back(), false); + adjoin_lines(c, false); } }; - /// Changes times of subs so start times begin on previous's end time. struct time_continuous_start : public validate_adjoinable { CMD_NAME("time/continuous/start") @@ -109,10 +135,8 @@ struct time_continuous_start : public validate_adjoinable { STR_HELP("Changes times of subs so start times begin on previous's end time") void operator()(agi::Context *c) { - wxArrayInt sels = c->subsGrid->GetSelection(); - c->subsGrid->AdjoinLines(sels.front(), sels.back(), true); + adjoin_lines(c, true); } - }; diff --git a/aegisub/src/subs_grid.cpp b/aegisub/src/subs_grid.cpp index f4fef7f33..a79bcb2b3 100644 --- a/aegisub/src/subs_grid.cpp +++ b/aegisub/src/subs_grid.cpp @@ -151,39 +151,3 @@ void SubtitlesGrid::RecombineLines() { context->ass->Commit(_("combining"), AssFile::COMMIT_DIAG_ADDREM | AssFile::COMMIT_DIAG_FULL); } - -void SubtitlesGrid::AdjoinLines(int n1,int n2,bool setStart) { - if (n1 == n2) { - if (setStart) { - --n1; - } - else { - ++n2; - } - } - // Set start - if (setStart) { - AssDialogue *prev = GetDialogue(n1); - AssDialogue *cur; - for (int i=n1+1;i<=n2;i++) { - cur = GetDialogue(i); - if (!cur) return; - cur->Start = prev->End; - prev = cur; - } - } - - // Set end - else { - AssDialogue *next; - AssDialogue *cur = GetDialogue(n1); - for (int i=n1;iEnd = next->Start; - cur = next; - } - } - - context->ass->Commit(_("adjoin"), AssFile::COMMIT_DIAG_TIME); -} diff --git a/aegisub/src/subs_grid.h b/aegisub/src/subs_grid.h index 93f7b3449..33632b94e 100644 --- a/aegisub/src/subs_grid.h +++ b/aegisub/src/subs_grid.h @@ -38,11 +38,5 @@ class SubtitlesGrid: public BaseGrid { public: SubtitlesGrid(wxWindow *parent, agi::Context *context); - /// @brief Adjoins selected lines, setting each line's start time to the previous line's end time - /// @param n1 First line to adjoin - /// @param n2 Last line to adjoin - /// @param setStart Set the start times (rather than end times) - void AdjoinLines(int first,int last,bool setStart); - void RecombineLines(); };