Refactor #1266 (code quality and simplicity)

This commit is contained in:
Wojtek Figat
2023-09-11 20:02:53 +02:00
parent 2ddb44afa8
commit 16fed8927c
14 changed files with 241 additions and 584 deletions

View File

@@ -1,257 +0,0 @@
using FlaxEditor.Content;
using FlaxEditor.Content.GUI;
using FlaxEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlaxEditor.Content
{
/// <summary>
/// class with is controling visability of items in content window
/// </summary>
internal class ContentFilter
{
#region Filters Config
/// <summary>
/// all suported files by engine Content Folder
/// </summary>
public static readonly List<string> SuportedFileExtencionsInContentFolder = new List<string>()
{
".flax",
".json",
".scene",
".prefab",
};
/// <summary>
/// all suported files by engine Source Folder
/// </summary>
public static readonly List<string> SuportedFileExtencionsInSourceFolder = new List<string>()
{
".shader",
".cs",
".h",
".cpp",
};
/// <summary>
/// ignores folders in source folder (top layer),
/// for example obj folder is default c# project folder
/// </summary>
internal static readonly List<string> HideFoldersInSourceFolder = new List<string>()
{
"obj", // default c# project folder
"Properties", // c# project stuff ?
};
/// <summary>
/// ignores files in source folder (top layer),
/// </summary>
internal static readonly List<string> HideFilesInSourceFolder = new List<string>() //dont edit
{
"Game.csproj", //solucion file
"Game.Gen.cs", //auto-generated not be edited
"GameEditorTarget.Build.cs",
"GameTarget.Build.cs",
};
#endregion
internal static ContentItem gameSettings;
internal static List<ContentItem> buildFiles;
internal static ContentTreeNode settings;
internal static ContentTreeNode shaders;
internal static ProjectTreeNode engine;
internal static List<ProjectTreeNode> plugins = new();
internal static List<ContentItem> FilterFolder(ContentFolder folder)
{
return FilterFolder(folder, SuportedFileExtencionsInContentFolder.ToArray(), SuportedFileExtencionsInSourceFolder.ToArray());
}
internal static List<ContentItem> FilterFolder(ContentFolder folder, string[] FileExtencionsInContentFolder, string[] ExtencionsInSourceFolder)
{
if (folder.CanHaveAssets)
{
for (int i = 0; i < folder.Children.Count; i++)
{
bool Visible = false;
for (int j = 0; j < FileExtencionsInContentFolder.Length; j++)
{
if ((folder.Children[i].Path.EndsWith(FileExtencionsInContentFolder[j]) || folder.Children[i].IsFolder))
{
if (folder.Children[i].Visible)
{
Visible = true;
}
break;
}
}
folder.Children[i].Visible = Visible;
}
}
else if (folder.CanHaveScripts)
{
for (int i = 0; i < folder.Children.Count; i++)
{
bool Visible = false;
for (int j = 0; j < ExtencionsInSourceFolder.Length; j++)
{
if ((folder.Children[i].Path.EndsWith(ExtencionsInSourceFolder[j]) || folder.Children[i].IsFolder))
{
if (folder.Children[i].Visible)
{
Visible = true;
}
break;
}
}
folder.Children[i].Visible = Visible;
}
}
return folder.Children;
}
internal static ProjectTreeNode Filter(ProjectTreeNode tree)
{
var content = tree.Children[0] as ContentTreeNode;
var source = tree.Children[1] as ContentTreeNode;
//filter content folder (top layer)
buildFiles = new();
for (int i = 0; i < content.Folder.Children.Count; i++)
{
if (content.Folder.Children[i].FileName == "GameSettings.json")
{
gameSettings = content.Folder.Children[i];
content.Folder.Children[i].Visible = false;
break;
}
}
int hindenCount = 0;
for (int i = content.Children.Count - 1; i >= 0; i--)//we are starting from back it faster
{
var node = content.Children[i] as ContentTreeNode;
if (node.Folder.FileName == "Settings")
{
settings = node;
hindenCount++;
node.Visible = false;
node.Folder.Visible = false;
}
if (node.Folder.FileName == "Shaders")
{
shaders = node;
hindenCount++;
node.Visible = false;
node.Folder.Visible = false;
}
if (hindenCount == 2)
break;
}
//-----------------------------------------------------------------------------------------------------
//filter source folder (top layer)
hindenCount = 0;
for (int i = 0; i < source.Folder.Children.Count; i++)
{
for (int j = 0; j < HideFilesInSourceFolder.Count; j++)
{
if (source.Folder.Children[i].FileName == HideFilesInSourceFolder[j])
{
source.Folder.Children[i].Visible = false;
hindenCount++;
if(i > 1)
{
buildFiles.Add(source.Folder.Children[i]);
}
if (HideFilesInSourceFolder.Count == hindenCount) goto HideFilesInSourceFolderComplited;
break;
}
}
}
HideFilesInSourceFolderComplited:
hindenCount = 0;
for (int i = source.Children.Count - 1; i >= 0; i--)
{
var node = source.Children[i] as ContentTreeNode;
for (int j = 0; j < HideFoldersInSourceFolder.Count; j++)
{
if (node.Folder.FileName == HideFoldersInSourceFolder[j])
{
node.Visible = false;
node.Folder.Visible = false;
hindenCount++;
if (HideFoldersInSourceFolder.Count == hindenCount) goto HideFoldersInSourceFolderComplited;
break;
}
}
}
HideFoldersInSourceFolderComplited:
//content
return tree;
}
internal static void UpdateFilterVisability(ContentSettingsDropdown dropdown)
{
engine.Visible = false;
engine.Folder.Visible = false;
foreach (var item in plugins)
{
item.Visible = false;
item.Folder.Visible = false;
}
foreach (var item in buildFiles)
{
item.Visible = false;
}
gameSettings.Visible = false;
settings.Visible = false;
settings.Folder.Visible = false;
shaders.Visible = false;
shaders.Folder.Visible = false;
for (int i = 0; i < dropdown.Selection.Count; i++)
{
if (dropdown.Selection[i] == 0) //Show Engine Content
{
engine.Visible = true;
engine.Folder.Visible = true;
}
if (dropdown.Selection[i] == 1)//Show Plugin Content
{
foreach (var item in plugins)
{
item.Visible = true;
item.Folder.Visible = true;
}
}
if (dropdown.Selection[i] == 2)//Show Build Files
{
foreach (var item in buildFiles)
{
item.Visible = true;
}
}
if (dropdown.Selection[i] == 3)//Show Game Settings
{
gameSettings.Visible = true;
settings.Visible = true;
settings.Folder.Visible = true;
}
if (dropdown.Selection[i] == 4)//"Show Shader Source"
{
shaders.Visible = true;
shaders.Folder.Visible = true;
}
}
engine.ParentTree.PerformLayout();
Editor.Instance.Windows.ContentWin.RefreshView();
}
}
}

