YoggieEngine/Editor/src/app.cpp

269 lines
6.7 KiB
C++
Raw Normal View History

#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <imgui.h>
#include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_glfw.h>
#include <imgui_internal.h>
#include <nfd.h>
#include "../../libs/guizmo/ImGuizmo.h"
#include "UI/Widgets.h"
#include "Project.h"
#include "SceneSerializer.h"
#include "EditorContext.h"
#include "SceneRuntime.h"
const unsigned int MS_PER_UPDATE = 2;
class Editor : public Application {
public:
Editor() : Application("Editor") {}
void Run() override
{
BarinkWindow mainWindow = BarinkWindow(1200, 700);
InputSystem = new InputManager();
renderer = new Renderer();
InputSystem->attach(&mainWindow);
InitImGui(mainWindow);
activeRuntime.Start();
double previous = glfwGetTime();
double lag = 0.0;
renderer->Prepare(activeRuntime.MainScene);
while (!mainWindow.WindowShouldClose())
{
double current = glfwGetTime();
double elapsed = current - previous;
previous = current;
lag += elapsed;
InputSystem->PollEvents();
while (lag >= MS_PER_UPDATE)
{
activeRuntime.Update();
lag -= MS_PER_UPDATE;
}
renderer->Render(activeRuntime.framebuffer, activeRuntime.MainScene);
ImGuiBegin();
RenderGUI();
ImGuiEnd();
mainWindow.SwapBuffers();
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
}
activeRuntime.Stop();
delete InputSystem;
delete renderer;
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void InitImGui(BarinkWindow& window )
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_::ImGuiConfigFlags_ViewportsEnable;
io.ConfigFlags |= ImGuiConfigFlags_::ImGuiConfigFlags_DockingEnable;
io.Fonts->AddFontFromFileTTF("build/Debug/Fonts/Roboto-Regular.ttf", 18);
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window.windowptr(), true);
ImGui_ImplOpenGL3_Init("#version 440");
}
void ImGuiBegin() {
ImGui_ImplGlfw_NewFrame();
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
ImGuizmo::SetOrthographic(true);
ImGuizmo::BeginFrame();
}
void ImGuiEnd() {
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* last_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(last_context);
}
}
void RenderGUI() {
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
// Show a menu bar
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("Application")) {
if (ImGui::MenuItem("Load Project"))
{
nfdresult_t result = NFD_OpenDialog({ "yproj" }, NULL, &path);
switch (result) {
case(NFD_OKAY):
Project::LoadProject(path, activeRuntime.CurrentProject);
break;
case(NFD_CANCEL):
break;
case(NFD_ERROR):
std::cout << "NFD_Error: " << NFD_GetError() << std::endl;
break;
}
}
if (ImGui::MenuItem("Save project as...")) {
nfdresult_t result = NFD_SaveDialog({ "yproj" }, NULL, &savePath);
switch (result) {
case(NFD_OKAY):
std::cout << "Save as: " << savePath << std::endl;
Project::SaveProject(savePath, *activeRuntime.CurrentProject.get());
break;
case(NFD_CANCEL):
break;
case(NFD_ERROR):
std::cout << "NFD_Error: " << NFD_GetError() << std::endl;
break;
}
}
if (ImGui::MenuItem("Preferences"))
{
}
if (ImGui::MenuItem("Exit"))
{
// TODO: Exit application
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Scene")) {
if (ImGui::MenuItem("Save scene"))
{
nfdresult_t result = NFD_SaveDialog({ "yscene" }, NULL, &scenePath);
switch (result) {
case(NFD_OKAY):
SaveScene(scenePath, activeRuntime.MainScene);
break;
case(NFD_CANCEL):
break;
case(NFD_ERROR):
std::cout << "NFD_Error: " << NFD_GetError() << std::endl;
break;
}
}
if (ImGui::MenuItem("Load scene"))
{
auto result = NFD_OpenDialog({ "yscene" }, NULL, &openScenePath);
switch (result) {
case (NFD_OKAY):
LoadScene(openScenePath, activeRuntime.MainScene);
break;
case(NFD_CANCEL):
break;
case(NFD_ERROR):
std::cout << "NFD_Error: " << NFD_GetError() << std::endl;
break;
}
}
if (ImGui::MenuItem("Add Entity")) {
activeRuntime.MainScene.AddEntity("New Entity");
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
ImGui::Begin("ProjectInfo");
ImGui::Text("Project: %s", activeRuntime.CurrentProject.get()->GetName().c_str());
ImGui::Text("Directory: %s", activeRuntime.CurrentProject.get()->GetProjectDirectory().u8string().c_str());
ImGui::End();
//ShowStats();
Viewport(*activeRuntime.framebuffer);
SceneExplorer(activeRuntime.Selected, activeRuntime.MainScene);
Inspector(activeRuntime.Selected, activeRuntime.MainScene);
Settings();
AssetsFinder();
Console();
ImGui::ShowDemoWindow();
ImGui::ShowMetricsWindow();
}
private:
EditorContext context;
SceneRuntime activeRuntime ;
char* path = nullptr;
char* savePath = nullptr;
char* scenePath = nullptr;
char* openScenePath = nullptr;
};
int main (int argc , char* argv[]) {
Editor().Run();
}