From 8c28cab78841d4565e2d358fc10bb4a2071ebed0 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 24 Apr 2020 05:28:09 +0700 Subject: [PATCH] mod children list of subprefabs --- sources/enduro2d/high/assets/prefab_asset.cpp | 21 +++---- untests/bin/library/prefab_child.json | 35 ++++++++++- untests/sources/untests_high/prefab.cpp | 60 +++++++++++++++++-- 3 files changed, 99 insertions(+), 17 deletions(-) diff --git a/sources/enduro2d/high/assets/prefab_asset.cpp b/sources/enduro2d/high/assets/prefab_asset.cpp index 3270616c..cb564bc8 100644 --- a/sources/enduro2d/high/assets/prefab_asset.cpp +++ b/sources/enduro2d/high/assets/prefab_asset.cpp @@ -117,6 +117,7 @@ namespace const bool success = the().validate_json( component_root->name.GetString(), component_root->value); + if ( !success ) { throw prefab_asset_loading_exception(); } @@ -125,10 +126,12 @@ namespace factory_loader<>::collect_context ctx( str(parent_address), component_root->value); + const bool success = the().collect_dependencies( component_root->name.GetString(), dependencies, ctx); + if ( !success ) { throw prefab_asset_loading_exception(); } @@ -170,11 +173,7 @@ namespace const asset_group& dependencies) { if ( root.HasMember("uuid") ) { - str uuid = content.uuid(); - if ( !json_utils::try_parse_value(root["uuid"], uuid) ) { - the().error("PREFAB: Incorrect formatting of 'uuid' property"); - throw prefab_asset_loading_exception(); - } + str uuid = root["uuid"].GetString(); content.set_uuid(std::move(uuid)); } @@ -196,20 +195,18 @@ namespace if ( root.HasMember("children") ) { const rapidjson::Value& children_root = root["children"]; - vector children; - children.reserve(children_root.Size()); - for ( rapidjson::SizeType i = 0; i < children_root.Size(); ++i ) { + const rapidjson::Value& child_root = children_root[i]; + prefab child; parse_prefab_inplace( child, parent_address, - children_root[i], + child_root, dependencies); - children.push_back(std::move(child)); - } - content.set_children(std::move(children)); + content.children().push_back(std::move(child)); + } } if ( root.HasMember("mod_children") ) { diff --git a/untests/bin/library/prefab_child.json b/untests/bin/library/prefab_child.json index 6972a937..22ea6909 100644 --- a/untests/bin/library/prefab_child.json +++ b/untests/bin/library/prefab_child.json @@ -13,19 +13,52 @@ "offset" : [8,4] } }, + "children" : [{ + "uuid" : "C07CDC21-8D1A-45E5-9321-AC7B6FADA847", + "components" : { + "named" : { + "name" : "child(3)" + } + } + }], "mod_children" : [{ "uuid" : "4A93547E-4635-4C2F-9C59-3546E11B1722", "components" : { "widget" : { "size" : [10,10] } - } + }, + "children" : [{ + "uuid" : "EA1F7728-8061-495E-9E8A-280C5E2979B3", + "prefab" : "prefab_root.json", + "components" : { + "named" : { + "name" : "subchild(1)" + } + } + }] }, { "uuid" : "58063213-9FC1-457C-B773-B826BE1BE6D7", "components" : { "widget" : { "size" : [20,20] } + }, + "children" : [{ + "uuid" : "4DDDD08D-F7B9-4588-8597-3E38051AC433", + "prefab" : "prefab_root.json", + "components" : { + "named" : { + "name" : "subchild(2)" + } + } + }] + }, { + "uuid" : "C07CDC21-8D1A-45E5-9321-AC7B6FADA847", + "components" : { + "widget" : { + "size" : [30,30] + } } }] } diff --git a/untests/sources/untests_high/prefab.cpp b/untests/sources/untests_high/prefab.cpp index 2987115f..0289090d 100644 --- a/untests/sources/untests_high/prefab.cpp +++ b/untests/sources/untests_high/prefab.cpp @@ -74,6 +74,22 @@ TEST_CASE("prefab"){ REQUIRE(child2_go.component()); REQUIRE(child2_go.component()->name() == "child(2)"); + + node_iptr child1_node = child1_go.component() + ? child1_go.component()->node() + : node_iptr(); + + node_iptr child2_node = child2_go.component() + ? child2_go.component()->node() + : node_iptr(); + + REQUIRE(child1_node); + REQUIRE(child1_node->owner() == child1_go); + REQUIRE(child1_node->child_count() == 0u); + + REQUIRE(child2_node); + REQUIRE(child2_node->owner() == child2_go); + REQUIRE(child2_node->child_count() == 0u); } } @@ -84,9 +100,10 @@ TEST_CASE("prefab"){ const prefab& prefab_child = prefab_child_res->content(); REQUIRE(prefab_child.uuid().empty()); - REQUIRE(prefab_child.children().size() == 2u); + REQUIRE(prefab_child.children().size() == 3u); REQUIRE(prefab_child.children()[0].uuid() == "4A93547E-4635-4C2F-9C59-3546E11B1722"); REQUIRE(prefab_child.children()[1].uuid() == "58063213-9FC1-457C-B773-B826BE1BE6D7"); + REQUIRE(prefab_child.children()[2].uuid() == "C07CDC21-8D1A-45E5-9321-AC7B6FADA847"); auto go = the().instantiate(prefab_child); @@ -114,29 +131,64 @@ TEST_CASE("prefab"){ REQUIRE(go_node); REQUIRE(go_node->owner() == go); - REQUIRE(go_node->child_count() == 2u); + REQUIRE(go_node->child_count() == 3u); node_iptr child1 = go_node->child_at(0u); node_iptr child2 = go_node->child_at(1u); + node_iptr child3 = go_node->child_at(2u); REQUIRE(child1); REQUIRE(child2); + REQUIRE(child3); gobject child1_go = child1->owner(); gobject child2_go = child2->owner(); + gobject child3_go = child3->owner(); REQUIRE(child1_go); REQUIRE(child2_go); + REQUIRE(child3_go); REQUIRE(child1_go.component()); - REQUIRE(child1_go.component()->name() == "child(1)"); - REQUIRE(child1_go.component()); + REQUIRE(child1_go.component()->name() == "child(1)"); REQUIRE(child1_go.component()->size() == v2f(10.f, 10.f)); REQUIRE(child2_go.component()); + REQUIRE(child2_go.component()); REQUIRE(child2_go.component()->name() == "child(2)"); REQUIRE(child2_go.component()->size() == v2f(20.f, 20.f)); + + REQUIRE(child3_go.component()); + REQUIRE(child3_go.component()); + REQUIRE(child3_go.component()->name() == "child(3)"); + REQUIRE(child3_go.component()->size() == v2f(30.f, 30.f)); + + node_iptr child1_node = child1_go.component() + ? child1_go.component()->node() + : node_iptr(); + + node_iptr child2_node = child2_go.component() + ? child2_go.component()->node() + : node_iptr(); + + node_iptr child3_node = child3_go.component() + ? child3_go.component()->node() + : node_iptr(); + + REQUIRE(child1_node); + REQUIRE(child1_node->owner() == child1_go); + REQUIRE(child1_node->child_count() == 1u); + REQUIRE(child1_node->first_child()->child_count() == 2u); + + REQUIRE(child2_node); + REQUIRE(child2_node->owner() == child2_go); + REQUIRE(child2_node->child_count() == 1u); + REQUIRE(child2_node->first_child()->child_count() == 2u); + + REQUIRE(child3_node); + REQUIRE(child3_node->owner() == child3_go); + REQUIRE(child3_node->child_count() == 0u); } } }