diff --git a/headers/enduro2d/high/components/layout.hpp b/headers/enduro2d/high/components/layout.hpp index f9839db7..efee1edd 100644 --- a/headers/enduro2d/high/components/layout.hpp +++ b/headers/enduro2d/high/components/layout.hpp @@ -11,8 +11,17 @@ namespace e2d { class layout final { + public: + ENUM_HPP_CLASS_DECL(modes, u8, + (vertical) + (horizontal)) public: layout() = default; + + layout& mode(modes value) noexcept; + [[nodiscard]] modes mode() const noexcept; + private: + modes mode_ = modes::vertical; }; class layout_item final { @@ -21,6 +30,8 @@ namespace e2d }; } +ENUM_HPP_REGISTER_TRAITS(e2d::layout::modes) + namespace e2d { template <> @@ -73,4 +84,12 @@ namespace e2d namespace e2d { + inline layout& layout::mode(modes value) noexcept { + mode_ = value; + return *this; + } + + inline layout::modes layout::mode() const noexcept { + return mode_; + } } diff --git a/samples/bin/library/scripts/emmy/components/layout.lua b/samples/bin/library/scripts/emmy/components/layout.lua index 68587b7c..fbf6ce5a 100644 --- a/samples/bin/library/scripts/emmy/components/layout.lua +++ b/samples/bin/library/scripts/emmy/components/layout.lua @@ -1,11 +1,64 @@ +-- ----------------------------------------------------------------------------- +-- +-- layout +-- +-- ----------------------------------------------------------------------------- + ---@class layout local layout = { + ---@type boolean + enabled = true, + + ---@type boolean + disabled = false, + + ---@type layout_modes + mode = layout.modes.vertical, } +---@class layout_modes +layout.modes = { + vertical = "vertical", + horizontal = "horizontal" +} + +---@overload fun(self: layout) +---@param self layout +function layout.enable(self) end + +---@overload fun(self: layout) +---@param self layout +function layout.disable(self) end + +-- ----------------------------------------------------------------------------- +-- +-- layout_item +-- +-- ----------------------------------------------------------------------------- + ---@class layout_item local layout_item = { + ---@type boolean + enabled = true, + + ---@type boolean + disabled = false } +---@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 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 a8298271..8078c4de 100644 --- a/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/components/layout_binds.cpp @@ -7,17 +7,102 @@ #include "../_high_binds.hpp" #include +#include #include namespace e2d::bindings::high { void bind_layout(sol::state& l) { l.new_usertype>("layout", - sol::no_constructor + sol::no_constructor, + + "enable", [](gcomponent& c){ + c.owner().component>().remove(); + }, + + "disable", [](gcomponent& c){ + c.owner().component>().ensure(); + }, + + "enabled", sol::property( + [](const gcomponent& c) -> bool { + return !c.owner().component>().exists(); + }, + [](gcomponent& c, bool yesno){ + if ( yesno ) { + c.owner().component>().remove(); + } else { + c.owner().component>().ensure(); + } + } + ), + + "disabled", sol::property( + [](const gcomponent& c) -> bool { + return c.owner().component>().exists(); + }, + [](gcomponent& c, bool yesno){ + if ( yesno ) { + c.owner().component>().ensure(); + } else { + c.owner().component>().remove(); + } + } + ), + + "mode", sol::property( + [](const gcomponent& c) -> layout::modes { + return c->mode(); + }, + [](gcomponent& c, layout::modes v){ + c->mode(v); + }) ); + #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 + l.new_usertype>("layout_item", - sol::no_constructor + sol::no_constructor, + + "enable", [](gcomponent& c){ + c.owner().component>().remove(); + }, + + "disable", [](gcomponent& c){ + c.owner().component>().ensure(); + }, + + "enabled", sol::property( + [](const gcomponent& c) -> bool { + return !c.owner().component>().exists(); + }, + [](gcomponent& c, bool yesno){ + if ( yesno ) { + c.owner().component>().remove(); + } else { + c.owner().component>().ensure(); + } + } + ), + + "disabled", sol::property( + [](const gcomponent& c) -> bool { + return c.owner().component>().exists(); + }, + [](gcomponent& c, bool yesno){ + if ( yesno ) { + c.owner().component>().ensure(); + } else { + c.owner().component>().remove(); + } + } + ) ); } } diff --git a/sources/enduro2d/high/components/layout.cpp b/sources/enduro2d/high/components/layout.cpp index 6d4aac77..976ff7c1 100644 --- a/sources/enduro2d/high/components/layout.cpp +++ b/sources/enduro2d/high/components/layout.cpp @@ -12,14 +12,33 @@ namespace e2d "type" : "object", "required" : [], "additionalProperties" : false, - "properties" : {} + "properties" : { + "mode" : { "$ref": "#/definitions/modes" } + }, + "definitions" : { + "modes" : { + "type" : "string", + "enum" : [ + "vertical", + "horizontal" + ] + } + } })json"; bool factory_loader::operator()( layout& component, const fill_context& ctx) const { - E2D_UNUSED(component, ctx); + 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); + } + return true; } @@ -63,7 +82,11 @@ namespace e2d const char* component_inspector::title = ICON_FA_BARS " layout"; void component_inspector::operator()(gcomponent& c) const { - E2D_UNUSED(c); + if ( layout::modes mode = c->mode(); + imgui_utils::show_enum_combo_box("mode", &mode) ) + { + c->mode(mode); + } } }