YoggieEngine/Editor/src/app.cpp

288 lines
7.8 KiB
C++
Raw Normal View History

2022-11-12 15:57:34 +00:00
#include "../../YoggieEngine/src/EntryPoint.h"
#include "../../YoggieEngine/src/AssetManager/ModelImporter.h"
#include "../../YoggieEngine/src/Physics/Physics.h"
#include <nfd.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "UI/GUIRenderer.h"
#include "UI/Widgets.h"
2022-11-10 20:51:11 +00:00
#include "Project/Project.h"
#include "SceneSerializer.h"
2022-11-10 20:51:11 +00:00
#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);
2023-01-04 14:57:08 +00:00
Camera cam = Camera(glm::vec3(14.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90);
class Editor : public Application {
public:
Editor()
: Application("Editor"),
AppWindow(1200,700),
framebuffer(new Framebuffer(1200,700)),
viewportRenderer(EditorSceneRendererConfig),
EditorGUIRenderer(AppWindow),
Selected((entt::entity)-1)
{
init_inputSystem(&AppWindow);
viewportRenderer.setCurrentFrameBuffer(*framebuffer);
}
2022-12-21 18:11:27 +00:00
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);
2022-12-21 18:11:27 +00:00
});
// Render scene
viewportRenderer.Render(ActiveScene);
}
void RenderEditorGUI() {
EditorGUIRenderer.Begin();
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
// Show a menu bar
{
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)
{
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 (firstMouse)
{
lastX = newX;
lastY = newY;
firstMouse = false;
}
float xoffset = newX - lastX;
float yoffset = newY - lastY;
lastX = newX;
lastY = newY;
xoffset *= sensitivity;
yoffset *= sensitivity;
cam.yaw += xoffset;
cam.pitch += yoffset;
if (cam.pitch > 89.0f)
cam.pitch = 89.0f;
if (cam.pitch < -89.0f)
cam.pitch = -89.0f;
}
else if (firstMouse == false)
{
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;
if (keyIsPressed(YOGGIE_KEY_A))
cam.Position -= cam.Right * movement_speed;
if (keyIsPressed(YOGGIE_KEY_S))
cam.Position -= cam.Front * movement_speed;
if (keyIsPressed(YOGGIE_KEY_D))
cam.Position += cam.Right * movement_speed;
}
}
{
RuntimeControls rc = RuntimeControls();
}
{
SceneExplorer explorer(Selected, ActiveScene);
}
2022-11-10 20:51:11 +00:00
{
Inspector inspector = Inspector();
if (ActiveScene.getReg().valid(Selected)) {
Entity SelectedEntity = Entity(Selected, &ActiveScene);
inspector.AddComponentDropDown(SelectedEntity);
inspector.ShowComponents(SelectedEntity);
2022-11-10 20:51:11 +00:00
}
}
{
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;
2023-01-04 14:57:08 +00:00
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();
2023-01-04 14:57:08 +00:00
Physics.Step(elapsed);
ActiveScene.Update();
RenderScene();
2023-01-04 14:57:08 +00:00
/*Physics.DebugRender(*framebuffer);*/
RenderEditorGUI();
AppWindow.SwapBuffers();
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
}
delete framebuffer;
ActiveScene.Stop();
}
private:
NativeWindow AppWindow;
Framebuffer* framebuffer;
Renderer viewportRenderer;
GUIRenderer EditorGUIRenderer;
// Editor State
2023-01-04 14:57:08 +00:00
bool SimulatePhysics = true;
entt::entity Selected;
std::unique_ptr<Project> CurrentProject;
Scene ActiveScene;
2022-12-21 18:11:27 +00:00
};
2022-11-12 15:57:34 +00:00
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);
2023-01-04 14:57:08 +00:00
auto Grass = scene.AddEntity("Grass/Window-Pane");
//auto& renderGrass = Grass.AddComponent<Render3DComponent>();
}