sprite and alpha_threshold mask props

This commit is contained in:
BlackMATov
2020-02-18 05:04:39 +07:00
parent 7cda07c14e
commit 363673601e
6 changed files with 155 additions and 13 deletions

View File

@@ -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_;
}
}

View File

@@ -0,0 +1,8 @@
{
"components" : {
"mask" : {},
"named" : {
"name" : "mask"
}
}
}

View File

@@ -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
}
}
}]
}]
}]
}]
}

View File

@@ -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)

View File

@@ -56,6 +56,22 @@ namespace e2d::bindings::high
},
[](gcomponent<mask>& c, bool v){
c->visible(v);
}),
"sprite", sol::property(
[](const gcomponent<mask>& c) -> sprite_asset::ptr {
return c->sprite();
},
[](gcomponent<mask>& c, const sprite_asset::ptr& v){
c->sprite(v);
}),
"alpha_threshold", sol::property(
[](const gcomponent<mask>& c) -> f32 {
return c->alpha_threshold();
},
[](gcomponent<mask>& c, f32 v){
c->alpha_threshold(v);
})
);
}

View File

@@ -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<atlas_asset, sprite_asset>(
path::combine(ctx.parent_address, ctx.root["atlas"].GetString()));
if ( !sprite ) {
the<debug>().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<sprite_asset>(
path::combine(ctx.parent_address, ctx.root["sprite"].GetString()));
if ( !sprite ) {
the<debug>().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<debug>().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<atlas_asset, sprite_asset>(
path::combine(ctx.parent_address, ctx.root["atlas"].GetString()));
}
if ( ctx.root.HasMember("sprite") ) {
dependencies.add_dependency<sprite_asset>(
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);
}
}
}