-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathViewGroup.cpp
More file actions
264 lines (229 loc) · 6.84 KB
/
ViewGroup.cpp
File metadata and controls
264 lines (229 loc) · 6.84 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
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
#include "ViewGroup.hpp"
View::Measure ViewGroup::calcChildMeasureSpec(View::Measure spec, int padding, LayoutParams::DimenType lpparam){
View::Measure result;
//If I am forced to be exact
if(spec.spec == View::Measure::EXACT){
//If child want to be Exact
if (lpparam.param == LayoutParams::Params::EXACT){
//Ok we allow child to be of that size
result.spec = View::Measure::EXACT;
result.value = lpparam.value;
}else
//If child want to match me
if (lpparam.param == LayoutParams::Params::MATCH_PARENT){
//Ok we tell it my size
result.spec = View::Measure::EXACT;
result.value = spec.value;
}else
//If child want to determine its own size
if (lpparam.param == LayoutParams::Params::WRAP_CONTENT){
//Ok, but don't be more than me.
result.spec = View::Measure::MAX;
result.value = spec.value;
}
}else if (spec.spec == View::Measure::MAX){ //If I have a max dimen to satisfy
//If child want to be Exact
if (lpparam.param == LayoutParams::Params::EXACT){
//Ok we allow child to be of that size
result.spec = View::Measure::EXACT;
result.value = lpparam.value;
}else
//If child want to match me
if (lpparam.param == LayoutParams::Params::MATCH_PARENT){
//Ok we tell it my size, but my size is not known so
// we tell it to be max upto my size
result.spec = View::Measure::MAX;
result.value = spec.value;
}else
//If child want to determine its own size
if (lpparam.param == LayoutParams::Params::WRAP_CONTENT){
//Ok, but don't be more than me.
result.spec = View::Measure::MAX;
result.value = spec.value;
}
}else if (spec.spec == View::Measure::ANY){ //If I can be any size
//If child want to be Exact
if (lpparam.param == LayoutParams::Params::EXACT){
//Ok we allow child to be of that size
result.spec = View::Measure::EXACT;
result.value = lpparam.value;
}else
//If child want to match me
if (lpparam.param == LayoutParams::Params::MATCH_PARENT){
//Child want to match my size, lets find out how big it wants to be
result.spec = View::Measure::ANY;
result.value = 0;
}else
//If child want to determine its own size
if (lpparam.param == LayoutParams::Params::WRAP_CONTENT){
//Child want to determine its own size.. lets find out how big
result.spec = View::Measure::ANY;
result.value = 0;
}
}
return result;
}
void ViewGroup::measureChild(View* v, View::Measure wm, View::Measure hm){
auto lp = v->getLayoutParams();
//Compute the measuring spec for each child
Measure new_wm = calcChildMeasureSpec(wm, mLeftPad + mRightPad, lp->width);
Measure new_hm = calcChildMeasureSpec(hm, mTopPad + mBottomPad, lp->height);
v->doMeasure(new_wm, new_hm);
}
void ViewGroup::measureChildren(View::Measure wm, View::Measure hm){
for(auto& child : children){
measureChild(child.get(), wm, hm);
}
}
void ViewGroup::addView(ViewPtr v){
if(v.get() == nullptr)
return;
addView(v, -1);
};
void ViewGroup::addView(ViewPtr v, int pos){
if(v.get() == nullptr)
return;
std::unique_ptr<LayoutParams>& ulp = v->layoutParams;
if(ulp.get() == nullptr)
ulp = generateDefaultLayoutParams();
addView(v, pos, std::move(ulp));
}
void ViewGroup::addView(ViewPtr v, int pos, std::unique_ptr<LayoutParams> lp){
if(v.get() == nullptr)
return;
addChild(v,pos,std::move(lp));
}
void ViewGroup::addChild(ViewPtr v, int pos, std::unique_ptr<LayoutParams> p){
v->setLayoutParams(std::move(p));
v->parent = this;
if(pos < 0){
children.insert(children.end(),v);
}else{
children.insert(children.begin()+pos, v);
}
}
void ViewGroup::delView(ViewPtr ){
}
void ViewGroup::drawChildren(Canvas& c){
//Draws children in sequence
for(auto& child : children){
c.save();
child->draw(c);
c.restore();
}
}
void ViewGroup::childFocused(View* child, View* focused){
/**
Set the child which contains the focused view
Also unfocuses the old view if this is new child
*/
if(focusedChild != nullptr)
if(focusedChild != child)
focusedChild->unfocus();
focusedChild = child;
if(parent != nullptr){
parent->childFocused(this,focused);
}
}
bool ViewGroup::requestFocus(FocusDirection dir){
/**
If a view wants focus, it always does it via its parent which is a ViewGroup
This makes correct focust transfer from one view to other view
*/
for(auto& child : children)
if(child->requestFocus(dir))
return true;
return View::requestFocus(dir);
}
bool ViewGroup::dispatchMouseEvent(MouseEvent* ev){
int orig_x = ev->x;
int orig_y = ev->y;
int x = ev->x + scrollX;
int y = ev->y + scrollY;
auto&& eventType = ev->getEventType();
/**
If this is mouse pressed then find the target
*/
if(eventType == MousePressed::type){
if(targetChild != nullptr)
targetChild = nullptr;
auto end = children.rend();
for(auto childr = children.rbegin(); childr != end; childr++){
auto& child = *childr;
if (x > child->left && x < child->right &&
y > child->top && y < child->bottom){
ev->x = x - child->left;
ev->y = y - child->top;
if(child->dispatchMouseEvent(ev)){
targetChild = child.get();
return true;
}
}
}
}
/**
If no child can take mouse event this view will handle it
*/
if(targetChild == nullptr){
ev->x = orig_x;
ev->y = orig_y;
return View::dispatchMouseEvent(ev);
}
/**
If some child can take mouse event then let it handle and reset the
target
*/
auto tmpTarget = targetChild;
if (eventType == MouseReleased::type){
targetChild = nullptr;
}
ev->x = x - tmpTarget->left;
ev->y = y - tmpTarget->top;
return tmpTarget->dispatchMouseEvent(ev);
}
bool ViewGroup::dispatchTextEnteredEvent(TextEvent* tevent){
if(focused)
return View::dispatchTextEnteredEvent(tevent);
if (focusedChild !=nullptr)
return focusedChild->dispatchTextEnteredEvent(tevent);
return false;
}
bool ViewGroup::dispatchKeyEvent(KeyEvent* event){
if(focused)
return View::dispatchKeyEvent(event);
if(onKeyEventListener != nullptr)
if(onKeyEventListener(this, event))
return true;
if(focusedChild != nullptr)
return focusedChild->dispatchKeyEvent(event);
return false;
}
void ViewGroup::unfocus(){
if(focusedChild !=nullptr)
focusedChild->unfocus();
focusedChild = nullptr;
}
void ViewGroup::childUnFocused(View* view, FocusDirection dir){
auto end = children.end();
auto it = children.begin();
for(; it != end; ++it){
if(view->id == (*it)->id){
break;
}
}
if(dir == FocusDirection::DOWN){
if(it != end)
++it;
if(it == end)
it = children.begin();
}else if (dir == FocusDirection::UP){
if(it > children.begin())
--it;
else
it = end-1;
}
(*it)->requestFocus();
}
void ViewGroup::interceptOnKey(std::function<bool(View*, KeyEvent*)> func){
onKeyEventListener = func;
}