Skip to content

Commit

Permalink
Merge pull request #482 from PixiEditor/1.0-bug-fixes
Browse files Browse the repository at this point in the history
Fixed reference layer shape serialization and recent file not found
  • Loading branch information
flabbet committed Mar 7, 2023
2 parents 9ce09f7 + 768ea5a commit 28a7bb2
Show file tree
Hide file tree
Showing 31 changed files with 295 additions and 115 deletions.
7 changes: 6 additions & 1 deletion src/PixiEditor.UpdateModule/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ public async Task<bool> CheckUpdateAvailable()

public bool CheckUpdateAvailable(ReleaseInfo latestRelease)
{
if (latestRelease == null || string.IsNullOrEmpty(latestRelease.TagName)) return false;
if (CurrentVersionTag == null) return false;

return latestRelease.WasDataFetchSuccessful && VersionDifferent(CurrentVersionTag, latestRelease.TagName);
}

public bool IsUpdateCompatible(string[] incompatibleVersions)
{
return !incompatibleVersions.Select(x => x.Trim()).Contains(CurrentVersionTag[..7].Trim());
return !incompatibleVersions.Select(x => x.Trim()).Contains(ExtractVersionString(CurrentVersionTag));
}

public async Task<bool> IsUpdateCompatible()
Expand Down Expand Up @@ -130,6 +133,8 @@ private static async Task<ReleaseInfo> GetLatestReleaseInfoAsync(string apiUrl)

