-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRelations.cpp
145 lines (126 loc) · 3.27 KB
/
Relations.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
#include "Relations.h"
#include "GComponent.h"
#include "utils/ByteBuffer.h"
NS_FGUI_BEGIN
USING_NS_CC;
Relations::Relations(GObject* owner) :
handling(nullptr)
{
_owner = owner;
}
Relations::~Relations()
{
clearAll();
}
void Relations::add(GObject * target, RelationType relationType)
{
add(target, relationType, false);
}
void Relations::add(GObject * target, RelationType relationType, bool usePercent)
{
CCASSERT(target, "target is null");
for (auto it = _items.begin(); it != _items.end(); ++it)
{
if ((*it)->getTarget() == target)
{
(*it)->add(relationType, usePercent);
return;
}
}
RelationItem* newItem = new RelationItem(_owner);
newItem->setTarget(target);
newItem->add(relationType, usePercent);
_items.push_back(newItem);
}
void Relations::remove(GObject * target, RelationType relationType)
{
for (auto it = _items.begin(); it != _items.end(); )
{
if ((*it)->getTarget() == target)
{
(*it)->remove(relationType);
if ((*it)->isEmpty())
{
delete (*it);
it = _items.erase(it);
}
else
it++;
}
else
it++;
}
}
bool Relations::contains(GObject * target)
{
for (auto it = _items.begin(); it != _items.end(); ++it)
{
if ((*it)->getTarget() == target)
return true;
}
return false;
}
void Relations::clearFor(GObject * target)
{
for (auto it = _items.begin(); it != _items.end(); )
{
if ((*it)->getTarget() == target)
{
delete (*it);
it = _items.erase(it);
}
else
it++;
}
}
void Relations::clearAll()
{
for (auto it = _items.begin(); it != _items.end(); ++it)
delete (*it);
_items.clear();
}
void Relations::copyFrom(const Relations & source)
{
clearAll();
for (auto it = source._items.begin(); it != source._items.end(); ++it)
{
RelationItem* item = new RelationItem(_owner);
item->copyFrom(**it);
_items.push_back(item);
}
}
void Relations::onOwnerSizeChanged(float dWidth, float dHeight, bool applyPivot)
{
for (auto it = _items.begin(); it != _items.end(); ++it)
(*it)->applyOnSelfSizeChanged(dWidth, dHeight, applyPivot);
}
bool Relations::isEmpty() const
{
return _items.size() == 0;
}
void Relations::setup(ByteBuffer * buffer, bool parentToChild)
{
int cnt = buffer->readByte();
GObject* target;
for (int i = 0; i < cnt; i++)
{
int targetIndex = buffer->readShort();
if (targetIndex == -1)
target = _owner->getParent();
else if (parentToChild)
target = (dynamic_cast<GComponent*>(_owner))->getChildAt(targetIndex);
else
target = _owner->getParent()->getChildAt(targetIndex);
RelationItem* newItem = new RelationItem(_owner);
newItem->setTarget(target);
_items.push_back(newItem);
int cnt2 = buffer->readByte();
for (int j = 0; j < cnt2; j++)
{
RelationType rt = (RelationType)buffer->readByte();
bool usePercent = buffer->readBool();
newItem->internalAdd(rt, usePercent);
}
}
}
NS_FGUI_END