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