Using migrated Public Visual API
[platform/core/uifw/dali-demo.git] / examples / gradients / gradients-example.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
20 #include "shared/view.h"
21
22 using namespace Dali;
23 using namespace Dali::Toolkit;
24
25 namespace
26 {
27 const char * const APPLICATION_TITLE( "Color Gradients" );
28
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 }
33
34 // This example shows how to render color gradients
35 //
36 class GradientController : public ConnectionTracker
37 {
38 public:
39
40   GradientController( Application& application )
41   : mApplication( application ),
42     mIndex( 0 )
43   {
44     // Connect to the Application's Init signal
45     mApplication.InitSignal().Connect( this, &GradientController::Create );
46   }
47
48   ~GradientController()
49   {
50     // Nothing to do here;
51   }
52
53   // The Init signal is received once (only) during the Application lifetime
54   void Create( Application& application )
55   {
56     // Get a handle to the stage
57     Stage stage = Stage::GetCurrent();
58     stage.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
59
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,
64                                             mView,
65                                             toolBar,
66                                             "",
67                                             TOOLBAR_IMAGE,
68                                             APPLICATION_TITLE );
69
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  );
78
79 // ---- Gradient for background
80
81     mGradientMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::GRADIENT );
82
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 );
90
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 );
98
99     OnChangeIconClicked( changeButton );
100   }
101
102   bool OnChangeIconClicked( Toolkit::Button button )
103   {
104     Property::Map gradientMap;
105
106     switch( mIndex%4 )
107     {
108       case 0: // linear gradient with units as objectBoundingBox
109       {
110         gradientMap.Insert( GradientVisual::Property::START_POSITION,   Vector2(  0.5f,  0.5f ) );
111         gradientMap.Insert( GradientVisual::Property::END_POSITION,     Vector2( -0.5f, -0.5f ) );
112         break;
113       }
114       case 1: // linear gradient with units as userSpaceOnUse
115       {
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 );
120         break;
121       }
122       case 2: // radial gradient with units as objectBoundingBox
123       {
124         gradientMap.Insert( GradientVisual::Property::CENTER, Vector2( 0.5f, 0.5f ) );
125         gradientMap.Insert( GradientVisual::Property::RADIUS, 1.414f );
126         break;
127       }
128       default: // radial gradient with units as userSpaceOnUse
129       {
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 );
134         break;
135       }
136     }
137
138     gradientMap.Merge( mGradientMap );
139     mView.SetProperty( Control::Property::BACKGROUND, gradientMap );
140
141     mIndex++;
142     return true;
143   }
144
145   void OnKeyEvent(const KeyEvent& event)
146   {
147     if(event.state == KeyEvent::Down)
148     {
149       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
150       {
151         mApplication.Quit();
152       }
153     }
154   }
155
156 private:
157   Application&  mApplication;
158
159   Property::Map mGradientMap;
160   Control mView;
161   unsigned mIndex;
162 };
163
164 void RunTest( Application& application )
165 {
166   GradientController test( application );
167
168   application.MainLoop();
169 }
170
171 // Entry point for Linux & Tizen applications
172 //
173 int DALI_EXPORT_API main( int argc, char **argv )
174 {
175   Application application = Application::New( &argc, &argv );
176
177   RunTest( application );
178
179   return 0;
180 }