Program.cs 5.5 KB

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