private void LoadScripts(ContentTreeNode parent, string[] files)
        {
            for (int i = 0; i < files.Length; i++)
            {
                var path = StringUtils.NormalizePath(files[i]);

                // Check if node already has that element (skip during init when we want to walk project dir very fast)
                if (_isDuringFastSetup || !parent.Folder.ContainsChild(path))
                {
                    // Create file item
                    ContentItem item;
                    if (path.EndsWith(".cs"))
                    {
                        item = new CSharpScriptItem(path);
                    }
                    else if (path.EndsWith(".cpp") || path.EndsWith(".h"))
                    {
                        item = new CppScriptItem(path);
                    }
                    else if (path.EndsWith(".shader") || path.EndsWith(".hlsl"))
                    {
                        item = new ShaderSourceItem(path);
                    }
                    else
                    {
                        item = new FileItem(path);
                    }

                    // Link
                    item.ParentFolder = parent.Folder;

                    // Fire event
                    if (_enableEvents)
                    {
                        ItemAdded?.Invoke(item);
                        WorkspaceModified?.Invoke();
                        if (!path.EndsWith(".Gen.cs"))
                        {
                            if (item is ScriptItem)
                            {
                                ScriptsBuilder.MarkWorkspaceDirty();
                            }
                            if (item is ScriptItem || item is ShaderSourceItem)
                            {
                                Editor.CodeEditing.SelectedEditor.OnFileAdded(path);
                            }
                        }
                    }
                    _itemsCreated++;
                }
            }
        }
示例#2
0
        private void LoadScripts(ContentTreeNode parent, string directory)
        {
            // Find files
            var files = Directory.GetFiles(directory, ScriptProxy.ExtensionFiler, SearchOption.TopDirectoryOnly);

            // Add them
            bool anyAdded = false;

            for (int i = 0; i < files.Length; i++)
            {
                var path = StringUtils.NormalizePath(files[i]);

                // Check if node already has that element (skip during init when we want to walk project dir very fast)
                if (_isDuringFastSetup || !parent.Folder.ContainsChild(path))
                {
                    // Create item object
                    var item = new ScriptItem(path);

                    // Link
                    item.ParentFolder = parent.Folder;

                    // Fire event
                    if (_enableEvents)
                    {
                        ItemAdded?.Invoke(item);
                        OnWorkspaceModified?.Invoke();
                    }
                    _itemsCreated++;
                    anyAdded = true;
                }
            }

            if (anyAdded && _enableEvents)
            {
                ScriptsBuilder.MarkWorkspaceDirty();
            }
        }
示例#3
0
        /// <inheritdoc />
        public override void OnDelete()
        {
            ScriptsBuilder.MarkWorkspaceDirty();

            base.OnDelete();
        }
示例#4
0
        /// <summary>
        /// Renames the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="newShortName">New name (without extension, just the filename).</param>
        public void Rename(ContentItem item, string newShortName)
        {
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            // Check if can rename this item
            if (!item.CanRename)
            {
                // Cannot
                MessageBox.Show("Cannot rename this item.", "Cannot rename", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Renaming a file to an extension it already has
            if (!item.IsFolder && StringUtils.NormalizeExtension(Path.GetExtension(newShortName)) == StringUtils.NormalizeExtension(Path.GetExtension(item.Path)))
            {
                newShortName = StringUtils.GetPathWithoutExtension(newShortName);
            }

            // Check if name is valid
            if (!Editor.ContentEditing.IsValidAssetName(item, newShortName, out string hint))
            {
                // Invalid name
                MessageBox.Show("Given asset name is invalid. " + hint,
                                "Invalid name",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            // Ensure has parent
            if (item.ParentFolder == null)
            {
                // Error
                Editor.LogWarning("Cannot rename root items. " + item.Path);
                return;
            }

            // Cache data
            string extension = Path.GetExtension(item.Path);
            var    newPath   = StringUtils.CombinePaths(item.ParentFolder.Path, newShortName + extension);

            // Check if was renaming mock element
            // Note: we create `_newElement` and then rename it to create new asset
            var itemFolder = item.ParentFolder;
            Action <ContentItem> endEvent = null;

            if (_newElement == item)
            {
                try
                {
                    endEvent = (Action <ContentItem>)_newElement.Tag;

                    // Create new asset
                    var proxy = _newElement.Proxy;
                    Editor.Log(string.Format("Creating asset {0} in {1}", proxy.Name, newPath));
                    proxy.Create(newPath, _newElement.Argument);
                }
                catch (Exception ex)
                {
                    Editor.LogWarning(ex);
                    Editor.LogError("Failed to create asset.");
                }
            }
            else
            {
                // Validate state
                Assert.IsNull(_newElement);

                // Rename asset
                Editor.Log(string.Format("Renaming asset {0} to {1}", item.Path, newShortName));
                Editor.ContentDatabase.Move(item, newPath);
            }

            if (_newElement != null)
            {
                // Trigger compilation if need to
                if (_newElement.Proxy is ScriptProxy && Editor.Instance.Options.Options.General.AutoReloadScriptsOnMainWindowFocus)
                {
                    ScriptsBuilder.MarkWorkspaceDirty();
                }

                // Destroy mock control
                _newElement.ParentFolder = null;
                _newElement.Dispose();
                _newElement = null;

                // Focus content window
                Focus();
                RootWindow?.Focus();
            }

            // Refresh database and view now
            Editor.ContentDatabase.RefreshFolder(itemFolder, true);
            RefreshView();

            if (endEvent != null)
            {
                var newItem = itemFolder.FindChild(newPath);
                if (newItem != null)
                {
                    endEvent(newItem);
                }
                else
                {
                    Editor.LogWarning("Failed to find the created new item.");
                }
            }
        }
示例#5
0
        /// <inheritdoc />
        public override void OnPathChanged()
        {
            ScriptsBuilder.MarkWorkspaceDirty();

            base.OnPathChanged();
        }