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