Include required header files directly rather than through dali.h
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / internal / controls / view / view-impl.cpp
1 /*
2  * Copyright (c) 2014 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 "view-impl.h"
20
21 // EXTERNAL INCLUDES
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
26 // INTERNAL INCLUDES
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace // to register type
38 {
39
40 BaseHandle Create()
41 {
42   return Toolkit::View::New();
43 }
44
45 TypeRegistration typeRegistration( typeid(Toolkit::View), typeid(Toolkit::Control), Create );
46
47 SignalConnectorType signalConnector1( typeRegistration, Toolkit::View::SIGNAL_ORIENTATION_ANIMATION_START , &View::DoConnectSignal );
48
49 }
50
51 namespace
52 {
53
54 const float ROTATION_ANIMATION_DURATION = 0.5f;
55
56 }
57
58 Toolkit::View View::New( bool fullscreen )
59 {
60   // Create the implementation, temporarily owned by this handle on stack
61   IntrusivePtr< View > internalView = new View(fullscreen);
62
63   // Pass ownership to CustomActor handle
64   Toolkit::View view( *internalView );
65
66   // Second-phase init of the implementation
67   // This can only be done after the CustomActor connection has been made...
68   internalView->Initialize();
69
70   return view;
71 }
72
73 Layer View::GetContentLayer( unsigned int index ) const
74 {
75   // Returns the layer stored in the layer map.
76   Layer layer;
77
78   LayerConstIt it = mContentLayers.find( index );
79
80   if( it != mContentLayers.end() )
81   {
82     layer = it->second;
83   }
84
85   return layer;
86 }
87
88 unsigned int View::AddContentLayer( Layer layer )
89 {
90   // layer must exist.
91   DALI_ASSERT_ALWAYS( layer );
92
93   unsigned int index = mNextLayerIndex;
94   LayerIt it = FindLayer( layer );
95
96   if( it == mContentLayers.end() )
97   {
98     // Add layer to the custom actor.
99     Self().Add( layer );
100
101     // Store the layer.
102     mContentLayers[mNextLayerIndex] = layer;
103
104     // Increase the index.
105     ++mNextLayerIndex;
106   }
107
108   return index;
109 }
110
111 void View::RemoveContentLayer( Layer layer )
112 {
113   // Check if layer was added in this view.
114   LayerIt it = FindLayer( layer );
115   if( it != mContentLayers.end() )
116   {
117     // Remove layer from custom actor.
118     Self().Remove( layer );
119
120     // Remove layer from layer map.
121     mContentLayers.erase( it );
122   }
123 }
124
125 Layer View::GetBackgroundLayer() const
126 {
127   return mBackgroundLayer;
128 }
129
130 void View::SetBackground( ImageActor backgroundImage )
131 {
132   // Create background layer if doesn't exist.
133
134   if( !mBackgroundLayer )
135   {
136     mBackgroundLayer = Layer::New();
137
138     mBackgroundLayer.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
139     mBackgroundLayer.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
140
141     // Add background layer to custom actor.
142     Self().Add( mBackgroundLayer );
143
144     // Drop the background layer
145
146     DALI_ASSERT_ALWAYS( mBackgroundLayer.OnStage() ); // We need to be on-stage to drop the layer
147     mBackgroundLayer.LowerToBottom();
148   }
149   else
150   {
151     // It removes the old background
152     if( 0 < mBackgroundLayer.GetChildCount() )
153     {
154       mBackgroundLayer.Remove( mBackgroundLayer.GetChildAt(0) );
155     }
156   }
157
158   backgroundImage.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
159   Constraint constraint = Constraint::New<Vector3>(
160       Actor::SCALE,
161       LocalSource( Actor::SIZE ),
162       ParentSource( Actor::SIZE ),
163       ScaleToFillXYKeepAspectRatioConstraint() );
164   backgroundImage.ApplyConstraint( constraint );
165   mBackgroundLayer.Add( backgroundImage );
166 }
167
168 void View::SetOrientationFunction( Degree portrait, Degree landscale, Degree portraitInverse, Degree landscapeInverse )
169 {
170   mOrientationFunction[View::PORTRAIT] = portrait;
171   mOrientationFunction[View::LANDSCAPE] = landscale;
172   mOrientationFunction[View::PORTRAIT_INVERSE] = portraitInverse;
173   mOrientationFunction[View::LANDSCAPE_INVERSE] = landscapeInverse;
174 }
175
176 void View::OrientationChanged( Dali::Orientation orientation )
177 {
178   // Nothing to do if orientation doesn't really change.
179   if ( orientation.GetDegrees() == mOrientation || !mAutoRotateEnabled )
180   {
181     return;
182   }
183
184   mOrientation = orientation.GetDegrees();
185
186   // has parent so we expect it to be on stage
187   mRotateAnimation = Animation::New( ROTATION_ANIMATION_DURATION );
188   mRotateAnimation.RotateTo( Self(), Degree( -orientation.GetDegrees() ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
189
190   // Resize the view
191   if( mFullScreen )
192   {
193     const Vector2& stageSize( Stage::GetCurrent().GetSize() );
194     const Vector3& currentSize( Self().GetCurrentSize() );
195
196     float minSize = std::min( stageSize.width, stageSize.height );
197     float maxSize = std::max( stageSize.width, stageSize.height );
198
199     Vector3 targetSize;
200     View::Orientation viewOrientation = DegreeToViewOrientation( Degree( orientation.GetDegrees() ) );
201     switch( viewOrientation )
202     {
203       case View::PORTRAIT:          // Fallthrough
204       case View::PORTRAIT_INVERSE:
205         targetSize = Vector3( minSize, maxSize, currentSize.depth );
206         break;
207       case View::LANDSCAPE:         // Fallthrough
208       case View::LANDSCAPE_INVERSE:
209         targetSize = Vector3( maxSize, minSize, currentSize.depth );
210         break;
211       default:
212         DALI_ASSERT_ALWAYS( false );
213     }
214
215     // if we linearly resize from portrait to landscape halfway through the animation
216     // we get size which is square between the both. This would cause a square image to grow
217     // if it is fitted to be 100% of view size. Therefore we do a nonlinear size animation
218     // where we shrink faster
219     // which one grows
220     if( targetSize.width > currentSize.width )
221     {
222       // width grows, shrink height faster
223       Vector3 shrink( currentSize );shrink.height = targetSize.height;
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 );
226     }
227     else
228     {
229       // height grows, shrink width faster
230       Vector3 shrink( currentSize );shrink.width = targetSize.width;
231       mRotateAnimation.Resize( Self(), shrink, AlphaFunctions::EaseOut, 0.0f, ROTATION_ANIMATION_DURATION * 0.5f );
232       mRotateAnimation.Resize( Self(), targetSize, AlphaFunctions::EaseIn, 0.0f, ROTATION_ANIMATION_DURATION );
233     }
234   }
235
236   Toolkit::View handle( GetOwner() );
237   mOrientationAnimationStartedSignalV2.Emit( handle, mRotateAnimation, orientation );
238
239   mRotateAnimation.Play();
240 }
241
242 void View::SetAutoRotate( bool enabled )
243 {
244   mAutoRotateEnabled = enabled;
245 }
246
247 Toolkit::View::OrientationAnimationStartedSignalV2& View::OrientationAnimationStartedSignal()
248 {
249   return mOrientationAnimationStartedSignalV2;
250 }
251
252 bool View::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
253 {
254   Dali::BaseHandle handle( object );
255
256   bool connected( true );
257   Toolkit::View view = Toolkit::View::DownCast(handle);
258
259   if( Toolkit::View::SIGNAL_ORIENTATION_ANIMATION_START == signalName )
260   {
261     view.OrientationAnimationStartedSignal().Connect( tracker, functor );
262   }
263   else
264   {
265     // signalName does not match any signal
266     connected = false;
267   }
268
269   return connected;
270 }
271
272 View::View(bool fullscreen)
273 : Control( CONTROL_BEHAVIOUR_NONE ),
274   mOrientation( -1 ),
275   mFullScreen(fullscreen),
276   mContentLayers(),
277   mNextLayerIndex( 0 ),
278   mOrientationFunction(),
279   mAutoRotateEnabled( true )
280 {
281   mOrientationFunction[View::PORTRAIT] = 0.f;
282   mOrientationFunction[View::LANDSCAPE] = 90.f;
283   mOrientationFunction[View::PORTRAIT_INVERSE] = 180.f;
284   mOrientationFunction[View::LANDSCAPE_INVERSE] =  270.f;
285 }
286
287 View::~View()
288 {
289 }
290
291 void View::OnInitialize()
292 {
293   Self().SetAnchorPoint( AnchorPoint::CENTER );
294   Self().SetParentOrigin( ParentOrigin::CENTER );
295
296   if( mFullScreen )
297   {
298     Self().SetSize( Stage::GetCurrent().GetSize() );
299   }
300 }
301
302 View::Orientation View::DegreeToViewOrientation( Degree degree )
303 {
304   View::Orientation orientation = PORTRAIT;
305
306   if( fabsf( mOrientationFunction[PORTRAIT] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT], degree ) )
307   {
308     orientation =  PORTRAIT;
309   }
310   else if( fabsf( mOrientationFunction[LANDSCAPE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE], degree ) )
311   {
312     orientation = LANDSCAPE;
313   }
314   else if( fabsf( mOrientationFunction[PORTRAIT_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[PORTRAIT_INVERSE], degree ) )
315   {
316     orientation = PORTRAIT_INVERSE;
317   }
318   else if( fabsf( mOrientationFunction[LANDSCAPE_INVERSE] - degree ) <= GetRangedEpsilon( mOrientationFunction[LANDSCAPE_INVERSE], degree ) )
319   {
320     orientation = LANDSCAPE_INVERSE;
321   }
322
323   return orientation;
324 }
325
326 View::LayerIt View::FindLayer( Layer layer )
327 {
328   for( LayerIt it = mContentLayers.begin(); it != mContentLayers.end(); ++it )
329   {
330     if(layer == it->second)
331     {
332       return it;
333     }
334   }
335
336   return mContentLayers.end();
337 }
338
339 } // namespace Internal
340
341 } // namespace Toolkit
342
343 } // namespace Dali