added play/pause and basic icon storage

This commit is contained in:
Erris
2026-02-28 09:42:20 +01:00
parent 9de9609dee
commit 3192d7b9a0
5 changed files with 157 additions and 39 deletions

View File

@@ -5,6 +5,8 @@
#include <open_engine.hpp>
#include "open_engine/application.hpp"
#include "open_engine/logging.hpp"
#include "open_engine/renderer/texture.hpp"
#include "open_engine/scene/entity.hpp"
#include "open_engine/scene/scene_serializer.hpp"
#include "panels/content_browser.hpp"
@@ -24,6 +26,12 @@
namespace OpenEngine {
enum class PlayState
{
Edit = 0,
Play = 1
};
class CameraController : public NativeScriptableEntity
{
public:
@@ -85,6 +93,10 @@ namespace OpenEngine {
editor_camera = EditorCamera(30.0f, 1920.0f/1080.0f, 0.1f, 1000.0f);
icons["play"] = Texture2D::Create("resources/textures/icons/play.png");
icons["stop"] = Texture2D::Create("resources/textures/icons/stop.png");
// TODO: Add file texture. Get free icons and add license
//icons["folder"] = Texture2D::Create("resources/textures/icons/folder.png");
/*
for (float i = 0; i < 200; i++) {
for (float y = 0; y < 200; y++) {
@@ -102,6 +114,13 @@ namespace OpenEngine {
scene_hierarchy.Init(scene);
}
Ref<Texture2D> GetIcon(const char* name)
{
if (icons.contains(name))
return icons[name];
return nullptr;
}
void OnDetach() override
{
}
@@ -126,9 +145,19 @@ namespace OpenEngine {
RenderCommand::Clear();
framebuffer->ClearBufferI(1, -1);
editor_camera.OnUpdate();
scene->OnUpdateEditor(editor_camera);
switch (state) {
case PlayState::Play: {
scene->OnUpdateRuntime();
break;
}
case PlayState::Edit: {
if (viewport_focused)
editor_camera.OnUpdate();
scene->OnUpdateEditor(editor_camera);
break;
}
}
auto [mx, my] = ImGui::GetMousePos();
@@ -144,8 +173,10 @@ namespace OpenEngine {
// Mouse Picking
if (Input::IsMouseButtonPressed(MouseCode::ButtonLeft)
&& mouse_x >= 0 && mouse_y >= 0
&& mouse_x < (int)viewport_size.x && (int)viewport_size.y &&
!ImGuizmo::IsOver() && !Input::IsKeyPressed(KeyCode::LeftAlt)) {
&& mouse_x < (int)viewport_size.x
&& mouse_y < (int)viewport_size.y
&& !ImGuizmo::IsOver()
&& !Input::IsKeyPressed(KeyCode::LeftAlt)) {
if (!clicked) {
int id = framebuffer->ReadPixel(1, mouse_x, mouse_y);
@@ -155,7 +186,6 @@ namespace OpenEngine {
scene_hierarchy.SetSelectedEntity(selected_entity);
}
clicked = true;
} else {
clicked = false;
@@ -254,9 +284,9 @@ namespace OpenEngine {
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0, 0 });
ImGui::Begin("Viewport");
bool focused = ImGui::IsWindowFocused();
bool hovered = ImGui::IsWindowHovered();
Application::Get().GetImGuiLayer()->SetBlockEvents((!focused && !hovered) || ImGui::IsAnyItemActive());
viewport_focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow);
viewport_hovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow);
Application::Get().GetImGuiLayer()->SetBlockEvents((!viewport_focused && !viewport_hovered) || ImGui::IsAnyItemActive());
ImVec2 viewport_panel_size = ImGui::GetContentRegionAvail();
@@ -370,11 +400,50 @@ namespace OpenEngine {
}
}
void DrawPlayBar()
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0, 2 });
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing, { 0, 0 });
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { 0, 0 });
ImGui::PushStyleColor(ImGuiCol_Button, { 0, 0, 0, 0 });
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, { 0.200f, 0.207f, 0.286f, 1.0f });
ImGui::PushStyleColor(ImGuiCol_ButtonActive, { 0, 0, 0, 0 });
ImGui::Begin("##play_state_bar", nullptr, ImGuiWindowFlags_NoDecoration
| ImGuiWindowFlags_NoScrollWithMouse
| ImGuiWindowFlags_NoScrollbar
| ImGuiWindowFlags_NoTitleBar);
float size = ImGui::GetContentRegionAvail().y;
auto icon = state == PlayState::Play ? icons["stop"] : icons["play"];
ImGui::SetCursorPosX((ImGui::GetContentRegionAvail().x * 0.5) - (size * 0.5) );
if (ImGui::ImageButton("##play_button",
(ImTextureID)icon->GetID(), { size, size })) {
switch (state) {
case PlayState::Play : {
OnSceneStop();
break;
}
case PlayState::Edit : {
OnScenePlay();
break;
}
}
}
ImGui::End();
ImGui::PopStyleColor(3);
ImGui::PopStyleVar(3);
}
void OnImGuiRender() override
{
OE_PROFILE_FUNCTION();
ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(450, 200));
ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(450, 35));
{
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
@@ -409,15 +478,25 @@ namespace OpenEngine {
DrawStats();
scene_hierarchy.OnImGuiRender();
brower.OnImGuiRender();
browser.OnImGuiRender();
DrawViewport();
DrawPlayBar();
ImGui::ShowDemoWindow();
ImGui::PopStyleVar();
};
void OnScenePlay()
{
state = PlayState::Play;
}
void OnSceneStop()
{
state = PlayState::Edit;
}
private:
ContentBrowserPanel brower;
ContentBrowserPanel browser;
std::vector<Time::ProfilingResult> profiling_results;
glm::vec2 world_pos_cursor = { 0.0f, 0.0f };
@@ -426,12 +505,18 @@ namespace OpenEngine {
Ref<OpenEngine::FrameBuffer> framebuffer;
glm::vec2 viewport_bounds[2];
bool viewport_focused = false, viewport_hovered = false;
SceneHierarchy scene_hierarchy;
EditorCamera editor_camera;
int guizmo_operation = -1;
// TODO: Rename this to GameState
PlayState state = PlayState::Edit;
std::unordered_map<const char*, Ref<Texture2D>> icons;
Ref<Scene> scene;
Entity selected_entity;

View File

@@ -16,7 +16,7 @@ namespace OpenEngine {
: current_directory(assets_directory)
{
// TODO: Add file texture. Get free icons and add license
folder_icon = Texture2D::Create("resources/textures/folder.png");
folder_icon = Texture2D::Create("resources/textures/icons/folder.png");
}
void ContentBrowserPanel::OnImGuiRender()