2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali-toolkit/devel-api/controls/gaussian-blur-view/gaussian-blur-view.h>
24 using Dali::Toolkit::TextLabel;
25 using Dali::Toolkit::GaussianBlurView;
30 const char* const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" );
31 const float BACKGROUND_IMAGE_WIDTH = 2048.0f;
36 * This example shows a scrolling background image which can be blurred (on/off) by tapping the screen
38 class GaussianBlurViewExample : public ConnectionTracker
42 GaussianBlurViewExample( Application& application )
43 : mApplication( application ),
47 mApplication.InitSignal().Connect( this, &GaussianBlurViewExample::Create );
50 ~GaussianBlurViewExample() = default;
54 void Create( Application& application )
56 Stage stage = Stage::GetCurrent();
57 Vector2 stageSize = stage.GetSize();
58 stage.KeyEventSignal().Connect(this, &GaussianBlurViewExample::OnKeyEvent);
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 );
64 stage.Add( mImageView );
66 float excessWidth = std::max( 0.0f, (BACKGROUND_IMAGE_WIDTH - stageSize.width) * 0.5f );
68 if( excessWidth > 0.0f )
70 // Move background image to show GaussianBlurView activity
72 float pixelsPerSecond = 10.0f;
73 float duration = excessWidth / pixelsPerSecond;
74 float qDuration = duration * 0.25f;
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) );
82 mAnimation.SetLooping( true );
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 );
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 );
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 );
105 stage.GetRootLayer().TouchSignal().Connect( this, &GaussianBlurViewExample::OnTouch );
108 bool OnTouch( Actor actor, const TouchData& touch )
110 const PointState::Type state = touch.GetState( 0 );
112 if( PointState::DOWN == state )
114 Stage stage = Stage::GetCurrent();
116 // Enable/disable blur effect
118 if( !mGaussianBlurView )
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 );
126 mGaussianBlurView.Add( mImageView );
127 mGaussianBlurView.Activate();
129 mGaussianBlurView.SetProperty( mGaussianBlurView.GetBlurStrengthPropertyIndex(), mStrength );
131 mOnLabel.SetProperty( Actor::Property::VISIBLE, true );
132 mOffLabel.SetProperty( Actor::Property::VISIBLE, false );
136 stage.Add( mImageView );
138 UnparentAndReset( mGaussianBlurView );
140 mOnLabel.SetProperty( Actor::Property::VISIBLE, false );
141 mOffLabel.SetProperty( Actor::Property::VISIBLE, true );
149 void OnKeyEvent(const KeyEvent& event)
151 if(event.state == KeyEvent::Down)
153 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
162 Application& mApplication;
164 Toolkit::ImageView mImageView;
166 Animation mAnimation;
171 GaussianBlurView mGaussianBlurView;
177 int DALI_EXPORT_API main( int argc, char **argv )
179 Application application = Application::New( &argc, &argv );
181 GaussianBlurViewExample test( application );
183 application.MainLoop();