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