2020-10-06 06:17:15 +00: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 10:34:52 +00:00
|
|
|
using System.Collections.Generic;
|
2020-10-06 06:17:15 +00:00
|
|
|
using System.IO;
|
2021-04-04 10:34:52 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-11-23 04:49:14 +00:00
|
|
|
using osu.Framework.Allocation;
|
2020-10-06 06:17:15 +00:00
|
|
|
using osu.Framework.Bindables;
|
2021-08-23 09:12:35 +00:00
|
|
|
using osu.Framework.Extensions;
|
2020-10-06 06:17:15 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-08-23 09:12:35 +00:00
|
|
|
using osu.Framework.Graphics.Cursor;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2020-10-06 06:17:15 +00:00
|
|
|
using osu.Framework.Input.Events;
|
2021-04-04 10:34:52 +00:00
|
|
|
using osu.Game.Database;
|
2020-11-23 04:49:14 +00:00
|
|
|
using osu.Game.Graphics.Containers;
|
2020-10-06 06:17:15 +00:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
2021-08-23 09:12:35 +00:00
|
|
|
using osuTK;
|
2020-10-06 06:17:15 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Setup
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A labelled textbox which reveals an inline file chooser when clicked.
|
|
|
|
/// </summary>
|
2021-08-23 09:12:35 +00:00
|
|
|
internal class FileChooserLabelledTextBox : LabelledTextBox, ICanAcceptFiles, IHasPopover
|
2020-10-06 06:17:15 +00:00
|
|
|
{
|
2021-04-04 10:34:52 +00:00
|
|
|
private readonly string[] handledExtensions;
|
2021-08-23 09:12:35 +00:00
|
|
|
|
2021-04-04 10:34:52 +00:00
|
|
|
public IEnumerable<string> HandledExtensions => handledExtensions;
|
|
|
|
|
|
|
|
private readonly Bindable<FileInfo> currentFile = new Bindable<FileInfo>();
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; }
|
2020-10-06 06:17:15 +00:00
|
|
|
|
2020-11-23 04:49:14 +00:00
|
|
|
[Resolved]
|
|
|
|
private SectionsContainer<SetupSection> sectionsContainer { get; set; }
|
|
|
|
|
2021-04-04 10:34:52 +00:00
|
|
|
public FileChooserLabelledTextBox(params string[] handledExtensions)
|
2020-10-06 06:17:15 +00:00
|
|
|
{
|
2021-04-04 10:34:52 +00:00
|
|
|
this.handledExtensions = handledExtensions;
|
2020-10-06 06:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override OsuTextBox CreateTextBox() =>
|
|
|
|
new FileChooserOsuTextBox
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
CornerRadius = CORNER_RADIUS,
|
2021-08-23 09:12:35 +00:00
|
|
|
OnFocused = this.ShowPopover
|
2020-10-06 06:17:15 +00:00
|
|
|
};
|
2020-11-23 04:49:14 +00:00
|
|
|
|
2021-04-04 10:34:52 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
game.RegisterImportHandler(this);
|
|
|
|
currentFile.BindValueChanged(onFileSelected);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFileSelected(ValueChangedEvent<FileInfo> file)
|
|
|
|
{
|
|
|
|
if (file.NewValue == null)
|
|
|
|
return;
|
|
|
|
|
2021-08-23 15:01:01 +00:00
|
|
|
this.HidePopover();
|
2021-04-04 10:34:52 +00: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);
|
|
|
|
game.UnregisterImportHandler(this);
|
|
|
|
}
|
|
|
|
|
2020-10-06 06:17:15 +00:00
|
|
|
internal class FileChooserOsuTextBox : OsuTextBox
|
|
|
|
{
|
|
|
|
public Action OnFocused;
|
|
|
|
|
|
|
|
protected override void OnFocus(FocusEvent e)
|
|
|
|
{
|
|
|
|
OnFocused?.Invoke();
|
|
|
|
base.OnFocus(e);
|
|
|
|
|
|
|
|
GetContainingInputManager().TriggerFocusContention(this);
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 09:12:35 +00:00
|
|
|
|
|
|
|
public Popover GetPopover() => new FileChooserPopover(handledExtensions, currentFile);
|
|
|
|
|
|
|
|
private class FileChooserPopover : OsuPopover
|
|
|
|
{
|
|
|
|
public FileChooserPopover(string[] handledExtensions, Bindable<FileInfo> currentFile)
|
|
|
|
{
|
|
|
|
Child = new Container
|
|
|
|
{
|
|
|
|
Size = new Vector2(600, 400),
|
|
|
|
Child = new OsuFileSelector(currentFile.Value?.DirectoryName, handledExtensions)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
CurrentFile = { BindTarget = currentFile }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 06:17:15 +00:00
|
|
|
}
|
|
|
|
}
|