feat: add new fancy reflection
This commit is contained in:
@@ -1,32 +1,13 @@
|
||||
#include <destrum/Components/Physics/BoxCollider.h>
|
||||
|
||||
#include <destrum/Graphics/DebugDraw.h>
|
||||
#include <destrum/Util/ImGuiUtils.h>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <destrum/Serialization/Reflect.h>
|
||||
|
||||
void BoxCollider::DrawDebugLines(const glm::vec4& color) const {
|
||||
DebugDraw::Box(GetWorldCenter(), GetWorldRotation(), m_HalfExtents, color);
|
||||
}
|
||||
|
||||
nlohmann::json BoxCollider::Serialize() const {
|
||||
auto data = SerializeCollider();
|
||||
data["halfExtents"] = m_HalfExtents;
|
||||
return data;
|
||||
}
|
||||
|
||||
void BoxCollider::Deserialize(const nlohmann::json& data) {
|
||||
DeserializeCollider(data);
|
||||
if (data.contains("halfExtents")) {
|
||||
SetHalfExtents(data.at("halfExtents").get<glm::vec3>());
|
||||
}
|
||||
}
|
||||
|
||||
void BoxCollider::ImGuiInspector() {
|
||||
Collider::ImGuiInspector();
|
||||
|
||||
glm::vec3 halfExtents = m_HalfExtents;
|
||||
if (ImGuiUtils::DrawVec3Control("Half extents", halfExtents)) {
|
||||
SetHalfExtents(halfExtents);
|
||||
}
|
||||
void BoxCollider::Reflect(FieldVisitor& v) {
|
||||
Collider::Reflect(v);
|
||||
v.Property("halfExtents", m_HalfExtents, [this](const glm::vec3& halfExtents) { SetHalfExtents(halfExtents); }, {.min = 0.0001});
|
||||
}
|
||||
|
||||
@@ -2,22 +2,14 @@
|
||||
|
||||
#include <destrum/Graphics/DebugDraw.h>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <destrum/Serialization/Reflect.h>
|
||||
|
||||
void CapsuleCollider::DrawDebugLines(const glm::vec4& color) const {
|
||||
DebugDraw::Capsule(GetWorldCenter(), GetWorldRotation(), m_Radius, m_Height, color);
|
||||
}
|
||||
|
||||
void CapsuleCollider::ImGuiInspector() {
|
||||
Collider::ImGuiInspector();
|
||||
|
||||
float radius = m_Radius;
|
||||
if (ImGui::DragFloat("Radius", &radius, 0.05f, 0.0001f)) {
|
||||
SetRadius(radius);
|
||||
}
|
||||
|
||||
float height = m_Height;
|
||||
if (ImGui::DragFloat("Height", &height, 0.05f, 0.0001f)) {
|
||||
SetHeight(height);
|
||||
}
|
||||
void CapsuleCollider::Reflect(FieldVisitor& v) {
|
||||
Collider::Reflect(v);
|
||||
v.Property("radius", m_Radius, [this](float radius) { SetRadius(radius); }, {.speed = 0.05f, .min = 0.0001});
|
||||
v.Property("height", m_Height, [this](float height) { SetHeight(height); }, {.speed = 0.05f, .min = 0.0001});
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
#include <destrum/Components/Physics/Collider.h>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <destrum/ObjectModel/Transform.h>
|
||||
#include <destrum/Util/ImGuiUtils.h>
|
||||
#include <destrum/Serialization/Reflect.h>
|
||||
|
||||
glm::vec3 Collider::GetWorldCenter() const {
|
||||
Transform& transform = GetTransform();
|
||||
@@ -14,14 +12,7 @@ glm::quat Collider::GetWorldRotation() const {
|
||||
return GetTransform().GetWorldRotation();
|
||||
}
|
||||
|
||||
void Collider::ImGuiInspector() {
|
||||
bool isTrigger = m_IsTrigger;
|
||||
if (ImGui::Checkbox("Is trigger", &isTrigger)) {
|
||||
SetTrigger(isTrigger);
|
||||
}
|
||||
|
||||
glm::vec3 centerOffset = m_CenterOffset;
|
||||
if (ImGuiUtils::DrawVec3Control("Center offset", centerOffset)) {
|
||||
SetCenterOffset(centerOffset);
|
||||
}
|
||||
void Collider::Reflect(FieldVisitor& v) {
|
||||
v.Property("isTrigger", m_IsTrigger, [this](bool trigger) { SetTrigger(trigger); });
|
||||
v.Property("centerOffset", m_CenterOffset, [this](const glm::vec3& offset) { SetCenterOffset(offset); });
|
||||
}
|
||||
|
||||
@@ -1,29 +1,13 @@
|
||||
#include <destrum/Components/Physics/ConvexHullCollider.h>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <destrum/Serialization/Reflect.h>
|
||||
|
||||
nlohmann::json ConvexHullCollider::Serialize() const {
|
||||
auto data = SerializeCollider();
|
||||
data["maxVertices"] = m_MaxVertices;
|
||||
return data;
|
||||
}
|
||||
|
||||
void ConvexHullCollider::Deserialize(const nlohmann::json& data) {
|
||||
DeserializeCollider(data);
|
||||
if (data.contains("maxVertices")) {
|
||||
SetMaxVertices(data.at("maxVertices").get<int>());
|
||||
}
|
||||
void ConvexHullCollider::Reflect(FieldVisitor& v) {
|
||||
Collider::Reflect(v);
|
||||
v.Property("maxVertices", m_MaxVertices, [this](int maxVertices) { SetMaxVertices(maxVertices); },
|
||||
{.speed = 1.0f, .min = MinVertices, .max = MaxVertices});
|
||||
}
|
||||
|
||||
void ConvexHullCollider::DrawDebugLines(const glm::vec4& color) const {
|
||||
DrawConvexHull(m_MaxVertices, color);
|
||||
}
|
||||
|
||||
void ConvexHullCollider::ImGuiInspector() {
|
||||
MeshShapeCollider::ImGuiInspector();
|
||||
|
||||
int maxVertices = m_MaxVertices;
|
||||
if (ImGui::SliderInt("Max vertices", &maxVertices, MinVertices, MaxVertices)) {
|
||||
SetMaxVertices(maxVertices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,22 +2,14 @@
|
||||
|
||||
#include <destrum/Graphics/DebugDraw.h>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <destrum/Serialization/Reflect.h>
|
||||
|
||||
void CylinderCollider::DrawDebugLines(const glm::vec4& color) const {
|
||||
DebugDraw::Cylinder(GetWorldCenter(), GetWorldRotation(), m_Radius, m_Height, color);
|
||||
}
|
||||
|
||||
void CylinderCollider::ImGuiInspector() {
|
||||
Collider::ImGuiInspector();
|
||||
|
||||
float radius = m_Radius;
|
||||
if (ImGui::DragFloat("Radius", &radius, 0.05f, 0.0001f)) {
|
||||
SetRadius(radius);
|
||||
}
|
||||
|
||||
float height = m_Height;
|
||||
if (ImGui::DragFloat("Height", &height, 0.05f, 0.0001f)) {
|
||||
SetHeight(height);
|
||||
}
|
||||
void CylinderCollider::Reflect(FieldVisitor& v) {
|
||||
Collider::Reflect(v);
|
||||
v.Property("radius", m_Radius, [this](float radius) { SetRadius(radius); }, {.speed = 0.05f, .min = 0.0001});
|
||||
v.Property("height", m_Height, [this](float height) { SetHeight(height); }, {.speed = 0.05f, .min = 0.0001});
|
||||
}
|
||||
|
||||
@@ -3,40 +3,12 @@
|
||||
#include <destrum/Physics/PhysicsWorld.h>
|
||||
#include <destrum/ObjectModel/GameObject.h>
|
||||
#include <destrum/Scene/Scene.h>
|
||||
#include <destrum/Serialization/Reflect.h>
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace {
|
||||
[[nodiscard]] const char* RigidbodyTypeName(RigidbodyType type) {
|
||||
switch (type) {
|
||||
case RigidbodyType::Static:
|
||||
return "Static";
|
||||
case RigidbodyType::Kinematic:
|
||||
return "Kinematic";
|
||||
case RigidbodyType::Dynamic:
|
||||
default:
|
||||
return "Dynamic";
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] RigidbodyType ParseRigidbodyType(const nlohmann::json& value) {
|
||||
if (value.is_string()) {
|
||||
const std::string type = value.get<std::string>();
|
||||
if (type == "Static") return RigidbodyType::Static;
|
||||
if (type == "Kinematic") return RigidbodyType::Kinematic;
|
||||
return RigidbodyType::Dynamic;
|
||||
}
|
||||
|
||||
const int type = value.get<int>();
|
||||
switch (type) {
|
||||
case 0: return RigidbodyType::Static;
|
||||
case 2: return RigidbodyType::Kinematic;
|
||||
case 1:
|
||||
default: return RigidbodyType::Dynamic;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Rigidbody::~Rigidbody() {
|
||||
if (m_World != nullptr) {
|
||||
m_World->UnregisterRigidbody(*this);
|
||||
@@ -110,78 +82,21 @@ glm::vec3 Rigidbody::GetLinearVelocity() const {
|
||||
return m_World->GetLinearVelocity(m_Body);
|
||||
}
|
||||
|
||||
nlohmann::json Rigidbody::Serialize() const {
|
||||
return {
|
||||
{"type", RigidbodyTypeName(m_Type)},
|
||||
{"mass", m_Mass},
|
||||
{"friction", m_Friction},
|
||||
{"restitution", m_Restitution},
|
||||
{"useGravity", m_UseGravity},
|
||||
{"allowSleep", m_AllowSleep},
|
||||
{"freezeRotation", m_FreezeRotation}
|
||||
};
|
||||
}
|
||||
void Rigidbody::Reflect(FieldVisitor& v) {
|
||||
static constexpr std::array<const char*, 3> TypeNames{"Static", "Dynamic", "Kinematic"};
|
||||
|
||||
void Rigidbody::Deserialize(const nlohmann::json& data) {
|
||||
if (data.contains("type")) {
|
||||
m_Type = ParseRigidbodyType(data.at("type"));
|
||||
}
|
||||
if (data.contains("mass")) {
|
||||
SetMass(data.at("mass").get<float>());
|
||||
}
|
||||
if (data.contains("friction")) {
|
||||
SetFriction(data.at("friction").get<float>());
|
||||
}
|
||||
if (data.contains("restitution")) {
|
||||
SetRestitution(data.at("restitution").get<float>());
|
||||
}
|
||||
if (data.contains("useGravity")) {
|
||||
m_UseGravity = data.at("useGravity").get<bool>();
|
||||
}
|
||||
if (data.contains("allowSleep")) {
|
||||
m_AllowSleep = data.at("allowSleep").get<bool>();
|
||||
}
|
||||
if (data.contains("freezeRotation")) {
|
||||
m_FreezeRotation = data.at("freezeRotation").get<bool>();
|
||||
}
|
||||
v.Property("type", m_Type, [this](RigidbodyType type) { SetType(type); }, {.label = "Body Type", .enumNames = TypeNames});
|
||||
v.Property("mass", m_Mass, [this](float mass) { SetMass(mass); }, {.min = 0.0001});
|
||||
v.Property("friction", m_Friction, [this](float friction) { SetFriction(friction); }, {.speed = 0.01f, .min = 0.0});
|
||||
v.Property("restitution", m_Restitution, [this](float restitution) { SetRestitution(restitution); },
|
||||
{.speed = 0.01f, .min = 0.0, .max = 1.0});
|
||||
v.Property("useGravity", m_UseGravity, [this](bool useGravity) { SetUseGravity(useGravity); });
|
||||
v.Property("allowSleep", m_AllowSleep, [this](bool allowSleep) { SetAllowSleep(allowSleep); });
|
||||
v.Property("freezeRotation", m_FreezeRotation, [this](bool freezeRotation) { SetFreezeRotation(freezeRotation); });
|
||||
}
|
||||
|
||||
void Rigidbody::ImGuiInspector() {
|
||||
static const char* typeNames[] = {"Static", "Dynamic", "Kinematic"};
|
||||
int type = static_cast<int>(m_Type);
|
||||
if (ImGui::Combo("Body type", &type, typeNames, IM_ARRAYSIZE(typeNames))) {
|
||||
SetType(static_cast<RigidbodyType>(type));
|
||||
}
|
||||
|
||||
float mass = m_Mass;
|
||||
if (ImGui::DragFloat("Mass", &mass, 0.1f, 0.0001f)) {
|
||||
SetMass(mass);
|
||||
}
|
||||
|
||||
float friction = m_Friction;
|
||||
if (ImGui::DragFloat("Friction", &friction, 0.01f, 0.0f)) {
|
||||
SetFriction(friction);
|
||||
}
|
||||
|
||||
float restitution = m_Restitution;
|
||||
if (ImGui::SliderFloat("Restitution", &restitution, 0.0f, 1.0f)) {
|
||||
SetRestitution(restitution);
|
||||
}
|
||||
|
||||
bool useGravity = m_UseGravity;
|
||||
if (ImGui::Checkbox("Use gravity", &useGravity)) {
|
||||
SetUseGravity(useGravity);
|
||||
}
|
||||
|
||||
bool allowSleep = m_AllowSleep;
|
||||
if (ImGui::Checkbox("Allow sleep", &allowSleep)) {
|
||||
SetAllowSleep(allowSleep);
|
||||
}
|
||||
|
||||
bool freezeRotation = m_FreezeRotation;
|
||||
if (ImGui::Checkbox("Freeze rotation", &freezeRotation)) {
|
||||
SetFreezeRotation(freezeRotation);
|
||||
}
|
||||
Component::ImGuiInspector();
|
||||
|
||||
if (HasPhysicsBody()) {
|
||||
const glm::vec3 velocity = GetLinearVelocity();
|
||||
|
||||
@@ -2,30 +2,13 @@
|
||||
|
||||
#include <destrum/Graphics/DebugDraw.h>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <destrum/Serialization/Reflect.h>
|
||||
|
||||
void SphereCollider::DrawDebugLines(const glm::vec4& color) const {
|
||||
DebugDraw::Sphere(GetWorldCenter(), m_Radius, color);
|
||||
}
|
||||
|
||||
nlohmann::json SphereCollider::Serialize() const {
|
||||
auto data = SerializeCollider();
|
||||
data["radius"] = m_Radius;
|
||||
return data;
|
||||
}
|
||||
|
||||
void SphereCollider::Deserialize(const nlohmann::json& data) {
|
||||
DeserializeCollider(data);
|
||||
if (data.contains("radius")) {
|
||||
SetRadius(data.at("radius").get<float>());
|
||||
}
|
||||
}
|
||||
|
||||
void SphereCollider::ImGuiInspector() {
|
||||
Collider::ImGuiInspector();
|
||||
|
||||
float radius = m_Radius;
|
||||
if (ImGui::DragFloat("Radius", &radius, 0.05f, 0.0001f)) {
|
||||
SetRadius(radius);
|
||||
}
|
||||
void SphereCollider::Reflect(FieldVisitor& v) {
|
||||
Collider::Reflect(v);
|
||||
v.Property("radius", m_Radius, [this](float radius) { SetRadius(radius); }, {.speed = 0.05f, .min = 0.0001});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user