Renaming of enum values for coding standards compliance.
[platform/core/uifw/dali-demo.git] / examples / gradients / gradients-example.cpp
1 /*
2  * Copyright (c) 2020 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/visuals/visual-properties-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 const char * const ROUNDED_CORNER_ICON( DEMO_IMAGE_DIR "icon-replace.png" );
33 const char * const ROUNDED_CORNER_ICON_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
34
35 const float CORNER_RADIUS_VALUE( 20.0f );
36
37 }
38
39 // This example shows how to render color gradients
40 //
41 class GradientController : public ConnectionTracker
42 {
43 public:
44
45   GradientController( Application& application )
46   : mApplication( application ),
47     mIndex( 0 ),
48     mRoundedCorner( false )
49   {
50     // Connect to the Application's Init signal
51     mApplication.InitSignal().Connect( this, &GradientController::Create );
52   }
53
54   ~GradientController()
55   {
56     // Nothing to do here;
57   }
58
59   // The Init signal is received once (only) during the Application lifetime
60   void Create( Application& application )
61   {
62     // Get a handle to the window
63     auto window = application.GetWindow();
64     window.KeyEventSignal().Connect(this, &GradientController::OnKeyEvent);
65
66     // Creates a default view with a default tool bar.
67     // The view is added to the window.
68     Toolkit::ToolBar toolBar;
69     Layer content = DemoHelper::CreateView( application,
70                                             mView,
71                                             toolBar,
72                                             "",
73                                             TOOLBAR_IMAGE,
74                                             APPLICATION_TITLE );
75
76     PushButton changeButton = Toolkit::PushButton::New();
77     changeButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_ICON );
78     changeButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_ICON_SELECTED );
79     changeButton.ClickedSignal().Connect( this, &GradientController::OnChangeIconClicked );
80     toolBar.AddControl( changeButton,
81                         DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
82                         Toolkit::Alignment::HORIZONTAL_RIGHT,
83                         DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
84
85     PushButton roundedCornerButton = Toolkit::PushButton::New();
86     roundedCornerButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, ROUNDED_CORNER_ICON );
87     roundedCornerButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, ROUNDED_CORNER_ICON_SELECTED );
88     roundedCornerButton.ClickedSignal().Connect( this, &GradientController::OnRoundedCornerClicked );
89     toolBar.AddControl( roundedCornerButton,
90                         DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
91                         Toolkit::Alignment::HORIZONTAL_CENTER,
92                         DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
93
94     mGradientControl = Control::New();
95     mGradientControl.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
96     mGradientControl.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
97     mGradientControl.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
98     Vector3 offset( 0.9f, 0.7f, 0.0f );
99     mGradientControl.SetProperty( Actor::Property::SIZE_MODE_FACTOR, offset );
100     content.Add( mGradientControl );
101
102 // ---- Gradient for background
103
104     mGradientMap.Insert( Toolkit::Visual::Property::TYPE,  Visual::GRADIENT );
105
106     Property::Array stopOffsets;
107     stopOffsets.PushBack( 0.0f );
108     stopOffsets.PushBack( 0.3f );
109     stopOffsets.PushBack( 0.6f );
110     stopOffsets.PushBack( 0.8f );
111     stopOffsets.PushBack( 1.0f );
112     mGradientMap.Insert( GradientVisual::Property::STOP_OFFSET, stopOffsets );
113
114     Property::Array stopColors;
115     stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 255.f )/255.f );
116     stopColors.PushBack( Vector4( 196.f, 198.f, 71.f, 122.f )/255.f );
117     stopColors.PushBack( Vector4( 214.f, 37.f, 139.f, 191.f )/255.f );
118     stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 150.f )/255.f );
119     stopColors.PushBack( Color::YELLOW );
120     mGradientMap.Insert( GradientVisual::Property::STOP_COLOR, stopColors );
121
122     mGradientMap.Insert( DevelVisual::Property::CORNER_RADIUS, mRoundedCorner ? CORNER_RADIUS_VALUE : 0.0f );
123
124     UpdateGradientMap();
125   }
126
127   bool OnChangeIconClicked( Toolkit::Button button )
128   {
129     mIndex++;
130     UpdateGradientMap();
131     return true;
132   }
133
134   bool OnRoundedCornerClicked( Toolkit::Button button )
135   {
136     mRoundedCorner = !mRoundedCorner;
137     mGradientMap[DevelVisual::Property::CORNER_RADIUS] = mRoundedCorner ? CORNER_RADIUS_VALUE : 0.0f;
138
139     UpdateGradientMap();
140
141     return true;
142   }
143
144   void UpdateGradientMap()
145   {
146     Property::Map gradientMap;
147
148     switch( mIndex%4 )
149     {
150       case 0: // linear gradient with units as objectBoundingBox
151       {
152         gradientMap.Insert( GradientVisual::Property::START_POSITION,   Vector2(  0.5f,  0.5f ) );
153         gradientMap.Insert( GradientVisual::Property::END_POSITION,     Vector2( -0.5f, -0.5f ) );
154         break;
155       }
156       case 1: // linear gradient with units as userSpaceOnUse
157       {
158         Vector2 halfWindowSize = Vector2(mApplication.GetWindow().GetSize())*0.5f;
159         gradientMap.Insert( GradientVisual::Property::START_POSITION,  halfWindowSize );
160         gradientMap.Insert( GradientVisual::Property::END_POSITION,   -halfWindowSize );
161         gradientMap.Insert( GradientVisual::Property::UNITS, GradientVisual::Units::USER_SPACE );
162         break;
163       }
164       case 2: // radial gradient with units as objectBoundingBox
165       {
166         gradientMap.Insert( GradientVisual::Property::CENTER, Vector2( 0.5f, 0.5f ) );
167         gradientMap.Insert( GradientVisual::Property::RADIUS, 1.414f );
168         break;
169       }
170       default: // radial gradient with units as userSpaceOnUse
171       {
172         Vector2 windowSize = mApplication.GetWindow().GetSize();
173         gradientMap.Insert( GradientVisual::Property::CENTER, windowSize * 0.5f );
174         gradientMap.Insert( GradientVisual::Property::RADIUS, windowSize.Length());
175         gradientMap.Insert( GradientVisual::Property::UNITS,  GradientVisual::Units::USER_SPACE );
176         break;
177       }
178     }
179
180     gradientMap.Merge( mGradientMap );
181     mGradientControl.SetProperty( Control::Property::BACKGROUND, gradientMap );
182   }
183
184   void OnKeyEvent(const KeyEvent& event)
185   {
186     if(event.GetState() == KeyEvent::DOWN)
187     {
188       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
189       {
190         mApplication.Quit();
191       }
192     }
193   }
194
195 private:
196   Application&  mApplication;
197
198   Property::Map mGradientMap;
199   Control mView;
200   Control mGradientControl;
201   unsigned mIndex;
202   bool mRoundedCorner;
203 };
204
205 int DALI_EXPORT_API main( int argc, char **argv )
206 {
207   Application application = Application::New( &argc, &argv );
208   GradientController test( application );
209   application.MainLoop();
210   return 0;
211 }