Allow undoing overwriting designs.

This commit is contained in:
Ottermandias 2023-11-10 19:00:33 +01:00
parent 36f6c48f7a
commit 859c738080
2 changed files with 45 additions and 7 deletions

View file

@ -19,12 +19,13 @@ namespace Glamourer.Designs;
public class DesignManager
{
private readonly CustomizationService _customizations;
private readonly ItemManager _items;
private readonly HumanModelList _humans;
private readonly SaveService _saveService;
private readonly DesignChanged _event;
private readonly List<Design> _designs = new();
private readonly CustomizationService _customizations;
private readonly ItemManager _items;
private readonly HumanModelList _humans;
private readonly SaveService _saveService;
private readonly DesignChanged _event;
private readonly List<Design> _designs = new();
private readonly Dictionary<Guid, DesignData> _undoStore = new();
public IReadOnlyList<Design> Designs
=> _designs;
@ -83,6 +84,10 @@ public class DesignManager
_event.Invoke(DesignChanged.Type.ReloadedAll, null!);
}
/// <summary> Whether an Undo for the given design is possible. </summary>
public bool CanUndo(Design? design)
=> design != null && _undoStore.ContainsKey(design.Identifier);
/// <summary> Create a new temporary design without adding it to the manager. </summary>
public DesignBase CreateTemporary()
=> new(_items);
@ -463,6 +468,7 @@ public class DesignManager
/// <summary> Apply an entire design based on its appliance rules piece by piece. </summary>
public void ApplyDesign(Design design, DesignBase other)
{
_undoStore[design.Identifier] = design.DesignData;
if (other.DoApplyWetness())
design.DesignData.SetIsWet(other.DesignData.IsWet());
if (other.DoApplyHatVisible())
@ -503,6 +509,16 @@ public class DesignManager
ChangeStain(design, EquipSlot.OffHand, other.DesignData.Stain(EquipSlot.OffHand));
}
public void UndoDesignChange(Design design)
{
if (!_undoStore.Remove(design.Identifier, out var otherData))
return;
var other = CreateTemporary();
other.DesignData = otherData;
ApplyDesign(design, other);
}
private void MigrateOldDesigns()
{
if (!File.Exists(_saveService.FileNames.MigrationDesignFile))