private static string ExtractVersionString(string versionString)
{
if (string.IsNullOrEmpty(versionString)) return string.Empty;

for (int i = 0; i < versionString.Length; i++)
{
if (!char.IsDigit(versionString[i]) && versionString[i] != '.')
Expand Down
10 changes: 8 additions & 2 deletions src/PixiEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ private bool HandleNewInstance()
if (mainWindow != null)
{
mainWindow.BringToForeground();
StartupArgs.Args = File.ReadAllText(passedArgsFile).Split(' ').ToList();
File.Delete(passedArgsFile);
List<string> args = new List<string>();
if (File.Exists(passedArgsFile))
{
args = File.ReadAllText(passedArgsFile).Split(' ').ToList();
File.Delete(passedArgsFile);
}
StartupArgs.Args = args;
StartupArgs.Args.Add("--openedInExisting");
mainWindow.DataContext.OnStartupCommand.Execute(null);
}
Expand Down
12 changes: 10 additions & 2 deletions src/PixiEditor/Helpers/DocumentViewModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using PixiEditor.DrawingApi.Core.ColorsImpl;
using PixiEditor.DrawingApi.Core.Numerics;
using PixiEditor.DrawingApi.Core.Surface;
using PixiEditor.Parser;
using BlendMode = PixiEditor.ChangeableDocument.Enums.BlendMode;

namespace PixiEditor.Helpers;
Expand Down Expand Up @@ -351,9 +352,16 @@ public ReferenceLayerBuilder WithImage(VecI size, byte[] pbgraData)
return this;
}

public ReferenceLayerBuilder WithRect(VecD offset, VecD size)
public ReferenceLayerBuilder WithShape(Corners rect)
{
Shape = new ShapeCorners(new RectD(offset, size));
Shape = new ShapeCorners
{
TopLeft = rect.TopLeft.ToVecD(),
TopRight = rect.TopRight.ToVecD(),
BottomLeft = rect.BottomLeft.ToVecD(),
BottomRight = rect.BottomRight.ToVecD()
};

return this;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/PixiEditor/Helpers/Extensions/PixiParserDocumentEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace PixiEditor.Helpers.Extensions;

internal static class PixiParserDocumentEx
{
public static VecD ToVecD(this Vector2 vec)
{
return new VecD(vec.X, vec.Y);
}

public static DocumentViewModel ToDocument(this Document document)
{
return DocumentViewModel.Build(b =>
Expand All @@ -18,7 +23,7 @@ public static DocumentViewModel ToDocument(this Document document)
.WithSwatches(document.Swatches, x => new(x.R, x.G, x.B, x.A))
.WithReferenceLayer(document.ReferenceLayer, (r, builder) => builder
.WithIsVisible(r.Enabled)
.WithRect(new VecD(r.OffsetX, r.OffsetY), new VecD(r.Width, r.Height))
.WithShape(r.Corners)
.WithSurface(Surface.Load(r.ImageBytes)));
BuildChildren(b, document.RootFolder.Children);
Expand Down
5 changes: 5 additions & 0 deletions src/PixiEditor/Helpers/Extensions/SerializableDocumentEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using PixiEditor.DrawingApi.Core.ColorsImpl;
using PixiEditor.DrawingApi.Core.Numerics;
using PixiEditor.DrawingApi.Core.Surface.ImageData;
using PixiEditor.Parser;
using PixiEditor.Parser.Collections.Deprecated;
using PixiEditor.Parser.Deprecated;
using PixiEditor.ViewModels.SubViewModels.Document;
Expand All @@ -10,6 +11,10 @@ namespace PixiEditor.Helpers.Extensions;

internal static class SerializableDocumentEx
{
public static Vector2 ToVector2(this VecD serializableVector2)
{
return new Vector2 { X = serializableVector2.X, Y = serializableVector2.Y };
}
public static Image ToImage(this SerializableLayer serializableLayer)
{
if (serializableLayer.PngBytes == null)
Expand Down
9 changes: 9 additions & 0 deletions src/PixiEditor/Models/DataHolders/RecentlyOpenedDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public string FileExtension
{
get
{
if (!File.Exists(FilePath))
{
return "? (Not found)";
}
if (Corrupt)
{
return "? (Corrupt)";
Expand Down Expand Up @@ -71,6 +75,11 @@ public RecentlyOpenedDocument(string path)

private WriteableBitmap LoadPreviewBitmap()
{
if (!File.Exists(FilePath))
{
return null;
}

if (FileExtension == ".pixi")
{
SerializableDocument serializableDocument;
Expand Down
47 changes: 37 additions & 10 deletions src/PixiEditor/Models/DocumentModels/ChangeExecutionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal class ChangeExecutionController
private VecD lastPrecisePos;

private UpdateableChangeExecutor? currentSession = null;

private UpdateableChangeExecutor? _queuedExecutor = null;

public ChangeExecutionController(DocumentViewModel document, DocumentInternalParts internals)
{
Expand All @@ -44,32 +46,51 @@ public ExecutorType GetCurrentExecutorType()
public bool TryStartExecutor<T>(bool force = false)
where T : UpdateableChangeExecutor, new()
{
if (currentSession is not null && !force)
if (CanStartExecutor(force))
return false;
if (force)
currentSession?.ForceStop();

T executor = new T();
executor.Initialize(document, internals, this, EndExecutor);
if (executor.Start() == ExecutionState.Success)
{
currentSession = executor;
return true;
}
return false;
return TryStartExecutorInternal(executor);
}

public bool TryStartExecutor(UpdateableChangeExecutor brandNewExecutor, bool force = false)
{
if (currentSession is not null && !force)
if (CanStartExecutor(force))
return false;
if (force)
currentSession?.ForceStop();
brandNewExecutor.Initialize(document, internals, this, EndExecutor);

return TryStartExecutorInternal(brandNewExecutor);
}

private bool CanStartExecutor(bool force)
{
return (currentSession is not null || _queuedExecutor is not null) && !force;
}

private bool TryStartExecutorInternal(UpdateableChangeExecutor executor)
{
executor.Initialize(document, internals, this, EndExecutor);

if (executor.StartMode == ExecutorStartMode.OnMouseLeftButtonDown)
{
_queuedExecutor = executor;
return true;
}

return StartExecutor(executor);
}

private bool StartExecutor(UpdateableChangeExecutor brandNewExecutor)
{
if (brandNewExecutor.Start() == ExecutionState.Success)
{
currentSession = brandNewExecutor;
return true;
}

return false;
}

Expand All @@ -78,6 +99,7 @@ private void EndExecutor(UpdateableChangeExecutor executor)
if (executor != currentSession)
throw new InvalidOperationException();
currentSession = null;
_queuedExecutor = null;
}

public bool TryStopActiveExecutor()
Expand Down Expand Up @@ -153,6 +175,11 @@ public void LeftMouseButtonDownInlet(VecD canvasPos)
//update internal state
LeftMouseState = MouseButtonState.Pressed;

if (_queuedExecutor != null && currentSession == null)
{
StartExecutor(_queuedExecutor);
}

//call session event
currentSession?.OnLeftMouseButtonDown(canvasPos);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@ public List<StructureMemberViewModel> FindPath(Guid guid)
list.Add(doc.StructureRoot);
return list;
}

/// <summary>
/// Returns all layers in the document.
/// </summary>
/// <returns>List of LayerViewModels. Empty if no layers found.</returns>
public List<LayerViewModel> GetAllLayers()
{
List<LayerViewModel> layers = new List<LayerViewModel>();
foreach (StructureMemberViewModel? member in doc.StructureRoot.Children)
{
if (member is LayerViewModel layer)
layers.Add(layer);
else if (member is FolderViewModel folder)
layers.AddRange(GetAllLayers(folder, layers));
}

return layers;
}

private List<LayerViewModel> GetAllLayers(FolderViewModel folder, List<LayerViewModel> layers)
{
foreach (StructureMemberViewModel? member in folder.Children)
{
if (member is LayerViewModel layer)
layers.Add(layer);
else if (member is FolderViewModel innerFolder)
layers.AddRange(GetAllLayers(innerFolder, layers));
}
return layers;
}

private bool FillPath(FolderViewModel folder, Guid guid, List<StructureMemberViewModel> toFill)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ internal class ShiftLayerExecutor : UpdateableChangeExecutor
private VecI startPos;
private MoveToolViewModel? tool;

public override ExecutorStartMode StartMode => ExecutorStartMode.OnMouseLeftButtonDown;

public override ExecutionState Start()
{
ViewModelMain? vm = ViewModelMain.Current;
StructureMemberViewModel? member = document!.SelectedStructureMember;
if(member != null)
_affectedMemberGuids.Add(member.GuidValue);
_affectedMemberGuids.AddRange(document!.SoftSelectedStructureMembers.Select(x => x.GuidValue));

tool = ViewModelMain.Current?.ToolsSubViewModel.GetTool<MoveToolViewModel>();
if (vm is null || tool is null)
return ExecutionState.Error;


if (tool.MoveAllLayers)
{
_affectedMemberGuids.AddRange(document.StructureHelper.GetAllLayers().Select(x => x.GuidValue));
}
else
{
if (member != null)
_affectedMemberGuids.Add(member.GuidValue);
_affectedMemberGuids.AddRange(document!.SoftSelectedStructureMembers.Select(x => x.GuidValue));
}

RemoveDrawOnMaskLayers(_affectedMemberGuids);

startPos = controller!.LastPixelPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal class TransformSelectedAreaExecutor : UpdateableChangeExecutor
{
private Guid[]? membersToTransform;
private MoveToolViewModel? tool;

public override ExecutorType Type { get; }

public TransformSelectedAreaExecutor(bool toolLinked)
Expand All @@ -25,10 +24,13 @@ public override ExecutionState Start()
if (tool is null || document!.SelectedStructureMember is null || document!.SelectionPathBindable.IsEmpty)
return ExecutionState.Error;

var members = document.SoftSelectedStructureMembers
tool.TransformingSelectedArea = true;
List<StructureMemberViewModel> members = new();

members = document.SoftSelectedStructureMembers
.Append(document.SelectedStructureMember)
.Where(static m => m is LayerViewModel);

.Where(static m => m is LayerViewModel).ToList();
if (!members.Any())
return ExecutionState.Error;

Expand All @@ -54,6 +56,11 @@ public override void OnTransformMoved(ShapeCorners corners)

public override void OnTransformApplied()
{
if (tool is not null)
{
tool.TransformingSelectedArea = false;
}

internals!.ActionAccumulator.AddActions(new EndTransformSelectedArea_Action());
internals!.ActionAccumulator.AddFinishedActions();
document!.TransformViewModel.HideTransform();
Expand All @@ -67,6 +74,11 @@ public override void OnTransformApplied()

public override void ForceStop()
{
if (tool is not null)
{
tool.TransformingSelectedArea = false;
}

internals!.ActionAccumulator.AddActions(new EndTransformSelectedArea_Action());
internals!.ActionAccumulator.AddFinishedActions();
document!.TransformViewModel.HideTransform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal abstract class UpdateableChangeExecutor

protected Action<UpdateableChangeExecutor>? onEnded;
public virtual ExecutorType Type => ExecutorType.Regular;
public virtual ExecutorStartMode StartMode => ExecutorStartMode.RightAway;

public void Initialize(DocumentViewModel document, DocumentInternalParts internals, ChangeExecutionController controller, Action<UpdateableChangeExecutor> onEnded)
{
Expand Down
6 changes: 6 additions & 0 deletions src/PixiEditor/Models/Enums/ExecutorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ internal enum ExecutorType
Regular,
ToolLinked,
}

internal enum ExecutorStartMode
{
RightAway,
OnMouseLeftButtonDown,
}
5 changes: 5 additions & 0 deletions src/PixiEditor/Models/IO/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public static SaveResult TrySave(DocumentViewModel document, string pathWithExte
return SaveResult.ConcurrencyError;
var bitmap = maybeBitmap.AsT1;

if (!encodersFactory.ContainsKey(typeFromPath))
{
return SaveResult.UnknownError;
}

if (!TrySaveAs(encodersFactory[typeFromPath](), pathWithExtension, bitmap, exportSize))
return SaveResult.UnknownError;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PixiEditor/PixiEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta2" />
<PackageReference Include="OneOf" Version="3.0.223" />
<PackageReference Include="PixiEditor.ColorPicker" Version="3.3.1" />
<PackageReference Include="PixiEditor.Parser" Version="3.2.0" />
<PackageReference Include="PixiEditor.Parser" Version="3.3.0" />
<PackageReference Include="PixiEditor.Parser.Skia" Version="3.0.0" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="WpfAnimatedGif" Version="2.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ private static ReferenceLayer GetReferenceLayer(IReadOnlyDocument document)
Height = (float)layer.Shape.RectSize.Y,
OffsetX = (float)layer.Shape.TopLeft.X,
OffsetY = (float)layer.Shape.TopLeft.Y,
Corners = new Corners
{
TopLeft = layer.Shape.TopLeft.ToVector2(),
TopRight = layer.Shape.TopRight.ToVector2(),
BottomLeft = layer.Shape.BottomLeft.ToVector2(),
BottomRight = layer.Shape.BottomRight.ToVector2()
},
Opacity = 1,
ImageBytes = stream.ToArray()
};
Expand Down
Loading

0 comments on commit 28a7bb2

Please sign in to comment.