View File

@@ -1,6 +1,7 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using FlaxEditor.GUI;
using FlaxEditor.GUI.ContextMenu;
using FlaxEditor.GUI.Drag;
using FlaxEngine;
using FlaxEngine.GUI;
@@ -72,7 +73,6 @@ namespace FlaxEditor.Content.GUI
if (_dragOverItems == null)
_dragOverItems = new DragItems(ValidateDragItem);
_dragOverItems.OnDragEnter(data);
var result = GetDragEffect(data);
_validDragOver = result != DragDropEffect.None;
@@ -122,4 +122,70 @@ namespace FlaxEditor.Content.GUI
return result;
}
}
sealed class ContentNavigationSeparator : ComboBox
{
public ContentNavigationButton Target;
public ContentNavigationSeparator(ContentNavigationButton target, float x, float y, float height)
{
Target = target;
Bounds = new Rectangle(x, y, 16, height);
Offsets = new Margin(Bounds.X, Bounds.Width, Bounds.Y, Bounds.Height);
UpdateTransform();
MaximumItemsInViewCount = 20;
var style = Style.Current;
BackgroundColor = style.BackgroundNormal;
BackgroundColorHighlighted = BackgroundColor;
BackgroundColorSelected = BackgroundColor;
}
protected override ContextMenu OnCreatePopup()
{
// Update items
ClearItems();
foreach (var child in Target.TargetNode.Children)
{
if (child is ContentTreeNode node)
{
if (node.Folder.VisibleInHierarchy) // Respect the filter set by ContentFilterConfig.Filter(...)
AddItem(node.Folder.ShortName);
}
}
return base.OnCreatePopup();
}
public override void Draw()
{
var style = Style.Current;
var rect = new Rectangle(Float2.Zero, Size);
var color = IsDragOver ? style.BackgroundSelected * 0.6f : (_mouseDown ? style.BackgroundSelected : (IsMouseOver ? style.BackgroundHighlighted : Color.Transparent));
Render2D.FillRectangle(rect, color);
Render2D.DrawSprite(Editor.Instance.Icons.ArrowRight12, new Rectangle(rect.Location.X, rect.Y + rect.Size.Y * 0.25f, rect.Size.X, rect.Size.X), EnabledInHierarchy ? style.Foreground : style.ForegroundDisabled);
}
protected override void OnLayoutMenuButton(ContextMenuButton button, int index, bool construct = false)
{
button.Icon = Editor.Instance.Icons.FolderClosed32;
if (_tooltips != null && _tooltips.Length > index)
button.TooltipText = _tooltips[index];
}
protected override void OnItemClicked(int index)
{
base.OnItemClicked(index);
var item = _items[index];
foreach (var child in Target.TargetNode.Children)
{
if (child is ContentTreeNode node && node.Folder.ShortName == item)
{
Editor.Instance.Windows.ContentWin.Navigate(node);
return;
}
}
}
}
}

