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