-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work on the undo/redo system
- Loading branch information
Showing
12 changed files
with
396 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
namespace VTT.Network.UndoRedo | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using VTT.Control; | ||
using VTT.Network.Packet; | ||
|
||
public class ActionMemory | ||
{ | ||
private readonly List<ServerAction> _actions = new List<ServerAction>(); | ||
private readonly object _lock = new object(); | ||
private ServerClient _owner; | ||
|
||
public ActionMemory(ServerClient serverClient) => this._owner = serverClient; | ||
|
||
public Guid Owner { get; set; } | ||
public int ActionAmount => _actions.Count; | ||
public int CurrentIndex { get; set; } = -1; | ||
public int ActionBufferSize { get; set; } = 32; | ||
|
||
public void NewAction(ServerAction sa) | ||
{ | ||
if (sa is SmallChangeAction sca && _actions.Count > 0) | ||
{ | ||
lock (_lock) | ||
{ | ||
ServerAction csa = _actions[CurrentIndex]; | ||
if (csa is SmallChangeAction sccsa && sccsa.ActionType == sa.ActionType) | ||
{ | ||
if (sccsa.AcceptSmallChange(sca)) | ||
{ | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
lock (_lock) | ||
{ | ||
if (CurrentIndex < _actions.Count - 1) | ||
{ | ||
_actions.RemoveRange(CurrentIndex + 1, _actions.Count - CurrentIndex - 1); | ||
} | ||
|
||
if (this._actions.Count + 1 > this.ActionBufferSize) | ||
{ | ||
this._actions.RemoveAt(0); | ||
--this.CurrentIndex; | ||
} | ||
|
||
_actions.Add(sa); | ||
++CurrentIndex; | ||
} | ||
} | ||
|
||
public bool UndoAction() | ||
{ | ||
lock (_lock) | ||
{ | ||
if (_actions.Count > 0 && CurrentIndex >= 0) | ||
{ | ||
ServerAction sa = _actions[CurrentIndex]; | ||
sa.Undo(); | ||
--CurrentIndex; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
public bool RedoAction() | ||
{ | ||
lock (_lock) | ||
{ | ||
if (_actions.Count < CurrentIndex + 1) | ||
{ | ||
ServerAction sa = _actions[CurrentIndex + 1]; | ||
sa.Redo(); | ||
++CurrentIndex; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} | ||
|
||
public abstract class ServerAction | ||
{ | ||
public abstract ServerActionType ActionType { get; } | ||
|
||
public abstract void Undo(); | ||
public abstract void Redo(); | ||
|
||
public void SendToAllOnMap(Map cMap, PacketBase packet) => packet.Broadcast(x => x.ClientMapID.Equals(cMap.ID)); | ||
} | ||
|
||
public abstract class SmallChangeAction : ServerAction | ||
{ | ||
public DateTime LastModifyTime { get; set; } | ||
|
||
public abstract bool AcceptSmallChange(SmallChangeAction newAction); | ||
|
||
public bool CheckIfRecent(DateTime otherTime, int ms) => (otherTime - this.LastModifyTime).Milliseconds <= ms; | ||
} | ||
|
||
public enum ServerActionType | ||
{ | ||
Unknown, | ||
AddDrawing, | ||
AddTurnEntry, | ||
AuraAddOrRemove, | ||
AuraChange | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
namespace VTT.Network.UndoRedo | ||
{ | ||
using System; | ||
using VTT.Control; | ||
using VTT.Network.Packet; | ||
|
||
public class AddTurnEntryAction : ServerAction | ||
{ | ||
public override ServerActionType ActionType => ServerActionType.AddTurnEntry; | ||
public Guid EntryObjectID { get; set; } | ||
public int AdditionIndex { get; set; } | ||
public float NumericValue { get; set; } | ||
public string TeamName { get; set; } | ||
public int EntryIndex { get; set; } | ||
public Map Map { get; set; } | ||
|
||
public int SafeGetEntryAtIndex() | ||
{ | ||
lock (this.Map.TurnTracker.Lock) | ||
{ | ||
if (this.EntryIndex < 0 || this.EntryIndex >= this.Map.TurnTracker.Entries.Count) | ||
{ | ||
return -1; | ||
} | ||
|
||
return this.EntryIndex; | ||
} | ||
} | ||
|
||
public override void Redo() | ||
{ | ||
int e = this.SafeGetEntryAtIndex(); | ||
if (e == -1) | ||
{ | ||
TurnTracker.Team t = (string.IsNullOrEmpty(this.TeamName) ? this.Map.TurnTracker.Teams[0] : this.Map.TurnTracker.Teams.Find(x => x.Name.Equals(this.TeamName))) ?? this.Map.TurnTracker.Teams[0]; | ||
lock (this.Map.TurnTracker.Lock) | ||
{ | ||
this.Map.TurnTracker.Add(new TurnTracker.Entry() { NumericValue = this.NumericValue, ObjectID = this.EntryObjectID, Team = t }, this.AdditionIndex == -1 ? this.Map.TurnTracker.Entries.Count : this.AdditionIndex); | ||
} | ||
|
||
this.Map.NeedsSave = true; | ||
new PacketAddTurnEntry() { AdditionIndex = this.AdditionIndex, ObjectID = this.EntryObjectID, TeamName = this.TeamName, Value = this.NumericValue }.Broadcast(x => x.ClientMapID.Equals(this.Map.ID)); | ||
} | ||
} | ||
|
||
public override void Undo() | ||
{ | ||
int e = this.SafeGetEntryAtIndex(); | ||
if (e != -1) | ||
{ | ||
lock (this.Map.TurnTracker.Lock) | ||
{ | ||
this.Map.TurnTracker.Remove(e); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.