Example #1
0
        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();
            }
        }