1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "components/ui_view.h"
17
18 #include "components/root_view.h"
19 #include "components/ui_view_group.h"
20 #include "core/render_manager.h"
21 #include "dfx/ui_view_bounds.h"
22 #include "dock/focus_manager.h"
23 #include "draw/draw_utils.h"
24 #include "engines/gfx/gfx_engine_manager.h"
25 #include "gfx_utils/graphic_log.h"
26 #include "gfx_utils/mem_api.h"
27 #include "securec.h"
28 #include "themes/theme_manager.h"
29
30 namespace OHOS {
UIView()31 UIView::UIView()
32 : touchable_(false),
33 visible_(true),
34 draggable_(false),
35 dragParentInstead_(true),
36 isViewGroup_(false),
37 needRedraw_(false),
38 styleAllocFlag_(false),
39 isIntercept_(false),
40 #if ENABLE_FOCUS_MANAGER
41 focusable_(false),
42 #endif
43 opaScale_(OPA_OPAQUE),
44 index_(0),
45 zIndex_(0),
46 id_(nullptr),
47 parent_(nullptr),
48 nextSibling_(nullptr),
49 nextRenderSibling_(nullptr),
50 style_(nullptr),
51 transMap_(nullptr),
52 onClickListener_(nullptr),
53 onLongPressListener_(nullptr),
54 onDragListener_(nullptr),
55 onTouchListener_(nullptr),
56 #if ENABLE_FOCUS_MANAGER
57 onFocusListener_(nullptr),
58 #endif
59 #if ENABLE_ROTATE_INPUT
60 onRotateListener_(nullptr),
61 #endif
62 viewExtraMsg_(nullptr),
63 rect_(0, 0, 0, 0),
64 visibleRect_(nullptr)
65 {
66 SetupThemeStyles();
67 }
68
~UIView()69 UIView::~UIView()
70 {
71 if (transMap_ != nullptr) {
72 delete transMap_;
73 transMap_ = nullptr;
74 }
75 if (visibleRect_ != nullptr) {
76 delete visibleRect_;
77 visibleRect_ = nullptr;
78 }
79 if (styleAllocFlag_) {
80 delete style_;
81 style_ = nullptr;
82 styleAllocFlag_ = false;
83 }
84 }
85
OnPreDraw(Rect & invalidatedArea) const86 bool UIView::OnPreDraw(Rect& invalidatedArea) const
87 {
88 Rect rect(GetRect());
89 uint16_t r = style_->borderRadius_; // radius must be positive
90 if (r == COORD_MAX) {
91 return true;
92 }
93 if (r != 0) {
94 r = ((r & 0x1) == 0) ? (r >> 1) : ((r + 1) >> 1);
95 rect.SetLeft(rect.GetX() + r);
96 rect.SetWidth(rect.GetWidth() - r);
97 rect.SetTop(rect.GetY() + r);
98 rect.SetHeight(rect.GetHeight() - r);
99 }
100 if (rect.IsContains(invalidatedArea)) {
101 return true;
102 }
103 invalidatedArea.Intersect(invalidatedArea, rect);
104 return false;
105 }
106
OnDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)107 void UIView::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
108 {
109 uint8_t opa = GetMixOpaScale();
110 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, GetOrigRect(), invalidatedArea, *style_, opa);
111 }
112
OnPostDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)113 void UIView::OnPostDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
114 {
115 DrawViewBounds(gfxDstBuffer, invalidatedArea);
116 }
117
DrawViewBounds(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)118 void UIView::DrawViewBounds(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
119 {
120 #if ENABLE_DEBUG
121 if (!UIViewBounds::GetInstance()->GetShowState()) {
122 return;
123 }
124 Style* style = new Style();
125 style->SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
126 style->SetStyle(STYLE_BORDER_COLOR, Color::Red().full);
127 style->SetStyle(STYLE_BORDER_WIDTH, 1);
128 style->SetStyle(STYLE_BORDER_OPA, OPA_OPAQUE / 2); // 2: half opacity
129 Rect viewRect(GetRect());
130 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, viewRect, invalidatedArea, *style, OPA_OPAQUE);
131
132 style->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
133 style->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
134 style->SetStyle(STYLE_BORDER_WIDTH, 0);
135 Rect tmpRect(viewRect);
136 int16_t length = 10; // 10: corner length
137
138 // left top corner
139 tmpRect.SetRight(viewRect.GetLeft() + length);
140 tmpRect.SetBottom(viewRect.GetTop());
141 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
142 tmpRect.SetRight(viewRect.GetLeft());
143 tmpRect.SetBottom(viewRect.GetTop() + length);
144 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
145
146 // left bottom corner
147 tmpRect.SetLeft(viewRect.GetLeft());
148 tmpRect.SetTop(viewRect.GetBottom() - length);
149 tmpRect.SetRight(viewRect.GetLeft());
150 tmpRect.SetBottom(viewRect.GetBottom());
151 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
152 tmpRect.SetTop(viewRect.GetBottom());
153 tmpRect.SetRight(viewRect.GetLeft() + length);
154 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
155
156 // right top corner
157 tmpRect.SetLeft(viewRect.GetRight() - length);
158 tmpRect.SetTop(viewRect.GetTop());
159 tmpRect.SetRight(viewRect.GetRight());
160 tmpRect.SetBottom(viewRect.GetTop());
161 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
162 tmpRect.SetLeft(viewRect.GetRight());
163 tmpRect.SetBottom(viewRect.GetTop() + length);
164 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
165
166 // right bottom corner
167 tmpRect = viewRect;
168 tmpRect.SetLeft(viewRect.GetRight());
169 tmpRect.SetTop(viewRect.GetBottom() - length);
170 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
171 tmpRect.SetLeft(viewRect.GetRight() - length);
172 tmpRect.SetTop(viewRect.GetBottom());
173 BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, tmpRect, invalidatedArea, *style, OPA_OPAQUE);
174 delete style;
175 #endif // ENABLE_DEBUG
176 }
177
SetupThemeStyles()178 void UIView::SetupThemeStyles()
179 {
180 Theme* theme = ThemeManager::GetInstance().GetCurrent();
181 if (theme != nullptr) {
182 style_ = &(theme->GetMainStyle());
183 } else {
184 style_ = &(StyleDefault::GetDefaultStyle());
185 }
186 }
187
SetStyle(Style & style)188 void UIView::SetStyle(Style& style)
189 {
190 if (styleAllocFlag_) {
191 delete style_;
192 styleAllocFlag_ = false;
193 }
194 style_ = &style;
195 }
196
SetStyle(uint8_t key,int64_t value)197 void UIView::SetStyle(uint8_t key, int64_t value)
198 {
199 if (!styleAllocFlag_) {
200 style_ = new Style(*style_);
201 if (style_ == nullptr) {
202 GRAPHIC_LOGE("new Style fail");
203 return;
204 }
205 styleAllocFlag_ = true;
206 }
207 int16_t width = GetWidth();
208 int16_t height = GetHeight();
209 int16_t x = GetX();
210 int16_t y = GetY();
211 style_->SetStyle(key, value);
212 Rect rect(x, y, x + width - 1, y + height - 1);
213 UpdateRectInfo(key, rect);
214 }
215
UpdateRectInfo(uint8_t key,const Rect & rect)216 void UIView::UpdateRectInfo(uint8_t key, const Rect& rect)
217 {
218 switch (key) {
219 case STYLE_BORDER_WIDTH: {
220 SetWidth(rect.GetWidth());
221 SetHeight(rect.GetHeight());
222 break;
223 }
224 case STYLE_PADDING_LEFT:
225 case STYLE_PADDING_RIGHT: {
226 SetWidth(rect.GetWidth());
227 break;
228 }
229 case STYLE_PADDING_TOP:
230 case STYLE_PADDING_BOTTOM: {
231 SetHeight(rect.GetHeight());
232 break;
233 }
234 case STYLE_MARGIN_LEFT: {
235 SetX(rect.GetX());
236 break;
237 }
238 case STYLE_MARGIN_TOP: {
239 SetY(rect.GetY());
240 break;
241 }
242 default:
243 break;
244 }
245 }
246
GetStyle(uint8_t key) const247 int64_t UIView::GetStyle(uint8_t key) const
248 {
249 return style_->GetStyle(key);
250 }
251
GetStyleConst() const252 const Style& UIView::GetStyleConst() const
253 {
254 return *style_;
255 }
256
SetOpaScale(uint8_t opaScale)257 void UIView::SetOpaScale(uint8_t opaScale)
258 {
259 opaScale_ = opaScale;
260 }
261
GetOpaScale() const262 uint8_t UIView::GetOpaScale() const
263 {
264 return opaScale_;
265 }
266
GetExtraMsg()267 UIView::ViewExtraMsg* UIView::GetExtraMsg()
268 {
269 return viewExtraMsg_;
270 }
271
SetExtraMsg(ViewExtraMsg * extraMsg)272 void UIView::SetExtraMsg(ViewExtraMsg* extraMsg)
273 {
274 viewExtraMsg_ = extraMsg;
275 }
276
Rotate(int16_t angle,const Vector2<float> & pivot)277 void UIView::Rotate(int16_t angle, const Vector2<float>& pivot)
278 {
279 Vector3<float> pivotStart3D = Vector3<float>(pivot.x_, pivot.y_, 0);
280 Vector3<float> pivotEnd3D = Vector3<float>(pivot.x_, pivot.y_, 1.0f);
281 Rotate(angle, pivotStart3D, pivotEnd3D);
282 }
283
Rotate(int16_t angle,const Vector3<float> & pivotStart,const Vector3<float> & pivotEnd)284 void UIView::Rotate(int16_t angle, const Vector3<float>& pivotStart, const Vector3<float>& pivotEnd)
285 {
286 if (transMap_ == nullptr) {
287 ReMeasure();
288 transMap_ = new TransformMap();
289 }
290 bool firstTrans = transMap_->IsInvalid();
291 Rect joinRect = transMap_->GetBoxRect();
292 transMap_->SetTransMapRect(GetOrigRect());
293 transMap_->Rotate(angle, pivotStart, pivotEnd);
294 if (firstTrans) {
295 joinRect = transMap_->GetBoxRect();
296 } else {
297 joinRect.Join(joinRect, transMap_->GetBoxRect());
298 }
299 joinRect.Join(joinRect, GetOrigRect());
300 InvalidateRect(joinRect);
301 }
302
Scale(const Vector2<float> & scale,const Vector2<float> & pivot)303 void UIView::Scale(const Vector2<float>& scale, const Vector2<float>& pivot)
304 {
305 Vector3<float> scale3D = Vector3<float>(scale.x_, scale.y_, 1.0f);
306 Vector3<float> pivot3D = Vector3<float>(pivot.x_, pivot.y_, 0);
307 Scale(scale3D, pivot3D);
308 }
309
Scale(const Vector3<float> & scale,const Vector3<float> & pivot)310 void UIView::Scale(const Vector3<float>& scale, const Vector3<float>& pivot)
311 {
312 if (transMap_ == nullptr) {
313 ReMeasure();
314 transMap_ = new TransformMap();
315 }
316 bool firstTrans = transMap_->IsInvalid();
317 Rect joinRect = transMap_->GetBoxRect();
318 transMap_->SetTransMapRect(GetOrigRect());
319 transMap_->Scale(scale, pivot);
320 if (firstTrans) {
321 joinRect = transMap_->GetBoxRect();
322 } else {
323 joinRect.Join(joinRect, transMap_->GetBoxRect());
324 }
325 joinRect.Join(joinRect, GetOrigRect());
326 InvalidateRect(joinRect);
327 }
328
Shear(const Vector2<float> & shearX,const Vector2<float> & shearY,const Vector2<float> & shearZ)329 void UIView::Shear(const Vector2<float>& shearX, const Vector2<float>& shearY, const Vector2<float>& shearZ)
330 {
331 if (transMap_ == nullptr) {
332 ReMeasure();
333 transMap_ = new TransformMap();
334 if (transMap_ == nullptr) {
335 return;
336 }
337 }
338 bool firstTrans = transMap_->IsInvalid();
339 Rect joinRect = transMap_->GetBoxRect();
340 transMap_->SetTransMapRect(GetOrigRect());
341 transMap_->Shear(shearX, shearY, shearZ);
342 if (firstTrans) {
343 joinRect = transMap_->GetBoxRect();
344 } else {
345 joinRect.Join(joinRect, transMap_->GetBoxRect());
346 }
347 joinRect.Join(joinRect, GetOrigRect());
348 InvalidateRect(joinRect);
349 }
350
Translate(const Vector2<int16_t> & trans)351 void UIView::Translate(const Vector2<int16_t>& trans)
352 {
353 Vector3<int16_t> trans3D = Vector3<int16_t>(trans.x_, trans.y_, 0);
354 Translate(trans3D);
355 }
356
Translate(const Vector3<int16_t> & trans)357 void UIView::Translate(const Vector3<int16_t>& trans)
358 {
359 if (transMap_ == nullptr) {
360 ReMeasure();
361 transMap_ = new TransformMap();
362 }
363 bool firstTrans = transMap_->IsInvalid();
364 Rect joinRect = transMap_->GetBoxRect();
365 transMap_->SetTransMapRect(GetOrigRect());
366 transMap_->Translate(trans);
367 if (firstTrans) {
368 joinRect = transMap_->GetBoxRect();
369 } else {
370 joinRect.Join(joinRect, transMap_->GetBoxRect());
371 }
372 joinRect.Join(joinRect, GetOrigRect());
373 InvalidateRect(joinRect);
374 }
375
IsTransInvalid()376 bool UIView::IsTransInvalid()
377 {
378 if (transMap_ == nullptr) {
379 return true;
380 }
381 return transMap_->IsInvalid();
382 }
383
SetCameraDistance(int16_t distance)384 void UIView::SetCameraDistance(int16_t distance)
385 {
386 if (transMap_ == nullptr) {
387 ReMeasure();
388 transMap_ = new TransformMap();
389 }
390 Rect joinRect = transMap_->GetBoxRect();
391 transMap_->SetTransMapRect(GetOrigRect());
392 transMap_->SetCameraDistance(distance);
393 joinRect.Join(joinRect, transMap_->GetBoxRect());
394 joinRect.Join(joinRect, GetOrigRect());
395 InvalidateRect(joinRect);
396 }
397
SetCameraPosition(const Vector2<float> & position)398 void UIView::SetCameraPosition(const Vector2<float>& position)
399 {
400 if (transMap_ == nullptr) {
401 ReMeasure();
402 transMap_ = new TransformMap();
403 }
404 Rect joinRect = transMap_->GetBoxRect();
405 transMap_->SetTransMapRect(GetOrigRect());
406 transMap_->SetCameraPosition(position);
407 joinRect.Join(joinRect, transMap_->GetBoxRect());
408 joinRect.Join(joinRect, GetOrigRect());
409 InvalidateRect(joinRect);
410 }
411
ResetTransParameter()412 void UIView::ResetTransParameter()
413 {
414 if (transMap_ != nullptr) {
415 delete transMap_;
416 transMap_ = nullptr;
417 Invalidate();
418 }
419 }
420
421 #if ENABLE_ROTATE_INPUT
RequestFocus()422 void UIView::RequestFocus()
423 {
424 FocusManager::GetInstance()->RequestFocus(this);
425 }
426
ClearFocus()427 void UIView::ClearFocus()
428 {
429 FocusManager::GetInstance()->ClearFocus();
430 }
431 #endif
432
Invalidate()433 void UIView::Invalidate()
434 {
435 InvalidateRect(GetRect());
436 }
437
InvalidateRect(const Rect & invalidatedArea)438 void UIView::InvalidateRect(const Rect& invalidatedArea)
439 {
440 if (!visible_) {
441 if (needRedraw_) {
442 needRedraw_ = false;
443 } else {
444 return;
445 }
446 }
447
448 Rect trunc(invalidatedArea);
449 bool isIntersect = true;
450 UIView* par = parent_;
451 UIView* cur = this;
452
453 while (par != nullptr) {
454 if (!par->visible_) {
455 return;
456 }
457
458 isIntersect = trunc.Intersect(par->GetContentRect(), trunc);
459 if (!isIntersect) {
460 break;
461 }
462
463 cur = par;
464 par = par->parent_;
465 }
466
467 if (isIntersect && (cur->GetViewType() == UI_ROOT_VIEW)) {
468 RootView* rootView = reinterpret_cast<RootView*>(cur);
469 rootView->AddInvalidateRectWithLock(trunc, this);
470 }
471 }
472
OnLongPressEvent(const LongPressEvent & event)473 bool UIView::OnLongPressEvent(const LongPressEvent& event)
474 {
475 if (onLongPressListener_ != nullptr) {
476 /* To ensure version compatibility, the listeners of both versions are invoked. */
477 bool isConsumed = onLongPressListener_->OnLongPress(*this, event);
478 return isConsumed;
479 }
480 return isIntercept_;
481 }
482
OnDragStartEvent(const DragEvent & event)483 bool UIView::OnDragStartEvent(const DragEvent& event)
484 {
485 if (onDragListener_ != nullptr) {
486 /* To ensure version compatibility, the listeners of both versions are invoked. */
487 bool isConsumed = onDragListener_->OnDragStart(*this, event);
488 return isConsumed;
489 }
490 return isIntercept_;
491 }
492
OnDragEvent(const DragEvent & event)493 bool UIView::OnDragEvent(const DragEvent& event)
494 {
495 if (onDragListener_ != nullptr) {
496 /* To ensure version compatibility, the listeners of both versions are invoked. */
497 bool isConsumed = onDragListener_->OnDrag(*this, event);
498 return isConsumed;
499 }
500 return isIntercept_;
501 }
502
OnDragEndEvent(const DragEvent & event)503 bool UIView::OnDragEndEvent(const DragEvent& event)
504 {
505 if (onDragListener_ != nullptr) {
506 /* To ensure version compatibility, the listeners of both versions are invoked. */
507 bool isConsumed = onDragListener_->OnDragEnd(*this, event);
508 return isConsumed;
509 }
510 return isIntercept_;
511 }
512
OnClickEvent(const ClickEvent & event)513 bool UIView::OnClickEvent(const ClickEvent& event)
514 {
515 if (onClickListener_ != nullptr) {
516 /* To ensure version compatibility, the listeners of both versions are invoked. */
517 bool isConsumed = onClickListener_->OnClick(*this, event);
518 return isConsumed;
519 }
520 return isIntercept_;
521 }
522
OnPressEvent(const PressEvent & event)523 bool UIView::OnPressEvent(const PressEvent& event)
524 {
525 if (onTouchListener_ != nullptr) {
526 /* To ensure version compatibility, the listeners of both versions are invoked. */
527 bool isConsumed = onTouchListener_->OnPress(*this, event);
528 return isConsumed;
529 }
530 return isIntercept_;
531 }
532
OnReleaseEvent(const ReleaseEvent & event)533 bool UIView::OnReleaseEvent(const ReleaseEvent& event)
534 {
535 if (onTouchListener_ != nullptr) {
536 /* To ensure version compatibility, the listeners of both versions are invoked. */
537 bool isConsumed = onTouchListener_->OnRelease(*this, event);
538 return isConsumed;
539 }
540 return isIntercept_;
541 }
542
OnCancelEvent(const CancelEvent & event)543 bool UIView::OnCancelEvent(const CancelEvent& event)
544 {
545 if (onTouchListener_ != nullptr) {
546 /* To ensure version compatibility, the listeners of both versions are invoked. */
547 bool isConsumed = onTouchListener_->OnCancel(*this, event);
548 return isConsumed;
549 }
550 return isIntercept_;
551 }
552
SetOnDragListener(OnDragListener * onDragListener)553 void UIView::SetOnDragListener(OnDragListener* onDragListener)
554 {
555 onDragListener_ = onDragListener;
556 }
557
GetOnDragListener()558 UIView::OnDragListener*& UIView::GetOnDragListener()
559 {
560 return onDragListener_;
561 }
562
SetOnClickListener(OnClickListener * onClickListener)563 void UIView::SetOnClickListener(OnClickListener* onClickListener)
564 {
565 onClickListener_ = onClickListener;
566 }
567
GetOnClickListener()568 UIView::OnClickListener*& UIView::GetOnClickListener()
569 {
570 return onClickListener_;
571 }
572
SetOnLongPressListener(OnLongPressListener * onLongPressListener)573 void UIView::SetOnLongPressListener(OnLongPressListener* onLongPressListener)
574 {
575 onLongPressListener_ = onLongPressListener;
576 }
577
GetOnLongPressListener()578 UIView::OnLongPressListener*& UIView::GetOnLongPressListener()
579 {
580 return onLongPressListener_;
581 }
582
SetOnTouchListener(OnTouchListener * onTouchListener)583 void UIView::SetOnTouchListener(OnTouchListener* onTouchListener)
584 {
585 onTouchListener_ = onTouchListener;
586 }
587
GetTouchListener()588 UIView::OnTouchListener*& UIView::GetTouchListener()
589 {
590 return onTouchListener_;
591 }
592
SetParent(UIView * parent)593 void UIView::SetParent(UIView* parent)
594 {
595 parent_ = parent;
596 }
597
GetParent() const598 UIView* UIView::GetParent() const
599 {
600 return parent_;
601 }
602
SetNextSibling(UIView * sibling)603 void UIView::SetNextSibling(UIView* sibling)
604 {
605 nextSibling_ = sibling;
606 }
607
GetNextSibling() const608 UIView* UIView::GetNextSibling() const
609 {
610 return nextSibling_;
611 }
612
SetNextRenderSibling(UIView * renderSibling)613 void UIView::SetNextRenderSibling(UIView* renderSibling)
614 {
615 nextRenderSibling_ = renderSibling;
616 }
617
GetNextRenderSibling() const618 UIView* UIView::GetNextRenderSibling() const
619 {
620 return nextRenderSibling_;
621 }
622
SetVisible(bool visible)623 void UIView::SetVisible(bool visible)
624 {
625 if (visible_ != visible) {
626 visible_ = visible;
627 needRedraw_ = true;
628 Invalidate();
629 }
630 }
631
IsVisible() const632 bool UIView::IsVisible() const
633 {
634 return visible_;
635 }
636
SetTouchable(bool touch)637 void UIView::SetTouchable(bool touch)
638 {
639 touchable_ = touch;
640 }
641
IsTouchable() const642 bool UIView::IsTouchable() const
643 {
644 return touchable_;
645 }
646
SetDraggable(bool draggable)647 void UIView::SetDraggable(bool draggable)
648 {
649 draggable_ = draggable;
650 dragParentInstead_ = !draggable;
651 }
652
IsDraggable() const653 bool UIView::IsDraggable() const
654 {
655 return draggable_;
656 }
657
SetDragParentInstead(bool dragParentInstead)658 void UIView::SetDragParentInstead(bool dragParentInstead)
659 {
660 dragParentInstead_ = dragParentInstead;
661 }
662
IsDragParentInstead() const663 bool UIView::IsDragParentInstead() const
664 {
665 return dragParentInstead_;
666 }
667
668 #if ENABLE_ROTATE_INPUT
OnRotateStartEvent(const RotateEvent & event)669 bool UIView::OnRotateStartEvent(const RotateEvent& event)
670 {
671 if (onRotateListener_ != nullptr) {
672 return onRotateListener_->OnRotateStart(*this, event);
673 }
674 return false;
675 }
676
OnRotateEvent(const RotateEvent & event)677 bool UIView::OnRotateEvent(const RotateEvent& event)
678 {
679 if (onRotateListener_ != nullptr) {
680 return onRotateListener_->OnRotate(*this, event);
681 }
682 return isIntercept_;
683 }
684
OnRotateEndEvent(const RotateEvent & event)685 bool UIView::OnRotateEndEvent(const RotateEvent& event)
686 {
687 if (onRotateListener_ != nullptr) {
688 return onRotateListener_->OnRotateEnd(*this, event);
689 }
690 return false;
691 }
692
SetOnRotateListener(OnRotateListener * onRotateListener)693 void UIView::SetOnRotateListener(OnRotateListener* onRotateListener)
694 {
695 onRotateListener_ = onRotateListener;
696 }
697
698 #endif
699
GetTargetView(const Point & point,UIView ** last)700 void UIView::GetTargetView(const Point& point, UIView** last)
701 {
702 if (last == nullptr) {
703 return;
704 }
705 UIView* par = parent_;
706 Rect rect = GetRect();
707
708 if (par != nullptr) {
709 rect.Intersect(par->GetContentRect(), rect);
710 }
711
712 if (visible_ && touchable_ && rect.IsContains(point)) {
713 *last = this;
714 }
715 }
716
GetTargetView(const Point & point,UIView ** current,UIView ** target)717 void UIView::GetTargetView(const Point& point, UIView** current, UIView** target)
718 {
719 if (current == nullptr) {
720 return;
721 }
722 UIView* par = parent_;
723 Rect rect = GetRect();
724
725 if (par != nullptr) {
726 rect.Intersect(par->GetContentRect(), rect);
727 }
728
729 if (visible_ && rect.IsContains(point)) {
730 if (touchable_) {
731 *current = this;
732 }
733 *target = this;
734 }
735 }
736
737 #if ENABLE_FOCUS_MANAGER
SetFocusable(bool focusable)738 void UIView::SetFocusable(bool focusable)
739 {
740 focusable_ = focusable;
741 }
742
IsFocusable() const743 bool UIView::IsFocusable() const
744 {
745 return focusable_;
746 }
747
Focus()748 void UIView::Focus()
749 {
750 if (focusable_ && onFocusListener_ != nullptr) {
751 onFocusListener_->OnFocus(*this);
752 }
753 }
754
Blur()755 void UIView::Blur()
756 {
757 if (onFocusListener_ != nullptr) {
758 onFocusListener_->OnBlur(*this);
759 }
760 }
761
SetOnFocusListener(OnFocusListener * onFocusListener)762 void UIView::SetOnFocusListener(OnFocusListener* onFocusListener)
763 {
764 onFocusListener_ = onFocusListener;
765 }
766
GetOnFocusListener() const767 UIView::OnFocusListener* UIView::GetOnFocusListener() const
768 {
769 return onFocusListener_;
770 }
771
772 #endif
773
GetRect() const774 Rect UIView::GetRect() const
775 {
776 if ((transMap_ != nullptr) && !transMap_->IsInvalid()) {
777 Rect r = transMap_->GetBoxRect();
778 Rect origRect = GetOrigRect();
779 r.SetX(r.GetX() + origRect.GetX() - transMap_->GetTransMapRect().GetX());
780 r.SetY(r.GetY() + origRect.GetY() - transMap_->GetTransMapRect().GetY());
781 return r;
782 }
783 return GetOrigRect();
784 }
785
GetContentRect()786 Rect UIView::GetContentRect()
787 {
788 if ((transMap_ != nullptr) && !transMap_->IsInvalid()) {
789 Rect r = transMap_->GetBoxRect();
790 Rect origRect = GetOrigRect();
791 r.SetX(r.GetX() + origRect.GetX() - transMap_->GetTransMapRect().GetX());
792 r.SetY(r.GetY() + origRect.GetY() - transMap_->GetTransMapRect().GetY());
793 return r;
794 }
795
796 Rect contentRect = GetRect();
797 contentRect.SetX(contentRect.GetX() + style_->paddingLeft_ + style_->borderWidth_);
798 contentRect.SetY(contentRect.GetY() + style_->paddingTop_ + style_->borderWidth_);
799 contentRect.SetWidth(GetWidth());
800 contentRect.SetHeight(GetHeight());
801 return contentRect;
802 }
803
GetOrigRect() const804 Rect UIView::GetOrigRect() const
805 {
806 int16_t x = rect_.GetX();
807 int16_t y = rect_.GetY();
808 UIView* par = parent_;
809 while (par != nullptr) {
810 x += par->GetRelativeRect().GetX() + par->GetStyle(STYLE_PADDING_LEFT) + par->GetStyle(STYLE_BORDER_WIDTH);
811 y += par->GetRelativeRect().GetY() + par->GetStyle(STYLE_PADDING_TOP) + par->GetStyle(STYLE_BORDER_WIDTH);
812 par = par->parent_;
813 }
814 return Rect(x, y, x + rect_.GetWidth() - 1, y + rect_.GetHeight() - 1);
815 }
816
GetMaskedRect() const817 Rect UIView::GetMaskedRect() const
818 {
819 Rect mask;
820 if (visibleRect_ != nullptr) {
821 mask.Intersect(GetRect(), GetVisibleRect());
822 } else {
823 mask = GetRect();
824 }
825 return mask;
826 }
827
GetVisibleRect() const828 Rect UIView::GetVisibleRect() const
829 {
830 if (visibleRect_ == nullptr) {
831 return GetRect();
832 }
833 Rect absoluteRect;
834 int16_t x = visibleRect_->GetX();
835 int16_t y = visibleRect_->GetY();
836 UIView* par = parent_;
837 while (par != nullptr) {
838 x += par->GetX();
839 y += par->GetY();
840 par = par->parent_;
841 }
842 absoluteRect.SetX(x);
843 absoluteRect.SetY(y);
844 absoluteRect.SetWidth(visibleRect_->GetWidth());
845 absoluteRect.SetHeight(visibleRect_->GetHeight());
846 return absoluteRect;
847 }
848
SetTransformMap(const TransformMap & transMap)849 void UIView::SetTransformMap(const TransformMap& transMap)
850 {
851 if ((transMap_ != nullptr) && (*transMap_ == transMap)) {
852 return;
853 }
854
855 if (transMap_ == nullptr) {
856 transMap_ = new TransformMap();
857 }
858 Rect preRect = GetRect();
859 *transMap_ = transMap;
860 transMap_->SetTransMapRect(GetOrigRect());
861
862 Rect joinRect;
863 joinRect.Join(preRect, transMap_->GetBoxRect());
864 InvalidateRect(joinRect);
865 }
866
SetWidth(int16_t width)867 void UIView::SetWidth(int16_t width)
868 {
869 if (GetWidth() != width) {
870 int16_t newWidth = width + style_->paddingLeft_ + style_->paddingRight_ +
871 (style_->borderWidth_ * 2); /* 2: left and right border */
872 rect_.SetWidth(newWidth);
873 }
874 }
875
SetWidthPercent(float widthPercent)876 void UIView::SetWidthPercent(float widthPercent)
877 {
878 if (IsInvalid(widthPercent)) {
879 return;
880 }
881 if ((GetParent() != nullptr) && (GetParent()->GetWidth() > 1)) {
882 int16_t newWidth = static_cast<int16_t>(GetParent()->GetWidth() * widthPercent);
883 SetWidth(newWidth);
884 }
885 }
886
GetWidth()887 int16_t UIView::GetWidth()
888 {
889 return rect_.GetWidth() - (style_->paddingLeft_ + style_->paddingRight_) -
890 (style_->borderWidth_ * 2); /* 2: left and right border */
891 }
892
SetHeight(int16_t height)893 void UIView::SetHeight(int16_t height)
894 {
895 if (GetHeight() != height) {
896 int16_t newHeight = height + style_->paddingTop_ + style_->paddingBottom_ +
897 (style_->borderWidth_ * 2); /* 2: top and bottom border */
898 rect_.SetHeight(newHeight);
899 }
900 }
901
SetHeightPercent(float heightPercent)902 void UIView::SetHeightPercent(float heightPercent)
903 {
904 if (IsInvalid(heightPercent)) {
905 return;
906 }
907 if ((GetParent() != nullptr) && (GetParent()->GetHeight() > 1)) {
908 int16_t newHeight = static_cast<int16_t>(GetParent()->GetHeight() * heightPercent);
909 SetHeight(newHeight);
910 }
911 }
912
GetHeight()913 int16_t UIView::GetHeight()
914 {
915 return rect_.GetHeight() - (style_->paddingTop_ + style_->paddingBottom_) -
916 (style_->borderWidth_ * 2); /* 2: top and bottom border */
917 }
918
Resize(int16_t width,int16_t height)919 void UIView::Resize(int16_t width, int16_t height)
920 {
921 SetWidth(width);
922 SetHeight(height);
923 }
924
ResizePercent(float widthPercent,float heightPercent)925 void UIView::ResizePercent(float widthPercent, float heightPercent)
926 {
927 if (IsInvalid(widthPercent) || IsInvalid(heightPercent)) {
928 return;
929 }
930 if ((GetParent() != nullptr) && (GetParent()->GetWidth() > 1) && (GetParent()->GetHeight() > 1)) {
931 int16_t newWidth = static_cast<int16_t>(GetParent()->GetWidth() * widthPercent);
932 int16_t newHeight = static_cast<int16_t>(GetParent()->GetHeight() * heightPercent);
933 Resize(newWidth, newHeight);
934 }
935 }
936
SetX(int16_t x)937 void UIView::SetX(int16_t x)
938 {
939 if (GetX() != x) {
940 rect_.SetX(x + GetStyle(STYLE_MARGIN_LEFT));
941 }
942 }
943
SetXPercent(float xPercent)944 void UIView::SetXPercent(float xPercent)
945 {
946 if (IsInvalid(xPercent)) {
947 return;
948 }
949 if ((GetParent() != nullptr) && (GetParent()->GetWidth() > 1)) {
950 int16_t newX = static_cast<int16_t>(GetParent()->GetWidth() * xPercent);
951 SetX(newX);
952 }
953 }
954
GetX() const955 int16_t UIView::GetX() const
956 {
957 return rect_.GetX() - GetStyle(STYLE_MARGIN_LEFT);
958 }
959
SetY(int16_t y)960 void UIView::SetY(int16_t y)
961 {
962 if (GetY() != y) {
963 rect_.SetY(y + GetStyle(STYLE_MARGIN_TOP));
964 }
965 }
966
SetYPercent(float yPercent)967 void UIView::SetYPercent(float yPercent)
968 {
969 if (IsInvalid(yPercent)) {
970 return;
971 }
972 if ((GetParent() != nullptr) && (GetParent()->GetHeight() > 1)) {
973 int16_t newY = static_cast<int16_t>(GetParent()->GetHeight() * yPercent);
974 SetY(newY);
975 }
976 }
977
GetY() const978 int16_t UIView::GetY() const
979 {
980 return rect_.GetY() - GetStyle(STYLE_MARGIN_TOP);
981 }
982
SetPosition(int16_t x,int16_t y)983 void UIView::SetPosition(int16_t x, int16_t y)
984 {
985 SetX(x);
986 SetY(y);
987 }
988
SetPositionPercent(float xPercent,float yPercent)989 void UIView::SetPositionPercent(float xPercent, float yPercent)
990 {
991 if (IsInvalid(xPercent) || IsInvalid(yPercent)) {
992 return;
993 }
994 if ((GetParent() != nullptr) && (GetParent()->GetWidth() > 1) && (GetParent()->GetHeight() > 1)) {
995 int16_t newX = static_cast<int16_t>(GetParent()->GetWidth() * xPercent);
996 int16_t newY = static_cast<int16_t>(GetParent()->GetHeight() * yPercent);
997 SetPosition(newX, newY);
998 }
999 }
1000
SetPosition(int16_t x,int16_t y,int16_t width,int16_t height)1001 void UIView::SetPosition(int16_t x, int16_t y, int16_t width, int16_t height)
1002 {
1003 SetPosition(x, y);
1004 SetWidth(width);
1005 SetHeight(height);
1006 }
1007
SetPositionPercent(float xPercent,float yPercent,float widthPercent,float heightPercent)1008 void UIView::SetPositionPercent(float xPercent, float yPercent, float widthPercent, float heightPercent)
1009 {
1010 if (IsInvalid(xPercent) || IsInvalid(yPercent) || IsInvalid(widthPercent) || IsInvalid(heightPercent)) {
1011 return;
1012 }
1013 if ((GetParent() != nullptr) && (GetParent()->GetWidth() > 1) && (GetParent()->GetHeight() > 1)) {
1014 int16_t newX = static_cast<int16_t>(GetParent()->GetWidth() * xPercent);
1015 int16_t newY = static_cast<int16_t>(GetParent()->GetHeight() * yPercent);
1016 int16_t newWidth = static_cast<int16_t>(GetParent()->GetWidth() * widthPercent);
1017 int16_t newHeight = static_cast<int16_t>(GetParent()->GetHeight() * heightPercent);
1018 SetPosition(newX, newY, newWidth, newHeight);
1019 }
1020 }
1021
IsViewGroup() const1022 bool UIView::IsViewGroup() const
1023 {
1024 return isViewGroup_;
1025 }
1026
SetIntercept(bool isIntercept)1027 void UIView::SetIntercept(bool isIntercept)
1028 {
1029 isIntercept_ = isIntercept;
1030 }
1031
IsIntercept()1032 bool UIView::IsIntercept()
1033 {
1034 return isIntercept_;
1035 }
1036
IsInvalid(float percent)1037 bool UIView::IsInvalid(float percent)
1038 {
1039 if ((percent < 1) && (percent > 0)) {
1040 return false;
1041 }
1042 return true;
1043 }
1044
GetTransformMap()1045 TransformMap& UIView::GetTransformMap()
1046 {
1047 if (transMap_ == nullptr) {
1048 transMap_ = new TransformMap();
1049 }
1050 return *transMap_;
1051 }
1052
GetChildById(const char * id) const1053 UIView* UIView::GetChildById(const char* id) const
1054 {
1055 return nullptr;
1056 }
1057
SetViewId(const char * id)1058 void UIView::SetViewId(const char* id)
1059 {
1060 id_ = id;
1061 }
1062
GetViewId() const1063 const char* UIView::GetViewId() const
1064 {
1065 return id_;
1066 }
1067
SetViewIndex(int16_t index)1068 void UIView::SetViewIndex(int16_t index)
1069 {
1070 index_ = index;
1071 }
1072
GetViewIndex() const1073 int16_t UIView::GetViewIndex() const
1074 {
1075 return index_;
1076 }
1077
GetViewType() const1078 UIViewType UIView::GetViewType() const
1079 {
1080 return UI_NUMBER_MAX;
1081 }
1082
LayoutCenterOfParent(int16_t xOffset,int16_t yOffset)1083 void UIView::LayoutCenterOfParent(int16_t xOffset, int16_t yOffset)
1084 {
1085 if (parent_ == nullptr) {
1086 return;
1087 }
1088
1089 int16_t topMargin = style_->marginTop_;
1090 int16_t leftMargin = style_->marginLeft_;
1091 int16_t rightMargin = style_->marginRight_;
1092 int16_t bottomMargin = style_->marginBottom_;
1093 // 2: half
1094 int16_t posX = parent_->GetWidth() / 2 - (rect_.GetWidth() - leftMargin + rightMargin) / 2 + xOffset;
1095 // 2: half
1096 int16_t posY = parent_->GetHeight() / 2 - (rect_.GetHeight() - topMargin + bottomMargin) / 2 + yOffset;
1097 SetPosition(posX, posY);
1098 }
1099
LayoutLeftOfParent(int16_t offset)1100 void UIView::LayoutLeftOfParent(int16_t offset)
1101 {
1102 if (parent_ == nullptr) {
1103 return;
1104 }
1105
1106 int16_t leftMargin = style_->marginLeft_;
1107 SetPosition(leftMargin + offset, GetY());
1108 }
1109
LayoutRightOfParent(int16_t offset)1110 void UIView::LayoutRightOfParent(int16_t offset)
1111 {
1112 if (parent_ == nullptr) {
1113 return;
1114 }
1115
1116 int16_t rightMargin = style_->marginRight_;
1117 SetPosition(parent_->GetWidth() - offset - rect_.GetWidth() - rightMargin, GetY());
1118 }
1119
LayoutTopOfParent(int16_t offset)1120 void UIView::LayoutTopOfParent(int16_t offset)
1121 {
1122 if (parent_ == nullptr) {
1123 return;
1124 }
1125
1126 int16_t topMargin = style_->marginTop_;
1127 SetPosition(GetX(), topMargin + offset);
1128 }
1129
LayoutBottomOfParent(int16_t offset)1130 void UIView::LayoutBottomOfParent(int16_t offset)
1131 {
1132 if (parent_ == nullptr) {
1133 return;
1134 }
1135
1136 int16_t bottomMargin = style_->marginBottom_;
1137 SetPosition(GetX(), parent_->GetHeight() - offset - rect_.GetHeight() - bottomMargin);
1138 }
1139
AlignLeftToSibling(const char * id,int16_t offset)1140 void UIView::AlignLeftToSibling(const char* id, int16_t offset)
1141 {
1142 if (parent_ == nullptr) {
1143 return;
1144 }
1145 UIView* sib = parent_->GetChildById(id);
1146 if (sib != nullptr) {
1147 int16_t margin = sib->style_->marginLeft_ - style_->marginLeft_;
1148 SetPosition(sib->GetX() - margin + offset, GetY());
1149 }
1150 }
1151
AlignRightToSibling(const char * id,int16_t offset)1152 void UIView::AlignRightToSibling(const char* id, int16_t offset)
1153 {
1154 if (parent_ == nullptr) {
1155 return;
1156 }
1157 UIView* sib = parent_->GetChildById(id);
1158 if (sib != nullptr) {
1159 int16_t margin = sib->style_->marginRight_ - style_->marginRight_;
1160 SetPosition(sib->GetX() + sib->rect_.GetWidth() - rect_.GetWidth() - offset + margin, GetY());
1161 }
1162 }
1163
AlignTopToSibling(const char * id,int16_t offset)1164 void UIView::AlignTopToSibling(const char* id, int16_t offset)
1165 {
1166 if (parent_ == nullptr) {
1167 return;
1168 }
1169 UIView* sib = parent_->GetChildById(id);
1170 if (sib != nullptr) {
1171 int16_t margin = sib->style_->marginTop_ - style_->marginTop_;
1172 SetPosition(GetX(), sib->GetY() + offset - margin);
1173 }
1174 }
1175
AlignBottomToSibling(const char * id,int16_t offset)1176 void UIView::AlignBottomToSibling(const char* id, int16_t offset)
1177 {
1178 if (parent_ == nullptr) {
1179 return;
1180 }
1181 UIView* sib = parent_->GetChildById(id);
1182 if (sib != nullptr) {
1183 int16_t margin = sib->style_->marginBottom_ - style_->marginBottom_;
1184 SetPosition(GetX(), sib->GetY() + sib->rect_.GetHeight() - rect_.GetHeight() - offset + margin);
1185 }
1186 }
1187
AlignHorCenterToSibling(const char * id,int16_t offset)1188 void UIView::AlignHorCenterToSibling(const char* id, int16_t offset)
1189 {
1190 if (parent_ == nullptr) {
1191 return;
1192 }
1193 UIView* sib = parent_->GetChildById(id);
1194 if (sib != nullptr) {
1195 int16_t margin =
1196 (sib->style_->marginRight_ - sib->style_->marginLeft_ - style_->marginRight_ + style_->marginLeft_) /
1197 2; // 2 : half
1198 SetPosition(sib->GetX() + sib->rect_.GetWidth() / 2 - rect_.GetWidth() / 2 + margin + offset, GetY());
1199 }
1200 }
AlignVerCenterToSibling(const char * id,int16_t offset)1201 void UIView::AlignVerCenterToSibling(const char* id, int16_t offset)
1202 {
1203 if (parent_ == nullptr) {
1204 return;
1205 }
1206 UIView* sib = parent_->GetChildById(id);
1207 if (sib != nullptr) {
1208 int16_t margin =
1209 (sib->style_->marginBottom_ - sib->style_->marginTop_ - style_->marginBottom_ + style_->marginTop_) /
1210 2; // 2 : half
1211 SetPosition(GetX(), sib->GetY() + sib->rect_.GetHeight() / 2 - rect_.GetHeight() / 2 + margin + offset);
1212 }
1213 }
1214
LayoutLeftToSibling(const char * id,int16_t offset)1215 void UIView::LayoutLeftToSibling(const char* id, int16_t offset)
1216 {
1217 if (parent_ == nullptr) {
1218 return;
1219 }
1220 UIView* sib = parent_->GetChildById(id);
1221 if (sib != nullptr) {
1222 int16_t margin = sib->style_->marginLeft_ + style_->marginRight_;
1223 SetPosition(sib->GetX() - offset - rect_.GetWidth() - margin, GetY());
1224 }
1225 }
1226
LayoutRightToSibling(const char * id,int16_t offset)1227 void UIView::LayoutRightToSibling(const char* id, int16_t offset)
1228 {
1229 if (parent_ == nullptr) {
1230 return;
1231 }
1232 UIView* sib = parent_->GetChildById(id);
1233 if (sib != nullptr) {
1234 int16_t margin = sib->style_->marginRight_ + style_->marginLeft_;
1235 SetPosition(sib->GetX() + sib->rect_.GetWidth() + offset + margin, GetY());
1236 }
1237 }
1238
LayoutTopToSibling(const char * id,int16_t offset)1239 void UIView::LayoutTopToSibling(const char* id, int16_t offset)
1240 {
1241 if (parent_ == nullptr) {
1242 return;
1243 }
1244 UIView* sib = parent_->GetChildById(id);
1245 if (sib != nullptr) {
1246 int16_t margin = sib->style_->marginTop_ + style_->marginBottom_;
1247 SetPosition(GetX(), sib->GetY() - offset - rect_.GetHeight() - margin);
1248 }
1249 }
1250
LayoutBottomToSibling(const char * id,int16_t offset)1251 void UIView::LayoutBottomToSibling(const char* id, int16_t offset)
1252 {
1253 if (parent_ == nullptr) {
1254 return;
1255 }
1256 UIView* sib = parent_->GetChildById(id);
1257 if (sib != nullptr) {
1258 int16_t margin = sib->style_->marginBottom_ + style_->marginTop_;
1259 SetPosition(GetX(), sib->GetY() + sib->rect_.GetHeight() + offset + margin);
1260 }
1261 }
1262
GetMixOpaScale() const1263 uint8_t UIView::GetMixOpaScale() const
1264 {
1265 uint8_t opaMix = opaScale_;
1266 UIView* parent = parent_;
1267 while (parent != nullptr) {
1268 uint8_t opaParent = parent->GetOpaScale();
1269 // 8: Shift right 8 bits
1270 opaMix = (opaParent == OPA_OPAQUE) ? opaMix : ((static_cast<uint16_t>(opaParent) * opaMix) >> 8);
1271 parent = parent->GetParent();
1272 }
1273 return opaMix;
1274 }
1275
GetBitmap(ImageInfo & imageInfo,ColorMode colorMode)1276 bool UIView::GetBitmap(ImageInfo& imageInfo, ColorMode colorMode)
1277 {
1278 UIView* tempRenderSibling = nextRenderSibling_;
1279 nextRenderSibling_ = nullptr;
1280 UIView* tempParent = parent_;
1281 parent_ = nullptr;
1282 int16_t tempX = rect_.GetX();
1283 int16_t tempY = rect_.GetY();
1284 rect_.SetPosition(0, 0);
1285
1286 Rect mask = GetRect();
1287 BufferInfo bufInfo{mask, 0, nullptr, nullptr, 0, 0, colorMode, 0};
1288 bufInfo.width = mask.GetWidth();
1289 bufInfo.height = mask.GetHeight();
1290 bufInfo.stride = bufInfo.width * DrawUtils::GetByteSizeByColorMode(bufInfo.mode);
1291 BaseGfxEngine::GetInstance()->AdjustLineStride(bufInfo);
1292 imageInfo.header.colorMode = bufInfo.mode;
1293 imageInfo.dataSize = bufInfo.stride * bufInfo.height;
1294 imageInfo.header.width = bufInfo.width;
1295 imageInfo.header.height = bufInfo.height;
1296 imageInfo.header.reserved = 0;
1297
1298 bufInfo.virAddr = ImageCacheMalloc(imageInfo);
1299 if (bufInfo.virAddr == nullptr) {
1300 GRAPHIC_LOGE("GetBitmap buffer alloc failed.");
1301 nextRenderSibling_ = tempRenderSibling;
1302 parent_ = tempParent;
1303 rect_.SetPosition(tempX, tempY);
1304 return false;
1305 }
1306 imageInfo.data = reinterpret_cast<uint8_t*>(bufInfo.virAddr);
1307 if (memset_s(bufInfo.virAddr, imageInfo.dataSize, 0, imageInfo.dataSize) != EOK) {
1308 GRAPHIC_LOGE("GetBitmap buffer memset failed.");
1309 ImageCacheFree(imageInfo);
1310 imageInfo.data = nullptr;
1311 return false;
1312 }
1313 bufInfo.phyAddr = bufInfo.virAddr;
1314
1315 RootView::GetInstance()->SaveDrawContext();
1316 RootView::GetInstance()->UpdateBufferInfo(&bufInfo);
1317 RootView::GetInstance()->MeasureView(this);
1318 RootView::GetInstance()->DrawTop(this, mask);
1319 RootView::GetInstance()->RestoreDrawContext();
1320
1321 nextRenderSibling_ = tempRenderSibling;
1322 parent_ = tempParent;
1323 rect_.SetPosition(tempX, tempY);
1324 return true;
1325 }
1326
IsOnViewTree()1327 bool UIView::IsOnViewTree()
1328 {
1329 UIView* par = this;
1330 while (par->GetParent() != nullptr) {
1331 par = par->GetParent();
1332 }
1333 if (par->GetViewType() != UI_ROOT_VIEW) {
1334 return false;
1335 }
1336 return true;
1337 }
1338
GetWidthWithMargin()1339 int16_t UIView::GetWidthWithMargin()
1340 {
1341 return GetRelativeRect().GetWidth() + GetStyle(STYLE_MARGIN_LEFT) + GetStyle(STYLE_MARGIN_RIGHT);
1342 }
1343
GetHeightWithMargin()1344 int16_t UIView::GetHeightWithMargin()
1345 {
1346 return GetRelativeRect().GetHeight() + GetStyle(STYLE_MARGIN_TOP) + GetStyle(STYLE_MARGIN_BOTTOM);
1347 }
1348
GetRelativeRect() const1349 Rect UIView::GetRelativeRect() const
1350 {
1351 return rect_;
1352 }
1353
ResizeVisibleArea(int16_t x,int16_t y,int16_t width,int16_t height)1354 void UIView::ResizeVisibleArea(int16_t x, int16_t y, int16_t width, int16_t height)
1355 {
1356 if (visibleRect_ == nullptr) {
1357 visibleRect_ = new Rect();
1358 if (visibleRect_ == nullptr) {
1359 GRAPHIC_LOGE("new Rect fail");
1360 return;
1361 }
1362 }
1363 visibleRect_->SetWidth(width);
1364 visibleRect_->SetHeight(height);
1365 visibleRect_->SetPosition(x, y);
1366 }
1367
SetZIndex(int16_t zIndex)1368 void UIView::SetZIndex(int16_t zIndex)
1369 {
1370 if (zIndex_ == zIndex) {
1371 return;
1372 }
1373
1374 zIndex_ = zIndex;
1375 if (parent_ != nullptr) {
1376 reinterpret_cast<UIViewGroup*>(parent_)->UpdateRenderView(this);
1377 }
1378 }
1379
GetZIndex()1380 int16_t UIView::GetZIndex()
1381 {
1382 return zIndex_;
1383 }
1384 } // namespace OHOS
1385