コード例 #1
0
        private static AddObjectViewModel CreateAndShowAddNamedObjectWindow(AddObjectViewModel addObjectViewModel = null)
        {
            TextInputWindow tiw = new TextInputWindow();

            tiw.Message = "Enter the new object's name";
            tiw.Text    = "New Object";
            // If windows is zoomed, the text may not wrap properly, so increase it:
            tiw.Width = 450;

            var currentObject = GlueState.Self.CurrentNamedObjectSave;

            bool isTypePredetermined = currentObject != null && currentObject.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
                        {
                            var classType = typeSelectControl.SourceClassType;
                            if (classType?.Contains(".") == true)
                            {
                                // un-qualify if it's something like "FlatRedBall.Sprite"
                                var lastIndex = classType.LastIndexOf(".");
                                classType = classType.Substring(lastIndex + 1);
                            }
                            string textToAssign = classType + "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;
                    }
                };

                if (addObjectViewModel != null)
                {
                    typeSelectControl.SourceType = addObjectViewModel.SourceType;
                    typeSelectControl.SourceFile = addObjectViewModel.SourceFile;
                }


                tiw.AddControl(typeSelectControl, AboveOrBelow.Above);
            }

            if (addObjectViewModel == null)
            {
                addObjectViewModel = new AddObjectViewModel();
            }
            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);
        }