margin layout item property

This commit is contained in:
BlackMATov
2020-02-10 04:13:11 +07:00
parent fce3c16f76
commit ea38121608
4 changed files with 62 additions and 5 deletions

View File

@@ -55,10 +55,14 @@ namespace e2d
layout_item& size(const v2f& value) noexcept;
[[nodiscard]] const v2f& size() const noexcept;
layout_item& margin(const v2f& value) noexcept;
[[nodiscard]] const v2f& margin() const noexcept;
layout_item& padding(const v2f& value) noexcept;
[[nodiscard]] const v2f& padding() const noexcept;
private:
v2f size_ = v2f::unit();
v2f margin_ = v2f::zero();
v2f padding_ = v2f::zero();
};
}
@@ -173,6 +177,15 @@ namespace e2d
return size_;
}
inline layout_item& layout_item::margin(const v2f& value) noexcept {
margin_ = value;
return *this;
}
inline const v2f& layout_item::margin() const noexcept {
return margin_;
}
inline layout_item& layout_item::padding(const v2f& value) noexcept {
padding_ = value;
return *this;
@@ -201,6 +214,7 @@ namespace e2d::layout_items
bool is_dirty(const const_gcomponent<layout_item>& self) noexcept;
gcomponent<layout_item> change_size(gcomponent<layout_item> self, const v2f& value);
gcomponent<layout_item> change_margin(gcomponent<layout_item> self, const v2f& value);
gcomponent<layout_item> change_padding(gcomponent<layout_item> self, const v2f& value);
gcomponent<layout> find_parent_layout(const_gcomponent<layout_item> self) noexcept;

View File

@@ -77,6 +77,9 @@ local layout_item = {
---@type v2f
size = v2f.unit(),
---@type v2f
margin = v2f.zero(),
---@type v2f
padding = v2f.zero()
}

View File

@@ -184,6 +184,14 @@ namespace e2d::bindings::high
layout_items::change_size(c, v);
}),
"margin", sol::property(
[](const gcomponent<layout_item>& c) -> v2f {
return c->margin();
},
[](gcomponent<layout_item>& c, const v2f& v){
layout_items::change_margin(c, v);
}),
"padding", sol::property(
[](const gcomponent<layout_item>& c) -> v2f {
return c->padding();

View File

@@ -127,6 +127,7 @@ namespace e2d
"additionalProperties" : false,
"properties" : {
"size" : { "$ref": "#/common_definitions/v2" },
"margin" : { "$ref": "#/common_definitions/v2" },
"padding" : { "$ref": "#/common_definitions/v2" }
}
})json";
@@ -144,6 +145,15 @@ namespace e2d
component.size(size);
}
if ( ctx.root.HasMember("margin") ) {
v2f margin = component.margin();
if ( !json_utils::try_parse_value(ctx.root["margin"], margin) ) {
the<debug>().error("LAYOUT_ITEM: Incorrect formatting of 'margin' property");
return false;
}
component.margin(margin);
}
if ( ctx.root.HasMember("padding") ) {
v2f padding = component.padding();
if ( !json_utils::try_parse_value(ctx.root["padding"], padding) ) {
@@ -201,6 +211,12 @@ namespace e2d
layout_items::change_size(c, size);
}
if ( v2f margin = c->margin();
ImGui::DragFloat2("margin", margin.data(), 1.f) )
{
layout_items::change_margin(c, margin);
}
if ( v2f padding = c->padding();
ImGui::DragFloat2("padding", padding.data(), 1.f) )
{
@@ -217,11 +233,20 @@ namespace e2d
c->size(),
ctx.selected() ? color32(255,255,255) : color32(127,127,127));
if ( ctx.selected() && c->padding() != v2f::zero() ) {
ctx.draw_wire_rect(
c->size() * 0.5f,
c->size() - c->padding() * 2.f,
ctx.selected() ? color32(255,255,255) : color32(127,127,127));
if ( ctx.selected() ) {
if ( c->margin() != v2f::zero() ) {
ctx.draw_wire_rect(
c->size() * 0.5f,
c->size() + c->margin() * 2.f,
ctx.selected() ? color32(255,255,255) : color32(127,127,127));
}
if ( c->padding() != v2f::zero() ) {
ctx.draw_wire_rect(
c->size() * 0.5f,
c->size() - c->padding() * 2.f,
ctx.selected() ? color32(255,255,255) : color32(127,127,127));
}
}
}
}
@@ -291,6 +316,13 @@ namespace e2d::layout_items
return mark_dirty(self);
}
gcomponent<layout_item> change_margin(gcomponent<layout_item> self, const v2f& value) {
if ( self ) {
self->margin(value);
}
return mark_dirty(self);
}
gcomponent<layout_item> change_padding(gcomponent<layout_item> self, const v2f& value) {
if ( self ) {
self->padding(value);