-
Notifications
You must be signed in to change notification settings - Fork 7
/
HierarchyObject.cpp
288 lines (254 loc) · 8.26 KB
/
HierarchyObject.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "HierarchyObject.h"
#include "HierarchyModel.h"
#include "Component.h"
#include <qdebug.h>
#include <qvector3d.h>
// 允许编辑的property类型
const std::set<int> editableTypes = { QVariant::Bool, QVariant::String, QVariant::Int, QVariant::Double, QMetaType::Float };
HierarchyObject * HierarchyObject::getChildren(const QString & name)
{
for (auto child : children) {
if (child->name == name) {
return child;
}
}
return nullptr;
}
int HierarchyObject::findChild(HierarchyObject * child)
{
for (int i = 0; i < children.size(); i++) {
if (child == children[i]) return i;
}
return -1;
}
HierarchyObject::HierarchyObject(const QString & name, HierarchyObject* parent)
{
transform = glm::identity<glm::mat4>();
this->name = name;
this->parentObj = parent;
this->enabled = true;
}
glm::mat4 HierarchyObject::localToWorld()
{
glm::mat4 res = glm::identity<glm::mat4>();
HierarchyObject* nextNode = this;
while (nextNode) {
res = nextNode->transform * res;
nextNode = nextNode->getParent();
}
return res;
}
//void HierarchyObject::setParent(HierarchyObject * newParent)
//{
// //assert(parent != NULL);
// if (parent != NULL) { // 初始添加时会出现parent==NULL
// std::vector<HierarchyObject*>& tempchildren = parent->children;
// tempchildren.erase(std::find(tempchildren.begin(), tempchildren.end(), this));
// }
// if (newParent == NULL) newParent = hierarchy->root;
// parent = newParent;
// newParent->children.push_back(this);
//}
HierarchyObject* HierarchyObject::popChild(int index) {
Q_ASSERT(index < children.size() && index >= 0);
HierarchyObject* child = children[index];
children.erase(children.begin() + index);
child->parentObj = NULL;
return child;
}
void HierarchyObject::insertChild(int index, HierarchyObject* child) {
Q_ASSERT(index <= children.size() && index >= 0);
children.insert(children.begin() + index, child);
child->parentObj = this;
}
void HierarchyObject::moveChild(int oldIndex, int newIndex) {
Q_ASSERT(oldIndex >= 0 && oldIndex < children.size());
Q_ASSERT(newIndex >= 0 && newIndex < children.size());
auto& v = children;
if (oldIndex > newIndex)
std::rotate(v.rend() - oldIndex - 1, v.rend() - oldIndex, v.rend() - newIndex);
else
std::rotate(v.begin() + oldIndex, v.begin() + oldIndex + 1, v.begin() + newIndex + 1);
}
Component * HierarchyObject::addComponent(Component * component)
{
assert(component);
components.push_back(component);
component->hierarchyObject = this;
component->onInit();
return component;
}
void HierarchyObject::updateRecursively()
{
if (!enabled) return;
for (auto component : components) {
assert(component);
if (component->getProp("enabled").toBool()) {
component->onUpdate();
}
}
for (auto child : children) {
assert(child);
child->updateRecursively();
}
}
void HierarchyObject::callRecursively(const std::function<void(HierarchyObject*)>& func, bool requireEnabled)
{
std::queue<HierarchyObject*> todoList;
todoList.push(this);
int failsafe = 0;
while (todoList.size() > 0) {
auto obj = todoList.front();
if ((!requireEnabled) || obj->enabled) {
for (auto child : obj->children) {
todoList.push(child);
}
func(obj);
}
todoList.pop();
if (++failsafe > 100000) {
throw "this must be broken";
}
}
}
// ---------abstract item model--------------------
QModelIndex HierarchyObject::index(int row, int column, const QModelIndex & parent) const
{
if (!parent.isValid()) {
// 根节点index,返回component标题项
if (row >= components.size() || row < 0) return QModelIndex();
return createIndex(row, column, nullptr);
}
else {
// 根节点就是component,返回属性项
if (parent.row() >= components.size() || parent.row() < 0) return QModelIndex();
return createIndex(row, column, components[parent.row()]);
}
}
QModelIndex HierarchyObject::parent(const QModelIndex & index) const
{
if (!index.isValid()) //传入节点是根节点
return QModelIndex();
if (index.internalPointer() == nullptr) {
// 是component
return QModelIndex();
}
// 剩余情况一定是属性项
Component* p = static_cast<Component*>(index.internalPointer());
int row = std::find(components.begin(), components.end(), p) - components.begin();
return createIndex(row, 0, nullptr);
}
int HierarchyObject::rowCount(const QModelIndex & parent) const
{
if (!parent.isValid()) //传入节点是根节点
return components.size();
if (parent.internalPointer() == nullptr) {
// 是component
return components[parent.row()]->getPropKeys().size();
}
// property本身没有子节点
return 0;
}
int HierarchyObject::columnCount(const QModelIndex & parent) const
{
return 2;
//if (!parent.isValid()) //传入节点是根节点
// return 1;
//if (parent.internalPointer() == nullptr) {
// // 是component, 下面的property有两列
// return 2;
//}
//// property
//return 0;
}
QVariant HierarchyObject::data(const QModelIndex & index, int role) const
{
if (!index.isValid()) //传入节点是根节点
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
if (index.internalPointer() == nullptr) {
// 是component 返回其名称
if (index.column() == 0) return QVariant(components[index.row()]->name());
else return QVariant();
}
// property
Component* c = static_cast<Component*>(index.internalPointer());
if (index.column() == 0) {
// 返回属性名
return c->getPropKeys()[index.row()];
}
else {
// 返回属性值
QVariant value = c->getProp(c->getPropKeys()[index.row()]);
if (editableTypes.count((int)value.type())) {
return value;
}
else {
QString res;
if (value.type() == QVariant::Vector3D) {
QDebug(&res) << value.value<QVector3D>();
return res;
}
return value.toString();
}
}
}
HierarchyObject::~HierarchyObject()
{
for (auto component : components) {
assert(component);
delete component;
}
for (auto child : children) {
assert(child);
delete child;
}
}
bool HierarchyObject::setData(const QModelIndex & index, const QVariant & value, int role)
{
if (!index.isValid()) return false;
if (index.internalPointer() == nullptr) {
// 是component 不允许修改
return false;
}
// property
Component* c = static_cast<Component*>(index.internalPointer());
if (index.column() == 0) {
// 属性名不允许修改
return false;
}
else {
// 修改属性值
qDebug() << "setData: " << value << "|" << role;
c->setProp(c->getPropKeys()[index.row()], value);
return true;
}
}
Qt::ItemFlags HierarchyObject::flags(const QModelIndex & index) const
{
if (!index.isValid()) return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (index.internalPointer() == nullptr) {
// 是component 不允许修改
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
// property
Component* c = static_cast<Component*>(index.internalPointer());
if (index.column() == 0) {
// 属性名不允许修改
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
else {
// 属性值
QVariant v = c->getProp(c->getPropKeys()[index.row()]);
//qDebug() << "type of " << c->name() << "." << c->getPropKeys()[index.row()] << " is " << v.type() << " | " << v.typeName();
if (editableTypes.count((int)v.type())) {
return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
}
QVariant HierarchyObject::headerData(int section, Qt::Orientation orientation, int role) const
{
return QVariant(QString("123"));
}