Add BaseApplication to wrap basic code

This commit is contained in:
SweetId 2023-10-08 22:26:43 -04:00
parent c60ece1670
commit 73e942809b
5 changed files with 187 additions and 0 deletions

View File

@ -1,6 +1,9 @@
#pragma once
#include <NazaraEditor/Core/Core.hpp>
#include <NazaraEditor/Core/Application/Action.hpp>
#include <NazaraEditor/Core/Application/BaseApplication.hpp>
#include <NazaraEditor/Core/Application/World.hpp>
#include <NazaraEditor/Core/UI/Window.hpp>
#include <NazaraEditor/Core/Reflection/Math.hpp>
#include <NazaraEditor/Core/Reflection/Core.hpp>

View File

@ -0,0 +1,46 @@
#pragma once
#include <NazaraEditor/Core/Core.hpp>
#include <NazaraEditor/Core/Application/Shortcut.hpp>
#include <Nazara/Renderer.hpp>
#include <string>
namespace Nz
{
class EditorAction
{
public:
struct Properties
{
std::string name;
std::string description;
Nz::Shortcut shortcut;
Nz::Texture* icon;
};
EditorAction(const Properties& properties)
: m_properties(std::make_shared<Properties>(properties))
{}
~EditorAction() = default;
EditorAction(const EditorAction&) = delete;
EditorAction& operator=(const EditorAction&) = delete;
EditorAction(EditorAction&&) = delete;
EditorAction& operator=(EditorAction&&) = delete;
virtual void Execute() = 0;
virtual void Revert() = 0;
virtual EditorAction* Clone() const = 0;
protected:
EditorAction(const std::shared_ptr<Properties>& properties)
: m_properties(properties)
{}
std::shared_ptr<Properties> m_properties;
};
}

View File

@ -0,0 +1,55 @@
#pragma once
#include <Nazara/Core.hpp>
#include <Nazara/Graphics.hpp>
#include <Nazara/Platform.hpp>
#include <Nazara/Renderer.hpp>
#include <Nazara/Utility.hpp>
#include <NazaraEditor/Core/Core.hpp>
#include <NazaraEditor/Core/Application/Action.hpp>
#include <NazaraEditor/Core/UI/Window.hpp>
#include <NazaraImgui/NazaraImgui.hpp>
namespace Nz
{
class NAZARAEDITOR_CORE_API EditorBaseApplication
: public Nz::Application<Nz::Graphics, Nz::Imgui, Nz::EditorCore>
{
public:
NazaraSignal(OnWorldChanged, Nz::EnttWorld*);
// Entity lifetime events
NazaraSignal(OnEntityCreated, entt::handle);
NazaraSignal(OnEntityDestroyed, entt::handle);
NazaraSignal(OnEntityParentChanged, entt::handle);
// Entity selection events
NazaraSignal(OnEntitySelected, entt::handle);
NazaraSignal(OnEntityDeselected, entt::handle);
EditorBaseApplication();
void NewWorld();
Nz::EnttWorld* GetCurrentWorld();
entt::handle CreateEntity();
template<typename T>
void RegisterWindow()
{
static_assert(std::is_base_of<Nz::EditorWindow, T>::value, "Register Window should be called with a subclass of Nz::EditorWindow");
m_windows.push_back(std::make_unique<T>(this));
}
private:
std::unique_ptr<Nz::WindowSwapchain> m_windowSwapchain;
Nz::EnttWorld* m_world;
std::vector<std::unique_ptr<Nz::EditorWindow>> m_windows;
std::vector<std::unique_ptr<EditorAction>> m_actions;
static EditorBaseApplication* s_instance;
};
}

View File

@ -0,0 +1,9 @@
#pragma once
namespace Nz
{
struct Shortcut
{
};
}

View File

@ -0,0 +1,74 @@
#include <NazaraEditor/Core/Application/BaseApplication.hpp>
namespace Nz
{
EditorBaseApplication* EditorBaseApplication::s_instance = nullptr;
EditorBaseApplication::EditorBaseApplication()
: m_world(nullptr)
{
s_instance = this;
auto& windowing = AddComponent<Nz::AppWindowingComponent>();
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
std::string windowTitle = "Nazara Editor";
Nz::Window& window = windowing.CreateWindow(Nz::VideoMode(1280, 720, 32), windowTitle);
m_windowSwapchain = std::make_unique<Nz::WindowSwapchain>(device, window);
// connect basic handler
window.GetEventHandler().OnQuit.Connect([&window](const auto* handler) {
NazaraUnused(handler);
window.Close();
});
AddComponent<Nz::AppEntitySystemComponent>();
Nz::Imgui::Instance()->Init(window);
ImGui::EnsureContextOnThisThread();
NewWorld();
AddUpdaterFunc(Interval{ Nz::Time::Milliseconds(16) }, [&](Nz::Time elapsed) {
if (!window.IsOpen())
return;
window.ProcessEvents();
Nz::RenderFrame frame = m_windowSwapchain->AcquireFrame();
if (!frame)
return;
Nz::Imgui::Instance()->Update(window, elapsed.AsSeconds());
Nz::Imgui::Instance()->Render(m_windowSwapchain.get(), frame);
frame.Present();
});
}
Nz::EnttWorld* EditorBaseApplication::GetCurrentWorld()
{
return m_world;
}
entt::handle EditorBaseApplication::CreateEntity()
{
if (m_world == nullptr)
return {};
entt::handle entity = m_world->CreateEntity();
entity.emplace<Nz::NodeComponent>();
OnEntityCreated(entity);
OnEntitySelected(entity);
return entity;
}
void EditorBaseApplication::NewWorld()
{
auto& ecs = GetComponent<Nz::AppEntitySystemComponent>();
m_world = &ecs.AddWorld<Nz::EnttWorld>();
}
}