From 18894c9cfc8be06cb37ba0ac54b282fda3aba6b6 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 13 Feb 2020 14:53:50 +0700 Subject: [PATCH] more layout direction types --- headers/enduro2d/high/components/layout.hpp | 40 ++++++------ .../scripts/emmy/components/layout.lua | 20 +++--- .../high_binds/components/layout_binds.cpp | 34 +++++----- sources/enduro2d/high/components/layout.cpp | 62 ++++++++++--------- .../enduro2d/high/systems/layout_system.cpp | 26 ++++++-- 5 files changed, 103 insertions(+), 79 deletions(-) diff --git a/headers/enduro2d/high/components/layout.hpp b/headers/enduro2d/high/components/layout.hpp index 29c04b07..a7aae13b 100644 --- a/headers/enduro2d/high/components/layout.hpp +++ b/headers/enduro2d/high/components/layout.hpp @@ -14,10 +14,6 @@ namespace e2d public: class dirty final {}; public: - ENUM_HPP_CLASS_DECL(modes, u8, - (horizontal) - (vertical)) - ENUM_HPP_CLASS_DECL(haligns, u8, (left) (center) @@ -33,17 +29,23 @@ namespace e2d (space_around) (space_evenly) (space_between)) + + ENUM_HPP_CLASS_DECL(directions, u8, + (row) + (row_reversed) + (column) + (column_reversed)) public: layout() = default; - layout& mode(modes value) noexcept; - [[nodiscard]] modes mode() const noexcept; - layout& halign(haligns value) noexcept; [[nodiscard]] haligns halign() const noexcept; layout& valign(valigns value) noexcept; [[nodiscard]] valigns valign() const noexcept; + + layout& direction(directions value) noexcept; + [[nodiscard]] directions direction() const noexcept; public: layout& size(const v2f& value) noexcept; [[nodiscard]] const v2f& size() const noexcept; @@ -54,9 +56,9 @@ namespace e2d 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; + directions direction_ = directions::row; private: v2f size_ = v2f::zero(); v2f margin_ = v2f::zero(); @@ -64,9 +66,9 @@ namespace e2d }; } -ENUM_HPP_REGISTER_TRAITS(e2d::layout::modes) ENUM_HPP_REGISTER_TRAITS(e2d::layout::haligns) ENUM_HPP_REGISTER_TRAITS(e2d::layout::valigns) +ENUM_HPP_REGISTER_TRAITS(e2d::layout::directions) namespace e2d { @@ -113,15 +115,6 @@ namespace e2d namespace e2d { - inline layout& layout::mode(modes value) noexcept { - mode_ = value; - return *this; - } - - inline layout::modes layout::mode() const noexcept { - return mode_; - } - inline layout& layout::halign(haligns value) noexcept { halign_ = value; return *this; @@ -140,6 +133,15 @@ namespace e2d return valign_; } + inline layout& layout::direction(directions value) noexcept { + direction_ = value; + return *this; + } + + inline layout::directions layout::direction() const noexcept { + return direction_; + } + inline layout& layout::size(const v2f& value) noexcept { size_ = value; return *this; @@ -174,9 +176,9 @@ namespace e2d::layouts gcomponent unmark_dirty(gcomponent self); bool is_dirty(const const_gcomponent& self) noexcept; - 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_direction(gcomponent self, layout::directions value); gcomponent change_size(gcomponent self, const v2f& value); gcomponent change_margin(gcomponent self, const v2f& value); diff --git a/samples/bin/library/scripts/emmy/components/layout.lua b/samples/bin/library/scripts/emmy/components/layout.lua index 3722fb64..3dcc1b61 100644 --- a/samples/bin/library/scripts/emmy/components/layout.lua +++ b/samples/bin/library/scripts/emmy/components/layout.lua @@ -9,15 +9,15 @@ local layout = { ---@type boolean dirty = false, - ---@type layout_modes - mode = layout.modes.horizontal, - ---@type layout_haligns halign = layout.haligns.center, ---@type layout_valigns valign = layout.valigns.center, + ---@type layout_directions + direction = layout.directions.row, + ---@type v2f size = v2f.zero(), @@ -28,12 +28,6 @@ local layout = { padding = v2f.zero() } ----@class layout_modes -layout.modes = { - horizontal = "horizontal", - vertical = "vertical" -} - ---@class layout_haligns layout.haligns = { left = "left", @@ -54,6 +48,14 @@ layout.valigns = { space_between = "space_between" } +---@class layout_directions +layout.directions = { + row = "row", + row_reversed = "row_reversed", + column = "column", + column_reversed = "column_reversed" +} + ---@overload fun(self: layout) ---@param self layout function layout.enable(self) end 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 8cc128fa..028db8ca 100644 --- a/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp @@ -67,14 +67,6 @@ namespace e2d::bindings::high } ), - "mode", sol::property( - [](const gcomponent& c) -> layout::modes { - return c->mode(); - }, - [](gcomponent& c, layout::modes v){ - layouts::change_mode(c, v); - }), - "halign", sol::property( [](const gcomponent& c) -> layout::haligns { return c->halign(); @@ -91,6 +83,14 @@ namespace e2d::bindings::high layouts::change_valign(c, v); }), + "direction", sol::property( + [](const gcomponent& c) -> layout::directions { + return c->direction(); + }, + [](gcomponent& c, layout::directions v){ + layouts::change_direction(c, v); + }), + "size", sol::property( [](const gcomponent& c) -> v2f { return c->size(); @@ -116,14 +116,6 @@ namespace e2d::bindings::high }) ); - #define LAYOUT_MODE_PAIR(x) {#x, layout::modes::x}, - l["layout"].get_or_create() - .new_enum("modes", { - LAYOUT_MODE_PAIR(vertical) - LAYOUT_MODE_PAIR(horizontal) - }); - #undef LAYOUT_MODE_PAIR - #define LAYOUT_HALIGN_PAIR(x) {#x, layout::haligns::x}, l["layout"].get_or_create() .new_enum("haligns", { @@ -147,5 +139,15 @@ namespace e2d::bindings::high LAYOUT_VALIGN_PAIR(space_between) }); #undef LAYOUT_VALIGN_PAIR + + #define LAYOUT_DIRECTION_PAIR(x) {#x, layout::directions::x}, + l["layout"].get_or_create() + .new_enum("directions", { + LAYOUT_DIRECTION_PAIR(row) + LAYOUT_DIRECTION_PAIR(row_reversed) + LAYOUT_DIRECTION_PAIR(column) + LAYOUT_DIRECTION_PAIR(column_reversed) + }); + #undef LAYOUT_DIRECTION_PAIR } } diff --git a/sources/enduro2d/high/components/layout.cpp b/sources/enduro2d/high/components/layout.cpp index cafadbde..1034c734 100644 --- a/sources/enduro2d/high/components/layout.cpp +++ b/sources/enduro2d/high/components/layout.cpp @@ -15,21 +15,14 @@ namespace e2d "required" : [], "additionalProperties" : false, "properties" : { - "mode" : { "$ref": "#/definitions/modes" }, "halign" : { "$ref": "#/definitions/haligns" }, "valign" : { "$ref": "#/definitions/valigns" }, + "direction" : { "$ref": "#/definitions/directions" }, "size" : { "$ref": "#/common_definitions/v2" }, "margin" : { "$ref": "#/common_definitions/v2" }, "padding" : { "$ref": "#/common_definitions/v2" } }, "definitions" : { - "modes" : { - "type" : "string", - "enum" : [ - "horizontal", - "vertical" - ] - }, "haligns" : { "type" : "string", "enum" : [ @@ -51,6 +44,15 @@ namespace e2d "space_evenly", "space_between" ] + }, + "directions" : { + "type" : "string", + "enum" : [ + "row", + "row_reversed", + "column", + "column_reversed" + ] } } })json"; @@ -59,15 +61,6 @@ namespace e2d layout& component, const fill_context& ctx) const { - if ( ctx.root.HasMember("mode") ) { - layout::modes mode = component.mode(); - if ( !json_utils::try_parse_value(ctx.root["mode"], mode) ) { - the().error("LAYOUT: Incorrect formatting of 'mode' property"); - return false; - } - component.mode(mode); - } - if ( ctx.root.HasMember("halign") ) { layout::haligns halign = component.halign(); if ( !json_utils::try_parse_value(ctx.root["halign"], halign) ) { @@ -86,6 +79,15 @@ namespace e2d component.valign(valign); } + if ( ctx.root.HasMember("direction") ) { + layout::directions direction = component.direction(); + if ( !json_utils::try_parse_value(ctx.root["direction"], direction) ) { + the().error("LAYOUT: Incorrect formatting of 'direction' property"); + return false; + } + component.direction(direction); + } + if ( ctx.root.HasMember("size") ) { v2f size = component.size(); if ( !json_utils::try_parse_value(ctx.root["size"], size) ) { @@ -168,12 +170,6 @@ namespace e2d ImGui::Separator(); - if ( layout::modes mode = c->mode(); - imgui_utils::show_enum_combo_box("mode", &mode) ) - { - layouts::change_mode(c, mode); - } - if ( layout::haligns halign = c->halign(); imgui_utils::show_enum_combo_box("halign", &halign) ) { @@ -186,6 +182,12 @@ namespace e2d layouts::change_valign(c, valign); } + if ( layout::directions direction = c->direction(); + imgui_utils::show_enum_combo_box("direction", &direction) ) + { + layouts::change_direction(c, direction); + } + if ( v2f size = c->size(); ImGui::DragFloat2("size", size.data(), 1.f) ) { @@ -252,13 +254,6 @@ namespace e2d::layouts return self.owner().component().exists(); } - gcomponent change_mode(gcomponent self, layout::modes value) { - if ( self ) { - self->mode(value); - } - return mark_dirty(self); - } - gcomponent change_halign(gcomponent self, layout::haligns value) { if ( self ) { self->halign(value); @@ -273,6 +268,13 @@ namespace e2d::layouts return mark_dirty(self); } + gcomponent change_direction(gcomponent self, layout::directions value) { + if ( self ) { + self->direction(value); + } + return mark_dirty(self); + } + gcomponent change_size(gcomponent self, const v2f& value) { if ( self ) { self->size(value); diff --git a/sources/enduro2d/high/systems/layout_system.cpp b/sources/enduro2d/high/systems/layout_system.cpp index b6ff3ad6..944c6dc9 100644 --- a/sources/enduro2d/high/systems/layout_system.cpp +++ b/sources/enduro2d/high/systems/layout_system.cpp @@ -28,10 +28,27 @@ namespace using namespace e2d; void update_yogo_node(const yogo_node& yn, const layout& l, const actor& a) { - switch ( l.mode() ) { - case layout::modes::horizontal: + switch ( l.direction() ) { + case layout::directions::row: YGNodeStyleSetFlexDirection(yn.as_root.get(), YGFlexDirectionRow); + break; + case layout::directions::row_reversed: + YGNodeStyleSetFlexDirection(yn.as_root.get(), YGFlexDirectionRowReverse); + break; + case layout::directions::column: + YGNodeStyleSetFlexDirection(yn.as_root.get(), YGFlexDirectionColumn); + break; + case layout::directions::column_reversed: + YGNodeStyleSetFlexDirection(yn.as_root.get(), YGFlexDirectionColumnReverse); + break; + default: + E2D_ASSERT_MSG(false, "unexpected layout direction"); + break; + } + switch ( l.direction() ) { + case layout::directions::row: + case layout::directions::row_reversed: switch ( l.halign() ) { case layout::haligns::left: YGNodeStyleSetJustifyContent(yn.as_root.get(), YGJustifyFlexStart); @@ -75,9 +92,8 @@ namespace } break; - case layout::modes::vertical: - YGNodeStyleSetFlexDirection(yn.as_root.get(), YGFlexDirectionColumn); - + case layout::directions::column: + case layout::directions::column_reversed: switch ( l.valign() ) { case layout::valigns::top: YGNodeStyleSetJustifyContent(yn.as_root.get(), YGJustifyFlexEnd);