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 string HandleObjectInFileSelected(NewObjectTypeSelectionControl typeSelectControl) { string newName; var spaceParen = typeSelectControl.SourceName.IndexOf(" ("); if (spaceParen != -1) { newName = typeSelectControl.SourceName.Substring(0, spaceParen); } else { newName = typeSelectControl.SourceName; } // If the user selected "Entire File" we want to make sure the space doesn't show up: newName = newName.Replace(" ", ""); string throwaway; bool isInvalid = NameVerifier.IsNamedObjectNameValid(newName, out throwaway); if (!isInvalid) { // let's get the type: var split = typeSelectControl.SourceName.Split('(', ')'); var last = split.LastOrDefault(item=>!string.IsNullOrEmpty(item)); if(last != null) { var lastDot = last.LastIndexOf('.'); newName = last.Substring(lastDot + 1, last.Length - (lastDot + 1)); } } return newName; }