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