Skip to content

Commit

Permalink
Added code generation when scaling or pivoting from canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
tzachshabtay committed Jun 23, 2018
1 parent 7a483a0 commit d22a30d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void Visit()
float toX = _translateXOnDown + xDiff;
float toY = _translateYOnDown + yDiff;
MovePivotAction action = new MovePivotAction(handle.GetFriendlyName(), _image, _translate,
toPivotX, toPivotY, toX, toY);
toPivotX, toPivotY, toX, toY, _editor.Project.Model);
_actions.RecordAction(action);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using AGS.API;
using AGS.Engine;
using GuiLabs.Undo;
Expand Down Expand Up @@ -249,7 +250,10 @@ private void scale(float width, float height)
}
else h = heightCandidate;
}
ScaleAction action = new ScaleAction(handle.GetFriendlyName(), _scale, w, h);

PropertyInfo prop = _scale.GetType().GetProperty(nameof(IScaleComponent.Scale));
PointF toScale = (w / _scale.BaseSize.Width, h / _scale.BaseSize.Height);
PropertyAction action = new PropertyAction(new InspectorProperty(_scale, "Scale", prop), toScale, _editor.Project.Model);
_actions.RecordAction(action);
}

Expand Down
86 changes: 86 additions & 0 deletions Source/Editor/AGS.Editor/UndoRedo/ModelAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AGS.API;
using AGS.Engine;

namespace AGS.Editor
{
public class ModelAction
{
public static Action Execute(StateModel model, params (IComponent component, string propertyName, object value)[] properties)
{
if (properties.Length == 0 || properties[0].component?.Entity == null)
return null;

var entityModel = getEntity(properties[0].component.Entity, model);
bool wasDirty = entityModel.IsDirty;

var oldProperties = new List<(ComponentModel componentModel, string name, bool hadValue, object oldValue)>();
foreach ((IComponent component, string propertyName, object value) in properties)
{
var componentModel = entityModel.Components.GetOrAdd(component.RegistrationType, _ => new ComponentModel
{ ComponentConcreteType = component.GetType(), Properties = new Dictionary<string, object>() });

if (componentModel.Properties.TryGetValue(propertyName, out object oldValue))
{
oldProperties.Add((componentModel, propertyName, true, oldValue));
}
else
{
oldProperties.Add((componentModel, propertyName, false, oldValue));
}
componentModel.Properties[propertyName] = value;
entityModel.IsDirty = true;
}

Action undoModel = () =>
{
foreach ((ComponentModel componentModel, string name, bool hadValue, object oldValue) in oldProperties)
{
if (hadValue) componentModel.Properties[name] = oldValue;
else componentModel.Properties.Remove(name);
}
if (!wasDirty) entityModel.IsDirty = false;
};
return undoModel;
}

private static EntityModel getEntity(IEntity entity, StateModel model)
{
return model.Entities.GetOrAdd(entity.ID, id =>
{
var e = new EntityModel
{
ID = id,
DisplayName = entity.DisplayName,
EntityConcreteType = entity.GetType(),
Components = new Dictionary<Type, ComponentModel>()
};
var room = entity.GetComponent<IHasRoomComponent>()?.Room;
if (room != null)
{
var roomModel = getRoom(room, model);
if (roomModel.BackgroundEntity != entity.ID)
roomModel.Entities.Add(entity.ID);
}
else
{
model.Guis.Add(entity.ID);
}
return e;
});
}

private static RoomModel getRoom(IRoom room, StateModel model)
{
var roomModel = model.Rooms.FirstOrDefault(r => r.ID == room.ID);
if (roomModel == null)
{
roomModel = new RoomModel { ID = room.ID, BackgroundEntity = room.Background?.Entity?.ID, Entities = new HashSet<string>() };
model.Rooms.Add(roomModel);
}
return roomModel;
}
}
}
23 changes: 18 additions & 5 deletions Source/Editor/AGS.Editor/UndoRedo/MovePivotAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ public class MovePivotAction : AbstractAction
private DateTime _timestamp;
private readonly IImageComponent _image;
private readonly ITranslateComponent _translate;
private readonly StateModel _model;
private float _fromPivotX, _fromPivotY, _fromX, _fromY, _toPivotX, _toPivotY, _toX, _toY;
private Action _undoModel;

