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;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Input.Events;
|
2021-04-04 18:34:52 +08:00
|
|
|
using osu.Game.Database;
|
2020-11-23 12:49:14 +08:00
|
|
|
using osu.Game.Graphics.Containers;
|
2020-10-06 14:17:15 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Setup
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A labelled textbox which reveals an inline file chooser when clicked.
|
|
|
|
/// </summary>
|
2021-04-04 18:34:52 +08:00
|
|
|
internal class FileChooserLabelledTextBox : LabelledTextBox, ICanAcceptFiles
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
2021-04-04 18:34:52 +08:00
|
|
|
private readonly string[] handledExtensions;
|
|
|
|
public IEnumerable<string> HandledExtensions => handledExtensions;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The target container to display the file chooser in.
|
|
|
|
/// </summary>
|
2020-10-06 14:17:15 +08:00
|
|
|
public Container Target;
|
|
|
|
|
2021-04-04 18:34:52 +08:00
|
|
|
private readonly Bindable<FileInfo> currentFile = new Bindable<FileInfo>();
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OsuGameBase game { get; set; }
|
2020-10-06 14:17:15 +08:00
|
|
|
|
2020-11-23 12:49:14 +08:00
|
|
|
[Resolved]
|
|
|
|
private SectionsContainer<SetupSection> sectionsContainer { get; set; }
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
protected override OsuTextBox CreateTextBox() =>
|
|
|
|
new FileChooserOsuTextBox
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
CornerRadius = CORNER_RADIUS,
|
|
|
|
OnFocused = DisplayFileChooser
|
|
|
|
};
|
|
|
|
|
|
|
|
public void DisplayFileChooser()
|
|
|
|
{
|
2020-11-23 12:49:14 +08:00
|
|
|
FileSelector fileSelector;
|
|
|
|
|
2021-04-04 18:34:52 +08:00
|
|
|
Target.Child = fileSelector = new FileSelector(currentFile.Value?.DirectoryName, handledExtensions)
|
2020-10-06 14:17:15 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = 400,
|
|
|
|
CurrentFile = { BindTarget = currentFile }
|
|
|
|
};
|
2020-11-23 12:49:14 +08:00
|
|
|
|
2020-11-24 14:54:27 +08:00
|
|
|
sectionsContainer.ScrollTo(fileSelector);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onFileSelected(ValueChangedEvent<FileInfo> file)
|
|
|
|
{
|
|
|
|
if (file.NewValue == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Target.Clear();
|
|
|
|
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 14:17:15 +08:00
|
|
|
internal class FileChooserOsuTextBox : OsuTextBox
|
|
|
|
{
|
|
|
|
public Action OnFocused;
|
|
|
|
|
|
|
|
protected override void OnFocus(FocusEvent e)
|
|
|
|
{
|
|
|
|
OnFocused?.Invoke();
|
|
|
|
base.OnFocus(e);
|
|
|
|
|
|
|
|
GetContainingInputManager().TriggerFocusContention(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|