BsPlayInEditorManager.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "BsPlayInEditorManager.h"
  2. #include "BsScriptGameObjectManager.h"
  3. #include "BsTime.h"
  4. #include "BsSceneManager.h"
  5. #include "BsSceneObject.h"
  6. namespace BansheeEngine
  7. {
  8. PlayInEditorManager::PlayInEditorManager()
  9. :mState(PlayInEditorState::Stopped), mNextState(PlayInEditorState::Stopped),
  10. mFrameStepActive(false), mScheduledStateChange(false), mPausableTime(0.0f)
  11. { }
  12. void PlayInEditorManager::setState(PlayInEditorState state)
  13. {
  14. // Delay state change to next frame as this method could be called in middle of object update, in which case
  15. // part of the objects before this call would receive different state than other objects.
  16. mScheduledStateChange = true;
  17. mNextState = state;
  18. }
  19. void PlayInEditorManager::setStateImmediate(PlayInEditorState state)
  20. {
  21. if (mState == state)
  22. return;
  23. PlayInEditorState oldState = mState;
  24. mState = state;
  25. switch (state)
  26. {
  27. case PlayInEditorState::Stopped:
  28. {
  29. mFrameStepActive = false;
  30. mPausableTime = 0.0f;
  31. mSavedScene->_instantiate();
  32. gSceneManager()._setRootNode(mSavedScene);
  33. mSavedScene = nullptr;
  34. }
  35. break;
  36. case PlayInEditorState::Playing:
  37. {
  38. if (oldState == PlayInEditorState::Paused)
  39. {
  40. ScriptGameObjectManager::instance().sendComponentInitializeEvents();
  41. }
  42. else // Was stopped
  43. {
  44. mSavedScene = SceneManager::instance().getRootNode()->clone(false);
  45. ScriptGameObjectManager::instance().sendComponentInitializeEvents();
  46. }
  47. }
  48. break;
  49. case PlayInEditorState::Paused:
  50. {
  51. mFrameStepActive = false;
  52. if (oldState == PlayInEditorState::Stopped)
  53. {
  54. mSavedScene = SceneManager::instance().getRootNode()->clone(false);
  55. ScriptGameObjectManager::instance().sendComponentInitializeEvents();
  56. }
  57. }
  58. break;
  59. default:
  60. break;
  61. }
  62. }
  63. void PlayInEditorManager::frameStep()
  64. {
  65. switch (mState)
  66. {
  67. case PlayInEditorState::Stopped:
  68. case PlayInEditorState::Paused:
  69. setState(PlayInEditorState::Playing);
  70. break;
  71. }
  72. mFrameStepActive = true;
  73. }
  74. void PlayInEditorManager::update()
  75. {
  76. if (mState == PlayInEditorState::Playing)
  77. mPausableTime += gTime().getFrameDelta();
  78. if (mScheduledStateChange)
  79. {
  80. setStateImmediate(mNextState);
  81. mScheduledStateChange = false;
  82. }
  83. if (mFrameStepActive)
  84. {
  85. setState(PlayInEditorState::Paused);
  86. mFrameStepActive = false;
  87. }
  88. }
  89. }