From a1b30761ce081ca192cf885fe79faea43d690182 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 30 Oct 2019 19:51:54 +0700 Subject: [PATCH] new named component --- headers/enduro2d/high/_all.hpp | 1 + headers/enduro2d/high/_high.hpp | 1 + headers/enduro2d/high/components/named.hpp | 56 ++++++++++++++++++++++ headers/enduro2d/utils/_utils.hpp | 12 +++++ sources/enduro2d/high/components/named.cpp | 43 +++++++++++++++++ sources/enduro2d/high/starter.cpp | 2 + 6 files changed, 115 insertions(+) create mode 100644 headers/enduro2d/high/components/named.hpp create mode 100644 sources/enduro2d/high/components/named.cpp diff --git a/headers/enduro2d/high/_all.hpp b/headers/enduro2d/high/_all.hpp index 0d924c07..d13de17b 100644 --- a/headers/enduro2d/high/_all.hpp +++ b/headers/enduro2d/high/_all.hpp @@ -35,6 +35,7 @@ #include "components/flipbook_player.hpp" #include "components/label.hpp" #include "components/model_renderer.hpp" +#include "components/named.hpp" #include "components/renderer.hpp" #include "components/scene.hpp" #include "components/spine_player.hpp" diff --git a/headers/enduro2d/high/_high.hpp b/headers/enduro2d/high/_high.hpp index 3d355404..88eaddcb 100644 --- a/headers/enduro2d/high/_high.hpp +++ b/headers/enduro2d/high/_high.hpp @@ -51,6 +51,7 @@ namespace e2d class flipbook_player; class label; class model_renderer; + class named; class renderer; class scene; class spine_player; diff --git a/headers/enduro2d/high/components/named.hpp b/headers/enduro2d/high/components/named.hpp new file mode 100644 index 00000000..5702b0fc --- /dev/null +++ b/headers/enduro2d/high/components/named.hpp @@ -0,0 +1,56 @@ +/******************************************************************************* + * This file is part of the "Enduro2D" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include "../_high.hpp" + +#include "../factory.hpp" + +namespace e2d +{ + class named final { + public: + named() = default; + + named& name(str value) noexcept; + + [[nodiscard]] str& name() noexcept; + [[nodiscard]] const str& name() const noexcept; + private: + str name_; + }; + + template <> + class factory_loader final : factory_loader<> { + public: + static const char* schema_source; + + bool operator()( + named& component, + const fill_context& ctx) const; + + bool operator()( + asset_dependencies& dependencies, + const collect_context& ctx) const; + }; +} + +namespace e2d +{ + inline named& named::name(str value) noexcept { + name_ = std::move(value); + return *this; + } + + inline str& named::name() noexcept { + return name_; + } + + inline const str& named::name() const noexcept { + return name_; + } +} diff --git a/headers/enduro2d/utils/_utils.hpp b/headers/enduro2d/utils/_utils.hpp index b2ffdd5b..12b07ac1 100644 --- a/headers/enduro2d/utils/_utils.hpp +++ b/headers/enduro2d/utils/_utils.hpp @@ -200,4 +200,16 @@ namespace e2d::utils return self_id; } }; + + // + // overloaded + // + + template < typename... Ts > + struct overloaded : Ts... { + using Ts::operator()...; + }; + + template < typename... Ts > + overloaded(Ts...) -> overloaded; } diff --git a/sources/enduro2d/high/components/named.cpp b/sources/enduro2d/high/components/named.cpp new file mode 100644 index 00000000..b669e1e4 --- /dev/null +++ b/sources/enduro2d/high/components/named.cpp @@ -0,0 +1,43 @@ +/******************************************************************************* + * This file is part of the "Enduro2D" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#include + +namespace e2d +{ + const char* factory_loader::schema_source = R"json({ + "type" : "object", + "required" : [], + "additionalProperties" : false, + "properties" : { + "name" : { "$ref": "#/common_definitions/name" } + } + })json"; + + bool factory_loader::operator()( + named& component, + const fill_context& ctx) const + { + if ( ctx.root.HasMember("name") ) { + str name = component.name(); + if ( !json_utils::try_parse_value(ctx.root["name"], name) ) { + the().error("NAMED: Incorrect formatting of 'name' property"); + return false; + } + component.name(name); + } + + return true; + } + + bool factory_loader::operator()( + asset_dependencies& dependencies, + const collect_context& ctx) const + { + E2D_UNUSED(dependencies, ctx); + return true; + } +} diff --git a/sources/enduro2d/high/starter.cpp b/sources/enduro2d/high/starter.cpp index d98310b2..d5fe0d12 100644 --- a/sources/enduro2d/high/starter.cpp +++ b/sources/enduro2d/high/starter.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -183,6 +184,7 @@ namespace e2d .register_component