1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6#include "Utility/BsModule.h"
7#include "RenderAPI/BsRenderWindow.h"
8#include "Utility/BsEvent.h"
9
10namespace bs
11{
12 /** @addtogroup RenderAPI-Internal
13 * @{
14 */
15
16 /** Handles creation and internal updates relating to render windows. */
17 class BS_CORE_EXPORT RenderWindowManager : public Module<RenderWindowManager>
18 {
19 public:
20 RenderWindowManager() = default;
21 ~RenderWindowManager() = default;
22
23 /**
24 * Creates a new render window using the specified options. Optionally makes the created window a child of another
25 * window.
26 */
27 SPtr<RenderWindow> create(RENDER_WINDOW_DESC& desc, SPtr<RenderWindow> parentWindow);
28
29 /** Called once per frame. Dispatches events. */
30 void _update();
31
32 /** Called by the core thread when window is destroyed. */
33 void notifyWindowDestroyed(RenderWindow* window);
34
35 /** Called by the core thread when window receives focus. */
36 void notifyFocusReceived(ct::RenderWindow* window);
37
38 /** Called by the core thread when window loses focus. */
39 void notifyFocusLost(ct::RenderWindow* window);
40
41 /** Called by the core thread when window is moved or resized. */
42 void notifyMovedOrResized(ct::RenderWindow* window);
43
44 /** Called by the core thread when mouse leaves a window. */
45 void notifyMouseLeft(ct::RenderWindow* window);
46
47 /** Called by the core thread when the user requests for the window to close. */
48 void notifyCloseRequested(ct::RenderWindow* coreWindow);
49
50 /** Called by the sim thread when window properties change. */
51 void notifySyncDataDirty(ct::RenderWindow* coreWindow);
52
53 /** Returns a list of all open render windows. */
54 Vector<RenderWindow*> getRenderWindows() const;
55
56 /** Returns the window that is currently the top-most modal window. Returns null if no modal windows are active. */
57 RenderWindow* getTopMostModal() const;
58
59 /** Event that is triggered when a window gains focus. */
60 Event<void(RenderWindow&)> onFocusGained;
61
62 /** Event that is triggered when a window loses focus. */
63 Event<void(RenderWindow&)> onFocusLost;
64
65 /** Event that is triggered when mouse leaves a window. */
66 Event<void(RenderWindow&)> onMouseLeftWindow;
67 protected:
68 friend class RenderWindow;
69
70 /** Finds a sim thread equivalent of the provided core thread window implementation. */
71 RenderWindow* getNonCore(const ct::RenderWindow* window) const;
72
73 /** @copydoc create */
74 virtual SPtr<RenderWindow> createImpl(RENDER_WINDOW_DESC& desc, UINT32 windowId, const SPtr<RenderWindow>& parentWindow) = 0;
75
76 protected:
77 mutable Mutex mWindowMutex;
78 Map<UINT32, RenderWindow*> mWindows;
79 Vector<RenderWindow*> mModalWindowStack;
80
81 RenderWindow* mWindowInFocus = nullptr;
82 RenderWindow* mNewWindowInFocus = nullptr;
83 Vector<RenderWindow*> mMovedOrResizedWindows;
84 Vector<RenderWindow*> mMouseLeftWindows;
85 Vector<RenderWindow*> mCloseRequestedWindows;
86 UnorderedSet<RenderWindow*> mDirtyProperties;
87 };
88
89 namespace ct
90 {
91 /**
92 * Handles creation and internal updates relating to render windows.
93 *
94 * @note Core thread only.
95 */
96 class BS_CORE_EXPORT RenderWindowManager : public Module<RenderWindowManager>
97 {
98 public:
99 RenderWindowManager();
100
101 /** Called once per frame. Dispatches events. */
102 void _update();
103
104 /** Called by the core thread when window properties change. */
105 void notifySyncDataDirty(RenderWindow* window);
106
107 /** Returns a list of all open render windows. */
108 Vector<RenderWindow*> getRenderWindows() const;
109
110 protected:
111 friend class RenderWindow;
112 friend class bs::RenderWindow;
113 friend class bs::RenderWindowManager;
114
115 /** Called whenever a window is created. */
116 void windowCreated(RenderWindow* window);
117
118 /** Called by the core thread when window is destroyed. */
119 void windowDestroyed(RenderWindow* window);
120
121 mutable Mutex mWindowMutex;
122 Vector<RenderWindow*> mCreatedWindows;
123 UnorderedSet<RenderWindow*> mDirtyProperties;
124 std::atomic_uint mNextWindowId;
125 };
126 }
127
128 /** @} */
129}