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