2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
23 using namespace Dali; // Needed for macros
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
41 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(uChannels, 1.0) ;\n
45 Dali::BaseHandle Create()
47 return Demo::ImageChannelControl::New();
50 DALI_TYPE_REGISTRATION_BEGIN( ImageChannelControl, Dali::Toolkit::Control, Create );
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 );
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 );
61 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "imageVisual", MAP, IMAGE_VISUAL );
62 DALI_TYPE_REGISTRATION_END();
64 } // anonymous namespace
67 Internal::ImageChannelControl::ImageChannelControl()
68 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
69 mChannels( 1.0f, 1.0f, 1.0f ),
71 mTargetVisibility(true)
75 Internal::ImageChannelControl::~ImageChannelControl()
79 Demo::ImageChannelControl Internal::ImageChannelControl::New()
81 IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
82 Demo::ImageChannelControl handle = Demo::ImageChannelControl( *impl );
87 void ImageChannelControl::SetImage( const std::string& url )
93 Property::Map properties;
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;
100 mVisual = Toolkit::VisualFactory::Get().CreateVisual( properties );
101 RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, mVisual );
102 mVisual.SetName("imageVisual");
107 void ImageChannelControl::SetVisibility( bool visibility )
109 printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetName().c_str(), visibility?"T":"F" );
116 mAnimation.FinishedSignal().Disconnect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
117 OnStateChangeAnimationFinished(mAnimation);
120 if( mVisibility != visibility )
124 if( mDisableVisibilityTransition.Count() > 0 )
126 mAnimation = CreateTransition( mDisableVisibilityTransition );
131 if( mEnableVisibilityTransition.Count() > 0 )
133 mAnimation = CreateTransition( mEnableVisibilityTransition );
140 mAnimation.FinishedSignal().Connect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
142 mTargetVisibility = visibility;
146 mVisibility = visibility;
150 void ImageChannelControl::OnStateChangeAnimationFinished( Animation& src )
152 mVisibility = mTargetVisibility;
155 void ImageChannelControl::OnInitialize()
158 mChannelIndex = self.RegisterProperty( "uChannels", Vector3(1.0f, 1.0f, 1.0f) );
161 void ImageChannelControl::OnStageConnection( int depth )
163 Control::OnStageConnection( depth );
166 void ImageChannelControl::OnStageDisconnection()
168 Control::OnStageDisconnection();
171 void ImageChannelControl::OnSizeSet( const Vector3& targetSize )
173 Control::OnSizeSet( targetSize );
177 Vector2 size( targetSize );
178 mVisual.SetSize( size );
182 Vector3 ImageChannelControl::GetNaturalSize()
187 mVisual.GetNaturalSize(naturalSize);
188 return Vector3(naturalSize);
190 return Vector3::ZERO;
193 void ImageChannelControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
196 Control::OnStyleChange( styleManager, change );
200 ///////////////////////////////////////////////////////////
205 void ImageChannelControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
207 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
209 if( imageChannelControl )
211 ImageChannelControl& impl = GetImpl( imageChannelControl );
212 Actor self = impl.Self();
215 case Demo::ImageChannelControl::Property::RESOURCE_URL:
217 impl.SetImage( value.Get<std::string>() );
220 case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
222 Property::Map* map = value.GetMap();
225 impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
226 impl.RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, impl.mVisual );
230 case Demo::ImageChannelControl::Property::VISIBILITY:
232 impl.SetVisibility( value.Get<bool>() );
235 case Demo::ImageChannelControl::Property::ENABLE_VISIBILITY_TRANSITION:
237 if( value.GetType() == Property::ARRAY )
239 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
241 else if( value.GetType() == Property::MAP )
243 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
247 case Demo::ImageChannelControl::Property::DISABLE_VISIBILITY_TRANSITION:
249 if( value.GetType() == Property::ARRAY )
251 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
253 else if( value.GetType() == Property::MAP )
255 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
259 case Demo::ImageChannelControl::Property::RED_CHANNEL:
261 impl.mChannels[0] = value.Get<float>();
262 self.SetProperty( impl.mChannelIndex, impl.mChannels );
265 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
267 impl.mChannels[1] = value.Get<float>();
268 self.SetProperty( impl.mChannelIndex, impl.mChannels );
271 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
273 impl.mChannels[2] = value.Get<float>();
274 self.SetProperty( impl.mChannelIndex, impl.mChannels );
281 Property::Value ImageChannelControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
283 Property::Value value;
285 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
287 if ( imageChannelControl )
289 ImageChannelControl& impl = GetImpl( imageChannelControl );
290 switch ( propertyIndex )
292 case Demo::ImageChannelControl::Property::RED_CHANNEL:
294 value = impl.mChannels[0];
297 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
299 value = impl.mChannels[1];
302 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
304 value = impl.mChannels[2];
307 case Demo::ImageChannelControl::Property::VISIBILITY:
309 value = impl.mVisibility;