Skip to content

Commit

Permalink
Cleanup for #1827
Browse files Browse the repository at this point in the history
  • Loading branch information
mafiesto4 committed Feb 19, 2024
1 parent 71627f1 commit a061afd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 48 deletions.
3 changes: 1 addition & 2 deletions Source/Editor/Content/Proxy/ScriptProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public override bool CanCreate(ContentFolder targetLocation)
/// <inheritdoc />
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;
Expand Down
65 changes: 30 additions & 35 deletions Source/Editor/CustomEditors/Dedicated/ScriptsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
internal class NewScriptItem : ItemsListContextMenu.Item
{
private string _scriptName;

public string ScriptName
{
get => _scriptName;
Expand All @@ -37,6 +38,7 @@ public NewScriptItem(string scriptName)
TooltipText = "Create a new script";
}
}

/// <summary>
/// Drag and drop scripts area control.
/// </summary>
Expand Down Expand Up @@ -99,18 +101,14 @@ private void OnAddScriptButtonClicked(Button button)
{
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
{
Expand All @@ -120,9 +118,9 @@ private void OnAddScriptButtonClicked(Button button)
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 =>
Expand All @@ -135,7 +133,6 @@ private void OnAddScriptButtonClicked(Button button)
{
CreateScript(newScriptItem);
}
};
cm.SortItems();
cm.Show(this, button.BottomLeft - new Float2((cm.Width - button.Width) / 2, 0));
Expand Down Expand Up @@ -174,16 +171,18 @@ private bool ValidateAsset(AssetItem assetItem)
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);
}

/// <inheritdoc />
Expand Down Expand Up @@ -236,6 +235,7 @@ public override DragDropEffect OnDragDrop(ref Float2 location, DragData data)
if (_dragScripts.HasValidDrag)
{
result = _dragScripts.Effect;

AddScripts(_dragScripts.Objects);
}
else if (_dragAssets.HasValidDrag)
Expand All @@ -252,17 +252,16 @@ public override DragDropEffect OnDragDrop(ref Float2 location, DragData data)

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
Expand All @@ -273,16 +272,14 @@ private void CreateScript(NewScriptItem item)

// 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);
}

/// <summary>
Expand Down Expand Up @@ -603,10 +600,10 @@ public sealed class ScriptsEditor : SyncPointEditor
/// <inheritdoc />
public override IEnumerable<object> 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; }
/// <summary>
/// Cached the newly created script name - used to add script after compilation.
/// </summary>
internal static string NewScriptName;

private void AddMissingScript(int index, LayoutElementsContainer layout)
{
Expand Down Expand Up @@ -715,17 +712,15 @@ public override void Initialize(LayoutElementsContainer layout)
// Area for drag&drop scripts
var dragArea = layout.CustomContainer<DragAreaControl>();
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
{
Expand Down Expand Up @@ -770,7 +765,7 @@ public override void Initialize(LayoutElementsContainer layout)
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))
Expand Down
25 changes: 14 additions & 11 deletions Source/Editor/GUI/ItemsListContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ public override int Compare(Control other)
/// </summary>
public event Action<Item> ItemClicked;

/// <summary>
/// Event fired when search text in this popup menu gets changed.
/// </summary>
public event Action<string> TextChanged;

/// <summary>
Expand Down Expand Up @@ -442,6 +445,7 @@ public override bool OnKeyDown(KeyboardKeys key)
Hide();
return true;
case KeyboardKeys.ArrowDown:
{
if (RootWindow.FocusedControl == null)
{
// Focus search box if nothing is focused
Expand All @@ -450,20 +454,19 @@ public override bool OnKeyDown(KeyboardKeys key)
}

// 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)
{
Expand Down

0 comments on commit a061afd

Please sign in to comment.