2 * Copyright (c) 2014 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 "view-impl.h"
22 #include <dali/public-api/animation/constraints.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/object/type-registry.h>
37 namespace // to register type
42 return Toolkit::View::New();
45 TypeRegistration typeRegistration( typeid(Toolkit::View), typeid(Toolkit::Control), Create );
47 SignalConnectorType signalConnector1( typeRegistration, Toolkit::View::SIGNAL_ORIENTATION_ANIMATION_START , &View::DoConnectSignal );
54 const float ROTATION_ANIMATION_DURATION = 0.5f;
58 Toolkit::View View::New( bool fullscreen )
60 // Create the implementation, temporarily owned by this handle on stack
61 IntrusivePtr< View > internalView = new View(fullscreen);
63 // Pass ownership to CustomActor handle
64 Toolkit::View view( *internalView );
66 // Second-phase init of the implementation
67 // This can only be done after the CustomActor connection has been made...
68 internalView->Initialize();
73 Layer View::GetContentLayer( unsigned int index ) const
75 // Returns the layer stored in the layer map.
78 LayerConstIt it = mContentLayers.find( index );
80 if( it != mContentLayers.end() )
88 unsigned int View::AddContentLayer( Layer layer )
91 DALI_ASSERT_ALWAYS( layer );
93 unsigned int index = mNextLayerIndex;
94 LayerIt it = FindLayer( layer );
96 if( it == mContentLayers.end() )
98 // Add layer to the custom actor.
102 mContentLayers[mNextLayerIndex] = layer;
104 // Increase the index.
111 void View::RemoveContentLayer( Layer layer )
113 // Check if layer was added in this view.
114 LayerIt it = FindLayer( layer );
115 if( it != mContentLayers.end() )
117 // Remove layer from custom actor.
118 Self().Remove( layer );
120 // Remove layer from layer map.
121 mContentLayers.erase( it );
125 Layer View::GetBackgroundLayer() const
127 return mBackgroundLayer;
130 void View::SetBackground( ImageActor backgroundImage )
132 // Create background layer if doesn't exist.
134 if( !mBackgroundLayer )
136 mBackgroundLayer = Layer::New();
138 mBackgroundLayer.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
139 mBackgroundLayer.SetSize( mViewSize );
141 // Add background layer to custom actor.
142 Self().Add( mBackgroundLayer );
144 // Drop the background layer
146 DALI_ASSERT_ALWAYS( mBackgroundLayer.OnStage() ); // We need to be on-stage to drop the layer
147 mBackgroundLayer.LowerToBottom();
151 // It removes the old background
152 if( 0 < mBackgroundLayer.GetChildCount() )
154 mBackgroundLayer.Remove( mBackgroundLayer.GetChildAt(0) );
158 backgroundImage.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
159 backgroundImage.SetScale( FillXYKeepAspectRatio( mViewSize, backgroundImage.GetSize() ) );
160 mBackgroundLayer.Add( backgroundImage );
163 void View::SetOrientationFunction( Degree portrait, Degree landscale, Degree portraitInverse, Degree landscapeInverse )
165 mOrientationFunction[View::PORTRAIT] = portrait;
166 mOrientationFunction[View::LANDSCAPE] = landscale;
167 mOrientationFunction[View::PORTRAIT_INVERSE] = portraitInverse;
168 mOrientationFunction[View::LANDSCAPE_INVERSE] = landscapeInverse;
171 void View::OrientationChanged( Dali::Orientation orientation )
173 // Nothing to do if orientation doesn't really change.
174 if ( orientation.GetDegrees() == mOrientation || !mAutoRotateEnabled )
179 mOrientation = orientation.GetDegrees();
181 // has parent so we expect it to be on stage
182 mRotateAnimation = Animation::New( ROTATION_ANIMATION_DURATION );
183 mRotateAnimation.RotateTo( Self(), Degree( -orientation.GetDegrees() ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
188 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
189 const Vector3& currentSize( Self().GetCurrentSize() );
191 float minSize = std::min( stageSize.width, stageSize.height );
192 float maxSize = std::max( stageSize.width, stageSize.height );
195 View::Orientation viewOrientation = DegreeToViewOrientation( Degree( orientation.GetDegrees() ) );
196 switch( viewOrientation )
198 case View::PORTRAIT: // Fallthrough
199 case View::PORTRAIT_INVERSE:
200 targetSize = Vector3( minSize, maxSize, currentSize.depth );
202 case View::LANDSCAPE: // Fallthrough
203 case View::LANDSCAPE_INVERSE:
204 targetSize = Vector3( maxSize, minSize, currentSize.depth );
207 DALI_ASSERT_ALWAYS( false );
210 // if we linearly resize from portrait to landscape halfway through the animation
211 // we get size which is square between the both. This would cause a square image to grow
212 // if it is fitted to be 100% of view size. Therefore we do a nonlinear size animation
213 // where we shrink faster
215 if( targetSize.width > currentSize.width )
217 // width grows, shrink height faster
218 Vector3 shrink( currentSize );shrink.height = targetSize.height;
219 mRotateAnimation.Resize( Self(), shrink, AlphaFunctions::EaseOut, 0.0f, ROTATION_ANIMATION_DURATION * 0.5f );
220 mRotateAnimation.Resize( Self(), targetSize, AlphaFunctions::EaseIn, 0.0f, ROTATION_ANIMATION_DURATION );
224 // height grows, shrink width faster
225 Vector3 shrink( currentSize );shrink.width = targetSize.width;
226 mRotateAnimation.Resize( Self(), shrink, AlphaFunctions::EaseOut, 0.0f, ROTATION_ANIMATION_DURATION * 0.5f );
227 mRotateAnimation.Resize( Self(), targetSize, AlphaFunctions::EaseIn, 0.0f, ROTATION_ANIMATION_DURATION );
231 Toolkit::View handle( GetOwner() );
232 mOrientationAnimationStartedSignal.Emit( handle, mRotateAnimation, orientation );
234 mRotateAnimation.Play();
237 void View::SetAutoRotate( bool enabled )
239 mAutoRotateEnabled = enabled;
242 Toolkit::View::OrientationAnimationStartedSignalType& View::OrientationAnimationStartedSignal()
244 return mOrientationAnimationStartedSignal;
247 bool View::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
249 Dali::BaseHandle handle( object );
251 bool connected( true );
252 Toolkit::View view = Toolkit::View::DownCast(handle);
254 if( Toolkit::View::SIGNAL_ORIENTATION_ANIMATION_START == signalName )
256 view.OrientationAnimationStartedSignal().Connect( tracker, functor );
260 // signalName does not match any signal
267 View::View(bool fullscreen)
268 : Control( CONTROL_BEHAVIOUR_NONE ),
270 mFullScreen(fullscreen),
272 mNextLayerIndex( 0 ),
273 mOrientationFunction(),
274 mAutoRotateEnabled( true )
276 mOrientationFunction[View::PORTRAIT] = 0.f;
277 mOrientationFunction[View::LANDSCAPE] = 90.f;
278 mOrientationFunction[View::PORTRAIT_INVERSE] = 180.f;
279 mOrientationFunction[View::LANDSCAPE_INVERSE] = 270.f;
286 void View::OnInitialize()
288 Self().SetAnchorPoint( AnchorPoint::CENTER );
289 Self().SetParentOrigin( ParentOrigin::CENTER );
293 Self().SetSize( Stage::GetCurrent().GetSize() );
297 void View::OnControlSizeSet( const Vector3& targetSize )
299 mViewSize = targetSize;
300 if( mBackgroundLayer )
302 mBackgroundLayer.SetSize( mViewSize );
303 if( mBackgroundLayer.GetChildCount() > 0 )
305 Actor background = mBackgroundLayer.GetChildAt(0);
306 background.SetScale( FillXYKeepAspectRatio( mViewSize, background.GetSize() ) );
311 View::Orientation View::DegreeToViewOrientation( Degree degree )
313 View::Orientation orientation = PORTRAIT;
315 if( fabsf( mOrientationFunction[PORTRAIT] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT], degree ) )
317 orientation = PORTRAIT;
319 else if( fabsf( mOrientationFunction[LANDSCAPE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE], degree ) )
321 orientation = LANDSCAPE;
323 else if( fabsf( mOrientationFunction[PORTRAIT_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT_INVERSE], degree ) )
325 orientation = PORTRAIT_INVERSE;
327 else if( fabsf( mOrientationFunction[LANDSCAPE_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE_INVERSE], degree ) )
329 orientation = LANDSCAPE_INVERSE;
335 View::LayerIt View::FindLayer( Layer layer )
337 for( LayerIt it = mContentLayers.begin(); it != mContentLayers.end(); ++it )
339 if(layer == it->second)
345 return mContentLayers.end();
348 } // namespace Internal
350 } // namespace Toolkit