combine layout and layout_item

This commit is contained in:
BlackMATov
2020-02-11 04:02:07 +07:00
parent ea38121608
commit d9b1f173a2
7 changed files with 106 additions and 297 deletions

View File

@@ -42,26 +42,21 @@ namespace e2d
layout& valign(valigns value) noexcept;
[[nodiscard]] valigns valign() const noexcept;
public:
layout& size(const v2f& value) noexcept;
[[nodiscard]] const v2f& size() const noexcept;
layout& margin(const v2f& value) noexcept;
[[nodiscard]] const v2f& margin() const noexcept;
layout& padding(const v2f& value) noexcept;
[[nodiscard]] const v2f& padding() const noexcept;
private:
modes mode_ = modes::horizontal;
haligns halign_ = haligns::center;
valigns valign_ = valigns::center;
};
class layout_item final {
public:
layout_item() = default;
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 size_ = v2f::zero();
v2f margin_ = v2f::zero();
v2f padding_ = v2f::zero();
};
@@ -100,20 +95,6 @@ namespace e2d
asset_dependencies& dependencies,
const collect_context& ctx) const;
};
template <>
class factory_loader<layout_item> final : factory_loader<> {
public:
static const char* schema_source;
bool operator()(
layout_item& component,
const fill_context& ctx) const;
bool operator()(
asset_dependencies& dependencies,
const collect_context& ctx) const;
};
}
namespace e2d
@@ -124,15 +105,7 @@ namespace e2d
static const char* title;
void operator()(gcomponent<layout>& c) const;
};
template <>
class component_inspector<layout_item> final : component_inspector<> {
public:
static const char* title;
void operator()(gcomponent<layout_item>& c) const;
void operator()(gcomponent<layout_item>& c, gizmos_context& ctx) const;
void operator()(gcomponent<layout>& c, gizmos_context& ctx) const;
};
}
@@ -164,34 +137,31 @@ namespace e2d
inline layout::valigns layout::valign() const noexcept {
return valign_;
}
}
namespace e2d
{
inline layout_item& layout_item::size(const v2f& value) noexcept {
inline layout& layout::size(const v2f& value) noexcept {
size_ = value;
return *this;
}
inline const v2f& layout_item::size() const noexcept {
inline const v2f& layout::size() const noexcept {
return size_;
}
inline layout_item& layout_item::margin(const v2f& value) noexcept {
inline layout& layout::margin(const v2f& value) noexcept {
margin_ = value;
return *this;
}
inline const v2f& layout_item::margin() const noexcept {
inline const v2f& layout::margin() const noexcept {
return margin_;
}
inline layout_item& layout_item::padding(const v2f& value) noexcept {
inline layout& layout::padding(const v2f& value) noexcept {
padding_ = value;
return *this;
}
inline const v2f& layout_item::padding() const noexcept {
inline const v2f& layout::padding() const noexcept {
return padding_;
}
}
@@ -205,17 +175,10 @@ namespace e2d::layouts
gcomponent<layout> change_mode(gcomponent<layout> self, layout::modes value);
gcomponent<layout> change_halign(gcomponent<layout> self, layout::haligns value);
gcomponent<layout> change_valign(gcomponent<layout> self, layout::valigns value);
}
namespace e2d::layout_items
{
gcomponent<layout_item> mark_dirty(gcomponent<layout_item> self);
gcomponent<layout_item> unmark_dirty(gcomponent<layout_item> self);
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;
gcomponent<layout> change_size(gcomponent<layout> self, const v2f& value);
gcomponent<layout> change_margin(gcomponent<layout> self, const v2f& value);
gcomponent<layout> change_padding(gcomponent<layout> self, const v2f& value);
gcomponent<layout> find_parent_layout(const_gcomponent<layout> self) noexcept;
}

View File

@@ -7,11 +7,9 @@
},{
"prototype" : "../prefabs/layout_prefab.json",
"children" : [{
"prototype" : "../prefabs/layout_prefab.json",
"components" : {
"named" : {
"name" : "layout_item"
},
"layout_item" : {
"layout" : {
"size" : [66,113]
}
},
@@ -24,11 +22,9 @@
}
}]
},{
"prototype" : "../prefabs/layout_prefab.json",
"components" : {
"named" : {
"name" : "layout_item"
},
"layout_item" : {
"layout" : {
"size" : [66,113]
}
},
@@ -41,11 +37,9 @@
}
}]
},{
"prototype" : "../prefabs/layout_prefab.json",
"components" : {
"named" : {
"name" : "layout_item"
},
"layout_item" : {
"layout" : {
"size" : [66,113]
}
},

View File

@@ -1,9 +1,3 @@
-- -----------------------------------------------------------------------------
--
-- layout
--
-- -----------------------------------------------------------------------------
---@class layout
local layout = {
---@type boolean
@@ -22,7 +16,16 @@ local layout = {
halign = layout.haligns.center,
---@type layout_valigns
valign = layout.valigns.center
valign = layout.valigns.center,
---@type v2f
size = v2f.zero(),
---@type v2f
margin = v2f.zero(),
---@type v2f
padding = v2f.zero()
}
---@class layout_modes
@@ -57,49 +60,5 @@ function layout.enable(self) end
---@param self layout
function layout.disable(self) end
-- -----------------------------------------------------------------------------
--
-- layout_item
--
-- -----------------------------------------------------------------------------
---@class layout_item
local layout_item = {
---@type boolean
enabled = true,
---@type boolean
disabled = false,
---@type boolean
dirty = false,
---@type v2f
size = v2f.unit(),
---@type v2f
margin = v2f.zero(),
---@type v2f
padding = v2f.zero()
}
---@overload fun(self: layout_item)
---@param self layout_item
function layout_item.enable(self) end
---@overload fun(self: layout_item)
---@param self layout_item
function layout_item.disable(self) end
-- -----------------------------------------------------------------------------
--
-- global
--
-- -----------------------------------------------------------------------------
---@type layout
_G.layout = _G.layout or layout
---@type layout_item
_G.layout_item = _G.layout_item or layout_item

View File

@@ -33,9 +33,6 @@ local gobject = {
---@type layout
layout = nil,
---@type layout_item
layout_item = nil,
---@type model_renderer
model_renderer = nil,

View File

@@ -89,6 +89,30 @@ namespace e2d::bindings::high
},
[](gcomponent<layout>& c, layout::valigns v){
layouts::change_valign(c, v);
}),
"size", sol::property(
[](const gcomponent<layout>& c) -> v2f {
return c->size();
},
[](gcomponent<layout>& c, const v2f& v){
layouts::change_size(c, v);
}),
"margin", sol::property(
[](const gcomponent<layout>& c) -> v2f {
return c->margin();
},
[](gcomponent<layout>& c, const v2f& v){
layouts::change_margin(c, v);
}),
"padding", sol::property(
[](const gcomponent<layout>& c) -> v2f {
return c->padding();
},
[](gcomponent<layout>& c, const v2f& v){
layouts::change_padding(c, v);
})
);
@@ -121,84 +145,5 @@ namespace e2d::bindings::high
LAYOUT_VALIGN_PAIR(space_between)
});
#undef LAYOUT_VALIGN_PAIR
l.new_usertype<gcomponent<layout_item>>("layout_item",
sol::no_constructor,
"enable", [](gcomponent<layout_item>& c){
c.owner().component<disabled<layout_item>>().remove();
layout_items::mark_dirty(c);
},
"disable", [](gcomponent<layout_item>& c){
c.owner().component<disabled<layout_item>>().ensure();
layout_items::mark_dirty(c);
},
"enabled", sol::property(
[](const gcomponent<layout_item>& c) -> bool {
return !c.owner().component<disabled<layout_item>>().exists();
},
[](gcomponent<layout_item>& c, bool yesno){
if ( yesno ) {
c.owner().component<disabled<layout_item>>().remove();
} else {
c.owner().component<disabled<layout_item>>().ensure();
}
layout_items::mark_dirty(c);
}
),
"disabled", sol::property(
[](const gcomponent<layout_item>& c) -> bool {
return c.owner().component<disabled<layout_item>>().exists();
},
[](gcomponent<layout_item>& c, bool yesno){
if ( yesno ) {
c.owner().component<disabled<layout_item>>().ensure();
} else {
c.owner().component<disabled<layout_item>>().remove();
}
layout_items::mark_dirty(c);
}
),
"dirty", sol::property(
[](const gcomponent<layout_item>& c) -> bool {
return layout_items::is_dirty(c);
},
[](gcomponent<layout_item>& c, bool yesno){
if ( yesno ) {
layout_items::mark_dirty(c);
} else {
layout_items::unmark_dirty(c);
}
}
),
"size", sol::property(
[](const gcomponent<layout_item>& c) -> v2f {
return c->size();
},
[](gcomponent<layout_item>& c, const v2f& v){
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();
},
[](gcomponent<layout_item>& c, const v2f& v){
layout_items::change_padding(c, v);
})
);
}
}

