2 * Copyright (c) 2020 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.
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>
21 #include "expanding-buttons-impl.h"
24 using namespace Dali::Toolkit;
32 const unsigned int GAP_BETWEEN_BUTTONS = 3;
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";
38 * Unparent the given number of registered controls from the supplied Vector of controls.
40 void ResetControls(std::vector<WeakHandle<Control> > controls, unsigned int numberOfButtons)
42 for(unsigned int index = 0; index < numberOfButtons; index++)
44 Dali::Toolkit::Control control = controls[index].GetHandle();
45 UnparentAndReset(control);
49 } // anonymous namespace
51 Internal::ExpandingButtons::ExpandingButtons()
52 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
53 mStyleButtonsHidden(false)
57 Internal::ExpandingButtons::~ExpandingButtons()
61 Demo::ExpandingButtons Internal::ExpandingButtons::New()
63 IntrusivePtr<Internal::ExpandingButtons> impl = new Internal::ExpandingButtons();
64 Demo::ExpandingButtons handle = Demo::ExpandingButtons(*impl);
69 void ExpandingButtons::OnInitialize()
71 mExpandButton = PushButton::New();
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);
81 void ExpandingButtons::OnRelayout(const Dali::Vector2& targetSize, Dali::RelayoutContainer& container)
83 mButtonSize = targetSize;
84 mExpandButton.SetProperty(Actor::Property::SIZE, targetSize);
87 void ExpandingButtons::RegisterButton(Dali::Toolkit::Control& control)
89 mExpandingControls.push_back(control);
92 void ExpandingButtons::Expand()
94 if(!mExpandCollapseAnimation)
96 mExpandCollapseAnimation = Animation::New(0.2f);
97 mExpandCollapseAnimation.FinishedSignal().Connect(this, &ExpandingButtons::OnExpandAnimationFinished);
100 unsigned int numberOfControls = mExpandingControls.size();
102 for(unsigned int index = 0; index < numberOfControls; index++)
104 Dali::Toolkit::Control control = mExpandingControls[index].GetHandle();
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);
113 mStyleButtonsHidden = false;
114 mExpandCollapseAnimation.Play();
117 void ExpandingButtons::OnExpandAnimationFinished(Animation& animation)
119 if(mStyleButtonsHidden)
121 unsigned int numberOfControls = mExpandingControls.size();
122 ResetControls(mExpandingControls, numberOfControls);
128 void ExpandingButtons::Collapse()
130 Demo::ExpandingButtons handle(GetOwner());
131 mCollapsedSignal.Emit(handle);
133 mStyleButtonsHidden = true;
134 mExpandButton.SetProperty(Button::Property::SELECTED, false);
136 if(mExpandCollapseAnimation)
138 unsigned int numberOfControls = mExpandingControls.size();
140 for(unsigned int index = 0; index < numberOfControls; index++)
142 Dali::Toolkit::Control control = mExpandingControls[index].GetHandle();
145 mExpandCollapseAnimation.AnimateTo(Property(control, Actor::Property::POSITION_X), 0.0f);
148 mExpandCollapseAnimation.Play();
152 // Hide or show (expand) buttons if expand button pressed
153 bool ExpandingButtons::OnExpandButtonClicked(Toolkit::Button button)
155 if(button.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>())
167 Demo::ExpandingButtons::ExpandingButtonsSignalType& ExpandingButtons::CollapsingSignal()
169 return mCollapsedSignal;
172 } // namespace Internal