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 <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
20 #include <dali-toolkit/devel-api/controls/renderer-factory/control-renderer.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" );
35 // This example shows how to render color gradients
37 class GradientController : public ConnectionTracker
41 GradientController( Application& application )
42 : mApplication( application ),
45 // Connect to the Application's Init signal
46 mApplication.InitSignal().Connect( this, &GradientController::Create );
51 // Nothing to do here;
54 // The Init signal is received once (only) during the Application lifetime
55 void Create( Application& application )
57 // Get a handle to the stage
58 Stage stage = Stage::GetCurrent();
59 stage.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
61 // Creates a default view with a default tool bar.
62 // The view is added to the stage.
63 Toolkit::ToolBar toolBar;
64 Layer content = DemoHelper::CreateView( application,
71 PushButton changeButton = Toolkit::PushButton::New();
72 changeButton.SetUnselectedImage( CHANGE_ICON );
73 changeButton.SetSelectedImage( CHANGE_ICON_SELECTED );
74 changeButton.ClickedSignal().Connect( this, &GradientController::OnChangeIconClicked );
75 toolBar.AddControl( changeButton,
76 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
77 Toolkit::Alignment::HorizontalRight,
78 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
80 // ---- Gradient for background
82 mGradientMap.Insert("rendererType", "gradient");
84 Property::Array stopOffsets;
85 stopOffsets.PushBack( 0.0f );
86 stopOffsets.PushBack( 0.3f );
87 stopOffsets.PushBack( 0.6f );
88 stopOffsets.PushBack( 0.8f );
89 stopOffsets.PushBack( 1.0f );
90 mGradientMap.Insert("stopOffset", stopOffsets );
92 Property::Array stopColors;
93 stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 255.f )/255.f );
94 stopColors.PushBack( Vector4( 196.f, 198.f, 71.f, 122.f )/255.f );
95 stopColors.PushBack( Vector4( 214.f, 37.f, 139.f, 191.f )/255.f );
96 stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 150.f )/255.f );
97 stopColors.PushBack( Color::YELLOW );
98 mGradientMap.Insert("stopColor", stopColors);
100 OnChangeIconClicked( changeButton );
103 bool OnChangeIconClicked( Toolkit::Button button )
105 Property::Map gradientMap;
109 case 0: // linear gradient with units as objectBoundingBox
111 gradientMap.Insert("startPosition", Vector2( 0.5f, 0.5f ));
112 gradientMap.Insert("endPosition", Vector2( -0.5f, -0.5f ));
115 case 1: // linear gradient with units as userSpaceOnUse
117 Vector2 halfStageSize = Stage::GetCurrent().GetSize()*0.5f;
118 gradientMap.Insert("startPosition", halfStageSize);
119 gradientMap.Insert("endPosition", -halfStageSize );
120 gradientMap.Insert("units", "USER_SPACE");
123 case 2: // radial gradient with units as objectBoundingBox
125 gradientMap.Insert("center", Vector2(0.5f, 0.5f));
126 gradientMap.Insert("radius", 1.414f);
129 default: // radial gradient with units as userSpaceOnUse
131 Vector2 stageSize = Stage::GetCurrent().GetSize();
132 gradientMap.Insert("center", stageSize*0.5f);
133 gradientMap.Insert("radius", stageSize.Length());
134 gradientMap.Insert("units", "USER_SPACE");
139 gradientMap.Merge( mGradientMap );
140 mView.SetProperty( Control::Property::BACKGROUND, gradientMap );
146 void OnKeyEvent(const KeyEvent& event)
148 if(event.state == KeyEvent::Down)
150 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
158 Application& mApplication;
160 Property::Map mGradientMap;
165 void RunTest( Application& application )
167 GradientController test( application );
169 application.MainLoop();
172 // Entry point for Linux & Tizen applications
174 int DALI_EXPORT_API main( int argc, char **argv )
176 Application application = Application::New( &argc, &argv );
178 RunTest( application );