Updated demos to use DALi clang-format
[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       if(!mActivate)
120       {
121         mActivate = true;
122         mGaussianBlurView.Activate();
123
124         mOnLabel.SetProperty(Actor::Property::VISIBLE, true);
125         mOffLabel.SetProperty(Actor::Property::VISIBLE, false);
126       }
127       else
128       {
129         mActivate = false;
130         mGaussianBlurView.Deactivate();
131
132         mOnLabel.SetProperty(Actor::Property::VISIBLE, false);
133         mOffLabel.SetProperty(Actor::Property::VISIBLE, true);
134       }
135     }
136
137     return true;
138   }
139
140   void OnKeyEvent(const KeyEvent& event)
141   {
142     if(event.GetState() == KeyEvent::DOWN)
143     {
144       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
145       {
146         mApplication.Quit();
147       }
148     }
149   }
150
151 private:
152   Application& mApplication;
153
154   Toolkit::ImageView mImageView;
155
156   Animation mAnimation;
157
158   TextLabel mOnLabel;
159   TextLabel mOffLabel;
160
161   GaussianBlurView mGaussianBlurView;
162
163   float mExcessWidth;
164   float mStrength;
165
166   bool mActivate;
167 };
168
169 int DALI_EXPORT_API main(int argc, char** argv)
170 {
171   Application application = Application::New(&argc, &argv);
172
173   GaussianBlurViewExample test(application);
174
175   application.MainLoop();
176
177   return 0;
178 }