From c2c577756616bcd8de817f0dcb081cc1ceadda38 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 25 Sep 2022 11:07:37 +0200 Subject: [PATCH] Component: Add ability to read TransformComponent from JSON --- src/inferno/component/nativescriptcomponent.h | 2 +- src/inferno/component/transformcomponent.cpp | 45 +++++++++++++++++++ src/inferno/component/transformcomponent.h | 10 +++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/inferno/component/nativescriptcomponent.h b/src/inferno/component/nativescriptcomponent.h index efa71a8..74ef6de 100644 --- a/src/inferno/component/nativescriptcomponent.h +++ b/src/inferno/component/nativescriptcomponent.h @@ -15,7 +15,7 @@ namespace Inferno { struct NativeScriptComponent { NativeScript* instance { nullptr }; - NativeScript* (*initialize)(); + NativeScript* (*initialize)() { nullptr }; // Dont allow manually setting instance during construction NativeScriptComponent() {} diff --git a/src/inferno/component/transformcomponent.cpp b/src/inferno/component/transformcomponent.cpp index 1147f91..1f12005 100644 --- a/src/inferno/component/transformcomponent.cpp +++ b/src/inferno/component/transformcomponent.cpp @@ -4,8 +4,53 @@ * SPDX-License-Identifier: MIT */ +#include "ruc/format/format.h" +#include "ruc/json/json.h" + #include "inferno/component/transformcomponent.h" +namespace Inferno { + +void fromJson(const ruc::Json& json, TransformComponent& value) +{ + VERIFY(json.type() == ruc::Json::Type::Object); + + if (json.exists("translate")) { + json.at("translate").getTo(value.translate); + } + if (json.exists("rotate")) { + json.at("rotate").getTo(value.rotate); + } + if (json.exists("scale")) { + json.at("scale").getTo(value.scale); + } +} + +} // namespace Inferno + +namespace glm { + +void toJson(ruc::Json& json, const vec3& value) +{ + json = ruc::Json { + { value.x, value.y, value.z }, + }; +} + +void fromJson(const ruc::Json& json, vec3& value) +{ + VERIFY(json.type() == ruc::Json::Type::Array); + + auto& values = json.asArray(); + VERIFY(values.size() == 3, "glm::vec3 expects 3 values, not {}", values.size()); + + value.x = values.at(0).get(); + value.y = values.at(1).get(); + value.z = values.at(2).get(); +} + +} // namespace glm + void ruc::format::Formatter::format(Builder& builder, glm::vec2 value) const { return Formatter>::format(builder, { value.x, value.y }); diff --git a/src/inferno/component/transformcomponent.h b/src/inferno/component/transformcomponent.h index 37d539f..5463608 100644 --- a/src/inferno/component/transformcomponent.h +++ b/src/inferno/component/transformcomponent.h @@ -9,6 +9,7 @@ #include "glm/ext/matrix_float4x4.hpp" // glm::mat4 #include "glm/ext/vector_float3.hpp" // glm::vec3 #include "ruc/format/format.h" +#include "ruc/json/json.h" namespace Inferno { @@ -19,8 +20,17 @@ struct TransformComponent { glm::mat4 transform { 1.0f }; // Identity matrix }; +void fromJson(const ruc::Json& json, TransformComponent& value); + } // namespace Inferno +namespace glm { + +void toJson(ruc::Json& json, const vec3& value); +void fromJson(const ruc::Json& json, vec3& value); + +} // namespace glm + template<> struct ruc::format::Formatter : Formatter> { void format(Builder& builder, glm::vec2 value) const;