Wed, 10 Apr 2013 03:47:17 +0300
History handling for list moving
gui_editactions.cpp | file | annotate | diff | comparison | revisions | |
history.cpp | file | annotate | diff | comparison | revisions | |
history.h | file | annotate | diff | comparison | revisions | |
ldtypes.cpp | file | annotate | diff | comparison | revisions | |
ldtypes.h | file | annotate | diff | comparison | revisions |
--- a/gui_editactions.cpp Wed Apr 10 03:30:58 2013 +0300 +++ b/gui_editactions.cpp Wed Apr 10 03:47:17 2013 +0300 @@ -285,30 +285,13 @@ static void doMoveSelection (const bool bUp) { vector<LDObject*> objs = g_ForgeWindow->getSelectedObjects (); - // If we move down, we need to iterate the array in reverse order. - const long start = bUp ? 0 : (objs.size() - 1); - const long end = bUp ? objs.size() : -1; - const long incr = bUp ? 1 : -1; + // Get the indices of the objects for history archival + vector<ulong> ulaIndices; + for (LDObject* obj : objs) + ulaIndices.push_back (obj->getIndex (g_CurrentFile)); - for (long i = start; i != end; i += incr) { - LDObject* obj = objs[i]; - - const long lIndex = obj->getIndex (g_CurrentFile), - lTarget = lIndex + (bUp ? -1 : 1); - - if ((bUp == true and lIndex == 0) or - (bUp == false and lIndex == (long)(g_CurrentFile->objects.size() - 1))) - { - // One of the objects hit the extrema. If this happens, this should be the first - // object to be iterated on. Thus, nothing has changed yet and it's safe to just - // abort the entire operation. - assert (i == start); - return; - } - - obj->swap (g_CurrentFile->objects[lTarget]); - } - + LDObject::moveObjects (objs, bUp); + History::addEntry (new ListMoveHistory (ulaIndices, bUp)); g_ForgeWindow->buildObjList (); }
--- a/history.cpp Wed Apr 10 03:30:58 2013 +0300 +++ b/history.cpp Wed Apr 10 03:47:17 2013 +0300 @@ -25,6 +25,44 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= +namespace History { + std::vector<HistoryEntry*> entries; + + static long lPos = -1; + + // ========================================================================= + void addEntry (HistoryEntry* entry) { + // If there's any entries after our current position, we need to remove them now + for (ulong i = lPos + 1; i < entries.size(); ++i) { + + delete entries[i]; + entries.erase (entries.begin() + i); + } + + entries.push_back (entry); + lPos++; + } + + // ========================================================================= + void undo () { + if (lPos == -1) + return; // nothing to undo + + entries[lPos--]->undo (); + } + + // ========================================================================= + void redo () { + if (lPos == (long) entries.size () - 1) + return; // nothing to redo; + + entries[++lPos]->redo (); + } +} + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= void DeleteHistory::undo () { for (ulong i = 0; i < cache.size(); ++i) { LDObject* obj = cache[i]->clone (); @@ -96,37 +134,25 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -namespace History { - std::vector<HistoryEntry*> entries; +std::vector<LDObject*> ListMoveHistory::getObjects (short ofs) { + std::vector<LDObject*> objs; - static long lPos = -1; + for (ulong idx : ulaIndices) + objs.push_back (g_CurrentFile->objects[idx + ofs]); - // ========================================================================= - void addEntry (HistoryEntry* entry) { - // If there's any entries after our current position, we need to remove them now - for (ulong i = lPos + 1; i < entries.size(); ++i) { - - delete entries[i]; - entries.erase (entries.begin() + i); - } - - entries.push_back (entry); - lPos++; - } - - // ========================================================================= - void undo () { - if (lPos == -1) - return; // nothing to undo - - entries[lPos--]->undo (); - } - - // ========================================================================= - void redo () { - if (lPos == (long) entries.size () - 1) - return; // nothing to redo; - - entries[++lPos]->redo (); - } -} \ No newline at end of file + return objs; +} + +void ListMoveHistory::undo () { + std::vector<LDObject*> objs = getObjects (bUp ? -1 : 1); + LDObject::moveObjects (objs, !bUp); + g_ForgeWindow->buildObjList (); +} + +void ListMoveHistory::redo () { + std::vector<LDObject*> objs = getObjects (0); + LDObject::moveObjects (objs, bUp); + g_ForgeWindow->buildObjList (); +} + +ListMoveHistory::~ListMoveHistory() {} \ No newline at end of file
--- a/history.h Wed Apr 10 03:30:58 2013 +0300 +++ b/history.h Wed Apr 10 03:47:17 2013 +0300 @@ -22,18 +22,10 @@ #include "common.h" #define IMPLEMENT_HISTORY_TYPE(N) \ - virtual HistoryType_e getType () const { \ - return HISTORY_##N; \ - } \ virtual ~N##History (); \ virtual void undo (); \ virtual void redo (); -enum HistoryType_e { - HISTORY_Delete, - HISTORY_SetColor, - HISTORY_SetContents, -}; // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -43,10 +35,6 @@ virtual void undo () {} virtual void redo () {} virtual ~HistoryEntry () {} - - virtual HistoryType_e getType () const { - return (HistoryType_e)(0); - }; }; // ============================================================================= @@ -78,6 +66,9 @@ ulaIndices (ulaIndices), daColors (daColors), dNewColor (dNewColor) {} }; +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= class SetContentsHistory : public HistoryEntry { public: IMPLEMENT_HISTORY_TYPE (SetContents) @@ -92,6 +83,21 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= +class ListMoveHistory : public HistoryEntry { +public: + IMPLEMENT_HISTORY_TYPE (ListMove) + + std::vector<ulong> ulaIndices; + bool bUp; + + std::vector<LDObject*> getObjects (short ofs); + ListMoveHistory (vector<ulong> ulaIndices, const bool bUp) : + ulaIndices (ulaIndices), bUp (bUp) {} +}; + +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= namespace History { extern std::vector<HistoryEntry*> entries;
--- a/ldtypes.cpp Wed Apr 10 03:30:58 2013 +0300 +++ b/ldtypes.cpp Wed Apr 10 03:47:17 2013 +0300 @@ -393,4 +393,30 @@ return i; return -1; +} + +void LDObject::moveObjects (std::vector<LDObject*> objs, const bool bUp) { + // If we move down, we need to iterate the array in reverse order. + const long start = bUp ? 0 : (objs.size() - 1); + const long end = bUp ? objs.size() : -1; + const long incr = bUp ? 1 : -1; + + for (long i = start; i != end; i += incr) { + LDObject* obj = objs[i]; + + const long lIndex = obj->getIndex (g_CurrentFile), + lTarget = lIndex + (bUp ? -1 : 1); + + if ((bUp == true and lIndex == 0) or + (bUp == false and lIndex == (long)(g_CurrentFile->objects.size() - 1))) + { + // One of the objects hit the extrema. If this happens, this should be the first + // object to be iterated on. Thus, nothing has changed yet and it's safe to just + // abort the entire operation. + assert (i == start); + return; + } + + obj->swap (g_CurrentFile->objects[lTarget]); + } } \ No newline at end of file