View File

@@ -1,27 +0,0 @@
using FlaxEditor.GUI;
using FlaxEngine;
namespace FlaxEditor.Content.GUI
{
internal class ContentNavigationBar : NavigationBar
{
ToolStrip _toolstrip;
internal float ofssetFromRightEdge = 80;
internal ContentNavigationBar(ToolStrip toolstrip) : base()
{
_toolstrip = toolstrip;
}
/// <inheritdoc />
protected override void Arrange()
{
base.Arrange();
var lastToolstripButton = _toolstrip.LastButton;
var parentSize = Parent.Size;
Bounds = new Rectangle
(
new Float2(lastToolstripButton.Right, 0),
new Float2(parentSize.X - X - ofssetFromRightEdge, _toolstrip.Height)
);
}
}
}

View File

@@ -1,85 +0,0 @@
using FlaxEditor;
using FlaxEditor.GUI;
using FlaxEditor.GUI.ContextMenu;
using FlaxEngine;
using FlaxEngine.GUI;
using System.Collections.Generic;
namespace FlaxEditor.Content.GUI
{
internal class ContentNavigationButtonSeparator : ComboBox
{
public ContentNavigationButton Target { get; }
public ContentNavigationButtonSeparator(ContentNavigationButton target, float x, float y,float height)
{
Target = target;
Bounds = new Rectangle(x, y, 16, height);
Offsets = new Margin(Bounds.X, Bounds.Width, Bounds.Y, Bounds.Height);
UpdateTransform();
MaximumItemsInViewCount = 20;
var style = Style.Current;
BackgroundColor = style.BackgroundNormal;
BackgroundColorHighlighted = BackgroundColor;
BackgroundColorSelected = BackgroundColor;
}
protected override ContextMenu OnCreatePopup()
{
UpdateDropDownItems();
return base.OnCreatePopup();
}
internal void UpdateDropDownItems()
{
ClearItems();
_items = new();
for (int i = 0; i < Target.TargetNode.Children.Count; i++)
{
if (Target.TargetNode.Children[i] is ContentTreeNode node)
{
if (node.Folder.VisibleInHierarchy) // respect the filter set by ContentFilterConfig.Filter(...)
AddItem(node.Folder.ShortName);
}
}
}
/// <inheritdoc />
public override void Draw()
{
// Cache data
var style = Style.Current;
var clientRect = new Rectangle(Float2.Zero, Size);
// Draw background
if (IsDragOver)
{
Render2D.FillRectangle(clientRect, Style.Current.BackgroundSelected * 0.6f);
}
else if (_mouseDown)
{
Render2D.FillRectangle(clientRect, style.BackgroundSelected);
}
else if (IsMouseOver)
{
Render2D.FillRectangle(clientRect, style.BackgroundHighlighted);
}
Render2D.DrawSprite(Editor.Instance.Icons.ArrowRight12, new Rectangle(clientRect.Location.X, clientRect.Y + ((Size.Y / 2f)/2f), Size.X, Size.X), EnabledInHierarchy ? Style.Current.Foreground : Style.Current.ForegroundDisabled);
}
protected override void OnLayoutMenuButton(ref ContextMenuButton button, int index, bool construct = false)
{
var style = Style.Current;
button.Icon = Editor.Instance.Icons.FolderClosed32;
if (_tooltips != null && _tooltips.Length > index)
{
button.TooltipText = _tooltips[index];
}
}
protected override void OnItemClicked(int index)
{
base.OnItemClicked(index);
if (Target.TargetNode.Children[index] is ContentTreeNode node)
{
Editor.Instance.Windows.ContentWin.Navigate(node);
}
// Navigate calls the OnDestroy at some point dont place code below or editor will crash
}
}
}

