Program.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using bs;
  5. namespace bs.Editor
  6. {
  7. /** @addtogroup BansheeEditor
  8. * Scripting API available in editor only, mainly used for extending the editor in various ways.
  9. * @{
  10. */
  11. /** @defgroup General General
  12. * Various functionality that doesn't fit into any other category.
  13. */
  14. /** @defgroup GUI-Editor GUI
  15. * Editor specific GUI elements.
  16. */
  17. /** @defgroup Input-Editor Input
  18. * %Input handling for editor only (not affected by game pauses).
  19. */
  20. /** @defgroup Inspectors Inspectors
  21. * Implementations of inspectors for various engine types.
  22. */
  23. /** @defgroup Settings Settings
  24. * Editor and project settings, and related window.
  25. */
  26. /** @defgroup Script Script
  27. * Script code management and compilation.
  28. */
  29. /** @defgroup Tests Tests
  30. * Editor only unit tests
  31. */
  32. /** @defgroup Utility-Editor Utility
  33. * Various utility functionality.
  34. */
  35. /** @defgroup Window Window
  36. * Interface and tools for creating custom editor windows.
  37. */
  38. /** @defgroup Windows Windows
  39. * Implementations of various editor windows (for example Scene, Game, Library).
  40. * @{
  41. */
  42. /** @defgroup AnimationEditor Animation Editor
  43. * Animation editor window and related functionality.
  44. */
  45. /** @defgroup Build Build
  46. * Build editor window and build manager.
  47. */
  48. /** @defgroup Inspector Inspector
  49. * Interface and utilities needed for implementing custom inspectors.
  50. */
  51. /** @defgroup Library Library
  52. * Managment of resources in the project (loading, saving, creating, querying, etc.).
  53. */
  54. /** @defgroup Scene-Editor Scene
  55. * Scene editor window and related functionality.
  56. * @{
  57. */
  58. /** @defgroup Gizmos Gizmos
  59. * Interface for creating custom gizmos.
  60. */
  61. /** @defgroup Handles Handles
  62. * Interface for creating custom handles (2D/3D tools).
  63. */
  64. /** @} */
  65. /** @} */
  66. /** @} */
  67. /** @cond Interop */
  68. /// <summary>
  69. /// Entry point to the editor.
  70. /// </summary>
  71. class Program
  72. {
  73. private static EditorApplication app;
  74. /// <summary>
  75. /// Called by the runtime whenever the editor assembly is loaded. This means initially when editor is started
  76. /// and every time assembly refresh occurs.
  77. /// </summary>
  78. static void OnInitialize()
  79. {
  80. app = new EditorApplication();
  81. }
  82. /// <summary>
  83. /// Called by the runtime when the editor is first started. Called after <see cref="OnInitialize"/>.
  84. /// </summary>
  85. static void OnEditorStartUp()
  86. {
  87. if (EditorSettings.AutoLoadLastProject)
  88. {
  89. string projectPath = EditorSettings.LastOpenProject;
  90. if (EditorApplication.IsValidProject(projectPath))
  91. EditorApplication.LoadProject(projectPath);
  92. else
  93. ProjectWindow.Open();
  94. }
  95. else
  96. ProjectWindow.Open();
  97. CodeEditorType activeCodeEditor = (CodeEditorType)EditorSettings.GetInt(SettingsWindow.ActiveCodeEditorKey, (int) CodeEditorType.None);
  98. CodeEditorType[] availableEditors = CodeEditor.AvailableEditors;
  99. if (Array.Exists(availableEditors, x => x == activeCodeEditor))
  100. CodeEditor.ActiveEditor = activeCodeEditor;
  101. else
  102. {
  103. if (availableEditors.Length > 0)
  104. CodeEditor.ActiveEditor = availableEditors[0];
  105. }
  106. }
  107. /// <summary>
  108. /// Called 60 times per second by the runtime.
  109. /// </summary>
  110. static void OnEditorUpdate()
  111. {
  112. app.OnEditorUpdate();
  113. }
  114. /// <summary>
  115. /// Attempts to save the current scene, and keeps retrying if failed or until user cancels.
  116. /// </summary>
  117. static void TrySaveSceneOnQuit()
  118. {
  119. Action success = () =>
  120. {
  121. EditorApplication.SaveProject();
  122. EditorApplication.Quit();
  123. };
  124. EditorApplication.SaveScene(success, TrySaveSceneOnQuit);
  125. }
  126. /// <summary>
  127. /// Called when the user requests that the editor shuts down. You must manually close the editor from this
  128. /// method if you choose to accept the users request.
  129. /// </summary>
  130. static void OnEditorQuitRequested()
  131. {
  132. Action<DialogBox.ResultType> dialogCallback =
  133. (result) =>
  134. {
  135. if (result == DialogBox.ResultType.Yes)
  136. TrySaveSceneOnQuit();
  137. else if (result == DialogBox.ResultType.No)
  138. {
  139. EditorApplication.SaveProject();
  140. EditorApplication.Quit();
  141. }
  142. };
  143. if (EditorApplication.IsSceneModified())
  144. {
  145. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  146. DialogBox.Type.YesNoCancel, dialogCallback);
  147. }
  148. else
  149. {
  150. EditorApplication.SaveProject();
  151. EditorApplication.Quit();
  152. }
  153. }
  154. }
  155. /** @endcond */
  156. }