-
Notifications
You must be signed in to change notification settings - Fork 1
/
ModuleGUI.cpp
325 lines (269 loc) · 8.61 KB
/
ModuleGUI.cpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "Globals.h"
#include "Application.h"
#include "ModuleRenderer3D.h"
#include "ModuleGUI.h"
#include "WindowGame.h"
#include "WindowHierarchy.h"
#include "WindowEngineStats.h"
#include "ModuleInput.h"
#include "About.h"
#include "ModuleFileSystem.h"
ModuleGUI::ModuleGUI(Application* app, bool start_enabled) : Module(app, start_enabled)
{
}
// Destructor
ModuleGUI::~ModuleGUI()
{
}
// Called before render is available
bool ModuleGUI::Init()
{
LOGC("Starting GUI module");
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
io = &ImGui::GetIO(); (void)io;
io->DisplaySize.x = SCREEN_WIDTH; // set the current display width
io->DisplaySize.y = SCREEN_HEIGHT; // set the current display height here
int width, height;
unsigned char* pixels = NULL;
io->Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
io->ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOpenGL(App->window->window, App->renderer3D->context);
ImGui_ImplOpenGL3_Init();
//INIT WINDOWS
game = new WindowGame(App);
about = new About(App);
windows.push_back(game);
windows.push_back(new WindowHierarchy(App));
inspector = new WindowInspector(App);
windows.push_back(new WindowEngineStats(App));
windows.push_back(inspector);
windows.push_back(about);
filesystem = new WindowFileSystem(App);
windows.push_back(filesystem);
return true;
}
bool ModuleGUI::Start() {
list <Module*> ::iterator it;
for (it = windows.begin(); it != windows.end(); ++it) {
Module* m = *it;
if (m != nullptr)
m->Start();
}
return true;
}
// PreUpdate: clear buffer
update_status ModuleGUI::PreUpdate(float dt)
{
list <Module*> ::iterator it;
for (it = windows.begin(); it != windows.end(); ++it) {
Module* m = *it;
if (m != nullptr)
m->PreUpdate(dt);
}
return UPDATE_CONTINUE;
}
update_status ModuleGUI::Update(float dt)
{
list <Module*> ::iterator it;
for (it = windows.begin(); it != windows.end(); ++it) {
Module* m = *it;
if (m != nullptr)
m->Update(dt);
}
return UPDATE_CONTINUE;
}
update_status ModuleGUI::PostUpdate(float dt)
{
list <Module*> ::iterator it;
for (it = windows.begin(); it != windows.end(); ++it) {
Module* m = *it;
if (m != nullptr)
m->PostUpdate(dt);
}
return UPDATE_CONTINUE;
}
// PostUpdate present buffer to screen
bool ModuleGUI::Draw()
{
//Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(App->window->window);
ImGui::NewFrame();
//Create Windows
App->input->quit = !CreateMenuBar(); //Create Menu Bar
if (App->input->GetKey(SDL_SCANCODE_F1) == KEY_DOWN) {
openConsole = !openConsole;
}
if (openConsole)
ShowConsole();
if (openWindowSettings)
ShowWindowSettings();
list <Module*> ::iterator it;
for (it = windows.begin(); it != windows.end(); ++it) {
Module* m = *it;
if (m != nullptr && m->isEnabled())
m->Draw();
}
if (savePopUp)
ImGui::OpenPopup("Save..");
App->scene_intro->SaveScenePopUp();
//renderig UI
ImGui::Render();
//view
glViewport(0, 0, (int)io->DisplaySize.x, (int)io->DisplaySize.y);
glClearColor(0.25f, 0.25f, 0.25f, 0.25f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context where shaders may be bound
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
return UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleGUI::CleanUp()
{
list <Module*> ::iterator it;
for (it = windows.begin(); it != windows.end(); ++it) {
Module* m = *it;
if (m != nullptr) {
m->CleanUp();
delete m;
m = nullptr;
}
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
return true;
}
bool ModuleGUI::CreateMenuBar() {
bool ret = true;
bool opt_fullscreen = true;
static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None;
// We are using the ImGuiWindowFlags_NoDocking flag to make the parent window not dockable into,
// because it would be confusing to have two docking targets within each others.
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->Pos);
ImGui::SetNextWindowSize(viewport->Size);
ImGui::SetNextWindowViewport(viewport->ID);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
// When using ImGuiDockNodeFlags_PassthruCentralNode, DockSpace() will render our background and handle the pass-thru hole, so we ask Begin() to not render a background.
dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode;
window_flags |= ImGuiWindowFlags_NoBackground;
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::Begin("DockSpace Demo", &p_open, window_flags);
ImGui::PopStyleVar();
if (opt_fullscreen)
ImGui::PopStyleVar(2);
// DockSpace
ImGuiIO& io = ImGui::GetIO();
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable)
{
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
}
ImGui::End();
if (ImGui::BeginMainMenuBar())
{
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("New")) {}
//if (ImGui::MenuItem("Open Scene", "Ctrl+O")) {}
if (ImGui::BeginMenu("Open Scene")) {
App->scene_intro->LoadScenePopUp();
ImGui::EndMenu();
}
if (ImGui::MenuItem("Save Scene", "Ctrl+S") || (App->input->GetKey(SDL_SCANCODE_LCTRL) == KEY_REPEAT && App->input->GetKey(SDL_SCANCODE_S) == KEY_DOWN)){
savePopUp = true;
}
//if (ImGui::MenuItem("Save As..")) {}
if (ImGui::MenuItem("Exit")) {
ret = false;
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit"))
{
if (ImGui::MenuItem("Undo", "CTRL+Z")) {}
if (ImGui::MenuItem("Redo", "CTRL+Y", false, false)) {} // Disabled item
ImGui::Separator();
if (ImGui::MenuItem("Cut", "CTRL+X")) {}
if (ImGui::MenuItem("Copy", "CTRL+C")) {}
if (ImGui::MenuItem("Paste", "CTRL+V")) {}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Window"))
{
if (ImGui::MenuItem("Game", "", openGame)) {
openGame = !openGame;
}
if (ImGui::MenuItem("Console", "F1", openConsole)) {
openConsole = !openConsole;
}
if (ImGui::MenuItem("Hirearchy", "", openHirearchy)) {
openHirearchy = !openHirearchy;
}
if (ImGui::MenuItem("Inspector", "", openInspector)) {
openInspector = !openInspector;
}
if (ImGui::MenuItem("Engine Stats", "", ShowFPS)) {
ShowFPS = !ShowFPS;
}
if (ImGui::MenuItem("Settings",NULL, openWindowSettings)) {
openWindowSettings = !openWindowSettings;
}
if (ImGui::MenuItem("Assets", "", openFileSystem)) {
openFileSystem = !openFileSystem;
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Help"))
{
if (ImGui::MenuItem("Mercury Engine Repository")) {
ShellExecuteA(NULL, "open", "https://github.com/knela96/Mercury-Engine", NULL, NULL, SW_SHOWNORMAL);
}
if (ImGui::MenuItem("Found any bug?")) {
ShellExecuteA(NULL, "open", "https://github.com/knela96/Mercury-Engine/issues", NULL, NULL, SW_SHOWNORMAL);
}
if (ImGui::MenuItem("About this Engine", "", openAbout)) {
openAbout = !openAbout;
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}else
LOG("Cannot create Menu Bar");
return ret;
}
void ModuleGUI::ShowConsole() {
//Console Code
console.Draw("Console", &openConsole);
}
void ModuleGUI::ShowWindowSettings() {
ImGui::Begin("Settings",&openWindowSettings);
/*ImGui::Text("Width: ");
ImGui::SameLine(); ImGui::PushID("screen_width");
ImGui::SliderInt("px", &screen_width, 800, 3840); ImGui::PopID();
ImGui::Text("Height: ");
ImGui::SameLine(); ImGui::PushID("screen_height");
ImGui::SliderInt("px", &screen_height, 600, 2160); ImGui::PopID();*/
if (ImGui::Checkbox("Fullscreen", &fullscreen))
App->window->SetFullscreen(fullscreen);
ImGui::SameLine();
if (ImGui::Checkbox("Borderless", &borderless))
App->window->SetBorderless(borderless);
if (ImGui::Checkbox("Resizable", &resizable))
App->window->SetResizable(resizable);
//windowed full screen borderless, frame cap, vsync, inputs list (q teclas pulsas), mouse position, camera fov, resolution
ImGui::Checkbox("Frame rate cap activated", &App->framerate_cap_activated);
ImGui::Text("Frame rate: "); ImGui::SameLine();
ImGui::SliderInt("fps",&App->framerate_cap, 10, 60);
SDL_GetMouseState(&MouseX,&MouseY);
ImGui::Text("Mouse Position: x=%i , y=%i ",MouseX,MouseY);
ImGui::End();
}