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. /// the different versions of the file.
int Id; int Id;
int Row = -1;
/// Is this a comment line? /// Is this a comment line?
bool Comment = false; bool Comment = false;
/// Layer number /// 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) { 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}); PushState({desc, &amend_id, single_line});
std::set<const AssDialogue*> changed_lines; std::set<const AssDialogue*> changed_lines;

View file

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

View file

@ -34,7 +34,6 @@
#include <libaegisub/signal.h> #include <libaegisub/signal.h>
#include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <wx/window.h> #include <wx/window.h>
@ -59,7 +58,6 @@ class BaseGrid final : public wxWindow {
int extendRow = -1; int extendRow = -1;
std::vector<AssDialogue*> index_line_map; ///< Row number -> dialogue line 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 /// Connection for video seek event. Stored explicitly so that it can be
/// blocked if the relevant option is disabled /// 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 /// @return Subtitle dialogue line for index, or 0 if invalid index
AssDialogue *GetDialogue(int n) const; 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: public:
BaseGrid(wxWindow* parent, agi::Context *context); BaseGrid(wxWindow* parent, agi::Context *context);
~BaseGrid(); ~BaseGrid();