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/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
21 #include "shared/view.h"
24 using namespace Dali::Toolkit;
28 const char* const APPLICATION_TITLE("Color Gradients");
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");
36 const float CORNER_RADIUS_VALUE(20.0f);
40 // This example shows how to render color gradients
42 class GradientController : public ConnectionTracker
45 GradientController(Application& application)
46 : mApplication(application),
50 // Connect to the Application's Init signal
51 mApplication.InitSignal().Connect(this, &GradientController::Create);
56 // Nothing to do here;
59 // The Init signal is received once (only) during the Application lifetime
60 void Create(Application& application)
62 // Get a handle to the window
63 auto window = application.GetWindow();
64 window.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
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,
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);
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);
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);
102 // ---- Gradient for background
104 mGradientMap.Insert(Toolkit::Visual::Property::TYPE, Visual::GRADIENT);
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);
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);
122 mGradientMap.Insert(DevelVisual::Property::CORNER_RADIUS, mRoundedCorner ? CORNER_RADIUS_VALUE : 0.0f);
127 bool OnChangeIconClicked(Toolkit::Button button)
134 bool OnRoundedCornerClicked(Toolkit::Button button)
136 mRoundedCorner = !mRoundedCorner;
138 Animation animation = Animation::New(2.0f);
141 animation.AnimateTo(DevelControl::GetVisualProperty(mGradientControl, Control::Property::BACKGROUND, DevelVisual::Property::CORNER_RADIUS), CORNER_RADIUS_VALUE);
145 animation.AnimateTo(DevelControl::GetVisualProperty(mGradientControl, Control::Property::BACKGROUND, DevelVisual::Property::CORNER_RADIUS), 0.0f);
152 void UpdateGradientMap()
154 Property::Map gradientMap;
158 case 0: // linear gradient with units as objectBoundingBox
160 gradientMap.Insert(GradientVisual::Property::START_POSITION, Vector2(0.5f, 0.5f));
161 gradientMap.Insert(GradientVisual::Property::END_POSITION, Vector2(-0.5f, -0.5f));
164 case 1: // linear gradient with units as userSpaceOnUse
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);
172 case 2: // radial gradient with units as objectBoundingBox
174 gradientMap.Insert(GradientVisual::Property::CENTER, Vector2(0.5f, 0.5f));
175 gradientMap.Insert(GradientVisual::Property::RADIUS, 1.414f);
178 default: // radial gradient with units as userSpaceOnUse
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);
188 gradientMap.Merge(mGradientMap);
189 mGradientControl.SetProperty(Control::Property::BACKGROUND, gradientMap);
192 void OnKeyEvent(const KeyEvent& event)
194 if(event.GetState() == KeyEvent::DOWN)
196 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
204 Application& mApplication;
206 Property::Map mGradientMap;
208 Control mGradientControl;
213 int DALI_EXPORT_API main(int argc, char** argv)
215 Application application = Application::New(&argc, &argv);
216 GradientController test(application);
217 application.MainLoop();