DALi Version 1.4.17
[platform/core/uifw/dali-demo.git] / examples / text-label / expanding-buttons-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dali-toolkit/dali-toolkit.h>
18 #include <dali-toolkit/devel-api/controls/control-devel.h>
19 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
20 #include <dali/public-api/animation/animation.h>
21
22 #include "expanding-buttons-impl.h"
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace Demo
28 {
29 namespace Internal
30 {
31
32 namespace
33 {
34
35 const unsigned int GAP_BETWEEN_BUTTONS = 3;
36
37 const char* const STYLES_IMAGE = DEMO_IMAGE_DIR "FontStyleButton_Main.png";
38 const char* const TICK_IMAGE_IMAGE = DEMO_IMAGE_DIR "FontStyleButton_OK_02.png";
39
40 /**
41  * Unparent the given number of registered controls from the supplied Vector of controls.
42  */
43 void ResetControls( std::vector< WeakHandle< Control > > controls, unsigned int numberOfButtons )
44 {
45   for( unsigned int index = 0; index < numberOfButtons; index++)
46   {
47     Dali::Toolkit::Control control = controls[index].GetHandle();
48     UnparentAndReset( control );
49   }
50 }
51
52 } // anonymous namespace
53
54
55 Internal::ExpandingButtons::ExpandingButtons()
56 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
57   mStyleButtonsHidden( false )
58 {
59 }
60
61 Internal::ExpandingButtons::~ExpandingButtons()
62 {
63 }
64
65 Demo::ExpandingButtons Internal::ExpandingButtons::New()
66 {
67   IntrusivePtr<Internal::ExpandingButtons> impl = new Internal::ExpandingButtons();
68   Demo::ExpandingButtons handle = Demo::ExpandingButtons( *impl );
69   impl->Initialize();
70   return handle;
71 }
72
73 void ExpandingButtons::OnInitialize()
74 {
75   mExpandButton = PushButton::New();
76
77   mExpandButton.ClickedSignal().Connect( this, &ExpandingButtons::OnExpandButtonClicked );
78   mExpandButton.SetProperty( Button::Property::TOGGLABLE, true );
79   mExpandButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, STYLES_IMAGE ); // Default for Styles
80   mExpandButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, TICK_IMAGE_IMAGE );
81   mExpandButton.SetProperty( Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
82   Self().Add( mExpandButton );
83 }
84
85 void ExpandingButtons::OnRelayout( const Dali::Vector2& targetSize, Dali::RelayoutContainer& container )
86 {
87   mButtonSize = targetSize;
88   mExpandButton.SetSize( targetSize );
89 }
90
91 void ExpandingButtons::RegisterButton( Dali::Toolkit::Control& control )
92 {
93   mExpandingControls.push_back( control );
94 }
95
96 void ExpandingButtons::Expand()
97 {
98   if ( !mExpandCollapseAnimation )
99   {
100     mExpandCollapseAnimation = Animation::New( 0.2f );
101     mExpandCollapseAnimation.FinishedSignal().Connect( this, &ExpandingButtons::OnExpandAnimationFinished );
102   }
103
104   unsigned int numberOfControls = mExpandingControls.size();
105
106   for( unsigned int index = 0; index < numberOfControls; index++ )
107   {
108     Dali::Toolkit::Control control = mExpandingControls[index].GetHandle();
109     if ( control )
110     {
111       Self().Add( control );
112       AlphaFunction focusedAlphaFunction = AlphaFunction( Vector2 ( 0.32f, 0.08f ), Vector2( 0.38f, 1.72f ) );
113       mExpandCollapseAnimation.AnimateTo( Property( control, Actor::Property::POSITION_X ), mButtonSize.width + ( mButtonSize.width + GAP_BETWEEN_BUTTONS ) * (index) , focusedAlphaFunction );
114     }
115   }
116   Self().RaiseToTop();
117   mStyleButtonsHidden = false;
118   mExpandCollapseAnimation.Play();
119 }
120
121 void ExpandingButtons::OnExpandAnimationFinished( Animation& animation )
122 {
123   if ( mStyleButtonsHidden )
124   {
125     unsigned int numberOfControls = mExpandingControls.size();
126     ResetControls( mExpandingControls, numberOfControls );
127     animation.Clear();
128     animation.Reset();
129   }
130 }
131
132 void ExpandingButtons::Collapse()
133 {
134   Demo::ExpandingButtons handle( GetOwner() );
135   mCollapsedSignal.Emit( handle );
136
137   mStyleButtonsHidden = true;
138   mExpandButton.SetProperty(Button::Property::SELECTED, false );
139
140   if ( mExpandCollapseAnimation )
141   {
142     unsigned int numberOfControls = mExpandingControls.size();
143
144     for ( unsigned int index = 0; index < numberOfControls; index++ )
145     {
146       Dali::Toolkit::Control control = mExpandingControls[index].GetHandle();
147       if ( control )
148       {
149         mExpandCollapseAnimation.AnimateTo( Property( control, Actor::Property::POSITION_X ), 0.0f );
150       }
151     }
152     mExpandCollapseAnimation.Play();
153   }
154 }
155
156 // Hide or show (expand) buttons if expand button pressed
157 bool ExpandingButtons::OnExpandButtonClicked( Toolkit::Button button )
158 {
159   if ( button.GetProperty( Toolkit::Button::Property::SELECTED ).Get<bool>() )
160   {
161     Expand();
162   }
163   else
164   {
165     Collapse();
166   }
167
168   return true;
169 }
170
171
172 Demo::ExpandingButtons::ExpandingButtonsSignalType& ExpandingButtons::CollapsingSignal()
173 {
174   return mCollapsedSignal;
175 }
176
177 } // Internal
178 } // Demo