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 <cstring> // for strcmp
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/public-api/object/type-registry-helper.h>
42 return Toolkit::View::New();
45 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::View, Toolkit::Control, Create )
47 DALI_SIGNAL_REGISTRATION( View, "orientation-animation-start", SIGNAL_ORIENTATION_ANIMATION_START )
49 DALI_TYPE_REGISTRATION_END()
51 const float ROTATION_ANIMATION_DURATION = 0.5f;
55 Toolkit::View View::New( bool fullscreen )
57 // Create the implementation, temporarily owned by this handle on stack
58 IntrusivePtr< View > internalView = new View(fullscreen);
60 // Pass ownership to CustomActor handle
61 Toolkit::View view( *internalView );
63 // Second-phase init of the implementation
64 // This can only be done after the CustomActor connection has been made...
65 internalView->Initialize();
70 Layer View::GetContentLayer( unsigned int index ) const
72 // Returns the layer stored in the layer map.
75 LayerConstIt it = mContentLayers.find( index );
77 if( it != mContentLayers.end() )
85 unsigned int View::AddContentLayer( Layer layer )
88 DALI_ASSERT_ALWAYS( layer );
90 unsigned int index = mNextLayerIndex;
91 LayerIt it = FindLayer( layer );
93 if( it == mContentLayers.end() )
95 // Add layer to the custom actor.
99 mContentLayers[mNextLayerIndex] = layer;
101 // Increase the index.
108 void View::RemoveContentLayer( Layer layer )
110 // Check if layer was added in this view.
111 LayerIt it = FindLayer( layer );
112 if( it != mContentLayers.end() )
114 // Remove layer from custom actor.
115 Self().Remove( layer );
117 // Remove layer from layer map.
118 mContentLayers.erase( it );
122 Layer View::GetBackgroundLayer() const
124 return mBackgroundLayer;
127 void View::SetBackground( ImageActor backgroundImage )
129 // Create background layer if doesn't exist.
131 if( !mBackgroundLayer )
133 mBackgroundLayer = Layer::New();
135 mBackgroundLayer.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
136 mBackgroundLayer.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
138 // Add background layer to custom actor.
139 Self().Add( mBackgroundLayer );
141 // Drop the background layer
143 DALI_ASSERT_ALWAYS( mBackgroundLayer.OnStage() ); // We need to be on-stage to drop the layer
144 mBackgroundLayer.LowerToBottom();
148 // It removes the old background
149 if( 0 < mBackgroundLayer.GetChildCount() )
151 mBackgroundLayer.Remove( mBackgroundLayer.GetChildAt(0) );
155 backgroundImage.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
156 backgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
157 backgroundImage.SetSizeScalePolicy( FILL_WITH_ASPECT_RATIO );
158 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 )
175 // Nothing to do if orientation doesn't really change.
176 if ( orientation.GetDegrees() == mOrientation || !mAutoRotateEnabled )
181 mOrientation = orientation.GetDegrees();
183 // has parent so we expect it to be on stage
184 mRotateAnimation = Animation::New( ROTATION_ANIMATION_DURATION );
185 mRotateAnimation.AnimateTo( Property( self, Actor::Property::ORIENTATION ), Quaternion( -orientation.GetRadians(), Vector3::ZAXIS ), AlphaFunctions::EaseOut );
190 const Vector2& stageSize( Stage::GetCurrent().GetSize() );
191 const Vector3& currentSize( self.GetCurrentSize() );
193 float minSize = std::min( stageSize.width, stageSize.height );
194 float maxSize = std::max( stageSize.width, stageSize.height );
197 View::Orientation viewOrientation = DegreeToViewOrientation( Degree( orientation.GetDegrees() ) );
198 switch( viewOrientation )
200 case View::PORTRAIT: // Fallthrough
201 case View::PORTRAIT_INVERSE:
202 targetSize = Vector3( minSize, maxSize, currentSize.depth );
204 case View::LANDSCAPE: // Fallthrough
205 case View::LANDSCAPE_INVERSE:
206 targetSize = Vector3( maxSize, minSize, currentSize.depth );
209 DALI_ASSERT_ALWAYS( false );
212 // if we linearly resize from portrait to landscape halfway through the animation
213 // we get size which is square between the both. This would cause a square image to grow
214 // if it is fitted to be 100% of view size. Therefore we do a nonlinear size animation
215 // where we shrink faster
217 if( targetSize.width > currentSize.width )
219 // width grows, shrink height faster
220 Vector3 shrink( currentSize );shrink.height = targetSize.height;
221 mRotateAnimation.AnimateTo( Property( self, Actor::Property::SIZE ), shrink, AlphaFunctions::EaseOut, TimePeriod( 0.0f, ROTATION_ANIMATION_DURATION * 0.5f ) );
222 mRotateAnimation.AnimateTo( Property( self, Actor::Property::SIZE ), targetSize, AlphaFunctions::EaseIn, TimePeriod( 0.0f, ROTATION_ANIMATION_DURATION ) );
226 // height grows, shrink width faster
227 Vector3 shrink( currentSize );shrink.width = targetSize.width;
228 mRotateAnimation.AnimateTo( Property( self, Actor::Property::SIZE ), shrink, AlphaFunctions::EaseOut, TimePeriod( 0.0f, ROTATION_ANIMATION_DURATION * 0.5f ) );
229 mRotateAnimation.AnimateTo( Property( self, Actor::Property::SIZE ), targetSize, AlphaFunctions::EaseIn, TimePeriod( 0.0f, ROTATION_ANIMATION_DURATION ) );
233 Toolkit::View handle( GetOwner() );
234 mOrientationAnimationStartedSignal.Emit( handle, mRotateAnimation, orientation );
236 mRotateAnimation.Play();
239 void View::SetAutoRotate( bool enabled )
241 mAutoRotateEnabled = enabled;
244 Toolkit::View::OrientationAnimationStartedSignalType& View::OrientationAnimationStartedSignal()
246 return mOrientationAnimationStartedSignal;
249 bool View::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
251 Dali::BaseHandle handle( object );
253 bool connected( true );
254 Toolkit::View view = Toolkit::View::DownCast(handle);
256 if( 0 == strcmp( signalName.c_str(), SIGNAL_ORIENTATION_ANIMATION_START ) )
258 view.OrientationAnimationStartedSignal().Connect( tracker, functor );
262 // signalName does not match any signal
269 View::View(bool fullscreen)
270 : Control( CONTROL_BEHAVIOUR_NONE ),
272 mFullScreen(fullscreen),
274 mNextLayerIndex( 0 ),
275 mOrientationFunction(),
276 mAutoRotateEnabled( true )
278 mOrientationFunction[View::PORTRAIT] = 0.f;
279 mOrientationFunction[View::LANDSCAPE] = 90.f;
280 mOrientationFunction[View::PORTRAIT_INVERSE] = 180.f;
281 mOrientationFunction[View::LANDSCAPE_INVERSE] = 270.f;
288 void View::OnInitialize()
290 Self().SetAnchorPoint( AnchorPoint::CENTER );
291 Self().SetParentOrigin( ParentOrigin::CENTER );
295 Self().SetSize( Stage::GetCurrent().GetSize() );
299 View::Orientation View::DegreeToViewOrientation( Degree degree )
301 View::Orientation orientation = PORTRAIT;
303 if( fabsf( mOrientationFunction[PORTRAIT] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT], degree ) )
305 orientation = PORTRAIT;
307 else if( fabsf( mOrientationFunction[LANDSCAPE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE], degree ) )
309 orientation = LANDSCAPE;
311 else if( fabsf( mOrientationFunction[PORTRAIT_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT_INVERSE], degree ) )
313 orientation = PORTRAIT_INVERSE;
315 else if( fabsf( mOrientationFunction[LANDSCAPE_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE_INVERSE], degree ) )
317 orientation = LANDSCAPE_INVERSE;
323 View::LayerIt View::FindLayer( Layer layer )
325 for( LayerIt it = mContentLayers.begin(); it != mContentLayers.end(); ++it )
327 if(layer == it->second)
333 return mContentLayers.end();
336 } // namespace Internal
338 } // namespace Toolkit