add basic core

This commit is contained in:
SweetId
2023-09-23 19:20:32 -04:00
parent 5c4c23b971
commit d1f132b07e
22 changed files with 824 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
#pragma once
#include <Nazara/Core/ResourceManager.hpp>
#include <string>
namespace Nz
{
class Asset
{
public:
virtual bool IsExtensionSupported(std::string_view ext) = 0;
};
template<typename TResourceManager>
class TAsset
{
public:
virtual bool IsExtensionSupported(std::string_view ext) { return m_resourceManager. }
protected:
TResourceManager m_resourceManager;
};
}

View File

@@ -0,0 +1,16 @@
#pragma once
#ifndef NAZARAEDITOR_CORE_CONFIG_HPP
#define NAZARAEDITOR_CORE_CONFIG_HPP
#if !defined(NAZARAEDITOR_STATIC)
#ifdef NAZARAEDITOR_CORE_BUILD
#define NAZARAEDITOR_CORE_API NAZARA_EXPORT
#else
#define NAZARAEDITOR_CORE_API NAZARA_IMPORT
#endif
#else
#define NAZARAEDITOR_CORE_API
#endif
#endif // NAZARAEDITOR_CORE_CONFIG_HPP

View File

@@ -0,0 +1,25 @@
#pragma once
#include <NazaraEditor/Core/Config.hpp>
#include <Nazara/Core/ModuleBase.hpp>
#include <NazaraImgui/NazaraImgui.hpp>
namespace Nz
{
class NAZARAEDITOR_CORE_API EditorCore : public Nz::ModuleBase<EditorCore>
{
friend ModuleBase;
public:
using Dependencies = TypeList<Imgui>;
struct Config {};
EditorCore(Config config);
~EditorCore();
private:
static EditorCore* s_instance;
};
}

View File

@@ -0,0 +1,144 @@
#pragma once
#include <NazaraEditor/Core/Reflection/Reflection.hpp>
#include <NazaraEditor/Core/Reflection/Math.hpp>
#include <Nazara/Core.hpp>
#include <Nazara/Utility.hpp>
#include <array>
#include <vector>
#include <string>
#include <imgui.h>
namespace Nz
{
template <>
class TypeReflect<Color>
{
public:
template <typename TPropertyEnumerator>
static void Reflect(TPropertyEnumerator& p, Color& obj)
{
p.AddProperty(obj.r, "R", "Red (0-255)");
p.AddProperty(obj.g, "G", "Green (0-255)");
p.AddProperty(obj.b, "B", "Blue (0-255)");
p.AddProperty(obj.a, "A", "Alpha (0-255)");
}
};
namespace EditorImgui
{
inline bool Begin(Color& obj, const std::string& name, const std::string& tooltip)
{
// your IMGUI Code
float values[] = { obj.r, obj.g, obj.b, obj.a };
if (ImGui::ColorEdit4(name.c_str(), values, ImGuiColorEditFlags_PickerHueWheel))
{
obj = Color(values[0], values[1], values[2], values[3]);
}
return false;
}
inline void End(Color&) {}
}
// Wrapper for array values
template <typename T>
struct ArrayEntry
{
T& value;
};
template <typename T>
class TypeReflect<ArrayEntry<T>>
{
public:
template <typename TPropertyEnumerator>
static void Reflect(TPropertyEnumerator& p, ArrayEntry<T>& obj)
{
p.AddProperty(p.value, "", "");
}
};
template <typename T, size_t Size>
class TypeReflect<std::array<T, Size>>
{
public:
template <typename TPropertyEnumerator>
static void Reflect(TPropertyEnumerator& p, std::array<T, Size>& obj)
{
for (size_t i = 0; i < Size; ++i)
{
p.AddProperty(ArrayEntry{ obj[i] }, "", "");
}
}
};
namespace EditorImgui
{
template <typename T, size_t Size>
static bool Begin(ArrayEntry<T>& obj, const std::string& name, const std::string& tooltip)
{
ImGui::BeginGroup();
}
template <typename T, size_t Size>
static void End(ArrayEntry<T>&) {
ImGui::EndGroup();
}
template <typename T, size_t Size>
static bool Begin(std::array<T, Size>& obj, const std::string& name, const std::string& tooltip)
{
return ImGui::TreeNode(name.c_str());
}
template <typename T, size_t Size>
static void End(std::array<T, Size>&) {
ImGui::TreePop();
}
}
template <>
class TypeReflect<entt::handle>
{
public:
template <typename TPropertyEnumerator>
static void Reflect(TPropertyEnumerator& p, entt::handle& obj)
{
for (auto&& curr : obj.storage())
{
if (auto& storage = curr.second; storage.contains(obj))
{
entt::id_type id = curr.first;
if (auto reflect = entt::resolve(id).func(entt::hashed_string("Reflect")); reflect)
reflect.invoke({}, p, obj);
}
}
}
};
namespace EditorImgui
{
static bool Begin(entt::handle& obj, const std::string& name, const std::string& tooltip)
{
std::string n = "Entity " + name;
return ImGui::TreeNode(n.c_str());
}
static void End(entt::handle&) {
ImGui::TreePop();
}
}
template <typename TPropertyEnumerator, typename TComponent>
inline void ReflectComponent(TPropertyEnumerator& p, entt::handle& obj)
{
TComponent* component = obj.try_get<TComponent>();
if (component != nullptr)
p.AddProperty(*component, "", "");
}
}

