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/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>
26 using namespace Dali; // Needed for macros
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 vec4 mixColor;\n
41 uniform mediump vec3 uChannels;\n
45 gl_FragColor = texture2D( sTexture, vTexCoord ) * mixColor * uColor * vec4(uChannels, 1.0) ;\n
49 Dali::BaseHandle Create()
51 return Demo::ImageChannelControl::New();
54 DALI_TYPE_REGISTRATION_BEGIN( ImageChannelControl, Dali::Toolkit::Control, Create );
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 );
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 );
65 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "imageVisual", MAP, IMAGE_VISUAL );
66 DALI_TYPE_REGISTRATION_END();
68 } // anonymous namespace
71 Internal::ImageChannelControl::ImageChannelControl()
72 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
73 mChannels( 1.0f, 1.0f, 1.0f ),
74 mChannelIndex( Property::INVALID_INDEX ),
76 mTargetVisibility(true)
80 Internal::ImageChannelControl::~ImageChannelControl()
84 Demo::ImageChannelControl Internal::ImageChannelControl::New()
86 IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
87 Demo::ImageChannelControl handle = Demo::ImageChannelControl( *impl );
92 void ImageChannelControl::SetImage( const std::string& url )
98 Property::Map properties;
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;
105 mVisual = Toolkit::VisualFactory::Get().CreateVisual( properties );
106 RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, mVisual );
107 mVisual.SetName("imageVisual");
112 void ImageChannelControl::SetVisibility( bool visibility )
114 printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetName().c_str(), visibility?"T":"F" );
119 mAnimation.FinishedSignal().Disconnect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
120 OnStateChangeAnimationFinished(mAnimation);
123 if( mVisibility != visibility )
127 if( mDisableVisibilityTransition.Count() > 0 )
129 mAnimation = CreateTransition( mDisableVisibilityTransition );
134 if( mEnableVisibilityTransition.Count() > 0 )
136 mAnimation = CreateTransition( mEnableVisibilityTransition );
143 mAnimation.FinishedSignal().Connect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
145 mTargetVisibility = visibility;
149 mVisibility = visibility;
153 void ImageChannelControl::OnStateChangeAnimationFinished( Animation& src )
155 mVisibility = mTargetVisibility;
158 void ImageChannelControl::OnInitialize()
161 mChannelIndex = self.RegisterProperty( "uChannels", Vector3(1.0f, 1.0f, 1.0f) );
164 void ImageChannelControl::OnStageConnection( int depth )
166 Control::OnStageConnection( depth );
169 void ImageChannelControl::OnStageDisconnection()
171 Control::OnStageDisconnection();
174 void ImageChannelControl::OnSizeSet( const Vector3& targetSize )
176 Control::OnSizeSet( targetSize );
180 Vector2 size( targetSize );
181 Property::Map transformMap;
183 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
184 .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
185 .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
186 .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
187 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
189 mVisual.SetTransformAndSize( transformMap, size );
193 Vector3 ImageChannelControl::GetNaturalSize()
198 mVisual.GetNaturalSize(naturalSize);
199 return Vector3(naturalSize);
201 return Vector3::ZERO;
204 void ImageChannelControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
207 Control::OnStyleChange( styleManager, change );
211 ///////////////////////////////////////////////////////////
216 void ImageChannelControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
218 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
220 if( imageChannelControl )
222 ImageChannelControl& impl = GetImpl( imageChannelControl );
223 Actor self = impl.Self();
226 case Demo::ImageChannelControl::Property::RESOURCE_URL:
228 impl.SetImage( value.Get<std::string>() );
231 case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
233 Property::Map* map = value.GetMap();
236 impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
237 impl.RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, impl.mVisual );
241 case Demo::ImageChannelControl::Property::VISIBILITY:
243 impl.SetVisibility( value.Get<bool>() );
246 case Demo::ImageChannelControl::Property::ENABLE_VISIBILITY_TRANSITION:
248 if( value.GetType() == Property::ARRAY )
250 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
252 else if( value.GetType() == Property::MAP )
254 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
258 case Demo::ImageChannelControl::Property::DISABLE_VISIBILITY_TRANSITION:
260 if( value.GetType() == Property::ARRAY )
262 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
264 else if( value.GetType() == Property::MAP )
266 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
270 case Demo::ImageChannelControl::Property::RED_CHANNEL:
272 impl.mChannels[0] = value.Get<float>();
273 self.SetProperty( impl.mChannelIndex, impl.mChannels );
276 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
278 impl.mChannels[1] = value.Get<float>();
279 self.SetProperty( impl.mChannelIndex, impl.mChannels );
282 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
284 impl.mChannels[2] = value.Get<float>();
285 self.SetProperty( impl.mChannelIndex, impl.mChannels );
292 Property::Value ImageChannelControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
294 Property::Value value;
296 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
298 if ( imageChannelControl )
300 ImageChannelControl& impl = GetImpl( imageChannelControl );
301 switch ( propertyIndex )
303 case Demo::ImageChannelControl::Property::RED_CHANNEL:
305 value = impl.mChannels[0];
308 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
310 value = impl.mChannels[1];
313 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
315 value = impl.mChannels[2];
318 case Demo::ImageChannelControl::Property::VISIBILITY:
320 value = impl.mVisibility;