Program.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Entry point to the editor.
  8. /// </summary>
  9. class Program
  10. {
  11. private static EditorApplication app;
  12. /// <summary>
  13. /// Called by the runtime whenever the editor assembly is loaded. This means initially when editor is started
  14. /// and every time assembly refresh occurs.
  15. /// </summary>
  16. static void OnInitialize()
  17. {
  18. app = new EditorApplication();
  19. }
  20. /// <summary>
  21. /// Called by the runtime when the editor is first started. Called after <see cref="OnInitialize"/>.
  22. /// </summary>
  23. static void OnEditorStartUp()
  24. {
  25. if (EditorSettings.AutoLoadLastProject)
  26. {
  27. string projectPath = EditorSettings.LastOpenProject;
  28. if (EditorApplication.IsValidProject(projectPath))
  29. EditorApplication.LoadProject(projectPath);
  30. else
  31. ProjectWindow.Open();
  32. }
  33. else
  34. ProjectWindow.Open();
  35. CodeEditorType activeCodeEditor = (CodeEditorType)EditorSettings.GetInt(SettingsWindow.ActiveCodeEditorKey, (int) CodeEditorType.None);
  36. CodeEditorType[] availableEditors = CodeEditor.AvailableEditors;
  37. if (Array.Exists(availableEditors, x => x == activeCodeEditor))
  38. CodeEditor.ActiveEditor = activeCodeEditor;
  39. else
  40. {
  41. if (availableEditors.Length > 0)
  42. CodeEditor.ActiveEditor = availableEditors[0];
  43. }
  44. }
  45. /// <summary>
  46. /// Called 60 times per second by the runtime.
  47. /// </summary>
  48. static void OnEditorUpdate()
  49. {
  50. app.OnEditorUpdate();
  51. }
  52. /// <summary>
  53. /// Attempts to save the current scene, and keeps retrying if failed or until user cancels.
  54. /// </summary>
  55. static void TrySaveScene()
  56. {
  57. Action success = () =>
  58. {
  59. EditorApplication.SaveProject();
  60. EditorApplication.Quit();
  61. };
  62. EditorApplication.SaveScene(success, TrySaveScene);
  63. }
  64. /// <summary>
  65. /// Called when the user requests that the editor shuts down. You must manually close the editor from this
  66. /// method if you choose to accept the users request.
  67. /// </summary>
  68. static void OnEditorQuitRequested()
  69. {
  70. Action<DialogBox.ResultType> dialogCallback =
  71. (result) =>
  72. {
  73. if (result == DialogBox.ResultType.Yes)
  74. TrySaveScene();
  75. else if (result == DialogBox.ResultType.No)
  76. {
  77. EditorApplication.SaveProject();
  78. EditorApplication.Quit();
  79. }
  80. };
  81. if (EditorApplication.IsSceneModified())
  82. {
  83. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  84. DialogBox.Type.YesNoCancel, dialogCallback);
  85. }
  86. else
  87. {
  88. EditorApplication.SaveProject();
  89. EditorApplication.Quit();
  90. }
  91. }
  92. }
  93. }