View File

@@ -0,0 +1,47 @@
#pragma once
#include <functional>
#include <string>
namespace Nz
{
template <typename TPropertyDetail>
struct EditorPropertyInspector
{
template <typename T>
void AddProperty(T& prop, const std::string& name, const std::string& tooltip);
/*template <typename T, typename U, typename V>
void AddProperty(T& parent, V(U::* Getter)() const, void(U::*Setter(const V&))(), const std::string& name, const std::string& tooltip)
{
U val = parent->Getter();
AddProperty(val, name, tooltip);
parent->Setter(val);
}*/
template <typename TGetter, typename TSetter>
void AddProperty(TGetter Getter, TSetter Setter, const std::string& name, const std::string& tooltip)
{
auto val = Getter();
AddProperty(val, name, tooltip);
Setter(val);
}
};
struct EditorRenderer
{
template <typename T>
static bool Begin(T& prop, const std::string& name, const std::string& tooltip)
{
return EditorImgui::Begin(prop, name, tooltip);
}
template <typename T>
static void End(T& prop)
{
EditorImgui::End(prop);
}
};
}
#include <NazaraEditor/Core/Reflection/Editor.inl>

View File

@@ -0,0 +1,15 @@
namespace Nz
{
template <typename TPropertyDetail>
template <typename T> void EditorPropertyInspector<TPropertyDetail>::AddProperty(T& prop, const std::string& name, const std::string& tooltip)
{
if (TPropertyDetail::Begin(prop, name, tooltip))
{
TypeReflect<T>::Reflect(*this, prop);
TPropertyDetail::End(prop);
}
}
}

View File

@@ -0,0 +1,52 @@
#pragma once
#include <NazaraEditor/Core/Reflection/Reflection.hpp>
#include <Nazara/Graphics.hpp>
#include <imgui.h>
namespace Nz
{
template <>
class TypeReflect<LightComponent::LightEntry>
{
public:
template <typename TPropertyEnumerator>
static void Reflect(TPropertyEnumerator& p, LightComponent::LightEntry& obj)
{
}
};
template <>
class TypeReflect<LightComponent>
{
public:
template <typename TPropertyEnumerator>
static void Reflect(TPropertyEnumerator& p, LightComponent& obj)
{
p.AddProperty([&obj]() { return obj.IsVisible(); }, [&obj](bool v) { obj.Show(v); }, "Is Visible", "Toggles lights visibility");
//p.AddProperty(obj.GetLights(), "Lights", "");
}
};
namespace EditorImgui
{
inline bool Begin(Nz::LightComponent::LightEntry& obj, const std::string& name, const std::string& tooltip)
{
return false;
}
inline void End(Nz::LightComponent::LightEntry&) {
}
inline bool Begin(Nz::LightComponent& obj, const std::string& name, const std::string& tooltip)
{
return ImGui::TreeNode("LightComponent");
}
inline void End(Nz::LightComponent&) {
ImGui::TreePop();
}
}
}

View File

