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 vec3 uChannels;\n
44 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(uChannels, 1.0) ;\n
48 Dali::BaseHandle Create()
50 return Demo::ImageChannelControl::New();
53 DALI_TYPE_REGISTRATION_BEGIN( ImageChannelControl, Dali::Toolkit::Control, Create );
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 );
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 );
64 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "imageVisual", MAP, IMAGE_VISUAL );
65 DALI_TYPE_REGISTRATION_END();
67 } // anonymous namespace
70 Internal::ImageChannelControl::ImageChannelControl()
71 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
72 mChannels( 1.0f, 1.0f, 1.0f ),
73 mChannelIndex( Property::INVALID_INDEX ),
75 mTargetVisibility(true)
79 Internal::ImageChannelControl::~ImageChannelControl()
83 Demo::ImageChannelControl Internal::ImageChannelControl::New()
85 IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
86 Demo::ImageChannelControl handle = Demo::ImageChannelControl( *impl );
91 void ImageChannelControl::SetImage( const std::string& url )
97 Property::Map properties;
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;
104 mVisual = Toolkit::VisualFactory::Get().CreateVisual( properties );
105 RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, mVisual );
106 mVisual.SetName("imageVisual");
111 void ImageChannelControl::SetVisibility( bool visibility )
113 printf("ImageChannelControl %s: SetVisibility( %s )\n", Self().GetName().c_str(), visibility?"T":"F" );
120 mAnimation.FinishedSignal().Disconnect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
121 OnStateChangeAnimationFinished(mAnimation);
124 if( mVisibility != visibility )
128 if( mDisableVisibilityTransition.Count() > 0 )
130 mAnimation = CreateTransition( mDisableVisibilityTransition );
135 if( mEnableVisibilityTransition.Count() > 0 )
137 mAnimation = CreateTransition( mEnableVisibilityTransition );
144 mAnimation.FinishedSignal().Connect( this, &ImageChannelControl::OnStateChangeAnimationFinished );
146 mTargetVisibility = visibility;
150 mVisibility = visibility;
154 void ImageChannelControl::OnStateChangeAnimationFinished( Animation& src )
156 mVisibility = mTargetVisibility;
159 void ImageChannelControl::OnInitialize()
162 mChannelIndex = self.RegisterProperty( "uChannels", Vector3(1.0f, 1.0f, 1.0f) );
165 void ImageChannelControl::OnStageConnection( int depth )
167 Control::OnStageConnection( depth );
170 void ImageChannelControl::OnStageDisconnection()
172 Control::OnStageDisconnection();
175 void ImageChannelControl::OnSizeSet( const Vector3& targetSize )
177 Control::OnSizeSet( targetSize );
181 Vector2 size( targetSize );
182 Property::Map transformMap;
184 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
185 .Add( Toolkit::DevelVisual::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
186 .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
187 .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
188 .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
190 mVisual.SetTransformAndSize( transformMap, size );
194 Vector3 ImageChannelControl::GetNaturalSize()
199 mVisual.GetNaturalSize(naturalSize);
200 return Vector3(naturalSize);
202 return Vector3::ZERO;
205 void ImageChannelControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
208 Control::OnStyleChange( styleManager, change );
212 ///////////////////////////////////////////////////////////
217 void ImageChannelControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
219 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
221 if( imageChannelControl )
223 ImageChannelControl& impl = GetImpl( imageChannelControl );
224 Actor self = impl.Self();
227 case Demo::ImageChannelControl::Property::RESOURCE_URL:
229 impl.SetImage( value.Get<std::string>() );
232 case Demo::ImageChannelControl::Property::IMAGE_VISUAL:
234 Property::Map* map = value.GetMap();
237 impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
238 impl.RegisterVisual( Demo::ImageChannelControl::Property::IMAGE_VISUAL, impl.mVisual );
242 case Demo::ImageChannelControl::Property::VISIBILITY:
244 impl.SetVisibility( value.Get<bool>() );
247 case Demo::ImageChannelControl::Property::ENABLE_VISIBILITY_TRANSITION:
249 if( value.GetType() == Property::ARRAY )
251 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
253 else if( value.GetType() == Property::MAP )
255 impl.mEnableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
259 case Demo::ImageChannelControl::Property::DISABLE_VISIBILITY_TRANSITION:
261 if( value.GetType() == Property::ARRAY )
263 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetArray());
265 else if( value.GetType() == Property::MAP )
267 impl.mDisableVisibilityTransition = Toolkit::TransitionData::New( *value.GetMap() );
271 case Demo::ImageChannelControl::Property::RED_CHANNEL:
273 impl.mChannels[0] = value.Get<float>();
274 self.SetProperty( impl.mChannelIndex, impl.mChannels );
277 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
279 impl.mChannels[1] = value.Get<float>();
280 self.SetProperty( impl.mChannelIndex, impl.mChannels );
283 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
285 impl.mChannels[2] = value.Get<float>();
286 self.SetProperty( impl.mChannelIndex, impl.mChannels );
293 Property::Value ImageChannelControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
295 Property::Value value;
297 Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
299 if ( imageChannelControl )
301 ImageChannelControl& impl = GetImpl( imageChannelControl );
302 switch ( propertyIndex )
304 case Demo::ImageChannelControl::Property::RED_CHANNEL:
306 value = impl.mChannels[0];
309 case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
311 value = impl.mChannels[1];
314 case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
316 value = impl.mChannels[2];
319 case Demo::ImageChannelControl::Property::VISIBILITY:
321 value = impl.mVisibility;