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