diff --git a/headers/enduro2d/utils/json_utils.hpp b/headers/enduro2d/utils/json_utils.hpp index e694124e..842c98e5 100644 --- a/headers/enduro2d/utils/json_utils.hpp +++ b/headers/enduro2d/utils/json_utils.hpp @@ -134,6 +134,58 @@ namespace e2d::json_utils try_parse_value(const rapidjson::Value& root, unit& v) noexcept { return try_parse_value(root, v.value); } + + template < typename T > + bool try_parse_value(const rapidjson::Value& root, trs2& t) noexcept { + trs2 tt = trs2::identity(); + + if ( root.HasMember("translation") ) { + if ( !try_parse_value(root["translation"], tt.translation) ) { + return false; + } + } + + if ( root.HasMember("rotation") ) { + if ( !try_parse_value(root["rotation"], tt.rotation) ) { + return false; + } + } + + if ( root.HasMember("scale") ) { + if ( !try_parse_value(root["scale"], tt.scale) ) { + return false; + } + } + + t = tt; + return true; + } + + template < typename T > + bool try_parse_value(const rapidjson::Value& root, trs3& t) noexcept { + trs3 tt = trs3::identity(); + + if ( root.HasMember("translation") ) { + if ( !try_parse_value(root["translation"], tt.translation) ) { + return false; + } + } + + if ( root.HasMember("rotation") ) { + if ( !try_parse_value(root["rotation"], tt.rotation) ) { + return false; + } + } + + if ( root.HasMember("scale") ) { + if ( !try_parse_value(root["scale"], tt.scale) ) { + return false; + } + } + + t = tt; + return true; + } } namespace e2d::json_utils diff --git a/sources/enduro2d/utils/json_utils.cpp b/sources/enduro2d/utils/json_utils.cpp index 2fe767b6..d3803881 100644 --- a/sources/enduro2d/utils/json_utils.cpp +++ b/sources/enduro2d/utils/json_utils.cpp @@ -192,6 +192,24 @@ namespace } }] }, + "t2" : { + "type" : "object", + "additionalProperties" : false, + "properties" : { + "translation" : { "$ref": "#/common_definitions/v2" }, + "rotation" : { "type" : "number" }, + "scale" : { "$ref": "#/common_definitions/v2" } + } + }, + "t3" : { + "type" : "object", + "additionalProperties" : false, + "properties" : { + "translation" : { "$ref": "#/common_definitions/v3" }, + "rotation" : { "$ref": "#/common_definitions/q4" }, + "scale" : { "$ref": "#/common_definitions/v3" } + } + }, "color" : { "anyOf" : [{ "type" : "number" diff --git a/untests/sources/untests_utils/json_utils.cpp b/untests/sources/untests_utils/json_utils.cpp index fa5790ce..563e70b5 100644 --- a/untests/sources/untests_utils/json_utils.cpp +++ b/untests/sources/untests_utils/json_utils.cpp @@ -62,6 +62,18 @@ namespace "b3_4" : [1,2,3,4,5,6], "b3_5" : [1,2,3,4,5,6,7], + "t2" : { + "translation" : [1,2], + "rotation" : 3, + "scale" : [4,5] + }, + + "t3" : { + "translation" : [1,2,3], + "rotation" : [4,5,6,7], + "scale" : [8,9,10] + }, + "c0" : 0.5, "c1" : { "r" : 0.1, "b" : 0.2 }, "c2" : { "r" : 0.1, "g" : 0.2, "b" : 0.3, "a" : 0.4 }, @@ -189,6 +201,15 @@ TEST_CASE("json_utils") { REQUIRE_FALSE(json_utils::try_parse_value(doc["s2"], s3)); REQUIRE_FALSE(json_utils::try_parse_value(doc["s2"], s4)); } + { + t2f t2; + REQUIRE(json_utils::try_parse_value(doc["t2"], t2)); + REQUIRE(t2 == make_trs2(v2f(1,2), radf(3), v2f(4,5))); + + t3f t3; + REQUIRE(json_utils::try_parse_value(doc["t3"], t3)); + REQUIRE(t3 == make_trs3(v3f(1,2,3), q4f(4,5,6,7), v3f(8,9,10))); + } { color c0, c1, c2; REQUIRE(json_utils::try_parse_value(doc["c0"], c0));