This commit is contained in:
Wojtek Figat
2024-02-19 10:54:29 +01:00
parent 71627f1bbd
commit a061afd840
3 changed files with 45 additions and 48 deletions

View File

@@ -69,8 +69,7 @@ namespace FlaxEditor.Content
/// <inheritdoc /> /// <inheritdoc />
public override bool IsFileNameValid(string filename) public override bool IsFileNameValid(string filename)
{ {
// Scripts cannot start with digit. if (char.IsDigit(filename[0]))
if (Char.IsDigit(filename[0]))
return false; return false;
if (filename.Equals("Script")) if (filename.Equals("Script"))
return false; return false;

View File

@@ -21,6 +21,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
internal class NewScriptItem : ItemsListContextMenu.Item internal class NewScriptItem : ItemsListContextMenu.Item
{ {
private string _scriptName; private string _scriptName;
public string ScriptName public string ScriptName
{ {
get => _scriptName; get => _scriptName;
@@ -37,6 +38,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
TooltipText = "Create a new script"; TooltipText = "Create a new script";
} }
} }
/// <summary> /// <summary>
/// Drag and drop scripts area control. /// Drag and drop scripts area control.
/// </summary> /// </summary>
@@ -99,18 +101,14 @@ namespace FlaxEditor.CustomEditors.Dedicated
{ {
if (!IsValidScriptName(text)) if (!IsValidScriptName(text))
return; return;
var items = cm.ItemsPanel.Children.Count(x => x.Visible && x is not NewScriptItem); if (!cm.ItemsPanel.Children.Any(x => x.Visible && x is not NewScriptItem))
if (items == 0)
{ {
// If there are no visible items, that means the search failed so we can find the create script // 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
// 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 createScriptItem = cm.ItemsPanel.Children.FirstOrDefault(x => x is NewScriptItem);
if (createScriptItem != null)
{ {
var item = createScriptItem as NewScriptItem; newScriptItem.Visible = true;
item.Visible = true; newScriptItem.ScriptName = text;
item.ScriptName = text;
} }
else else
{ {
@@ -120,9 +118,9 @@ namespace FlaxEditor.CustomEditors.Dedicated
else else
{ {
// Make sure to hide the create script button if there // Make sure to hide the create script button if there
var createScriptButton = cm.ItemsPanel.Children.FirstOrDefault(x => x is NewScriptItem); var newScriptItem = cm.ItemsPanel.Children.FirstOrDefault(x => x is NewScriptItem);
if (createScriptButton != null) if (newScriptItem != null)
createScriptButton.Visible = false; newScriptItem.Visible = false;
} }
}; };
cm.ItemClicked += item => cm.ItemClicked += item =>
@@ -135,7 +133,6 @@ namespace FlaxEditor.CustomEditors.Dedicated
{ {
CreateScript(newScriptItem); CreateScript(newScriptItem);
} }
}; };
cm.SortItems(); cm.SortItems();
cm.Show(this, button.BottomLeft - new Float2((cm.Width - button.Width) / 2, 0)); 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 scriptItem.ScriptType != ScriptType.Null;
return false; return false;
} }
private static bool IsValidScriptName(string text) private static bool IsValidScriptName(string text)
{ {
if (string.IsNullOrEmpty(text))
return false;
if (text.Contains(' ')) if (text.Contains(' '))
return false; return false;
if (char.IsDigit(text[0])) if (char.IsDigit(text[0]))
return false; return false;
if (text.Any(c => !char.IsLetterOrDigit(c) && c != '_')) if (text.Any(c => !char.IsLetterOrDigit(c) && c != '_'))
return false; return false;
return true; return Editor.Instance.ContentDatabase.GetProxy("cs").IsFileNameValid(text);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -236,6 +235,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
if (_dragScripts.HasValidDrag) if (_dragScripts.HasValidDrag)
{ {
result = _dragScripts.Effect; result = _dragScripts.Effect;
AddScripts(_dragScripts.Objects); AddScripts(_dragScripts.Objects);
} }
else if (_dragAssets.HasValidDrag) else if (_dragAssets.HasValidDrag)
@@ -252,17 +252,16 @@ namespace FlaxEditor.CustomEditors.Dedicated
private void CreateScript(NewScriptItem item) private void CreateScript(NewScriptItem item)
{ {
ScriptsEditor.NewScriptItem = item; ScriptsEditor.NewScriptName = item.ScriptName;
var paths = Directory.GetFiles(Globals.ProjectSourceFolder, "*.Build.cs"); var paths = Directory.GetFiles(Globals.ProjectSourceFolder, "*.Build.cs");
string moduleName = null; string moduleName = null;
foreach (var p in paths) foreach (var p in paths)
{ {
var file = File.ReadAllText(p); var file = File.ReadAllText(p);
// Skip
if (!file.Contains("GameProjectTarget")) if (!file.Contains("GameProjectTarget"))
continue; continue; // Skip
if (file.Contains("Modules.Add(\"Game\")")) if (file.Contains("Modules.Add(\"Game\")"))
{ {
// Assume Game represents the main game module // Assume Game represents the main game module
@@ -273,16 +272,14 @@ namespace FlaxEditor.CustomEditors.Dedicated
// Ensure the path slashes are correct for the OS // Ensure the path slashes are correct for the OS
var correctedPath = Path.GetFullPath(Globals.ProjectSourceFolder); var correctedPath = Path.GetFullPath(Globals.ProjectSourceFolder);
if (string.IsNullOrEmpty(moduleName)) 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); var error = FileSystem.ShowBrowseFolderDialog(Editor.Instance.Windows.MainWindow, correctedPath, "Select a module folder to put the new script in", out moduleName);
if (error) if (error)
return; return;
} }
var path = Path.Combine(Globals.ProjectSourceFolder, moduleName, item.ScriptName + ".cs"); var path = Path.Combine(Globals.ProjectSourceFolder, moduleName, item.ScriptName + ".cs");
new CSharpScriptProxy().Create(path, null); Editor.Instance.ContentDatabase.GetProxy("cs").Create(path, null);
} }
/// <summary> /// <summary>
@@ -603,10 +600,10 @@ namespace FlaxEditor.CustomEditors.Dedicated
/// <inheritdoc /> /// <inheritdoc />
public override IEnumerable<object> UndoObjects => _scripts; public override IEnumerable<object> UndoObjects => _scripts;
// We need somewhere to store the newly created script name. /// <summary>
// The problem is that the ScriptsEditor gets destroyed after scripts compilation /// Cached the newly created script name - used to add script after compilation.
// so we must make it static to store this information. /// </summary>
internal static NewScriptItem NewScriptItem { get; set; } internal static string NewScriptName;
private void AddMissingScript(int index, LayoutElementsContainer layout) private void AddMissingScript(int index, LayoutElementsContainer layout)
{ {
@@ -715,17 +712,15 @@ namespace FlaxEditor.CustomEditors.Dedicated
// Area for drag&drop scripts // Area for drag&drop scripts
var dragArea = layout.CustomContainer<DragAreaControl>(); var dragArea = layout.CustomContainer<DragAreaControl>();
dragArea.CustomControl.ScriptsEditor = this; dragArea.CustomControl.ScriptsEditor = this;
// If the initialization is triggered by an editor recompilation, check if it // If the initialization is triggered by an editor recompilation, check if it was due to script generation from DragAreaControl
// was due to script generation from DragAreaControl. if (NewScriptName != null)
if (NewScriptItem != null)
{ {
var script = Editor.Instance.CodeEditing.Scripts.Get() var script = Editor.Instance.CodeEditing.Scripts.Get().FirstOrDefault(x => x.Name == NewScriptName);
.FirstOrDefault(x => x.Name == NewScriptItem.ScriptName); NewScriptName = null;
if (script != null) if (script != null)
{ {
dragArea.CustomControl.AddScript(script); dragArea.CustomControl.AddScript(script);
NewScriptItem = null;
} }
else else
{ {
@@ -770,7 +765,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
var values = new ScriptsContainer(elementType, i, Values); var values = new ScriptsContainer(elementType, i, Values);
var scriptType = TypeUtils.GetObjectType(script); var scriptType = TypeUtils.GetObjectType(script);
var editor = CustomEditorsUtil.CreateEditor(scriptType, false); var editor = CustomEditorsUtil.CreateEditor(scriptType, false);
// Check if actor has all the required scripts // Check if actor has all the required scripts
bool hasAllRequirements = true; bool hasAllRequirements = true;
if (scriptType.HasAttribute(typeof(RequireScriptAttribute), false)) if (scriptType.HasAttribute(typeof(RequireScriptAttribute), false))

View File

@@ -189,6 +189,9 @@ namespace FlaxEditor.GUI
/// </summary> /// </summary>
public event Action<Item> ItemClicked; public event Action<Item> ItemClicked;
/// <summary>
/// Event fired when search text in this popup menu gets changed.
/// </summary>
public event Action<string> TextChanged; public event Action<string> TextChanged;
/// <summary> /// <summary>
@@ -442,6 +445,7 @@ namespace FlaxEditor.GUI
Hide(); Hide();
return true; return true;
case KeyboardKeys.ArrowDown: case KeyboardKeys.ArrowDown:
{
if (RootWindow.FocusedControl == null) if (RootWindow.FocusedControl == null)
{ {
// Focus search box if nothing is focused // Focus search box if nothing is focused
@@ -450,20 +454,19 @@ namespace FlaxEditor.GUI
} }
// Focus the first visible item or then next one // 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 item = items[focusedIndex + 1];
var focusedIndex = items.IndexOf(focusedItem); item.Focus();
if (focusedIndex == -1) _scrollPanel.ScrollViewTo(item);
focusedIndex = -1; return true;
if (focusedIndex + 1 < items.Count)
{
var item = items[focusedIndex + 1];
item.Focus();
_scrollPanel.ScrollViewTo(item);
return true;
}
} }
break; break;
}
case KeyboardKeys.ArrowUp: case KeyboardKeys.ArrowUp:
if (focusedItem != null) if (focusedItem != null)
{ {