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