Exemplo n.º 1
0
        public static ReferencedFileSave AddReferencedFileSave(IElement element, string directoryPath, string fileName,
                                                               AssetTypeInfo resultAssetTypeInfo, string option, out string errorMessage)
        {
            char invalidCharacter;
            ReferencedFileSave rfs = null;

            errorMessage = null;

            #region Get directory

            string directoryRelativeToContent = "";

            if (!String.IsNullOrEmpty(directoryPath))
            {
                //string directory = directoryTreeNode.GetRelativePath().Replace("/", "\\");

                directoryRelativeToContent = directoryPath;
            }
            else if (element != null)
            {
                //string directory = elementToAddTo.GetRelativePath().Replace("/", "\\");

                directoryRelativeToContent = element.Name.Replace(@"/", @"\") + "\\";
            }
            else
            {
                directoryRelativeToContent = "GlobalContent/";
            }


            #endregion

            bool userCancelled = false;


            #region If there is some reason why the file name won't, then work show a message box

            string whyIsntValid;

            if (!NameVerifier.IsReferencedFileNameValid(fileName, resultAssetTypeInfo, rfs, element, out whyIsntValid))
            {
                errorMessage = "Invalid file name:\n" + fileName + "\n" + whyIsntValid;
            }
            else if (resultAssetTypeInfo == null)
            {
                errorMessage = "You must select a valid type for your new file.";
            }
            else if (ObjectFinder.Self.GetReferencedFileSaveFromFile(directoryRelativeToContent + fileName + "." + resultAssetTypeInfo.Extension) != null)
            {
                errorMessage = "There is already a file named\n\n" + directoryRelativeToContent + fileName + "." + resultAssetTypeInfo.Extension;
            }
            // TODO:  This currently checks for an exact match, but we should prevent different files (like a .emix and .scnx) from having the same name
            else if (EditorLogic.CurrentElement != null &&
                     EditorLogic.CurrentElement.GetReferencedFileSaveRecursively(directoryRelativeToContent + fileName + "." + resultAssetTypeInfo.Extension) != null)
            {
                errorMessage = "There is already a file named " + fileName;
            }
            // need to check global content for duplicates.  This is not implemented yet.
            #endregion

            #region Else, create the file

            else
            {
                string createdFile = PluginManager.CreateNewFile(resultAssetTypeInfo, option == "2D",
                                                                 directoryRelativeToContent, fileName);

                // let's just re-route this
                // to the code that adds existing
                // files now that we have a file and
                // that's exactly what we're doing.
                rfs = RightClickHelper.AddSingleFile(createdFile, ref userCancelled, option);

                if (rfs == null && !userCancelled)
                {
                    throw new NullReferenceException("The RFS shouldn't be null");
                }
            }

            #endregion



            return(rfs);
        }
        private static void DragDropFile(object sender, DragEventArgs e)
        {
            TreeNode targetNode = null;

            TreeView tree          = (TreeView)sender;
            TreeNode directoryNode = null;


            Point pt = new Point(e.X, e.Y);

            pt         = tree.PointToClient(pt);
            targetNode = tree.GetNodeAt(pt);

            TreeNode nodeDroppedOn = targetNode;

            while (targetNode != null && !targetNode.IsEntityNode() && !targetNode.IsScreenNode())
            {
                if (directoryNode == null && targetNode.IsDirectoryNode())
                {
                    directoryNode = targetNode;
                }

                targetNode = targetNode.Parent;
            }

            var directoryPath = directoryNode == null ? null : directoryNode.GetRelativePath();

            bool userCancelled = false;

            if (targetNode == null)
            {
                ElementViewWindow.SelectedNode = nodeDroppedOn;
                foreach (var fileName in (string[])e.Data.GetData("FileDrop"))
                {
                    string creationReport;

                    string extension = FileManager.GetExtension(fileName);

                    if (extension == "entz" || extension == "scrz")
                    {
                        ElementImporter.ImportElementFromFile(fileName, true);
                    }
                    else
                    {
                        string errorMessage;

                        RightClickHelper.AddSingleFile(fileName, ref userCancelled);
                    }
                }

                GluxCommands.Self.SaveGlux();
            }
            else if (targetNode is ScreenTreeNode || targetNode is EntityTreeNode)
            {
                bool any = false;
                foreach (var fileName in (string[])e.Data.GetData("FileDrop"))
                {
                    // First select the entity
                    ElementViewWindow.SelectedNode = targetNode;


                    var    element             = GlueState.Self.CurrentElement;
                    string directoryOfTreeNode = EditorLogic.CurrentTreeNode.GetRelativePath();


                    FlatRedBall.Glue.Managers.TaskManager.Self.AddSync(() =>
                    {
                        RightClickHelper.AddSingleFile(fileName, ref userCancelled, element, directoryOfTreeNode);
                    },
                                                                       "Add file " + fileName);
                    any = true;
                }
                if (any)
                {
                    FlatRedBall.Glue.Managers.TaskManager.Self.AddSync(() =>
                    {
                        GluxCommands.Self.SaveGlux();
                    },
                                                                       "Save .glux");
                }
            }
        }