1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 21:27:24 +08:00

Merge pull request #10233 from peppy/file-selector

Add a basic file selector with extension filtering support
This commit is contained in:
Dan Balasescu 2020-09-25 14:44:32 +09:00 committed by GitHub
commit 440adc305d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 170 additions and 34 deletions

View File

@ -3,7 +3,6 @@
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Platform;
using osu.Game.Graphics.UserInterfaceV2; using osu.Game.Graphics.UserInterfaceV2;
namespace osu.Game.Tests.Visual.Settings namespace osu.Game.Tests.Visual.Settings
@ -11,7 +10,7 @@ namespace osu.Game.Tests.Visual.Settings
public class TestSceneDirectorySelector : OsuTestScene public class TestSceneDirectorySelector : OsuTestScene
{ {
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host) private void load()
{ {
Add(new DirectorySelector { RelativeSizeAxes = Axes.Both }); Add(new DirectorySelector { RelativeSizeAxes = Axes.Both });
} }

View File

@ -0,0 +1,24 @@
// 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 NUnit.Framework;
using osu.Framework.Graphics;
using osu.Game.Graphics.UserInterfaceV2;
namespace osu.Game.Tests.Visual.Settings
{
public class TestSceneFileSelector : OsuTestScene
{
[Test]
public void TestAllFiles()
{
AddStep("create", () => Child = new FileSelector { RelativeSizeAxes = Axes.Both });
}
[Test]
public void TestJpgFilesOnly()
{
AddStep("create", () => Child = new FileSelector(validFileExtensions: new[] { ".jpg" }) { RelativeSizeAxes = Axes.Both });
}
}
}

View File

@ -129,7 +129,7 @@ namespace osu.Game.Tournament.Screens
protected virtual void ChangePath() protected virtual void ChangePath()
{ {
var target = directorySelector.CurrentDirectory.Value.FullName; var target = directorySelector.CurrentPath.Value.FullName;
var fileBasedIpc = ipc as FileBasedIPC; var fileBasedIpc = ipc as FileBasedIPC;
Logger.Log($"Changing Stable CE location to {target}"); Logger.Log($"Changing Stable CE location to {target}");

View File

@ -28,11 +28,11 @@ namespace osu.Game.Graphics.UserInterfaceV2
private GameHost host { get; set; } private GameHost host { get; set; }
[Cached] [Cached]
public readonly Bindable<DirectoryInfo> CurrentDirectory = new Bindable<DirectoryInfo>(); public readonly Bindable<DirectoryInfo> CurrentPath = new Bindable<DirectoryInfo>();
public DirectorySelector(string initialPath = null) public DirectorySelector(string initialPath = null)
{ {
CurrentDirectory.Value = new DirectoryInfo(initialPath ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)); CurrentPath.Value = new DirectoryInfo(initialPath ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -74,7 +74,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
} }
}; };
CurrentDirectory.BindValueChanged(updateDisplay, true); CurrentPath.BindValueChanged(updateDisplay, true);
} }
private void updateDisplay(ValueChangedEvent<DirectoryInfo> directory) private void updateDisplay(ValueChangedEvent<DirectoryInfo> directory)
@ -92,22 +92,27 @@ namespace osu.Game.Graphics.UserInterfaceV2
} }
else else
{ {
directoryFlow.Add(new ParentDirectoryPiece(CurrentDirectory.Value.Parent)); directoryFlow.Add(new ParentDirectoryPiece(CurrentPath.Value.Parent));
foreach (var dir in CurrentDirectory.Value.GetDirectories().OrderBy(d => d.Name)) directoryFlow.AddRange(GetEntriesForPath(CurrentPath.Value));
{
if ((dir.Attributes & FileAttributes.Hidden) == 0)
directoryFlow.Add(new DirectoryPiece(dir));
}
} }
} }
catch (Exception) catch (Exception)
{ {
CurrentDirectory.Value = directory.OldValue; CurrentPath.Value = directory.OldValue;
this.FlashColour(Color4.Red, 300); this.FlashColour(Color4.Red, 300);
} }
} }
protected virtual IEnumerable<DisplayPiece> GetEntriesForPath(DirectoryInfo path)
{
foreach (var dir in path.GetDirectories().OrderBy(d => d.Name))
{
if ((dir.Attributes & FileAttributes.Hidden) == 0)
yield return new DirectoryPiece(dir);
}
}
private class CurrentDirectoryDisplay : CompositeDrawable private class CurrentDirectoryDisplay : CompositeDrawable
{ {
[Resolved] [Resolved]
@ -126,7 +131,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
Origin = Anchor.Centre, Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Spacing = new Vector2(5), Spacing = new Vector2(5),
Height = DirectoryPiece.HEIGHT, Height = DisplayPiece.HEIGHT,
Direction = FillDirection.Horizontal, Direction = FillDirection.Horizontal,
}, },
}; };
@ -150,7 +155,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
flow.ChildrenEnumerable = new Drawable[] flow.ChildrenEnumerable = new Drawable[]
{ {
new OsuSpriteText { Text = "Current Directory: ", Font = OsuFont.Default.With(size: DirectoryPiece.HEIGHT), }, new OsuSpriteText { Text = "Current Directory: ", Font = OsuFont.Default.With(size: DisplayPiece.HEIGHT), },
new ComputerPiece(), new ComputerPiece(),
}.Concat(pathPieces); }.Concat(pathPieces);
} }
@ -198,24 +203,44 @@ namespace osu.Game.Graphics.UserInterfaceV2
} }
} }
private class DirectoryPiece : CompositeDrawable protected class DirectoryPiece : DisplayPiece
{ {
public const float HEIGHT = 20;
protected const float FONT_SIZE = 16;
protected readonly DirectoryInfo Directory; protected readonly DirectoryInfo Directory;
private readonly string displayName;
protected FillFlowContainer Flow;
[Resolved] [Resolved]
private Bindable<DirectoryInfo> currentDirectory { get; set; } private Bindable<DirectoryInfo> currentDirectory { get; set; }
public DirectoryPiece(DirectoryInfo directory, string displayName = null) public DirectoryPiece(DirectoryInfo directory, string displayName = null)
: base(displayName)
{ {
Directory = directory; Directory = directory;
}
protected override bool OnClick(ClickEvent e)
{
currentDirectory.Value = Directory;
return true;
}
protected override string FallbackName => Directory.Name;
protected override IconUsage? Icon => Directory.Name.Contains(Path.DirectorySeparatorChar)
? FontAwesome.Solid.Database
: FontAwesome.Regular.Folder;
}
protected abstract class DisplayPiece : CompositeDrawable
{
public const float HEIGHT = 20;
protected const float FONT_SIZE = 16;
private readonly string displayName;
protected FillFlowContainer Flow;
protected DisplayPiece(string displayName = null)
{
this.displayName = displayName; this.displayName = displayName;
} }
@ -259,20 +284,14 @@ namespace osu.Game.Graphics.UserInterfaceV2
{ {
Anchor = Anchor.CentreLeft, Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft, Origin = Anchor.CentreLeft,
Text = displayName ?? Directory.Name, Text = displayName ?? FallbackName,
Font = OsuFont.Default.With(size: FONT_SIZE) Font = OsuFont.Default.With(size: FONT_SIZE)
}); });
} }
protected override bool OnClick(ClickEvent e) protected abstract string FallbackName { get; }
{
currentDirectory.Value = Directory;
return true;
}
protected virtual IconUsage? Icon => Directory.Name.Contains(Path.DirectorySeparatorChar) protected abstract IconUsage? Icon { get; }
? FontAwesome.Solid.Database
: FontAwesome.Regular.Folder;
} }
} }
} }

