Add SavingWorldWindow to see the progress when saving a world

This commit is contained in:
Stone_Red
2023-10-05 18:13:12 +02:00
parent ee081cbcf9
commit a71a80182b
5 changed files with 90 additions and 23 deletions
@@ -0,0 +1,9 @@
<Project StylesheetPath="stylesheet/default_ui_skin.xmms">
<Project.ExportOptions />
<Window Title="Saving World" Left="260" Top="215">
<VerticalStackPanel VerticalAlignment="Center">
<HorizontalProgressBar HorizontalAlignment="Center" Width="500" Id="progressBar" />
<Label Text="Saving..." Margin="0, 10, 0, 0" HorizontalAlignment="Center" Id="label" />
</VerticalStackPanel>
</Window>
</Project>
@@ -62,6 +62,9 @@
<None Update="Content\QuickMenu.xmmp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Content\SavingWorldWindow.xmmp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Content\SettingsWindow.xmmp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
@@ -1,14 +1,10 @@
using FluentResults;
using Myra.Graphics2D.UI;
using Myra.Graphics2D.UI;
using StoneRed.LogicSimulator.Misc;
using StoneRed.LogicSimulator.WorldSaveSystem;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace StoneRed.LogicSimulator.UserInterface.Windows;
@@ -64,7 +60,7 @@ internal class SaveWorldWindow : SrlsWindow
srls.ShowWindow(inputDialog);
}
private async void NewSaveInputDialog_Ok(object? sender, InputDialogEventArgs e)
private void NewSaveInputDialog_Ok(object? sender, InputDialogEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Text))
{
@@ -73,28 +69,16 @@ internal class SaveWorldWindow : SrlsWindow
}
worldData.SaveName = e.Text;
await Save();
Save();
}
private async void SaveButton_Clicked(object? sender, EventArgs e)
private void SaveButton_Clicked(object? sender, EventArgs e)
{
await Save();
Save();
}
private async Task Save()
private void Save()
{
WorldSaver worldSaver = new WorldSaver(srls);
Result result = await worldSaver.SaveWorld(worldData, new Progress<WorldSaveLoadProgress>());
if (result.IsSuccess)
{
_ = Dialog.CreateMessageBox("Success", "Saved world!");
}
else
{
_ = Dialog.CreateMessageBox("Error", string.Join(',', result.Errors.Select(e => e.Message)));
}
Close();
srls.ShowWindow(new SavingWorldWindow(worldData));
}
}
@@ -0,0 +1,66 @@
using FluentResults;
using Myra.Graphics2D.UI;
using StoneRed.LogicSimulator.WorldSaveSystem;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace StoneRed.LogicSimulator.UserInterface.Windows;
internal class SavingWorldWindow : SrlsWindow
{
private readonly WorldData worldData;
private bool windowClosable = false;
private Label label = null!;
private HorizontalProgressBar horizontalProgressBar = null!;
protected override string XmmpPath => "SavingWorldWindow.xmmp";
public SavingWorldWindow(WorldData worldData)
{
this.worldData = worldData;
}
public override void Initialize()
{
window.Closing += (_, e) => e.Cancel = !windowClosable;
label = window.FindChildById<Label>("label");
horizontalProgressBar = window.FindChildById<HorizontalProgressBar>("progressBar");
horizontalProgressBar.Maximum = 100;
SaveWorld();
}
private void SaveWorld()
{
Progress<WorldSaveLoadProgress> progress = new Progress<WorldSaveLoadProgress>();
progress.ProgressChanged += Progress_ProgressChanged;
WorldSaver worldLoader = new WorldSaver(srls);
_ = Task.Run(async () =>
{
Result<WorldData> result = await worldLoader.SaveWorld(worldData, progress);
windowClosable = true;
if (result.IsSuccess)
{
Close();
}
else
{
srls.ShowWindow(new InfoDialog(string.Join(',', result.Errors.Select(e => e.Message))));
}
});
}
private void Progress_ProgressChanged(object? sender, WorldSaveLoadProgress e)
{
horizontalProgressBar.Value = e.Percentage;
label.Text = e.Message;
}
}
@@ -36,8 +36,13 @@ internal class WorldWriterV1 : IWorldWriter
writer.Write((ushort)1); // Write file version
writer.Write(numberOfLogicGates);
int gateNumber = 0;
foreach (LogicGate logicGate in worldData.LogicGates)
{
gateNumber++;
progress.Report(new((int)((double)gateNumber / numberOfLogicGates * 50d), $"Saving logic gates ({gateNumber}/{numberOfLogicGates})"));
writer.Write(logicGate.Id);
if (!LogicGatesManager.TryGetTypeLogicGateName(logicGate.GetType(), out string? typeName))