2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
20 #include "shared/view.h"
23 using namespace Dali::Toolkit;
27 const char* const APPLICATION_TITLE("Color Gradients");
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");
35 const float CORNER_RADIUS_VALUE(20.0f);
39 // This example shows how to render color gradients
41 class GradientController : public ConnectionTracker
44 GradientController(Application& application)
45 : mApplication(application),
49 // Connect to the Application's Init signal
50 mApplication.InitSignal().Connect(this, &GradientController::Create);
55 // Nothing to do here;
58 // The Init signal is received once (only) during the Application lifetime
59 void Create(Application& application)
61 // Get a handle to the window
62 auto window = application.GetWindow();
63 window.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
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,
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);
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);
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);
101 // ---- Gradient for background
103 mGradientMap.Insert(Toolkit::Visual::Property::TYPE, Visual::GRADIENT);
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);
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);
121 mGradientMap.Insert(DevelVisual::Property::CORNER_RADIUS, mRoundedCorner ? CORNER_RADIUS_VALUE : 0.0f);
126 bool OnChangeIconClicked(Toolkit::Button button)
133 bool OnRoundedCornerClicked(Toolkit::Button button)
135 mRoundedCorner = !mRoundedCorner;
136 mGradientMap[DevelVisual::Property::CORNER_RADIUS] = mRoundedCorner ? CORNER_RADIUS_VALUE : 0.0f;
143 void UpdateGradientMap()
145 Property::Map gradientMap;
149 case 0: // linear gradient with units as objectBoundingBox
151 gradientMap.Insert(GradientVisual::Property::START_POSITION, Vector2(0.5f, 0.5f));
152 gradientMap.Insert(GradientVisual::Property::END_POSITION, Vector2(-0.5f, -0.5f));
155 case 1: // linear gradient with units as userSpaceOnUse
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);
163 case 2: // radial gradient with units as objectBoundingBox
165 gradientMap.Insert(GradientVisual::Property::CENTER, Vector2(0.5f, 0.5f));
166 gradientMap.Insert(GradientVisual::Property::RADIUS, 1.414f);
169 default: // radial gradient with units as userSpaceOnUse
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);
179 gradientMap.Merge(mGradientMap);
180 mGradientControl.SetProperty(Control::Property::BACKGROUND, gradientMap);
183 void OnKeyEvent(const KeyEvent& event)
185 if(event.GetState() == KeyEvent::DOWN)
187 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
195 Application& mApplication;
197 Property::Map mGradientMap;
199 Control mGradientControl;
204 int DALI_EXPORT_API main(int argc, char** argv)
206 Application application = Application::New(&argc, &argv);
207 GradientController test(application);
208 application.MainLoop();