2 * Copyright (c) 2015 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 "shared/view.h"
22 using namespace Dali::Toolkit;
26 const char * const APPLICATION_TITLE( "Color Gradients" );
28 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
29 const char * const CHANGE_ICON( DEMO_IMAGE_DIR "icon-change.png" );
30 const char * const CHANGE_ICON_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
33 // This example shows how to render color gradients
35 class GradientController : public ConnectionTracker
39 GradientController( Application& application )
40 : mApplication( application ),
43 // Connect to the Application's Init signal
44 mApplication.InitSignal().Connect( this, &GradientController::Create );
49 // Nothing to do here;
52 // The Init signal is received once (only) during the Application lifetime
53 void Create( Application& application )
55 // Get a handle to the stage
56 Stage stage = Stage::GetCurrent();
57 stage.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
59 // Creates a default view with a default tool bar.
60 // The view is added to the stage.
61 Toolkit::ToolBar toolBar;
62 Layer content = DemoHelper::CreateView( application,
69 PushButton changeButton = Toolkit::PushButton::New();
70 changeButton.SetUnselectedImage( CHANGE_ICON );
71 changeButton.SetSelectedImage( CHANGE_ICON_SELECTED );
72 changeButton.ClickedSignal().Connect( this, &GradientController::OnChangeIconClicked );
73 toolBar.AddControl( changeButton,
74 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
75 Toolkit::Alignment::HorizontalRight,
76 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
78 // ---- Gradient for background
80 mGradientMap.Insert("rendererType", "GRADIENT");
82 Property::Array stopOffsets;
83 stopOffsets.PushBack( 0.0f );
84 stopOffsets.PushBack( 0.3f );
85 stopOffsets.PushBack( 0.6f );
86 stopOffsets.PushBack( 0.8f );
87 stopOffsets.PushBack( 1.0f );
88 mGradientMap.Insert("stopOffset", stopOffsets );
90 Property::Array stopColors;
91 stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 255.f )/255.f );
92 stopColors.PushBack( Vector4( 196.f, 198.f, 71.f, 122.f )/255.f );
93 stopColors.PushBack( Vector4( 214.f, 37.f, 139.f, 191.f )/255.f );
94 stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 150.f )/255.f );
95 stopColors.PushBack( Color::YELLOW );
96 mGradientMap.Insert("stopColor", stopColors);
98 OnChangeIconClicked( changeButton );
101 bool OnChangeIconClicked( Toolkit::Button button )
103 Property::Map gradientMap;
107 case 0: // linear gradient with units as objectBoundingBox
109 gradientMap.Insert("startPosition", Vector2( 0.5f, 0.5f ));
110 gradientMap.Insert("endPosition", Vector2( -0.5f, -0.5f ));
113 case 1: // linear gradient with units as userSpaceOnUse
115 Vector2 halfStageSize = Stage::GetCurrent().GetSize()*0.5f;
116 gradientMap.Insert("startPosition", halfStageSize);
117 gradientMap.Insert("endPosition", -halfStageSize );
118 gradientMap.Insert("units", "USER_SPACE");
121 case 2: // radial gradient with units as objectBoundingBox
123 gradientMap.Insert("center", Vector2(0.5f, 0.5f));
124 gradientMap.Insert("radius", 1.414f);
127 default: // radial gradient with units as userSpaceOnUse
129 Vector2 stageSize = Stage::GetCurrent().GetSize();
130 gradientMap.Insert("center", stageSize*0.5f);
131 gradientMap.Insert("radius", stageSize.Length());
132 gradientMap.Insert("units", "USER_SPACE");
137 gradientMap.Merge( mGradientMap );
138 mView.SetProperty( Control::Property::BACKGROUND, gradientMap );
144 void OnKeyEvent(const KeyEvent& event)
146 if(event.state == KeyEvent::Down)
148 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
156 Application& mApplication;
158 Property::Map mGradientMap;
163 void RunTest( Application& application )
165 GradientController test( application );
167 application.MainLoop();
170 // Entry point for Linux & Tizen applications
172 int DALI_EXPORT_API main( int argc, char **argv )
174 Application application = Application::New( &argc, &argv );
176 RunTest( application );