-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathEditor.cpp
More file actions
254 lines (217 loc) · 9.67 KB
/
Editor.cpp
File metadata and controls
254 lines (217 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
Copyright(c) 2015-2026 Panos Karabelas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//= INCLUDES ====================================
#include "pch.h"
#include "Editor.h"
#include "GeneralWindows.h"
#include "Widgets/MenuBar.h"
#include "Core/Engine.h"
#include "Core/Settings.h"
#include "ImGui/ImGui_Extension.h"
#include "ImGui/Implementation/ImGui_RHI.h"
#include "ImGui/Implementation/imgui_impl_sdl3.h"
#include "Widgets/AssetBrowser.h"
#include "Widgets/Console.h"
#include "Widgets/LightAuditor.h"
#include "Widgets/Style.h"
#include "Widgets/ProgressDialog.h"
#include "Widgets/Properties.h"
#include "Widgets/Viewport.h"
#include "Widgets/WorldViewer.h"
#include "Widgets/ShaderEditor.h"
#include "Widgets/ResourceViewer.h"
#include "Widgets/Profiler.h"
#include "Widgets/RenderOptions.h"
#include "Widgets/ScriptEditor.h"
//===============================================
//= NAMESPACES =====
using namespace std;
//==================
namespace
{
float font_size = 18.0f;
float font_scale = 1.0f;
void process_event(spartan::sp_variant data)
{
SDL_Event* event_sdl = static_cast<SDL_Event*>(get<void*>(data));
ImGui_ImplSDL3_ProcessEvent(event_sdl);
}
}
Editor::Editor(const vector<string>& args)
{
spartan::Engine::Initialize(args);
ImGui::CreateContext();
// configure ImGui
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange; // cursor control is given to ImGui, but dynamically, from the engine
io.ConfigWindowsResizeFromEdges = true;
io.IniFilename = "editor.ini";
// font_bold configuration
ImFontConfig config; // config for bold font (mainly for use in headers)
config.GlyphOffset.y = -2.0f;
const string dir_fonts = spartan::ResourceCache::GetResourceDirectory(spartan::ResourceDirectory::Fonts) + "/";
font_normal = io.Fonts->AddFontFromFileTTF((dir_fonts + "OpenSans/OpenSans-Medium.ttf").c_str(), font_size * spartan::Window::GetDpiScale());
font_bold = io.Fonts->AddFontFromFileTTF((dir_fonts + "OpenSans/OpenSans-Bold.ttf").c_str(), font_size * spartan::Window::GetDpiScale(), &config);
io.FontGlobalScale = font_scale;
// initialize imgui backends
SP_ASSERT_MSG(ImGui_ImplSDL3_InitForVulkan(static_cast<SDL_Window*>(spartan::Window::GetHandleSDL())), "Failed to initialize ImGui's SDL backend");
ImGui::RHI::Initialize();
// create all imgui widgets
m_widgets.emplace_back(make_shared<Style>(this));
m_widgets.emplace_back(make_shared<ProgressDialog>(this));
m_widgets.emplace_back(make_shared<Console>(this));
m_widgets.emplace_back(make_shared<Profiler>(this));
m_widgets.emplace_back(make_shared<ResourceViewer>(this));
m_widgets.emplace_back(make_shared<ShaderEditor>(this));
m_widgets.emplace_back(make_shared<ScriptEditor>(this));
m_widgets.emplace_back(make_shared<RenderOptions>(this));
m_widgets.emplace_back(make_shared<LightAuditor>(this));
m_widgets.emplace_back(make_shared<TextureViewer>(this));
m_widgets.emplace_back(make_shared<Viewport>(this));
m_widgets.emplace_back(make_shared<AssetBrowser>(this));
m_widgets.emplace_back(make_shared<Properties>(this));
m_widgets.emplace_back(make_shared<WorldViewer>(this));
MenuBar::Initialize(this);
// allow imgui to get event's from the engine's event processing loop
SP_SUBSCRIBE_TO_EVENT(spartan::EventType::Sdl, SP_EVENT_HANDLER_VARIANT_STATIC(process_event));
GeneralWindows::Initialize(this);
}
Editor::~Editor()
{
if (ImGui::GetCurrentContext())
{
ImGui::RHI::shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
}
spartan::Engine::Shutdown();
}
void Editor::Tick()
{
// main loop
while (!spartan::Window::WantsToClose())
{
bool render_editor = spartan::Engine::IsFlagSet(spartan::EngineMode::EditorVisible);
// logic
{
// imgui
if (render_editor)
{
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
}
// engine
spartan::Engine::Tick();
// editor
if (render_editor)
{
BeginWindow();
for (shared_ptr<Widget>& widget : m_widgets)
{
widget->Tick();
}
MenuBar::Tick();
ImGui::End();
// various windows that don't belong to a certain widget
GeneralWindows::Tick();
}
}
// render
if (render_editor)
{
ImGui::Render();
if (spartan::Engine::IsFlagSet(spartan::EngineMode::EditorVisible))
{
// main window
ImGui::RHI::render(ImGui::GetDrawData());
spartan::Renderer::SubmitAndPresent();
}
// child windows
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
}
}
}
void Editor::BeginWindow()
{
// note: don't use ImGuiWindowFlags_MenuBar here since we use BeginMainMenuBar() separately
const auto window_flags =
ImGuiWindowFlags_NoDocking |
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoNavFocus;
// set window position and size to the work area (excludes main menu bar)
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize);
// draw window border for borderless window (use full viewport for border around entire window)
{
ImDrawList* draw_list = ImGui::GetForegroundDrawList();
ImVec2 min = viewport->Pos;
ImVec2 max = ImVec2(viewport->Pos.x + viewport->Size.x, viewport->Pos.y + viewport->Size.y);
ImU32 border_color = IM_COL32(40, 40, 42, 255);
draw_list->AddRect(min, max, border_color, 0.0f, 0, 1.0f);
}
// set window style
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
// begin window
const char* name = "##main_window";
bool open = true;
ImGui::Begin(name, &open, window_flags);
ImGui::PopStyleVar(3);
// begin dock space
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable)
{
// dock space
const auto window_id = ImGui::GetID(name);
if (!ImGui::DockBuilderGetNode(window_id))
{
// reset current docking state
ImGui::DockBuilderRemoveNode(window_id);
ImGui::DockBuilderAddNode(window_id, ImGuiDockNodeFlags_None);
ImGui::DockBuilderSetNodeSize(window_id, ImGui::GetMainViewport()->Size);
// dockBuilderSplitNode(ImGuiID node_id, ImGuiDir split_dir, float size_ratio_for_node_at_dir, ImGuiID* out_id_dir, ImGuiID* out_id_other);
ImGuiID dock_main_id = window_id;
ImGuiID dock_right_id = ImGui::DockBuilderSplitNode(dock_main_id, ImGuiDir_Right, 0.17f, nullptr, &dock_main_id);
ImGuiID dock_right_down_id = ImGui::DockBuilderSplitNode(dock_right_id, ImGuiDir_Down, 0.6f, nullptr, &dock_right_id);
ImGuiID dock_down_id = ImGui::DockBuilderSplitNode(dock_main_id, ImGuiDir_Down, 0.22f, nullptr, &dock_main_id);
ImGuiID dock_down_right_id = ImGui::DockBuilderSplitNode(dock_down_id, ImGuiDir_Right, 0.3f, nullptr, &dock_down_id);
// dock windows
ImGui::DockBuilderDockWindow("World", dock_right_id);
ImGui::DockBuilderDockWindow("Properties", dock_right_down_id);
ImGui::DockBuilderDockWindow("Console", dock_down_id);
ImGui::DockBuilderDockWindow("Assets", dock_down_right_id);
ImGui::DockBuilderDockWindow("Viewport", dock_main_id);
ImGui::DockBuilderFinish(dock_main_id);
}
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 0.0f);
ImGui::DockSpace(window_id, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_PassthruCentralNode);
ImGui::PopStyleVar();
}
}