Elevate Engine 1
Loading...
Searching...
No Matches
ComponentCommand.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace Elevate
9{
11 {
12 public:
13 RemoveComponentCommand(Component* comp) : m_component(comp)
14 {
15 if (m_component)
16 {
17 try {
18 m_obj = comp->gameObject->shared_from_this();
19 }
20 catch (...) {}
21
22 m_backup.reset(m_component->Clone());
23 m_factory = m_component->GetFactory();
24 m_destructor = m_component->GetDestructor();
25 }
26 }
27 private:
28 std::weak_ptr<GameObject> m_obj;
29 Component* m_component = nullptr;
30 std::unique_ptr<Component> m_backup;
33
34 virtual void Execute() override
35 {
36 if (!m_obj.expired() && m_destructor) {
37 m_destructor(m_obj);
38 }
39 }
40
41 virtual void Undo() override
42 {
43 if (m_backup && !m_obj.expired() && m_factory)
44 {
45 Component* created = m_factory(m_obj);
46 if (created)
47 {
48 created->CopyFrom(m_backup.get());
49 }
50 }
51 }
52 };
53
55 {
56 public:
58 std::weak_ptr<GameObject> object,
61 : m_obj(object), m_factory(factory), m_destructor(destructor) { }
62 private:
63 std::weak_ptr<GameObject> m_obj;
66
67 virtual void Execute() override
68 {
69 EE_CORE_TRACE("AddComponentCommand");
70 if (!m_obj.expired() && m_factory)
71 {
72 m_factory(m_obj);
73 }
74 }
75
76 virtual void Undo() override
77 {
78 if (!m_obj.expired() && m_destructor)
79 {
80 m_destructor(m_obj);
81 }
82 }
83 };
84}
AddComponentCommand(std::weak_ptr< GameObject > object, GameObjectComponentFactory factory, GameObjectComponentDestructor destructor)
virtual Component * Clone()=0
virtual GameObjectComponentDestructor GetDestructor() const =0
GameObject * gameObject
Definition Component.h:73
virtual GameObjectComponentFactory GetFactory() const =0
std::function< Component *(std::weak_ptr< GameObject >)> GameObjectComponentFactory
Definition Component.h:21
std::function< void(std::weak_ptr< GameObject >)> GameObjectComponentDestructor
Definition Component.h:22