Clamp size in TextLabel demo
[platform/core/uifw/dali-demo.git] / examples / text-label / center-layout-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "center-layout-impl.h"
20
21 // INTERNAL INCLUDES
22 #include "center-layout.h"
23
24 namespace
25 {
26   int ConvertToEven(int value)
27   {
28     return (value % 2 == 0) ? value : (value + 1);
29   }
30 }
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 Toolkit::CenterLayout CenterLayout::New()
42 {
43   // Create the implementation, temporarily owned by this handle on stack
44   IntrusivePtr< CenterLayout > impl = new CenterLayout();
45
46   // Pass ownership to CustomActor handle
47   Toolkit::CenterLayout handle( *impl );
48
49   // Second-phase init of the implementation
50   // This can only be done after the CustomActor connection has been made...
51   impl->Initialize();
52
53   return handle;
54 }
55
56 CenterLayout::CenterLayout()
57 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
58 {
59 }
60
61 CenterLayout::~CenterLayout()
62 {
63 }
64
65 void CenterLayout::OnInitialize()
66 {
67   CustomActor self = Self();
68
69   // Move background behind text label
70   Dali::Toolkit::Control::DownCast( self ).SetBackgroundColor( Vector4(0.1f,0.1f,0.1f,1.0f) );
71   self.GetChildAt(0).SetZ(-1.0f);
72
73   Stage stage = Stage::GetCurrent();
74   Vector2 stageSize = stage.GetSize();
75
76   // Resize the center layout when the corner is grabbed
77   mGrabCorner = CreateSolidColorActor( Color::GREEN );
78   mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
79   mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
80   mGrabCorner.SetSize( stageSize.width*0.1f, stageSize.width*0.1f );
81   mGrabCorner.SetZ(1.0f);
82   self.Add( mGrabCorner );
83
84   mPanGestureDetector = PanGestureDetector::New();
85   mPanGestureDetector.Attach( mGrabCorner );
86   mPanGestureDetector.DetectedSignal().Connect( this, &CenterLayout::OnPan );
87 }
88
89 void CenterLayout::OnRelayout( const Vector2& size, ActorSizeContainer& container )
90 {
91   CustomActor self = Self();
92
93   if( mLayoutSize.x <= 0.0f )
94   {
95     mLayoutSize = size;
96   }
97
98   for( unsigned int i=0; i<self.GetChildCount(); ++i )
99   {
100     Dali::Toolkit::Control child = Dali::Toolkit::Control::DownCast( self.GetChildAt(i) );
101
102     if( child )
103     {
104       child.SetParentOrigin( ParentOrigin::TOP_CENTER );
105       child.SetAnchorPoint( AnchorPoint::TOP_CENTER );
106
107       float height = child.GetHeightForWidth( size.width );
108
109       container.push_back( ActorSizePair( child, Vector2( size.width, std::min(height, size.height) ) ) );
110     }
111   }
112 }
113
114 void CenterLayout::OnPan( Actor actor, const PanGesture& gesture )
115 {
116   mLayoutSize.x += gesture.displacement.x * 2.0f;
117   mLayoutSize.y += gesture.displacement.y * 2.0f;
118
119   if( mLayoutSize.x >= 2.0f &&
120       mLayoutSize.y >= 2.0f )
121   {
122     // Avoid pixel mis-alignment issue
123     Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
124                                    ConvertToEven(static_cast<int>(mLayoutSize.y)) );
125
126     Self().SetSize( clampedSize );
127
128     RelayoutRequest();
129   }
130 }
131
132 } // namespace Internal
133
134 } // namespace Toolkit
135
136 } // namespace Dali