Editor Refactor
This refactor of the editor code makes the code more maintainable. All widget objects have now moved away from RAII and are now just allocated object that live for the entirety of the applications lifetime. This feels better as I am used to this style plus constantly pushing and popping objects from the stack seems a little wasteful (although I as of right now have no way to prove that it is ).
This commit is contained in:
@ -8,84 +8,63 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include "UI/GUIRenderer.h"
|
||||
#include "UI/Widgets.h"
|
||||
#include "Project/Project.h"
|
||||
#include "SceneSerializer.h"
|
||||
#include "AssetManagement/SceneSerializer.h"
|
||||
#include "AssetManagement/AssetManager.h"
|
||||
#include "UI/MainMenuBar.h"
|
||||
void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene);
|
||||
|
||||
RendererConfig EditorSceneRendererConfig{
|
||||
1200, // Screen Width
|
||||
700, // Screen Height
|
||||
glm::vec3{0,0,0}, // Clear Color
|
||||
true // Depth testing
|
||||
};
|
||||
glm::vec3 temp = glm::vec3(0);
|
||||
Camera cam = Camera(glm::vec3(14.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90);
|
||||
#include "Views/Viewport.h"
|
||||
#include "PropertyPanels/SceneExplorer.h"
|
||||
#include "AssetManagement/AssetFinder.h"
|
||||
#include "MainMenuBar.h"
|
||||
#include "PropertyPanels/Inspector.h"
|
||||
#include "Project/ProjectInfo.h"
|
||||
#include "Runtime/RuntimeControls.h"
|
||||
#include "Project/Settings.h"
|
||||
#include "Console.h"
|
||||
|
||||
using namespace YoggieEngine;
|
||||
|
||||
|
||||
class Editor : public Application {
|
||||
public:
|
||||
Editor()
|
||||
: Application("Editor"),
|
||||
AppWindow(1200,700),
|
||||
framebuffer(new Framebuffer(1200,700)),
|
||||
viewportRenderer(EditorSceneRendererConfig),
|
||||
EditorGUIRenderer(AppWindow),
|
||||
Selected((entt::entity)-1)
|
||||
Editor() : Application("Editor"), Selected((entt::entity)-1){}
|
||||
|
||||
void Run() override
|
||||
{
|
||||
LoadLastOrEmptyProject();
|
||||
|
||||
init_inputSystem(&AppWindow);
|
||||
viewportRenderer.setCurrentFrameBuffer(*framebuffer);
|
||||
}
|
||||
MainMenuBar menuBar = MainMenuBar();
|
||||
ProjectInfo projectInfo(project);
|
||||
Viewport sceneview = Viewport(scene);
|
||||
RuntimeControls rc = RuntimeControls();
|
||||
SceneExplorer explorer(Selected, scene);
|
||||
Inspector inspector = Inspector();
|
||||
Settings settings = Settings();
|
||||
// AssetFinder assetsView = AssetFinder();
|
||||
Console console = Console();
|
||||
|
||||
void RenderScene() {
|
||||
// submit DrawCommands for all render3DComponents
|
||||
auto group = ActiveScene.getReg().view<TransformComponent, Render3DComponent>();
|
||||
group.each([&](auto enity, TransformComponent& t, Render3DComponent& renderComponent) {
|
||||
viewportRenderer.Submit(renderComponent, t);
|
||||
});
|
||||
|
||||
// Render scene
|
||||
viewportRenderer.Render(ActiveScene);
|
||||
|
||||
}
|
||||
|
||||
void RenderEditorGUI() {
|
||||
EditorGUIRenderer.Begin();
|
||||
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
|
||||
|
||||
// Show a menu bar
|
||||
double previous = glfwGetTime();
|
||||
double lag = 0.0;
|
||||
while (!appWindow.WindowShouldClose())
|
||||
{
|
||||
MainMenuBar menuBar = MainMenuBar();
|
||||
menuBar.ApplicationMenu(CurrentProject);
|
||||
menuBar.SceneMenu(CurrentProject, ActiveScene);
|
||||
menuBar.SelectMenu();
|
||||
menuBar.WindowMenu();
|
||||
menuBar.DebugMenu();
|
||||
menuBar.Help();
|
||||
}
|
||||
|
||||
{
|
||||
ProjectInfo projectInfo(*(CurrentProject.get()));
|
||||
}
|
||||
|
||||
{
|
||||
Viewport sceneview = Viewport(*framebuffer, viewportRenderer.getCamera());
|
||||
if (sceneview.isFocused)
|
||||
PollEvents();
|
||||
double now = glfwGetTime();
|
||||
double elapsed = now - previous ;
|
||||
previous = now;
|
||||
lag += elapsed;
|
||||
|
||||
if (sceneview.isFocused)
|
||||
{
|
||||
const float movement_speed = 0.1f;
|
||||
static float lastX = 400, lastY = 300;
|
||||
const float sensitivity = 0.1;
|
||||
static bool firstMouse = true;
|
||||
|
||||
if (MouseButtonPressed(YOGGIE_MOUSE_BUTTON_RIGHT) ){
|
||||
|
||||
glfwSetInputMode(AppWindow.GetGLFWHandle(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
||||
auto newX = getCursorPosX(&AppWindow);
|
||||
auto newY = getCursorPosY(&AppWindow);
|
||||
if (MouseButtonPressed(YOGGIE_MOUSE_BUTTON_RIGHT)) {
|
||||
|
||||
glfwSetInputMode(appWindow.GetGLFWHandle(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
||||
auto newX = getCursorPosX(&appWindow);
|
||||
auto newY = getCursorPosY(&appWindow);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
@ -104,139 +83,124 @@ public:
|
||||
xoffset *= sensitivity;
|
||||
yoffset *= sensitivity;
|
||||
|
||||
cam.yaw += xoffset;
|
||||
cam.pitch += yoffset;
|
||||
sceneview.cam.yaw += xoffset;
|
||||
sceneview.cam.pitch += yoffset;
|
||||
|
||||
if (cam.pitch > 89.0f)
|
||||
cam.pitch = 89.0f;
|
||||
if (cam.pitch < -89.0f)
|
||||
cam.pitch = -89.0f;
|
||||
if (sceneview.cam.pitch > 89.0f)
|
||||
sceneview.cam.pitch = 89.0f;
|
||||
if (sceneview.cam.pitch < -89.0f)
|
||||
sceneview.cam.pitch = -89.0f;
|
||||
|
||||
}
|
||||
else if (firstMouse == false)
|
||||
{
|
||||
glfwSetInputMode(AppWindow.GetGLFWHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
firstMouse = true;
|
||||
glfwSetInputMode(appWindow.GetGLFWHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
firstMouse = true;
|
||||
}
|
||||
|
||||
|
||||
// Check for Camara movement input here!
|
||||
if (keyIsPressed(YOGGIE_KEY_W))
|
||||
cam.Position += cam.Front * movement_speed;
|
||||
sceneview.cam.Position += sceneview.cam.Front * movement_speed;
|
||||
|
||||
if (keyIsPressed(YOGGIE_KEY_A))
|
||||
cam.Position -= cam.Right * movement_speed;
|
||||
sceneview.cam.Position -= sceneview.cam.Right * movement_speed;
|
||||
|
||||
if (keyIsPressed(YOGGIE_KEY_S))
|
||||
cam.Position -= cam.Front * movement_speed;
|
||||
sceneview.cam.Position -= sceneview.cam.Front * movement_speed;
|
||||
|
||||
if (keyIsPressed(YOGGIE_KEY_D))
|
||||
cam.Position += cam.Right * movement_speed;
|
||||
sceneview.cam.Position += sceneview.cam.Right * movement_speed;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
GuiBegin();
|
||||
|
||||
{
|
||||
RuntimeControls rc = RuntimeControls();
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
SceneExplorer explorer(Selected, ActiveScene);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
Inspector inspector = Inspector();
|
||||
if (ActiveScene.getReg().valid(Selected)) {
|
||||
Entity SelectedEntity = Entity(Selected, &ActiveScene);
|
||||
// Show a menu bar
|
||||
menuBar.ApplicationMenu(project);
|
||||
menuBar.SceneMenu(project, scene);
|
||||
menuBar.SelectMenu();
|
||||
menuBar.WindowMenu();
|
||||
menuBar.DebugMenu();
|
||||
menuBar.Help();
|
||||
|
||||
|
||||
if (scene.getReg().valid(Selected)) {
|
||||
Entity SelectedEntity = Entity(Selected, &scene);
|
||||
inspector.AddComponentDropDown(SelectedEntity);
|
||||
inspector.ShowComponents(SelectedEntity);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
Settings settings = Settings();
|
||||
}
|
||||
|
||||
{
|
||||
// AssetFinder assetsView = AssetFinder();
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
Console console = Console();
|
||||
console.Show();
|
||||
}
|
||||
|
||||
ImGui::ShowDemoWindow();
|
||||
ImGui::ShowMetricsWindow();
|
||||
EditorGUIRenderer.End();
|
||||
}
|
||||
|
||||
void Run() override
|
||||
{
|
||||
CreateTestProject(CurrentProject, ActiveScene);
|
||||
ActiveScene.Start();
|
||||
|
||||
|
||||
// Create the physics engine demo!
|
||||
Physics Physics;
|
||||
Physics.Demo();
|
||||
/*Physics.EnableDebugVisuals();
|
||||
Physics.Step(0);
|
||||
Physics.SubmitMesh();
|
||||
*/
|
||||
|
||||
double previous = glfwGetTime();
|
||||
double lag = 0.0;
|
||||
while (!AppWindow.WindowShouldClose())
|
||||
{
|
||||
|
||||
double current = glfwGetTime();
|
||||
double elapsed = current - previous;
|
||||
previous = current;
|
||||
lag += elapsed;
|
||||
|
||||
AppWindow.Poll();
|
||||
cam.Update();
|
||||
projectInfo.Update();
|
||||
sceneview.Update();
|
||||
rc.Update();
|
||||
explorer.Update();
|
||||
settings.Update();
|
||||
inspector.Update();
|
||||
console.Update();
|
||||
|
||||
Physics.Step(elapsed);
|
||||
ActiveScene.Update();
|
||||
|
||||
RenderScene();
|
||||
/*Physics.DebugRender(*framebuffer);*/
|
||||
|
||||
RenderEditorGUI();
|
||||
|
||||
AppWindow.SwapBuffers();
|
||||
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
ImGui::ShowDemoWindow();
|
||||
ImGui::ShowMetricsWindow();
|
||||
|
||||
|
||||
GuiEnd();
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
delete framebuffer;
|
||||
ActiveScene.Stop();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void LoadLastOrEmptyProject() {
|
||||
// Check if there is a last known loaded project and
|
||||
// load that one .
|
||||
|
||||
// Otherwise load no project..
|
||||
// OR
|
||||
// Load an empty project.
|
||||
|
||||
std::string path = (std::filesystem::current_path()).string();
|
||||
project.setProjectDirectory(path);
|
||||
|
||||
AssetManager::Init();
|
||||
AssetManager::setAssetPath(project.GetProjectDirectory());
|
||||
AssetManager::BuildAssetView();
|
||||
|
||||
// Create a level and load it as the current level
|
||||
auto importer = ModelImporter();
|
||||
|
||||
|
||||
// create an ambient light source
|
||||
auto light = scene.AddEntity("Light");
|
||||
auto& lightComponent = light.AddComponent<LightComponent>();
|
||||
lightComponent.Color = glm::vec3(1.0f);
|
||||
|
||||
|
||||
// Create a cube
|
||||
auto model = importer.Import("build/Debug/Models/Cube.obj");
|
||||
|
||||
auto cube = scene.AddEntity("Cube");
|
||||
auto& render3DComponent = cube.AddComponent<Render3DComponent>();
|
||||
render3DComponent.mesh = *(model->renderable->mesh);
|
||||
|
||||
cube.GetComponent<TransformComponent>().Position = glm::vec3(1.0f, 0.0f, 5.0f);
|
||||
|
||||
auto cube2 = scene.AddEntity("Cube2");
|
||||
auto& rendercube2 = cube2.AddComponent<Render3DComponent>();
|
||||
rendercube2.mesh = *(model->renderable->mesh);
|
||||
auto& relationcube = cube.AddComponent<RelationComponent>(cube2);
|
||||
auto& rigidbodycomp = cube.AddComponent<RigidBody>();
|
||||
auto& rigidbodycomp2 = cube2.AddComponent<RigidBody>();
|
||||
|
||||
|
||||
auto Grass = scene.AddEntity("Grass/Window-Pane");
|
||||
//auto& renderGrass = Grass.AddComponent<Render3DComponent>();
|
||||
}
|
||||
|
||||
private:
|
||||
NativeWindow AppWindow;
|
||||
|
||||
Framebuffer* framebuffer;
|
||||
Renderer viewportRenderer;
|
||||
|
||||
GUIRenderer EditorGUIRenderer;
|
||||
|
||||
// Editor State
|
||||
bool SimulatePhysics = true;
|
||||
entt::entity Selected;
|
||||
|
||||
|
||||
|
||||
std::unique_ptr<Project> CurrentProject;
|
||||
Scene ActiveScene;
|
||||
Project project;
|
||||
Scene scene;
|
||||
|
||||
};
|
||||
|
||||
@ -245,43 +209,3 @@ YoggieEngine::Application* CreateApplication() {
|
||||
return new Editor();
|
||||
|
||||
}
|
||||
|
||||
void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene ) {
|
||||
project = std::make_unique<Project>();
|
||||
std::string path = (std::filesystem::current_path()).string();
|
||||
project.get()->setProjectDirectory(path);
|
||||
|
||||
AssetManager::Init();
|
||||
AssetManager::setAssetPath(project.get()->GetProjectDirectory());
|
||||
AssetManager::BuildAssetView();
|
||||
|
||||
// Create a level and load it as the current level
|
||||
auto importer = ModelImporter();
|
||||
|
||||
|
||||
// create an ambient light source
|
||||
auto light = scene.AddEntity("Light");
|
||||
auto lightComponent = light.AddComponent<LightComponent>();
|
||||
lightComponent.Color = glm::vec3(1.0f);
|
||||
|
||||
|
||||
// Create a cube
|
||||
auto model = importer.Import("build/Debug/Models/Cube.obj");
|
||||
|
||||
auto cube = scene.AddEntity("Cube");
|
||||
auto& render3DComponent = cube.AddComponent<Render3DComponent>();
|
||||
render3DComponent.mesh = *(model->renderable->mesh);
|
||||
|
||||
cube.GetComponent<TransformComponent>().Position = glm::vec3(1.0f, 0.0f, 5.0f);
|
||||
|
||||
auto cube2 = scene.AddEntity("Cube2");
|
||||
auto& rendercube2 = cube2.AddComponent<Render3DComponent>();
|
||||
rendercube2.mesh = *(model->renderable->mesh);
|
||||
auto relationcube = cube.AddComponent<RelationComponent>(cube2);
|
||||
|
||||
auto Grass = scene.AddEntity("Grass/Window-Pane");
|
||||
//auto& renderGrass = Grass.AddComponent<Render3DComponent>();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user