Fix various SVACE errors
[platform/core/uifw/dali-demo.git] / examples / styling / image-channel-control-impl.cpp
1 /*
2  * Copyright (c) 2016 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 #include "image-channel-control-impl.h"
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/public-api/object/type-registry-helper.h>
20 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
21 #include <cstdio>
22
23 using namespace Dali; // Needed for macros
24
25 namespace Demo
26 {
27 namespace Internal
28 {
29
30 namespace
31 {
32
33 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
34   varying mediump vec2 vTexCoord;\n
35   uniform sampler2D sTexture;\n
36   uniform mediump vec4 uColor;\n
37   uniform mediump vec3 uChannels;\n
38   \n
39   void main()\n
40   {\n
41     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(uChannels, 1.0) ;\n
42   }\n
43 );
44
45 Dali::BaseHandle Create()
46 {
47   return Demo::ImageChannelControl::New();
48 }
49
50 DALI_TYPE_REGISTRATION_BEGIN( ImageChannelControl, Dali::Toolkit::Control, Create );
51
52 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "url", STRING, RESOURCE_URL );
53 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "redChannel", FLOAT, RED_CHANNEL );
54 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "greenChannel", FLOAT, GREEN_CHANNEL );
55 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "blueChannel", FLOAT, BLUE_CHANNEL );
56
57 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "visibility", BOOLEAN, VISIBILITY );
58 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "enableVisibilityTransition", ARRAY, ENABLE_VISIBILITY_TRANSITION );
59 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "disableVisibilityTransition", ARRAY, DISABLE_VISIBILITY_TRANSITION );
60
61 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "imageVisual", MAP, IMAGE_VISUAL );
62 DALI_TYPE_REGISTRATION_END();
63
64 } // anonymous namespace
65
66
67 Internal::ImageChannelControl::ImageChannelControl()
68 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
69   mChannels( 1.0f, 1.0f, 1.0f ),
70   mChannelIndex( Property::INVALID_INDEX ),
71   mVisibility(true),
72   mTargetVisibility(true)
73 {
74 }
75
76 Internal::ImageChannelControl::~ImageChannelControl()
77 {
78 }
79
80 Demo::ImageChannelControl Internal::ImageChannelControl::New()
81 {
82   IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
83   Demo::ImageChannelControl handle = Demo::ImageChannelControl( *impl );
84   impl->Initialize();
85   return handle;
86 }
87
88 void ImageChannelControl::SetImage( const std::string& url )
89 {
90   mUrl = url;
91
92   Actor self = Self();
93
94   Property::Map properties;
95   Property::Map shader;
96   shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FRAGMENT_SHADER;
97   properties[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE;
98   properties[Dali::Toolkit::Visual::Property::SHADER]=shader;
99   properties[Dali::Toolkit::ImageVisual::Property::URL] = url;
100
101   mVisual = Toolkit::VisualFactory::Get().CreateVisual( properties );
102   RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, mVisual );
103   mVisual.SetName("imageVisual");
104
105   RelayoutRequest();
106 }
107
108 void ImageChannelControl::SetVisibility( bool visibility )
109 {
110   printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetName().c_str(), visibility?"T":"F" );
111
112   Animation animation;
113
114   if( mAnimation )
115   {
116     mAnimation.Stop();
117     mAnimation.FinishedSignal().Disconnect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
118     OnStateChangeAnimationFinished(mAnimation);
119   }
120
121   if( mVisibility != visibility )
122   {
123     if( mVisibility )
124     {
125       if( mDisableVisibilityTransition.Count() > 0 )
126       {
127         mAnimation = CreateTransition( mDisableVisibilityTransition );
128       }
129     }
130     else
131     {
132       if( mEnableVisibilityTransition.Count() > 0 )
133       {
134         mAnimation = CreateTransition( mEnableVisibilityTransition );
135       }
136     }
137   }
138
139   if( mAnimation )
140   {
141     mAnimation.FinishedSignal().Connect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
142     mAnimation.Play();
143     mTargetVisibility = visibility;
144   }
145   else
146   {
147     mVisibility = visibility;
148   }
149 }
150
151 void ImageChannelControl::OnStateChangeAnimationFinished( Animation& src )
152 {
153   mVisibility = mTargetVisibility;
154 }
155
156 void ImageChannelControl::OnInitialize()
157 {
158   Actor self = Self();
159   mChannelIndex = self.RegisterProperty( "uChannels", Vector3(1.0f, 1.0f, 1.0f) );
160 }
161
162 void ImageChannelControl::OnStageConnection( int depth )
163 {
164   Control::OnStageConnection( depth );
165 }
166
167 void ImageChannelControl::OnStageDisconnection()
168 {
169   Control::OnStageDisconnection();
170 }
171
172 void ImageChannelControl::OnSizeSet( const Vector3& targetSize )
173 {
174   Control::OnSizeSet( targetSize );
175
176   if( mVisual )
177   {
178     Vector2 size( targetSize );
179     mVisual.SetSize( size );
180   }
181 }
182
183 Vector3 ImageChannelControl::GetNaturalSize()
184 {
185   if( mVisual )
186   {
187     Vector2 naturalSize;
188     mVisual.GetNaturalSize(naturalSize);
189     return Vector3(naturalSize);
190   }
191   return Vector3::ZERO;
192 }
193
194 void ImageChannelControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
195 {
196   // Chain up.
197   Control::OnStyleChange( styleManager, change );
198 }
199
200
201 ///////////////////////////////////////////////////////////
202 //
203 // Properties
204 //
205
206 void ImageChannelControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
207 {
208   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
209
210   if( imageChannelControl )
211   {
212     ImageChannelControl& impl = GetImpl( imageChannelControl );
213     Actor self = impl.Self();
214     switch ( index )
215     {
216       case Demo::ImageChannelControl::Property::RESOURCE_URL:
217       {
218         impl.SetImage( value.Get<std::string>() );
219         break;
220       }
221       case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
222       {
223         Property::Map* map = value.GetMap();
224         if( map )
225         {
226           impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
227           impl.RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, impl.mVisual );
228         }
229         break;
230       }
231       case Demo::ImageChannelControl::Property::VISIBILITY:
232       {
233         impl.SetVisibility( value.Get<bool>() );
234         break;
235       }
236       case Demo::ImageChannelControl::Property::ENABLE_VISIBILITY_TRANSITION:
237       {
238         if( value.GetType() == Property::ARRAY )
239         {
240           impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
241         }
242         else if( value.GetType() == Property::MAP )
243         {
244           impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
245         }
246         break;
247       }
248       case Demo::ImageChannelControl::Property::DISABLE_VISIBILITY_TRANSITION:
249       {
250         if( value.GetType() == Property::ARRAY )
251         {
252           impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
253         }
254         else if( value.GetType() == Property::MAP )
255         {
256           impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
257         }
258         break;
259       }
260       case Demo::ImageChannelControl::Property::RED_CHANNEL:
261       {
262         impl.mChannels[0] = value.Get<float>();
263         self.SetProperty( impl.mChannelIndex, impl.mChannels );
264         break;
265       }
266       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
267       {
268         impl.mChannels[1] = value.Get<float>();
269         self.SetProperty( impl.mChannelIndex, impl.mChannels );
270         break;
271       }
272       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
273       {
274         impl.mChannels[2] = value.Get<float>();
275         self.SetProperty( impl.mChannelIndex, impl.mChannels );
276         break;
277       }
278     }
279   }
280 }
281
282 Property::Value ImageChannelControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
283 {
284   Property::Value value;
285
286   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
287
288   if ( imageChannelControl )
289   {
290     ImageChannelControl& impl = GetImpl( imageChannelControl );
291     switch ( propertyIndex )
292     {
293       case Demo::ImageChannelControl::Property::RED_CHANNEL:
294       {
295         value = impl.mChannels[0];
296         break;
297       }
298       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
299       {
300         value = impl.mChannels[1];
301         break;
302       }
303       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
304       {
305         value = impl.mChannels[2];
306         break;
307       }
308       case Demo::ImageChannelControl::Property::VISIBILITY:
309       {
310         value = impl.mVisibility;
311         break;
312       }
313       default:
314         break;
315     }
316   }
317
318   return value;
319 }
320
321 } // Internal
322 } // Demo