View File

@@ -17,7 +17,10 @@ namespace e2d
"properties" : {
"mode" : { "$ref": "#/definitions/modes" },
"halign" : { "$ref": "#/definitions/haligns" },
"valign" : { "$ref": "#/definitions/valigns" }
"valign" : { "$ref": "#/definitions/valigns" },
"size" : { "$ref": "#/common_definitions/v2" },
"margin" : { "$ref": "#/common_definitions/v2" },
"padding" : { "$ref": "#/common_definitions/v2" }
},
"definitions" : {
"modes" : {
@@ -81,6 +84,33 @@ namespace e2d
component.valign(valign);
}
if ( ctx.root.HasMember("size") ) {
v2f size = component.size();
if ( !json_utils::try_parse_value(ctx.root["size"], size) ) {
the<debug>().error("LAYOUT: Incorrect formatting of 'size' property");
return false;
}
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: 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) ) {
the<debug>().error("LAYOUT: Incorrect formatting of 'padding' property");
return false;
}
component.padding(padding);
}
return true;
}
@@ -119,62 +149,6 @@ namespace e2d
}
}
namespace e2d
{
const char* factory_loader<layout_item>::schema_source = R"json({
"type" : "object",
"required" : [],
"additionalProperties" : false,
"properties" : {
"size" : { "$ref": "#/common_definitions/v2" },
"margin" : { "$ref": "#/common_definitions/v2" },
"padding" : { "$ref": "#/common_definitions/v2" }
}
})json";
bool factory_loader<layout_item>::operator()(
layout_item& component,
const fill_context& ctx) const
{
if ( ctx.root.HasMember("size") ) {
v2f size = component.size();
if ( !json_utils::try_parse_value(ctx.root["size"], size) ) {
the<debug>().error("LAYOUT_ITEM: Incorrect formatting of 'size' property");
return false;
}
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) ) {
the<debug>().error("LAYOUT_ITEM: Incorrect formatting of 'padding' property");
return false;
}
component.padding(padding);
}
return true;
}
bool factory_loader<layout_item>::operator()(
asset_dependencies& dependencies,
const collect_context& ctx) const
{
E2D_UNUSED(dependencies, ctx);
return true;
}
}
namespace e2d
{
const char* component_inspector<layout>::title = ICON_FA_BARS " layout";
@@ -197,35 +171,28 @@ namespace e2d
{
layouts::change_valign(c, valign);
}
}
}
namespace e2d
{
const char* component_inspector<layout_item>::title = ICON_FA_GRIP_LINES " layout_item";
void component_inspector<layout_item>::operator()(gcomponent<layout_item>& c) const {
if ( v2f size = c->size();
ImGui::DragFloat2("size", size.data(), 1.f) )
{
layout_items::change_size(c, size);
layouts::change_size(c, size);
}
if ( v2f margin = c->margin();
ImGui::DragFloat2("margin", margin.data(), 1.f) )
{
layout_items::change_margin(c, margin);
layouts::change_margin(c, margin);
}
if ( v2f padding = c->padding();
ImGui::DragFloat2("padding", padding.data(), 1.f) )
{
layout_items::change_padding(c, padding);
layouts::change_padding(c, padding);
}
}
void component_inspector<layout_item>::operator()(
gcomponent<layout_item>& c,
void component_inspector<layout>::operator()(
gcomponent<layout>& c,
gizmos_context& ctx) const
{
ctx.draw_wire_rect(
@@ -256,6 +223,9 @@ namespace e2d::layouts
gcomponent<layout> mark_dirty(gcomponent<layout> self) {
if ( self ) {
self.owner().component<layout::dirty>().ensure();
if ( gcomponent<layout> parent = find_parent_layout(self) ) {
parent.owner().component<layout::dirty>().ensure();
}
}
return self;
}
@@ -291,46 +261,29 @@ namespace e2d::layouts
}
return mark_dirty(self);
}
}
namespace e2d::layout_items
{
gcomponent<layout_item> mark_dirty(gcomponent<layout_item> self) {
layouts::mark_dirty(find_parent_layout(self));
return self;
}
gcomponent<layout_item> unmark_dirty(gcomponent<layout_item> self) {
layouts::unmark_dirty(find_parent_layout(self));
return self;
}
bool is_dirty(const const_gcomponent<layout_item>& self) noexcept {
return layouts::is_dirty(find_parent_layout(self));
}
gcomponent<layout_item> change_size(gcomponent<layout_item> self, const v2f& value) {
gcomponent<layout> change_size(gcomponent<layout> self, const v2f& value) {
if ( self ) {
self->size(value);
}
return mark_dirty(self);
}
gcomponent<layout_item> change_margin(gcomponent<layout_item> self, const v2f& value) {
gcomponent<layout> change_margin(gcomponent<layout> 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) {
gcomponent<layout> change_padding(gcomponent<layout> 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> self) noexcept {
const_gcomponent<actor> self_actor = self.owner().component<actor>();
return self_actor
? nodes::find_component_from_parents<layout>(self_actor->node())

View File

@@ -199,7 +199,6 @@ namespace e2d
.register_component<label::dirty>("label.dirty")
.register_component<layout>("layout")
.register_component<layout::dirty>("layout.dirty")
.register_component<layout_item>("layout_item")
.register_component<model_renderer>("model_renderer")
.register_component<named>("named")
.register_component<renderer>("renderer")
@@ -226,7 +225,6 @@ namespace e2d
//.register_component<label::dirty>("label.dirty")
.register_component<layout>("layout")
//.register_component<layout::dirty>("layout.dirty")
.register_component<layout_item>("layout_item")
.register_component<model_renderer>("model_renderer")
.register_component<named>("named")
.register_component<renderer>("renderer")