mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
add inner and outer texrects to sprite asset and atlas sprite
This commit is contained in:
@@ -30,15 +30,18 @@ namespace e2d
|
|||||||
sprite& assign(const sprite& other);
|
sprite& assign(const sprite& other);
|
||||||
|
|
||||||
sprite& set_pivot(const v2f& pivot) noexcept;
|
sprite& set_pivot(const v2f& pivot) noexcept;
|
||||||
sprite& set_texrect(const b2f& texrect) noexcept;
|
sprite& set_inner_texrect(const b2f& texrect) noexcept;
|
||||||
|
sprite& set_outer_texrect(const b2f& texrect) noexcept;
|
||||||
sprite& set_texture(const texture_asset::ptr& texture) noexcept;
|
sprite& set_texture(const texture_asset::ptr& texture) noexcept;
|
||||||
|
|
||||||
const v2f& pivot() const noexcept;
|
const v2f& pivot() const noexcept;
|
||||||
const b2f& texrect() const noexcept;
|
const b2f& inner_texrect() const noexcept;
|
||||||
|
const b2f& outer_texrect() const noexcept;
|
||||||
const texture_asset::ptr& texture() const noexcept;
|
const texture_asset::ptr& texture() const noexcept;
|
||||||
private:
|
private:
|
||||||
v2f pivot_;
|
v2f pivot_;
|
||||||
b2f texrect_;
|
b2f inner_texrect_;
|
||||||
|
b2f outer_texrect_;
|
||||||
texture_asset::ptr texture_;
|
texture_asset::ptr texture_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,7 @@ namespace
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const char* atlas_asset_schema_source = R"json(
|
const char* atlas_asset_schema_source = R"json({
|
||||||
{
|
|
||||||
"type" : "object",
|
"type" : "object",
|
||||||
"required" : [ "texture" ],
|
"required" : [ "texture" ],
|
||||||
"additionalProperties" : false,
|
"additionalProperties" : false,
|
||||||
@@ -35,14 +34,26 @@ namespace
|
|||||||
"items" : { "$ref": "#/definitions/sprite" }
|
"items" : { "$ref": "#/definitions/sprite" }
|
||||||
},
|
},
|
||||||
"sprite" : {
|
"sprite" : {
|
||||||
"type" : "object",
|
"anyOf" : [{
|
||||||
"required" : [ "name", "pivot", "texrect" ],
|
"type" : "object",
|
||||||
"additionalProperties" : false,
|
"required" : [ "name", "pivot", "texrect" ],
|
||||||
"properties" : {
|
"additionalProperties" : false,
|
||||||
"name" : { "$ref": "#/common_definitions/name" },
|
"properties" : {
|
||||||
"pivot" : { "$ref": "#/common_definitions/v2" },
|
"name" : { "$ref": "#/common_definitions/name" },
|
||||||
"texrect" : { "$ref": "#/common_definitions/b2" }
|
"pivot" : { "$ref": "#/common_definitions/v2" },
|
||||||
}
|
"texrect" : { "$ref": "#/common_definitions/b2" }
|
||||||
|
}
|
||||||
|
},{
|
||||||
|
"type" : "object",
|
||||||
|
"required" : [ "name", "pivot", "inner_texrect", "outer_texrect" ],
|
||||||
|
"additionalProperties" : false,
|
||||||
|
"properties" : {
|
||||||
|
"name" : { "$ref": "#/common_definitions/name" },
|
||||||
|
"pivot" : { "$ref": "#/common_definitions/v2" },
|
||||||
|
"inner_texrect" : { "$ref": "#/common_definitions/b2" },
|
||||||
|
"outer_texrect" : { "$ref": "#/common_definitions/b2" }
|
||||||
|
}
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})json";
|
})json";
|
||||||
@@ -68,7 +79,8 @@ namespace
|
|||||||
struct sprite_desc {
|
struct sprite_desc {
|
||||||
str_hash name;
|
str_hash name;
|
||||||
v2f pivot;
|
v2f pivot;
|
||||||
b2f texrect;
|
b2f inner_texrect;
|
||||||
|
b2f outer_texrect;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool parse_sprites(
|
bool parse_sprites(
|
||||||
@@ -94,10 +106,28 @@ namespace
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
E2D_ASSERT(sprite_json.HasMember("texrect"));
|
E2D_ASSERT(
|
||||||
if ( !json_utils::try_parse_value(sprite_json["texrect"], tsprite_descs[i].texrect) ) {
|
sprite_json.HasMember("texrect") ||
|
||||||
the<debug>().error("ATLAS: Incorrect formatting of 'texrect' property");
|
(sprite_json.HasMember("inner_texrect") && sprite_json.HasMember("outer_texrect")));
|
||||||
return false;
|
|
||||||
|
if ( sprite_json.HasMember("texrect") ) {
|
||||||
|
b2f texrect;
|
||||||
|
if ( !json_utils::try_parse_value(sprite_json["texrect"], texrect) ) {
|
||||||
|
the<debug>().error("ATLAS: Incorrect formatting of 'texrect' property");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
tsprite_descs[i].inner_texrect = texrect;
|
||||||
|
tsprite_descs[i].outer_texrect = texrect;
|
||||||
|
} else {
|
||||||
|
if ( !json_utils::try_parse_value(sprite_json["inner_texrect"], tsprite_descs[i].inner_texrect) ) {
|
||||||
|
the<debug>().error("ATLAS: Incorrect formatting of 'inner_texrect' property");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !json_utils::try_parse_value(sprite_json["outer_texrect"], tsprite_descs[i].outer_texrect) ) {
|
||||||
|
the<debug>().error("ATLAS: Incorrect formatting of 'outer_texrect' property");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +164,8 @@ namespace
|
|||||||
for ( const sprite_desc& desc : sprite_descs ) {
|
for ( const sprite_desc& desc : sprite_descs ) {
|
||||||
sprite spr;
|
sprite spr;
|
||||||
spr.set_pivot(desc.pivot);
|
spr.set_pivot(desc.pivot);
|
||||||
spr.set_texrect(desc.texrect);
|
spr.set_inner_texrect(desc.inner_texrect);
|
||||||
|
spr.set_outer_texrect(desc.outer_texrect);
|
||||||
spr.set_texture(texture);
|
spr.set_texture(texture);
|
||||||
ncontent.insert(std::make_pair(desc.name, sprite_asset::create(std::move(spr))));
|
ncontent.insert(std::make_pair(desc.name, sprite_asset::create(std::move(spr))));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,14 +21,26 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
const char* sprite_asset_schema_source = R"json({
|
const char* sprite_asset_schema_source = R"json({
|
||||||
"type" : "object",
|
"anyOf" : [{
|
||||||
"required" : [ "texture", "pivot", "texrect" ],
|
"type" : "object",
|
||||||
"additionalProperties" : false,
|
"required" : [ "texture", "pivot", "texrect" ],
|
||||||
"properties" : {
|
"additionalProperties" : false,
|
||||||
"texture" : { "$ref": "#/common_definitions/address" },
|
"properties" : {
|
||||||
"pivot" : { "$ref": "#/common_definitions/v2" },
|
"texture" : { "$ref": "#/common_definitions/address" },
|
||||||
"texrect" : { "$ref": "#/common_definitions/b2" }
|
"pivot" : { "$ref": "#/common_definitions/v2" },
|
||||||
}
|
"texrect" : { "$ref": "#/common_definitions/b2" }
|
||||||
|
}
|
||||||
|
},{
|
||||||
|
"type" : "object",
|
||||||
|
"required" : [ "texture", "pivot", "inner_texrect", "outer_texrect" ],
|
||||||
|
"additionalProperties" : false,
|
||||||
|
"properties" : {
|
||||||
|
"texture" : { "$ref": "#/common_definitions/address" },
|
||||||
|
"pivot" : { "$ref": "#/common_definitions/v2" },
|
||||||
|
"inner_texrect" : { "$ref": "#/common_definitions/b2" },
|
||||||
|
"outer_texrect" : { "$ref": "#/common_definitions/b2" }
|
||||||
|
}
|
||||||
|
}]
|
||||||
})json";
|
})json";
|
||||||
|
|
||||||
const rapidjson::SchemaDocument& sprite_asset_schema() {
|
const rapidjson::SchemaDocument& sprite_asset_schema() {
|
||||||
@@ -59,26 +71,48 @@ namespace
|
|||||||
path::combine(parent_address, root["texture"].GetString()));
|
path::combine(parent_address, root["texture"].GetString()));
|
||||||
|
|
||||||
v2f pivot;
|
v2f pivot;
|
||||||
|
b2f inner_texrect;
|
||||||
|
b2f outer_texrect;
|
||||||
|
|
||||||
E2D_ASSERT(root.HasMember("pivot"));
|
E2D_ASSERT(root.HasMember("pivot"));
|
||||||
if ( !json_utils::try_parse_value(root["pivot"], pivot) ) {
|
if ( !json_utils::try_parse_value(root["pivot"], pivot) ) {
|
||||||
the<debug>().error("SPRITE: Incorrect formatting of 'pivot' property");
|
the<debug>().error("SPRITE: Incorrect formatting of 'pivot' property");
|
||||||
return stdex::make_rejected_promise<sprite>(sprite_asset_loading_exception());
|
return stdex::make_rejected_promise<sprite>(sprite_asset_loading_exception());
|
||||||
}
|
}
|
||||||
|
|
||||||
b2f texrect;
|
E2D_ASSERT(
|
||||||
E2D_ASSERT(root.HasMember("texrect"));
|
root.HasMember("texrect") ||
|
||||||
if ( !json_utils::try_parse_value(root["texrect"], texrect) ) {
|
(root.HasMember("inner_texrect") && root.HasMember("outer_texrect")));
|
||||||
the<debug>().error("SPRITE: Incorrect formatting of 'texrect' property");
|
|
||||||
return stdex::make_rejected_promise<sprite>(sprite_asset_loading_exception());
|
if ( root.HasMember("texrect") ) {
|
||||||
|
b2f texrect;
|
||||||
|
if ( !json_utils::try_parse_value(root["texrect"], texrect) ) {
|
||||||
|
the<debug>().error("SPRITE: Incorrect formatting of 'texrect' property");
|
||||||
|
return stdex::make_rejected_promise<sprite>(sprite_asset_loading_exception());
|
||||||
|
}
|
||||||
|
inner_texrect = texrect;
|
||||||
|
outer_texrect = texrect;
|
||||||
|
} else {
|
||||||
|
if ( !json_utils::try_parse_value(root["inner_texrect"], inner_texrect) ) {
|
||||||
|
the<debug>().error("SPRITE: Incorrect formatting of 'inner_texrect' property");
|
||||||
|
return stdex::make_rejected_promise<sprite>(sprite_asset_loading_exception());
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !json_utils::try_parse_value(root["outer_texrect"], outer_texrect) ) {
|
||||||
|
the<debug>().error("SPRITE: Incorrect formatting of 'outer_texrect' property");
|
||||||
|
return stdex::make_rejected_promise<sprite>(sprite_asset_loading_exception());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return texture_p.then([
|
return texture_p.then([
|
||||||
pivot,
|
pivot,
|
||||||
texrect
|
inner_texrect,
|
||||||
|
outer_texrect
|
||||||
](const texture_asset::load_result& texture){
|
](const texture_asset::load_result& texture){
|
||||||
sprite content;
|
sprite content;
|
||||||
content.set_pivot(pivot);
|
content.set_pivot(pivot);
|
||||||
content.set_texrect(texrect);
|
content.set_inner_texrect(inner_texrect);
|
||||||
|
content.set_outer_texrect(outer_texrect);
|
||||||
content.set_texture(texture);
|
content.set_texture(texture);
|
||||||
return content;
|
return content;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -204,8 +204,8 @@ namespace e2d
|
|||||||
if ( const sprite_asset::ptr& spr_a = c->sprite() ) {
|
if ( const sprite_asset::ptr& spr_a = c->sprite() ) {
|
||||||
const sprite& spr = spr_a->content();
|
const sprite& spr = spr_a->content();
|
||||||
ctx.draw_wire_rect(
|
ctx.draw_wire_rect(
|
||||||
spr.texrect().position - spr.pivot() + spr.texrect().size * 0.5f,
|
spr.outer_texrect().position - spr.pivot() + spr.outer_texrect().size * 0.5f,
|
||||||
spr.texrect().size,
|
spr.outer_texrect().size,
|
||||||
ctx.selected() ? color32::yellow() : color32::magenta());
|
ctx.selected() ? color32::yellow() : color32::magenta());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,14 +26,16 @@ namespace e2d
|
|||||||
|
|
||||||
void sprite::clear() noexcept {
|
void sprite::clear() noexcept {
|
||||||
pivot_ = v2f::zero();
|
pivot_ = v2f::zero();
|
||||||
texrect_ = b2f::zero();
|
inner_texrect_ = b2f::zero();
|
||||||
|
outer_texrect_ = b2f::zero();
|
||||||
texture_.reset();
|
texture_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sprite::swap(sprite& other) noexcept {
|
void sprite::swap(sprite& other) noexcept {
|
||||||
using std::swap;
|
using std::swap;
|
||||||
swap(pivot_, other.pivot_);
|
swap(pivot_, other.pivot_);
|
||||||
swap(texrect_, other.texrect_);
|
swap(inner_texrect_, other.inner_texrect_);
|
||||||
|
swap(outer_texrect_, other.outer_texrect_);
|
||||||
swap(texture_, other.texture_);
|
swap(texture_, other.texture_);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +51,8 @@ namespace e2d
|
|||||||
if ( this != &other ) {
|
if ( this != &other ) {
|
||||||
sprite s;
|
sprite s;
|
||||||
s.pivot_ = other.pivot_;
|
s.pivot_ = other.pivot_;
|
||||||
s.texrect_ = other.texrect_;
|
s.inner_texrect_ = other.inner_texrect_;
|
||||||
|
s.outer_texrect_ = other.outer_texrect_;
|
||||||
s.texture_ = other.texture_;
|
s.texture_ = other.texture_;
|
||||||
swap(s);
|
swap(s);
|
||||||
}
|
}
|
||||||
@@ -61,8 +64,13 @@ namespace e2d
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite& sprite::set_texrect(const b2f& texrect) noexcept {
|
sprite& sprite::set_inner_texrect(const b2f& texrect) noexcept {
|
||||||
texrect_ = texrect;
|
inner_texrect_ = texrect;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
sprite& sprite::set_outer_texrect(const b2f& texrect) noexcept {
|
||||||
|
outer_texrect_ = texrect;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +83,12 @@ namespace e2d
|
|||||||
return pivot_;
|
return pivot_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const b2f& sprite::texrect() const noexcept {
|
const b2f& sprite::inner_texrect() const noexcept {
|
||||||
return texrect_;
|
return inner_texrect_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const b2f& sprite::outer_texrect() const noexcept {
|
||||||
|
return outer_texrect_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const texture_asset::ptr& sprite::texture() const noexcept {
|
const texture_asset::ptr& sprite::texture() const noexcept {
|
||||||
@@ -92,7 +104,8 @@ namespace e2d
|
|||||||
|
|
||||||
bool operator==(const sprite& l, const sprite& r) noexcept {
|
bool operator==(const sprite& l, const sprite& r) noexcept {
|
||||||
return l.pivot() == r.pivot()
|
return l.pivot() == r.pivot()
|
||||||
&& l.texrect() == r.texrect()
|
&& l.inner_texrect() == r.inner_texrect()
|
||||||
|
&& l.outer_texrect() == r.outer_texrect()
|
||||||
&& l.texture() == r.texture();
|
&& l.texture() == r.texture();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -458,7 +458,7 @@ namespace e2d::render_system_impl
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const b2f& tex_r = spr.texrect();
|
const b2f& tex_r = spr.outer_texrect();
|
||||||
const v2f& tex_s = tex_p->size().cast_to<f32>();
|
const v2f& tex_s = tex_p->size().cast_to<f32>();
|
||||||
|
|
||||||
const f32 sw = tex_r.size.x;
|
const f32 sw = tex_r.size.x;
|
||||||
|
|||||||
@@ -4,5 +4,10 @@
|
|||||||
"name" : "sprite",
|
"name" : "sprite",
|
||||||
"pivot" : { "x" : 1, "y" : 2 },
|
"pivot" : { "x" : 1, "y" : 2 },
|
||||||
"texrect" : { "x" : 5, "y" : 6, "w" : 7, "h" : 8 }
|
"texrect" : { "x" : 5, "y" : 6, "w" : 7, "h" : 8 }
|
||||||
|
},{
|
||||||
|
"name" : "sprite2",
|
||||||
|
"pivot" : { "x" : 1, "y" : 2 },
|
||||||
|
"inner_texrect" : { "x" : 1, "y" : 2, "w" : 3, "h" : 4 },
|
||||||
|
"outer_texrect" : { "x" : 5, "y" : 6, "w" : 7, "h" : 8 }
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|||||||
6
untests/bin/library/sprite2.json
Normal file
6
untests/bin/library/sprite2.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"texture" : "image.png",
|
||||||
|
"pivot" : { "x" : 1, "y" : 2 },
|
||||||
|
"inner_texrect" : { "x" : 1, "y" : 2, "w" : 3, "h" : 4 },
|
||||||
|
"outer_texrect" : { "x" : 5, "y" : 6, "w" : 7, "h" : 8 }
|
||||||
|
}
|
||||||
@@ -188,20 +188,37 @@ TEST_CASE("library"){
|
|||||||
REQUIRE(atlas_res->content().texture() == texture_res);
|
REQUIRE(atlas_res->content().texture() == texture_res);
|
||||||
|
|
||||||
REQUIRE(atlas_res->find_nested_asset("sprite"));
|
REQUIRE(atlas_res->find_nested_asset("sprite"));
|
||||||
|
REQUIRE(atlas_res->find_nested_asset("sprite2"));
|
||||||
|
|
||||||
sprite_asset::ptr spr = atlas_res->find_nested_asset<sprite_asset>("sprite");
|
sprite_asset::ptr spr = atlas_res->find_nested_asset<sprite_asset>("sprite");
|
||||||
REQUIRE(spr);
|
REQUIRE(spr);
|
||||||
REQUIRE(spr->content().pivot() == v2f(1.f,2.f));
|
REQUIRE(spr->content().pivot() == v2f(1.f,2.f));
|
||||||
REQUIRE(spr->content().texrect() == b2f(5.f,6.f,7.f,8.f));
|
REQUIRE(spr->content().inner_texrect() == b2f(5.f,6.f,7.f,8.f));
|
||||||
|
REQUIRE(spr->content().outer_texrect() == b2f(5.f,6.f,7.f,8.f));
|
||||||
REQUIRE(spr->content().texture()== texture_res);
|
REQUIRE(spr->content().texture()== texture_res);
|
||||||
|
|
||||||
|
sprite_asset::ptr spr2 = atlas_res->find_nested_asset<sprite_asset>("sprite2");
|
||||||
|
REQUIRE(spr2);
|
||||||
|
REQUIRE(spr2->content().pivot() == v2f(1.f,2.f));
|
||||||
|
REQUIRE(spr2->content().inner_texrect() == b2f(1.f,2.f,3.f,4.f));
|
||||||
|
REQUIRE(spr2->content().outer_texrect() == b2f(5.f,6.f,7.f,8.f));
|
||||||
|
REQUIRE(spr2->content().texture()== texture_res);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto sprite_res = l.load_asset<sprite_asset>("sprite.json");
|
auto sprite_res = l.load_asset<sprite_asset>("sprite.json");
|
||||||
REQUIRE(sprite_res);
|
REQUIRE(sprite_res);
|
||||||
REQUIRE(sprite_res->content().pivot() == v2f(1.f, 2.f));
|
REQUIRE(sprite_res->content().pivot() == v2f(1.f, 2.f));
|
||||||
REQUIRE(sprite_res->content().texrect() == b2f(5.f, 6.f, 7.f, 8.f));
|
REQUIRE(sprite_res->content().inner_texrect() == b2f(5.f, 6.f, 7.f, 8.f));
|
||||||
|
REQUIRE(sprite_res->content().outer_texrect() == b2f(5.f, 6.f, 7.f, 8.f));
|
||||||
REQUIRE(sprite_res->content().texture() == texture_res);
|
REQUIRE(sprite_res->content().texture() == texture_res);
|
||||||
|
|
||||||
|
auto sprite2_res = l.load_asset<sprite_asset>("sprite2.json");
|
||||||
|
REQUIRE(sprite2_res);
|
||||||
|
REQUIRE(sprite2_res->content().pivot() == v2f(1.f, 2.f));
|
||||||
|
REQUIRE(sprite2_res->content().inner_texrect() == b2f(1.f, 2.f, 3.f, 4.f));
|
||||||
|
REQUIRE(sprite2_res->content().outer_texrect() == b2f(5.f, 6.f, 7.f, 8.f));
|
||||||
|
REQUIRE(sprite2_res->content().texture() == texture_res);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user