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