PlayInEditor.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //************** Copyright (c) 2016-2019 Marko Pintera ([email protected]). All rights reserved. *******************//
  3. using System;
  4. using System.Collections.Generic;
  5. namespace bs.Editor
  6. {
  7. /** @addtogroup Editor-General
  8. * @{
  9. */
  10. public partial class PlayInEditor
  11. {
  12. private static List<UUID> lastSelectedSceneObjects = new List<UUID>();
  13. /// <summary>Triggered right after the play mode is exited.</summary>
  14. public static event Action OnStopped;
  15. /// <summary>
  16. /// Updates the play state of the game, making the game stop or start running. Note the actual state change
  17. /// will be delayed until the next update() call. Use the onPlay/onStopped/onPaused/onUnpaused event to get notified
  18. /// when the change actually happens.
  19. /// </summary>
  20. public static PlayInEditorState State
  21. {
  22. get => Internal_getState();
  23. set
  24. {
  25. if (value == PlayInEditorState.Paused)
  26. {
  27. Internal_setState(value);
  28. return;
  29. }
  30. bool isPlaying = value == PlayInEditorState.Playing;
  31. if (!isPlaying)
  32. Selection.SceneObject = null;
  33. else
  34. {
  35. if (EditorSettings.GetBool(LogWindow.CLEAR_ON_PLAY_KEY, true))
  36. {
  37. Debug.Clear();
  38. LogWindow log = EditorWindow.GetWindow<LogWindow>();
  39. if (log != null)
  40. log.Refresh();
  41. }
  42. lastSelectedSceneObjects.Clear();
  43. SceneObject[] sos = Selection.SceneObjects;
  44. foreach (var entry in sos)
  45. lastSelectedSceneObjects.Add(entry.UUID);
  46. }
  47. Internal_setState(value);
  48. }
  49. }
  50. static partial void Callback_OnStopped()
  51. {
  52. // Select last selected objects before rendering play mode
  53. if (lastSelectedSceneObjects.Count == 0)
  54. Selection.SceneObject = null;
  55. else
  56. {
  57. List<SceneObject> selection = new List<SceneObject>();
  58. SceneObject root = Scene.Root;
  59. if (root != null)
  60. {
  61. Stack<SceneObject> todo = new Stack<SceneObject>();
  62. todo.Push(root);
  63. while (todo.Count > 0)
  64. {
  65. SceneObject so = todo.Pop();
  66. if (so.IsDestroyed)
  67. continue;
  68. foreach (var entry in lastSelectedSceneObjects)
  69. {
  70. if (so.UUID == entry)
  71. {
  72. selection.Add(so);
  73. break;
  74. }
  75. }
  76. int numChildren = so.GetNumChildren();
  77. for (int i = 0; i < numChildren; i++)
  78. todo.Push(so.GetChild(i));
  79. }
  80. }
  81. Selection.SceneObjects = selection.ToArray();
  82. }
  83. OnStopped?.Invoke();
  84. }
  85. }
  86. /** @} */
  87. }