2020-10-06 14:17:15 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-03 02:36:28 +08:00
|
|
|
#nullable enable
|
|
|
|
|
2020-10-06 14:17:15 +08:00
|
|
|
using System;
|
2021-04-04 18:34:52 +08:00
|
|
|
using System.Collections.Generic;
|
2020-10-06 14:17:15 +08:00
|
|
|
using System.IO;
|
2021-04-04 18:34:52 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-11-23 12:49:14 +08:00
|
|
|
using osu.Framework.Allocation;
|
2020-10-06 14:17:15 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-08-23 17:12:35 +08:00
|
|
|
using osu.Framework.Extensions;
|
2022-06-03 02:38:14 +08:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2020-10-06 14:17:15 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-08-23 17:12:35 +08:00
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2021-04-04 18:34:52 +08:00
|
|
|
using osu.Game.Database;
|
2020-10-06 14:17:15 +08:00
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
2021-08-23 17:12:35 +08:00
|
|
|
using osuTK;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Setup
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A labelled textbox which reveals an inline file chooser when clicked.
|
|
|
|
/// </summary>
|
2022-05-17 15:32:31 +08:00
|
|
|
internal class FileChooserLabelledTextBox : LabelledTextBoxWithPopover, ICanAcceptFiles
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2021-04-04 18:34:52 +08:00
|
|
|
private readonly string[] handledExtensions;
|
2021-08-23 17:12:35 +08:00
|
|
|
|
2021-04-04 18:34:52 +08:00
|
|
|
public IEnumerable<string> HandledExtensions => handledExtensions;
|
|
|
|
|
2022-06-03 02:36:28 +08:00
|
|
|
private readonly Bindable<FileInfo?> currentFile = new Bindable<FileInfo?>();
|
2021-04-04 18:34:52 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2022-06-03 02:36:28 +08:00
|
|
|
private OsuGameBase game { get; set; } = null!;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
2021-04-04 18:34:52 +08:00
|
|
|
public FileChooserLabelledTextBox(params string[] handledExtensions)
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2021-04-04 18:34:52 +08:00
|
|
|
this.handledExtensions = handledExtensions;
|
2020-10-06 14:17:15 +08:00
|
|
|
}
|
|
|
|
|
2021-04-04 18:34:52 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
game.RegisterImportHandler(this);
|
|
|
|
currentFile.BindValueChanged(onFileSelected);
|
|
|
|
}
|
|
|
|
|
2022-06-03 02:36:28 +08:00
|
|
|
private void onFileSelected(ValueChangedEvent<FileInfo?> file)
|
2021-04-04 18:34:52 +08:00
|
|
|
{
|
|
|
|
if (file.NewValue == null)
|
|
|
|
return;
|
|
|
|
|
2021-08-23 23:01:01 +08:00
|
|
|
this.HidePopover();
|
2021-04-04 18:34:52 +08:00
|
|
|
Current.Value = file.NewValue.FullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task ICanAcceptFiles.Import(params string[] paths)
|
|
|
|
{
|
|
|
|
Schedule(() => currentFile.Value = new FileInfo(paths.First()));
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task ICanAcceptFiles.Import(params ImportTask[] tasks) => throw new NotImplementedException();
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
2022-06-03 02:38:14 +08:00
|
|
|
|
|
|
|
if (game.IsNotNull())
|
|
|
|
game.UnregisterImportHandler(this);
|
2021-04-04 18:34:52 +08:00
|
|
|
}
|
|
|
|
|
2022-05-17 15:32:31 +08:00
|
|
|
public override Popover GetPopover() => new FileChooserPopover(handledExtensions, currentFile);
|
2021-08-23 17:12:35 +08:00
|
|
|
|
|
|
|
private class FileChooserPopover : OsuPopover
|
|
|
|
{
|
2022-06-03 02:36:28 +08:00
|
|
|
public FileChooserPopover(string[] handledExtensions, Bindable<FileInfo?> currentFile)
|
2021-08-23 17:12:35 +08:00
|
|
|
{
|
|
|
|
Child = new Container
|
|
|
|
{
|
|
|
|
Size = new Vector2(600, 400),
|
|
|
|
Child = new OsuFileSelector(currentFile.Value?.DirectoryName, handledExtensions)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
CurrentFile = { BindTarget = currentFile }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 14:17:15 +08:00
|
|
|
}
|
|
|
|
}
|