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 "navigation-control-impl.h"
22 #include <dali/public-api/animation/animation.h>
23 #include <dali/public-api/events/key-event.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
28 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
29 #include <dali-toolkit/internal/controls/navigation-frame/navigation-tool-bar.h>
30 #include <dali-toolkit/internal/controls/navigation-frame/navigation-title-bar.h>
31 #include <dali-toolkit/internal/controls/relayout-controller.h>
42 namespace // to register type
47 return Toolkit::NavigationControl::New();
50 // Setup properties, signals and actions using the type-registry.
51 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::NavigationControl, Toolkit::Control, Create )
53 DALI_ACTION_REGISTRATION( NavigationControl, "push", ACTION_PUSH )
54 DALI_ACTION_REGISTRATION( NavigationControl, "pop", ACTION_POP )
56 DALI_TYPE_REGISTRATION_END()
60 NavigationControl::NavigationControl()
61 : Control( REQUIRES_TOUCH_EVENTS ),
64 mOrientationAngle( 0 ),
65 mOrientationAnimationDuration( 1.0f ),
66 mOrientationAnimationAlphaFunc( AlphaFunctions::EaseOut ),
67 mItemPositionCoefficient( Vector3( 0.0f, 1.0f, 0.0f) ),
73 NavigationControl::~NavigationControl()
75 // Clear all the items in the stack, forces their destruction before NavigationControl is destroyed.
79 void NavigationControl::OnInitialize()
81 //create layers for display background, current item, and bars respectively
82 mBackgroundLayer = CreateLayer();
83 mContentLayer = CreateLayer();
84 mBarLayer = CreateLayer();
85 mPopupLayer = CreateLayer();
88 void NavigationControl::OnControlChildAdd( Actor& child )
90 Toolkit::Page page = Toolkit::Page::DownCast(child);
92 // If it's a page then store it locally, Off stage.
95 mUnpushedItems.push_back(page);
97 // Orphan it until needed later during "push".
98 Self().Remove( child );
102 Toolkit::NavigationControl NavigationControl::New()
104 // Create the implementation, temporarily owned by this handle on stack
105 IntrusivePtr< NavigationControl > internalNavigationControl = new NavigationControl();
107 // Pass ownership to CustomActor handle
108 Toolkit::NavigationControl navigationControl( *internalNavigationControl );
110 // Second-phase init of the implementation
111 // This can only be done after the CustomActor connection has been made...
112 internalNavigationControl->Initialize();
114 return navigationControl;
117 void NavigationControl::OnStageConnection()
119 //only works when navigation control is already on stage!
120 mContentLayer.RaiseAbove( mBackgroundLayer );
121 mBarLayer.RaiseAbove( mContentLayer );
122 mPopupLayer.RaiseAbove( mBarLayer );
123 Self().SetSensitive(true);
127 void NavigationControl::PushItem( Toolkit::Page page )
129 // check the uninitialized item
130 // check the duplicated push for the top item
131 if(!page || page == mCurrentItem)
138 mContentLayer.Remove( mCurrentItem );
141 //push the new item into the stack and show it
142 mItemStack.push_back(page);
144 mContentLayer.Add(page);
145 mCurrentItem.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
147 //set up the popup menu which would response to the KEY_MENU
151 Toolkit::NavigationControl handle( GetOwner() );
152 mItemPushedSignal.Emit(handle, page);
155 Toolkit::Page NavigationControl::PopItem()
157 // cannot pop out the bottom-most item
158 Toolkit::Page poppedItem;
159 if(mItemStack.size() > 1)
161 // pop out the top item of the stack and show the new item right under the old one.
162 mContentLayer.Remove(mCurrentItem);
163 poppedItem = mItemStack.back();
164 mItemStack.pop_back();
165 mCurrentItem = mItemStack.back();
166 mContentLayer.Add(mCurrentItem);
168 //set up the popup menu which would response to the KEY_MENU
173 Toolkit::NavigationControl handle( GetOwner() );
174 mItemPoppedSignal.Emit(handle, poppedItem);
179 size_t NavigationControl::GetItemCount() const
181 return mItemStack.size();
184 Toolkit::Page NavigationControl::GetItem(std::size_t index) const
186 DALI_ASSERT_ALWAYS( index < mItemStack.size() );
187 return mItemStack[index];
190 Toolkit::Page NavigationControl::GetCurrentItem() const
195 void NavigationControl::SetBackground( Actor background)
197 // It removes the old background
200 mBackgroundLayer.Remove( mBackground );
202 mBackgroundLayer.Add( background );
203 mBackground = background;
204 mBackground.SetSize( mControlSize );
207 void NavigationControl::CreateNavigationToolBar(Toolkit::NaviToolBarStyle toolBarStylePortrait,
208 Toolkit::NaviToolBarStyle toolBarStyleLandscape )
210 // Set a navigation tool bar at the bottom of the navigation frame
211 // the controls on the tool bar will update automatically when item is pushed or popped by responding to the signals
212 mToolBar = new NavigationToolBar(*this, toolBarStylePortrait, toolBarStyleLandscape);
215 void NavigationControl::CreateNavigationTitleBar(Toolkit::NaviTitleBarStyle titleBarStylePortrait,
216 Toolkit::NaviTitleBarStyle titleBarStyleLandscape)
218 // Set a navigation title bar at the top of the navigation frame
219 // the tile/subtitle/titl icon/buttons will update automatically when item is pushed or popped by responding to the signals
220 mTitleBar = new NavigationTitleBar(*this, titleBarStylePortrait, titleBarStyleLandscape);
223 void NavigationControl::OrientationChanged( int angle )
225 if( mOrientationAngle != angle )
227 Vector2 targetSize = Vector2(GetSizeSet());
229 // checking to see if changing from landscape -> portrait, or portrait -> landscape
230 if( mOrientationAngle%180 != angle%180 )
232 targetSize = Vector2( targetSize.height, targetSize.width );
235 mOrientationAngle = angle;
241 mItemPositionCoefficient = Vector3(0.0f, 1.0f, 0.0f);
246 mItemPositionCoefficient = Vector3(1.0f, 0.0f, 0.0f);
251 mItemPositionCoefficient = Vector3(0.0f, -1.0f, 0.0f);
256 mItemPositionCoefficient = Vector3(-1.0f, 0.0f, 0.0f);
261 DALI_ASSERT_ALWAYS(false);
266 Animation animation = Animation::New( mOrientationAnimationDuration );
267 animation.RotateTo( Self(), Degree( -angle ), Vector3::ZAXIS, mOrientationAnimationAlphaFunc );
270 Self().SetSize( targetSize );
276 void NavigationControl::SetOrientationRotateAnimation( float duration, AlphaFunction alphaFunc)
278 mOrientationAnimationDuration = duration;
279 mOrientationAnimationAlphaFunc = alphaFunc;
282 Layer NavigationControl::GetBarLayer() const
287 void NavigationControl::OnRelayout( const Vector2& size, ActorSizeContainer& container )
289 const Vector2 setSize( size );
293 // always set the current item to fully occupy navigationControl space apart from the bars,
294 // here the bars might be hidden if the current item does not need them
295 float positionOffset = 0.0f;
296 float sizeShrink = 0.0f;
299 positionOffset += mTitleBar->GetBarHeight()*0.5f;
300 sizeShrink += mTitleBar->GetBarHeight();
304 positionOffset -= mToolBar->GetBarHeight()*0.5f;
305 sizeShrink += mToolBar->GetBarHeight();
307 mCurrentItem.SetPosition( mItemPositionCoefficient * positionOffset);
308 Vector2 itemSize( setSize.x, setSize.y-sizeShrink );
310 Relayout(mCurrentItem, itemSize, container);
313 container.push_back(ActorSizePair( mBarLayer, setSize ));
314 container.push_back(ActorSizePair( mPopupLayer, setSize ));
317 void NavigationControl::OnControlSizeSet( const Vector3& size )
319 if( mControlSize == Vector2(size) )
323 mControlSize = Vector2(size);
325 mBarLayer.SetSize(mControlSize);
326 mPopupLayer.SetSize(mControlSize);
330 mBackground.SetSize( mControlSize );
334 mToolBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
338 mTitleBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
342 bool NavigationControl::OnKeyEvent( const KeyEvent& event )
344 bool consumed = false;
346 if(event.state == KeyEvent::Down)
348 if(event.keyCode == 96 ) // F12 == for test
349 //if( event.keyCode == Dali::DALI_KEY_BACK || event.keyCode == Dali::DALI_KEY_ESCAPE )
351 if( mPopupMenu && mPopupMenu.IsSensitive() ) // State:POPUP_SHOW
362 if( mPopupMenu && event.keyCode == 9)
363 //if( mPopupMenu && ( event.keyCode == Dali::DALI_KEY_MENU || event.keyCode == Dali::DALI_KEY_SEND ) )
364 //Todo: replace with dali key enum after the mapping between X key definition and dali key enum is implemented in dali-adapto
365 //if( mPopupMenu && event.keyPressedName == "XF86Send" )
367 if( !mPopupMenu.IsSensitive() ) // State: POPUP_HIDE
371 else // State:POPUP_SHOW
382 Layer NavigationControl::CreateLayer()
384 Layer layer = Layer::New();
385 layer.SetPositionInheritanceMode(USE_PARENT_POSITION);
390 void NavigationControl::SetupPopupMenu()
394 mPopupLayer.Remove( mPopupMenu );
396 mPopupMenu = mCurrentItem.GetPopupMenu();
399 mPopupLayer.Add( mPopupMenu );
400 mPopupMenu.OutsideTouchedSignal().Connect(this, &NavigationControl::OnPopupTouchedOutside);
404 void NavigationControl::OnPopupTouchedOutside()
412 Toolkit::NavigationControl::ItemPushedSignalType& NavigationControl::ItemPushedSignal()
414 return mItemPushedSignal;
417 Toolkit::NavigationControl::ItemPoppedSignalType& NavigationControl::ItemPoppedSignal()
419 return mItemPoppedSignal;
422 bool NavigationControl::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
426 Dali::BaseHandle handle( object );
427 Toolkit::NavigationControl control = Toolkit::NavigationControl::DownCast( handle );
428 DALI_ASSERT_ALWAYS( control );
430 if( 0 == strcmp( actionName.c_str(), ACTION_PUSH ) )
432 for( PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter )
434 const Property::Value& value = *iter;
436 DALI_ASSERT_ALWAYS( value.GetType() == Property::STRING );
437 std::string itemName = value.Get<std::string>();
439 for( std::list<Toolkit::Page>::iterator itemsIter = GetImpl( control ).mUnpushedItems.begin(); itemsIter != GetImpl( control ).mUnpushedItems.end(); ++itemsIter )
441 Toolkit::Page page = *itemsIter;
442 if( page.GetName() == itemName )
444 GetImpl( control ).PushItem( page );
451 else if( 0 == strcmp( actionName.c_str(), ACTION_POP ) )
453 GetImpl( control ).PopItem();
461 } // namespace Internal
463 } // namespace Toolkit