Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / color-visual / color-visual-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 <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
21 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
22 #include <dali-toolkit/devel-api/visual-factory/transition-data.h>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
29
30 const char* IMAGE_FILE( DEMO_IMAGE_DIR "gallery-medium-1.jpg" );
31
32 const float BLUR_RADIUS_VALUE( 10.0f );
33 const float NO_BLUR_VALUE( 0.0f );
34 const float ANIMATION_DURATION( 2.0f );
35
36 const Property::Value SHADOW
37 {
38   { Visual::Property::TYPE, Visual::COLOR },
39   { Visual::Property::MIX_COLOR, Vector4( 0.0f, 0.0f, 0.0f, 0.5f ) },
40   { Visual::Property::TRANSFORM, Property::Map{ { Visual::Transform::Property::OFFSET, Vector2( 0.05f, 0.05f ) },
41                                                 { Visual::Transform::Property::SIZE, Vector2( 1.05f, 1.05f ) },
42                                                 { Visual::Transform::Property::ORIGIN, Align::CENTER },
43                                                 { Visual::Transform::Property::ANCHOR_POINT, Align::CENTER } } },
44   { DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_VALUE }
45 };
46
47 } // namespace
48
49 // This example shows the blur radius property of the color visual and animates it.
50 //
51 class ColorVisualExample : public ConnectionTracker
52 {
53 public:
54
55   ColorVisualExample( Application& application )
56   : mApplication( application ),
57     mShadowVisible( true )
58   {
59     // Connect to the Application's Init signal
60     mApplication.InitSignal().Connect( this, &ColorVisualExample::Create );
61   }
62
63   ~ColorVisualExample()
64   {
65     // Nothing to do here;
66   }
67
68   // The Init signal is received once (only) during the Application lifetime
69   void Create( Application& application )
70   {
71     // Get a handle to the window
72     Window window = application.GetWindow();
73     window.SetBackgroundColor( Color::WHITE );
74
75     mImageView = ImageView::New( IMAGE_FILE );
76     mImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
77     mImageView.SetProperty( Actor::Property::SIZE, Vector2( 200.0f, 200.0f ) );
78     mImageView.SetProperty( DevelControl::Property::SHADOW, SHADOW );
79
80     window.Add( mImageView );
81
82     // Respond to a click anywhere on the window
83     window.GetRootLayer().TouchedSignal().Connect( this, &ColorVisualExample::OnTouch );
84
85     // Respond to key events
86     window.KeyEventSignal().Connect( this, &ColorVisualExample::OnKeyEvent );
87   }
88
89   bool OnTouch( Actor actor, const TouchEvent& touch )
90   {
91     if( touch.GetState( 0 ) == PointState::UP )
92     {
93       float initialValue, targetValue;
94       if( !mShadowVisible )
95       {
96         initialValue = NO_BLUR_VALUE;
97         targetValue = BLUR_RADIUS_VALUE;
98       }
99       else
100       {
101         initialValue = BLUR_RADIUS_VALUE;
102         targetValue = NO_BLUR_VALUE;
103       }
104
105       mShadowVisible = !mShadowVisible;
106
107       TransitionData transitionData = TransitionData::New( Property::Map().Add( "target", "shadow" )
108                                                                           .Add( "property", "blurRadius" )
109                                                                           .Add( "initialValue", initialValue )
110                                                                           .Add( "targetValue", targetValue )
111                                                                           .Add( "animator", Property::Map().Add( "duration", ANIMATION_DURATION ) ) );
112       Animation animation = DevelControl::CreateTransition( Toolkit::Internal::GetImplementation( mImageView ), transitionData );
113       animation.Play();
114     }
115     return true;
116   }
117
118   void OnKeyEvent( const KeyEvent& event )
119   {
120     if( event.GetState() == KeyEvent::DOWN )
121     {
122       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
123       {
124         mApplication.Quit();
125       }
126     }
127   }
128
129 private:
130   Application&  mApplication;
131   ImageView mImageView;
132   bool mShadowVisible;
133 };
134
135 int DALI_EXPORT_API main( int argc, char **argv )
136 {
137   Application application = Application::New( &argc, &argv );
138   ColorVisualExample test( application );
139   application.MainLoop();
140   return 0;
141 }