BsRenderWindowManager.cpp 6.5 KB

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