2020-05-12 17:12:58 +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.
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
using System;
|
2020-05-12 17:12:58 +08:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2020-05-13 17:08:43 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2020-05-12 17:12:58 +08:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Settings
|
|
|
|
{
|
|
|
|
public class TestSceneDirectorySelector : OsuTestScene
|
|
|
|
{
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(GameHost host)
|
|
|
|
{
|
|
|
|
Add(new DirectorySelector { RelativeSizeAxes = Axes.Both });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class DirectorySelector : CompositeDrawable
|
|
|
|
{
|
|
|
|
private FillFlowContainer directoryFlow;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private GameHost host { get; set; }
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
[Cached]
|
|
|
|
private readonly Bindable<DirectoryInfo> currentDirectory = new Bindable<DirectoryInfo>();
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
public DirectorySelector(string initialPath = null)
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
2020-05-13 17:08:43 +08:00
|
|
|
currentDirectory.Value = new DirectoryInfo(initialPath ??= Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
|
2020-05-12 17:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
Padding = new MarginPadding(10);
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
new FillFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2020-05-13 17:08:43 +08:00
|
|
|
new CurrentDirectoryDisplay
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = 50,
|
|
|
|
},
|
|
|
|
new OsuScrollContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Child = directoryFlow = new FillFlowContainer
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
currentDirectory.BindValueChanged(updateDisplay, true);
|
2020-05-12 17:12:58 +08:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
private void updateDisplay(ValueChangedEvent<DirectoryInfo> directory)
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
|
|
|
directoryFlow.Clear();
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
if (directory.NewValue == null)
|
|
|
|
{
|
|
|
|
// var drives = DriveInfo.GetDrives();
|
|
|
|
//
|
|
|
|
// foreach (var drive in drives)
|
|
|
|
// directoryFlow.Add(new DirectoryRow(drive.RootDirectory));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
directoryFlow.Add(new ParentDirectoryRow(currentDirectory.Value.Parent));
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
foreach (var dir in currentDirectory.Value.GetDirectories().OrderBy(d => d.Name))
|
|
|
|
{
|
|
|
|
if ((dir.Attributes & FileAttributes.Hidden) == 0)
|
|
|
|
directoryFlow.Add(new DirectoryRow(dir));
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 17:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public class CurrentDirectoryDisplay : CompositeDrawable
|
|
|
|
{
|
2020-05-13 17:08:43 +08:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<DirectoryInfo> currentDirectory { get; set; }
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
|
|
|
FillFlowContainer flow;
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
flow = new FillFlowContainer
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Spacing = new Vector2(10),
|
|
|
|
Height = 20,
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
currentDirectory.BindValueChanged(dir =>
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
|
|
|
flow.Clear();
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
flow.Add(new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = "Current Directory: ",
|
|
|
|
Font = OsuFont.Default.With(size: DirectoryRow.HEIGHT),
|
|
|
|
});
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
flow.Add(new DirectoryRow(null, "Computer"));
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
DirectoryInfo traverse = dir.NewValue;
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
while (traverse != null)
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
2020-05-13 17:08:43 +08:00
|
|
|
flow.Add(new DirectoryRow(traverse));
|
|
|
|
traverse = traverse.Parent;
|
2020-05-12 17:12:58 +08:00
|
|
|
}
|
2020-05-13 17:08:43 +08:00
|
|
|
}, true);
|
2020-05-12 17:12:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ParentDirectoryRow : DirectoryRow
|
|
|
|
{
|
|
|
|
public override IconUsage Icon => FontAwesome.Solid.Folder;
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
public ParentDirectoryRow(DirectoryInfo directory)
|
|
|
|
: base(directory, "..")
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
private class DirectoryRow : CompositeDrawable
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
2020-05-13 17:08:43 +08:00
|
|
|
public const float HEIGHT = 20;
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
private readonly DirectoryInfo directory;
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<DirectoryInfo> currentDirectory { get; set; }
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
public DirectoryRow(DirectoryInfo directory, string display = null)
|
|
|
|
{
|
|
|
|
this.directory = directory;
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
AutoSizeAxes = Axes.X;
|
|
|
|
Height = HEIGHT;
|
2020-05-12 17:12:58 +08:00
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
AddRangeInternal(new Drawable[]
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
|
|
|
new SpriteIcon
|
|
|
|
{
|
|
|
|
Icon = Icon,
|
2020-05-13 17:08:43 +08:00
|
|
|
Size = new Vector2(HEIGHT)
|
2020-05-12 17:12:58 +08:00
|
|
|
},
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
2020-05-13 17:08:43 +08:00
|
|
|
X = HEIGHT + 5,
|
|
|
|
Text = display ?? directory.Name,
|
|
|
|
Font = OsuFont.Default.With(size: HEIGHT)
|
2020-05-12 17:12:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:08:43 +08:00
|
|
|
protected override bool OnClick(ClickEvent e)
|
2020-05-12 17:12:58 +08:00
|
|
|
{
|
2020-05-13 17:08:43 +08:00
|
|
|
currentDirectory.Value = directory;
|
|
|
|
return true;
|
2020-05-12 17:12:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual IconUsage Icon => FontAwesome.Regular.Folder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|