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-toolkit/internal/controls/navigation-frame/navigation-tool-bar.h>
23 #include <dali-toolkit/internal/controls/navigation-frame/navigation-title-bar.h>
24 #include <dali-toolkit/internal/controls/relayout-controller.h>
25 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
36 namespace // to register type
40 return Toolkit::NavigationControl::New();
43 TypeRegistration mType( typeid(Toolkit::NavigationControl), typeid(Toolkit::Control), Create );
45 TypeAction a1(mType, Toolkit::NavigationControl::ACTION_PUSH, &NavigationControl::DoAction);
46 TypeAction a2(mType, Toolkit::NavigationControl::ACTION_POP, &NavigationControl::DoAction);
49 NavigationControl::NavigationControl()
50 : Control( REQUIRES_TOUCH_EVENTS ),
53 mOrientationAngle( 0 ),
54 mOrientationAnimationDuration( 1.0f ),
55 mOrientationAnimationAlphaFunc( AlphaFunctions::EaseOut ),
56 mItemPositionCoefficient( Vector3( 0.0f, 1.0f, 0.0f) ),
62 NavigationControl::~NavigationControl()
64 // Clear all the items in the stack, forces their destruction before NavigationControl is destroyed.
68 void NavigationControl::OnInitialize()
70 //create layers for display background, current item, and bars respectively
71 mBackgroundLayer = CreateLayer();
72 mContentLayer = CreateLayer();
73 mBarLayer = CreateLayer();
74 mPopupLayer = CreateLayer();
77 void NavigationControl::OnControlChildAdd( Actor& child )
79 Toolkit::Page page = Toolkit::Page::DownCast(child);
81 // If it's a page then store it locally, Off stage.
84 mUnpushedItems.push_back(page);
86 // Orphan it until needed later during "push".
87 Self().Remove( child );
91 Toolkit::NavigationControl NavigationControl::New()
93 // Create the implementation, temporarily owned by this handle on stack
94 IntrusivePtr< NavigationControl > internalNavigationControl = new NavigationControl();
96 // Pass ownership to CustomActor handle
97 Toolkit::NavigationControl navigationControl( *internalNavigationControl );
99 // Second-phase init of the implementation
100 // This can only be done after the CustomActor connection has been made...
101 internalNavigationControl->Initialize();
103 return navigationControl;
106 void NavigationControl::OnStageConnection()
108 //only works when navigation control is already on stage!
109 mContentLayer.RaiseAbove( mBackgroundLayer );
110 mBarLayer.RaiseAbove( mContentLayer );
111 mPopupLayer.RaiseAbove( mBarLayer );
112 Self().SetSensitive(true);
116 void NavigationControl::PushItem( Toolkit::Page page )
118 // check the uninitialized item
119 // check the duplicated push for the top item
120 if(!page || page == mCurrentItem)
127 mContentLayer.Remove( mCurrentItem );
130 //push the new item into the stack and show it
131 mItemStack.push_back(page);
133 mContentLayer.Add(page);
134 mCurrentItem.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
136 //set up the popup menu which would response to the KEY_MENU
140 Toolkit::NavigationControl handle( GetOwner() );
141 mItemPushedSignal.Emit(handle, page);
144 Toolkit::Page NavigationControl::PopItem()
146 // cannot pop out the bottom-most item
147 Toolkit::Page poppedItem;
148 if(mItemStack.size() > 1)
150 // pop out the top item of the stack and show the new item right under the old one.
151 mContentLayer.Remove(mCurrentItem);
152 poppedItem = mItemStack.back();
153 mItemStack.pop_back();
154 mCurrentItem = mItemStack.back();
155 mContentLayer.Add(mCurrentItem);
157 //set up the popup menu which would response to the KEY_MENU
162 Toolkit::NavigationControl handle( GetOwner() );
163 mItemPoppedSignal.Emit(handle, poppedItem);
168 size_t NavigationControl::GetItemCount() const
170 return mItemStack.size();
173 Toolkit::Page NavigationControl::GetItem(std::size_t index) const
175 DALI_ASSERT_ALWAYS( index < mItemStack.size() );
176 return mItemStack[index];
179 Toolkit::Page NavigationControl::GetCurrentItem() const
184 void NavigationControl::SetBackground( Actor background)
186 // It removes the old background
189 mBackgroundLayer.Remove( mBackground );
191 mBackgroundLayer.Add( background );
192 mBackground = background;
193 mBackground.SetSize( mControlSize );
196 void NavigationControl::CreateNavigationToolBar(Toolkit::NaviToolBarStyle toolBarStylePortrait,
197 Toolkit::NaviToolBarStyle toolBarStyleLandscape )
199 // Set a navigation tool bar at the bottom of the navigation frame
200 // the controls on the tool bar will update automatically when item is pushed or popped by responding to the signals
201 mToolBar = new NavigationToolBar(*this, toolBarStylePortrait, toolBarStyleLandscape);
204 void NavigationControl::CreateNavigationTitleBar(Toolkit::NaviTitleBarStyle titleBarStylePortrait,
205 Toolkit::NaviTitleBarStyle titleBarStyleLandscape)
207 // Set a navigation title bar at the top of the navigation frame
208 // the tile/subtitle/titl icon/buttons will update automatically when item is pushed or popped by responding to the signals
209 mTitleBar = new NavigationTitleBar(*this, titleBarStylePortrait, titleBarStyleLandscape);
212 void NavigationControl::OrientationChanged( int angle )
214 if( mOrientationAngle != angle )
216 Vector2 targetSize = Vector2(GetSizeSet());
218 // checking to see if changing from landscape -> portrait, or portrait -> landscape
219 if( mOrientationAngle%180 != angle%180 )
221 targetSize = Vector2( targetSize.height, targetSize.width );
224 mOrientationAngle = angle;
230 mItemPositionCoefficient = Vector3(0.0f, 1.0f, 0.0f);
235 mItemPositionCoefficient = Vector3(1.0f, 0.0f, 0.0f);
240 mItemPositionCoefficient = Vector3(0.0f, -1.0f, 0.0f);
245 mItemPositionCoefficient = Vector3(-1.0f, 0.0f, 0.0f);
250 DALI_ASSERT_ALWAYS(false);
255 Animation animation = Animation::New( mOrientationAnimationDuration );
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