@@ -0,0 +1,78 @@
#pragma once
#include <NazaraEditor/Core/Reflection/Reflection.hpp>
#include <Nazara/Math.hpp>
#include <imgui.h>
namespace Nz
{
namespace EditorImgui
{
inline bool Begin(bool& obj, const std::string& name, const std::string& /*tooltip*/)
{
ImGui::Checkbox(name.c_str(), &obj);
return false;
}
inline void End(bool&) {}
#define IMGUI_BASIC_INTEGER(Type) \
inline bool Begin(Type& obj, const std::string& name, const std::string& /*tooltip*/) \
{ \
int value = static_cast<int>(obj); \
if (ImGui::DragInt(name.c_str(), &value, 1.f, std::numeric_limits<Type>::min(), std::numeric_limits<Type>::max())) \
obj = static_cast<Type>(value); \
return false; \
} \
inline void End(Type&) {}
IMGUI_BASIC_INTEGER(Nz::Int8);
IMGUI_BASIC_INTEGER(Nz::Int16)
IMGUI_BASIC_INTEGER(Nz::Int32);
IMGUI_BASIC_INTEGER(Nz::Int64);
IMGUI_BASIC_INTEGER(Nz::UInt8);
IMGUI_BASIC_INTEGER(Nz::UInt16);
IMGUI_BASIC_INTEGER(Nz::UInt32);
IMGUI_BASIC_INTEGER(Nz::UInt64);
#define IMGUI_BASIC_FLOAT(Type) \
inline bool Begin(Type& obj, const std::string& name, const std::string& /*tooltip*/) \
{ \
float value = (float)obj; \
if (ImGui::DragFloat(name.c_str(), &value, 1.f, std::numeric_limits<Type>::min(), std::numeric_limits<Type>::max())) \
obj = (Type)value; \
return false; \
} \
inline void End(Type&) {}
IMGUI_BASIC_FLOAT(float);
IMGUI_BASIC_FLOAT(double);
inline bool Begin(Nz::Vector3f& obj, const std::string& name, const std::string& /*tooltip*/)
{
float value[] = { obj.x, obj.y, obj.z };
if (ImGui::DragFloat3(name.c_str(), value, 1.f, std::numeric_limits<float>::min(), std::numeric_limits<float>::max()))
obj = Nz::Vector3f(value[0], value[1], value[2]);
return false;
}
inline void End(Nz::Vector3f&) {}
inline bool Begin(Nz::Quaternionf& obj, const std::string& name, const std::string& /*tooltip*/)
{
auto euler = obj.ToEulerAngles();
float value[] = { euler.pitch.ToDegrees(), euler.yaw.ToDegrees(), euler.roll.ToDegrees() };
if (ImGui::DragFloat3(name.c_str(), value, 1.f, std::numeric_limits<float>::min(), std::numeric_limits<float>::max()))
{
obj = EulerAnglesf(value[0], value[1], value[2]).ToQuaternion();
}
return false;
}
inline void End(Nz::Quaternionf&) {}
}
}

View File

@@ -0,0 +1,12 @@
#pragma once
namespace Nz
{
template <typename T>
class TypeReflect
{
public:
template <typename TEnumerator>
static void Reflect(TEnumerator&, T&) { } // default specialization
};
}

View File

@@ -0,0 +1,36 @@
#pragma once
#include <NazaraEditor/Core/Reflection/Reflection.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <imgui.h>
namespace Nz
{
template <>
class TypeReflect<NodeComponent>
{
public:
template <typename TPropertyEnumerator>
static void Reflect(TPropertyEnumerator& p, NodeComponent& obj)
{
p.AddProperty([&obj]() { return obj.GetPosition(); }, [&obj](const Nz::Vector3f& v) { return obj.SetPosition(v); }, "Position", "Position of the node");
p.AddProperty([&obj]() { return obj.GetRotation(); }, [&obj](const Nz::Quaternionf& v) { return obj.SetRotation(v); }, "Rotation", "Rotation of the node");
p.AddProperty([&obj]() { return obj.GetScale(); }, [&obj](const Nz::Vector3f& v) { return obj.SetScale(v); }, "Scale", "Scale of the node");
}
};
namespace EditorImgui
{
inline bool Begin(Nz::NodeComponent& obj, const std::string& name, const std::string& tooltip)
{
return ImGui::TreeNode("NodeComponent");
}
inline void End(Nz::NodeComponent&) {
ImGui::TreePop();
}
}
}

View File

View File

View File

@@ -0,0 +1,54 @@
#pragma once
#include <NazaraUtils/Prerequisites.hpp>
#include <NazaraEditor/Core/Config.hpp>
#include <NazaraImgui/NazaraImgui.hpp>
namespace Nz
{
using ActionCallback = std::function<void(void)>;
class NAZARAEDITOR_CORE_API EditorWindow
: private Nz::ImguiHandler
{
public:
EditorWindow(const std::string& name = "");
~EditorWindow();
EditorWindow(const EditorWindow&) = delete;
EditorWindow& operator=(const EditorWindow&) = delete;
virtual void OnRenderImgui() override;
void AddMenuAction(const std::string& path, const std::string& shortcut, ActionCallback callback);
void AddMenuSeparator(const std::string& path);
protected:
virtual void OnEditorGUI() {};
private:
void DrawMenus();
std::string m_windowName;
struct MenuAction
{
std::string label;
std::string shortcut;
ActionCallback callback;
};
struct MenuSeparator
{};
struct MenuList
{
std::string label;
std::vector<std::variant<MenuAction, MenuSeparator, MenuList>> entries;
};
MenuList m_root;
MenuList& GetOrCreateMenuHierarchy(const std::vector<std::string_view>& hierarchy);
};
}