/// <summary> /// Records the given operation /// </summary> private static void RecordOperation (UndoProRecord operation) { // First, make sure the internal records representation is updated UpdateUndoRecords (); // Make sure this record isn't included in the previous group Undo.IncrementCurrentGroup (); // Create a dummy record with the given label if (dummyObject == null) dummyObject = new Texture2D (8, 8); Undo.RegisterCompleteObjectUndo (dummyObject, operation.label); // Make sure future undo records are not included into this group Undo.FlushUndoRecordObjects (); Undo.IncrementCurrentGroup (); // Now get the new Undo state records.undoState = FetchUndoState (); // Record operation internally records.UndoRecordsAdded (1); records.undoProRecords.Add (operation); if (OnAddUndoRecord != null) OnAddUndoRecord.Invoke (new string[] { operation.label }, true); }
/// <summary> /// Updates the internal record-stack accounting for the given amount of default undo entries /// </summary> public void UndoRecordsAdded(int addedRecordsCount) { if (undoState.redoRecords.Count == 0) { ClearRedo(); } //Debug.Log ("Shifted records by " + addedRecordsCount + " because of new added undo records!"); for (int recCnt = 0; recCnt < undoProRecords.Count; recCnt++) { UndoProRecord record = undoProRecords[recCnt]; if (record.relativeStackPos <= 0) { record.relativeStackPos -= addedRecordsCount; } } }
/// <summary> /// Updates onternal records to represent the undo/redo operation represented by a opShift (negative: Undo; positive: Redo); /// Returns the records which are affected by this undo/redo operation (switched from Undo-stack to redo or vice versa) /// </summary> public List <UndoProRecord> PerformOperationInternal(int opShift, int anomalyAddedCount) { if (opShift == 0) { return(new List <UndoProRecord> ()); } // Anomaly bool opDir = opShift <= 0; //if (anomalyAddedCount < 0) // opDir = !opDir; int anomalyShift = (opDir? anomalyAddedCount : -anomalyAddedCount); #if UNDO_DEBUG if (anomalyAddedCount != 0) { Debug.Log("Anomaly of " + anomalyAddedCount + "; OpDir: " + opDir + "; Shift: " + anomalyShift); } #endif List <UndoProRecord> operatedRecords = new List <UndoProRecord> (); for (int recCnt = 0; recCnt < undoProRecords.Count; recCnt++) { UndoProRecord record = undoProRecords[recCnt]; int prevInd = record.relativeStackPos; record.relativeStackPos -= opShift; if (anomalyAddedCount != 0 && isUndo(prevInd) != opDir) // Anomaly { record.relativeStackPos += anomalyShift; } if (isUndo(prevInd) != isUndo(record.relativeStackPos)) // affected by this undo/redo operation { operatedRecords.Add(record); } } return(operatedRecords); }