Updates following enum changes to mesh and gradient renderer
[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",  "gradient");
83
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 );
91
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);
99
100     OnChangeIconClicked( changeButton );
101   }
102
103   bool OnChangeIconClicked( Toolkit::Button button )
104   {
105     Property::Map gradientMap;
106
107     switch( mIndex%4 )
108     {
109       case 0: // linear gradient with units as objectBoundingBox
110       {
111         gradientMap.Insert("startPosition",   Vector2( 0.5f, 0.5f ));
112         gradientMap.Insert("endPosition",    Vector2( -0.5f, -0.5f ));
113         break;
114       }
115       case 1: // linear gradient with units as userSpaceOnUse
116       {
117         Vector2 halfStageSize = Stage::GetCurrent().GetSize()*0.5f;
118         gradientMap.Insert("startPosition",   halfStageSize);
119         gradientMap.Insert("endPosition",    -halfStageSize );
120         gradientMap.Insert("units",  "USER_SPACE");
121         break;
122       }
123       case 2: // radial gradient with units as objectBoundingBox
124       {
125         gradientMap.Insert("center",  Vector2(0.5f, 0.5f));
126         gradientMap.Insert("radius",  1.414f);
127         break;
128       }
129       default: // radial gradient with units as userSpaceOnUse
130       {
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");
135         break;
136       }
137     }
138
139     gradientMap.Merge( mGradientMap );
140     mView.SetProperty( Control::Property::BACKGROUND, gradientMap );
141
142     mIndex++;
143     return true;
144   }
145
146   void OnKeyEvent(const KeyEvent& event)
147   {
148     if(event.state == KeyEvent::Down)
149     {
150       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
151       {
152         mApplication.Quit();
153       }
154     }
155   }
156
157 private:
158   Application&  mApplication;
159
160   Property::Map mGradientMap;
161   Control mView;
162   unsigned mIndex;
163 };
164
165 void RunTest( Application& application )
166 {
167   GradientController test( application );
168
169   application.MainLoop();
170 }
171
172 // Entry point for Linux & Tizen applications
173 //
174 int DALI_EXPORT_API main( int argc, char **argv )
175 {
176   Application application = Application::New( &argc, &argv );
177
178   RunTest( application );
179
180   return 0;
181 }