BsPlayInEditorManager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "BsPhysics.h"
  10. #include "BsAudio.h"
  11. #include "BsAnimationManager.h"
  12. namespace BansheeEngine
  13. {
  14. PlayInEditorManager::PlayInEditorManager()
  15. :mState(PlayInEditorState::Stopped), mNextState(PlayInEditorState::Stopped),
  16. mFrameStepActive(false), mScheduledStateChange(false), mPausableTime(0.0f)
  17. {
  18. if (!gApplication().isEditor())
  19. mState = PlayInEditorState::Playing;
  20. else
  21. setSystemsPauseState(true);
  22. }
  23. void PlayInEditorManager::setState(PlayInEditorState state)
  24. {
  25. if (!gApplication().isEditor())
  26. return;
  27. // Delay state change to next frame as this method could be called in middle of object update, in which case
  28. // part of the objects before this call would receive different state than other objects.
  29. mScheduledStateChange = true;
  30. mNextState = state;
  31. }
  32. void PlayInEditorManager::setStateImmediate(PlayInEditorState state)
  33. {
  34. if (mState == state)
  35. return;
  36. PlayInEditorState oldState = mState;
  37. mState = state;
  38. switch (state)
  39. {
  40. case PlayInEditorState::Stopped:
  41. {
  42. mFrameStepActive = false;
  43. mPausableTime = 0.0f;
  44. setSystemsPauseState(true);
  45. mSavedScene->_instantiate();
  46. gSceneManager()._setRootNode(mSavedScene);
  47. mSavedScene = nullptr;
  48. }
  49. break;
  50. case PlayInEditorState::Playing:
  51. {
  52. if (oldState == PlayInEditorState::Stopped)
  53. {
  54. saveSceneInMemory();
  55. ScriptGameObjectManager::instance().wakeRuntimeComponents();
  56. }
  57. setSystemsPauseState(false);
  58. }
  59. break;
  60. case PlayInEditorState::Paused:
  61. {
  62. mFrameStepActive = false;
  63. setSystemsPauseState(true);
  64. if (oldState == PlayInEditorState::Stopped)
  65. {
  66. saveSceneInMemory();
  67. ScriptGameObjectManager::instance().wakeRuntimeComponents();
  68. }
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. void PlayInEditorManager::frameStep()
  76. {
  77. if (!gApplication().isEditor())
  78. return;
  79. switch (mState)
  80. {
  81. case PlayInEditorState::Stopped:
  82. case PlayInEditorState::Paused:
  83. setState(PlayInEditorState::Playing);
  84. break;
  85. default:
  86. break;
  87. }
  88. mFrameStepActive = true;
  89. }
  90. void PlayInEditorManager::update()
  91. {
  92. if (mState == PlayInEditorState::Playing)
  93. mPausableTime += gTime().getFrameDelta();
  94. if (mScheduledStateChange)
  95. {
  96. setStateImmediate(mNextState);
  97. mScheduledStateChange = false;
  98. }
  99. if (mFrameStepActive)
  100. {
  101. setState(PlayInEditorState::Paused);
  102. mFrameStepActive = false;
  103. }
  104. }
  105. void PlayInEditorManager::saveSceneInMemory()
  106. {
  107. mSavedScene = SceneManager::instance().getRootNode()->clone(false);
  108. // Remove objects with "dont save" flag
  109. Stack<HSceneObject> todo;
  110. todo.push(mSavedScene);
  111. while (!todo.empty())
  112. {
  113. HSceneObject current = todo.top();
  114. todo.pop();
  115. if (current->hasFlag(SOF_DontSave))
  116. current->destroy();
  117. else
  118. {
  119. UINT32 numChildren = current->getNumChildren();
  120. for (UINT32 i = 0; i < numChildren; i++)
  121. todo.push(current->getChild(i));
  122. }
  123. }
  124. }
  125. void PlayInEditorManager::setSystemsPauseState(bool paused)
  126. {
  127. gPhysics().setPaused(paused);
  128. gAudio().setPaused(paused);
  129. gAnimation().setPaused(paused);
  130. }
  131. }