View File

@ -0,0 +1,94 @@
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
namespace osu.Game.Graphics.UserInterfaceV2
{
public class FileSelector : DirectorySelector
{
private readonly string[] validFileExtensions;
[Cached]
public readonly Bindable<FileInfo> CurrentFile = new Bindable<FileInfo>();
public FileSelector(string initialPath = null, string[] validFileExtensions = null)
: base(initialPath)
{
this.validFileExtensions = validFileExtensions ?? Array.Empty<string>();
}
protected override IEnumerable<DisplayPiece> GetEntriesForPath(DirectoryInfo path)
{
foreach (var dir in base.GetEntriesForPath(path))
yield return dir;
IEnumerable<FileInfo> files = path.GetFiles();
if (validFileExtensions.Length > 0)
files = files.Where(f => validFileExtensions.Contains(f.Extension));
foreach (var file in files.OrderBy(d => d.Name))
{
if ((file.Attributes & FileAttributes.Hidden) == 0)
yield return new FilePiece(file);
}
}
protected class FilePiece : DisplayPiece
{
private readonly FileInfo file;
[Resolved]
private Bindable<FileInfo> currentFile { get; set; }
public FilePiece(FileInfo file)
{
this.file = file;
}
protected override bool OnClick(ClickEvent e)
{
currentFile.Value = file;
return true;
}
protected override string FallbackName => file.Name;
protected override IconUsage? Icon
{
get
{
switch (file.Extension)
{
case ".ogg":
case ".mp3":
case ".wav":
return FontAwesome.Regular.FileAudio;
case ".jpg":
case ".jpeg":
case ".png":
return FontAwesome.Regular.FileImage;
case ".mp4":
case ".avi":
case ".mov":
case ".flv":
return FontAwesome.Regular.FileVideo;
default:
return FontAwesome.Regular.File;
}
}
}
}
}
}

View File

@ -106,7 +106,7 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance
private void start() private void start()
{ {
var target = directorySelector.CurrentDirectory.Value; var target = directorySelector.CurrentPath.Value;
try try
{ {