Skip to content

Commit 454c921

Browse files
Improving the Imgui layer abstraction (#413)
* updated formatting and fixed minors issues * refactored imgui layer
1 parent d08d098 commit 454c921

14 files changed

+248
-263
lines changed

Tetragrama/Components/DockspaceUIComponent.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ namespace Tetragrama::Components
7878
m_window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
7979
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
8080

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

8383
ImGui::PopStyleVar(3);
8484

8585
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable)
8686
{
8787
// Dock space
88-
const auto window_id = ImGui::GetID(m_name.c_str());
88+
const auto window_id = ImGui::GetID(Name.c_str());
8989
if (!ImGui::DockBuilderGetNode(window_id))
9090
{
9191
// Reset current docking state
@@ -158,9 +158,9 @@ namespace Tetragrama::Components
158158
if (ImGui::Button("...", ImVec2(50, 0)) && is_import_button_enabled)
159159
{
160160
Helpers::UIDispatcher::RunAsync([this]() -> std::future<void> {
161-
if (auto layer = m_parent_layer.lock())
161+
if (ParentLayer)
162162
{
163-
auto window = layer->GetAttachedWindow();
163+
auto window = ParentLayer->GetAttachedWindow();
164164
std::vector<std::string_view> filters{".obj", ".gltf"};
165165
std::string filename = co_await window->OpenFileDialogAsync(filters);
166166

@@ -536,9 +536,9 @@ namespace Tetragrama::Components
536536

537537
std::future<void> DockspaceUIComponent::OnOpenSceneAsync()
538538
{
539-
if (auto layer = m_parent_layer.lock())
539+
if (ParentLayer)
540540
{
541-
auto window = layer->GetAttachedWindow();
541+
auto window = ParentLayer->GetAttachedWindow();
542542
std::vector<std::string_view> filters = {"."};
543543
std::string scene_filename = co_await window->OpenFileDialogAsync(filters);
544544

@@ -566,10 +566,10 @@ namespace Tetragrama::Components
566566

567567
std::future<void> DockspaceUIComponent::OnExitAsync()
568568
{
569-
if (auto layer = m_parent_layer.lock())
569+
if (ParentLayer)
570570
{
571571
ZEngine::Windows::Events::WindowClosedEvent e{};
572-
layer->OnEvent(e);
572+
ParentLayer->OnEvent(e);
573573
}
574574
ZENGINE_CORE_WARN("Editor stopped")
575575
co_return;

Tetragrama/Components/HierarchyViewUIComponent.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ namespace Tetragrama::Components
6161

6262
void HierarchyViewUIComponent::Render(ZEngine::Rendering::Renderers::GraphicRenderer* const renderer, ZEngine::Rendering::Buffers::CommandBuffer* const command_buffer)
6363
{
64-
ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse);
65-
if (ImGui::BeginPopupContextWindow(m_name.c_str()))
64+
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse);
65+
if (ImGui::BeginPopupContextWindow(Name.c_str()))
6666
{
6767
if (ImGui::MenuItem("Create Empty"))
6868
{

Tetragrama/Components/InspectorViewUIComponent.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ namespace Tetragrama::Components
6868

6969
std::future<void> InspectorViewUIComponent::RequestStartOrPauseRenderMessageHandlerAsync(Messengers::GenericMessage<bool>& message)
7070
{
71-
{
72-
std::lock_guard lock(m_mutex);
73-
m_is_allowed_to_render = message.GetValue();
74-
}
7571
co_return;
7672
}
7773

@@ -84,7 +80,7 @@ namespace Tetragrama::Components
8480
m_recieved_unselected_request = false;
8581
}
8682

87-
ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse);
83+
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse);
8884

8985
Helpers::DrawEntityControl("Name", m_scene_entity, m_node_flag, [this] {
9086
ImGui::Dummy(ImVec2(0, 3));

Tetragrama/Components/LogUIComponent.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Tetragrama::Components
2626

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

3131
ImGui::SameLine();
3232
m_is_clear_button_pressed = ImGui::Button("Clear");

Tetragrama/Components/ProjectViewUIComponent.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Tetragrama::Components
1212

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

1717
ImGui::End();
1818
}

Tetragrama/Components/SceneViewportUIComponent.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <pch.h>
2-
#include <Layers/UILayer.h>
32
#include <MessageToken.h>
43
#include <Messengers/Messenger.h>
54
#include <SceneViewportUIComponent.h>
@@ -73,7 +72,7 @@ namespace Tetragrama::Components
7372
void SceneViewportUIComponent::Render(ZEngine::Rendering::Renderers::GraphicRenderer* const renderer, ZEngine::Rendering::Buffers::CommandBuffer* const command_buffer)
7473
{
7574
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
76-
ImGui::Begin(m_name.c_str(), (m_can_be_closed ? &m_can_be_closed : NULL), ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove);
75+
ImGui::Begin(Name.c_str(), (CanBeClosed ? &CanBeClosed : NULL), ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove);
7776

7877
auto viewport_offset = ImGui::GetCursorPos();
7978
m_content_region_available_size = ImGui::GetContentRegionAvail();

Tetragrama/Components/UIComponent.cpp

-56
Original file line numberDiff line numberDiff line change
@@ -1,56 +0,0 @@
1-
#include <pch.h>
2-
#include <UIComponent.h>
3-
4-
using namespace ZEngine;
5-
using namespace ZEngine::Helpers;
6-
7-
namespace Tetragrama::Components
8-
{
9-
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) {}
10-
11-
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) {}
12-
13-
void UIComponent::SetName(std::string_view name)
14-
{
15-
std::string_view current(m_name);
16-
if (current.compare(name) != 0)
17-
{
18-
m_name = name.data();
19-
}
20-
}
21-
22-
void UIComponent::SetVisibility(bool visibility)
23-
{
24-
m_visibility = visibility;
25-
}
26-
27-
std::string_view UIComponent::GetName() const
28-
{
29-
return m_name;
30-
}
31-
32-
bool UIComponent::GetVisibility() const
33-
{
34-
return m_visibility;
35-
}
36-
37-
void UIComponent::SetParentLayer(const Ref<Layers::ImguiLayer>& layer)
38-
{
39-
m_parent_layer = layer;
40-
}
41-
42-
bool UIComponent::HasParentLayer() const
43-
{
44-
return m_parent_layer.expired() == false;
45-
}
46-
47-
void UIComponent::SetParentUI(const Ref<UIComponent>& item)
48-
{
49-
m_parent_ui = item;
50-
}
51-
52-
bool UIComponent::HasParentUI() const
53-
{
54-
return m_parent_ui.expired() == false;
55-
}
56-
} // namespace Tetragrama::Components

Tetragrama/Components/UIComponent.h

+10-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#pragma once
2-
#include <Events/UIComponentEvent.h>
32
#include <ImguiLayer.h>
43
#include <ZEngine/Core/IRenderable.h>
54
#include <ZEngine/Core/IUpdatable.h>
65
#include <ZEngine/Helpers/IntrusivePtr.h>
76
#include <string>
8-
#include <vector>
97

108
namespace Tetragrama::Layers
119
{
@@ -14,33 +12,17 @@ namespace Tetragrama::Layers
1412

1513
namespace Tetragrama::Components
1614
{
17-
class UIComponent : public ZEngine::Core::IRenderable, public ZEngine::Core::IUpdatable, public ZEngine::Helpers::RefCounted
15+
struct UIComponent : public ZEngine::Core::IRenderable, public ZEngine::Core::IUpdatable, public ZEngine::Helpers::RefCounted
1816
{
19-
20-
public:
2117
UIComponent() = default;
22-
UIComponent(std::string_view name, bool visibility, bool can_be_closed);
23-
UIComponent(const ZEngine::Helpers::Ref<Tetragrama::Layers::ImguiLayer>& layer, std::string_view name, bool visibility, bool can_be_closed);
24-
virtual ~UIComponent() = default;
25-
26-
void SetName(std::string_view name);
27-
void SetVisibility(bool visibility);
28-
29-
std::string_view GetName() const;
30-
bool GetVisibility() const;
31-
32-
void SetParentLayer(const ZEngine::Helpers::Ref<Tetragrama::Layers::ImguiLayer>& layer);
33-
void SetParentUI(const ZEngine::Helpers::Ref<UIComponent>& item);
34-
35-
bool HasParentLayer() const;
36-
bool HasParentUI() const;
37-
38-
protected:
39-
bool m_visibility{false};
40-
bool m_can_be_closed{false};
41-
bool m_is_allowed_to_render{true};
42-
std::string m_name;
43-
ZEngine::Helpers::WeakRef<Tetragrama::Layers::ImguiLayer> m_parent_layer;
44-
ZEngine::Helpers::WeakRef<UIComponent> m_parent_ui;
18+
UIComponent(std::string_view name, bool visibility, bool can_be_closed) : Name(name.data()), IsVisible(visibility), CanBeClosed(can_be_closed) {}
19+
virtual ~UIComponent() = default;
20+
21+
bool IsVisible = true;
22+
bool CanBeClosed = false;
23+
uint32_t ChildrenCount = 0;
24+
Tetragrama::Layers::ImguiLayer* ParentLayer = nullptr;
25+
std::string Name = "";
26+
std::vector<UIComponent*> Children = {};
4527
};
4628
} // namespace Tetragrama::Components

Tetragrama/Editor.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <pch.h>
22
#include <Editor.h>
3-
#include <Layers/UILayer.h>
43
#include <MessageToken.h>
54
#include <Messengers/Messenger.h>
65
#include <fmt/format.h>
@@ -17,7 +16,7 @@ namespace Tetragrama
1716

1817
Editor::Editor(const EditorConfiguration& config) : m_engine_configuration()
1918
{
20-
m_ui_layer = CreateRef<Layers::UILayer>();
19+
m_ui_layer = CreateRef<Layers::ImguiLayer>();
2120
m_render_layer = CreateRef<Layers::RenderLayer>();
2221

2322
s_editor_configuration = config;

0 commit comments

Comments
 (0)