2 * Copyright (c) 2017 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/buttons/button-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" );
34 // This example shows how to render color gradients
36 class GradientController : public ConnectionTracker
40 GradientController( Application& application )
41 : mApplication( application ),
44 // Connect to the Application's Init signal
45 mApplication.InitSignal().Connect( this, &GradientController::Create );
50 // Nothing to do here;
53 // The Init signal is received once (only) during the Application lifetime
54 void Create( Application& application )
56 // Get a handle to the stage
57 Stage stage = Stage::GetCurrent();
58 stage.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
60 // Creates a default view with a default tool bar.
61 // The view is added to the stage.
62 Toolkit::ToolBar toolBar;
63 Layer content = DemoHelper::CreateView( application,
70 PushButton changeButton = Toolkit::PushButton::New();
71 changeButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_ICON );
72 changeButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_ICON_SELECTED );
73 changeButton.ClickedSignal().Connect( this, &GradientController::OnChangeIconClicked );
74 toolBar.AddControl( changeButton,
75 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
76 Toolkit::Alignment::HorizontalRight,
77 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
79 // ---- Gradient for background
81 mGradientMap.Insert( Toolkit::Visual::Property::TYPE, Visual::GRADIENT );
83 Property::Array stopOffsets;
84 stopOffsets.PushBack( 0.0f );
85 stopOffsets.PushBack( 0.3f );
86 stopOffsets.PushBack( 0.6f );
87 stopOffsets.PushBack( 0.8f );
88 stopOffsets.PushBack( 1.0f );
89 mGradientMap.Insert( GradientVisual::Property::STOP_OFFSET, stopOffsets );
91 Property::Array stopColors;
92 stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 255.f )/255.f );
93 stopColors.PushBack( Vector4( 196.f, 198.f, 71.f, 122.f )/255.f );
94 stopColors.PushBack( Vector4( 214.f, 37.f, 139.f, 191.f )/255.f );
95 stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 150.f )/255.f );
96 stopColors.PushBack( Color::YELLOW );
97 mGradientMap.Insert( GradientVisual::Property::STOP_COLOR, stopColors );
99 OnChangeIconClicked( changeButton );
102 bool OnChangeIconClicked( Toolkit::Button button )
104 Property::Map gradientMap;
108 case 0: // linear gradient with units as objectBoundingBox
110 gradientMap.Insert( GradientVisual::Property::START_POSITION, Vector2( 0.5f, 0.5f ) );
111 gradientMap.Insert( GradientVisual::Property::END_POSITION, Vector2( -0.5f, -0.5f ) );
114 case 1: // linear gradient with units as userSpaceOnUse
116 Vector2 halfStageSize = Stage::GetCurrent().GetSize()*0.5f;
117 gradientMap.Insert( GradientVisual::Property::START_POSITION, halfStageSize );
118 gradientMap.Insert( GradientVisual::Property::END_POSITION, -halfStageSize );
119 gradientMap.Insert( GradientVisual::Property::UNITS, GradientVisual::Units::USER_SPACE );
122 case 2: // radial gradient with units as objectBoundingBox
124 gradientMap.Insert( GradientVisual::Property::CENTER, Vector2( 0.5f, 0.5f ) );
125 gradientMap.Insert( GradientVisual::Property::RADIUS, 1.414f );
128 default: // radial gradient with units as userSpaceOnUse
130 Vector2 stageSize = Stage::GetCurrent().GetSize();
131 gradientMap.Insert( GradientVisual::Property::CENTER, stageSize * 0.5f );
132 gradientMap.Insert( GradientVisual::Property::RADIUS, stageSize.Length());
133 gradientMap.Insert( GradientVisual::Property::UNITS, GradientVisual::Units::USER_SPACE );
138 gradientMap.Merge( mGradientMap );
139 mView.SetProperty( Control::Property::BACKGROUND, gradientMap );
145 void OnKeyEvent(const KeyEvent& event)
147 if(event.state == KeyEvent::Down)
149 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
157 Application& mApplication;
159 Property::Map mGradientMap;
164 int DALI_EXPORT_API main( int argc, char **argv )
166 Application application = Application::New( &argc, &argv );
167 GradientController test( application );
168 application.MainLoop();