Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / gaussian-blur-view / gaussian-blur-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 #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   {
47     mApplication.InitSignal().Connect( this, &GaussianBlurViewExample::Create );
48   }
49
50   ~GaussianBlurViewExample() = default;
51
52 private:
53
54   void Create( Application& application )
55   {
56     Stage stage = Stage::GetCurrent();
57     Vector2 stageSize = stage.GetSize();
58     stage.KeyEventSignal().Connect(this, &GaussianBlurViewExample::OnKeyEvent);
59
60     mImageView = Toolkit::ImageView::New( BACKGROUND_IMAGE );
61     mImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
62     mImageView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
63
64     stage.Add( mImageView );
65
66     float excessWidth = std::max( 0.0f, (BACKGROUND_IMAGE_WIDTH - stageSize.width) * 0.5f );
67
68     if( excessWidth > 0.0f )
69     {
70       // Move background image to show GaussianBlurView activity
71
72       float pixelsPerSecond = 10.0f;
73       float duration = excessWidth / pixelsPerSecond;
74       float qDuration = duration * 0.25f;
75
76       mAnimation = Animation::New( duration );
77       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X),  excessWidth, TimePeriod(0.0f          , qDuration) );
78       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X),         0.0f, TimePeriod(qDuration     , qDuration) );
79       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X), -excessWidth, TimePeriod(2.0f*qDuration, qDuration) );
80       mAnimation.AnimateTo( Property(mImageView, Actor::Property::POSITION_X),         0.0f, TimePeriod(3.0f*qDuration, qDuration) );
81
82       mAnimation.SetLooping( true );
83       mAnimation.Play();
84     }
85
86     Layer onTop = Layer::New();
87     onTop.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
88     onTop.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
89     onTop.SetProperty( Actor::Property::SIZE, stageSize );
90     stage.Add( onTop );
91     onTop.RaiseToTop();
92
93     mOnLabel = TextLabel::New( "Blur ON" );
94     mOnLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
95     mOnLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::GREEN );
96     mOnLabel.SetProperty( Actor::Property::VISIBLE, false );
97     onTop.Add( mOnLabel );
98
99     mOffLabel = TextLabel::New( "Blur OFF" );
100     mOffLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
101     mOffLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
102     mOffLabel.SetProperty( Actor::Property::VISIBLE, true );
103     onTop.Add( mOffLabel );
104
105     stage.GetRootLayer().TouchSignal().Connect( this, &GaussianBlurViewExample::OnTouch );
106   }
107
108   bool OnTouch( Actor actor, const TouchData& touch )
109   {
110       const PointState::Type state = touch.GetState( 0 );
111
112       if( PointState::DOWN == state )
113       {
114         Stage stage = Stage::GetCurrent();
115
116         // Enable/disable blur effect
117
118         if( !mGaussianBlurView )
119         {
120           mGaussianBlurView = GaussianBlurView::New( 30, 8.0f, Pixel::RGBA8888, 0.5f, 0.5f, false );
121           mGaussianBlurView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
122           mGaussianBlurView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
123           mGaussianBlurView.SetProperty( Actor::Property::SIZE, stage.GetSize() );
124           stage.Add( mGaussianBlurView );
125
126           mGaussianBlurView.Add( mImageView );
127           mGaussianBlurView.Activate();
128
129           mGaussianBlurView.SetProperty( mGaussianBlurView.GetBlurStrengthPropertyIndex(), mStrength );
130
131           mOnLabel.SetProperty( Actor::Property::VISIBLE, true );
132           mOffLabel.SetProperty( Actor::Property::VISIBLE, false );
133         }
134         else
135         {
136           stage.Add( mImageView );
137
138           UnparentAndReset( mGaussianBlurView );
139
140           mOnLabel.SetProperty( Actor::Property::VISIBLE, false );
141           mOffLabel.SetProperty( Actor::Property::VISIBLE, true );
142         }
143
144       }
145
146     return true;
147   }
148
149   void OnKeyEvent(const KeyEvent& event)
150   {
151     if(event.state == KeyEvent::Down)
152     {
153       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
154       {
155         mApplication.Quit();
156       }
157     }
158   }
159
160 private:
161
162   Application&  mApplication;
163
164   Toolkit::ImageView mImageView;
165
166   Animation mAnimation;
167
168   TextLabel mOnLabel;
169   TextLabel mOffLabel;
170
171   GaussianBlurView mGaussianBlurView;
172
173   float mExcessWidth;
174   float mStrength;
175 };
176
177 int DALI_EXPORT_API main( int argc, char **argv )
178 {
179   Application application = Application::New( &argc, &argv );
180
181   GaussianBlurViewExample test( application );
182
183   application.MainLoop();
184
185   return 0;
186 }