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