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
45 GradientController( Application& application )
46 : mApplication( application ),
48 mRoundedCorner( false )
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 stage
63 Stage stage = Stage::GetCurrent();
64 stage.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
66 // Creates a default view with a default tool bar.
67 // The view is added to the stage.
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::HorizontalRight,
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::HorizontalCenter,
92 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
94 mGradientControl = Control::New();
95 mGradientControl.SetAnchorPoint( AnchorPoint::CENTER );
96 mGradientControl.SetParentOrigin( ParentOrigin::CENTER );
97 mGradientControl.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
98 Vector3 offset( 0.9f, 0.7f, 0.0f );
99 mGradientControl.SetSizeModeFactor( 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;
137 mGradientMap[DevelVisual::Property::CORNER_RADIUS] = mRoundedCorner ? CORNER_RADIUS_VALUE : 0.0f;
144 void UpdateGradientMap()
146 Property::Map gradientMap;
150 case 0: // linear gradient with units as objectBoundingBox
152 gradientMap.Insert( GradientVisual::Property::START_POSITION, Vector2( 0.5f, 0.5f ) );
153 gradientMap.Insert( GradientVisual::Property::END_POSITION, Vector2( -0.5f, -0.5f ) );
156 case 1: // linear gradient with units as userSpaceOnUse
158 Vector2 halfStageSize = Stage::GetCurrent().GetSize()*0.5f;
159 gradientMap.Insert( GradientVisual::Property::START_POSITION, halfStageSize );
160 gradientMap.Insert( GradientVisual::Property::END_POSITION, -halfStageSize );
161 gradientMap.Insert( GradientVisual::Property::UNITS, GradientVisual::Units::USER_SPACE );
164 case 2: // radial gradient with units as objectBoundingBox
166 gradientMap.Insert( GradientVisual::Property::CENTER, Vector2( 0.5f, 0.5f ) );
167 gradientMap.Insert( GradientVisual::Property::RADIUS, 1.414f );
170 default: // radial gradient with units as userSpaceOnUse
172 Vector2 stageSize = Stage::GetCurrent().GetSize();
173 gradientMap.Insert( GradientVisual::Property::CENTER, stageSize * 0.5f );
174 gradientMap.Insert( GradientVisual::Property::RADIUS, stageSize.Length());
175 gradientMap.Insert( GradientVisual::Property::UNITS, GradientVisual::Units::USER_SPACE );
180 gradientMap.Merge( mGradientMap );
181 mGradientControl.SetProperty( Control::Property::BACKGROUND, gradientMap );
184 void OnKeyEvent(const KeyEvent& event)
186 if(event.state == KeyEvent::Down)
188 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
196 Application& mApplication;
198 Property::Map mGradientMap;
200 Control mGradientControl;
205 int DALI_EXPORT_API main( int argc, char **argv )
207 Application application = Application::New( &argc, &argv );
208 GradientController test( application );
209 application.MainLoop();