-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.cpp
More file actions
137 lines (135 loc) · 4 KB
/
Group.cpp
File metadata and controls
137 lines (135 loc) · 4 KB
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
132
133
134
135
136
137
#include <stdexcept>
#include <utility>
#include "Group.hpp"
#include "IControl.hpp"
#include "IObject.hpp"
namespace Engine {
void Group::addObject(bool shouldDelete, IObject* obj) {
objects.emplace_back(shouldDelete, obj);
obj->objectIterator = std::prev(objects.end());
}
void Group::addControl(bool shouldDelete, IControl* ctrl) {
controls.emplace_back(shouldDelete, ctrl);
ctrl->controlIterator = std::prev(controls.end());
}
void Group::insertObject(bool shouldDelete, IObject* obj, std::list<std::pair<bool, IObject*>>::iterator it) {
objects.emplace(it, shouldDelete, obj);
obj->objectIterator = std::prev(it);
}
Group::~Group() {
Clear();
}
void Group::Clear() {
for (auto& it : objects) {
if (it.first) delete it.second;
}
objects.clear();
for (auto& it : controls) {
if (it.first) delete it.second;
}
controls.clear();
}
void Group::Update(float deltaTime) {
for (auto it = objects.begin(); it != objects.end();) {
auto preIt = it++;
if (preIt->second->Visible)
preIt->second->Update(deltaTime);
}
}
void Group::Draw() const {
for (auto& it : objects) {
if (it.second->Visible)
it.second->Draw();
}
}
void Group::OnKeyDown(int keyCode) {
for (auto it = controls.begin(); it != controls.end();) {
auto preIt = it++;
preIt->second->OnKeyDown(keyCode);
}
}
void Group::OnKeyUp(int keyCode) {
for (auto it = controls.begin(); it != controls.end();) {
auto preIt = it++;
preIt->second->OnKeyUp(keyCode);
}
}
void Group::OnMouseDown(int button, int mx, int my) {
for (auto it = controls.begin(); it != controls.end();) {
auto preIt = it++;
preIt->second->OnMouseDown(button, mx, my);
}
}
void Group::OnMouseUp(int button, int mx, int my) {
for (auto it = controls.begin(); it != controls.end();) {
auto preIt = it++;
preIt->second->OnMouseUp(button, mx, my);
}
}
void Group::OnMouseMove(int mx, int my) {
for (auto it = controls.begin(); it != controls.end();) {
auto preIt = it++;
preIt->second->OnMouseMove(mx, my);
}
}
void Group::OnMouseScroll(int mx, int my, int delta) {
for (auto it = controls.begin(); it != controls.end();) {
auto preIt = it++;
preIt->second->OnMouseScroll(mx, my, delta);
}
}
void Group::RemoveObject(std::list<std::pair<bool, IObject*>>::iterator it) {
if (it->first) delete it->second;
objects.erase(it);
}
void Group::RemoveControl(std::list<std::pair<bool, IControl*>>::iterator it) {
if (it->first) delete it->second;
controls.erase(it);
}
void Group::RemoveControlObject(std::list<std::pair<bool, IControl*>>::iterator ctrlIt, std::list<std::pair<bool, IObject*>>::iterator objIt) {
RemoveControl(ctrlIt);
RemoveObject(objIt);
}
void Group::AddNewObject(IObject* obj) {
addObject(true, obj);
}
void Group::InsertNewObject(IObject* obj, std::list<std::pair<bool, IObject*>>::iterator it) {
insertObject(true, obj, it);
}
void Group::AddNewControl(IControl* ctrl) {
addControl(true, ctrl);
}
void Group::AddNewControlObject(IControl* ctrl) {
if (!dynamic_cast<IObject*>(ctrl))
throw std::invalid_argument("The control must inherit both IObject and IControl.");
addObject(false, dynamic_cast<IObject*>(ctrl));
addControl(true, ctrl);
}
void Group::AddRefObject(IObject& obj) {
addObject(false, &obj);
}
void Group::InsertRefObject(IObject& obj, std::list<std::pair<bool, IObject*>>::iterator it) {
insertObject(false, &obj, it);
}
void Group::AddRefControl(IControl& ctrl) {
addControl(false, &ctrl);
}
void Group::AddRefControlObject(IControl& ctrl) {
if (!dynamic_cast<IObject*>(&ctrl))
throw std::invalid_argument("The control must inherit both IObject and IControl.");
addObject(false, dynamic_cast<IObject*>(&ctrl));
addControl(false, &ctrl);
}
std::list<IObject*> Group::GetObjects() {
std::list<IObject*> list;
for (auto& it : objects)
list.push_back(it.second);
return list;
}
std::list<IControl*> Group::GetControls() {
std::list<IControl*> list;
for (auto& it : controls)
list.push_back(it.second);
return list;
}
}