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>
27 #include <dali-toolkit/internal/controls/navigation-frame/navigation-tool-bar.h>
28 #include <dali-toolkit/internal/controls/navigation-frame/navigation-title-bar.h>
29 #include <dali-toolkit/internal/controls/relayout-controller.h>
30 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
41 namespace // to register type
46 const char* const ACTION_PUSH = "push";
47 const char* const ACTION_POP = "pop";
51 return Toolkit::NavigationControl::New();
54 TypeRegistration mType( typeid( Toolkit::NavigationControl ), typeid( Toolkit::Control ), Create );
56 TypeAction a1( mType, ACTION_PUSH, &NavigationControl::DoAction );
57 TypeAction a2( mType, ACTION_POP, &NavigationControl::DoAction );
61 NavigationControl::NavigationControl()
62 : Control( REQUIRES_TOUCH_EVENTS ),
65 mOrientationAngle( 0 ),
66 mOrientationAnimationDuration( 1.0f ),
67 mOrientationAnimationAlphaFunc( AlphaFunctions::EaseOut ),
68 mItemPositionCoefficient( Vector3( 0.0f, 1.0f, 0.0f) ),
74 NavigationControl::~NavigationControl()
76 // Clear all the items in the stack, forces their destruction before NavigationControl is destroyed.
80 void NavigationControl::OnInitialize()
82 //create layers for display background, current item, and bars respectively
83 mBackgroundLayer = CreateLayer();
84 mContentLayer = CreateLayer();
85 mBarLayer = CreateLayer();
86 mPopupLayer = CreateLayer();
89 void NavigationControl::OnControlChildAdd( Actor& child )
91 Toolkit::Page page = Toolkit::Page::DownCast(child);
93 // If it's a page then store it locally, Off stage.
96 mUnpushedItems.push_back(page);
98 // Orphan it until needed later during "push".
99 Self().Remove( child );
103 Toolkit::NavigationControl NavigationControl::New()
105 // Create the implementation, temporarily owned by this handle on stack
106 IntrusivePtr< NavigationControl > internalNavigationControl = new NavigationControl();
108 // Pass ownership to CustomActor handle
109 Toolkit::NavigationControl navigationControl( *internalNavigationControl );
111 // Second-phase init of the implementation
112 // This can only be done after the CustomActor connection has been made...
113 internalNavigationControl->Initialize();
115 return navigationControl;
118 void NavigationControl::OnStageConnection()
120 //only works when navigation control is already on stage!
121 mContentLayer.RaiseAbove( mBackgroundLayer );
122 mBarLayer.RaiseAbove( mContentLayer );
123 mPopupLayer.RaiseAbove( mBarLayer );
124 Self().SetSensitive(true);
128 void NavigationControl::PushItem( Toolkit::Page page )
130 // check the uninitialized item
131 // check the duplicated push for the top item
132 if(!page || page == mCurrentItem)
139 mContentLayer.Remove( mCurrentItem );
142 //push the new item into the stack and show it
143 mItemStack.push_back(page);
145 mContentLayer.Add(page);
146 mCurrentItem.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
148 //set up the popup menu which would response to the KEY_MENU
152 Toolkit::NavigationControl handle( GetOwner() );
153 mItemPushedSignal.Emit(handle, page);
156 Toolkit::Page NavigationControl::PopItem()
158 // cannot pop out the bottom-most item
159 Toolkit::Page poppedItem;
160 if(mItemStack.size() > 1)
162 // pop out the top item of the stack and show the new item right under the old one.
163 mContentLayer.Remove(mCurrentItem);
164 poppedItem = mItemStack.back();
165 mItemStack.pop_back();
166 mCurrentItem = mItemStack.back();
167 mContentLayer.Add(mCurrentItem);
169 //set up the popup menu which would response to the KEY_MENU
174 Toolkit::NavigationControl handle( GetOwner() );
175 mItemPoppedSignal.Emit(handle, poppedItem);
180 size_t NavigationControl::GetItemCount() const
182 return mItemStack.size();
185 Toolkit::Page NavigationControl::GetItem(std::size_t index) const
187 DALI_ASSERT_ALWAYS( index < mItemStack.size() );
188 return mItemStack[index];
191 Toolkit::Page NavigationControl::GetCurrentItem() const
196 void NavigationControl::SetBackground( Actor background)
198 // It removes the old background
201 mBackgroundLayer.Remove( mBackground );
203 mBackgroundLayer.Add( background );
204 mBackground = background;
205 mBackground.SetSize( mControlSize );
208 void NavigationControl::CreateNavigationToolBar(Toolkit::NaviToolBarStyle toolBarStylePortrait,
209 Toolkit::NaviToolBarStyle toolBarStyleLandscape )
211 // Set a navigation tool bar at the bottom of the navigation frame
212 // the controls on the tool bar will update automatically when item is pushed or popped by responding to the signals
213 mToolBar = new NavigationToolBar(*this, toolBarStylePortrait, toolBarStyleLandscape);
216 void NavigationControl::CreateNavigationTitleBar(Toolkit::NaviTitleBarStyle titleBarStylePortrait,
217 Toolkit::NaviTitleBarStyle titleBarStyleLandscape)
219 // Set a navigation title bar at the top of the navigation frame
220 // the tile/subtitle/titl icon/buttons will update automatically when item is pushed or popped by responding to the signals
221 mTitleBar = new NavigationTitleBar(*this, titleBarStylePortrait, titleBarStyleLandscape);
224 void NavigationControl::OrientationChanged( int angle )
226 if( mOrientationAngle != angle )
228 Vector2 targetSize = Vector2(GetSizeSet());
230 // checking to see if changing from landscape -> portrait, or portrait -> landscape
231 if( mOrientationAngle%180 != angle%180 )
233 targetSize = Vector2( targetSize.height, targetSize.width );
236 mOrientationAngle = angle;
242 mItemPositionCoefficient = Vector3(0.0f, 1.0f, 0.0f);
247 mItemPositionCoefficient = Vector3(1.0f, 0.0f, 0.0f);
252 mItemPositionCoefficient = Vector3(0.0f, -1.0f, 0.0f);
257 mItemPositionCoefficient = Vector3(-1.0f, 0.0f, 0.0f);
262 DALI_ASSERT_ALWAYS(false);
267 Animation animation = Animation::New( mOrientationAnimationDuration );
268 animation.RotateTo( Self(), Degree( -angle ), Vector3::ZAXIS, mOrientationAnimationAlphaFunc );
271 Self().SetSize( targetSize );
277 void NavigationControl::SetOrientationRotateAnimation( float duration, AlphaFunction alphaFunc)
279 mOrientationAnimationDuration = duration;
280 mOrientationAnimationAlphaFunc = alphaFunc;
283 Layer NavigationControl::GetBarLayer() const
288 void NavigationControl::OnRelayout( const Vector2& size, ActorSizeContainer& container )
290 const Vector2 setSize( size );
294 // always set the current item to fully occupy navigationControl space apart from the bars,
295 // here the bars might be hidden if the current item does not need them
296 float positionOffset = 0.0f;
297 float sizeShrink = 0.0f;
300 positionOffset += mTitleBar->GetBarHeight()*0.5f;
301 sizeShrink += mTitleBar->GetBarHeight();
305 positionOffset -= mToolBar->GetBarHeight()*0.5f;
306 sizeShrink += mToolBar->GetBarHeight();
308 mCurrentItem.SetPosition( mItemPositionCoefficient * positionOffset);
309 Vector2 itemSize( setSize.x, setSize.y-sizeShrink );
311 Relayout(mCurrentItem, itemSize, container);
314 container.push_back(ActorSizePair( mBarLayer, setSize ));
315 container.push_back(ActorSizePair( mPopupLayer, setSize ));
318 void NavigationControl::OnControlSizeSet( const Vector3& size )
320 if( mControlSize == Vector2(size) )
324 mControlSize = Vector2(size);
326 mBarLayer.SetSize(mControlSize);
327 mPopupLayer.SetSize(mControlSize);
331 mBackground.SetSize( mControlSize );
335 mToolBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
339 mTitleBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
343 bool NavigationControl::OnKeyEvent( const KeyEvent& event )
345 bool consumed = false;
347 if(event.state == KeyEvent::Down)
349 if(event.keyCode == 96 ) // F12 == for test
350 //if( event.keyCode == Dali::DALI_KEY_BACK || event.keyCode == Dali::DALI_KEY_ESCAPE )
352 if( mPopupMenu && mPopupMenu.IsSensitive() ) // State:POPUP_SHOW
363 if( mPopupMenu && event.keyCode == 9)
364 //if( mPopupMenu && ( event.keyCode == Dali::DALI_KEY_MENU || event.keyCode == Dali::DALI_KEY_SEND ) )
365 //Todo: replace with dali key enum after the mapping between X key definition and dali key enum is implemented in dali-adapto
366 //if( mPopupMenu && event.keyPressedName == "XF86Send" )
368 if( !mPopupMenu.IsSensitive() ) // State: POPUP_HIDE
372 else // State:POPUP_SHOW
383 Layer NavigationControl::CreateLayer()
385 Layer layer = Layer::New();
386 layer.SetPositionInheritanceMode(USE_PARENT_POSITION);
391 void NavigationControl::SetupPopupMenu()
395 mPopupLayer.Remove( mPopupMenu );
397 mPopupMenu = mCurrentItem.GetPopupMenu();
400 mPopupLayer.Add( mPopupMenu );
401 mPopupMenu.OutsideTouchedSignal().Connect(this, &NavigationControl::OnPopupTouchedOutside);
405 void NavigationControl::OnPopupTouchedOutside()
413 Toolkit::NavigationControl::ItemPushedSignalType& NavigationControl::ItemPushedSignal()
415 return mItemPushedSignal;
418 Toolkit::NavigationControl::ItemPoppedSignalType& NavigationControl::ItemPoppedSignal()
420 return mItemPoppedSignal;
423 bool NavigationControl::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
427 Dali::BaseHandle handle( object );
428 Toolkit::NavigationControl control = Toolkit::NavigationControl::DownCast( handle );
429 DALI_ASSERT_ALWAYS( control );
431 if( 0 == strcmp( actionName.c_str(), ACTION_PUSH ) )
433 for( PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter )
435 const Property::Value& value = *iter;
437 DALI_ASSERT_ALWAYS( value.GetType() == Property::STRING );
438 std::string itemName = value.Get<std::string>();
440 for( std::list<Toolkit::Page>::iterator itemsIter = GetImpl( control ).mUnpushedItems.begin(); itemsIter != GetImpl( control ).mUnpushedItems.end(); ++itemsIter )
442 Toolkit::Page page = *itemsIter;
443 if( page.GetName() == itemName )
445 GetImpl( control ).PushItem( page );
452 else if( 0 == strcmp( actionName.c_str(), ACTION_POP ) )
454 GetImpl( control ).PopItem();
462 } // namespace Internal
464 } // namespace Toolkit