-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a component to show a tree of folders from the file system
- Loading branch information
1 parent
b94d751
commit 55bbae6
Showing
7 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Source/Editor/AGS.Editor/Components/FileSelector/FolderNodeViewProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using AGS.API; | ||
using AGS.Engine; | ||
|
||
namespace AGS.Editor | ||
{ | ||
public class FolderNodeViewProvider: ITreeNodeViewProvider | ||
{ | ||
private readonly ITreeNodeViewProvider _inner; | ||
private readonly IGameFactory _factory; | ||
private const string FOLDER_HOVERED = "FolderHovered"; | ||
|
||
private static int _nextNodeId; | ||
|
||
public FolderNodeViewProvider(ITreeNodeViewProvider inner, IGameFactory factory) | ||
{ | ||
_inner = inner; | ||
_factory = factory; | ||
} | ||
|
||
public void BeforeDisplayingNode(ITreeStringNode item, ITreeNodeView nodeView, bool isCollapsed, bool isHovered, bool isSelected) | ||
{ | ||
isHovered |= item.Properties.Bools.GetValue(FOLDER_HOVERED); | ||
_inner.BeforeDisplayingNode(item, nodeView, isCollapsed, isHovered, isSelected); | ||
var folderIcon = (ILabel) nodeView.ExpandButton.TreeNode.Children.First(c => c.ID.StartsWith("FolderIcon", StringComparison.InvariantCulture)); | ||
folderIcon.Text = isSelected ? FontIcons.FolderOpen : FontIcons.Folder; | ||
folderIcon.TextConfig.Brush = isHovered ? GameViewColors.HoveredTextBrush : GameViewColors.TextBrush; | ||
} | ||
|
||
public ITreeNodeView CreateNode(ITreeStringNode node, IRenderLayer layer, IObject parent) | ||
{ | ||
var view = _inner.CreateNode(node, layer, parent); | ||
int nodeId = Interlocked.Increment(ref _nextNodeId); | ||
var folderIcon = _factory.UI.GetLabel($"FolderIcon_{node.Text}_{nodeId}", "", 25f, 25f, 15f, 0f, view.ExpandButton, AGSTextConfig.Clone(FontIcons.IconConfig)); | ||
folderIcon.Text = FontIcons.Folder; | ||
folderIcon.IsPixelPerfect = false; | ||
folderIcon.Enabled = true; | ||
folderIcon.MouseEnter.Subscribe(() => | ||
{ | ||
node.Properties.Bools.SetValue(FOLDER_HOVERED, true); | ||
view.OnRefreshDisplayNeeded.Invoke(); | ||
}); | ||
folderIcon.MouseLeave.Subscribe(() => | ||
{ | ||
node.Properties.Bools.SetValue(FOLDER_HOVERED, false); | ||
view.OnRefreshDisplayNeeded.Invoke(); | ||
}); | ||
folderIcon.MouseClicked.Subscribe(async (args) => await view.TreeItem.MouseClicked.InvokeAsync(args)); | ||
return view; | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
Source/Editor/AGS.Editor/Components/FileSelector/FolderTree.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AGS.API; | ||
using AGS.Engine; | ||
|
||
namespace AGS.Editor | ||
{ | ||
[RequiredComponent(typeof(ITreeViewComponent))] | ||
public class FolderTree: AGSComponent | ||
{ | ||
private ITreeViewComponent _treeView; | ||
private string _defaultFolder; | ||
private readonly IDevice _device; | ||
private Folder _root; | ||
private readonly IGameFactory _factory; | ||
|
||
public FolderTree(IDevice device, IGameFactory factory) | ||
{ | ||
_device = device; | ||
_factory = factory; | ||
} | ||
|
||
public string DefaultFolder | ||
{ | ||
get => _defaultFolder; | ||
set | ||
{ | ||
_defaultFolder = value; | ||
buildTreeModel(); | ||
} | ||
} | ||
|
||
public override void Init() | ||
{ | ||
base.Init(); | ||
Entity.Bind<ITreeViewComponent>(c => { _treeView = c; configureTreeUI(); refreshTreeUI(); }, _ => _treeView = null); | ||
} | ||
|
||
private async void buildTreeModel() | ||
{ | ||
_root = await buildTreeModel(DefaultFolder); | ||
refreshTreeUI(); | ||
} | ||
|
||
private async Task<Folder> buildTreeModel(string path) | ||
{ | ||
var dirs = (await Task.Run(() => _device.FileSystem.GetDirectories(path))).ToArray(); | ||
List<Folder> folders = new List<Folder>(dirs.Length); | ||
foreach (var dir in dirs) | ||
{ | ||
folders.Add(await buildTreeModel(dir)); | ||
} | ||
return new Folder(Path.GetFileName(path), path, folders.ToArray()); | ||
} | ||
|
||
private void configureTreeUI() | ||
{ | ||
var tree = _treeView; | ||
if (tree == null) return; | ||
tree.NodeViewProvider = new FolderNodeViewProvider(tree.NodeViewProvider, _factory); | ||
} | ||
|
||
private void refreshTreeUI() | ||
{ | ||
var root = _root; | ||
var tree = _treeView; | ||
if (root == null || tree == null) return; | ||
|
||
tree.Tree = _root.ToNode(GameViewColors.ButtonTextConfig.Font); | ||
tree.Expand(tree.Tree); | ||
} | ||
|
||
private class Folder | ||
{ | ||
public Folder(string name, string fullPath, Folder[] folders) | ||
{ | ||
Name = name; | ||
FullPath = fullPath; | ||
Folders = folders; | ||
} | ||
|
||
public string FullPath { get; } | ||
public string Name { get; } | ||
public Folder[] Folders { get; } | ||
|
||
public ITreeStringNode ToNode(IFont font) | ||
{ | ||
AGSTreeStringNode node = new AGSTreeStringNode(Name, font); | ||
if (Folders.Length > 0) | ||
{ | ||
node.TreeNode.AddChildren(Folders.Select(f => f.ToNode(font)).ToList()); | ||
} | ||
return node; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters