From 363673601e3aacbd9bfa0d36c4e680f7c8e200a9 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 18 Feb 2020 05:04:39 +0700 Subject: [PATCH] sprite and alpha_threshold mask props --- headers/enduro2d/high/components/mask.hpp | 30 +++++++++ samples/bin/library/prefabs/mask_prefab.json | 8 +++ samples/bin/library/scenes/sample_09.json | 45 +++++++++++--- .../library/scripts/emmy/components/mask.lua | 8 ++- .../high_binds/components/mask_binds.cpp | 16 +++++ sources/enduro2d/high/components/mask.cpp | 61 ++++++++++++++++++- 6 files changed, 155 insertions(+), 13 deletions(-) create mode 100644 samples/bin/library/prefabs/mask_prefab.json diff --git a/headers/enduro2d/high/components/mask.hpp b/headers/enduro2d/high/components/mask.hpp index 9051e7c9..4bcfe55a 100644 --- a/headers/enduro2d/high/components/mask.hpp +++ b/headers/enduro2d/high/components/mask.hpp @@ -8,16 +8,28 @@ #include "_components.hpp" +#include "../assets/atlas_asset.hpp" +#include "../assets/sprite_asset.hpp" + namespace e2d { class mask final { public: mask() = default; + mask(const sprite_asset::ptr& sprite); mask& visible(bool value) noexcept; [[nodiscard]] bool visible() const noexcept; + + mask& sprite(const sprite_asset::ptr& value) noexcept; + [[nodiscard]] const sprite_asset::ptr& sprite() const noexcept; + + mask& alpha_threshold(f32 value) noexcept; + [[nodiscard]] f32 alpha_threshold() const noexcept; private: bool visible_ = false; + sprite_asset::ptr sprite_; + f32 alpha_threshold_ = 0.1f; }; } @@ -59,4 +71,22 @@ namespace e2d inline bool mask::visible() const noexcept { return visible_; } + + inline mask& mask::sprite(const sprite_asset::ptr& value) noexcept { + sprite_ = value; + return *this; + } + + inline const sprite_asset::ptr& mask::sprite() const noexcept { + return sprite_; + } + + inline mask& mask::alpha_threshold(f32 value) noexcept { + alpha_threshold_ = math::clamp(value, 0.f, 1.f); + return *this; + } + + inline f32 mask::alpha_threshold() const noexcept { + return alpha_threshold_; + } } diff --git a/samples/bin/library/prefabs/mask_prefab.json b/samples/bin/library/prefabs/mask_prefab.json new file mode 100644 index 00000000..d65186ba --- /dev/null +++ b/samples/bin/library/prefabs/mask_prefab.json @@ -0,0 +1,8 @@ +{ + "components" : { + "mask" : {}, + "named" : { + "name" : "mask" + } + } +} diff --git a/samples/bin/library/scenes/sample_09.json b/samples/bin/library/scenes/sample_09.json index fda74e0f..90cb032c 100644 --- a/samples/bin/library/scenes/sample_09.json +++ b/samples/bin/library/scenes/sample_09.json @@ -8,7 +8,7 @@ "prototype" : "../prefabs/layout_prefab.json", "components" : { "actor" : { - "translation" : [-250,0] + "translation" : [-250,6] }, "layout" : { "size" : [500,200], @@ -25,9 +25,7 @@ "children" : [{ "prototype" : "../prefabs/ship_prefab.json", "components" : { - "actor" : { - "translation" : [33,56.5] - } + "layout" : {} } }] },{ @@ -40,9 +38,7 @@ "children" : [{ "prototype" : "../prefabs/ship_prefab.json", "components" : { - "actor" : { - "translation" : [33,56.5] - } + "layout" : {} } }] },{ @@ -55,11 +51,40 @@ "children" : [{ "prototype" : "../prefabs/ship_prefab.json", "components" : { - "actor" : { - "translation" : [33,56.5] - } + "layout" : {} } }] + },{ + "prototype" : "../prefabs/layout_prefab.json", + "components" : { + "layout" : { + "size" : [66,113] + } + }, + "children" : [{ + "prototype" : "../prefabs/mask_prefab.json", + "components" : { + "layout" : {}, + "mask" : { + "sprite" : "../sprites/ship_sprite.json" + } + }, + "children" : [{ + "prototype" : "../prefabs/ship_prefab.json", + "components" : { + "actor" : { + "rotation" : -1 + } + } + },{ + "prototype" : "../prefabs/ship_prefab.json", + "components" : { + "actor" : { + "rotation" : 1 + } + } + }] + }] }] }] } diff --git a/samples/bin/library/scripts/emmy/components/mask.lua b/samples/bin/library/scripts/emmy/components/mask.lua index 5eb97804..35ccffa5 100644 --- a/samples/bin/library/scripts/emmy/components/mask.lua +++ b/samples/bin/library/scripts/emmy/components/mask.lua @@ -7,7 +7,13 @@ local mask = { disabled = false, ---@type boolean - visible = false + visible = false, + + ---@type sprite_asset + sprite = nil, + + ---@type number + alpha_threshold = 0.1 } ---@overload fun(self: mask) diff --git a/sources/enduro2d/high/bindings/high_binds/components/mask_binds.cpp b/sources/enduro2d/high/bindings/high_binds/components/mask_binds.cpp index 12348a22..b3a42e1c 100644 --- a/sources/enduro2d/high/bindings/high_binds/components/mask_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/components/mask_binds.cpp @@ -56,6 +56,22 @@ namespace e2d::bindings::high }, [](gcomponent& c, bool v){ c->visible(v); + }), + + "sprite", sol::property( + [](const gcomponent& c) -> sprite_asset::ptr { + return c->sprite(); + }, + [](gcomponent& c, const sprite_asset::ptr& v){ + c->sprite(v); + }), + + "alpha_threshold", sol::property( + [](const gcomponent& c) -> f32 { + return c->alpha_threshold(); + }, + [](gcomponent& c, f32 v){ + c->alpha_threshold(v); }) ); } diff --git a/sources/enduro2d/high/components/mask.cpp b/sources/enduro2d/high/components/mask.cpp index fe5c9889..a2d3f6f8 100644 --- a/sources/enduro2d/high/components/mask.cpp +++ b/sources/enduro2d/high/components/mask.cpp @@ -13,7 +13,10 @@ namespace e2d "required" : [], "additionalProperties" : false, "properties" : { - "visible" : { "type" : "boolean" } + "visible" : { "type" : "boolean" }, + "atlas" : { "$ref": "#/common_definitions/address" }, + "sprite" : { "$ref": "#/common_definitions/address" }, + "alpha_threshold" : { "type" : "number" } } })json"; @@ -30,6 +33,43 @@ namespace e2d component.visible(visible); } + if ( ctx.root.HasMember("atlas") ) { + auto sprite = ctx.dependencies.find_asset( + path::combine(ctx.parent_address, ctx.root["atlas"].GetString())); + if ( !sprite ) { + the().error("MASK: Dependency 'atlas' is not found:\n" + "--> Parent address: %0\n" + "--> Dependency address: %1", + ctx.parent_address, + ctx.root["atlas"].GetString()); + return false; + } + component.sprite(sprite); + } + + if ( ctx.root.HasMember("sprite") ) { + auto sprite = ctx.dependencies.find_asset( + path::combine(ctx.parent_address, ctx.root["sprite"].GetString())); + if ( !sprite ) { + the().error("MASK: Dependency 'sprite' is not found:\n" + "--> Parent address: %0\n" + "--> Dependency address: %1", + ctx.parent_address, + ctx.root["sprite"].GetString()); + return false; + } + component.sprite(sprite); + } + + if ( ctx.root.HasMember("alpha_threshold") ) { + f32 alpha_threshold = component.alpha_threshold(); + if ( !json_utils::try_parse_value(ctx.root["alpha_threshold"], alpha_threshold) ) { + the().error("MASK: Incorrect formatting of 'alpha_threshold' property"); + return false; + } + component.alpha_threshold(alpha_threshold); + } + return true; } @@ -37,7 +77,16 @@ namespace e2d asset_dependencies& dependencies, const collect_context& ctx) const { - E2D_UNUSED(dependencies, ctx); + if ( ctx.root.HasMember("atlas") ) { + dependencies.add_dependency( + path::combine(ctx.parent_address, ctx.root["atlas"].GetString())); + } + + if ( ctx.root.HasMember("sprite") ) { + dependencies.add_dependency( + path::combine(ctx.parent_address, ctx.root["sprite"].GetString())); + } + return true; } } @@ -52,5 +101,13 @@ namespace e2d { c->visible(visible); } + + ///TODO(BlackMat): add 'sprite' inspector + + if ( f32 alpha_threshold = c->alpha_threshold(); + ImGui::SliderFloat("alpha_threshold", &alpha_threshold, 0.f, 1.f) ) + { + c->alpha_threshold(alpha_threshold); + } } }