SpiecsEngine
 
Loading...
Searching...
No Matches
ImguiViewport.cpp
Go to the documentation of this file.
1/**
2* @file ImguiViewport.cpp.
3* @brief The ImguiViewport Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9
10#include "Render/Vulkan/VulkanRenderBackend.h"
11#include "Systems/SlateSystem.h"
13#include "ImGuizmo.h"
15#include "..\..\..\Core\Thread\ThreadModel.h"
16#include "Slate/SlateStyleLayout.h"
17
18namespace Spices {
19
21 const std::string& panelName ,
22 FrameInfo& frameInfo ,
23 uint32_t index
24 )
25 : ImguiSlate(panelName, frameInfo)
26 , m_Index(index)
27 {
29
30 {
31 SPICES_PROFILE_ZONEN("Create SceneColor ImTextureID");
32
33 m_ViewportContext = std::make_shared<SlateImage>("SceneColor", "SlateRenderer.Slate.Default");
34 }
35
36 /**
37 * @brief Instance a FloattingInfo.
38 */
39 {
40 std::stringstream ss;
41 ss << "FloatingInfo_" << m_Index;
42
43 m_FloatingInfo = SlateSystem::GetRegister()->Register<ImguiFloatingInfo>(false, ss.str(), this);
44 }
45
46 /**
47 * @brief Instance a Gizmos.
48 */
49 {
50 std::stringstream ss;
51 ss << "Gizmos_" << m_Index;
52
53 m_Gizmos = SlateSystem::GetRegister()->Register<ImguiGizmos>(false, ss.str(), this);
54 }
55
56 /**
57 * @brief Instance a Gizmos.
58 */
59 {
60 std::stringstream ss;
61 ss << "ToolBar_" << m_Index;
62
63 m_ToolBar = SlateSystem::GetRegister()->Register<ImguiViewportToolBar>(false, ss.str(), this);
64 }
65
66 /**
67 * @brief Build Toggle behave list.
68 */
69 {
70 m_ToggleStateList = std::make_shared<scl::behave_state_list<void>>();
71
72 {
73 auto state = m_ToggleStateList->AddNode();
74 state->PushBehave("Toggle", [&]() {
75
76 m_IsToggled = !m_IsToggled;
77
78 if (m_IsToggled)
79 {
80 AsyncTask(ThreadPoolEnum::Game, []() {
81 SlateStyleLayout::Get()->StoreLayoutCache();
82 });
83 }
84 else
85 {
86 m_WindowFlags ^= ImGuiWindowFlags_NoResize & ImGuiWindowFlags_NoMove;
87
88 AsyncTask(ThreadPoolEnum::Game, []() {
89 SlateStyleLayout::Get()->LoadLayoutCache();
90 });
91 }
92 });
93 }
94 {
95 auto state = m_ToggleStateList->AddNode();
96 state->PushBehave("Toggle", [&]() {
97
98 if (m_IsToggled)
99 {
100 const ImGuiViewport* viewport = ImGui::GetMainViewport();
101 ImGui::SetNextWindowPos(viewport->Pos);
102 ImGui::SetNextWindowSize(viewport->Size);
103
104 m_WindowFlags |= ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
105 }
106 });
107 }
108
109 m_ToggleStateList->SetState(1);
110 }
111 }
112
114 {
116
117 m_ToggleStateList->GetState()->ExecuteBehave("Toggle");
118
119 /**
120 * @brief Begin render Console.
121 */
122 Begin(0.5, ImGuiWindowFlags_NoTitleBar);
123
124 /**
125 * @brief Render Viewport image.
126 */
127 {
128 SPICES_PROFILE_ZONEN("Render Viewport Image");
129
130 ImGuiH::CustomMaterialImage(m_ViewportContext.get(), m_PanelSize);
131 }
132
133 /**
134 * @brief Render ToolBar.
135 */
136 m_ToolBar->OnRender();
137
138 /**
139 * @brief Render FloatingInfo.
140 */
141 m_FloatingInfo->OnRender();
142
143 /**
144 * @brief Render Gizmos.
145 */
146 m_Gizmos->OnRender();
147
148 /**
149 * @brief End Viewport Slate.
150 */
151 End();
152
153 m_ToggleStateList->IncreateState();
154 }
155
157 {
159
160 /**
161 * @brief Instance a EventDispatcher.
162 */
163 EventDispatcher dispatcher(event);
164
165 /**
166 * @brief Dispatch SlateResizeEvent.
167 */
171 }
172
173 void ImguiViewport::QueryIsResizedThisFrame(const ImVec2& thisFrameSize)
174 {
176
177 /**
178 * @brief Clamp min value to 1 for viewport.
179 */
180 ImVec2 tempSize = ImVec2(glm::max(thisFrameSize.x, 1.0f), glm::max(thisFrameSize.y, 1.0f));
181
182 if (m_PanelSize.x != tempSize.x || m_PanelSize.y != tempSize.y)
183 {
184 m_IsResized = true;
185 }
186 else
187 {
188 m_IsResized = false;
189 }
190
191 m_PanelSize = tempSize;
192 }
193
194 std::pair<uint32_t, uint32_t> ImguiViewport::GetMousePosInViewport() const
195 {
197
198 ImGuiIO& io = ImGui::GetIO();
199 ImVec2 viewportPos = io.MousePos - m_PanelPos;
200
201 std::pair<uint32_t, uint32_t> pair = std::make_pair(
202 static_cast<uint32_t>(viewportPos.x),
203 static_cast<uint32_t>(viewportPos.y)
204 );
205
206 return pair;
207 }
208
209 void ImguiViewport::Toggle() const
210 {
212
213 m_ToggleStateList->ResetState();
214 }
215
217 {
219
220 /**
221 * @brief Recreate SceneColor SlateImage.
222 */
223 m_ViewportContext->ReBuildTextureID();
224
225 /**
226 * @brief Do not block the event.
227 */
228 return false;
229 }
230
232 {
234
235 /**
236 * @brief Recreate SceneColor SlateImage.
237 */
238 m_ViewportContext->ReBuildTextureID();
239
240 /**
241 * @brief Do not block the event.
242 */
243 return false;
244 }
245
247 {
249
250 if (event.GetKeyCode() == Key::F11)
251 {
252 Toggle();
253 }
254
255 return false;
256 }
257}
#define BIND_EVENT_FN(x)
Bind Event.
Definition Event.h:88
#define SPICES_PROFILE_ZONEN(...)
#define SPICES_PROFILE_ZONE
EventDispatcher(Event &event)
Constructor Function.
Definition Event.h:172
This Class store a Specific Event type first and Dispatch a event handle function to it.
Definition Event.h:156
This Class is the basic Event Class. Inherit from it and create specific event class.
Definition Event.h:96
FrameInfo Class. This class defines the FrameInfo data.
Definition FrameInfo.h:32
void End()
End a slate.
ImguiSlate(const std::string &panelName, FrameInfo &frameInfo)
Constructor Function. Init with Slate's name.
Definition ImguiUtils.h:35
bool m_IsResized
Boolean of whether resized this frame.
Definition ImguiUtils.h:186
This Class defines the basic behaves of specific slate. When we add an new Slate, we need inherit fro...
Definition ImguiUtils.h:27
uint32_t m_Index
This viewport index.
bool OnWindowResizeOver(WindowResizeOverEvent &event) const
Event Dispatcher target. Registry on Windows Resized.
void Toggle() const
Toggle viewport.
bool OnToggleSlate(KeyPressedEvent &event) const
Toggle focused slate.
ImguiViewport(const std::string &panelName, FrameInfo &frameInfo, uint32_t index=0)
Constructor Function.
bool OnSlateResize(SlateResizeEvent &event) const
Event Dispatcher target. Registry on Slate(Viewport) Resized.
virtual void QueryIsResizedThisFrame(const ImVec2 &thisFrameSize) override
Query whether viewport is resized this frame. Clamp min m_panelsize value to 1 here,...
virtual void OnRender() override
This interface is called On SlateRenderer Render.
std::pair< uint32_t, uint32_t > GetMousePosInViewport() const
Get Mouse Position Relative Viewport(Might be negative value).
virtual void OnEvent(Event &event) override
This interface is called On Global Event Function Pointer is called.
The ImguiViewport Class. This class defines how to render a viewport.
const int & GetKeyCode() const
Get Key Input Code.
Definition KeyEvent.h:30
This Class is inherited from KeyEvent Class.
Definition KeyEvent.h:57
This Class is inherited from Event Class. Called by Viewport Resize.
Definition SlateEvent.h:18