Changed DALI_*_DIR to DEMO_*_DIR
[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 <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"
22
23 using namespace Dali;
24 using namespace Dali::Toolkit;
25
26 namespace
27 {
28 const char * const APPLICATION_TITLE( "Color Gradients" );
29
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" );
33 }
34
35 // This example shows how to render color gradients
36 //
37 class GradientController : public ConnectionTracker
38 {
39 public:
40
41   GradientController( Application& application )
42   : mApplication( application ),
43     mIndex( 0 )
44   {
45     // Connect to the Application's Init signal
46     mApplication.InitSignal().Connect( this, &GradientController::Create );
47   }
48
49   ~GradientController()
50   {
51     // Nothing to do here;
52   }
53
54   // The Init signal is received once (only) during the Application lifetime
55   void Create( Application& application )
56   {
57     // Get a handle to the stage
58     Stage stage = Stage::GetCurrent();
59     stage.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
60
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,
65                                             mView,
66                                             toolBar,
67                                             "",
68                                             TOOLBAR_IMAGE,
69                                             APPLICATION_TITLE );
70
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  );
79
80 // ---- Gradient for background
81
82     mGradientMap.Insert("rendererType",  "gradientRenderer");
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("gradientStopOffset",   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("gradientStopColor",   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("gradientStartPosition",   Vector2( 0.5f, 0.5f ));
110         gradientMap.Insert("gradientEndPosition",    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("gradientStartPosition",   halfStageSize);
117         gradientMap.Insert("gradientEndPosition",    -halfStageSize );
118         gradientMap.Insert("gradientUnits",  "userSpace");
119         break;
120       }
121       case 2: // radial gradient with units as objectBoundingBox
122       {
123         gradientMap.Insert("gradientCenter",  Vector2(0.5f, 0.5f));
124         gradientMap.Insert("gradientRadius",  1.414f);
125         break;
126       }
127       default: // radial gradient with units as userSpaceOnUse
128       {
129         Vector2 stageSize = Stage::GetCurrent().GetSize();
130         gradientMap.Insert("gradientCenter",  stageSize*0.5f);
131         gradientMap.Insert("gradientRadius",  stageSize.Length());
132         gradientMap.Insert("gradientUnits",  "userSpace");
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   bool OnTouch( Actor actor, const TouchEvent& touch )
145   {
146     // quit the application
147     mApplication.Quit();
148     return true;
149   }
150
151   void OnKeyEvent(const KeyEvent& event)
152   {
153     if(event.state == KeyEvent::Down)
154     {
155       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
156       {
157         mApplication.Quit();
158       }
159     }
160   }
161
162 private:
163   Application&  mApplication;
164
165   Property::Map mGradientMap;
166   Control mView;
167   unsigned mIndex;
168 };
169
170 void RunTest( Application& application )
171 {
172   GradientController test( application );
173
174   application.MainLoop();
175 }
176
177 // Entry point for Linux & Tizen applications
178 //
179 int main( int argc, char **argv )
180 {
181   Application application = Application::New( &argc, &argv );
182
183   RunTest( application );
184
185   return 0;
186 }