public MovePivotAction(string entityName, IImageComponent image, ITranslateComponent translate,
float toPivotX, float toPivotY, float toX, float toY)
float toPivotX, float toPivotY, float toX, float toY, StateModel model)
{
_model = model;
_timestamp = DateTime.Now;
_translate = translate;
_image = image;
Expand All @@ -34,14 +37,14 @@ protected override void ExecuteCore()
_fromPivotY = _image.Pivot.Y;
_fromX = _translate.X;
_fromY = _translate.Y;
_image.Pivot = new PointF(_toPivotX, _toPivotY);
_translate.Position = new Position(_toX, _toY);
execute();
}

protected override void UnExecuteCore()
{
_image.Pivot = new PointF(_fromPivotX, _fromPivotY);
_translate.Position = new Position(_fromX, _fromY);
_undoModel?.Invoke();
}

/// <summary>
Expand All @@ -60,8 +63,7 @@ public override bool TryToMerge(IAction followingAction)
_toPivotX = next._toPivotX;
_toPivotY = next._toPivotY;
_actionDisplayName = next._actionDisplayName;
_image.Pivot = new PointF(_toPivotX, _toPivotY);
_translate.Position = new Position(_toX, _toY);
execute();
return true;
}
return false;
Expand All @@ -72,5 +74,16 @@ private bool isRecent(MovePivotAction followingAction)
var timeDelta = followingAction._timestamp.Subtract(_timestamp);
return timeDelta.Seconds < 2;
}

private void execute()
{
var pivot = new PointF(_toPivotX, _toPivotY);
var position = new Position(_toX, _toY);
_image.Pivot = pivot;
_translate.Position = position;
ModelAction.Execute(_model,
(_image, nameof(IImageComponent.Pivot), pivot),
(_translate, nameof(ITranslateComponent.Position), position));
}
}
}
65 changes: 2 additions & 63 deletions Source/Editor/AGS.Editor/UndoRedo/PropertyAction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AGS.API;
using AGS.Engine;
using GuiLabs.Undo;

namespace AGS.Editor
Expand Down Expand Up @@ -72,67 +68,10 @@ private bool isRecent(PropertyAction followingAction)
private void execute()
{
Property.SetValue(ParentObject, Value, null);

if (ParentObject is API.IComponent component && component.Entity != null)
{
var entityModel = _model.Entities.GetOrAdd(component.Entity.ID, id =>
{
var e = new EntityModel
{
ID = id,
DisplayName = component.Entity.DisplayName,
EntityConcreteType = component.Entity.GetType(),
Components = new Dictionary<Type, ComponentModel>()
};
var room = component.Entity.GetComponent<IHasRoomComponent>()?.Room;
if (room != null)
{
var roomModel = getRoom(room);
if (roomModel.BackgroundEntity != component.Entity.ID)
roomModel.Entities.Add(component.Entity.ID);
}
else
{
_model.Guis.Add(component.Entity.ID);
}
return e;
});

var componentModel = entityModel.Components.GetOrAdd(component.RegistrationType, _ => new ComponentModel
{ ComponentConcreteType = component.GetType(), Properties = new Dictionary<string, object>() });

bool wasDirty = entityModel.IsDirty;

if (componentModel.Properties.TryGetValue(Property.Name, out object oldValue))
{
_undoModel = () =>
{
componentModel.Properties[Property.Name] = oldValue;
if (!wasDirty) entityModel.IsDirty = false;
};
}
else
{
_undoModel = () =>
{
componentModel.Properties.Remove(Property.Name);
if (!wasDirty) entityModel.IsDirty = false;
};
}
componentModel.Properties[Property.Name] = Value;
entityModel.IsDirty = true;
}
}

private RoomModel getRoom(IRoom room)
{
var roomModel = _model.Rooms.FirstOrDefault(r => r.ID == room.ID);
if (roomModel == null)
if (ParentObject is API.IComponent component)
{
roomModel = new RoomModel { ID = room.ID, BackgroundEntity = room.Background?.Entity?.ID, Entities = new HashSet<string>() };
_model.Rooms.Add(roomModel);
_undoModel = ModelAction.Execute(_model, (component, Property.Name, Value));
}
return roomModel;
}
}
}
62 changes: 0 additions & 62 deletions Source/Editor/AGS.Editor/UndoRedo/ScaleAction.cs

This file was deleted.

0 comments on commit d22a30d

Please sign in to comment.