BsRenderWindowManager.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. for (auto& dirtyPropertyEntry : mDirtyProperties)
  14. {
  15. DirtyPropertyData& dirtyPropertyData = dirtyPropertyEntry.second;
  16. if (dirtyPropertyEntry.second.data != nullptr)
  17. bs_delete(dirtyPropertyEntry.second.data);
  18. }
  19. mDirtyProperties.clear();
  20. }
  21. RenderWindowPtr RenderWindowManager::create(RENDER_WINDOW_DESC& desc, RenderWindowPtr parentWindow)
  22. {
  23. RenderWindowPtr renderWindow = createImpl(desc, parentWindow);
  24. renderWindow->_setThisPtr(renderWindow);
  25. renderWindow->initialize();
  26. {
  27. BS_LOCK_MUTEX(mWindowMutex);
  28. mCreatedWindows.push_back(renderWindow.get());
  29. mCoreToNonCoreMap[renderWindow->getCore().get()] = renderWindow.get();
  30. }
  31. return renderWindow;
  32. }
  33. void RenderWindowManager::notifyWindowDestroyed(RenderWindow* window)
  34. {
  35. {
  36. BS_LOCK_MUTEX(mWindowMutex);
  37. auto iterFind = std::find(begin(mCreatedWindows), end(mCreatedWindows), window);
  38. if(iterFind == mCreatedWindows.end())
  39. BS_EXCEPT(InternalErrorException, "Trying to destroy a window that is not in the created windows list.");
  40. mCreatedWindows.erase(iterFind);
  41. auto iterFind2 = std::find(begin(mMovedOrResizedWindows), end(mMovedOrResizedWindows), window);
  42. if(iterFind2 != mMovedOrResizedWindows.end())
  43. mMovedOrResizedWindows.erase(iterFind2);
  44. auto iterFind3 = mDirtyProperties.find(window);
  45. if (iterFind3 != mDirtyProperties.end())
  46. {
  47. if (iterFind3->second.data != nullptr)
  48. bs_delete(iterFind3->second.data);
  49. mDirtyProperties.erase(iterFind3);
  50. }
  51. mCoreToNonCoreMap.erase(window->getCore().get());
  52. }
  53. }
  54. void RenderWindowManager::notifyFocusReceived(RenderWindowCore* coreWindow)
  55. {
  56. RenderWindow* window = getNonCore(coreWindow);
  57. BS_LOCK_MUTEX(mWindowMutex);
  58. mNewWindowInFocus = window;
  59. setDirtyProperties(coreWindow);
  60. }
  61. void RenderWindowManager::notifyFocusLost(RenderWindowCore* coreWindow)
  62. {
  63. RenderWindow* window = getNonCore(coreWindow);
  64. BS_LOCK_MUTEX(mWindowMutex);
  65. mNewWindowInFocus = nullptr;
  66. setDirtyProperties(coreWindow);
  67. }
  68. void RenderWindowManager::notifyMovedOrResized(RenderWindowCore* coreWindow)
  69. {
  70. RenderWindow* window = getNonCore(coreWindow);
  71. bool isValidWindow = false;
  72. {
  73. BS_LOCK_MUTEX(mWindowMutex);
  74. isValidWindow = std::find(begin(mCreatedWindows), end(mCreatedWindows), window) != mCreatedWindows.end();
  75. }
  76. if(!isValidWindow)
  77. return;
  78. BS_LOCK_MUTEX(mWindowMutex);
  79. auto iterFind = std::find(begin(mMovedOrResizedWindows), end(mMovedOrResizedWindows), window);
  80. if (iterFind == end(mMovedOrResizedWindows))
  81. mMovedOrResizedWindows.push_back(window);
  82. setDirtyProperties(coreWindow);
  83. }
  84. void RenderWindowManager::notifyPropertiesDirty(RenderWindowCore* coreWindow)
  85. {
  86. RenderWindow* window = getNonCore(coreWindow);
  87. BS_LOCK_MUTEX(mWindowMutex);
  88. setDirtyProperties(coreWindow);
  89. }
  90. void RenderWindowManager::setDirtyProperties(RenderWindowCore* coreWindow)
  91. {
  92. RenderWindow* window = getNonCore(coreWindow);
  93. auto iterFind = mDirtyProperties.find(window);
  94. if (iterFind != mDirtyProperties.end())
  95. {
  96. if (iterFind->second.data != nullptr)
  97. bs_delete(iterFind->second.data);
  98. }
  99. mDirtyProperties[window] = DirtyPropertyData();
  100. DirtyPropertyData& dirtyPropertyData = mDirtyProperties[window];
  101. dirtyPropertyData.size = coreWindow->getSyncData(nullptr);
  102. dirtyPropertyData.data = (UINT8*)bs_alloc(dirtyPropertyData.size);
  103. coreWindow->getSyncData(dirtyPropertyData.data);
  104. }
  105. void RenderWindowManager::windowMouseLeft(RenderWindowCore* coreWindow)
  106. {
  107. BS_LOCK_MUTEX(mWindowMutex);
  108. RenderWindow* window = getNonCore(coreWindow);
  109. auto iterFind = std::find(begin(mMouseLeftWindows), end(mMouseLeftWindows), window);
  110. if (iterFind == end(mMouseLeftWindows))
  111. mMouseLeftWindows.push_back(window);
  112. }
  113. void RenderWindowManager::_update()
  114. {
  115. RenderWindow* newWinInFocus = nullptr;
  116. Vector<RenderWindow*> movedOrResizedWindows;
  117. Vector<RenderWindow*> mouseLeftWindows;
  118. {
  119. BS_LOCK_MUTEX(mWindowMutex);
  120. newWinInFocus = mNewWindowInFocus;
  121. movedOrResizedWindows = mMovedOrResizedWindows;
  122. mMovedOrResizedWindows.clear();
  123. mouseLeftWindows = mMouseLeftWindows;
  124. mMouseLeftWindows.clear();
  125. for (auto& dirtyPropertyEntry : mDirtyProperties)
  126. {
  127. RenderWindow* window = dirtyPropertyEntry.first;
  128. DirtyPropertyData& dirtyPropertyData = dirtyPropertyEntry.second;
  129. window->setSyncData(dirtyPropertyData.data, dirtyPropertyData.size);
  130. if (dirtyPropertyEntry.second.data != nullptr)
  131. bs_delete(dirtyPropertyEntry.second.data);
  132. }
  133. mDirtyProperties.clear();
  134. }
  135. if(mWindowInFocus != newWinInFocus)
  136. {
  137. if(mWindowInFocus != nullptr)
  138. {
  139. if(!onFocusLost.empty())
  140. onFocusLost(*mWindowInFocus);
  141. }
  142. if(newWinInFocus != nullptr)
  143. {
  144. if(!onFocusGained.empty())
  145. onFocusGained(*newWinInFocus);
  146. }
  147. mWindowInFocus = newWinInFocus;
  148. }
  149. for(auto& window : movedOrResizedWindows)
  150. {
  151. if(!window->onResized.empty())
  152. window->onResized();
  153. }
  154. if (!onMouseLeftWindow.empty())
  155. {
  156. for (auto& window : mouseLeftWindows)
  157. onMouseLeftWindow(*window);
  158. }
  159. }
  160. Vector<RenderWindow*> RenderWindowManager::getRenderWindows() const
  161. {
  162. BS_LOCK_MUTEX(mWindowMutex);
  163. return mCreatedWindows;
  164. }
  165. RenderWindow* RenderWindowManager::getNonCore(const RenderWindowCore* window) const
  166. {
  167. auto iterFind = mCoreToNonCoreMap.find(window);
  168. if (iterFind != mCoreToNonCoreMap.end())
  169. return iterFind->second;
  170. return nullptr;
  171. }
  172. SPtr<RenderWindowCore> RenderWindowCoreManager::create(RENDER_WINDOW_DESC& desc)
  173. {
  174. SPtr<RenderWindowCore> renderWindow = createInternal(desc);
  175. renderWindow->initialize();
  176. return renderWindow;
  177. }
  178. void RenderWindowCoreManager::windowCreated(RenderWindowCore* window)
  179. {
  180. BS_LOCK_MUTEX(mWindowMutex);
  181. mCreatedWindows.push_back(window);
  182. }
  183. void RenderWindowCoreManager::windowDestroyed(RenderWindowCore* window)
  184. {
  185. {
  186. BS_LOCK_MUTEX(mWindowMutex);
  187. auto iterFind = std::find(begin(mCreatedWindows), end(mCreatedWindows), window);
  188. if (iterFind == mCreatedWindows.end())
  189. BS_EXCEPT(InternalErrorException, "Trying to destroy a window that is not in the created windows list.");
  190. mCreatedWindows.erase(iterFind);
  191. }
  192. }
  193. Vector<RenderWindowCore*> RenderWindowCoreManager::getRenderWindows() const
  194. {
  195. BS_LOCK_MUTEX(mWindowMutex);
  196. return mCreatedWindows;
  197. }
  198. }