more align types for layouts

This commit is contained in:
BlackMATov
2020-02-10 03:52:58 +07:00
parent 3eb7be66a4
commit fce3c16f76
4 changed files with 81 additions and 58 deletions

View File

@@ -21,12 +21,16 @@ namespace e2d
ENUM_HPP_CLASS_DECL(haligns, u8, ENUM_HPP_CLASS_DECL(haligns, u8,
(left) (left)
(center) (center)
(right)) (right)
(space_around)
(space_between))
ENUM_HPP_CLASS_DECL(valigns, u8, ENUM_HPP_CLASS_DECL(valigns, u8,
(top) (top)
(center) (center)
(bottom)) (bottom)
(space_around)
(space_between))
public: public:
layout() = default; layout() = default;
@@ -38,14 +42,10 @@ namespace e2d
layout& valign(valigns value) noexcept; layout& valign(valigns value) noexcept;
[[nodiscard]] valigns valign() const noexcept; [[nodiscard]] valigns valign() const noexcept;
layout& spacing(f32 value) noexcept;
[[nodiscard]] f32 spacing() const noexcept;
private: private:
modes mode_ = modes::horizontal; modes mode_ = modes::horizontal;
haligns halign_ = haligns::center; haligns halign_ = haligns::center;
valigns valign_ = valigns::center; valigns valign_ = valigns::center;
f32 spacing_ = 0.f;
}; };
class layout_item final { class layout_item final {
@@ -54,8 +54,12 @@ namespace e2d
layout_item& size(const v2f& value) noexcept; layout_item& size(const v2f& value) noexcept;
[[nodiscard]] const v2f& size() const noexcept; [[nodiscard]] const v2f& size() const noexcept;
layout_item& padding(const v2f& value) noexcept;
[[nodiscard]] const v2f& padding() const noexcept;
private: private:
v2f size_ = v2f::unit(); v2f size_ = v2f::unit();
v2f padding_ = v2f::zero();
}; };
} }
@@ -156,15 +160,6 @@ namespace e2d
inline layout::valigns layout::valign() const noexcept { inline layout::valigns layout::valign() const noexcept {
return valign_; return valign_;
} }
inline layout& layout::spacing(f32 value) noexcept {
spacing_ = value;
return *this;
}
inline f32 layout::spacing() const noexcept {
return spacing_;
}
} }
namespace e2d namespace e2d
@@ -177,6 +172,15 @@ namespace e2d
inline const v2f& layout_item::size() const noexcept { inline const v2f& layout_item::size() const noexcept {
return size_; 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 namespace e2d::layouts
@@ -188,7 +192,6 @@ namespace e2d::layouts
gcomponent<layout> change_mode(gcomponent<layout> self, layout::modes value); gcomponent<layout> change_mode(gcomponent<layout> self, layout::modes value);
gcomponent<layout> change_halign(gcomponent<layout> self, layout::haligns value); gcomponent<layout> change_halign(gcomponent<layout> self, layout::haligns value);
gcomponent<layout> change_valign(gcomponent<layout> self, layout::valigns value); gcomponent<layout> change_valign(gcomponent<layout> self, layout::valigns value);
gcomponent<layout> change_spacing(gcomponent<layout> self, f32 value);
} }
namespace e2d::layout_items namespace e2d::layout_items
@@ -198,6 +201,7 @@ namespace e2d::layout_items
bool is_dirty(const const_gcomponent<layout_item>& self) noexcept; 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_size(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; gcomponent<layout> find_parent_layout(const_gcomponent<layout_item> self) noexcept;
} }

View File

@@ -22,10 +22,7 @@ local layout = {
halign = layout.haligns.center, halign = layout.haligns.center,
---@type layout_valigns ---@type layout_valigns
valign = layout.valigns.center, valign = layout.valigns.center
---@type number
spacing = 0.0
} }
---@class layout_modes ---@class layout_modes
@@ -38,14 +35,18 @@ layout.modes = {
layout.haligns = { layout.haligns = {
left = "left", left = "left",
center = "center", center = "center",
right = "right" right = "right",
space_around = "space_around",
space_between = "space_between"
} }
---@class layout_valigns ---@class layout_valigns
layout.valigns = { layout.valigns = {
top = "top", top = "top",
center = "center", center = "center",
bottom = "bottom" bottom = "bottom",
space_around = "space_around",
space_between = "space_between"
} }
---@overload fun(self: layout) ---@overload fun(self: layout)
@@ -74,7 +75,10 @@ local layout_item = {
dirty = false, dirty = false,
---@type v2f ---@type v2f
size = v2f.unit() size = v2f.unit(),
---@type v2f
padding = v2f.zero()
} }
---@overload fun(self: layout_item) ---@overload fun(self: layout_item)

View File

