BsPlayInEditorManager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPlayInEditorManager.h"
  4. #include "BsScriptGameObjectManager.h"
  5. #include "BsTime.h"
  6. #include "BsSceneManager.h"
  7. #include "BsSceneObject.h"
  8. #include "BsApplication.h"
  9. namespace BansheeEngine
  10. {
  11. PlayInEditorManager::PlayInEditorManager()
  12. :mState(PlayInEditorState::Stopped), mNextState(PlayInEditorState::Stopped),
  13. mFrameStepActive(false), mScheduledStateChange(false), mPausableTime(0.0f)
  14. {
  15. if (!gApplication().isEditor())
  16. mState = PlayInEditorState::Playing;
  17. }
  18. void PlayInEditorManager::setState(PlayInEditorState state)
  19. {
  20. if (!gApplication().isEditor())
  21. return;
  22. // Delay state change to next frame as this method could be called in middle of object update, in which case
  23. // part of the objects before this call would receive different state than other objects.
  24. mScheduledStateChange = true;
  25. mNextState = state;
  26. }
  27. void PlayInEditorManager::setStateImmediate(PlayInEditorState state)
  28. {
  29. if (mState == state)
  30. return;
  31. PlayInEditorState oldState = mState;
  32. mState = state;
  33. switch (state)
  34. {
  35. case PlayInEditorState::Stopped:
  36. {
  37. mFrameStepActive = false;
  38. mPausableTime = 0.0f;
  39. mSavedScene->_instantiate();
  40. gSceneManager()._setRootNode(mSavedScene);
  41. mSavedScene = nullptr;
  42. }
  43. break;
  44. case PlayInEditorState::Playing:
  45. {
  46. if (oldState == PlayInEditorState::Paused)
  47. {
  48. ScriptGameObjectManager::instance().sendComponentInitializeEvents();
  49. }
  50. else // Was stopped
  51. {
  52. saveSceneInMemory();
  53. ScriptGameObjectManager::instance().sendComponentInitializeEvents();
  54. }
  55. }
  56. break;
  57. case PlayInEditorState::Paused:
  58. {
  59. mFrameStepActive = false;
  60. if (oldState == PlayInEditorState::Stopped)
  61. {
  62. saveSceneInMemory();
  63. ScriptGameObjectManager::instance().sendComponentInitializeEvents();
  64. }
  65. }
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. void PlayInEditorManager::frameStep()
  72. {
  73. if (!gApplication().isEditor())
  74. return;
  75. switch (mState)
  76. {
  77. case PlayInEditorState::Stopped:
  78. case PlayInEditorState::Paused:
  79. setState(PlayInEditorState::Playing);
  80. break;
  81. }
  82. mFrameStepActive = true;
  83. }
  84. void PlayInEditorManager::update()
  85. {
  86. if (mState == PlayInEditorState::Playing)
  87. mPausableTime += gTime().getFrameDelta();
  88. if (mScheduledStateChange)
  89. {
  90. setStateImmediate(mNextState);
  91. mScheduledStateChange = false;
  92. }
  93. if (mFrameStepActive)
  94. {
  95. setState(PlayInEditorState::Paused);
  96. mFrameStepActive = false;
  97. }
  98. }
  99. void PlayInEditorManager::saveSceneInMemory()
  100. {
  101. mSavedScene = SceneManager::instance().getRootNode()->clone(false);
  102. // Remove objects with "dont save" flag
  103. Stack<HSceneObject> todo;
  104. todo.push(mSavedScene);
  105. while (!todo.empty())
  106. {
  107. HSceneObject current = todo.top();
  108. todo.pop();
  109. if (current->hasFlag(SOF_DontSave))
  110. current->destroy();
  111. else
  112. {
  113. UINT32 numChildren = current->getNumChildren();
  114. for (UINT32 i = 0; i < numChildren; i++)
  115. todo.push(current->getChild(i));
  116. }
  117. }
  118. }
  119. }