added quad primitive, shortcuts and safe entity deletion

This commit is contained in:
Erris
2026-03-02 21:27:42 +01:00
parent c189b12365
commit b7e5ceb1d1
7 changed files with 171 additions and 91 deletions

View File

@@ -1,9 +1,12 @@
#ifndef EDITOR_HPP
#define EDITOR_HPP
#include <X11/X.h>
#include <open_engine.hpp>
#include "open_engine/renderer/renderer3d.hpp"
#include "open_engine/scene/components.hpp"
#include "open_engine/scene/scene_serializer.hpp"
#include "panels/content_browser.hpp"
#include "panels/scene_hierarchy.hpp"
@@ -101,6 +104,14 @@ namespace OpenEngine {
cube.AddComponent<MeshComponent>(mesh);
Entity quad = scene->CreateEntity("quad");
quad.AddComponent<TransformComponent>();
quad.GetComponents<TransformComponent>().translation = {0, 0, 0};
Ref<Mesh> quad_mesh = CreateQuad(quad, true);
quad.AddComponent<MeshComponent>(quad_mesh);
Entity cube2 = scene->CreateEntity("cube2");
cube2.AddComponent<TransformComponent>();
cube2.GetComponents<TransformComponent>().translation = {2, 0, 0};
@@ -108,7 +119,6 @@ namespace OpenEngine {
Ref<Mesh> mesh2 = CreateCube(cube2);
cube2.AddComponent<MeshComponent>(mesh2);
OE_DEBUG("cubes is like {} {}", (uint32_t)cube, (uint32_t)cube2);
/*
auto view = scene->GetRegistry().view<TagComponent>();
@@ -217,6 +227,8 @@ namespace OpenEngine {
}
framebuffer->Unbind();
scene->UpdateEntities();
}
bool EditorKeyBinds(KeyPressedEvent& event)
@@ -227,6 +239,29 @@ namespace OpenEngine {
bool shift = Input::IsKeyPressed(Key::LeftShift) || Input::IsKeyPressed(Key::RightShift);
switch(event.GetKeyCode()) {
case KeyCode::S: {
if (control) {
std::string file = FileDialogs::SaveFile("useless");
OE_TRACE("saving to filename: {}", file);
SaveScene(file);
}
break;
}
case KeyCode::N: {
if (control) {
scene = CreateRef<Scene>();
scene->OnViewportResize((uint32_t)viewport_size.x, (uint32_t)viewport_size.y);
scene_hierarchy.Init(scene);
}
break;
}
case KeyCode::O: {
if (control) {
std::string file = FileDialogs::OpenFile("useless");
OE_DEBUG("loading scene: {}", file);
OpenScene(file);}
break;
}
case KeyCode::Q: {
guizmo_operation = -1;
break;
@@ -243,6 +278,12 @@ namespace OpenEngine {
guizmo_operation = ImGuizmo::OPERATION::SCALE;
break;
}
case KeyCode::Delete: {
scene->MarkEntityForDeletion(selected_entity);
scene_hierarchy.ClearSelection();
selected_entity = {};
break;
}
default:
break;
}
@@ -428,6 +469,14 @@ namespace OpenEngine {
}
}
void SaveScene(const std::string& file)
{
if (!file.empty()) {
SceneSerializer serializer(scene);
serializer.Serialize(file);
}
}
void DrawPlayBar()
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0, 2 });
@@ -478,10 +527,7 @@ namespace OpenEngine {
if (ImGui::MenuItem("Save Scene As", "Ctrl+S")) {
std::string file = FileDialogs::SaveFile("useless");
OE_TRACE("saving to filename: {}", file);
if (!file.empty()) {
SceneSerializer serializer(scene);
serializer.Serialize(file);
}
SaveScene(file);
}
if (ImGui::MenuItem("Open Scene", "Ctrl+O")) {
std::string file = FileDialogs::OpenFile("useless");
@@ -560,24 +606,3 @@ class EditorApp : public OpenEngine::Application
};
#endif // EDITOR_HPP
/*
#include <cstdio>
#include <string>
std::string OpenFile() {
char buffer[1024];
// This command opens a native GTK file picker and returns the path
FILE* pipe = popen("zenity --file-selection", "r");
if (!pipe) return "";
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
std::string path = buffer;
path.erase(path.find_last_not_of("\n") + 1); // Clean newline
pclose(pipe);
return path;
}
pclose(pipe);
return "";
}
*/

View File

@@ -27,6 +27,7 @@ namespace OpenEngine {
Entity GetSelectedEntity() const { return selected_context; };
void SetSelectedEntity(Entity entity) { selected_context = entity; };
void ClearSelection() { selected_context = {}; };
private:
void DrawEntityNode(Entity& entity);

View File

@@ -133,7 +133,6 @@ namespace OpenEngine {
{
auto& tag = entity.GetComponents<TagComponent>().tag;
bool entity_marked_deletion = false;
if (renamed_entity == entity && selected_context == entity) {
char buffer[255];
std::memset(buffer, 0, sizeof(buffer));
@@ -161,8 +160,10 @@ namespace OpenEngine {
bool opened = ImGui::TreeNodeEx((void*)(uint64_t)(uint32_t)entity, flags, "%s", tag.c_str());
if (ImGui::BeginPopupContextItem()) {
if (ImGui::MenuItem("Delete entity"))
entity_marked_deletion = true;
if (ImGui::MenuItem("Delete entity")) {
scene->MarkEntityForDeletion(entity);
selected_context = {};
}
ImGui::EndPopup();
}
@@ -189,12 +190,6 @@ namespace OpenEngine {
if (opened)
ImGui::TreePop();
}
if (entity_marked_deletion) {
scene->DeleteEntity(entity);
if (selected_context == entity)
selected_context = {};
}
}
void SceneHierarchy::DrawComponents(Entity& entity)