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.
|
|
|
|
|
|
|
|
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;
|
2022-06-15 22:45:31 +08:00
|
|
|
using osu.Framework.Graphics.Cursor;
|
2021-08-23 17:12:35 +08:00
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2022-06-15 22:45:31 +08:00
|
|
|
using osu.Framework.Localisation;
|
2022-06-17 03:51:31 +08:00
|
|
|
using osu.Framework.Platform;
|
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
|
|
|
|
{
|
2022-06-15 22:45:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A labelled drawable displaying file chooser on click, with placeholder text support.
|
|
|
|
/// todo: this should probably not use PopoverTextBox just to display placeholder text, but is the best way for now.
|
|
|
|
/// </summary>
|
2022-11-24 13:32:20 +08:00
|
|
|
internal partial class LabelledFileChooser : LabelledDrawable<LabelledTextBoxWithPopover.PopoverTextBox>, IHasCurrentValue<FileInfo?>, ICanAcceptFiles, IHasPopover
|
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;
|
|
|
|
|
|
|
|
[Resolved]
|
2022-06-03 02:36:28 +08:00
|
|
|
private OsuGameBase game { get; set; } = null!;
|
2020-10-06 14:17:15 +08:00
|
|
|
|
2022-06-17 03:51:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The initial path to use when displaying the <see cref="FileChooserPopover"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Uses a <see langword="null"/> value before the first selection is made
|
|
|
|
/// to ensure that the first selection starts at <see cref="GameHost.InitialFileSelectorPath"/>.
|
|
|
|
/// </remarks>
|
|
|
|
private string? initialChooserPath;
|
2022-06-17 01:48:17 +08:00
|
|
|
|
2022-06-16 00:29:09 +08:00
|
|
|
private readonly BindableWithCurrent<FileInfo?> current = new BindableWithCurrent<FileInfo?>();
|
2022-06-15 14:52:12 +08:00
|
|
|
|
2022-06-16 00:29:09 +08:00
|
|
|
public Bindable<FileInfo?> Current
|
2022-06-15 14:52:12 +08:00
|
|
|
{
|
|
|
|
get => current.Current;
|
|
|
|
set => current.Current = value;
|
|
|
|
}
|
|
|
|
|
2022-06-15 22:45:31 +08:00
|
|
|
public LocalisableString Text
|
|
|
|
{
|
|
|
|
get => Component.PlaceholderText;
|
|
|
|
set => Component.PlaceholderText = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompositeDrawable TabbableContentContainer
|
|
|
|
{
|
|
|
|
set => Component.TabbableContentContainer = value;
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:52:12 +08:00
|
|
|
public LabelledFileChooser(params string[] handledExtensions)
|
2022-06-15 22:45:31 +08:00
|
|
|
: base(false)
|
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);
|
2022-06-16 00:29:09 +08:00
|
|
|
Current.BindValueChanged(onFileSelected);
|
2021-04-04 18:34:52 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 02:36:28 +08:00
|
|
|
private void onFileSelected(ValueChangedEvent<FileInfo?> file)
|
2021-04-04 18:34:52 +08:00
|
|
|
{
|
2022-06-16 00:29:09 +08:00
|
|
|
if (file.NewValue != null)
|
|
|
|
this.HidePopover();
|
2022-06-17 01:48:17 +08:00
|
|
|
|
2022-06-17 03:51:31 +08:00
|
|
|
initialChooserPath = file.NewValue?.DirectoryName;
|
2021-04-04 18:34:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Task ICanAcceptFiles.Import(params string[] paths)
|
|
|
|
{
|
2022-06-16 00:29:09 +08:00
|
|
|
Schedule(() => Current.Value = new FileInfo(paths.First()));
|
2021-04-04 18:34:52 +08:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2022-12-13 20:03:25 +08:00
|
|
|
Task ICanAcceptFiles.Import(ImportTask[] tasks, ImportParameters parameters) => throw new NotImplementedException();
|
2021-04-04 18:34:52 +08:00
|
|
|
|
|
|
|
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-06-15 22:45:31 +08:00
|
|
|
protected override LabelledTextBoxWithPopover.PopoverTextBox CreateComponent() => new LabelledTextBoxWithPopover.PopoverTextBox
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
CornerRadius = CORNER_RADIUS,
|
|
|
|
OnFocused = this.ShowPopover,
|
|
|
|
};
|
|
|
|
|
2022-06-17 03:51:31 +08:00
|
|
|
public Popover GetPopover() => new FileChooserPopover(handledExtensions, Current, initialChooserPath);
|
2021-08-23 17:12:35 +08:00
|
|
|
|
2022-11-24 13:32:20 +08:00
|
|
|
private partial class FileChooserPopover : OsuPopover
|
2021-08-23 17:12:35 +08:00
|
|
|
{
|
2022-06-17 01:48:17 +08:00
|
|
|
public FileChooserPopover(string[] handledExtensions, Bindable<FileInfo?> currentFile, string? chooserPath)
|
2021-08-23 17:12:35 +08:00
|
|
|
{
|
|
|
|
Child = new Container
|
|
|
|
{
|
|
|
|
Size = new Vector2(600, 400),
|
2022-06-17 01:48:17 +08:00
|
|
|
Child = new OsuFileSelector(chooserPath, handledExtensions)
|
2021-08-23 17:12:35 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
CurrentFile = { BindTarget = currentFile }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 14:17:15 +08:00
|
|
|
}
|
|
|
|
}
|