-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
59 lines (49 loc) · 1.18 KB
/
object.cpp
File metadata and controls
59 lines (49 loc) · 1.18 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
#include "object.h"
#include "event.h"
Object::Object(Object* parent) {
setParent(parent);
}
Object::~Object() {
// Todo: 释放子对象
}
/**
* @brief 设置自己的父对象,同时把自己添加到父对象的列表中,并且设置好自己的唯一id
* @param 父对象的指针
* @remark B的父对象是A,若C将父对象设为B,则此函数自动将C的父对象转为A
*/
void Object::setParent(Object* parent) {
m_windowParent = parent; // 指定父对象
if (parent == NULL) // 如果父对象为NULL,则什么也不做,m_parent是默认值NULL
return;
else if (parent->getParent() != NULL) {
setParent(parent->getParent());
}
else {
m_objectId = parent->addChild(this); // 将自己添加到父对象的列表中,并设置唯一id
}
}
void Object::dispatchEvent(Event* event)
{
if (eventLoop(event)) // 先自己来处理事件
return;
for (auto s : m_children)
{
if (s->eventLoop(event))
break;
}
}
/**
* @brief 开启事件循环,负责把事件分发给不同的函数
*/
bool Object::eventLoop(Event* event) {
return false;
}
/**
* @brief 添加子对象
* @param 子对象
* @return 返回子对象的唯一id
*/
int Object::addChild(Object* child) {
m_children.push_back(child);
return m_children.size();
}