-1

I am implementing the system through inheritance, but there is an ambiguous part.

Parent class undostate child class recordstate

I made an undoredo function. And through this function, we are making a recording function. However, I want to make this function through inheritance. Because through undoredo, it seems that you can distinguish each action unit.

undoredo was implemented through the stack, but the recording function is similarly implemented by creating a record that inherits undoredo (the difference is to use a list).

The important thing here is that clicking the mouse creates an action But with the recordstate entering the record

Because I don't want to make two undoredostates entering undoredo (undoredo is the parent object of record)

I want to create and use recordstate, an object type that can be used in both places.

  1. The question is, is it really a weird idea to try to put the same thing on the parent class's stack into the childclass list
  2. Are there any design patterns associated with this?
public class UndoState 
{
    public StateNum state=StateNum.Selected;

    public List<int> listSelectedObj;
    public List<int> listFade;
    public List<Material> listMaterial;
    public bool boodoubleState = false;

    public int numChangeSet ;        
   
    public UndoState(StateNum st)
    {
        state = st;
    }
    public UndoState(StateNum st, int num)
    {
        state = st;
        numChangeSet = num;
    }
   
    public UndoState(StateNum st, List<int> SelectedObjMouse, 
        List<int> listFades, List<Material> listMaterials, bool next)
    {
        state = st;
        listSelectedObj = SelectedObjMouse;
        listFade = listFades;
        listMaterial = listMaterials;

        boodoubleState = next;
    }
  
    public UndoState() { } 
    
    public UndoState(StateNum st,List<int> SelectedObjMouse, bool next) 
    {
        state = st;
        boodoubleState =  next;
        listSelectedObj = SelectedObjMouse;
    }
    public UndoState(StateNum st, List<int> SelectedObjMouse,
        List<int> fadeList,bool next)
    {
        state = st;
        listSelectedObj = SelectedObjMouse;
        listFade = fadeList;
        boodoubleState = next;
    }
   
    
    
    
}


i making this cs

public class RecordUndoRedo : UndoRedo
{
    public List<RecordState> recordUndoStates=new List<RecordState>();
    SceneData sceneData;

    void Start()
    {
        sceneData = new SceneData();
        videoScene = Camera.main.transform.parent.GetComponent<VideoScene>();
        RecordState startState = new RecordState();
     
    }
    
    
}
public class UndoRedo :MonoBehaviour
{
    public Stack<UndoState> undoStates =new Stack<UndoState>();
    public Stack<UndoState> redoStates =new Stack<UndoState>();

    protected VideoScene videoScene;

    void Start()
    {
        videoScene = Camera.main.transform.parent.GetComponent<VideoScene>();

        UndoState nullStat = new UndoState();
        undoStates.Push(nullStat);
        callCommandUi =transform.GetComponent<CallCommandUi>();
        
    }
    public void AddState(UndoState state)
    {
       
        if (undoStates.Count == 20)
        {
            for (int i = 0; i < 20; i++)
            {
                redoStates.Push(undoStates.Pop());
            }
            for (int i = 1; i < 20; i++)
            {
                undoStates.Push(redoStates.Pop());
            }
        }
        undoStates.Push(state);
        redoStates.Clear();
    }
}


public class RecordState : UndoState
{
    public MemoryStream mouseVector3;

    public RecordState() { }
    public RecordState(StateNum stateNum)
    {
        state = stateNum;
    }
    public RecordState(StateNum stateNum,MemoryStream memorySt)
    {
        state = stateNum;
        mouseVector3 = memorySt;
    }
    public RecordState(StateNum st, int num)
    {
        state = st;
        numChangeSet = num;
    }

    public RecordState(StateNum st, List<int> SelectedObjMouse,
        List<int> listFades, List<Material> listMaterials, bool next)
    {
        state = st;
        listSelectedObj = SelectedObjMouse;
        listFade = listFades;
        listMaterial = listMaterials;

        boodoubleState = next;
    }

    public RecordState(StateNum st, List<int> SelectedObjMouse, bool next)
    {
        state = st;
        boodoubleState = next;
        listSelectedObj = SelectedObjMouse;
    }
    public RecordState(StateNum st, List<int> SelectedObjMouse,
        List<int> fadeList, bool next)
    {
        state = st;
        listSelectedObj = SelectedObjMouse;
        listFade = fadeList;
        boodoubleState = next;
    }
}

example add how to call add state

if (Input.GetMouseButtonDown(2) && videoScene.booRecordOn)
        {
            RecordState recordUndoRedo 
                = new RecordState(StateNum.MouseRot);
            undoRedo.AddState(recordUndoRedo);
        }

1 Answers1

2

Can't really comment on the code without actually seeing it, but from what you describe, maybe inheritance isn't the way to go at all. So for the second part of your question, here goes.

Check out the Memento design pattern:

Memento is a behavioral design pattern that lets you save and restore the previous state of an object without revealing the details of its implementation.

Alongside with the command pattern, you can achieve the undo redo functionality.

You can use Command and Memento together when implementing “undo”. In this case, commands are responsible for performing various operations over a target object, while mementos save the state of that object just before a command gets executed.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61