Merge "DALi Version 1.2.11" into devel/master
[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   mVisibility(true),
71   mTargetVisibility(true)
72 {
73 }
74
75 Internal::ImageChannelControl::~ImageChannelControl()
76 {
77 }
78
79 Demo::ImageChannelControl Internal::ImageChannelControl::New()
80 {
81   IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
82   Demo::ImageChannelControl handle = Demo::ImageChannelControl( *impl );
83   impl->Initialize();
84   return handle;
85 }
86
87 void ImageChannelControl::SetImage( const std::string& url )
88 {
89   mUrl = url;
90
91   Actor self = Self();
92
93   Property::Map properties;
94   Property::Map shader;
95   shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FRAGMENT_SHADER;
96   properties[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE;
97   properties[Dali::Toolkit::Visual::Property::SHADER]=shader;
98   properties[Dali::Toolkit::ImageVisual::Property::URL] = url;
99
100   Dali::Toolkit::InitializeVisual( self, mVisual, properties );
101   RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, self, mVisual );
102   mVisual.SetName("imageVisual");
103
104   RelayoutRequest();
105 }
106
107 void ImageChannelControl::SetVisibility( bool visibility )
108 {
109   printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetName().c_str(), visibility?"T":"F" );
110
111   Animation animation;
112
113   if( mAnimation )
114   {
115     mAnimation.Stop();
116     mAnimation.FinishedSignal().Disconnect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
117     OnStateChangeAnimationFinished(mAnimation);
118   }
119
120   if( mVisibility != visibility )
121   {
122     if( mVisibility )
123     {
124       if( mDisableVisibilityTransition.Count() > 0 )
125       {
126         mAnimation = CreateTransition( mDisableVisibilityTransition );
127       }
128     }
129     else
130     {
131       if( mEnableVisibilityTransition.Count() > 0 )
132       {
133         mAnimation = CreateTransition( mEnableVisibilityTransition );
134       }
135     }
136   }
137
138   if( mAnimation )
139   {
140     mAnimation.FinishedSignal().Connect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
141     mAnimation.Play();
142     mTargetVisibility = visibility;
143   }
144   else
145   {
146     mVisibility = visibility;
147   }
148 }
149
150 void ImageChannelControl::OnStateChangeAnimationFinished( Animation& src )
151 {
152   mVisibility = mTargetVisibility;
153 }
154
155 void ImageChannelControl::OnInitialize()
156 {
157   Actor self = Self();
158   mChannelIndex = self.RegisterProperty( "uChannels", Vector3(1.0f, 1.0f, 1.0f) );
159 }
160
161 void ImageChannelControl::OnStageConnection( int depth )
162 {
163   Control::OnStageConnection( depth );
164
165   if( mVisual )
166   {
167     CustomActor self = Self();
168     mVisual.SetOnStage( self );
169   }
170 }
171
172 void ImageChannelControl::OnStageDisconnection()
173 {
174   if( mVisual )
175   {
176     CustomActor self = Self();
177     mVisual.SetOffStage( self );
178   }
179
180   Control::OnStageDisconnection();
181 }
182
183 void ImageChannelControl::OnSizeSet( const Vector3& targetSize )
184 {
185   Control::OnSizeSet( targetSize );
186
187   if( mVisual )
188   {
189     Vector2 size( targetSize );
190     mVisual.SetSize( size );
191   }
192 }
193
194 Vector3 ImageChannelControl::GetNaturalSize()
195 {
196   if( mVisual )
197   {
198     Vector2 naturalSize;
199     mVisual.GetNaturalSize(naturalSize);
200     return Vector3(naturalSize);
201   }
202   return Vector3::ZERO;
203 }
204
205 void ImageChannelControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
206 {
207   // Chain up.
208   Control::OnStyleChange( styleManager, change );
209 }
210
211
212 ///////////////////////////////////////////////////////////
213 //
214 // Properties
215 //
216
217 void ImageChannelControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
218 {
219   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
220
221   if( imageChannelControl )
222   {
223     ImageChannelControl& impl = GetImpl( imageChannelControl );
224     Actor self = impl.Self();
225     switch ( index )
226     {
227       case Demo::ImageChannelControl::Property::RESOURCE_URL:
228       {
229         impl.SetImage( value.Get<std::string>() );
230         break;
231       }
232       case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
233       {
234         Property::Map* map = value.GetMap();
235         if( map )
236         {
237           Dali::Toolkit::InitializeVisual( self, impl.mVisual, *map );
238         }
239         break;
240       }
241       case Demo::ImageChannelControl::Property::VISIBILITY:
242       {
243         impl.SetVisibility( value.Get<bool>() );
244         break;
245       }
246       case Demo::ImageChannelControl::Property::ENABLE_VISIBILITY_TRANSITION:
247       {
248         if( value.GetType() == Property::ARRAY )
249         {
250           impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
251         }
252         else if( value.GetType() == Property::MAP )
253         {
254           impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
255         }
256         break;
257       }
258       case Demo::ImageChannelControl::Property::DISABLE_VISIBILITY_TRANSITION:
259       {
260         if( value.GetType() == Property::ARRAY )
261         {
262           impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
263         }
264         else if( value.GetType() == Property::MAP )
265         {
266           impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
267         }
268         break;
269       }
270       case Demo::ImageChannelControl::Property::RED_CHANNEL:
271       {
272         impl.mChannels[0] = value.Get<float>();
273         self.SetProperty( impl.mChannelIndex, impl.mChannels );
274         break;
275       }
276       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
277       {
278         impl.mChannels[1] = value.Get<float>();
279         self.SetProperty( impl.mChannelIndex, impl.mChannels );
280         break;
281       }
282       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
283       {
284         impl.mChannels[2] = value.Get<float>();
285         self.SetProperty( impl.mChannelIndex, impl.mChannels );
286         break;
287       }
288     }
289   }
290 }
291
292 Property::Value ImageChannelControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
293 {
294   Property::Value value;
295
296   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
297
298   if ( imageChannelControl )
299   {
300     ImageChannelControl& impl = GetImpl( imageChannelControl );
301     switch ( propertyIndex )
302     {
303       case Demo::ImageChannelControl::Property::RED_CHANNEL:
304       {
305         value = impl.mChannels[0];
306         break;
307       }
308       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
309       {
310         value = impl.mChannels[1];
311         break;
312       }
313       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
314       {
315         value = impl.mChannels[2];
316         break;
317       }
318       case Demo::ImageChannelControl::Property::VISIBILITY:
319       {
320         value = impl.mVisibility;
321         break;
322       }
323       default:
324         break;
325     }
326   }
327
328   return value;
329 }
330
331 } // Internal
332 } // Demo