Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
SGNComponent.h
Go to the documentation of this file.
1/* Copyright (c) 2018 DIVIDE-Studio
2Copyright (c) 2009 Ionut Cava
3
4This file is part of DIVIDE Framework.
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software
8and associated documentation files (the "Software"), to deal in the Software
9without restriction,
10including without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense,
12and/or sell copies of the Software, and to permit persons to whom the Software
13is furnished to do so,
14subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
22PARTICULAR PURPOSE AND NONINFRINGEMENT.
23IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24DAMAGES OR OTHER LIABILITY,
25WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26CONNECTION WITH THE SOFTWARE
27OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29*/
30
31#pragma once
32#ifndef DVD_SGN_COMPONENT_H_
33#define DVD_SGN_COMPONENT_H_
34
35#include "EditorComponent.h"
38#include <ECS/Component.h>
39
40namespace Divide {
41
43enum class RenderStage : U8;
44class SceneGraphNode;
45class SGNComponent;
46class SceneRenderState;
47struct RenderStagePass;
48
49} //namespace Divide
50
51namespace ECS {
52 struct CustomEvent {
53 enum class Type : Divide::U8 {
62 COUNT
63 };
64
68
69 struct DataPair
70 {
73 };
74
75 union
76 {
79 };
80 };
81}
82
83namespace Divide {
84
85template<class T, typename... Args>
86void AddSGNComponent(SceneGraphNode* node, Args... args);
87template<class T>
88void RemoveSGNComponent(SceneGraphNode* node);
89
90//ref: http://www.nirfriedman.com/2018/04/29/unforgettable-factory/
91template <typename Base, typename... Args>
92struct Factory {
93 using ConstructFunc = DELEGATE_STD<void, SceneGraphNode*, Args...>;
95 using FactoryContainerConstruct = ska::bytell_hash_map<ComponentType, ConstructFunc>;
96 using FactoryContainerDestruct = ska::bytell_hash_map<ComponentType, DestructFunc>;
97
98 template <typename... ConstructArgs>
99 static void construct(ComponentType type, SceneGraphNode* node, ConstructArgs&&... args) {
100 constructData().at(type)(node, FWD(args)...);
101 }
102
103 static void destruct(const ComponentType type, SceneGraphNode* node) {
104 destructData().at(type)(node);
105 }
106
107 template <typename T, ComponentType C>
109 Base
110 {
111 template<typename... InnerArgs>
112 Registrar(InnerArgs&&... args)
113 : Base(Key{ s_registered }, C, FWD(args)...)
114 {
115 }
116
117 void OnData([[maybe_unused]] const ECS::CustomEvent& data) override {}
118
120 {
121 Factory::constructData().emplace(C,
122 []( SceneGraphNode* node, Args... args ) -> void
123 {
124 AddSGNComponent<T, Args...>(node, FWD(args)...);
125 });
126
127 Factory::destructData().emplace(C,
128 []( SceneGraphNode* node ) -> void
129 {
130 RemoveSGNComponent<T>( node );
131 } );
132 return true;
133 }
134
135 static bool s_registered;
136
137 friend T;
138 };
139
140 friend Base;
141
142private:
143 struct Key {
144 Key(const bool registered) noexcept : _registered(registered) {}
145
146 private:
147 bool _registered = false;
148
149 template <typename T, ComponentType C>
150 friend struct Registrar;
151 };
152
153 Factory() = default;
154
156 {
157 NO_DESTROY static FactoryContainerConstruct container;
158 return container;
159 }
160
162 {
163 NO_DESTROY static FactoryContainerDestruct container;
164 return container;
165 }
166};
167
168template <typename Base, typename... Args>
169template <typename T, ComponentType C>
170bool Factory<Base, Args...>::Registrar<T, C>::s_registered = RegisterComponentType();
171
172struct EntityOnUpdate;
173
175 public Factory<SGNComponent>
176{
177 public:
178 explicit SGNComponent(Key key, ComponentType type, SceneGraphNode* parentSGN, PlatformContext& context);
179
180 virtual void OnData(const ECS::CustomEvent& data);
181
182 virtual void saveToXML(boost::property_tree::ptree& pt) const;
183 virtual void loadFromXML(const boost::property_tree::ptree& pt);
184
185 virtual bool saveCache(ByteBuffer& outputBuffer) const;
186 virtual bool loadCache(ByteBuffer& inputBuffer);
187
188 [[nodiscard]] U64 uniqueID() const;
189
190 [[nodiscard]] virtual bool enabled() const;
191 virtual void enabled(bool state);
192
193 [[nodiscard]] EditorComponent& editorComponent() noexcept { return _editorComponent; }
194
195 POINTER_R_IW(SceneGraphNode, parentSGN, nullptr);
198
199 protected:
200 friend class EditorComponent;
201
202 std::atomic_bool _enabled{true};
203 mutable std::atomic_bool _hasChanged{false};
204};
205
206template<typename T, ComponentType C>
207using BaseComponentType = SGNComponent::Registrar<T, C>;
208
209#define INIT_COMPONENT(X) static bool X##_registered = X::s_registered
210
211#define DVD_COMPONENT_SIGNATURE(Name, Enum) class Name##Component final : public BaseComponentType<Name##Component, Enum>
212
213#define DVD_COMPONENT_PARENT(Name, Enum) using Parent = BaseComponentType<Name##Component, Enum>; \
214 friend class Name##System;
215#define BEGIN_COMPONENT(Name, Enum) \
216 DVD_COMPONENT_SIGNATURE(Name, Enum) { \
217 DVD_COMPONENT_PARENT(Name, Enum)
218
219#define BEGIN_COMPONENT_EXT1(Name, Enum, Base1) \
220 DVD_COMPONENT_SIGNATURE(Name, Enum) , public Base1 { \
221 DVD_COMPONENT_PARENT(Name, Enum)
222
223#define END_COMPONENT(Name) \
224 }; \
225 INIT_COMPONENT(Name##Component);
226
227} // namespace Divide
228#endif //DVD_SGN_COMPONENT_H_
229
230#include "SGNComponent.inl"
#define FWD(...)
#define NO_DESTROY
PlatformContext & context() noexcept
POINTER_R_IW(SceneGraphNode, parentSGN, nullptr)
virtual void saveToXML(boost::property_tree::ptree &pt) const
virtual bool enabled() const
std::atomic_bool _enabled
Definition: SGNComponent.h:202
virtual void loadFromXML(const boost::property_tree::ptree &pt)
PROPERTY_R(EditorComponent, editorComponent)
virtual void OnData(const ECS::CustomEvent &data)
virtual bool saveCache(ByteBuffer &outputBuffer) const
EditorComponent & editorComponent() noexcept
Definition: SGNComponent.h:193
PROPERTY_R_IW(ComponentType, type, ComponentType::COUNT)
std::atomic_bool _hasChanged
Definition: SGNComponent.h:203
virtual bool loadCache(ByteBuffer &inputBuffer)
constexpr T Base(T a)
Base value.
Definition: MathHelper.inl:503
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::function< Ret(Args...) > DELEGATE_STD
uint8_t U8
uint16_t U16
SGNComponent::Registrar< T, C > BaseComponentType
Definition: SGNComponent.h:207
void AddSGNComponent(SceneGraphNode *node, Args... args)
void RemoveSGNComponent(SceneGraphNode *node)
uint32_t U32
uint64_t U64
Key(const bool registered) noexcept
Definition: SGNComponent.h:144
void OnData(const ECS::CustomEvent &data) override
Definition: SGNComponent.h:117
static bool RegisterComponentType()
Definition: SGNComponent.h:119
Registrar(InnerArgs &&... args)
Definition: SGNComponent.h:112
static void destruct(const ComponentType type, SceneGraphNode *node)
Definition: SGNComponent.h:103
static FactoryContainerConstruct & constructData()
Definition: SGNComponent.h:155
DELEGATE_STD< void, SceneGraphNode *, Args... > ConstructFunc
Definition: SGNComponent.h:93
ska::bytell_hash_map< ComponentType, ConstructFunc > FactoryContainerConstruct
Definition: SGNComponent.h:95
Factory()=default
DELEGATE_STD< void, SceneGraphNode * > DestructFunc
Definition: SGNComponent.h:94
static void construct(ComponentType type, SceneGraphNode *node, ConstructArgs &&... args)
Definition: SGNComponent.h:99
static FactoryContainerDestruct & destructData()
Definition: SGNComponent.h:161
ska::bytell_hash_map< ComponentType, DestructFunc > FactoryContainerDestruct
Definition: SGNComponent.h:96
DataPair _dataPair
Definition: SGNComponent.h:77
Divide::U32 _data
Definition: SGNComponent.h:78
Divide::U32 _flag
Definition: SGNComponent.h:67
Divide::SGNComponent * _sourceCmp
Definition: SGNComponent.h:66