Browse Source

Component: Add ability to read TransformComponent from JSON

master
Riyyi 2 years ago
parent
commit
c2c5777566
  1. 2
      src/inferno/component/nativescriptcomponent.h
  2. 45
      src/inferno/component/transformcomponent.cpp
  3. 10
      src/inferno/component/transformcomponent.h

2
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() {}

45
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<float>();
value.y = values.at(1).get<float>();
value.z = values.at(2).get<float>();
}
} // namespace glm
void ruc::format::Formatter<glm::vec2>::format(Builder& builder, glm::vec2 value) const
{
return Formatter<std::vector<float>>::format(builder, { value.x, value.y });

10
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<glm::vec2> : Formatter<std::vector<float>> {
void format(Builder& builder, glm::vec2 value) const;

Loading…
Cancel
Save