BsRenderWindowManager.cpp 7.0 KB

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