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