Show dialogue only when there are unsaved changes

This commit is contained in:
Abdurrahmaan Iqbal
2019-10-17 10:24:45 +01:00
committed by abcdefg30
parent 780982dbe2
commit d2819dca77
2 changed files with 29 additions and 7 deletions

View File

@@ -29,6 +29,8 @@ namespace OpenRA.Mods.Common.Traits
int nextId;
public bool Modified;
public void WorldLoaded(World w, WorldRenderer wr)
{
Add(new OpenMapAction());
@@ -36,6 +38,7 @@ namespace OpenRA.Mods.Common.Traits
public void Add(IEditorAction editorAction)
{
Modified = true;
editorAction.Execute();
if (undoStack.Count > 0)
@@ -55,6 +58,8 @@ namespace OpenRA.Mods.Common.Traits
if (!HasUndos())
return;
Modified = true;
var editorAction = undoStack.Pop();
undoStack.Peek().Status = EditorActionStatus.Active;
editorAction.Action.Undo();
@@ -81,6 +86,8 @@ namespace OpenRA.Mods.Common.Traits
if (!HasRedos())
return;
Modified = true;
var editorAction = redoStack.Pop();
editorAction.Status = EditorActionStatus.Active;
@@ -114,6 +121,12 @@ namespace OpenRA.Mods.Common.Traits
while (undoStack.Peek().Id != id)
Redo();
}
public bool HasUnsavedItems()
{
// Modified and last action isn't the OpenMapAction (+ no redos)
return Modified && !(undoStack.Peek().Action is OpenMapAction && !HasRedos());
}
}
public enum EditorActionStatus

View File

@@ -339,9 +339,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
hideMenu = true;
var editorActorLayer = world.WorldActor.Trait<EditorActorLayer>();
var actionManager = world.WorldActor.Trait<EditorActionManager>();
Ui.OpenWindow("SAVE_MAP_PANEL", new WidgetArgs()
{
{ "onSave", (Action<string>)(_ => hideMenu = false) },
{ "onSave", (Action<string>)(_ => { hideMenu = false; actionManager.Modified = false; }) },
{ "onExit", () => hideMenu = false },
{ "map", world.Map },
{ "playerDefinitions", editorActorLayer.Players.ToMiniYaml() },
@@ -355,15 +356,23 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (world.Type != WorldType.Editor)
return;
var actionManager = world.WorldActor.Trait<EditorActionManager>();
var button = AddButton("EXIT_EDITOR", "Exit Map Editor");
// Show dialog only if updated since last save
button.OnClick = () =>
{
hideMenu = true;
ConfirmationDialogs.ButtonPrompt(
title: "Exit Map Editor",
text: "Exit and lose all unsaved changes?",
onConfirm: OnQuit,
onCancel: ShowMenu);
if (actionManager.HasUnsavedItems())
{
hideMenu = true;
ConfirmationDialogs.ButtonPrompt(
title: "Exit Map Editor",
text: "Exit and lose all unsaved changes?",
onConfirm: OnQuit,
onCancel: ShowMenu);
}
else
OnQuit();
};
}
}