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