osu/osu.Game/Graphics/UserInterfaceV2/DirectorySelector.cs

298 lines
9.5 KiB
C#
Raw Normal View History

2020-05-13 09:37:14 +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;
using System.Collections.Generic;
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.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Platform;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;
2020-05-13 09:37:14 +00:00
namespace osu.Game.Graphics.UserInterfaceV2
{
public class DirectorySelector : CompositeDrawable
{
private FillFlowContainer directoryFlow;
[Resolved]
private GameHost host { get; set; }
[Cached]
public readonly Bindable<DirectoryInfo> CurrentPath = new Bindable<DirectoryInfo>();
2020-05-13 09:37:14 +00:00
public DirectorySelector(string initialPath = null)
{
CurrentPath.Value = new DirectoryInfo(initialPath ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
2020-05-13 09:37:14 +00:00
}
2020-05-13 09:52:10 +00:00
[BackgroundDependencyLoader]
private void load()
2020-05-13 09:37:14 +00:00
{
Padding = new MarginPadding(10);
InternalChild = new GridContainer
2020-05-13 09:37:14 +00:00
{
RelativeSizeAxes = Axes.Both,
RowDimensions = new[]
2020-05-13 09:37:14 +00:00
{
new Dimension(GridSizeMode.Absolute, 50),
new Dimension(),
},
Content = new[]
{
new Drawable[]
2020-05-13 09:37:14 +00:00
{
new CurrentDirectoryDisplay
{
RelativeSizeAxes = Axes.Both,
2020-05-13 09:37:14 +00:00
},
},
new Drawable[]
{
2020-05-13 09:37:14 +00:00
new OsuScrollContainer
{
RelativeSizeAxes = Axes.Both,
Child = directoryFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Spacing = new Vector2(2),
}
}
}
}
2020-05-13 09:37:14 +00:00
};
CurrentPath.BindValueChanged(updateDisplay, true);
2020-05-13 09:37:14 +00:00
}
private void updateDisplay(ValueChangedEvent<DirectoryInfo> directory)
{
directoryFlow.Clear();
try
2020-05-13 09:37:14 +00:00
{
if (directory.NewValue == null)
{
var drives = DriveInfo.GetDrives();
2020-05-13 09:37:14 +00:00
foreach (var drive in drives)
directoryFlow.Add(new DirectoryPiece(drive.RootDirectory));
}
else
{
directoryFlow.Add(new ParentDirectoryPiece(CurrentPath.Value.Parent));
directoryFlow.AddRange(GetEntriesForPath(CurrentPath.Value));
}
2020-05-13 09:37:14 +00:00
}
catch (Exception)
2020-05-13 09:37:14 +00:00
{
CurrentPath.Value = directory.OldValue;
this.FlashColour(Color4.Red, 300);
2020-05-13 09:37:14 +00:00
}
}
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);
}
}
2020-05-13 09:52:10 +00:00
private class CurrentDirectoryDisplay : CompositeDrawable
2020-05-13 09:37:14 +00:00
{
[Resolved]
private Bindable<DirectoryInfo> currentDirectory { get; set; }
2020-05-13 09:52:10 +00:00
private FillFlowContainer flow;
2020-05-13 09:37:14 +00:00
[BackgroundDependencyLoader]
private void load()
{
InternalChildren = new Drawable[]
{
flow = new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
Spacing = new Vector2(5),
Height = DisplayPiece.HEIGHT,
2020-05-13 09:37:14 +00:00
Direction = FillDirection.Horizontal,
},
};
2020-05-13 09:52:10 +00:00
currentDirectory.BindValueChanged(updateDisplay, true);
}
2020-05-13 09:37:14 +00:00
2020-05-13 09:52:10 +00:00
private void updateDisplay(ValueChangedEvent<DirectoryInfo> dir)
{
flow.Clear();
2020-05-13 09:37:14 +00:00
2020-05-13 09:52:10 +00:00
List<DirectoryPiece> pathPieces = new List<DirectoryPiece>();
2020-05-13 09:37:14 +00:00
2020-05-13 09:52:10 +00:00
DirectoryInfo ptr = dir.NewValue;
2020-05-13 09:37:14 +00:00
2020-05-13 09:52:10 +00:00
while (ptr != null)
{
pathPieces.Insert(0, new CurrentDisplayPiece(ptr));
ptr = ptr.Parent;
}
2020-05-13 09:37:14 +00:00
2020-05-13 09:52:10 +00:00
flow.ChildrenEnumerable = new Drawable[]
{
new OsuSpriteText { Text = "Current Directory: ", Font = OsuFont.Default.With(size: DisplayPiece.HEIGHT), },
2020-05-13 09:52:10 +00:00
new ComputerPiece(),
}.Concat(pathPieces);
2020-05-13 09:37:14 +00:00
}
2020-05-13 09:52:10 +00:00
private class ComputerPiece : CurrentDisplayPiece
2020-05-13 09:37:14 +00:00
{
protected override IconUsage? Icon => null;
2020-05-13 09:37:14 +00:00
2020-05-13 09:52:10 +00:00
public ComputerPiece()
2020-05-13 09:37:14 +00:00
: base(null, "Computer")
{
}
}
2020-05-13 09:52:10 +00:00
private class CurrentDisplayPiece : DirectoryPiece
2020-05-13 09:37:14 +00:00
{
2020-05-13 09:52:10 +00:00
public CurrentDisplayPiece(DirectoryInfo directory, string displayName = null)
2020-05-13 09:37:14 +00:00
: base(directory, displayName)
{
}
[BackgroundDependencyLoader]
private void load()
{
Flow.Add(new SpriteIcon
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = FontAwesome.Solid.ChevronRight,
Size = new Vector2(FONT_SIZE / 2)
});
}
2020-05-13 09:52:10 +00:00
protected override IconUsage? Icon => Directory.Name.Contains(Path.DirectorySeparatorChar) ? base.Icon : null;
2020-05-13 09:37:14 +00:00
}
}
2020-05-13 09:52:10 +00:00
private class ParentDirectoryPiece : DirectoryPiece
2020-05-13 09:37:14 +00:00
{
protected override IconUsage? Icon => FontAwesome.Solid.Folder;
2020-05-13 09:37:14 +00:00
2020-05-13 09:52:10 +00:00
public ParentDirectoryPiece(DirectoryInfo directory)
2020-05-13 09:37:14 +00:00
: base(directory, "..")
{
}
}
protected class DirectoryPiece : DisplayPiece
{
protected readonly DirectoryInfo Directory;
[Resolved]
private Bindable<DirectoryInfo> currentDirectory { get; set; }
public DirectoryPiece(DirectoryInfo directory, string displayName = null)
: base(displayName)
{
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
2020-05-13 09:37:14 +00:00
{
public const float HEIGHT = 20;
protected const float FONT_SIZE = 16;
private readonly string displayName;
protected FillFlowContainer Flow;
protected DisplayPiece(string displayName = null)
2020-05-13 09:37:14 +00:00
{
this.displayName = displayName;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AutoSizeAxes = Axes.Both;
Masking = true;
CornerRadius = 5;
InternalChildren = new Drawable[]
{
new Box
{
Colour = colours.GreySeafoamDarker,
RelativeSizeAxes = Axes.Both,
},
Flow = new FillFlowContainer
{
AutoSizeAxes = Axes.X,
Height = 20,
Margin = new MarginPadding { Vertical = 2, Horizontal = 5 },
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5),
}
};
if (Icon.HasValue)
{
Flow.Add(new SpriteIcon
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = Icon.Value,
Size = new Vector2(FONT_SIZE)
});
}
Flow.Add(new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Text = displayName ?? FallbackName,
2020-05-13 09:37:14 +00:00
Font = OsuFont.Default.With(size: FONT_SIZE)
});
}
protected abstract string FallbackName { get; }
2020-05-13 09:37:14 +00:00
protected abstract IconUsage? Icon { get; }
2020-05-13 09:37:14 +00:00
}
}
}