BsRenderWindowManager.cpp 7.6 KB

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