BsPlayInEditorManager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. gAnimation().setPaused(false);
  59. }
  60. break;
  61. case PlayInEditorState::Paused:
  62. {
  63. mFrameStepActive = false;
  64. setSystemsPauseState(true);
  65. gAnimation().setPaused(true);
  66. if (oldState == PlayInEditorState::Stopped)
  67. {
  68. saveSceneInMemory();
  69. ScriptGameObjectManager::instance().wakeRuntimeComponents();
  70. }
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. void PlayInEditorManager::frameStep()
  78. {
  79. if (!gApplication().isEditor())
  80. return;
  81. switch (mState)
  82. {
  83. case PlayInEditorState::Stopped:
  84. case PlayInEditorState::Paused:
  85. setState(PlayInEditorState::Playing);
  86. break;
  87. default:
  88. break;
  89. }
  90. mFrameStepActive = true;
  91. }
  92. void PlayInEditorManager::update()
  93. {
  94. if (mState == PlayInEditorState::Playing)
  95. mPausableTime += gTime().getFrameDelta();
  96. if (mScheduledStateChange)
  97. {
  98. setStateImmediate(mNextState);
  99. mScheduledStateChange = false;
  100. }
  101. if (mFrameStepActive)
  102. {
  103. setState(PlayInEditorState::Paused);
  104. mFrameStepActive = false;
  105. }
  106. }
  107. void PlayInEditorManager::saveSceneInMemory()
  108. {
  109. mSavedScene = SceneManager::instance().getRootNode()->clone(false);
  110. // Remove objects with "dont save" flag
  111. Stack<HSceneObject> todo;
  112. todo.push(mSavedScene);
  113. while (!todo.empty())
  114. {
  115. HSceneObject current = todo.top();
  116. todo.pop();
  117. if (current->hasFlag(SOF_DontSave))
  118. current->destroy();
  119. else
  120. {
  121. UINT32 numChildren = current->getNumChildren();
  122. for (UINT32 i = 0; i < numChildren; i++)
  123. todo.push(current->getChild(i));
  124. }
  125. }
  126. }
  127. void PlayInEditorManager::setSystemsPauseState(bool paused)
  128. {
  129. gPhysics().setPaused(paused);
  130. gAudio().setPaused(paused);
  131. }
  132. }