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( DALI_IMAGE_DIR "top-bar.png" );
31 const char * const CHANGE_ICON( DALI_IMAGE_DIR "icon-change.png" );
32 const char * const CHANGE_ICON_SELECTED( DALI_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("renderer-type", "gradient-renderer");
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.f );
89 mGradientMap.Insert("gradient-stop-offset", 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("gradient-stop-color", 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("gradient-start-position", Vector2( 0.5f, 0.5f ));
110 gradientMap.Insert("gradient-end-position", 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("gradient-start-position", halfStageSize);
117 gradientMap.Insert("gradient-end-position", -halfStageSize );
118 gradientMap.Insert("gradient-units", "user-space");
121 case 2: // radial gradient with units as objectBoundingBox
123 gradientMap.Insert("gradient-center", Vector2(0.5f, 0.5f));
124 gradientMap.Insert("gradient-radius", 1.414f);
127 default: // radial gradient with units as userSpaceOnUse
129 Vector2 stageSize = Stage::GetCurrent().GetSize();
130 gradientMap.Insert("gradient-center", stageSize*0.5f);
131 gradientMap.Insert("gradient-radius", stageSize.Length());
132 gradientMap.Insert("gradient-units", "user-space");
137 gradientMap.Merge( mGradientMap );
138 mView.SetProperty( Control::Property::BACKGROUND, gradientMap );
144 bool OnTouch( Actor actor, const TouchEvent& touch )
146 // quit the application
151 void OnKeyEvent(const KeyEvent& event)
153 if(event.state == KeyEvent::Down)
155 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
163 Application& mApplication;
165 Property::Map mGradientMap;
170 void RunTest( Application& application )
172 GradientController test( application );
174 application.MainLoop();
177 // Entry point for Linux & Tizen applications
179 int main( int argc, char **argv )
181 Application application = Application::New( &argc, &argv );
183 RunTest( application );