Skip to content

Commit

Permalink
Some explorer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Sep 19, 2024
1 parent 422158a commit 54cf6d8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 52 deletions.
70 changes: 42 additions & 28 deletions src/Myra/Graphics2D/UI/Misc/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using Myra.Graphics2D.UI.Styles;
using System.ComponentModel;
using System.Xml.Serialization;


#if MONOGAME || FNA
using Microsoft.Xna.Framework;
Expand All @@ -20,30 +22,42 @@ public class TreeView : Widget, ITreeViewNode
{
private readonly StackPanelLayout _layout = new StackPanelLayout(Orientation.Vertical);
private readonly List<TreeViewNode> _allNodes = new List<TreeViewNode>();
private TreeViewNode _selectedRow;
private TreeViewNode _selectedNode;
private bool _rowInfosDirty = true;

internal List<TreeViewNode> AllNodes => _allNodes;

public int ChildNodesCount => Children.Count;

private TreeViewNode HoverRow { get; set; }
internal TreeViewNode HoverRow { get; set; }

[Browsable(false)]
[XmlIgnore]
[Obsolete("Use SelectedNode")]
public TreeViewNode SelectedRow
{
get => SelectedNode;

set => SelectedNode = value;
}

[Browsable(false)]
[XmlIgnore]
public TreeViewNode SelectedNode
{
get
{
return _selectedRow;
return _selectedNode;
}

set
{
if (value == _selectedRow)
if (value == _selectedNode)
{
return;
}

_selectedRow = value;
_selectedNode = value;

var ev = SelectionChanged;
if (ev != null)
Expand Down Expand Up @@ -74,17 +88,17 @@ public override void OnKeyDown(Keys k)
{
base.OnKeyDown(k);

if (SelectedRow == null)
if (SelectedNode == null)
{
return;
}

int index = 0;
IList<Widget> parentWidgets = null;
if (SelectedRow.ParentNode != null)
if (SelectedNode.ParentNode != null)
{
parentWidgets = SelectedRow.ParentNode.ChildNodesGrid.Widgets;
index = parentWidgets.IndexOf(SelectedRow);
parentWidgets = SelectedNode.ParentNode.ChildNodesGrid.Widgets;
index = parentWidgets.IndexOf(SelectedNode);
if (index == -1)
{
return;
Expand All @@ -94,50 +108,50 @@ public override void OnKeyDown(Keys k)
switch (k)
{
case Keys.Enter:
SelectedRow.IsExpanded = !SelectedRow.IsExpanded;
SelectedNode.IsExpanded = !SelectedNode.IsExpanded;
break;
case Keys.Up:
{
if (parentWidgets != null)
{
if (index == 0 && SelectedRow.ParentNode != null)
if (index == 0 && SelectedNode.ParentNode != null)
{
SelectedRow = SelectedRow.ParentNode;
SelectedNode = SelectedNode.ParentNode;
}
else if (index > 0)
{
var previousRow = (TreeViewNode)parentWidgets[index - 1];
if (!previousRow.IsExpanded || previousRow.ChildNodesCount == 0)
{
SelectedRow = previousRow;
SelectedNode = previousRow;
}
else
{
SelectedRow = (TreeViewNode)previousRow.ChildNodesGrid.Widgets[previousRow.ChildNodesCount - 1];
SelectedNode = (TreeViewNode)previousRow.ChildNodesGrid.Widgets[previousRow.ChildNodesCount - 1];
}
}
}
}
break;
case Keys.Down:
{
if (SelectedRow.IsExpanded && SelectedRow.ChildNodesCount > 0)
if (SelectedNode.IsExpanded && SelectedNode.ChildNodesCount > 0)
{
SelectedRow = (TreeViewNode)SelectedRow.ChildNodesGrid.Widgets[0];
SelectedNode = (TreeViewNode)SelectedNode.ChildNodesGrid.Widgets[0];
}
else if (parentWidgets != null && index + 1 < parentWidgets.Count)
{
SelectedRow = (TreeViewNode)parentWidgets[index + 1];
SelectedNode = (TreeViewNode)parentWidgets[index + 1];
}
else if (parentWidgets != null && index + 1 >= parentWidgets.Count)
{
var parentOfParent = SelectedRow.ParentNode.ParentNode;
var parentOfParent = SelectedNode.ParentNode.ParentNode;
if (parentOfParent != null)
{
var parentIndex = parentOfParent.ChildNodesGrid.Widgets.IndexOf(SelectedRow.ParentNode);
var parentIndex = parentOfParent.ChildNodesGrid.Widgets.IndexOf(SelectedNode.ParentNode);
if (parentIndex + 1 < parentOfParent.ChildNodesCount)
{
SelectedRow = (TreeViewNode)parentOfParent.ChildNodesGrid.Widgets[parentIndex + 1];
SelectedNode = (TreeViewNode)parentOfParent.ChildNodesGrid.Widgets[parentIndex + 1];
}
}
}
Expand All @@ -159,7 +173,7 @@ public override void OnTouchDown()

if (HoverRow != null && HoverRow.RowVisible)
{
SelectedRow = HoverRow;
SelectedNode = HoverRow;
}
}

Expand Down Expand Up @@ -249,10 +263,9 @@ public TreeViewNode AddSubNode(Widget content)

public void RemoveAllSubNodes()
{
HoverRow = SelectedNode = null;
Children.Clear();
_allNodes.Clear();
_selectedRow = null;
HoverRow = null;
}

private bool Iterate(TreeViewNode node, Func<TreeViewNode, bool> action)
Expand Down Expand Up @@ -328,18 +341,19 @@ public override void InternalRender(RenderContext context)
}
if (SelectionBackground != null)
{
if (HoverRow != null && HoverRow != SelectedRow && SelectionHoverBackground != null)
if (HoverRow != null && HoverRow != SelectedNode && SelectionHoverBackground != null)
{
var rect = BuildRowRect(HoverRow);
SelectionHoverBackground.Draw(context, rect);
}

if (SelectedRow != null && SelectedRow.RowVisible)
if (SelectedNode != null && SelectedNode.RowVisible)
{
var rect = BuildRowRect(SelectedRow);
var rect = BuildRowRect(SelectedNode);
SelectionBackground.Draw(context, rect);
}
} else
}
else
{
if (HoverRow != null && SelectionHoverBackground != null)
{
Expand Down Expand Up @@ -439,7 +453,7 @@ protected internal override void CopyFrom(Widget w)
SelectionBackground = treeView.SelectionBackground;
SelectionHoverBackground = treeView.SelectionHoverBackground;

foreach(TreeViewNode node in treeView.Children)
foreach (TreeViewNode node in treeView.Children)
{
AddSubNode(node.Content);
}
Expand Down
18 changes: 13 additions & 5 deletions src/Myra/Graphics2D/UI/Misc/TreeViewNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public bool IsExpanded

public int ContentHeight => _layout.GetRowHeight(0);

public int ChildNodesCount => _childNodesStackPanel.Children.Count;
public int ChildNodesCount => _childNodesStackPanel.Children.Count;

internal bool RowVisible { get; set; }

Expand Down Expand Up @@ -157,9 +157,17 @@ public void RemoveSubNode(TreeViewNode subNode)
{
_childNodesStackPanel.Children.Remove(subNode);
_topTree.AllNodes.Remove(subNode);
if (_topTree != null && _topTree.SelectedRow == subNode)
if (_topTree != null)
{
_topTree.SelectedRow = null;
if (_topTree.SelectedNode == subNode)
{
_topTree.SelectedNode = null;
}

if (_topTree.HoverRow == subNode)
{
_topTree.HoverRow = null;
}
}
}

Expand All @@ -168,9 +176,9 @@ public void RemoveSubNodeAt(int index)
var subNode = (TreeViewNode)_childNodesStackPanel.Children[index];
_childNodesStackPanel.Children.RemoveAt(index);
_topTree.AllNodes.Remove(subNode);
if (_topTree.SelectedRow == subNode)
if (_topTree.SelectedNode == subNode)
{
_topTree.SelectedRow = null;
_topTree.SelectedNode = null;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/MyraPad/AsyncTasksQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private void RefreshProc(object state)
}
catch (Exception ex)
{
Studio.MainForm.QueueClearExplorer();
Studio.MainForm.QueueSetStatusText(ex.Message);
}

Expand All @@ -71,6 +72,7 @@ private void RefreshProc(object state)
}
catch (Exception ex)
{
Studio.MainForm.QueueClearExplorer();
Studio.MainForm.QueueSetStatusText(ex.Message);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/MyraPad/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class Studio : Game
private readonly GraphicsDeviceManager _graphicsDeviceManager;
private readonly State _state;
private Desktop _desktop;
private StudioWidget _ui;
private MainForm _ui;

public static Studio Instance => _instance;

public static StudioWidget MainForm => Instance._ui;
public static MainForm MainForm => Instance._ui;

public Project Project => _ui.Project;

Expand Down Expand Up @@ -87,7 +87,7 @@ protected override void LoadContent()

_desktop = new Desktop();

_ui = new StudioWidget(_state);
_ui = new MainForm(_state);
_desktop.Root = _ui;

if (_state != null && !string.IsNullOrEmpty(_state.EditedFile) && File.Exists(_state.EditedFile))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generated by MyraPad at 9/18/2024 5:26:28 PM */
/* Generated by MyraPad at 9/19/2024 11:10:13 AM */
using Myra;
using Myra.Graphics2D;
using Myra.Graphics2D.TextureAtlases;
Expand All @@ -22,7 +22,7 @@

namespace MyraPad.UI
{
partial class StudioWidget: VerticalStackPanel
partial class MainForm: VerticalStackPanel
{
private void BuildUI()
{
Expand Down
Loading

0 comments on commit 54cf6d8

Please sign in to comment.