Skip to content

Commit

Permalink
Improving the Imgui layer abstraction (JeanPhilippeKernel#413)
Browse files Browse the repository at this point in the history
* updated formatting and fixed minors issues

* refactored imgui layer
  • Loading branch information
JeanPhilippeKernel authored Jan 6, 2025
1 parent d08d098 commit 454c921
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 263 deletions.
16 changes: 8 additions & 8 deletions Tetragrama/Components/DockspaceUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ namespace Tetragrama::Components
m_window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));

ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), m_window_flags);
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), m_window_flags);

ImGui::PopStyleVar(3);

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable)
{
// Dock space
const auto window_id = ImGui::GetID(m_name.c_str());
const auto window_id = ImGui::GetID(Name.c_str());
if (!ImGui::DockBuilderGetNode(window_id))
{
// Reset current docking state
Expand Down Expand Up @@ -158,9 +158,9 @@ namespace Tetragrama::Components
if (ImGui::Button("...", ImVec2(50, 0)) && is_import_button_enabled)
{
Helpers::UIDispatcher::RunAsync([this]() -> std::future<void> {
if (auto layer = m_parent_layer.lock())
if (ParentLayer)
{
auto window = layer->GetAttachedWindow();
auto window = ParentLayer->GetAttachedWindow();
std::vector<std::string_view> filters{".obj", ".gltf"};
std::string filename = co_await window->OpenFileDialogAsync(filters);

Expand Down Expand Up @@ -536,9 +536,9 @@ namespace Tetragrama::Components

std::future<void> DockspaceUIComponent::OnOpenSceneAsync()
{
if (auto layer = m_parent_layer.lock())
if (ParentLayer)
{
auto window = layer->GetAttachedWindow();
auto window = ParentLayer->GetAttachedWindow();
std::vector<std::string_view> filters = {"."};
std::string scene_filename = co_await window->OpenFileDialogAsync(filters);

Expand Down Expand Up @@ -566,10 +566,10 @@ namespace Tetragrama::Components

std::future<void> DockspaceUIComponent::OnExitAsync()
{
if (auto layer = m_parent_layer.lock())
if (ParentLayer)
{
ZEngine::Windows::Events::WindowClosedEvent e{};
layer->OnEvent(e);
ParentLayer->OnEvent(e);
}
ZENGINE_CORE_WARN("Editor stopped")
co_return;
Expand Down
4 changes: 2 additions & 2 deletions Tetragrama/Components/HierarchyViewUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ namespace Tetragrama::Components

void HierarchyViewUIComponent::Render(ZEngine::Rendering::Renderers::GraphicRenderer* const renderer, ZEngine::Rendering::Buffers::CommandBuffer* const command_buffer)
{
ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse);
if (ImGui::BeginPopupContextWindow(m_name.c_str()))
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse);
if (ImGui::BeginPopupContextWindow(Name.c_str()))
{
if (ImGui::MenuItem("Create Empty"))
{
Expand Down
6 changes: 1 addition & 5 deletions Tetragrama/Components/InspectorViewUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ namespace Tetragrama::Components

std::future<void> InspectorViewUIComponent::RequestStartOrPauseRenderMessageHandlerAsync(Messengers::GenericMessage<bool>& message)
{
{
std::lock_guard lock(m_mutex);
m_is_allowed_to_render = message.GetValue();
}
co_return;
}

Expand All @@ -84,7 +80,7 @@ namespace Tetragrama::Components
m_recieved_unselected_request = false;
}

ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse);
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse);

Helpers::DrawEntityControl("Name", m_scene_entity, m_node_flag, [this] {
ImGui::Dummy(ImVec2(0, 3));
Expand Down
2 changes: 1 addition & 1 deletion Tetragrama/Components/LogUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Tetragrama::Components

void LogUIComponent::Render(ZEngine::Rendering::Renderers::GraphicRenderer* const renderer, ZEngine::Rendering::Buffers::CommandBuffer* const command_buffer)
{
ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse);
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse);

ImGui::SameLine();
m_is_clear_button_pressed = ImGui::Button("Clear");
Expand Down
2 changes: 1 addition & 1 deletion Tetragrama/Components/ProjectViewUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tetragrama::Components

void ProjectViewUIComponent::Render(ZEngine::Rendering::Renderers::GraphicRenderer* const renderer, ZEngine::Rendering::Buffers::CommandBuffer* const command_buffer)
{
ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse);
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse);

ImGui::End();
}
Expand Down
3 changes: 1 addition & 2 deletions Tetragrama/Components/SceneViewportUIComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <pch.h>
#include <Layers/UILayer.h>
#include <MessageToken.h>
#include <Messengers/Messenger.h>
#include <SceneViewportUIComponent.h>
Expand Down Expand Up @@ -73,7 +72,7 @@ namespace Tetragrama::Components
void SceneViewportUIComponent::Render(ZEngine::Rendering::Renderers::GraphicRenderer* const renderer, ZEngine::Rendering::Buffers::CommandBuffer* const command_buffer)
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove);
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove);

auto viewport_offset = ImGui::GetCursorPos();
m_content_region_available_size = ImGui::GetContentRegionAvail();
Expand Down
56 changes: 0 additions & 56 deletions Tetragrama/Components/UIComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,56 +0,0 @@
#include <pch.h>
#include <UIComponent.h>

using namespace ZEngine;
using namespace ZEngine::Helpers;

namespace Tetragrama::Components
{
UIComponent::UIComponent(std::string_view name, bool visibility, bool can_be_closed) : m_name(name.data()), m_visibility(visibility), m_can_be_closed(can_be_closed) {}

UIComponent::UIComponent(const Ref<Layers::ImguiLayer>& layer, std::string_view name, bool visibility, bool can_be_closed) : m_parent_layer(layer), m_name(name.data()), m_visibility(visibility), m_can_be_closed(can_be_closed) {}

void UIComponent::SetName(std::string_view name)
{
std::string_view current(m_name);
if (current.compare(name) != 0)
{
m_name = name.data();
}
}

void UIComponent::SetVisibility(bool visibility)
{
m_visibility = visibility;
}

std::string_view UIComponent::GetName() const
{
return m_name;
}

bool UIComponent::GetVisibility() const
{
return m_visibility;
}

void UIComponent::SetParentLayer(const Ref<Layers::ImguiLayer>& layer)
{
m_parent_layer = layer;
}

bool UIComponent::HasParentLayer() const
{
return m_parent_layer.expired() == false;
}

void UIComponent::SetParentUI(const Ref<UIComponent>& item)
{
m_parent_ui = item;
}

bool UIComponent::HasParentUI() const
{
return m_parent_ui.expired() == false;
}
} // namespace Tetragrama::Components
38 changes: 10 additions & 28 deletions Tetragrama/Components/UIComponent.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#pragma once
#include <Events/UIComponentEvent.h>
#include <ImguiLayer.h>
#include <ZEngine/Core/IRenderable.h>
#include <ZEngine/Core/IUpdatable.h>
#include <ZEngine/Helpers/IntrusivePtr.h>
#include <string>
#include <vector>

namespace Tetragrama::Layers
{
Expand All @@ -14,33 +12,17 @@ namespace Tetragrama::Layers

namespace Tetragrama::Components
{
class UIComponent : public ZEngine::Core::IRenderable, public ZEngine::Core::IUpdatable, public ZEngine::Helpers::RefCounted
struct UIComponent : public ZEngine::Core::IRenderable, public ZEngine::Core::IUpdatable, public ZEngine::Helpers::RefCounted
{

public:
UIComponent() = default;
UIComponent(std::string_view name, bool visibility, bool can_be_closed);
UIComponent(const ZEngine::Helpers::Ref<Tetragrama::Layers::ImguiLayer>& layer, std::string_view name, bool visibility, bool can_be_closed);
virtual ~UIComponent() = default;

void SetName(std::string_view name);
void SetVisibility(bool visibility);

std::string_view GetName() const;
bool GetVisibility() const;

void SetParentLayer(const ZEngine::Helpers::Ref<Tetragrama::Layers::ImguiLayer>& layer);
void SetParentUI(const ZEngine::Helpers::Ref<UIComponent>& item);

bool HasParentLayer() const;
bool HasParentUI() const;

protected:
bool m_visibility{false};
bool m_can_be_closed{false};
bool m_is_allowed_to_render{true};
std::string m_name;
ZEngine::Helpers::WeakRef<Tetragrama::Layers::ImguiLayer> m_parent_layer;
ZEngine::Helpers::WeakRef<UIComponent> m_parent_ui;
UIComponent(std::string_view name, bool visibility, bool can_be_closed) : Name(name.data()), IsVisible(visibility), CanBeClosed(can_be_closed) {}
virtual ~UIComponent() = default;

bool IsVisible = true;
bool CanBeClosed = false;
uint32_t ChildrenCount = 0;
Tetragrama::Layers::ImguiLayer* ParentLayer = nullptr;
std::string Name = "";
std::vector<UIComponent*> Children = {};
};
} // namespace Tetragrama::Components
3 changes: 1 addition & 2 deletions Tetragrama/Editor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <pch.h>
#include <Editor.h>
#include <Layers/UILayer.h>
#include <MessageToken.h>
#include <Messengers/Messenger.h>
#include <fmt/format.h>
Expand All @@ -17,7 +16,7 @@ namespace Tetragrama

Editor::Editor(const EditorConfiguration& config) : m_engine_configuration()
{
m_ui_layer = CreateRef<Layers::UILayer>();
m_ui_layer = CreateRef<Layers::ImguiLayer>();
m_render_layer = CreateRef<Layers::RenderLayer>();

s_editor_configuration = config;
Expand Down
Loading

0 comments on commit 454c921

Please sign in to comment.