2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include "navigation-control-impl.h"
21 #include <dali-toolkit/internal/controls/navigation-frame/navigation-tool-bar.h>
22 #include <dali-toolkit/internal/controls/navigation-frame/navigation-title-bar.h>
23 #include <dali-toolkit/internal/controls/relayout-controller.h>
24 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
35 namespace // to register type
39 return Toolkit::NavigationControl::New();
42 TypeRegistration mType( typeid(Toolkit::NavigationControl), typeid(Toolkit::Control), Create );
44 TypeAction a1(mType, Toolkit::NavigationControl::ACTION_PUSH, &NavigationControl::DoAction);
45 TypeAction a2(mType, Toolkit::NavigationControl::ACTION_POP, &NavigationControl::DoAction);
48 NavigationControl::NavigationControl()
52 mOrientationAngle( 0 ),
53 mOrientationAnimationDuration( 1.0f ),
54 mOrientationAnimationAlphaFunc( AlphaFunctions::EaseOut ),
55 mItemPositionCoefficient( Vector3( 0.0f, 1.0f, 0.0f) ),
61 NavigationControl::~NavigationControl()
63 // Clear all the items in the stack, forces their destruction before NavigationControl is destroyed.
67 void NavigationControl::OnInitialize()
69 //create layers for display background, current item, and bars respectively
70 mBackgroundLayer = CreateLayer();
71 mContentLayer = CreateLayer();
72 mBarLayer = CreateLayer();
73 mPopupLayer = CreateLayer();
76 void NavigationControl::OnControlChildAdd( Actor& child )
78 Toolkit::Page page = Toolkit::Page::DownCast(child);
80 // If it's a page then store it locally, Off stage.
83 mUnpushedItems.push_back(page);
85 // Orphan it until needed later during "push".
86 Self().Remove( child );
90 Toolkit::NavigationControl NavigationControl::New()
92 // Create the implementation, temporarily owned by this handle on stack
93 IntrusivePtr< NavigationControl > internalNavigationControl = new NavigationControl();
95 // Pass ownership to CustomActor handle
96 Toolkit::NavigationControl navigationControl( *internalNavigationControl );
98 // Second-phase init of the implementation
99 // This can only be done after the CustomActor connection has been made...
100 internalNavigationControl->Initialize();
102 return navigationControl;
105 void NavigationControl::OnStageConnection()
107 //only works when navigation control is already on stage!
108 mContentLayer.RaiseAbove( mBackgroundLayer );
109 mBarLayer.RaiseAbove( mContentLayer );
110 mPopupLayer.RaiseAbove( mBarLayer );
111 Self().SetSensitive(true);
115 void NavigationControl::PushItem( Toolkit::Page page )
117 // check the uninitialized item
118 // check the duplicated push for the top item
119 if(!page || page == mCurrentItem)
126 mContentLayer.Remove( mCurrentItem );
129 //push the new item into the stack and show it
130 mItemStack.push_back(page);
132 mContentLayer.Add(page);
133 mCurrentItem.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
135 //set up the popup menu which would response to the KEY_MENU
139 Toolkit::NavigationControl handle( GetOwner() );
140 mItemPushedSignal.Emit(handle, page);
143 Toolkit::Page NavigationControl::PopItem()
145 // cannot pop out the bottom-most item
146 Toolkit::Page poppedItem;
147 if(mItemStack.size() > 1)
149 // pop out the top item of the stack and show the new item right under the old one.
150 mContentLayer.Remove(mCurrentItem);
151 poppedItem = mItemStack.back();
152 mItemStack.pop_back();
153 mCurrentItem = mItemStack.back();
154 mContentLayer.Add(mCurrentItem);
156 //set up the popup menu which would response to the KEY_MENU
161 Toolkit::NavigationControl handle( GetOwner() );
162 mItemPoppedSignal.Emit(handle, poppedItem);
167 size_t NavigationControl::GetItemCount() const
169 return mItemStack.size();
172 Toolkit::Page NavigationControl::GetItem(std::size_t index) const
174 DALI_ASSERT_ALWAYS( index < mItemStack.size() );
175 return mItemStack[index];
178 Toolkit::Page NavigationControl::GetCurrentItem() const
183 void NavigationControl::SetBackground( Actor background)
185 // It removes the old background
188 mBackgroundLayer.Remove( mBackground );
190 mBackgroundLayer.Add( background );
191 mBackground = background;
192 mBackground.SetSize( mControlSize );
195 void NavigationControl::CreateNavigationToolBar(Toolkit::NaviToolBarStyle toolBarStylePortrait,
196 Toolkit::NaviToolBarStyle toolBarStyleLandscape )
198 // Set a navigation tool bar at the bottom of the navigation frame
199 // the controls on the tool bar will update automatically when item is pushed or popped by responding to the signals
200 mToolBar = new NavigationToolBar(*this, toolBarStylePortrait, toolBarStyleLandscape);
203 void NavigationControl::CreateNavigationTitleBar(Toolkit::NaviTitleBarStyle titleBarStylePortrait,
204 Toolkit::NaviTitleBarStyle titleBarStyleLandscape)
206 // Set a navigation title bar at the top of the navigation frame
207 // the tile/subtitle/titl icon/buttons will update automatically when item is pushed or popped by responding to the signals
208 mTitleBar = new NavigationTitleBar(*this, titleBarStylePortrait, titleBarStyleLandscape);
211 void NavigationControl::OrientationChanged( int angle )
213 if( mOrientationAngle != angle )
215 Vector2 targetSize = Vector2(GetSizeSet());
217 // checking to see if changing from landscape -> portrait, or portrait -> landscape
218 if( mOrientationAngle%180 != angle%180 )
220 targetSize = Vector2( targetSize.height, targetSize.width );
223 mOrientationAngle = angle;
229 mItemPositionCoefficient = Vector3(0.0f, 1.0f, 0.0f);
234 mItemPositionCoefficient = Vector3(1.0f, 0.0f, 0.0f);
239 mItemPositionCoefficient = Vector3(0.0f, -1.0f, 0.0f);
244 mItemPositionCoefficient = Vector3(-1.0f, 0.0f, 0.0f);
249 DALI_ASSERT_ALWAYS(false);
254 Animation animation = Animation::New( mOrientationAnimationDuration );
255 animation.SetDestroyAction( Animation::Bake );
256 animation.RotateTo( Self(), Degree( -angle ), Vector3::ZAXIS, mOrientationAnimationAlphaFunc );
259 Self().SetSize( targetSize );
265 void NavigationControl::SetOrientationRotateAnimation( float duration, AlphaFunction alphaFunc)
267 mOrientationAnimationDuration = duration;
268 mOrientationAnimationAlphaFunc = alphaFunc;
271 Layer NavigationControl::GetBarLayer() const
276 void NavigationControl::OnRelaidOut( Vector2 size, ActorSizeContainer& container )
278 const Vector2 setSize( size );
282 // always set the current item to fully occupy navigationControl space apart from the bars,
283 // here the bars might be hidden if the current item does not need them
284 float positionOffset = 0.0f;
285 float sizeShrink = 0.0f;
288 positionOffset += mTitleBar->GetBarHeight()*0.5f;
289 sizeShrink += mTitleBar->GetBarHeight();
293 positionOffset -= mToolBar->GetBarHeight()*0.5f;
294 sizeShrink += mToolBar->GetBarHeight();
296 mCurrentItem.SetPosition( mItemPositionCoefficient * positionOffset);
297 Vector2 itemSize( setSize.x, setSize.y-sizeShrink );
299 Relayout(mCurrentItem, itemSize, container);
302 container.push_back(ActorSizePair( mBarLayer, setSize ));
303 container.push_back(ActorSizePair( mPopupLayer, setSize ));
306 void NavigationControl::OnControlSizeSet( const Vector3& size )
308 if( mControlSize == Vector2(size) )
312 mControlSize = Vector2(size);
314 mBarLayer.SetSize(mControlSize);
315 mPopupLayer.SetSize(mControlSize);
319 mBackground.SetSize( mControlSize );
323 mToolBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
327 mTitleBar->ScaleStyleUpdate( mControlSize, mOrientationAngle );
331 bool NavigationControl::OnKeyEvent( const KeyEvent& event )
333 bool consumed = false;
335 if(event.state == KeyEvent::Down)
337 if(event.keyCode == 96 ) // F12 == for test
338 //if( event.keyCode == Dali::DALI_KEY_BACK || event.keyCode == Dali::DALI_KEY_ESCAPE )
340 if( mPopupMenu && mPopupMenu.IsSensitive() ) // State:POPUP_SHOW
351 if( mPopupMenu && event.keyCode == 9)
352 //if( mPopupMenu && ( event.keyCode == Dali::DALI_KEY_MENU || event.keyCode == Dali::DALI_KEY_SEND ) )
353 //Todo: replace with dali key enum after the mapping between X key definition and dali key enum is implemented in dali-adapto
354 //if( mPopupMenu && event.keyPressedName == "XF86Send" )
356 if( !mPopupMenu.IsSensitive() ) // State: POPUP_HIDE
360 else // State:POPUP_SHOW
371 Layer NavigationControl::CreateLayer()
373 Layer layer = Layer::New();
374 layer.SetPositionInheritanceMode(USE_PARENT_POSITION);
379 void NavigationControl::SetupPopupMenu()
383 mPopupLayer.Remove( mPopupMenu );
385 mPopupMenu = mCurrentItem.GetPopupMenu();
388 mPopupLayer.Add( mPopupMenu );
389 mPopupMenu.OutsideTouchedSignal().Connect(this, &NavigationControl::OnPopupTouchedOutside);
393 void NavigationControl::OnPopupTouchedOutside()
401 Toolkit::NavigationControl::ItemPushedSignalV2& NavigationControl::ItemPushedSignal()
403 return mItemPushedSignal;
406 Toolkit::NavigationControl::ItemPoppedSignalV2& NavigationControl::ItemPoppedSignal()
408 return mItemPoppedSignal;
411 bool NavigationControl::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
415 Dali::BaseHandle handle(object);
416 Toolkit::NavigationControl control = Toolkit::NavigationControl::DownCast(handle);
417 DALI_ASSERT_ALWAYS(control);
419 if (Toolkit::NavigationControl::ACTION_PUSH == actionName)
421 for (PropertyValueConstIter iter = attributes.begin(); iter != attributes.end(); ++iter)
423 const Property::Value& value = *iter;
425 DALI_ASSERT_ALWAYS(value.GetType() == Property::STRING);
426 std::string itemName = value.Get<std::string> ();
428 for (std::list<Toolkit::Page>::iterator itemsIter = GetImpl(control).mUnpushedItems.begin(); itemsIter != GetImpl(control).mUnpushedItems.end(); ++itemsIter)
430 Toolkit::Page page = *itemsIter;
431 if (page.GetName() == itemName)
433 GetImpl(control).PushItem(page);
440 else if(Toolkit::NavigationControl::ACTION_POP == actionName)
442 GetImpl(control).PopItem();
450 } // namespace Internal
452 } // namespace Toolkit