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))

View file

@ -84,6 +84,16 @@ public class DesignPanel
Disabled = _selector.Selected?.WriteProtected() ?? true,
};
private HeaderDrawer.Button UndoButton()
=> new()
{
Description = "Undo the last change if you accidentally overwrote your design with a different one.",
Icon = FontAwesomeIcon.Undo,
OnClick = UndoOverwrite,
Visible = _selector.Selected != null,
Disabled = !_manager.CanUndo(_selector.Selected),
};
private HeaderDrawer.Button ExportToClipboardButton()
=> new()
{
@ -95,7 +105,7 @@ public class DesignPanel
private void DrawHeader()
=> HeaderDrawer.Draw(SelectionName, 0, ImGui.GetColorU32(ImGuiCol.FrameBg),
2, SetFromClipboardButton(), ExportToClipboardButton(), LockButton(),
3, SetFromClipboardButton(), UndoButton(), ExportToClipboardButton(), LockButton(),
HeaderDrawer.Button.IncognitoButton(_selector.IncognitoMode, v => _selector.IncognitoMode = v));
private string SelectionName
@ -411,6 +421,18 @@ public class DesignPanel
}
}
private void UndoOverwrite()
{
try
{
_manager.UndoDesignChange(_selector.Selected!);
}
catch (Exception ex)
{
Glamourer.Messager.NotificationMessage(ex, $"Could not undo last changes to {_selector.Selected!.Name}.", NotificationType.Error, false);
}
}
private void ExportToClipboard()
{
try