A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Collections.Generic;
5using System.IO;
6using System.Linq;
7using JetBrains.Annotations;
8using osu.Framework.Allocation;
9using osu.Framework.Bindables;
10using osu.Framework.Graphics.Containers;
11using osuTK;
12
13namespace osu.Framework.Graphics.UserInterface
14{
15 public abstract class DirectorySelectorBreadcrumbDisplay : CompositeDrawable
16 {
17 /// <summary>
18 /// Creates a caption to be displayed in front of the breadcrumb items.
19 /// </summary>
20 [CanBeNull]
21 protected virtual Drawable CreateCaption() => null;
22
23 /// <summary>
24 /// Create a directory item in the breadcrumb trail.
25 /// </summary>
26 protected abstract DirectorySelectorDirectory CreateDirectoryItem(DirectoryInfo directory, string displayName = null);
27
28 /// <summary>
29 /// Create the root directory item in the breadcrumb trail.
30 /// </summary>
31 protected abstract DirectorySelectorDirectory CreateRootDirectoryItem();
32
33 [Resolved]
34 private Bindable<DirectoryInfo> currentDirectory { get; set; }
35
36 private FillFlowContainer flow;
37
38 [BackgroundDependencyLoader]
39 private void load()
40 {
41 InternalChild = flow = new FillFlowContainer
42 {
43 Anchor = Anchor.Centre,
44 Origin = Anchor.Centre,
45 RelativeSizeAxes = Axes.X,
46 AutoSizeAxes = Axes.Y,
47 Spacing = new Vector2(5),
48 Direction = FillDirection.Horizontal,
49 };
50
51 currentDirectory.BindValueChanged(updateDisplay, true);
52 }
53
54 private void updateDisplay(ValueChangedEvent<DirectoryInfo> dir)
55 {
56 flow.Clear();
57
58 List<DirectorySelectorDirectory> pathPieces = new List<DirectorySelectorDirectory>();
59
60 DirectoryInfo ptr = dir.NewValue;
61
62 while (ptr != null)
63 {
64 pathPieces.Insert(0, CreateDirectoryItem(ptr));
65 ptr = ptr.Parent;
66 }
67
68 var caption = CreateCaption();
69 if (caption != null)
70 flow.Add(caption);
71
72 flow.AddRange(pathPieces.Prepend(CreateRootDirectoryItem()));
73 }
74 }
75}