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>
25 #include <dali/public-api/object/type-registry-helper.h>
41 return Toolkit::View::New();
44 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::View, Toolkit::Control, Create )
46 DALI_SIGNAL_REGISTRATION( View, "orientation-animation-start", SIGNAL_ORIENTATION_ANIMATION_START )
48 DALI_TYPE_REGISTRATION_END()
50 const float ROTATION_ANIMATION_DURATION = 0.5f;
54 Toolkit::View View::New( bool fullscreen )
56 // Create the implementation, temporarily owned by this handle on stack
57 IntrusivePtr< View > internalView = new View(fullscreen);
59 // Pass ownership to CustomActor handle
60 Toolkit::View view( *internalView );
62 // Second-phase init of the implementation
63 // This can only be done after the CustomActor connection has been made...
64 internalView->Initialize();
69 Layer View::GetContentLayer( unsigned int index ) const
71 // Returns the layer stored in the layer map.
74 LayerConstIt it = mContentLayers.find( index );
76 if( it != mContentLayers.end() )
84 unsigned int View::AddContentLayer( Layer layer )
87 DALI_ASSERT_ALWAYS( layer );
89 unsigned int index = mNextLayerIndex;
90 LayerIt it = FindLayer( layer );
92 if( it == mContentLayers.end() )
94 // Add layer to the custom actor.
98 mContentLayers[mNextLayerIndex] = layer;
100 // Increase the index.
107 void View::RemoveContentLayer( Layer layer )
109 // Check if layer was added in this view.
110 LayerIt it = FindLayer( layer );
111 if( it != mContentLayers.end() )
113 // Remove layer from custom actor.
114 Self().Remove( layer );
116 // Remove layer from layer map.
117 mContentLayers.erase( it );
121 Layer View::GetBackgroundLayer() const
123 return mBackgroundLayer;
126 void View::SetBackground( ImageActor backgroundImage )
128 // Create background layer if doesn't exist.
130 if( !mBackgroundLayer )
132 mBackgroundLayer = Layer::New();
134 mBackgroundLayer.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
135 mBackgroundLayer.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
137 // Add background layer to custom actor.
138 Self().Add( mBackgroundLayer );
140 // Drop the background layer
142 DALI_ASSERT_ALWAYS( mBackgroundLayer.OnStage() ); // We need to be on-stage to drop the layer
143 mBackgroundLayer.LowerToBottom();
147 // It removes the old background
148 if( 0 < mBackgroundLayer.GetChildCount() )
150 mBackgroundLayer.Remove( mBackgroundLayer.GetChildAt(0) );
154 backgroundImage.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
155 backgroundImage.SetRelayoutEnabled( false ); // We will scale its size manually
156 mBackgroundLayer.Add( backgroundImage );
161 void View::SetOrientationFunction( Degree portrait, Degree landscale, Degree portraitInverse, Degree landscapeInverse )
163 mOrientationFunction[View::PORTRAIT] = portrait;
164 mOrientationFunction[View::LANDSCAPE] = landscale;
165 mOrientationFunction[View::PORTRAIT_INVERSE] = portraitInverse;
166 mOrientationFunction[View::LANDSCAPE_INVERSE] = landscapeInverse;
169 void View::OrientationChanged( Dali::Orientation orientation )
171 // Nothing to do if orientation doesn't really change.
172 if ( orientation.GetDegrees() == mOrientation || !mAutoRotateEnabled )
177 mOrientation = orientation.GetDegrees();
179 // has parent so we expect it to be on stage
180 mRotateAnimation = Animation::New( ROTATION_ANIMATION_DURATION );
181 mRotateAnimation.RotateTo( Self(), Degree( -orientation.GetDegrees() ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
186 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
187 const Vector3& currentSize( Self().GetCurrentSize() );
189 float minSize = std::min( stageSize.width, stageSize.height );
190 float maxSize = std::max( stageSize.width, stageSize.height );
193 View::Orientation viewOrientation = DegreeToViewOrientation( Degree( orientation.GetDegrees() ) );
194 switch( viewOrientation )
196 case View::PORTRAIT: // Fallthrough
197 case View::PORTRAIT_INVERSE:
198 targetSize = Vector3( minSize, maxSize, currentSize.depth );
200 case View::LANDSCAPE: // Fallthrough
201 case View::LANDSCAPE_INVERSE:
202 targetSize = Vector3( maxSize, minSize, currentSize.depth );
205 DALI_ASSERT_ALWAYS( false );
208 // if we linearly resize from portrait to landscape halfway through the animation
209 // we get size which is square between the both. This would cause a square image to grow
210 // if it is fitted to be 100% of view size. Therefore we do a nonlinear size animation
211 // where we shrink faster
213 if( targetSize.width > currentSize.width )
215 // width grows, shrink height faster
216 Vector3 shrink( currentSize );shrink.height = targetSize.height;
217 mRotateAnimation.Resize( Self(), shrink, AlphaFunctions::EaseOut, 0.0f, ROTATION_ANIMATION_DURATION * 0.5f );
218 mRotateAnimation.Resize( Self(), targetSize, AlphaFunctions::EaseIn, 0.0f, ROTATION_ANIMATION_DURATION );
222 // height grows, shrink width faster
223 Vector3 shrink( currentSize );shrink.width = targetSize.width;
224 mRotateAnimation.Resize( Self(), shrink, AlphaFunctions::EaseOut, 0.0f, ROTATION_ANIMATION_DURATION * 0.5f );
225 mRotateAnimation.Resize( Self(), targetSize, AlphaFunctions::EaseIn, 0.0f, ROTATION_ANIMATION_DURATION );
229 Toolkit::View handle( GetOwner() );
230 mOrientationAnimationStartedSignal.Emit( handle, mRotateAnimation, orientation );
232 mRotateAnimation.Play();
235 void View::SetAutoRotate( bool enabled )
237 mAutoRotateEnabled = enabled;
240 Toolkit::View::OrientationAnimationStartedSignalType& View::OrientationAnimationStartedSignal()
242 return mOrientationAnimationStartedSignal;
245 bool View::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
247 Dali::BaseHandle handle( object );
249 bool connected( true );
250 Toolkit::View view = Toolkit::View::DownCast(handle);
252 if( 0 == strcmp( signalName.c_str(), SIGNAL_ORIENTATION_ANIMATION_START ) )
254 view.OrientationAnimationStartedSignal().Connect( tracker, functor );
258 // signalName does not match any signal
265 View::View(bool fullscreen)
266 : Control( CONTROL_BEHAVIOUR_NONE ),
268 mFullScreen(fullscreen),
270 mNextLayerIndex( 0 ),
271 mOrientationFunction(),
272 mAutoRotateEnabled( true )
274 mOrientationFunction[View::PORTRAIT] = 0.f;
275 mOrientationFunction[View::LANDSCAPE] = 90.f;
276 mOrientationFunction[View::PORTRAIT_INVERSE] = 180.f;
277 mOrientationFunction[View::LANDSCAPE_INVERSE] = 270.f;
284 void View::OnInitialize()
286 Self().SetAnchorPoint( AnchorPoint::CENTER );
287 Self().SetParentOrigin( ParentOrigin::CENTER );
291 Self().SetSize( Stage::GetCurrent().GetSize() );
295 void View::OnRelayout( const Vector2& size, RelayoutContainer& container )
297 if( mBackgroundLayer )
299 if( mBackgroundLayer && mBackgroundLayer.GetChildCount() > 0 )
301 Actor background = mBackgroundLayer.GetChildAt(0);
302 background.SetScale( FillXYKeepAspectRatio( Vector3( size.width, size.height, 1.0f ), background.GetTargetSize() ) );
307 View::Orientation View::DegreeToViewOrientation( Degree degree )
309 View::Orientation orientation = PORTRAIT;
311 if( fabsf( mOrientationFunction[PORTRAIT] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT], degree ) )
313 orientation = PORTRAIT;
315 else if( fabsf( mOrientationFunction[LANDSCAPE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE], degree ) )
317 orientation = LANDSCAPE;
319 else if( fabsf( mOrientationFunction[PORTRAIT_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT_INVERSE], degree ) )
321 orientation = PORTRAIT_INVERSE;
323 else if( fabsf( mOrientationFunction[LANDSCAPE_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE_INVERSE], degree ) )
325 orientation = LANDSCAPE_INVERSE;
331 View::LayerIt View::FindLayer( Layer layer )
333 for( LayerIt it = mContentLayers.begin(); it != mContentLayers.end(); ++it )
335 if(layer == it->second)
341 return mContentLayers.end();
344 } // namespace Internal
346 } // namespace Toolkit