Advertisement
jwow22

Command

Jan 5th, 2022
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5.  
  6. public enum CommandType
  7. {
  8.     None, Position, Create, Delete
  9. }
  10.  
  11. public enum BehaviourType
  12. {
  13.     KeyDown, KeyUp, Hold
  14. }
  15.  
  16. [Serializable]
  17. public class Control
  18. {
  19.     public KeyCode Key;
  20.     public BehaviourType Behaviour;
  21.     public UnityEvent Event;
  22.  
  23.     public Control(KeyCode key, BehaviourType behaviourType = BehaviourType.KeyDown)
  24.     {
  25.         Key = key;
  26.         Event = new UnityEvent();
  27.         Behaviour = behaviourType;
  28.     }
  29.  
  30.     public void AddAction(UnityAction call)
  31.     {
  32.         Event.AddListener(call);
  33.     }
  34. }
  35. public class Command
  36. {
  37.     public CommandType Type;
  38.     public CommandHandler CommandHandler;
  39.  
  40.     public virtual void Execute() { }
  41.  
  42.     public virtual void Undo() { }
  43. }
  44.  
  45. public class RecordPositionCommand : Command
  46. {
  47.     private readonly Dictionary<Vertex, Vector3> _vertexPositions;
  48.     private readonly List<Vertex> _vertices;
  49.  
  50.     public RecordPositionCommand(List<Vertex> vertices)
  51.     {
  52.         _vertices = vertices;
  53.         _vertexPositions = new Dictionary<Vertex, Vector3>();
  54.  
  55.         // Uncomment for debugging
  56.         Type = CommandType.Position;
  57.     }
  58.  
  59.     public override void Execute()
  60.     {
  61.         foreach (Vertex vertex in _vertices)
  62.         {
  63.             _vertexPositions.Add(vertex, vertex.Position);
  64.         }
  65.         CommandHandler.CommandStack.Push(this);
  66.     }
  67.  
  68.     public override void Undo()
  69.     {
  70.         foreach (var kvp in _vertexPositions)
  71.         {
  72.             kvp.Key.MoveTo(kvp.Value);
  73.         }
  74.     }
  75. }
  76.  
  77. public class DeleteVertexCommand : Command
  78. {
  79.     private Vertex _vertex;
  80.     private List<Vertex> _connectedVertices;
  81.  
  82.     public override void Execute()
  83.     {
  84.         EditorController editorController = EditorController.Instance;
  85.         if (!editorController.SelectedVertex())
  86.         {
  87.             return;
  88.         }
  89.         _vertex = editorController.SelectedVertices[0];
  90.         _connectedVertices = _vertex.GetConnectedVertices();
  91.         foreach (Vertex vertex in editorController.SelectedVertices)
  92.         {
  93.             GraphManager.Instance.Graph.RemoveVertex(vertex);
  94.         }
  95.         editorController.DeselectAll();
  96.         MouseController.Instance.ResetStates();
  97.        
  98.         Type = CommandType.Delete;
  99.         CommandHandler.CommandStack.Push(this);
  100.     }
  101.  
  102.     public override void Undo()
  103.     {
  104.         Vertex oldVertex = GraphManager.Instance.Graph.AddVertex(_vertex);
  105.         foreach (Vertex vertex in _connectedVertices)
  106.         {
  107.             GraphManager.Instance.Graph.AddEdge(vertex, oldVertex);
  108.         }
  109.     }
  110. }
  111.  
  112. public class CreateVertexCommand : Command
  113. {
  114.     private List<Vertex> _vertices;
  115.  
  116.     public override void Execute()
  117.     {
  118.         EditorController editorController = EditorController.Instance;
  119.  
  120.         if (!editorController.SelectedVertex()) { return; }
  121.         List<Vertex> addedVertex = new List<Vertex>();
  122.         _vertices = addedVertex;
  123.        
  124.         foreach (Vertex vertex in editorController.SelectedVertices)
  125.         {
  126.             Vertex newVertex = GraphManager.Instance.Graph.AddConnectedVertex(vertex, new Vertex(vertex.Position));
  127.             addedVertex.Add(newVertex);
  128.         }
  129.  
  130.         editorController.DeselectAll();
  131.         foreach (Vertex vertex in addedVertex)
  132.         {
  133.             vertex.Selectable.OnSelect();
  134.         }
  135.        
  136.         CommandHandler.CommandStack.Push(this);
  137.         Type = CommandType.Create;
  138.     }
  139.  
  140.     public override void Undo()
  141.     {
  142.         foreach (Vertex vertex in _vertices)
  143.         {
  144.             GraphManager.Instance.Graph.RemoveVertex(vertex);
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement