From a061afd840da6b2af921b5f41cdb770644d5b188 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 19 Feb 2024 10:54:29 +0100 Subject: [PATCH] Cleanup for #1827 --- Source/Editor/Content/Proxy/ScriptProxy.cs | 3 +- .../CustomEditors/Dedicated/ScriptsEditor.cs | 65 +++++++++---------- Source/Editor/GUI/ItemsListContextMenu.cs | 25 +++---- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/Source/Editor/Content/Proxy/ScriptProxy.cs b/Source/Editor/Content/Proxy/ScriptProxy.cs index f688f37df..d728dcc38 100644 --- a/Source/Editor/Content/Proxy/ScriptProxy.cs +++ b/Source/Editor/Content/Proxy/ScriptProxy.cs @@ -69,8 +69,7 @@ namespace FlaxEditor.Content /// public override bool IsFileNameValid(string filename) { - // Scripts cannot start with digit. - if (Char.IsDigit(filename[0])) + if (char.IsDigit(filename[0])) return false; if (filename.Equals("Script")) return false; diff --git a/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs b/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs index 216c205a1..63374361d 100644 --- a/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs +++ b/Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs @@ -21,6 +21,7 @@ namespace FlaxEditor.CustomEditors.Dedicated internal class NewScriptItem : ItemsListContextMenu.Item { private string _scriptName; + public string ScriptName { get => _scriptName; @@ -37,6 +38,7 @@ namespace FlaxEditor.CustomEditors.Dedicated TooltipText = "Create a new script"; } } + /// /// Drag and drop scripts area control. /// @@ -99,18 +101,14 @@ namespace FlaxEditor.CustomEditors.Dedicated { if (!IsValidScriptName(text)) return; - var items = cm.ItemsPanel.Children.Count(x => x.Visible && x is not NewScriptItem); - if (items == 0) + if (!cm.ItemsPanel.Children.Any(x => x.Visible && x is not NewScriptItem)) { - // If there are no visible items, that means the search failed so we can find the create script - // button or create one if it's the first time. - - var createScriptItem = cm.ItemsPanel.Children.FirstOrDefault(x => x is NewScriptItem); - if (createScriptItem != null) + // If there are no visible items, that means the search failed so we can find the create script button or create one if it's the first time + var newScriptItem = (NewScriptItem)cm.ItemsPanel.Children.FirstOrDefault(x => x is NewScriptItem); + if (newScriptItem != null) { - var item = createScriptItem as NewScriptItem; - item.Visible = true; - item.ScriptName = text; + newScriptItem.Visible = true; + newScriptItem.ScriptName = text; } else { @@ -120,9 +118,9 @@ namespace FlaxEditor.CustomEditors.Dedicated else { // Make sure to hide the create script button if there - var createScriptButton = cm.ItemsPanel.Children.FirstOrDefault(x => x is NewScriptItem); - if (createScriptButton != null) - createScriptButton.Visible = false; + var newScriptItem = cm.ItemsPanel.Children.FirstOrDefault(x => x is NewScriptItem); + if (newScriptItem != null) + newScriptItem.Visible = false; } }; cm.ItemClicked += item => @@ -135,7 +133,6 @@ namespace FlaxEditor.CustomEditors.Dedicated { CreateScript(newScriptItem); } - }; cm.SortItems(); cm.Show(this, button.BottomLeft - new Float2((cm.Width - button.Width) / 2, 0)); @@ -174,16 +171,18 @@ namespace FlaxEditor.CustomEditors.Dedicated return scriptItem.ScriptType != ScriptType.Null; return false; } - + private static bool IsValidScriptName(string text) { + if (string.IsNullOrEmpty(text)) + return false; if (text.Contains(' ')) return false; if (char.IsDigit(text[0])) return false; if (text.Any(c => !char.IsLetterOrDigit(c) && c != '_')) return false; - return true; + return Editor.Instance.ContentDatabase.GetProxy("cs").IsFileNameValid(text); } /// @@ -236,6 +235,7 @@ namespace FlaxEditor.CustomEditors.Dedicated if (_dragScripts.HasValidDrag) { result = _dragScripts.Effect; + AddScripts(_dragScripts.Objects); } else if (_dragAssets.HasValidDrag) @@ -252,17 +252,16 @@ namespace FlaxEditor.CustomEditors.Dedicated private void CreateScript(NewScriptItem item) { - ScriptsEditor.NewScriptItem = item; + ScriptsEditor.NewScriptName = item.ScriptName; var paths = Directory.GetFiles(Globals.ProjectSourceFolder, "*.Build.cs"); string moduleName = null; foreach (var p in paths) { var file = File.ReadAllText(p); - // Skip if (!file.Contains("GameProjectTarget")) - continue; - + continue; // Skip + if (file.Contains("Modules.Add(\"Game\")")) { // Assume Game represents the main game module @@ -273,16 +272,14 @@ namespace FlaxEditor.CustomEditors.Dedicated // Ensure the path slashes are correct for the OS var correctedPath = Path.GetFullPath(Globals.ProjectSourceFolder); - if (string.IsNullOrEmpty(moduleName)) { var error = FileSystem.ShowBrowseFolderDialog(Editor.Instance.Windows.MainWindow, correctedPath, "Select a module folder to put the new script in", out moduleName); if (error) return; } - var path = Path.Combine(Globals.ProjectSourceFolder, moduleName, item.ScriptName + ".cs"); - new CSharpScriptProxy().Create(path, null); + Editor.Instance.ContentDatabase.GetProxy("cs").Create(path, null); } /// @@ -603,10 +600,10 @@ namespace FlaxEditor.CustomEditors.Dedicated /// public override IEnumerable UndoObjects => _scripts; - // We need somewhere to store the newly created script name. - // The problem is that the ScriptsEditor gets destroyed after scripts compilation - // so we must make it static to store this information. - internal static NewScriptItem NewScriptItem { get; set; } + /// + /// Cached the newly created script name - used to add script after compilation. + /// + internal static string NewScriptName; private void AddMissingScript(int index, LayoutElementsContainer layout) { @@ -715,17 +712,15 @@ namespace FlaxEditor.CustomEditors.Dedicated // Area for drag&drop scripts var dragArea = layout.CustomContainer(); dragArea.CustomControl.ScriptsEditor = this; - - // If the initialization is triggered by an editor recompilation, check if it - // was due to script generation from DragAreaControl. - if (NewScriptItem != null) + + // If the initialization is triggered by an editor recompilation, check if it was due to script generation from DragAreaControl + if (NewScriptName != null) { - var script = Editor.Instance.CodeEditing.Scripts.Get() - .FirstOrDefault(x => x.Name == NewScriptItem.ScriptName); + var script = Editor.Instance.CodeEditing.Scripts.Get().FirstOrDefault(x => x.Name == NewScriptName); + NewScriptName = null; if (script != null) { dragArea.CustomControl.AddScript(script); - NewScriptItem = null; } else { @@ -770,7 +765,7 @@ namespace FlaxEditor.CustomEditors.Dedicated var values = new ScriptsContainer(elementType, i, Values); var scriptType = TypeUtils.GetObjectType(script); var editor = CustomEditorsUtil.CreateEditor(scriptType, false); - + // Check if actor has all the required scripts bool hasAllRequirements = true; if (scriptType.HasAttribute(typeof(RequireScriptAttribute), false)) diff --git a/Source/Editor/GUI/ItemsListContextMenu.cs b/Source/Editor/GUI/ItemsListContextMenu.cs index a0351bf0f..c8c2a9c22 100644 --- a/Source/Editor/GUI/ItemsListContextMenu.cs +++ b/Source/Editor/GUI/ItemsListContextMenu.cs @@ -189,6 +189,9 @@ namespace FlaxEditor.GUI /// public event Action ItemClicked; + /// + /// Event fired when search text in this popup menu gets changed. + /// public event Action TextChanged; /// @@ -442,6 +445,7 @@ namespace FlaxEditor.GUI Hide(); return true; case KeyboardKeys.ArrowDown: + { if (RootWindow.FocusedControl == null) { // Focus search box if nothing is focused @@ -450,20 +454,19 @@ namespace FlaxEditor.GUI } // Focus the first visible item or then next one + var items = GetVisibleItems(); + var focusedIndex = items.IndexOf(focusedItem); + if (focusedIndex == -1) + focusedIndex = -1; + if (focusedIndex + 1 < items.Count) { - var items = GetVisibleItems(); - var focusedIndex = items.IndexOf(focusedItem); - if (focusedIndex == -1) - focusedIndex = -1; - if (focusedIndex + 1 < items.Count) - { - var item = items[focusedIndex + 1]; - item.Focus(); - _scrollPanel.ScrollViewTo(item); - return true; - } + var item = items[focusedIndex + 1]; + item.Focus(); + _scrollPanel.ScrollViewTo(item); + return true; } break; + } case KeyboardKeys.ArrowUp: if (focusedItem != null) {