CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ComponentUI.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
13
15
17 friend class ComponentUIRegistry;
18
19 private:
21 ComponentUI* next = nullptr;
22
25
26 public:
29
31 virtual ~ComponentUI();
32
33 public:
35 [[nodiscard]] virtual String getName() const = 0;
36
41 [[nodiscard]] virtual bool supports(Entity entity) const = 0;
42
45 virtual void render(Entity entity) = 0;
46
49 virtual void addComponent(Entity entity) = 0;
50
53 virtual void removeComponent(Entity entity) = 0;
54 };
55
56 template<typename C, typename EO = EntityObject<C>> class BasicComponentUI : public ComponentUI {
57 public:
59 [[nodiscard]] bool supports(Entity entity) const override { return entity.has<C>(); }
60
62 void render(Entity entity) final { render(entity.get<C>(), entity); }
63
67 virtual void render(C& component, Entity entity) = 0;
68
70 void addComponent(Entity entity) override { EO::make(entity); }
71
73 void removeComponent(Entity entity) override { entity.remove<C>(); }
74 };
75
76 template<typename C, typename EO = EntityObject<C>> class TComponentUI : public ComponentUI {
77 public:
79 std::function<void(C&, EO)> render_;
80 std::function<bool(Entity)> supports_;
81 std::function<void(Entity)> addComponent_;
82 std::function<void(Entity)> removeComponent_;
83
84 public:
85 template<typename Constructor> CE_EXPLICIT(false) TComponentUI(Constructor&& constructor) { // NOLINT
86 name = typeid(C).name();
87 supports_ = [](Entity entity) {
88 return entity.has<C>();
89 };
90 addComponent_ = [](Entity entity) {
91 if constexpr(std::is_default_constructible_v<C>) {
92 entity.add<C>();
93 }
94 };
95 removeComponent_ = [](Entity entity) {
96 entity.remove<C>();
97 };
98 constructor(*this, name, supports_, addComponent_, removeComponent_, render_);
99 }
100
101 public:
103 [[nodiscard]] bool supports(const Entity entity) const override {
104 if(supports_) {
105 return supports_(entity);
106 }
107 return false;
108 }
109
111 void render(Entity entity) override {
112 if(render_) {
113 render_(entity.get<C>(), entity.as<EO>());
114 }
115 }
116
118 void addComponent(const Entity entity) override {
119 if(addComponent_) {
120 addComponent_(entity);
121 }
122 }
123
125 void removeComponent(const Entity entity) override {
126 if(removeComponent_) {
127 removeComponent_(entity);
128 }
129 }
130
133 };
134
136 friend class ComponentUI;
137
138 private:
140 static inline ComponentUI* first = nullptr;
141
143 static inline ComponentUI* last = nullptr;
144
145 public:
146 [[nodiscard]] static bool anySupport(Entity entity);
147 static void renderUI(Entity entity);
148 };
149
150#define CE_EDITOR_COMPONENT_TOKENPASTE(x, y) x##y
151#define CE_EDITOR_COMPONENT_TOKENPASTE2(x, y) CE_EDITOR_COMPONENT_TOKENPASTE(x, y)
152#define CE_EDITOR_COMPONENT(...) \
153 static const ::CeresEngine::Editor::UI::TComponentUI<__VA_ARGS__> CE_EDITOR_COMPONENT_TOKENPASTE2(componentUI, __COUNTER__) [[maybe_unused]] = [ \
154 ](::CeresEngine::Editor::UI::TComponentUI<__VA_ARGS__> & ui, ::CeresEngine::String & name, auto& supports, auto& add, auto& remove, auto& render) noexcept
155#define CE_EDITOR_COMPONENT_ADVANCED(EditorClass) \
156 static const EditorClass CE_EDITOR_COMPONENT_TOKENPASTE2(componentUI, EditorClass) [[maybe_unused]] {}
157#define CE_EDITOR_COMPONENT_ADVANCED_BEGIN(Name, ...) struct Name##ComponentUI final : public ::CeresEngine::Editor::UI::TComponentUI<__VA_ARGS__>
158#define CE_EDITOR_COMPONENT_ADVANCED_DECL(Name) \
159 Name##ComponentUI() : TComponentUI<__VA_ARGS__>(constructor_){}; \
160 static inline const auto constructor_ = [ \
161 ](Name##ComponentUI & ui, ::CeresEngine::String & name, auto& supports, auto& add, auto& remove, auto& render) noexcept
162#define CE_EDITOR_COMPONENT_ADVANCED_END(Name) static const Name##ComponentUI CE_EDITOR_COMPONENT_TOKENPASTE2(componentUI, __COUNTER__) [[maybe_unused]]{};
163
164} // namespace CeresEngine::Editor::UI
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
Definition ComponentUI.hpp:56
void removeComponent(Entity entity) override
Removes the component from the entity.
Definition ComponentUI.hpp:73
virtual void render(C &component, Entity entity)=0
Renders a UI for the component of the given entity.
bool supports(Entity entity) const override
Checks if the given entity is supported by this component UI.
Definition ComponentUI.hpp:59
void addComponent(Entity entity) override
Adds the component to the entity.
Definition ComponentUI.hpp:70
void render(Entity entity) final
Renders a UI for the component of the given entity.
Definition ComponentUI.hpp:62
Definition ComponentUI.hpp:16
ComponentUI * previous
The previous component UI in the chain.
Definition ComponentUI.hpp:24
virtual String getName() const =0
ComponentUI * next
The next component UI in the chain.
Definition ComponentUI.hpp:21
ComponentUI()
Creates a new ComponentUI object.
virtual void removeComponent(Entity entity)=0
Removes the component from the entity.
virtual void addComponent(Entity entity)=0
Adds the component to the entity.
virtual ~ComponentUI()
Destroys the ComponentUI object.
virtual void render(Entity entity)=0
Renders a UI for the component of the given entity.
virtual bool supports(Entity entity) const =0
Checks if the given entity is supported by this component UI.
Definition ComponentUI.hpp:135
static ComponentUI * last
The last component UI in the linked list.
Definition ComponentUI.hpp:143
static ComponentUI * first
The first component UI in the linked list.
Definition ComponentUI.hpp:140
Definition ComponentUI.hpp:76
void addComponent(const Entity entity) override
Adds the component to the entity.
Definition ComponentUI.hpp:118
void removeComponent(const Entity entity) override
Removes the component from the entity.
Definition ComponentUI.hpp:125
String getName() const final
Definition ComponentUI.hpp:132
bool supports(const Entity entity) const override
Checks if the given entity is supported by this component UI.
Definition ComponentUI.hpp:103
void render(Entity entity) override
Renders a UI for the component of the given entity.
Definition ComponentUI.hpp:111
std::function< void(Entity)> removeComponent_
Definition ComponentUI.hpp:82
std::function< void(C &, EO)> render_
Definition ComponentUI.hpp:79
std::function< bool(Entity)> supports_
Definition ComponentUI.hpp:80
String name
Definition ComponentUI.hpp:78
std::function< void(Entity)> addComponent_
Definition ComponentUI.hpp:81
The base entity class.
Definition Entity.hpp:41
AbstractComponent & get(const ComponentType &type) const
Gets a component of the given type.
E as() const
Converts the entity handle type into a E entity object.
void remove(const ComponentType &type) const
Removes a component of the given type from the entity.
bool has(const ComponentType &type) const noexcept
Checks if the entity has all components of the given type.
Definition ComponentUI.hpp:14
@ Constructor
Indicates that the item is a constructor.
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25