2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "center-layout-impl.h"
22 #include "center-layout.h"
26 int ConvertToEven(int value)
28 return (value % 2 == 0) ? value : (value + 1);
41 Toolkit::CenterLayout CenterLayout::New()
43 // Create the implementation, temporarily owned by this handle on stack
44 IntrusivePtr< CenterLayout > impl = new CenterLayout();
46 // Pass ownership to CustomActor handle
47 Toolkit::CenterLayout handle( *impl );
49 // Second-phase init of the implementation
50 // This can only be done after the CustomActor connection has been made...
56 CenterLayout::CenterLayout()
57 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) )
61 CenterLayout::~CenterLayout()
65 void CenterLayout::OnInitialize()
67 CustomActor self = Self();
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);
73 Stage stage = Stage::GetCurrent();
74 Vector2 stageSize = stage.GetSize();
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 );
84 mPanGestureDetector = PanGestureDetector::New();
85 mPanGestureDetector.Attach( mGrabCorner );
86 mPanGestureDetector.DetectedSignal().Connect( this, &CenterLayout::OnPan );
89 void CenterLayout::OnRelayout( const Vector2& size, ActorSizeContainer& container )
91 CustomActor self = Self();
93 if( mLayoutSize.x <= 0.0f )
98 for( unsigned int i=0; i<self.GetChildCount(); ++i )
100 Dali::Toolkit::Control child = Dali::Toolkit::Control::DownCast( self.GetChildAt(i) );
104 child.SetParentOrigin( ParentOrigin::TOP_CENTER );
105 child.SetAnchorPoint( AnchorPoint::TOP_CENTER );
107 float height = child.GetHeightForWidth( size.width );
109 const Size childSize( size.width, std::min( height, size.height ) );
110 child.SetSize( childSize );
112 container.push_back( ActorSizePair( child, childSize ) );
117 void CenterLayout::OnPan( Actor actor, const PanGesture& gesture )
119 mLayoutSize.x += gesture.displacement.x * 2.0f;
120 mLayoutSize.y += gesture.displacement.y * 2.0f;
122 if( mLayoutSize.x >= 2.0f &&
123 mLayoutSize.y >= 2.0f )
125 // Avoid pixel mis-alignment issue
126 Vector2 clampedSize = Vector2( ConvertToEven(static_cast<int>(mLayoutSize.x)),
127 ConvertToEven(static_cast<int>(mLayoutSize.y)) );
129 Self().SetSize( clampedSize );
135 } // namespace Internal
137 } // namespace Toolkit