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