View File

@@ -1,95 +0,0 @@
using FlaxEditor.Content;
using FlaxEditor.CustomEditors.Editors;
using FlaxEditor.GUI;
using FlaxEditor.GUI.ContextMenu;
using FlaxEditor.States;
using FlaxEditor.Utilities;
using FlaxEngine;
using FlaxEngine.GUI;
using FlaxEngine.Json;
using System.Collections.Generic;
using System.Linq;
namespace FlaxEditor.Content.GUI
{
class ContentSettingsDropdown : ComboBox
{
internal readonly List<string> Settings = new()
{
"Show Engine Content",
"Show Plugin Content",
"Show Build Files",
"Show Game Settings",
"Show Shader Source"
};
public ContentSettingsDropdown()
{
SupportMultiSelect = true;
MaximumItemsInViewCount = 20;
var style = Style.Current;
BackgroundColor = style.BackgroundNormal;
BackgroundColorHighlighted = BackgroundColor;
BackgroundColorSelected = BackgroundColor;
}
protected override ContextMenu OnCreatePopup()
{
UpdateDropDownItems();
return base.OnCreatePopup();
}
internal void UpdateDropDownItems()
{
ClearItems();
for (int i = 0; i < Settings.Count; i++)
{
AddItem(Settings[i]);
}
}
/// <inheritdoc />
public override void Draw()
{
// Cache data
var style = Style.Current;
var clientRect = new Rectangle(Float2.Zero, Size);
// Draw background
if (IsDragOver)
{
Render2D.FillRectangle(clientRect, Style.Current.BackgroundSelected * 0.6f);
}
else if (_mouseDown)
{
Render2D.FillRectangle(clientRect, style.BackgroundSelected);
}
else if (IsMouseOver)
{
Render2D.FillRectangle(clientRect, style.BackgroundHighlighted);
}
float size = (Size.Y / 1.5f); //Icon size
// Draw text
Render2D.DrawText(Font.GetFont(), "Settings", new Rectangle(size, 0, Size.X - Size.Y, Size.Y), TextColor, TextAlignment.Center, TextAlignment.Center);
Render2D.DrawSprite(Editor.Instance.Icons.Settings12, new Rectangle((Size.Y - size) / 2.0f, (Size.Y - size) / 2.0f, size, size));
}
protected override void OnLayoutMenuButton(ref ContextMenuButton button, int index, bool construct = false)
{
var style = Style.Current;
if (_selectedIndices.Contains(index))
{
button.Icon = style.CheckBoxTick;
}
else
{
button.Icon = SpriteHandle.Default;
}
if (_tooltips != null && _tooltips.Length > index)
{
button.TooltipText = _tooltips[index];
}
}
protected override void OnSelectedIndexChanged()
{
ContentFilter.UpdateFilterVisability(this);
base.OnSelectedIndexChanged();
}
}
}

View File

@@ -441,11 +441,9 @@ namespace FlaxEditor.Content
{
get
{
// Skip when hidden
if (!Visible)
{
return Rectangle.Empty;
}
var view = Parent as ContentView;
var size = Size;
switch (view?.ViewType ?? ContentViewType.Tiles)
@@ -453,7 +451,7 @@ namespace FlaxEditor.Content
case ContentViewType.Tiles:
{
var textHeight = DefaultTextHeight * size.X / DefaultWidth;
return new Rectangle(0, size.Y - textHeight, size.X - 2, textHeight);
return new Rectangle(0, size.Y - textHeight, size.X, textHeight);
}
case ContentViewType.List:
{
@@ -671,8 +669,6 @@ namespace FlaxEditor.Content
/// <inheritdoc />
public override void Draw()
{
// Cache data
var size = Size;
var style = Style.Current;
var view = Parent as ContentView;

View File

@@ -17,6 +17,6 @@ namespace FlaxEditor.Content
}
/// <inheritdoc />
public override string NavButtonLabel => string.Empty;
public override string NavButtonLabel => " /";
}
}