diff --git a/headers/enduro2d/high/components/layout.hpp b/headers/enduro2d/high/components/layout.hpp index aef737c9..b83d9a0f 100644 --- a/headers/enduro2d/high/components/layout.hpp +++ b/headers/enduro2d/high/components/layout.hpp @@ -21,12 +21,16 @@ namespace e2d ENUM_HPP_CLASS_DECL(haligns, u8, (left) (center) - (right)) + (right) + (space_around) + (space_between)) ENUM_HPP_CLASS_DECL(valigns, u8, (top) (center) - (bottom)) + (bottom) + (space_around) + (space_between)) public: layout() = default; @@ -38,14 +42,10 @@ namespace e2d layout& valign(valigns value) noexcept; [[nodiscard]] valigns valign() const noexcept; - - layout& spacing(f32 value) noexcept; - [[nodiscard]] f32 spacing() const noexcept; private: modes mode_ = modes::horizontal; haligns halign_ = haligns::center; valigns valign_ = valigns::center; - f32 spacing_ = 0.f; }; class layout_item final { @@ -54,8 +54,12 @@ namespace e2d layout_item& size(const v2f& value) noexcept; [[nodiscard]] const v2f& size() const noexcept; + + layout_item& padding(const v2f& value) noexcept; + [[nodiscard]] const v2f& padding() const noexcept; private: v2f size_ = v2f::unit(); + v2f padding_ = v2f::zero(); }; } @@ -156,15 +160,6 @@ namespace e2d inline layout::valigns layout::valign() const noexcept { return valign_; } - - inline layout& layout::spacing(f32 value) noexcept { - spacing_ = value; - return *this; - } - - inline f32 layout::spacing() const noexcept { - return spacing_; - } } namespace e2d @@ -177,6 +172,15 @@ namespace e2d inline const v2f& layout_item::size() const noexcept { return size_; } + + inline layout_item& layout_item::padding(const v2f& value) noexcept { + padding_ = value; + return *this; + } + + inline const v2f& layout_item::padding() const noexcept { + return padding_; + } } namespace e2d::layouts @@ -188,7 +192,6 @@ namespace e2d::layouts gcomponent change_mode(gcomponent self, layout::modes value); gcomponent change_halign(gcomponent self, layout::haligns value); gcomponent change_valign(gcomponent self, layout::valigns value); - gcomponent change_spacing(gcomponent self, f32 value); } namespace e2d::layout_items @@ -198,6 +201,7 @@ namespace e2d::layout_items bool is_dirty(const const_gcomponent& self) noexcept; gcomponent change_size(gcomponent self, const v2f& value); + gcomponent change_padding(gcomponent self, const v2f& value); gcomponent find_parent_layout(const_gcomponent self) noexcept; } diff --git a/samples/bin/library/scripts/emmy/components/layout.lua b/samples/bin/library/scripts/emmy/components/layout.lua index c4cf4aef..f58d4fae 100644 --- a/samples/bin/library/scripts/emmy/components/layout.lua +++ b/samples/bin/library/scripts/emmy/components/layout.lua @@ -22,10 +22,7 @@ local layout = { halign = layout.haligns.center, ---@type layout_valigns - valign = layout.valigns.center, - - ---@type number - spacing = 0.0 + valign = layout.valigns.center } ---@class layout_modes @@ -38,14 +35,18 @@ layout.modes = { layout.haligns = { left = "left", center = "center", - right = "right" + right = "right", + space_around = "space_around", + space_between = "space_between" } ---@class layout_valigns layout.valigns = { top = "top", center = "center", - bottom = "bottom" + bottom = "bottom", + space_around = "space_around", + space_between = "space_between" } ---@overload fun(self: layout) @@ -74,7 +75,10 @@ local layout_item = { dirty = false, ---@type v2f - size = v2f.unit() + size = v2f.unit(), + + ---@type v2f + padding = v2f.zero() } ---@overload fun(self: layout_item) diff --git a/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp b/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp index c5646fac..1050ed27 100644 --- a/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp @@ -89,14 +89,6 @@ namespace e2d::bindings::high }, [](gcomponent& c, layout::valigns v){ layouts::change_valign(c, v); - }), - - "spacing", sol::property( - [](const gcomponent& c) -> f32 { - return c->spacing(); - }, - [](gcomponent& c, f32 v){ - layouts::change_spacing(c, v); }) ); @@ -114,6 +106,8 @@ namespace e2d::bindings::high LAYOUT_HALIGN_PAIR(left) LAYOUT_HALIGN_PAIR(center) LAYOUT_HALIGN_PAIR(right) + LAYOUT_HALIGN_PAIR(space_around) + LAYOUT_HALIGN_PAIR(space_between) }); #undef LAYOUT_HALIGN_PAIR @@ -123,6 +117,8 @@ namespace e2d::bindings::high LAYOUT_VALIGN_PAIR(top) LAYOUT_VALIGN_PAIR(center) LAYOUT_VALIGN_PAIR(bottom) + LAYOUT_VALIGN_PAIR(space_around) + LAYOUT_VALIGN_PAIR(space_between) }); #undef LAYOUT_VALIGN_PAIR @@ -186,6 +182,14 @@ namespace e2d::bindings::high }, [](gcomponent& c, const v2f& v){ layout_items::change_size(c, v); + }), + + "padding", sol::property( + [](const gcomponent& c) -> v2f { + return c->padding(); + }, + [](gcomponent& c, const v2f& v){ + layout_items::change_padding(c, v); }) ); } diff --git a/sources/enduro2d/high/components/layout.cpp b/sources/enduro2d/high/components/layout.cpp index 06b0f0ae..641bd1b5 100644 --- a/sources/enduro2d/high/components/layout.cpp +++ b/sources/enduro2d/high/components/layout.cpp @@ -17,8 +17,7 @@ namespace e2d "properties" : { "mode" : { "$ref": "#/definitions/modes" }, "halign" : { "$ref": "#/definitions/haligns" }, - "valign" : { "$ref": "#/definitions/valigns" }, - "spacing" : { "type" : "number" } + "valign" : { "$ref": "#/definitions/valigns" } }, "definitions" : { "modes" : { @@ -33,7 +32,9 @@ namespace e2d "enum" : [ "left", "center", - "right" + "right", + "space_around", + "space_between" ] }, "valigns" : { @@ -41,7 +42,9 @@ namespace e2d "enum" : [ "top", "center", - "bottom" + "bottom", + "space_around", + "space_between" ] } } @@ -78,15 +81,6 @@ namespace e2d component.valign(valign); } - if ( ctx.root.HasMember("spacing") ) { - f32 spacing = component.spacing(); - if ( !json_utils::try_parse_value(ctx.root["spacing"], spacing) ) { - the().error("LAYOUT: Incorrect formatting of 'spacing' property"); - return false; - } - component.spacing(spacing); - } - return true; } @@ -132,7 +126,8 @@ namespace e2d "required" : [], "additionalProperties" : false, "properties" : { - "size" : { "$ref": "#/common_definitions/v2" } + "size" : { "$ref": "#/common_definitions/v2" }, + "padding" : { "$ref": "#/common_definitions/v2" } } })json"; @@ -149,6 +144,15 @@ namespace e2d component.size(size); } + if ( ctx.root.HasMember("padding") ) { + v2f padding = component.padding(); + if ( !json_utils::try_parse_value(ctx.root["padding"], padding) ) { + the().error("LAYOUT_ITEM: Incorrect formatting of 'padding' property"); + return false; + } + component.padding(padding); + } + return true; } @@ -183,12 +187,6 @@ namespace e2d { layouts::change_valign(c, valign); } - - if ( f32 spacing = c->spacing(); - ImGui::DragFloat("spacing", &spacing, 1.f) ) - { - layouts::change_spacing(c, spacing); - } } } @@ -202,6 +200,12 @@ namespace e2d { layout_items::change_size(c, size); } + + if ( v2f padding = c->padding(); + ImGui::DragFloat2("padding", padding.data(), 1.f) ) + { + layout_items::change_padding(c, padding); + } } void component_inspector::operator()( @@ -212,6 +216,13 @@ namespace e2d c->size() * 0.5f, 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)); + } } } @@ -255,13 +266,6 @@ namespace e2d::layouts } return mark_dirty(self); } - - gcomponent change_spacing(gcomponent self, f32 value) { - if ( self ) { - self->spacing(value); - } - return mark_dirty(self); - } } namespace e2d::layout_items @@ -287,6 +291,13 @@ namespace e2d::layout_items return mark_dirty(self); } + gcomponent change_padding(gcomponent self, const v2f& value) { + if ( self ) { + self->padding(value); + } + return mark_dirty(self); + } + gcomponent find_parent_layout(const_gcomponent self) noexcept { const_gcomponent self_actor = self.owner().component(); return self_actor