private void OnGUI() { // Magic number. minSize = new Vector2(420f, 320f); titleContent = new GUIContent("Training Course Wizard"); GUIStyle labelStyle = new GUIStyle(EditorStyles.label); labelStyle.richText = true; labelStyle.wordWrap = true; EditorIcon logo = new EditorIcon("logo_creator"); Rect rect = GUILayoutUtility.GetRect(position.width, 150, GUI.skin.box); GUI.DrawTexture(rect, logo.Texture, ScaleMode.ScaleToFit); if (RuntimeConfigurator.Exists == false) { EditorGUILayout.HelpBox("The current scene is not a training scene. No course can be created. To automatically setup the scene, select \"Innoactive > Training > Setup Current Scene as Training Scene\".", MessageType.Error); } EditorGUI.BeginDisabledGroup(RuntimeConfigurator.Exists == false); EditorGUILayout.LabelField("<b>Create a new training course.</b>", labelStyle); trainingName = EditorGUILayout.TextField(new GUIContent("Training Course Name", "Set a file name for the new training course."), trainingName); EditorGUILayout.LabelField("The new course will be set for the current scene."); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); // ReSharper disable once InvertIf if (GUILayout.Button("Create", GUILayout.Width(128), GUILayout.Height(32))) { int invalidCharacterIndex; if (string.IsNullOrEmpty(trainingName)) { errorMessage = "Training course name is empty!"; } else if ((invalidCharacterIndex = trainingName.IndexOfAny(Path.GetInvalidFileNameChars())) >= 0) { errorMessage = string.Format("Course name contains invalid character: {0}", trainingName[invalidCharacterIndex]); } else { string trainingCoursePath = SaveManager.GetTrainingPath(trainingName); string trainingCourseFolder = Path.GetDirectoryName(trainingCoursePath); if (Directory.Exists(trainingCourseFolder)) { errorMessage = string.Format("Training course with name \"{0}\" already exists!", trainingName); } else { TrainingWindow trainingWindow = TrainingWindow.GetWindow(); trainingWindow.Focus(); ICourse course = CreateCourse(); if (trainingWindow.SetTrainingCourseWithUserConfirmation(course)) { SaveManager.SaveTrainingCourseToFile(course); RuntimeConfigurator.SetSelectedTrainingCourse(trainingCoursePath.Substring(Application.streamingAssetsPath.Length + 1)); Close(); } } } } EditorGUI.EndDisabledGroup(); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (string.IsNullOrEmpty(errorMessage) == false) { EditorGUILayout.HelpBox(errorMessage, MessageType.Error); } }
private void OnGUI() { if (course == null || focusedWindow != this) { Close(); instance.IsClosed = true; } GUI.SetNextControlName(textFieldIdentifier.ToString()); newName = EditorGUILayout.TextField(newName); newName = newName.Trim(); if (isFocusSet == false) { isFocusSet = true; EditorGUI.FocusTextInControl(textFieldIdentifier.ToString()); } if ((Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)) { if (string.IsNullOrEmpty(newName) == false && ValidateCourseName(newName)) { string oldName = course.Data.Name; string oldPath = SaveManager.GetTrainingPath(oldName); string oldFolder = Path.GetDirectoryName(oldPath); string newPath = SaveManager.GetTrainingPath(newName); string newFolder = Path.GetDirectoryName(newPath); RevertableChangesHandler.Do(new TrainingCommand( // ReSharper disable once ImplicitlyCapturedClosure () => { if (ValidateCourseName(newName)) { Directory.Move(oldFolder, newFolder); File.Move(string.Format("{0}.meta", oldFolder), string.Format("{0}.meta", newFolder)); File.Move(string.Format("{0}/{1}.json", newFolder, oldName), newPath); File.Move(string.Format("{0}/{1}.json.meta", newFolder, oldName), string.Format("{0}.meta", newPath)); course.Data.Name = newName; SaveManager.SaveTrainingCourseToFile(course); RuntimeConfigurator.SetSelectedTrainingCourse(newPath.Substring(Application.streamingAssetsPath.Length + 1)); TrainingWindow.GetWindow().IsDirty = false; } }, // ReSharper disable once ImplicitlyCapturedClosure () => { if (Directory.Exists(newFolder) == false) { return; } Directory.Move(newFolder, oldFolder); File.Move(string.Format("{0}.meta", newFolder), string.Format("{0}.meta", oldFolder)); File.Move(string.Format("{0}/{1}.json", oldFolder, newName), oldPath); File.Move(string.Format("{0}/{1}.json.meta", oldFolder, newName), string.Format("{0}.meta", oldPath)); course.Data.Name = oldName; SaveManager.SaveTrainingCourseToFile(course); RuntimeConfigurator.SetSelectedTrainingCourse(oldPath.Substring(Application.streamingAssetsPath.Length + 1)); } )); } Close(); instance.IsClosed = true; Event.current.Use(); } else if (Event.current.keyCode == KeyCode.Escape) { Close(); instance.IsClosed = true; Event.current.Use(); } }