2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 #include <FBaseColArrayList.h>
18 #include <FGrpCanvas.h>
19 #include <FGrpFloatRectangle.h>
20 #include <FGrpFloatPoint.h>
21 #include <FUiAnimVisualElementPropertyAnimation.h>
22 #include <FBaseSysLog.h>
23 #include "FUiAnim_VisualElement.h"
24 #include "FUiCtrl_GalleryCanvas.h"
25 #include "FUiCtrl_GalleryBitmap.h"
28 using namespace Tizen::Base;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Ui;
31 using namespace Tizen::Ui::Animations;
33 namespace Tizen { namespace Ui { namespace Controls
36 _GalleryAnimationProvider::_GalleryAnimationProvider(void)
37 : __pAnimationDelegator(null)
39 __animationTiming.duration_ms = 0;
40 __animationTiming.timingFunction = GALLERY_ANIMATION_TIMING_FUNC_LINEAR;
43 _GalleryAnimationProvider::~_GalleryAnimationProvider(void)
48 VisualElementAnimation*
49 _GalleryAnimationProvider::CreateAnimationForProperty(VisualElement& source, const String& property)
51 const IVisualElementAnimationTimingFunction* pTimingFunction = GetTimingFunc(__animationTiming.timingFunction);
53 VisualElementAnimation* pAnimation = VisualElementAnimationProvider::CreateAnimationForProperty(source, property);//dynamic_cast<VisualElementPropertyAnimation*>(visualElement.CreateAnimationForPropertyN(visualElement, VEPropTransform));
54 if (pAnimation != null)
56 result r = pAnimation->SetDuration(__animationTiming.duration_ms);
57 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
59 pAnimation->SetTimingFunction(pTimingFunction);
60 pAnimation->SetVisualElementAnimationStatusEventListener(__pAnimationDelegator);
63 SetLastResult(E_SUCCESS);
74 _GalleryAnimationProvider::SetAnimationDelegator(IVisualElementAnimationStatusEventListener* pAnimationDelegator)
76 __pAnimationDelegator = pAnimationDelegator;
81 _GalleryAnimationProvider::SetGalleryAnimationTiming(_GalleryAnimationTiming animationTiming)
83 __animationTiming = animationTiming;
87 const IVisualElementAnimationTimingFunction*
88 _GalleryAnimationProvider::GetTimingFunc(_GalleryAnimationTimingFunction timingFunction) const
90 const IVisualElementAnimationTimingFunction* pTimingFunc = null;
91 switch (timingFunction)
93 case GALLERY_ANIMATION_TIMING_FUNC_LINEAR:
94 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("Linear");
96 case GALLERY_ANIMATION_TIMING_FUNC_EASEIN:
97 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("EaseIn");
99 case GALLERY_ANIMATION_TIMING_FUNC_EASEOUT:
100 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("EaseOut");
102 case GALLERY_ANIMATION_TIMING_FUNC_EASEINOUT:
103 pTimingFunc = VisualElementAnimation::GetTimingFunctionByName("EaseInOut");
115 _GalleryCanvas::CreateGalleryCanvasN(FloatRectangle& canvasBounds)
117 result r = E_SUCCESS;
119 _GalleryCanvas* pCanvas = new (std::nothrow) _GalleryCanvas();
120 SysTryCatch(NID_UI_CTRL, pCanvas != null, , E_OUT_OF_MEMORY,
121 "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _GalleryCanvas.");
123 r = pCanvas->Construct(canvasBounds);
124 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
126 SetLastResult(E_SUCCESS);
137 _GalleryCanvas::SetImage(const _GalleryBitmap* pImage, const FloatRectangle& viewBounds, _GalleryFittingType fittingType)
144 FloatRectangle canvasBounds = GetBounds();
145 canvasBounds.width = viewBounds.width;
146 canvasBounds.height = viewBounds.height;
147 result r = SetBounds(canvasBounds);
148 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
150 _GalleryBitmap* pCanvasImage = _GalleryBitmap::CloneN(const_cast<_GalleryBitmap*>(pImage));
152 SysTryReturn(NID_UI_CTRL, pCanvasImage != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
155 __pImage = pCanvasImage;
157 if (__pImage->IsInitialize() == false || __visualElementBoundsChanged == true)
159 result r = __pImage->Initialize(__pRootCanvas->GetControlSize(), fittingType);
160 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
162 FloatDimension imageSize = GetScaledBitmapSize(*__pImage, __pRootCanvas->GetControlSize(), fittingType);
163 FloatPoint position(0,0);
164 if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_MIDDLE)
166 position.y = (__bounds.height - imageSize.height) / 2;
168 else if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_BOTTOM)
170 position.y = (__bounds.height - imageSize.height);
172 if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_CENTER)
174 position.x = (__bounds.width - imageSize.width) / 2;
176 else if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_RIGHT)
178 position.x = (__bounds.width - imageSize.width);
181 FloatRectangle visualElementBounds(position.x, position.y, imageSize.width, imageSize.height);
182 r = SetVisualElementBounds(visualElementBounds);
183 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
185 __originalImageBounds = visualElementBounds;
186 SetImageBounds(GetBounds());
188 __visualElementBoundsChanged = false;
191 SetNeedUpdateImage(true);
197 _GalleryCanvas::GetImage(void) const
203 _GalleryCanvas::SetBounds(const FloatRectangle& bounds)
205 SysTryReturnResult(NID_UI_CTRL, bounds.width >= 0.0 && bounds.height >= 0.0, E_INVALID_ARG,
206 "The bounds width or height is negative value.");
209 if (GetFrameVisualElement().IsClipChildrenEnabled() == true)
211 GetFrameVisualElement().SetBounds(bounds);
216 const FloatRectangle&
217 _GalleryCanvas::GetBounds(void) const
223 _GalleryCanvas::SetVisibility(bool visibility)
225 if (IsVisibility() == visibility)
230 GetImageVisualElement().SetImplicitAnimationEnabled(false);
231 GetImageVisualElement().SetShowState(visibility);
232 GetFrameVisualElement().SetImplicitAnimationEnabled(false);
233 GetFrameVisualElement().SetShowState(visibility);
239 _GalleryCanvas::IsVisibility(void) const
241 return GetImageVisualElement().GetShowState();
245 _GalleryCanvas::RefreshCanvas(void)
247 return GetImageVisualElement().InvalidateRectangle(null);
251 _GalleryCanvas::LoadImage(void)
253 Canvas* pCanvas = null;
254 result r = E_SUCCESS;
255 Bitmap* pScaledImage = null;
257 if (IsNeedUpdateImage() == true)
259 _GalleryBitmap* pImage = GetImage();
262 FloatRectangle rect = GetImageVisualElementBounds();
265 pCanvas = GetImageVisualElement().GetCanvasN();
267 SysTryCatch(NID_UI_CTRL, pCanvas != null, , r, "[%s] Propagating.", GetErrorMessage(r));
269 pCanvas->SetBackgroundColor(__pRootCanvas->GetBackgroundColor());
270 r = pCanvas->Clear();
271 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
273 pScaledImage = ScaledBitmapN(pImage->GetInternalBitmap(), Dimension(rect.width, rect.height));
275 SysTryCatch(NID_UI_CTRL, pScaledImage != null, , r, "[%s] Propagating.", GetErrorMessage(r));
277 r = pCanvas->DrawBitmap(Point(0,0), *pScaledImage);
278 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
280 r = GetImageVisualElement().SetFlushNeeded();
281 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
286 SetNeedUpdateImage(false);
297 _GalleryCanvas::_GalleryCanvas(void)
298 : __pImageVisualElement(null)
299 , __pFrameVisualElement(null)
301 , __bounds(0, 0, 0, 0)
303 , __visableRegionOfImage(0, 0, 0, 0)
304 , __needUpdateImage(false)
305 , __visualElementBoundsChanged(false)
306 , __imageBounds(0, 0, 0, 0)
307 , __originalImageBounds(0, 0, 0, 0)
308 , __pRootCanvas(null)
310 , __imageAnchor(0, 0)
311 , __imageVerticalAlignment(GALLERY_VERTICAL_ALIGN_MIDDLE)
312 , __imageHorizontalAlignment(GALLERY_HORIZONTAL_ALIGN_CENTER)
317 _GalleryCanvas::~_GalleryCanvas(void)
319 GetImageVisualElement().RemoveAllAnimations();
324 if (__pImageVisualElement != null)
326 __pImageVisualElement->Destroy();
329 if (__pFrameVisualElement != null)
331 __pFrameVisualElement->Destroy();
336 _GalleryCanvas::Construct(FloatRectangle& canvasBounds)
338 result r = E_SUCCESS;
339 _VisualElement* pImageVisualElement = null;
340 _VisualElement* pFrameVisualElement = null;
342 pImageVisualElement = new (std::nothrow) _VisualElement();
343 SysTryCatch(NID_UI_CTRL, pImageVisualElement != null, , E_OUT_OF_MEMORY,
344 "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _VisualElement.");
346 r = pImageVisualElement->Construct();
347 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
349 pImageVisualElement->SetImplicitAnimationEnabled(false);
350 pImageVisualElement->SetBounds(canvasBounds);
352 pImageVisualElement->SetShowState(true);
354 r = pImageVisualElement->SetSurfaceOpaque(false);
355 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
357 SetImageVisualElement(*pImageVisualElement);
359 pFrameVisualElement = new (std::nothrow) _VisualElement();
360 SysTryCatch(NID_UI_CTRL, pFrameVisualElement != null, , E_OUT_OF_MEMORY,
361 "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _VisualElement.");
362 r = pFrameVisualElement->Construct();
363 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
365 pFrameVisualElement->SetImplicitAnimationEnabled(false);
366 pFrameVisualElement->SetBounds(canvasBounds);
368 pFrameVisualElement->SetShowState(true);
370 SetFrameVisualElement(*pFrameVisualElement);
372 r = GetFrameVisualElement().AttachChild(GetImageVisualElement());
373 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
375 GetFrameVisualElement().SetClipChildrenEnabled(true);
380 if (pImageVisualElement != null)
382 pImageVisualElement->Destroy();
385 if (pFrameVisualElement != null)
387 pFrameVisualElement->Destroy();
389 __pImageVisualElement = null;
390 __pFrameVisualElement = null;
396 _GalleryCanvas::SetImageVisualElement(_VisualElement& visualElement)
398 __pImageVisualElement = &visualElement;
403 _GalleryCanvas::GetImageVisualElement(void) const
405 return *__pImageVisualElement;
409 _GalleryCanvas::SetFrameVisualElement(_VisualElement& visualElement)
411 __pFrameVisualElement = &visualElement;
416 _GalleryCanvas::GetFrameVisualElement(void) const
418 return *__pFrameVisualElement;
422 _GalleryCanvas::SetAnimation(VisualElementPropertyAnimation& animation)
424 __pAnimation = &animation;
428 VisualElementPropertyAnimation&
429 _GalleryCanvas::GetAnimation(void) const
431 return *__pAnimation;
435 _GalleryCanvas::SetNeedUpdateImage(bool needUpdate)
437 __needUpdateImage = needUpdate;
442 _GalleryCanvas::IsNeedUpdateImage(void) const
444 return __needUpdateImage;
448 _GalleryCanvas::SetCanvasFactor(float factor)
455 _GalleryCanvas::GetCanvasFactor(void) const
461 _GalleryCanvas::SetVisualElementBounds(const FloatRectangle& bounds)
463 if (GetImageVisualElementBounds() == bounds)
468 __visualElementBoundsChanged = true;
469 return GetImageVisualElement().SetBounds(bounds);
473 _GalleryCanvas::GetImageVisualElementBounds(void) const
475 return GetImageVisualElement().GetBounds();
479 _GalleryCanvas::SetImageBounds(const FloatRectangle& canvasBounds)
481 float factor = GetCanvasFactor();
482 if (__visualElementBoundsChanged == true)
486 __imageBounds.width = __originalImageBounds.width * factor;
487 __imageBounds.height = __originalImageBounds.height * factor;
489 if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_MIDDLE)
491 __imageBounds.y = (canvasBounds.height - __imageBounds.height) / 2;
493 else if (__imageVerticalAlignment == GALLERY_VERTICAL_ALIGN_BOTTOM)
495 __imageBounds.y = (canvasBounds.height - __imageBounds.height);
497 if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_CENTER)
499 __imageBounds.x = (canvasBounds.width - __imageBounds.width) / 2;
501 else if (__imageHorizontalAlignment == GALLERY_HORIZONTAL_ALIGN_RIGHT)
503 __imageBounds.x = (canvasBounds.width - __imageBounds.width);
510 _GalleryCanvas::GetOriginalImageBounds(void) const
512 return __originalImageBounds;
516 _GalleryCanvas::GetImageBounds(void) const
518 return __imageBounds;
522 _GalleryCanvas::SetAnimationDelegator(_GalleryAnimationProvider* pVEDelegator)
524 if (GetImageVisualElement().GetAnimationProvider() == pVEDelegator
525 && GetFrameVisualElement().GetAnimationProvider() == pVEDelegator)
530 result r = GetImageVisualElement().SetAnimationProvider(pVEDelegator);
531 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
533 r = GetFrameVisualElement().SetAnimationProvider(pVEDelegator);
534 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
540 _GalleryCanvas::SetRootCanvas(_GalleryRootCanvas& rootCanvas)
542 __pRootCanvas = &rootCanvas;
547 _GalleryCanvas::SetImageAnchor(Tizen::Graphics::FloatPoint anchor)
549 __imageAnchor = anchor;
554 _GalleryCanvas::GetImageAnchor(void) const
556 return __imageAnchor;
560 _GalleryCanvas::GetAlignBoundary(void) const
562 FloatRectangle imageBounds = GetImageBounds();
563 if (__originalImageBounds.width != imageBounds.width || __originalImageBounds.height != imageBounds.height)
565 imageBounds.x += GetBounds().x;
566 imageBounds.y += GetBounds().y;
571 return FloatRectangle(-1, -1, -1, -1);
576 _GalleryCanvas::SetClippingOfFrameVisualElement(bool clipping)
578 GetFrameVisualElement().SetClipChildrenEnabled(clipping);
582 _GalleryCanvas::SetImageAlignment(_GalleryVerticalAlignment imageVerticalAlignment
583 , _GalleryHorizontalAlignment imageHorizontalAlignment)
585 __imageVerticalAlignment = imageVerticalAlignment;
586 __imageHorizontalAlignment = imageHorizontalAlignment;
590 _GalleryCanvas::GetImageAlignment(_GalleryVerticalAlignment& imageVerticalAlignment
591 , _GalleryHorizontalAlignment& imageHorizontalAlignment) const
593 imageVerticalAlignment = __imageVerticalAlignment;
594 imageHorizontalAlignment = __imageHorizontalAlignment;
598 _GalleryCanvas::GetScaledBitmapSize(_GalleryBitmap& galleryBitmap, const FloatDimension& size, _GalleryFittingType fittingType) const
600 FloatDimension scaledSize = size;
601 if (fittingType == GALLERY_FITTING_TYPE_NONE)
606 if (fittingType != GALLERY_FITTING_TYPE_FIT_XY)
608 float width = galleryBitmap.GetSize().width;
609 float height = galleryBitmap.GetSize().height;
611 float widthFactor = size.width / width;
612 float heightFactor = size.height / height;
614 if (fittingType == GALLERY_FITTING_TYPE_STRECTABLE)
616 if (widthFactor < 1.0f && heightFactor < 1.0f)
621 else if (fittingType == GALLERY_FITTING_TYPE_SHRINKABLE)
623 if (widthFactor > 1.0f && heightFactor > 1.0f)
629 if (widthFactor > 1.0f && heightFactor > 1.0f)
631 scaledSize.SetSize(width, height);
635 if (widthFactor > heightFactor)
637 scaledSize.width = width * heightFactor;
641 scaledSize.height = height * widthFactor;
649 _GalleryCanvas::ScaledBitmapN(Bitmap* pSourceBitmap, Dimension scaledSize)
651 Bitmap* pScaledBitmap = new (std::nothrow) Bitmap();
652 SysTryReturn(NID_UI_CTRL, pScaledBitmap != null, null, E_OUT_OF_MEMORY,
653 "[E_OUT_OF_MEMORY] Failed to allocate the memory for the Bitmap.");
655 FloatRectangle rect(0, 0, pSourceBitmap->GetWidthF(), pSourceBitmap->GetHeightF());
657 int widthShare = rect.width / scaledSize.width;
658 int heightShare = rect.height / scaledSize.height;
660 Dimension realScaleSize = scaledSize;
662 result r = pScaledBitmap->Construct(*pSourceBitmap, rect);
663 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
665 if (widthShare > heightShare)
675 while (share > pow(2, (power + 1)))
680 r = pScaledBitmap->SetScalingQuality(BITMAP_SCALING_QUALITY_HIGH);
681 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
683 for (; power >= 0; power--)
685 realScaleSize.width = scaledSize.width * pow(2, power);
686 realScaleSize.height = scaledSize.height * pow(2, power);
687 r = pScaledBitmap->Scale(realScaleSize);
688 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
691 return pScaledBitmap;
694 delete pScaledBitmap;
700 // _GalleryRootCanvas
702 _GalleryRootCanvas::CreateGalleryRootCanvasN(_Control& control)
704 _GalleryRootCanvas* pRootCanvas = new (std::nothrow) _GalleryRootCanvas(control);
705 SysTryReturn(NID_UI_CTRL, pRootCanvas != null, null, E_OUT_OF_MEMORY,
706 "[E_OUT_OF_MEMORY] Failed to allocate the memory for the _GalleryRootCanvas.");
708 SetLastResult(E_SUCCESS);
714 _GalleryRootCanvas::SetBackgroundImage(const _GalleryBitmap* pImage)
716 __pBackgroundImage = const_cast <_GalleryBitmap*>(pImage);
721 _GalleryRootCanvas::GetBackgroundImage(void) const
723 return __pBackgroundImage;
727 _GalleryRootCanvas::GetBackgroundColor(void) const
729 return GetControl().GetBackgroundColor();
733 _GalleryRootCanvas::SetBounds(const FloatRectangle& bounds)
735 if (GetBounds() == bounds)
740 return GetControl().SetBounds(bounds);
744 _GalleryRootCanvas::GetBounds(void) const
746 return GetControl().GetBoundsF();
750 _GalleryRootCanvas::GetControlSize(void) const
752 return GetControl().GetSizeF();
756 _GalleryRootCanvas::SetVisibility(bool visibility)
758 if (IsVisibility() == visibility)
763 GetControl().SetVisibleState(visibility);
769 _GalleryRootCanvas::IsVisibility(void) const
771 return GetControl().GetVisibleState();
775 _GalleryRootCanvas::AddChildCanvas(_GalleryCanvas& canvas)
777 _VisualElement& childVisualElement = canvas.GetFrameVisualElement();
778 _VisualElement* pRootVisualElement = GetImageVisualElement();
779 SysTryReturn(NID_UI_CTRL, pRootVisualElement != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Root VisualElement is null.");
781 result r = pRootVisualElement->AttachChild(childVisualElement);
782 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
784 canvas.SetRootCanvas(*this);
790 _GalleryRootCanvas::RemoveChildCanvas(_GalleryCanvas& canvas)
792 _VisualElement& childVisualElement = canvas.GetFrameVisualElement();
793 _VisualElement* pRootVisualElement = GetImageVisualElement();
794 SysTryReturn(NID_UI_CTRL, pRootVisualElement != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Root VisualElement is null.");
796 result r = pRootVisualElement->DetachChild(childVisualElement);
797 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
803 _GalleryRootCanvas::RefreshCanvas(void)
805 GetControl().Invalidate();
809 _GalleryRootCanvas::_GalleryRootCanvas(_Control& control)
811 , __bounds(0, 0, 0, 0)
812 , __pBackgroundImage(null)
813 , __needUpdateBackgroundImage(false)
818 _GalleryRootCanvas::~_GalleryRootCanvas(void)
824 _GalleryRootCanvas::SetControl(_Control& control)
826 __pControl = &control;
831 _GalleryRootCanvas::GetControl(void) const
837 _GalleryRootCanvas::GetImageVisualElement(void) const
839 return GetControl().GetVisualElement();
842 }}} // Tizen::Ui::Controls