Remove use of deprecated Control flag REQUIRES_STYLE_CHANGE_SIGNALS
[platform/core/uifw/dali-demo.git] / examples / styling / image-channel-control-impl.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 #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 vec3 uChannels;\n
41   \n
42   void main()\n
43   {\n
44       gl_FragColor = texture2D( sTexture, vTexCoord ) * vec4(mixColor,1.0) * uColor * vec4(uChannels, 1.0) ;\n
45   }\n
46 );
47
48 Dali::BaseHandle Create()
49 {
50   return Demo::ImageChannelControl::New();
51 }
52
53 DALI_TYPE_REGISTRATION_BEGIN( ImageChannelControl, Dali::Toolkit::Control, Create );
54
55 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "url", STRING, RESOURCE_URL );
56 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "redChannel", FLOAT, RED_CHANNEL );
57 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "greenChannel", FLOAT, GREEN_CHANNEL );
58 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "blueChannel", FLOAT, BLUE_CHANNEL );
59
60 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "visibility", BOOLEAN, VISIBILITY );
61 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "enableVisibilityTransition", ARRAY, ENABLE_VISIBILITY_TRANSITION );
62 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "disableVisibilityTransition", ARRAY, DISABLE_VISIBILITY_TRANSITION );
63
64 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "imageVisual", MAP, IMAGE_VISUAL );
65 DALI_TYPE_REGISTRATION_END();
66
67 } // anonymous namespace
68
69
70 Internal::ImageChannelControl::ImageChannelControl()
71 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
72   mChannels( 1.0f, 1.0f, 1.0f ),
73   mChannelIndex( Property::INVALID_INDEX ),
74   mVisibility(true),
75   mTargetVisibility(true)
76 {
77 }
78
79 Internal::ImageChannelControl::~ImageChannelControl()
80 {
81 }
82
83 Demo::ImageChannelControl Internal::ImageChannelControl::New()
84 {
85   IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
86   Demo::ImageChannelControl handle = Demo::ImageChannelControl( *impl );
87   impl->Initialize();
88   return handle;
89 }
90
91 void ImageChannelControl::SetImage( const std::string& url )
92 {
93   mUrl = url;
94
95   Actor self = Self();
96
97   Property::Map properties;
98   Property::Map shader;
99   shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FRAGMENT_SHADER;
100   properties[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE;
101   properties[Dali::Toolkit::Visual::Property::SHADER]=shader;
102   properties[Dali::Toolkit::ImageVisual::Property::URL] = url;
103
104   mVisual = Toolkit::VisualFactory::Get().CreateVisual( properties );
105   Toolkit::DevelControl::RegisterVisual( *this, Demo::ImageChannelControl::Property::IMAGE_VISUAL, mVisual );
106   mVisual.SetName("imageVisual");
107
108   RelayoutRequest();
109 }
110
111 void ImageChannelControl::SetVisibility( bool visibility )
112 {
113   printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str(), visibility?"T":"F" );
114
115   if( mAnimation )
116   {
117     mAnimation.Stop();
118     mAnimation.FinishedSignal().Disconnect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
119     OnStateChangeAnimationFinished(mAnimation);
120   }
121
122   if( mVisibility != visibility )
123   {
124     if( mVisibility )
125     {
126       if( mDisableVisibilityTransition.Count() > 0 )
127       {
128         mAnimation = Toolkit::DevelControl::CreateTransition( *this, mDisableVisibilityTransition );
129       }
130     }
131     else
132     {
133       if( mEnableVisibilityTransition.Count() > 0 )
134       {
135         mAnimation = Toolkit::DevelControl::CreateTransition( *this, mEnableVisibilityTransition );
136       }
137     }
138   }
139
140   if( mAnimation )
141   {
142     mAnimation.FinishedSignal().Connect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
143     mAnimation.Play();
144     mTargetVisibility = visibility;
145   }
146   else
147   {
148     mVisibility = visibility;
149   }
150 }
151
152 void ImageChannelControl::OnStateChangeAnimationFinished( Animation& src )
153 {
154   mVisibility = mTargetVisibility;
155 }
156
157 void ImageChannelControl::OnInitialize()
158 {
159   Actor self = Self();
160   mChannelIndex = self.RegisterProperty( "uChannels", Vector3(1.0f, 1.0f, 1.0f) );
161 }
162
163 void ImageChannelControl::OnSceneConnection( int depth )
164 {
165   Control::OnSceneConnection( depth );
166 }
167
168 void ImageChannelControl::OnSceneDisconnection()
169 {
170   Control::OnSceneDisconnection();
171 }
172
173 void ImageChannelControl::OnSizeSet( const Vector3& targetSize )
174 {
175   Control::OnSizeSet( targetSize );
176
177   if( mVisual )
178   {
179     Vector2 size( targetSize );
180     Property::Map transformMap;
181     transformMap
182       .Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
183       .Add( Toolkit::Visual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
184       .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
185       .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
186       .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) )
187       .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) );
188
189     mVisual.SetTransformAndSize( transformMap, size );
190   }
191 }
192
193 Vector3 ImageChannelControl::GetNaturalSize()
194 {
195   if( mVisual )
196   {
197     Vector2 naturalSize;
198     mVisual.GetNaturalSize(naturalSize);
199     return Vector3(naturalSize);
200   }
201   return Vector3::ZERO;
202 }
203
204 void ImageChannelControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
205 {
206   // Chain up.
207   Control::OnStyleChange( styleManager, change );
208 }
209
210
211 ///////////////////////////////////////////////////////////
212 //
213 // Properties
214 //
215
216 void ImageChannelControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
217 {
218   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
219
220   if( imageChannelControl )
221   {
222     ImageChannelControl& impl = GetImpl( imageChannelControl );
223     Actor self = impl.Self();
224     switch ( index )
225     {
226       case Demo::ImageChannelControl::Property::RESOURCE_URL:
227       {
228         impl.SetImage( value.Get<std::string>() );
229         break;
230       }
231       case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
232       {
233         Property::Map* map = value.GetMap();
234         if( map )
235         {
236           impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
237           Toolkit::DevelControl::RegisterVisual( impl, Demo::ImageChannelControl::Property::IMAGE_VISUAL, impl.mVisual );
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