forked from TomohikoMukai/ssdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.h
131 lines (121 loc) · 2.85 KB
/
Object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef OBJECT_H
#define OBJECT_H
#pragma once
#include <memory>
#include <vector>
#include <d3d11.h>
#include <DirectXMath.h>
struct Object
{
public:
using SharedPtr = std::shared_ptr<Object>;
using WeakPtr = std::weak_ptr<Object>;
using UniquePtr = std::unique_ptr<Object>;
protected:
WeakPtr self;
WeakPtr parent;
WeakPtr root;
std::vector<SharedPtr> children;
bool isVisible;
public:
static SharedPtr CreateInstance()
{
SharedPtr s(new Object);
s->self = s;
return s;
}
virtual ~Object()
{
}
public:
bool IsVisible() const
{
return isVisible;
}
virtual void Show()
{
isVisible = true;
}
virtual void Hide()
{
isVisible = false;
}
public:
int NumChildren() const
{
return static_cast<int>(children.size());
}
SharedPtr Child(int id)
{
return children[id];
}
public:
void AddChild(SharedPtr child)
{
children.push_back(child);
child->parent = self;
child->SetRoot(parent.lock() == nullptr ? self.lock() : root.lock());
}
void SetRoot(SharedPtr r)
{
if (root.lock() == r)
{
return;
}
root = r;
for (auto it = children.begin(); it != children.end(); ++it)
{
(*it)->SetRoot(r);
}
}
public:
virtual bool OnInit(ID3D11Device* device, ID3D11DeviceContext* deviceContext, const UINT width, const UINT height)
{
for (auto it = children.begin(); it != children.end(); ++it)
{
(*it)->OnInit(device, deviceContext, width, height);
}
return true;
}
virtual void OnDestroy()
{
for (auto it = children.begin(); it != children.end(); ++it)
{
(*it)->OnDestroy();
}
children.clear();
parent.reset();
root.reset();
self.reset();
}
virtual void OnRender(ID3D11Device* device, ID3D11DeviceContext* deviceContext)
{
for (auto it = children.begin(); it != children.end(); ++it)
{
(*it)->OnRender(device, deviceContext);
}
}
virtual void OnResize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, const UINT width, const UINT height)
{
for (auto it = children.begin(); it != children.end(); ++it)
{
(*it)->OnResize(device, deviceContext, width, height);
}
}
virtual void OnUpdate(ID3D11Device* device, ID3D11DeviceContext* deviceContext, float elapsed)
{
for (auto it = children.begin(); it != children.end(); ++it)
{
(*it)->OnUpdate(device, deviceContext, elapsed);
}
}
protected:
Object()
: isVisible(true)
{
}
private:
Object(const Object& src);
void operator =(const Object& src);
};
#endif //OBJECT_H