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>
26 #include <dali/public-api/size-negotiation/relayout-container.h>
29 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
30 #include <dali-toolkit/internal/controls/navigation-frame/navigation-tool-bar.h>
31 #include <dali-toolkit/internal/controls/navigation-frame/navigation-title-bar.h>
32 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
43 namespace // to register type
48 return Toolkit::NavigationControl::New();
51 // Setup properties, signals and actions using the type-registry.
52 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::NavigationControl, Toolkit::Control, Create )
54 DALI_ACTION_REGISTRATION( NavigationControl, "push", ACTION_PUSH )
55 DALI_ACTION_REGISTRATION( NavigationControl, "pop", ACTION_POP )
57 DALI_TYPE_REGISTRATION_END()
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);
268 Animation animation = Animation::New( mOrientationAnimationDuration );
269 animation.AnimateTo( Property( self, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -angle ) ), Vector3::ZAXIS ), mOrientationAnimationAlphaFunc );
272 self.SetSize( targetSize );
278 void NavigationControl::SetOrientationRotateAnimation( float duration, AlphaFunction alphaFunc)
280 mOrientationAnimationDuration = duration;
281 mOrientationAnimationAlphaFunc = alphaFunc;
284 Layer NavigationControl::GetBarLayer() const
289 void NavigationControl::OnRelayout( const Vector2& size, RelayoutContainer& container )
291 const Vector2 setSize( size );
295 // always set the current item to fully occupy navigationControl space apart from the bars,
296 // here the bars might be hidden if the current item does not need them
297 float positionOffset = 0.0f;
298 float sizeShrink = 0.0f;
301 positionOffset += mTitleBar->GetBarHeight()*0.5f;
302 sizeShrink += mTitleBar->GetBarHeight();
306 positionOffset -= mToolBar->GetBarHeight()*0.5f;
307 sizeShrink += mToolBar->GetBarHeight();
309 mCurrentItem.SetPosition( mItemPositionCoefficient * positionOffset);
310 Vector2 itemSize( setSize.x, setSize.y-sizeShrink );
312 container.Add( mCurrentItem, itemSize );
315 container.Add( mBarLayer, setSize );
316 container.Add( mPopupLayer, setSize );
319 void NavigationControl::OnControlSizeSet( const Vector3& size )
321 if( mControlSize == Vector2(size) )
325 mControlSize = Vector2(size);
327 mBarLayer.SetSize(mControlSize);
328 mPopupLayer.SetSize(mControlSize);
332 mBackground.SetSize( mControlSize );
336 mToolBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
340 mTitleBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
344 bool NavigationControl::OnKeyEvent( const KeyEvent& event )
346 bool consumed = false;
348 if(event.state == KeyEvent::Down)
350 if(event.keyCode == 96 ) // F12 == for test
351 //if( event.keyCode == Dali::DALI_KEY_BACK || event.keyCode == Dali::DALI_KEY_ESCAPE )
353 if( mPopupMenu && mPopupMenu.IsSensitive() ) // State:POPUP_SHOW
364 if( mPopupMenu && event.keyCode == 9)
365 //if( mPopupMenu && ( event.keyCode == Dali::DALI_KEY_MENU || event.keyCode == Dali::DALI_KEY_SEND ) )
366 //Todo: replace with dali key enum after the mapping between X key definition and dali key enum is implemented in dali-adapto
367 //if( mPopupMenu && event.keyPressedName == "XF86Send" )
369 if( !mPopupMenu.IsSensitive() ) // State: POPUP_HIDE
373 else // State:POPUP_SHOW
384 Layer NavigationControl::CreateLayer()
386 Layer layer = Layer::New();
387 layer.SetPositionInheritanceMode(USE_PARENT_POSITION);
392 void NavigationControl::SetupPopupMenu()
396 mPopupLayer.Remove( mPopupMenu );
398 mPopupMenu = mCurrentItem.GetPopupMenu();
401 mPopupLayer.Add( mPopupMenu );
402 mPopupMenu.OutsideTouchedSignal().Connect(this, &NavigationControl::OnPopupTouchedOutside);
406 void NavigationControl::OnPopupTouchedOutside()
414 Toolkit::NavigationControl::ItemPushedSignalType& NavigationControl::ItemPushedSignal()
416 return mItemPushedSignal;
419 Toolkit::NavigationControl::ItemPoppedSignalType& NavigationControl::ItemPoppedSignal()
421 return mItemPoppedSignal;
424 bool NavigationControl::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
428 Dali::BaseHandle handle( object );
429 Toolkit::NavigationControl control = Toolkit::NavigationControl::DownCast( handle );
430 DALI_ASSERT_ALWAYS( control );
432 if( 0 == strcmp( actionName.c_str(), ACTION_PUSH ) )
434 for( PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter )
436 const Property::Value& value = *iter;
438 DALI_ASSERT_ALWAYS( value.GetType() == Property::STRING );
439 std::string itemName = value.Get<std::string>();
441 for( std::list<Toolkit::Page>::iterator itemsIter = GetImpl( control ).mUnpushedItems.begin(); itemsIter != GetImpl( control ).mUnpushedItems.end(); ++itemsIter )
443 Toolkit::Page page = *itemsIter;
444 if( page.GetName() == itemName )
446 GetImpl( control ).PushItem( page );
453 else if( 0 == strcmp( actionName.c_str(), ACTION_POP ) )
455 GetImpl( control ).PopItem();
463 } // namespace Internal
465 } // namespace Toolkit