mirror of
https://github.com/muskit/MercuryConverter.git
synced 2026-06-02 20:24:26 -07:00
flesh out the data scan UI, tweak selection table
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Platform.Storage;
|
||||
using Avalonia.Threading;
|
||||
using MercuryConverter.Data;
|
||||
using UAssetAPI;
|
||||
using UAssetAPI.UnrealTypes;
|
||||
|
||||
namespace MercuryConverter.UI.Dialogs;
|
||||
|
||||
public partial class DataScanning : UserControl
|
||||
{
|
||||
public static DataScanning? Instance { get; private set; }
|
||||
public DataScanning()
|
||||
{
|
||||
Instance = this;
|
||||
InitializeComponent();
|
||||
|
||||
ScanPath.Text = "";
|
||||
|
||||
if (!Design.IsDesignMode)
|
||||
RunFlow();
|
||||
}
|
||||
|
||||
public void RunFlow()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var path = ""; // TODO: set to current/saved data path
|
||||
|
||||
// Content selection
|
||||
var selectedPath = await BeginDirSelection();
|
||||
Console.WriteLine($"selectedPath=[{selectedPath}]");
|
||||
|
||||
if (selectedPath == "") // cancelled opening folder
|
||||
{
|
||||
// TODO:
|
||||
// return and go to completed mode if scan already completed
|
||||
// continue if no scan has been completed
|
||||
// break if we already have a path but somehow not scanned
|
||||
UISetError("No data folder provided.");
|
||||
return;
|
||||
}
|
||||
if (!Directory.Exists(selectedPath))
|
||||
{
|
||||
UISetError("Folder does not exist.");
|
||||
return;
|
||||
}
|
||||
if (!(File.Exists(Path.Combine(selectedPath, "MusicParameterTable.uasset")) && File.Exists(Path.Combine(selectedPath, "MusicParameterTable.uexp"))))
|
||||
{
|
||||
UISetError("Missing MusicParameterTable asset files. Without them, we have nothing to work with.");
|
||||
return;
|
||||
}
|
||||
|
||||
path = selectedPath;
|
||||
|
||||
UIScanningMode(path);
|
||||
Database.SetupNew(path);
|
||||
UIScanCompletedMode();
|
||||
});
|
||||
}
|
||||
|
||||
private void UISelectMode()
|
||||
{
|
||||
UISetError();
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
ScanStatus.Text = "select your data folder...";
|
||||
ScanPath.IsVisible = false;
|
||||
ButtonGroup.IsVisible = false;
|
||||
ProgressAnimation.IsVisible = true;
|
||||
});
|
||||
}
|
||||
|
||||
private void UIScanningMode(string path)
|
||||
{
|
||||
UISetError();
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
ScanStatus.Text = "scanning...";
|
||||
ScanPath.IsVisible = true;
|
||||
ScanPath.Text = path;
|
||||
ScanInfo.IsVisible = true;
|
||||
ButtonGroup.IsVisible = false;
|
||||
ProgressAnimation.IsVisible = true;
|
||||
});
|
||||
}
|
||||
|
||||
private void UIScanCompletedMode()
|
||||
{
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
ScanStatus.Text = "scan complete";
|
||||
ScanPath.IsVisible = true;
|
||||
ScanInfo.IsVisible = true;
|
||||
ButtonGroup.IsVisible = true;
|
||||
ProgressAnimation.IsVisible = false;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use only when no other processes are running.
|
||||
/// </summary>
|
||||
/// <param name="error"></param>
|
||||
private void UISetError(string? error = null)
|
||||
{
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
if (error == null)
|
||||
{
|
||||
ScanError.IsVisible = false;
|
||||
return;
|
||||
}
|
||||
|
||||
ScanError.IsVisible = true;
|
||||
ErrorText.Text = error;
|
||||
ProgressAnimation.IsVisible = false;
|
||||
ButtonGroup.IsVisible = true;
|
||||
ScanStatus.Text = "an error has occurred";
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private async Task<string> BeginDirSelection(string? startDir = null)
|
||||
{
|
||||
IReadOnlyList<IStorageFolder>? dirSelection = null;
|
||||
|
||||
UISelectMode();
|
||||
|
||||
await Dispatcher.UIThread.Invoke(async () =>
|
||||
{
|
||||
await Task.Delay(250);
|
||||
var tl = TopLevel.GetTopLevel(MainWindow.Instance)!;
|
||||
dirSelection = await tl.StorageProvider.OpenFolderPickerAsync
|
||||
(
|
||||
new FolderPickerOpenOptions
|
||||
{
|
||||
Title = "Locate Data Folder",
|
||||
AllowMultiple = false,
|
||||
SuggestedStartLocation = startDir == null ? null : await tl.StorageProvider.TryGetFolderFromPathAsync(startDir),
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
if (dirSelection!.Count <= 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return dirSelection!.First().TryGetLocalPath()!;
|
||||
}
|
||||
|
||||
private void CloseHandler(object sender, RoutedEventArgs args)
|
||||
{
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
MainWindow.Instance!.Dialog.IsOpen = false;
|
||||
});
|
||||
}
|
||||
|
||||
private void OpenDataHandler(object sender, RoutedEventArgs args)
|
||||
{
|
||||
RunFlow();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user