Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / effects-view / effects-view-example.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
18 // EXTERNAL INCLUDES
19
20 // INTERNAL INCLUDES
21 #include "shared/view.h"
22
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/effects-view/effects-view.h>
25 #include <dali/dali.h>
26 #include <sstream>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 namespace
32 {
33 const char* const TITLE("EffectsView: effect size = ");
34 const char*       TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
35 const char*       VIEW_SWAP_IMAGE(DEMO_IMAGE_DIR "icon-change.png");
36 const char*       VIEW_SWAP_SELECTED_IMAGE(DEMO_IMAGE_DIR "icon-change-selected.png");
37 const char*       TEST_IMAGE(DEMO_IMAGE_DIR "Kid1.svg");
38 } // namespace
39
40 // This example illustrates the capabilities of the EffectsView container
41 //
42 class EffectsViewApp : public ConnectionTracker
43 {
44 public:
45   /**
46    * Constructor
47    */
48   EffectsViewApp(Application& application);
49   /**
50    * Destructor
51    */
52   ~EffectsViewApp();
53
54 private:
55   /**
56    * Initialisation. This method gets called once the main loop of application is up and running
57    */
58   void OnAppInitialize(Application& application);
59
60   /**
61    * Create a effect view of drop shadow
62    *
63    * @param[in] type The type of effect to be performed by the EffectView.
64    * @param[in] viewSize Size of the effect view
65    * @param[in] effectSize The effect size used in image filters.
66    */
67   EffectsView CreateEffectsView(EffectsView::EffectType type, const Vector2& viewSize, int effectSize);
68
69   /**
70    * Animate the effect offset and color properties.
71    * @param[in] effectsView The view whose properties to be animated.
72    */
73   void AnimateEffectProperties(EffectsView& effectsView);
74
75   /**
76    * Set title onto the toolbar
77    * @param[in] effectSize The effect size value to be indicated on the title
78    */
79   void SetTitle(int effectSize);
80
81   /**
82    * Callback function to change the effect size.
83    * @param[in] button The button which triggered the callback.
84    */
85   bool ChangeEffectSize(Button button);
86
87   /**
88    * Main key event handler
89    */
90   void OnKeyEvent(const KeyEvent& event);
91
92 private:
93   Application&       mApplication;
94   Layer              mContents;
95   Toolkit::Control   mView;
96   Toolkit::ToolBar   mToolBar;
97   EffectsView        mDropShadowView;
98   EffectsView        mEmbossView;
99   Toolkit::TextLabel mTitleActor; ///< The title on the toolbar
100   Vector2            mWindowSize;
101   int                mEffectSize;
102 };
103
104 EffectsViewApp::EffectsViewApp(Application& application)
105 : mApplication(application),
106   mEffectSize(2)
107 {
108   // Connect to the Application's Init signal
109   mApplication.InitSignal().Connect(this, &EffectsViewApp::OnAppInitialize);
110 }
111
112 EffectsViewApp::~EffectsViewApp()
113 {
114   // Nothing to do here;
115 }
116
117 void EffectsViewApp::OnAppInitialize(Application& application)
118 {
119   // The Init signal is received once (only) during the Application lifetime
120
121   auto window = application.GetWindow();
122   window.KeyEventSignal().Connect(this, &EffectsViewApp::OnKeyEvent);
123   window.SetBackgroundColor(Color::WHITE);
124
125   mWindowSize = window.GetSize();
126
127   // Creates a default view with a default tool bar.
128   // The view is added to the window.
129   mContents = DemoHelper::CreateView(application, mView, mToolBar, "", TOOLBAR_IMAGE, "");
130
131   // Creates view change button.
132   Toolkit::PushButton viewButton = Toolkit::PushButton::New();
133   viewButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, VIEW_SWAP_IMAGE);
134   viewButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, VIEW_SWAP_SELECTED_IMAGE);
135   // Connects the view change button clicked signal to the OnView method.
136   viewButton.ClickedSignal().Connect(this, &EffectsViewApp::ChangeEffectSize);
137   mToolBar.AddControl(viewButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
138
139   Vector2 effectsViewSize(mWindowSize.width, mWindowSize.height * 0.25f);
140   mDropShadowView = CreateEffectsView(EffectsView::DROP_SHADOW, effectsViewSize, mEffectSize);
141   mDropShadowView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
142   mDropShadowView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
143   mDropShadowView.SetProperty(Actor::Property::POSITION_Z, -mWindowSize.height * 0.1f);
144   mContents.Add(mDropShadowView);
145
146   mEmbossView = CreateEffectsView(EffectsView::EMBOSS, effectsViewSize, mEffectSize);
147   mEmbossView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
148   mEmbossView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
149   mEmbossView.SetProperty(Actor::Property::POSITION_Z, mWindowSize.height * 0.1f);
150   mContents.Add(mEmbossView);
151
152   SetTitle(mEffectSize);
153 }
154
155 EffectsView EffectsViewApp::CreateEffectsView(EffectsView::EffectType type, const Vector2& viewSize, int effectSize)
156 {
157   Toolkit::EffectsView effectsView = Toolkit::EffectsView::New(type);
158   // set control size
159   effectsView.SetProperty(Actor::Property::SIZE, Vector2(viewSize.width, viewSize.height));
160   // set effect size property
161   effectsView.SetProperty(EffectsView::Property::EFFECT_SIZE, effectSize);
162
163   // Create some content
164   // text
165   std::string text = (type == EffectsView::DROP_SHADOW) ? "Drop Shadow" : "Emboss";
166   TextLabel   textActor(TextLabel::New(text));
167   textActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER_LEFT);
168   textActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT);
169   textActor.SetProperty(Actor::Property::SIZE, viewSize);
170   textActor.SetProperty(Actor::Property::POSITION, Vector2(viewSize.width * 0.4f, viewSize.height * 0.3f));
171   textActor.SetProperty(TextLabel::Property::POINT_SIZE, 14.f);
172   effectsView.Add(textActor);
173
174   // image
175   ImageView icon = ImageView::New(TEST_IMAGE);
176   icon.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER_LEFT);
177   icon.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT);
178   icon.SetProperty(Actor::Property::POSITION_X, viewSize.width * 0.1f);
179   icon.SetProperty(Actor::Property::SIZE, Vector2(viewSize.height * 0.8f, viewSize.height * 0.8f));
180   effectsView.Add(icon);
181
182   AnimateEffectProperties(effectsView);
183
184   return effectsView;
185 }
186
187 void EffectsViewApp::AnimateEffectProperties(EffectsView& effectsView)
188 {
189   const float animationTime(5.0f);
190   Animation   animation(Animation::New(animationTime));
191
192   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(2.f, -2.f, 0.0f), TimePeriod(animationTime * 0.0f, animationTime * 0.2f));
193   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(-2.f, -2.f, 0.0f), TimePeriod(animationTime * 0.2f, animationTime * 0.2f));
194   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(-2.f, 2.f, 0.0f), TimePeriod(animationTime * 0.4f, animationTime * 0.2f));
195   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3(4.f, 4.f, 0.0f), TimePeriod(animationTime * 0.6f, animationTime * 0.2f));
196   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_OFFSET), Vector3::ZERO, TimePeriod(animationTime * 0.8f, animationTime * 0.2f));
197
198   effectsView.SetProperty(EffectsView::Property::EFFECT_COLOR, Color::BLACK);
199   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_COLOR), Color::BLUE, TimePeriod(animationTime * 0.0f, animationTime * 0.33f));
200   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_COLOR), Color::RED, TimePeriod(animationTime * 0.33f, animationTime * 0.33f));
201   animation.AnimateTo(Property(effectsView, EffectsView::Property::EFFECT_COLOR), Color::BLACK, TimePeriod(animationTime * 0.66f, animationTime * 0.34f));
202
203   animation.SetLooping(true);
204   animation.Play();
205 }
206
207 void EffectsViewApp::SetTitle(int effectSize)
208 {
209   std::ostringstream title;
210   title << TITLE << effectSize;
211
212   if(!mTitleActor)
213   {
214     mTitleActor = DemoHelper::CreateToolBarLabel(title.str());
215     // Add title to the tool bar.
216     mToolBar.AddControl(mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HORIZONTAL_CENTER);
217   }
218   mTitleActor.SetProperty(Toolkit::TextLabel::Property::TEXT, title.str());
219 }
220
221 bool EffectsViewApp::ChangeEffectSize(Button button)
222 {
223   mEffectSize = (mEffectSize + 1) % 5;
224   mDropShadowView.SetProperty(EffectsView::Property::EFFECT_SIZE, mEffectSize);
225   mEmbossView.SetProperty(EffectsView::Property::EFFECT_SIZE, mEffectSize);
226   SetTitle(mEffectSize);
227
228   return true;
229 }
230
231 void EffectsViewApp::OnKeyEvent(const KeyEvent& event)
232 {
233   if(event.GetState() == KeyEvent::DOWN)
234   {
235     if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
236     {
237       mApplication.Quit();
238     }
239   }
240 }
241
242 /*****************************************************************************/
243
244 int DALI_EXPORT_API main(int argc, char** argv)
245 {
246   Application    application = Application::New(&argc, &argv, DEMO_THEME_PATH);
247   EffectsViewApp test(application);
248   application.MainLoop();
249   return 0;
250 }