BsRenderWindowManager.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "BsRenderWindowManager.h"
  2. #include "BsPlatform.h"
  3. using namespace std::placeholders;
  4. namespace BansheeEngine
  5. {
  6. RenderWindowManager::RenderWindowManager()
  7. :mWindowInFocus(nullptr), mNewWindowInFocus(nullptr)
  8. {
  9. Platform::onMouseLeftWindow.connect(std::bind(&RenderWindowManager::windowMouseLeft, this, _1));
  10. }
  11. RenderWindowManager::~RenderWindowManager()
  12. {
  13. }
  14. RenderWindowPtr RenderWindowManager::create(RENDER_WINDOW_DESC& desc, RenderWindowPtr parentWindow)
  15. {
  16. UINT32 id = RenderWindowCoreManager::instance().mNextWindowId.fetch_add(1, std::memory_order_relaxed);
  17. RenderWindowPtr renderWindow = createImpl(desc, id, parentWindow);
  18. renderWindow->_setThisPtr(renderWindow);
  19. {
  20. BS_LOCK_MUTEX(mWindowMutex);
  21. mWindows[renderWindow->mWindowId] = renderWindow.get();
  22. }
  23. renderWindow->initialize();
  24. return renderWindow;
  25. }
  26. void RenderWindowManager::notifyWindowDestroyed(RenderWindow* window)
  27. {
  28. {
  29. BS_LOCK_MUTEX(mWindowMutex);
  30. auto iterFind = std::find_if(begin(mMovedOrResizedWindows), end(mMovedOrResizedWindows),
  31. [&](const MoveOrResizeData& x) { return x.window == window; });
  32. if(iterFind != mMovedOrResizedWindows.end())
  33. mMovedOrResizedWindows.erase(iterFind);
  34. mWindows.erase(window->mWindowId);
  35. mDirtyProperties.erase(window);
  36. }
  37. }
  38. void RenderWindowManager::notifyFocusReceived(RenderWindowCore* coreWindow)
  39. {
  40. BS_LOCK_MUTEX(mWindowMutex);
  41. RenderWindow* window = getNonCore(coreWindow);
  42. mNewWindowInFocus = window;
  43. }
  44. void RenderWindowManager::notifyFocusLost(RenderWindowCore* coreWindow)
  45. {
  46. BS_LOCK_MUTEX(mWindowMutex);
  47. mNewWindowInFocus = nullptr;
  48. }
  49. void RenderWindowManager::notifyMovedOrResized(RenderWindowCore* coreWindow)
  50. {
  51. BS_LOCK_MUTEX(mWindowMutex);
  52. RenderWindow* window = getNonCore(coreWindow);
  53. if (window == nullptr)
  54. return;
  55. auto iterFind = std::find_if(begin(mMovedOrResizedWindows), end(mMovedOrResizedWindows),
  56. [&](const MoveOrResizeData& x) { return x.window == window; });
  57. const RenderWindowProperties& props = coreWindow->getProperties();
  58. MoveOrResizeData* moveResizeData = nullptr;
  59. if (iterFind != end(mMovedOrResizedWindows))
  60. {
  61. moveResizeData = &*iterFind;
  62. }
  63. else
  64. {
  65. MoveOrResizeData newEntry;
  66. newEntry.window = window;
  67. mMovedOrResizedWindows.push_back(newEntry);
  68. moveResizeData = &mMovedOrResizedWindows.back();
  69. }
  70. moveResizeData->x = props.getLeft();
  71. moveResizeData->y = props.getTop();
  72. moveResizeData->width = props.getWidth();
  73. moveResizeData->height = props.getHeight();
  74. }
  75. void RenderWindowManager::notifySyncDataDirty(RenderWindowCore* coreWindow)
  76. {
  77. BS_LOCK_MUTEX(mWindowMutex);
  78. RenderWindow* window = getNonCore(coreWindow);
  79. if (window != nullptr)
  80. mDirtyProperties.insert(window);
  81. }
  82. void RenderWindowManager::windowMouseLeft(RenderWindowCore* coreWindow)
  83. {
  84. BS_LOCK_MUTEX(mWindowMutex);
  85. RenderWindow* window = getNonCore(coreWindow);
  86. auto iterFind = std::find(begin(mMouseLeftWindows), end(mMouseLeftWindows), window);
  87. if (iterFind == end(mMouseLeftWindows))
  88. mMouseLeftWindows.push_back(window);
  89. }
  90. void RenderWindowManager::_update()
  91. {
  92. RenderWindow* newWinInFocus = nullptr;
  93. Vector<MoveOrResizeData> movedOrResizedWindows;
  94. Vector<RenderWindow*> mouseLeftWindows;
  95. {
  96. BS_LOCK_MUTEX(mWindowMutex);
  97. newWinInFocus = mNewWindowInFocus;
  98. for (auto& moveResizeData : mMovedOrResizedWindows)
  99. {
  100. RenderWindow* window = moveResizeData.window;
  101. const RenderWindowProperties& props = window->getProperties();
  102. // Need to eliminate non-dirty ones because it's possible we already triggered the resize event
  103. // if the resize call originated from the sim thread, so we don't trigger it twice.
  104. bool isDirty = moveResizeData.x != props.getLeft() || moveResizeData.y != props.getTop()
  105. || moveResizeData.width != props.getWidth() || moveResizeData.height != props.getHeight();
  106. if (isDirty)
  107. movedOrResizedWindows.push_back(moveResizeData);
  108. }
  109. mMovedOrResizedWindows.clear();
  110. mouseLeftWindows = mMouseLeftWindows;
  111. mMouseLeftWindows.clear();
  112. for (auto& dirtyPropertyWindow : mDirtyProperties)
  113. dirtyPropertyWindow->syncProperties();
  114. mDirtyProperties.clear();
  115. }
  116. if(mWindowInFocus != newWinInFocus)
  117. {
  118. if(mWindowInFocus != nullptr)
  119. onFocusLost(*mWindowInFocus);
  120. if(newWinInFocus != nullptr)
  121. onFocusGained(*newWinInFocus);
  122. mWindowInFocus = newWinInFocus;
  123. }
  124. for (auto& moveResizeData : movedOrResizedWindows)
  125. {
  126. moveResizeData.window->onResized();
  127. }
  128. if (!onMouseLeftWindow.empty())
  129. {
  130. for (auto& window : mouseLeftWindows)
  131. onMouseLeftWindow(*window);
  132. }
  133. }
  134. Vector<RenderWindow*> RenderWindowManager::getRenderWindows() const
  135. {
  136. BS_LOCK_MUTEX(mWindowMutex);
  137. Vector<RenderWindow*> windows;
  138. for (auto& windowPair : mWindows)
  139. windows.push_back(windowPair.second);
  140. return windows;
  141. }
  142. RenderWindow* RenderWindowManager::getNonCore(const RenderWindowCore* window) const
  143. {
  144. auto iterFind = mWindows.find(window->mWindowId);
  145. if (iterFind != mWindows.end())
  146. return iterFind->second;
  147. return nullptr;
  148. }
  149. RenderWindowCoreManager::RenderWindowCoreManager()
  150. {
  151. mNextWindowId = 0;
  152. }
  153. SPtr<RenderWindowCore> RenderWindowCoreManager::create(RENDER_WINDOW_DESC& desc)
  154. {
  155. UINT32 id = mNextWindowId.fetch_add(1, std::memory_order_relaxed);
  156. SPtr<RenderWindowCore> renderWindow = createInternal(desc, id);
  157. renderWindow->initialize();
  158. return renderWindow;
  159. }
  160. void RenderWindowCoreManager::_update()
  161. {
  162. BS_LOCK_MUTEX(mWindowMutex);
  163. for (auto& dirtyPropertyWindow : mDirtyProperties)
  164. dirtyPropertyWindow->syncProperties();
  165. mDirtyProperties.clear();
  166. }
  167. void RenderWindowCoreManager::windowCreated(RenderWindowCore* window)
  168. {
  169. BS_LOCK_MUTEX(mWindowMutex);
  170. mCreatedWindows.push_back(window);
  171. }
  172. void RenderWindowCoreManager::windowDestroyed(RenderWindowCore* window)
  173. {
  174. {
  175. BS_LOCK_MUTEX(mWindowMutex);
  176. auto iterFind = std::find(begin(mCreatedWindows), end(mCreatedWindows), window);
  177. if (iterFind == mCreatedWindows.end())
  178. BS_EXCEPT(InternalErrorException, "Trying to destroy a window that is not in the created windows list.");
  179. mCreatedWindows.erase(iterFind);
  180. mDirtyProperties.erase(window);
  181. }
  182. }
  183. Vector<RenderWindowCore*> RenderWindowCoreManager::getRenderWindows() const
  184. {
  185. BS_LOCK_MUTEX(mWindowMutex);
  186. return mCreatedWindows;
  187. }
  188. void RenderWindowCoreManager::notifySyncDataDirty(RenderWindowCore* window)
  189. {
  190. BS_LOCK_MUTEX(mWindowMutex);
  191. mDirtyProperties.insert(window);
  192. }
  193. }