@@ -89,14 +89,6 @@ namespace e2d::bindings::high
}, },
[](gcomponent<layout>& c, layout::valigns v){ [](gcomponent<layout>& c, layout::valigns v){
layouts::change_valign(c, v); layouts::change_valign(c, v);
}),
"spacing", sol::property(
[](const gcomponent<layout>& c) -> f32 {
return c->spacing();
},
[](gcomponent<layout>& c, f32 v){
layouts::change_spacing(c, v);
}) })
); );
@@ -114,6 +106,8 @@ namespace e2d::bindings::high
LAYOUT_HALIGN_PAIR(left) LAYOUT_HALIGN_PAIR(left)
LAYOUT_HALIGN_PAIR(center) LAYOUT_HALIGN_PAIR(center)
LAYOUT_HALIGN_PAIR(right) LAYOUT_HALIGN_PAIR(right)
LAYOUT_HALIGN_PAIR(space_around)
LAYOUT_HALIGN_PAIR(space_between)
}); });
#undef LAYOUT_HALIGN_PAIR #undef LAYOUT_HALIGN_PAIR
@@ -123,6 +117,8 @@ namespace e2d::bindings::high
LAYOUT_VALIGN_PAIR(top) LAYOUT_VALIGN_PAIR(top)
LAYOUT_VALIGN_PAIR(center) LAYOUT_VALIGN_PAIR(center)
LAYOUT_VALIGN_PAIR(bottom) LAYOUT_VALIGN_PAIR(bottom)
LAYOUT_VALIGN_PAIR(space_around)
LAYOUT_VALIGN_PAIR(space_between)
}); });
#undef LAYOUT_VALIGN_PAIR #undef LAYOUT_VALIGN_PAIR
@@ -186,6 +182,14 @@ namespace e2d::bindings::high
}, },
[](gcomponent<layout_item>& c, const v2f& v){ [](gcomponent<layout_item>& c, const v2f& v){
layout_items::change_size(c, v); layout_items::change_size(c, v);
}),
"padding", sol::property(
[](const gcomponent<layout_item>& c) -> v2f {
return c->padding();
},
[](gcomponent<layout_item>& c, const v2f& v){
layout_items::change_padding(c, v);
}) })
); );
} }

View File

@@ -17,8 +17,7 @@ namespace e2d
"properties" : { "properties" : {
"mode" : { "$ref": "#/definitions/modes" }, "mode" : { "$ref": "#/definitions/modes" },
"halign" : { "$ref": "#/definitions/haligns" }, "halign" : { "$ref": "#/definitions/haligns" },
"valign" : { "$ref": "#/definitions/valigns" }, "valign" : { "$ref": "#/definitions/valigns" }
"spacing" : { "type" : "number" }
}, },
"definitions" : { "definitions" : {
"modes" : { "modes" : {
@@ -33,7 +32,9 @@ namespace e2d
"enum" : [ "enum" : [
"left", "left",
"center", "center",
"right" "right",
"space_around",
"space_between"
] ]
}, },
"valigns" : { "valigns" : {
@@ -41,7 +42,9 @@ namespace e2d
"enum" : [ "enum" : [
"top", "top",
"center", "center",
"bottom" "bottom",
"space_around",
"space_between"
] ]
} }
} }
@@ -78,15 +81,6 @@ namespace e2d
component.valign(valign); component.valign(valign);
} }
if ( ctx.root.HasMember("spacing") ) {
f32 spacing = component.spacing();
if ( !json_utils::try_parse_value(ctx.root["spacing"], spacing) ) {
the<debug>().error("LAYOUT: Incorrect formatting of 'spacing' property");
return false;
}
component.spacing(spacing);
}
return true; return true;
} }
@@ -132,7 +126,8 @@ namespace e2d
"required" : [], "required" : [],
"additionalProperties" : false, "additionalProperties" : false,
"properties" : { "properties" : {
"size" : { "$ref": "#/common_definitions/v2" } "size" : { "$ref": "#/common_definitions/v2" },
"padding" : { "$ref": "#/common_definitions/v2" }
} }
})json"; })json";
@@ -149,6 +144,15 @@ namespace e2d
component.size(size); component.size(size);
} }
if ( ctx.root.HasMember("padding") ) {
v2f padding = component.padding();
if ( !json_utils::try_parse_value(ctx.root["padding"], padding) ) {
the<debug>().error("LAYOUT_ITEM: Incorrect formatting of 'padding' property");
return false;
}
component.padding(padding);
}
return true; return true;
} }
@@ -183,12 +187,6 @@ namespace e2d
{ {
layouts::change_valign(c, valign); 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); 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<layout_item>::operator()( void component_inspector<layout_item>::operator()(
@@ -212,6 +216,13 @@ namespace e2d
c->size() * 0.5f, c->size() * 0.5f,
c->size(), c->size(),
ctx.selected() ? color32(255,255,255) : color32(127,127,127)); 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); return mark_dirty(self);
} }
gcomponent<layout> change_spacing(gcomponent<layout> self, f32 value) {
if ( self ) {
self->spacing(value);
}
return mark_dirty(self);
}
} }
namespace e2d::layout_items namespace e2d::layout_items
@@ -287,6 +291,13 @@ namespace e2d::layout_items
return mark_dirty(self); return mark_dirty(self);
} }
gcomponent<layout_item> change_padding(gcomponent<layout_item> self, const v2f& value) {
if ( self ) {
self->padding(value);
}
return mark_dirty(self);
}
gcomponent<layout> find_parent_layout(const_gcomponent<layout_item> self) noexcept { gcomponent<layout> find_parent_layout(const_gcomponent<layout_item> self) noexcept {
const_gcomponent<actor> self_actor = self.owner().component<actor>(); const_gcomponent<actor> self_actor = self.owner().component<actor>();
return self_actor return self_actor