CodeWalker/Project/UndoStep.cs

655 lines
18 KiB
C#
Raw Normal View History

2017-09-21 18:33:05 +08:00
using CodeWalker.GameFiles;
using CodeWalker.World;
using SharpDX;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeWalker.Project
{
public abstract class UndoStep
{
//revert the object to the state marked at the start of this step
2017-09-26 08:27:18 +08:00
public abstract void Undo(WorldForm wf, ref MapSelection sel);
2017-09-21 18:33:05 +08:00
//revert the object to the state marked at the end of this step
2017-09-26 08:27:18 +08:00
public abstract void Redo(WorldForm wf, ref MapSelection sel);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public class MultiPositionUndoStep : UndoStep
{
private MapSelection Selection;
private MapSelection[] Items;
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
public MultiPositionUndoStep(MapSelection multiSel, MapSelection[] items, Vector3 startpos, WorldForm wf)
2017-09-26 08:27:18 +08:00
{
Selection = multiSel;
Items = items;
StartPosition = startpos;
EndPosition = multiSel.WidgetPosition;
UpdateGraphics(wf);
2017-09-26 08:27:18 +08:00
}
private void Update(WorldForm wf, ref MapSelection sel, Vector3 p, Vector3 o)
{
//update selection items positions for new widget position p
Vector3 dp = p - o;
for (int i = 0; i < Items.Length; i++)
{
var refpos = Items[i].WidgetPosition;
Items[i].SetPosition(refpos + dp, refpos, false);
}
sel.MultipleSelectionCenter = p; //center used for widget pos...
wf.SelectMulti(Items);
wf.SetWidgetPosition(p);
UpdateGraphics(wf);
}
private void UpdateGraphics(WorldForm wf)
{
Dictionary<YndFile, int> pathYnds = new Dictionary<YndFile, int>();
Dictionary<TrainTrack, int> trainTracks = new Dictionary<TrainTrack, int>();
Dictionary<YmtFile, int> scenarioYmts = new Dictionary<YmtFile, int>();
if (Items != null)
{
foreach (var item in Items)
{
if (item.PathNode != null)
{
pathYnds[item.PathNode.Ynd] = 1;
}
if (item.TrainTrackNode != null)
{
trainTracks[item.TrainTrackNode.Track] = 1;
}
if (item.ScenarioNode != null)
{
scenarioYmts[item.ScenarioNode.Ymt] = 1;
}
}
}
foreach (var kvp in pathYnds)
{
wf.UpdatePathYndGraphics(kvp.Key, true);
}
foreach (var kvp in trainTracks)
{
wf.UpdateTrainTrackGraphics(kvp.Key, false);
}
foreach (var kvp in scenarioYmts)
{
wf.UpdateScenarioGraphics(kvp.Key, false);
}
2017-09-26 08:27:18 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
{
Update(wf, ref sel, StartPosition, EndPosition);
}
public override void Redo(WorldForm wf, ref MapSelection sel)
{
Update(wf, ref sel, EndPosition, StartPosition);
}
public override string ToString()
{
return (Items?.Length ?? 0).ToString() + " items: Position";
}
}
2017-09-21 18:33:05 +08:00
public class EntityPositionUndoStep : UndoStep
{
public YmapEntityDef Entity { get; set; }
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
public EntityPositionUndoStep(YmapEntityDef ent, Vector3 startpos)
{
Entity = ent;
StartPosition = startpos;
EndPosition = ent?.WidgetPosition ?? Vector3.Zero;
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 p)
2017-09-21 18:33:05 +08:00
{
Entity?.SetPositionFromWidget(p);
if (Entity != sel.EntityDef) wf.SelectEntity(Entity);
wf.SetWidgetPosition(Entity.WidgetPosition);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartPosition);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndPosition);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return (Entity?._CEntityDef.archetypeName.ToString() ?? "") + ": Position";
}
}
public class EntityRotationUndoStep : UndoStep
{
public YmapEntityDef Entity { get; set; }
public Quaternion StartRotation { get; set; }
public Quaternion EndRotation { get; set; }
public EntityRotationUndoStep(YmapEntityDef ent, Quaternion startrot)
{
Entity = ent;
StartRotation = startrot;
EndRotation = ent?.WidgetOrientation ?? Quaternion.Identity;
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Quaternion q)
2017-09-21 18:33:05 +08:00
{
Entity?.SetOrientationFromWidget(q);
if (Entity != sel.EntityDef) wf.SelectEntity(Entity);
wf.SetWidgetRotation(q);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartRotation);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndRotation);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return (Entity?._CEntityDef.archetypeName.ToString() ?? "") + ": Rotation";
}
}
public class EntityScaleUndoStep : UndoStep
{
public YmapEntityDef Entity { get; set; }
public Vector3 StartScale { get; set; }
public Vector3 EndScale { get; set; }
public EntityScaleUndoStep(YmapEntityDef ent, Vector3 startscale)
{
Entity = ent;
StartScale = startscale;
EndScale = ent?.Scale ?? Vector3.One;
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 s)
2017-09-21 18:33:05 +08:00
{
Entity?.SetScale(s);
if (Entity != sel.EntityDef) wf.SelectEntity(Entity);
wf.SetWidgetScale(s);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartScale);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndScale);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return (Entity?._CEntityDef.archetypeName.ToString() ?? "") + ": Scale";
}
}
public class EntityPivotPositionUndoStep : UndoStep
{
public YmapEntityDef Entity { get; set; }
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
public EntityPivotPositionUndoStep(YmapEntityDef ent, Vector3 startpos)
{
Entity = ent;
StartPosition = startpos;
EndPosition = ent?.WidgetPosition ?? Vector3.Zero;
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 p)
2017-09-21 18:33:05 +08:00
{
Entity?.SetPivotPositionFromWidget(p);
if (Entity != sel.EntityDef) wf.SelectEntity(Entity);
wf.SetWidgetPosition(p);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartPosition);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndPosition);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return (Entity?._CEntityDef.archetypeName.ToString() ?? "") + ": Pivot Position";
}
}
public class EntityPivotRotationUndoStep : UndoStep
{
public YmapEntityDef Entity { get; set; }
public Quaternion StartRotation { get; set; }
public Quaternion EndRotation { get; set; }
public EntityPivotRotationUndoStep(YmapEntityDef ent, Quaternion startrot)
{
Entity = ent;
StartRotation = startrot;
EndRotation = ent?.WidgetOrientation ?? Quaternion.Identity;
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Quaternion q)
2017-09-21 18:33:05 +08:00
{
Entity?.SetPivotOrientationFromWidget(q);
if (Entity != sel.EntityDef) wf.SelectEntity(Entity);
wf.SetWidgetRotation(q);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartRotation);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndRotation);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return (Entity?._CEntityDef.archetypeName.ToString() ?? "") + ": Pivot Rotation";
}
}
public class CarGenPositionUndoStep : UndoStep
{
public YmapCarGen CarGen { get; set; }
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
public CarGenPositionUndoStep(YmapCarGen cargen, Vector3 startpos)
{
CarGen = cargen;
StartPosition = startpos;
EndPosition = cargen?.Position ?? Vector3.Zero;
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 p)
2017-09-21 18:33:05 +08:00
{
CarGen?.SetPosition(p);
if (CarGen != sel.CarGenerator) wf.SelectCarGen(CarGen);
wf.SetWidgetPosition(p);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartPosition);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndPosition);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return "CarGen " + (CarGen?._CCarGen.carModel.ToString() ?? "") + ": Position";
}
}
public class CarGenRotationUndoStep : UndoStep
{
public YmapCarGen CarGen { get; set; }
public Quaternion StartRotation { get; set; }
public Quaternion EndRotation { get; set; }
public CarGenRotationUndoStep(YmapCarGen cargen, Quaternion startrot)
{
CarGen = cargen;
StartRotation = startrot;
EndRotation = cargen?.Orientation ?? Quaternion.Identity;
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Quaternion q)
2017-09-21 18:33:05 +08:00
{
CarGen?.SetOrientation(q);
if (CarGen != sel.CarGenerator) wf.SelectCarGen(CarGen);
wf.SetWidgetRotation(q);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartRotation);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndRotation);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return "CarGen " + (CarGen?._CCarGen.carModel.ToString() ?? "") + ": Rotation";
}
}
public class CarGenScaleUndoStep : UndoStep
{
public YmapCarGen CarGen { get; set; }
public Vector3 StartScale { get; set; }
public Vector3 EndScale { get; set; }
public CarGenScaleUndoStep(YmapCarGen cargen, Vector3 startscale)
{
CarGen = cargen;
StartScale = startscale;
EndScale = new Vector3(cargen?._CCarGen.perpendicularLength ?? 1.0f);
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 s)
2017-09-21 18:33:05 +08:00
{
CarGen?.SetScale(s);
if (CarGen != sel.CarGenerator) wf.SelectCarGen(CarGen);
wf.SetWidgetScale(s);
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartScale);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndScale);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return "CarGen " + (CarGen?._CCarGen.carModel.ToString() ?? "") + ": Scale";
}
}
public class PathNodePositionUndoStep : UndoStep
{
public YndNode PathNode { get; set; }
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
2017-09-26 08:27:18 +08:00
public PathNodePositionUndoStep(YndNode pathnode, Vector3 startpos, WorldForm wf)
2017-09-21 18:33:05 +08:00
{
PathNode = pathnode;
StartPosition = startpos;
EndPosition = pathnode?.Position ?? Vector3.Zero;
2017-09-26 08:27:18 +08:00
UpdateGraphics(wf); //forces the update of the path graphics when it's moved...
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 p)
2017-09-21 18:33:05 +08:00
{
PathNode?.SetPosition(p);
if (PathNode != sel.PathNode)
{
if (sel.PathLink != null)
{
wf.SelectPathLink(sel.PathLink);
}
else
{
wf.SelectPathNode(PathNode);
}
}
wf.SetWidgetPosition(p);
2017-09-26 08:27:18 +08:00
UpdateGraphics(wf);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void UpdateGraphics(WorldForm wf)
2017-09-21 18:33:05 +08:00
{
if (PathNode != null)
{
//Ynd graphics needs to be updated.....
wf.UpdatePathNodeGraphics(PathNode, false);
}
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartPosition);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndPosition);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return "PathNode " + (PathNode?._RawData.ToString() ?? "") + ": Position";
}
}
public class TrainTrackNodePositionUndoStep : UndoStep
{
public TrainTrackNode Node { get; set; }
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
2017-09-26 08:27:18 +08:00
public TrainTrackNodePositionUndoStep(TrainTrackNode node, Vector3 startpos, WorldForm wf)
2017-09-21 18:33:05 +08:00
{
Node = node;
StartPosition = startpos;
EndPosition = node?.Position ?? Vector3.Zero;
2017-09-26 08:27:18 +08:00
UpdateGraphics(wf); //forces the update of the path graphics when it's moved...
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 p)
2017-09-21 18:33:05 +08:00
{
Node?.SetPosition(p);
if (Node != sel.TrainTrackNode)
{
wf.SelectTrainTrackNode(Node);
}
wf.SetWidgetPosition(p);
2017-09-26 08:27:18 +08:00
UpdateGraphics(wf);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void UpdateGraphics(WorldForm wf)
2017-09-21 18:33:05 +08:00
{
if (Node != null)
{
//Ynd graphics needs to be updated.....
wf.UpdateTrainTrackNodeGraphics(Node, false);
}
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartPosition);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndPosition);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return "TrainTrackNode " + (Node?.ToString() ?? "") + ": Position";
}
}
public class ScenarioNodePositionUndoStep : UndoStep
{
public ScenarioNode ScenarioNode { get; set; }
public Vector3 StartPosition { get; set; }
public Vector3 EndPosition { get; set; }
2017-09-26 08:27:18 +08:00
public ScenarioNodePositionUndoStep(ScenarioNode node, Vector3 startpos, WorldForm wf)
2017-09-21 18:33:05 +08:00
{
ScenarioNode = node;
StartPosition = startpos;
EndPosition = node?.Position ?? Vector3.Zero;
2017-09-26 08:27:18 +08:00
UpdateGraphics(wf); //forces the update of the path graphics when it's moved...
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Vector3 p)
2017-09-21 18:33:05 +08:00
{
ScenarioNode?.SetPosition(p);
if (ScenarioNode != sel.ScenarioNode) wf.SelectScenarioNode(ScenarioNode);
wf.SetWidgetPosition(p);
2017-09-26 08:27:18 +08:00
UpdateGraphics(wf);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void UpdateGraphics(WorldForm wf)
2017-09-21 18:33:05 +08:00
{
if (ScenarioNode != null)
{
//Ymt graphics needs to be updated.....
wf.UpdateScenarioGraphics(ScenarioNode.Ymt, false);
}
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartPosition);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndPosition);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return ScenarioNode.ToString() + ": Position";
}
}
public class ScenarioNodeRotationUndoStep : UndoStep
{
public ScenarioNode ScenarioNode { get; set; }
public Quaternion StartRotation { get; set; }
public Quaternion EndRotation { get; set; }
2017-09-26 08:27:18 +08:00
public ScenarioNodeRotationUndoStep(ScenarioNode node, Quaternion startrot, WorldForm wf)
2017-09-21 18:33:05 +08:00
{
ScenarioNode = node;
StartRotation = startrot;
EndRotation = node?.Orientation ?? Quaternion.Identity;
2017-09-26 08:27:18 +08:00
//UpdateGraphics(wf);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void Update(WorldForm wf, ref MapSelection sel, Quaternion q)
2017-09-21 18:33:05 +08:00
{
ScenarioNode?.SetOrientation(q);
if (ScenarioNode != sel.ScenarioNode) wf.SelectScenarioNode(ScenarioNode);
wf.SetWidgetRotation(q);
2017-09-26 08:27:18 +08:00
//UpdateGraphics(wf);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
private void UpdateGraphics(WorldForm wf)
2017-09-21 18:33:05 +08:00
{
////this function shouldn't actually be needed for rotating...
//if (ScenarioNode != null)
//{
// //Ymt graphics needs to be updated.....
// wf.UpdateScenarioGraphics(ScenarioNode.Ymt, false);
//}
}
2017-09-26 08:27:18 +08:00
public override void Undo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, StartRotation);
2017-09-21 18:33:05 +08:00
}
2017-09-26 08:27:18 +08:00
public override void Redo(WorldForm wf, ref MapSelection sel)
2017-09-21 18:33:05 +08:00
{
2017-09-26 08:27:18 +08:00
Update(wf, ref sel, EndRotation);
2017-09-21 18:33:05 +08:00
}
public override string ToString()
{
return ScenarioNode.ToString() + ": Rotation";
}
}
}