Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / gaussian-blur-view / gaussian-blur-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 #include <algorithm>
19
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali-toolkit/devel-api/controls/gaussian-blur-view/gaussian-blur-view.h>
22
23 using namespace Dali;
24 using Dali::Toolkit::TextLabel;
25 using Dali::Toolkit::GaussianBlurView;
26
27 namespace
28 {
29
30 const char* const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" );
31 const float BACKGROUND_IMAGE_WIDTH = 2048.0f;
32
33 }
34
35 /**
36  * This example shows a scrolling background image which can be blurred (on/off) by tapping the screen
37  */
38 class GaussianBlurViewExample : public ConnectionTracker
39 {
40 public:
41
42   GaussianBlurViewExample( Application& application )
43   : mApplication( application ),
44     mExcessWidth( 0.0f ),
45     mStrength( 1.0f ),
46     mActivate( false )
47   {
48     mApplication.InitSignal().Connect( this, &GaussianBlurViewExample::Create );
49   }
50
51   ~GaussianBlurViewExample() = default;
52
53 private:
54
55   void Create( Application& application )
56   {
57     auto window = application.GetWindow();
58     Vector2 windowSize = window.GetSize();
59     window.KeyEventSignal().Connect(this, &GaussianBlurViewExample::OnKeyEvent);
60
61     mImageView = Toolkit::ImageView::New( BACKGROUND_IMAGE );
62     mImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
63     mImageView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
64
65     float excessWidth = std::max( 0.0f, (BACKGROUND_IMAGE_WIDTH - windowSize.width) * 0.5f );
66
67     if( excessWidth > 0.0f )
68     {
69       // Move background image to show GaussianBlurView activity
70
71       float pixelsPerSecond = 10.0f;
72       float duration = excessWidth / pixelsPerSecond;
73       float qDuration = duration * 0.25f;
74
75       mAnimation = Animation::New( duration );
76       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X),  excessWidth, TimePeriod(0.0f          , qDuration) );
77       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X),         0.0f, TimePeriod(qDuration     , qDuration) );
78       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X), -excessWidth, TimePeriod(2.0f*qDuration, qDuration) );
79       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X),         0.0f, TimePeriod(3.0f*qDuration, qDuration) );
80
81       mAnimation.SetLooping( true );
82       mAnimation.Play();
83     }
84
85     Layer onTop = Layer::New();
86     onTop.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
87     onTop.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
88     onTop.SetProperty( Actor::Property::SIZE, windowSize );
89     window.Add( onTop );
90     onTop.RaiseToTop();
91
92     mOnLabel = TextLabel::New( "Blur ON" );
93     mOnLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
94     mOnLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::GREEN );
95     mOnLabel.SetProperty( Actor::Property::VISIBLE, false );
96     onTop.Add( mOnLabel );
97
98     mOffLabel = TextLabel::New( "Blur OFF" );
99     mOffLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
100     mOffLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
101     mOffLabel.SetProperty( Actor::Property::VISIBLE, true );
102     onTop.Add( mOffLabel );
103
104     mGaussianBlurView = GaussianBlurView::New( 30, 8.0f, Pixel::RGBA8888, 0.5f, 0.5f, false );
105     mGaussianBlurView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
106     mGaussianBlurView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
107     mGaussianBlurView.SetProperty( Actor::Property::SIZE, windowSize );
108     window.Add( mGaussianBlurView );
109
110     mGaussianBlurView.Add( mImageView );
111     mGaussianBlurView.SetProperty( mGaussianBlurView.GetBlurStrengthPropertyIndex(), mStrength );
112
113     window.GetRootLayer().TouchedSignal().Connect( this, &GaussianBlurViewExample::OnTouch );
114   }
115
116   bool OnTouch( Actor actor, const TouchEvent& touch )
117   {
118     const PointState::Type state = touch.GetState( 0 );
119
120     if( PointState::DOWN == state )
121     {
122       if( !mActivate )
123       {
124         mActivate = true;
125         mGaussianBlurView.Activate();
126
127         mOnLabel.SetProperty( Actor::Property::VISIBLE, true );
128         mOffLabel.SetProperty( Actor::Property::VISIBLE, false );
129       }
130       else
131       {
132         mActivate = false;
133         mGaussianBlurView.Deactivate();
134
135         mOnLabel.SetProperty( Actor::Property::VISIBLE, false );
136         mOffLabel.SetProperty( Actor::Property::VISIBLE, true );
137       }
138     }
139
140     return true;
141   }
142
143   void OnKeyEvent(const KeyEvent& event)
144   {
145     if(event.GetState() == KeyEvent::DOWN)
146     {
147       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
148       {
149         mApplication.Quit();
150       }
151     }
152   }
153
154 private:
155
156   Application&  mApplication;
157
158   Toolkit::ImageView mImageView;
159
160   Animation mAnimation;
161
162   TextLabel mOnLabel;
163   TextLabel mOffLabel;
164
165   GaussianBlurView mGaussianBlurView;
166
167   float mExcessWidth;
168   float mStrength;
169
170   bool mActivate;
171 };
172
173 int DALI_EXPORT_API main( int argc, char **argv )
174 {
175   Application application = Application::New( &argc, &argv );
176
177   GaussianBlurViewExample test( application );
178
179   application.MainLoop();
180
181   return 0;
182 }