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