Ejemplo n.º 1
0
        public NamedObjectSave ShowAddNewObjectDialog(AddObjectViewModel addObjectViewModel = null)
        {
            NamedObjectSave newNamedObject = null;

            // add named object, add object, addnamedobject, add new object, addnewobject, createobject, addobject

            addObjectViewModel = CreateAndShowAddNamedObjectWindow(addObjectViewModel);

            if (addObjectViewModel.DialogResult == DialogResult.OK)
            {
                string whyItIsntValid = null;
                bool isValid = NameVerifier.IsNamedObjectNameValid(addObjectViewModel.ObjectName, out whyItIsntValid);

                if (isValid)
                {
                    if (addObjectViewModel.SourceType == SourceType.Entity && !RecursionManager.Self.CanContainInstanceOf(GlueState.Self.CurrentElement, addObjectViewModel.SourceClassType))
                    {
                        isValid = false;
                        whyItIsntValid = "This type would result in infinite recursion";
                    }
                }

                if (isValid)
                {
                    newNamedObject = GlueCommands.Self.GluxCommands.AddNewNamedObjectToSelectedElement(addObjectViewModel);
                }
                else
                {
                    GlueGui.ShowMessageBox(whyItIsntValid);
                }
            }

            return newNamedObject;
        }
Ejemplo n.º 2
0
        public NamedObjectSave AddNewNamedObjectToSelectedElement(AddObjectViewModel addObjectViewModel)
        {
            MembershipInfo membershipInfo = NamedObjectSaveExtensionMethodsGlue.GetMemberMembershipInfo(addObjectViewModel.ObjectName);

            var newNos = NamedObjectSaveExtensionMethodsGlue.AddNewNamedObjectToSelectedElement(addObjectViewModel.ObjectName, membershipInfo, false);

            if (addObjectViewModel.SourceClassType != NoType && !string.IsNullOrEmpty(addObjectViewModel.SourceClassType))
            {
                newNos.SourceType = addObjectViewModel.SourceType;
                newNos.SourceClassType = addObjectViewModel.SourceClassType;
                newNos.SourceFile = addObjectViewModel.SourceFile;
                newNos.SourceName = addObjectViewModel.SourceNameInFile;
                newNos.UpdateCustomProperties();

                EditorLogic.CurrentElementTreeNode.UpdateReferencedTreeNodes();
            }
            else if (!string.IsNullOrEmpty(addObjectViewModel.SourceFile))
            {
                newNos.SourceType = addObjectViewModel.SourceType;
                newNos.SourceFile = addObjectViewModel.SourceFile;
                newNos.SourceName = addObjectViewModel.SourceNameInFile;
                newNos.UpdateCustomProperties();

                EditorLogic.CurrentElementTreeNode.UpdateReferencedTreeNodes();
            }

            newNos.SourceClassGenericType = addObjectViewModel.SourceClassGenericType;

            var ati = newNos.GetAssetTypeInfo();

            if (ati != null && ati.DefaultPublic)
            {
                newNos.HasPublicProperty = true;
            }

            var currentEntity = GlueState.Self.CurrentElement as EntitySave;

            if (currentEntity != null && currentEntity.CreatedByOtherEntities && currentEntity.PooledByFactory)
            {
                bool wasAnythingAdded =
                    FlatRedBall.Glue.Factories.FactoryManager.AddResetVariablesFor(newNos);

                if (wasAnythingAdded)
                {
                    PluginManager.ReceiveOutput("Added reset variables for " + newNos);
                }
            }

            PluginManager.ReactToNewObject(newNos);
            MainGlueWindow.Self.PropertyGrid.Refresh();
            PropertyGridHelper.UpdateNamedObjectDisplay();
            ElementViewWindow.GenerateSelectedElementCode();

            // it may already be selected, so force select it
            MainGlueWindow.Self.ElementTreeView.SelectedNode = null;
            MainGlueWindow.Self.ElementTreeView.SelectedNode = GlueState.Self.Find.NamedObjectTreeNode(newNos);
            GluxCommands.Self.SaveGlux();

            return newNos;
        }
        private static AddObjectViewModel CreateAndShowAddNamedObjectWindow()
        {
            AddObjectViewModel addObjectViewModel = new AddObjectViewModel();

            TextInputWindow tiw = new TextInputWindow();
            tiw.DisplayText = "Enter the new object's name";
            tiw.Text = "New Object";
            bool isTypePredetermined =  EditorLogic.CurrentNamedObject != null && EditorLogic.CurrentNamedObject.IsList;

            NewObjectTypeSelectionControl typeSelectControl = null;
            if (!isTypePredetermined)
            {
                tiw.Width = 400;


                typeSelectControl = new NewObjectTypeSelectionControl();
                typeSelectControl.Width = tiw.Width - 22;

                typeSelectControl.AfterStrongSelect += delegate
                {
                    tiw.ClickOk();
                };

                typeSelectControl.AfterSelect += delegate(object sender, EventArgs args)
                {
                    string result = tiw.Result;

                    bool isDefault = string.IsNullOrEmpty(result);

                    // Victor Chelaru November 3, 2012
                    // I don't know if we want to only re-assign when default.
                    // The downside is that the user may have already entered a
                    // name, an then changed the type.  This would result in the
                    // user-entered name being overwritten.  However, if we don't
                    // change the name, then an old name that the user entered which
                    // is specific to the type may not get reset.  I'm leaning towards
                    // always changing the name to help prevent misnaming, and it's also
                    // less programatically complex.
                    //if (isDefault)
                    {
                        string newName;

                        if(!string.IsNullOrEmpty(typeSelectControl.SourceFile) && !string.IsNullOrEmpty(typeSelectControl.SourceName))
                        {
                            newName = HandleObjectInFileSelected(typeSelectControl);
                        }
                        else if (string.IsNullOrEmpty(typeSelectControl.SourceClassType))
                        {
                            newName = "ObjectInstance";

                        }
                        else
                        {

                            string textToAssign = typeSelectControl.SourceClassType + "Instance";
                            if (textToAssign.Contains("/") || textToAssign.Contains("\\"))
                            {
                                textToAssign = FileManager.RemovePath(textToAssign);
                            }

                            newName = textToAssign.Replace("<T>", "");
                        }

                        // We need to make sure this is a unique name.
                        newName = StringFunctions.MakeStringUnique(newName, EditorLogic.CurrentElement.AllNamedObjects);
                        tiw.Result = newName;
                    }
                };
                tiw.AddControl(typeSelectControl, AboveOrBelow.Above);
            }

            addObjectViewModel.DialogResult = tiw.ShowDialog();

            addObjectViewModel.SourceType = SourceType.FlatRedBallType;
            addObjectViewModel.SourceClassType = null;
            addObjectViewModel.SourceFile = null;
            addObjectViewModel.SourceNameInFile = null;
            addObjectViewModel.SourceClassGenericType = null;
            addObjectViewModel.ObjectName = tiw.Result;

            if(isTypePredetermined)
            {
                var parentList = GlueState.Self.CurrentNamedObjectSave;

                var genericType = parentList.SourceClassGenericType;

                if(!string.IsNullOrEmpty(genericType))
                {
                    addObjectViewModel.SourceClassType = genericType;
                    
                    // the generic type will be fully qualified (like FlatRedBall.Sprite)
                    // but object types for FRB primitives are not qualified, so we need to remove
                    // any dots

                    if(addObjectViewModel.SourceClassType.Contains("."))
                    {
                        int lastDot = addObjectViewModel.SourceClassType.LastIndexOf('.');

                        addObjectViewModel.SourceClassType = addObjectViewModel.SourceClassType.Substring(lastDot + 1);
                    }

                    if(ObjectFinder.Self.GetEntitySave(genericType) != null)
                    {
                        addObjectViewModel.SourceType = SourceType.Entity;
                    }
                    else
                    {
                        addObjectViewModel.SourceType = SourceType.FlatRedBallType;
                    }
                }
            }

            if (typeSelectControl != null)
            {
                if (!string.IsNullOrEmpty(typeSelectControl.SourceClassType) || typeSelectControl.SourceType == SourceType.File)
                {
                    addObjectViewModel.SourceType = typeSelectControl.SourceType;
                }
                addObjectViewModel.SourceFile = typeSelectControl.SourceFile;
                addObjectViewModel.SourceNameInFile = typeSelectControl.SourceName;

                addObjectViewModel.SourceClassType = typeSelectControl.SourceClassType;
                addObjectViewModel.SourceClassGenericType = typeSelectControl.SourceClassGenericType;
            }

            return addObjectViewModel;
        }
        private static void MoveReferencedFile(TreeNode treeNodeMoving, TreeNode targetNode)
        {
            while (targetNode != null && targetNode.IsReferencedFile
                ())
            {
                targetNode = targetNode.Parent;
            }
            // If the user drops a file on a Screen or Entity, let's allow them to
            // complete the operation on the Files node
            if (targetNode is BaseElementTreeNode)
            {
                targetNode = ((BaseElementTreeNode)targetNode).FilesTreeNode;
            }

            ReferencedFileSave referencedFileSave = treeNodeMoving.Tag as ReferencedFileSave;

            if (targetNode.IsGlobalContentContainerNode())
            {
                if (targetNode.GetContainingElementTreeNode() == null)
                {
                    string targetDirectory = ProjectManager.MakeAbsolute(targetNode.GetRelativePath(), true);
                    MoveReferencedFileToDirectory(referencedFileSave, targetDirectory);
                }
                else
                {
                    DragAddFileToGlobalContent(treeNodeMoving, referencedFileSave);
                    // This means the user wants to add the file
                    // to global content.
                }
            }
            else if (targetNode.IsFolderForGlobalContentFiles())
            {
                string targetDirectory = ProjectManager.MakeAbsolute(targetNode.GetRelativePath(), true);
                MoveReferencedFileToDirectory(referencedFileSave, targetDirectory);
            }
            else if(targetNode.IsRootNamedObjectNode())
            {
                AddObjectViewModel viewModel = new AddObjectViewModel();
                viewModel.SourceType = SourceType.File;
                viewModel.SourceFile = (treeNodeMoving.Tag as ReferencedFileSave).Name;
                GlueCommands.Self.DialogCommands.ShowAddNewObjectDialog(viewModel);
            }
            else if (!targetNode.IsFilesContainerNode() &&
                !targetNode.IsFolderInFilesContainerNode() &&
                !targetNode.IsFolderForGlobalContentFiles())
            {
                MessageBox.Show(@"Can't drop this file here");
                return;
            }
            else if (!string.IsNullOrEmpty(referencedFileSave.SourceFile) ||
                referencedFileSave.SourceFileCache.Count != 0)
            {
                MessageBox.Show("Can't move the file\n\n" + referencedFileSave.Name + "\n\nbecause it has source-referencing files.  These sources will be broken " +
                    "if the file is moved.  You will need to manually move the file, modify the source references, remove this file, then add the newly-created file.");
                return;
            }
            //if (targetNode.IsFolderInFilesContainerNode() || targetNode.IsFilesContainerNode())
            else
            {
                // See if we're moving the RFS from one Element to another
                IElement container = ObjectFinder.Self.GetElementContaining(referencedFileSave);
                TreeNode elementTreeNodeDroppingIn = targetNode.GetContainingElementTreeNode();
                IElement elementDroppingIn = null;
                if (elementTreeNodeDroppingIn != null)
                {
                    // User didn't drop on an entity, but instead on a node within the entity.
                    // Let's check if it's a subfolder. If so, we need to tell the user that we
                    // can't add the file in a subfolder.

                    if (targetNode.IsFolderInFilesContainerNode())
                    {
                        MessageBox.Show("Shared files cannot be added to subfolders, so it will be added directly to \"Files\"");
                    }

                    elementDroppingIn = elementTreeNodeDroppingIn.Tag as IElement;
                }

                if (container != elementDroppingIn)
                {
                    ElementViewWindow.SelectedNode = targetNode;

                    string absoluteFileName = ProjectManager.MakeAbsolute(referencedFileSave.Name, true);
                    string creationReport;
                    string errorMessage;

                    var newlyCreatedFile = ElementCommands.Self.CreateReferencedFileSaveForExistingFile(elementDroppingIn, null, absoluteFileName,
                                                                    PromptHandleEnum.Prompt, 
                                                                    referencedFileSave.GetAssetTypeInfo(),
                                                                    out creationReport,
                                                                    out errorMessage);

                    ElementViewWindow.UpdateChangedElements();

                    
                    

                    if (!String.IsNullOrEmpty(errorMessage))
                    {
                        MessageBox.Show(errorMessage);
                    }
                    else if(newlyCreatedFile != null)
                    {
                        GlueCommands.Self.TreeNodeCommands.SelectTreeNode(newlyCreatedFile);

                    }
                }
                else
                {
                    // Not moving into or out of an element
                    string targetDirectory = ProjectManager.MakeAbsolute(targetNode.GetRelativePath(), true);
                    MoveReferencedFileToDirectory(referencedFileSave, targetDirectory);
                }
            }
        }