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