1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 03:57:25 +08:00
osu-lazer/osu.Game.Tests/Visual/Settings/TestSceneDirectorySelector.cs

202 lines
6.3 KiB
C#
Raw Normal View History

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.
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;
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; }
[Cached]
private readonly Bindable<DirectoryInfo> currentDirectory = new Bindable<DirectoryInfo>();
2020-05-12 17:12:58 +08:00
public DirectorySelector(string initialPath = null)
2020-05-12 17:12:58 +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[]
{
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,
}
}
}
},
};
currentDirectory.BindValueChanged(updateDisplay, true);
2020-05-12 17:12:58 +08:00
}
private void updateDisplay(ValueChangedEvent<DirectoryInfo> directory)
2020-05-12 17:12:58 +08:00
{
directoryFlow.Clear();
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
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
{
[Resolved]
private Bindable<DirectoryInfo> currentDirectory { get; set; }
2020-05-12 17:12:58 +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,
},
};
currentDirectory.BindValueChanged(dir =>
2020-05-12 17:12:58 +08:00
{
flow.Clear();
flow.Add(new OsuSpriteText
{
Text = "Current Directory: ",
Font = OsuFont.Default.With(size: DirectoryRow.HEIGHT),
});
2020-05-12 17:12:58 +08:00
flow.Add(new DirectoryRow(null, "Computer"));
2020-05-12 17:12:58 +08:00
DirectoryInfo traverse = dir.NewValue;
2020-05-12 17:12:58 +08:00
while (traverse != null)
2020-05-12 17:12:58 +08:00
{
flow.Add(new DirectoryRow(traverse));
traverse = traverse.Parent;
2020-05-12 17:12:58 +08:00
}
}, true);
2020-05-12 17:12:58 +08:00
}
}
private class ParentDirectoryRow : DirectoryRow
{
public override IconUsage Icon => FontAwesome.Solid.Folder;
public ParentDirectoryRow(DirectoryInfo directory)
: base(directory, "..")
2020-05-12 17:12:58 +08:00
{
}
}
private class DirectoryRow : CompositeDrawable
2020-05-12 17:12:58 +08:00
{
public const float HEIGHT = 20;
2020-05-12 17:12:58 +08:00
private readonly DirectoryInfo directory;
2020-05-12 17:12:58 +08:00
[Resolved]
private Bindable<DirectoryInfo> currentDirectory { get; set; }
2020-05-12 17:12:58 +08:00
public DirectoryRow(DirectoryInfo directory, string display = null)
{
this.directory = directory;
2020-05-12 17:12:58 +08:00
AutoSizeAxes = Axes.X;
Height = HEIGHT;
2020-05-12 17:12:58 +08:00
AddRangeInternal(new Drawable[]
2020-05-12 17:12:58 +08:00
{
new SpriteIcon
{
Icon = Icon,
Size = new Vector2(HEIGHT)
2020-05-12 17:12:58 +08:00
},
new OsuSpriteText
{
X = HEIGHT + 5,
Text = display ?? directory.Name,
Font = OsuFont.Default.With(size: HEIGHT)
2020-05-12 17:12:58 +08:00
}
});
}
protected override bool OnClick(ClickEvent e)
2020-05-12 17:12:58 +08:00
{
currentDirectory.Value = directory;
return true;
2020-05-12 17:12:58 +08:00
}
public virtual IconUsage Icon => FontAwesome.Regular.Folder;
}
}
}