Add the line's row number to AssDialogue

The grid needs to be able to map AssDialogue * to a row number, and just
storing it in the AssDialogue is nearly strictly better than a std::map
mapping events to rows. Probably will be of use elsewhere as well.
This commit is contained in:
Thomas Goyne 2014-03-31 07:41:57 -07:00
parent 84c5eb25b3
commit 3bbbc56053
4 changed files with 12 additions and 18 deletions

View File

@ -130,6 +130,8 @@ struct AssDialogueBase {
/// the different versions of the file.
int Id;
int Row = -1;
/// Is this a comment line?
bool Comment = false;
/// Layer number

View File

@ -164,6 +164,12 @@ AssStyle *AssFile::GetStyle(std::string const& name) {
}
int AssFile::Commit(wxString const& desc, int type, int amend_id, AssDialogue *single_line) {
if (type == COMMIT_NEW || (type & COMMIT_DIAG_ADDREM)) {
int i = 0;
for (auto& event : Events)
event.Row = i++;
}
PushState({desc, &amend_id, single_line});
std::set<const AssDialogue*> changed_lines;

View File

@ -228,12 +228,9 @@ void BaseGrid::UpdateStyle() {
void BaseGrid::UpdateMaps() {
index_line_map.clear();
line_index_map.clear();
for (auto& curdiag : context->ass->Events) {
line_index_map[&curdiag] = (int)index_line_map.size();
for (auto& curdiag : context->ass->Events)
index_line_map.push_back(&curdiag);
}
SetColumnWidths();
AdjustScrollbar();
@ -242,7 +239,7 @@ void BaseGrid::UpdateMaps() {
void BaseGrid::OnActiveLineChanged(AssDialogue *new_active) {
if (new_active) {
int row = GetDialogueIndex(new_active);
int row = new_active->Row;
MakeRowVisible(row);
extendRow = row;
Refresh(false);
@ -792,11 +789,6 @@ AssDialogue *BaseGrid::GetDialogue(int n) const {
return index_line_map[n];
}
int BaseGrid::GetDialogueIndex(AssDialogue *diag) const {
auto it = line_index_map.find(diag);
return it != line_index_map.end() ? it->second : -1;
}
bool BaseGrid::IsDisplayed(const AssDialogue *line) const {
if (!context->videoController->IsLoaded()) return false;
int frame = context->videoController->GetFrameN();
@ -856,8 +848,9 @@ void BaseGrid::OnKeyDown(wxKeyEvent &event) {
return;
}
auto active_line = context->selectionController->GetActiveLine();
int old_extend = extendRow;
int next = mid(0, GetDialogueIndex(context->selectionController->GetActiveLine()) + dir * step, GetRows() - 1);
int next = mid(0, (active_line ? active_line->Row : 0) + dir * step, GetRows() - 1);
context->selectionController->SetActiveLine(GetDialogue(next));
// Move selection

View File

@ -34,7 +34,6 @@
#include <libaegisub/signal.h>
#include <map>
#include <memory>
#include <vector>
#include <wx/window.h>
@ -59,7 +58,6 @@ class BaseGrid final : public wxWindow {
int extendRow = -1;
std::vector<AssDialogue*> index_line_map; ///< Row number -> dialogue line
std::map<AssDialogue*,int> line_index_map; ///< Dialogue line -> row number
/// Connection for video seek event. Stored explicitly so that it can be
/// blocked if the relevant option is disabled
@ -120,11 +118,6 @@ class BaseGrid final : public wxWindow {
/// @return Subtitle dialogue line for index, or 0 if invalid index
AssDialogue *GetDialogue(int n) const;
/// @brief Get index by dialogue line
/// @param diag Dialogue line to look up
/// @return Subtitle index for object, or -1 if unknown subtitle
int GetDialogueIndex(AssDialogue *diag) const;
public:
BaseGrid(wxWindow* parent, agi::Context *context);
~BaseGrid();