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