77abb2c5e862222801195b8fad40d1a8ed30bd14
[platform/core/uifw/dali-demo.git] / examples / gradients / gradients-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 "shared/view.h"
22
23 using namespace Dali;
24 using namespace Dali::Toolkit;
25
26 namespace
27 {
28 const char* const APPLICATION_TITLE("Color Gradients");
29
30 const char* const TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
31 const char* const CHANGE_ICON(DEMO_IMAGE_DIR "icon-change.png");
32 const char* const CHANGE_ICON_SELECTED(DEMO_IMAGE_DIR "icon-change-selected.png");
33 const char* const ROUNDED_CORNER_ICON(DEMO_IMAGE_DIR "icon-replace.png");
34 const char* const ROUNDED_CORNER_ICON_SELECTED(DEMO_IMAGE_DIR "icon-replace-selected.png");
35
36 const float CORNER_RADIUS_VALUE(20.0f);
37
38 } // namespace
39
40 // This example shows how to render color gradients
41 //
42 class GradientController : public ConnectionTracker
43 {
44 public:
45   GradientController(Application& application)
46   : mApplication(application),
47     mIndex(0),
48     mRoundedCorner(false)
49   {
50     // Connect to the Application's Init signal
51     mApplication.InitSignal().Connect(this, &GradientController::Create);
52   }
53
54   ~GradientController()
55   {
56     // Nothing to do here;
57   }
58
59   // The Init signal is received once (only) during the Application lifetime
60   void Create(Application& application)
61   {
62     // Get a handle to the window
63     auto window = application.GetWindow();
64     window.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
65
66     // Creates a default view with a default tool bar.
67     // The view is added to the window.
68     Toolkit::ToolBar toolBar;
69     Layer            content = DemoHelper::CreateView(application,
70                                            mView,
71                                            toolBar,
72                                            "",
73                                            TOOLBAR_IMAGE,
74                                            APPLICATION_TITLE);
75
76     PushButton changeButton = Toolkit::PushButton::New();
77     changeButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_ICON);
78     changeButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_ICON_SELECTED);
79     changeButton.ClickedSignal().Connect(this, &GradientController::OnChangeIconClicked);
80     toolBar.AddControl(changeButton,
81                        DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
82                        Toolkit::Alignment::HORIZONTAL_RIGHT,
83                        DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
84
85     PushButton roundedCornerButton = Toolkit::PushButton::New();
86     roundedCornerButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, ROUNDED_CORNER_ICON);
87     roundedCornerButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, ROUNDED_CORNER_ICON_SELECTED);
88     roundedCornerButton.ClickedSignal().Connect(this, &GradientController::OnRoundedCornerClicked);
89     toolBar.AddControl(roundedCornerButton,
90                        DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
91                        Toolkit::Alignment::HORIZONTAL_CENTER,
92                        DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
93
94     mGradientControl = Control::New();
95     mGradientControl.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
96     mGradientControl.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
97     mGradientControl.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
98     Vector3 offset(0.9f, 0.7f, 0.0f);
99     mGradientControl.SetProperty(Actor::Property::SIZE_MODE_FACTOR, offset);
100     content.Add(mGradientControl);
101
102     // ---- Gradient for background
103
104     mGradientMap.Insert(Toolkit::Visual::Property::TYPE, Visual::GRADIENT);
105
106     Property::Array stopOffsets;
107     stopOffsets.PushBack(0.0f);
108     stopOffsets.PushBack(0.3f);
109     stopOffsets.PushBack(0.6f);
110     stopOffsets.PushBack(0.8f);
111     stopOffsets.PushBack(1.0f);
112     mGradientMap.Insert(GradientVisual::Property::STOP_OFFSET, stopOffsets);
113
114     Property::Array stopColors;
115     stopColors.PushBack(Vector4(129.f, 198.f, 193.f, 255.f) / 255.f);
116     stopColors.PushBack(Vector4(196.f, 198.f, 71.f, 122.f) / 255.f);
117     stopColors.PushBack(Vector4(214.f, 37.f, 139.f, 191.f) / 255.f);
118     stopColors.PushBack(Vector4(129.f, 198.f, 193.f, 150.f) / 255.f);
119     stopColors.PushBack(Color::YELLOW);
120     mGradientMap.Insert(GradientVisual::Property::STOP_COLOR, stopColors);
121
122     mGradientMap.Insert(DevelVisual::Property::CORNER_RADIUS, mRoundedCorner ? CORNER_RADIUS_VALUE : 0.0f);
123
124     UpdateGradientMap();
125   }
126
127   bool OnChangeIconClicked(Toolkit::Button button)
128   {
129     mIndex++;
130     UpdateGradientMap();
131     return true;
132   }
133
134   bool OnRoundedCornerClicked(Toolkit::Button button)
135   {
136     mRoundedCorner = !mRoundedCorner;
137
138     Animation animation = Animation::New(2.0f);
139     if(mRoundedCorner)
140     {
141       animation.AnimateTo(DevelControl::GetVisualProperty(mGradientControl, Control::Property::BACKGROUND, DevelVisual::Property::CORNER_RADIUS), CORNER_RADIUS_VALUE);
142     }
143     else
144     {
145       animation.AnimateTo(DevelControl::GetVisualProperty(mGradientControl, Control::Property::BACKGROUND, DevelVisual::Property::CORNER_RADIUS), 0.0f);
146     }
147     animation.Play();
148
149     return true;
150   }
151
152   void UpdateGradientMap()
153   {
154     Property::Map gradientMap;
155
156     switch(mIndex % 4)
157     {
158       case 0: // linear gradient with units as objectBoundingBox
159       {
160         gradientMap.Insert(GradientVisual::Property::START_POSITION, Vector2(0.5f, 0.5f));
161         gradientMap.Insert(GradientVisual::Property::END_POSITION, Vector2(-0.5f, -0.5f));
162         break;
163       }
164       case 1: // linear gradient with units as userSpaceOnUse
165       {
166         Vector2 halfWindowSize = Vector2(mApplication.GetWindow().GetSize()) * 0.5f;
167         gradientMap.Insert(GradientVisual::Property::START_POSITION, halfWindowSize);
168         gradientMap.Insert(GradientVisual::Property::END_POSITION, -halfWindowSize);
169         gradientMap.Insert(GradientVisual::Property::UNITS, GradientVisual::Units::USER_SPACE);
170         break;
171       }
172       case 2: // radial gradient with units as objectBoundingBox
173       {
174         gradientMap.Insert(GradientVisual::Property::CENTER, Vector2(0.5f, 0.5f));
175         gradientMap.Insert(GradientVisual::Property::RADIUS, 1.414f);
176         break;
177       }
178       default: // radial gradient with units as userSpaceOnUse
179       {
180         Vector2 windowSize = mApplication.GetWindow().GetSize();
181         gradientMap.Insert(GradientVisual::Property::CENTER, windowSize * 0.5f);
182         gradientMap.Insert(GradientVisual::Property::RADIUS, windowSize.Length());
183         gradientMap.Insert(GradientVisual::Property::UNITS, GradientVisual::Units::USER_SPACE);
184         break;
185       }
186     }
187
188     gradientMap.Merge(mGradientMap);
189     mGradientControl.SetProperty(Control::Property::BACKGROUND, gradientMap);
190   }
191
192   void OnKeyEvent(const KeyEvent& event)
193   {
194     if(event.GetState() == KeyEvent::DOWN)
195     {
196       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
197       {
198         mApplication.Quit();
199       }
200     }
201   }
202
203 private:
204   Application& mApplication;
205
206   Property::Map mGradientMap;
207   Control       mView;
208   Control       mGradientControl;
209   unsigned      mIndex;
210   bool          mRoundedCorner;
211 };
212
213 int DALI_EXPORT_API main(int argc, char** argv)
214 {
215   Application        application = Application::New(&argc, &argv);
216   GradientController test(application);
217   application.MainLoop();
218   return 0;
219 }