2 * Copyright (c) 2014 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.
19 #include <dali-toolkit/internal/controls/image-view/image-view-impl.h>
22 #include <dali/public-api/animation/constraints.h>
23 #include <dali/public-api/object/type-registry.h>
26 #include <dali-toolkit/public-api/shader-effects/distance-field-effect.h>
35 return Toolkit::ImageView::New();
37 TypeRegistration mType( typeid(Toolkit::ImageView), typeid(Toolkit::Control), Create );
40 * CameraDetailConstraint, generates detail value
41 * based on camera's position and ImageView's position.
43 struct CameraDetailConstraint
45 CameraDetailConstraint(float detailFactor)
46 : mDetailFactor(detailFactor)
51 float operator()(const float& current,
52 const PropertyInput& propertyTargetPosition,
53 const PropertyInput& propertySourcePosition)
55 const Vector3& targetPosition = propertyTargetPosition.GetVector3();
56 const Vector3& sourcePosition = propertySourcePosition.GetVector3();
57 const float distance = (targetPosition - sourcePosition).Length();
58 const float detail = mDetailFactor / distance;
63 const float mDetailFactor;
66 } // unnamed namespace
77 ///////////////////////////////////////////////////////////////////////////////////////////////////
79 ///////////////////////////////////////////////////////////////////////////////////////////////////
81 Dali::Toolkit::ImageView ImageView::New()
83 // Create the implementation
84 ImageViewPtr imageView(new ImageView());
86 // Pass ownership to CustomActor via derived handle
87 Dali::Toolkit::ImageView handle(*imageView);
89 // Second-phase init of the implementation
90 // This can only be done after the CustomActor connection has been made...
91 imageView->Initialize();
96 ImageView::ImageView()
97 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
98 mPropertyDetail( Property::INVALID_INDEX )
102 void ImageView::Initialize()
105 // Register property that represents the level of detail.
106 mPropertyDetail = self.RegisterProperty(Toolkit::ImageView::DETAIL_PROPERTY_NAME, 0.0f);
108 // Create an empty image actor, filling the entire size of this ImageView.
110 mImageActor = ImageActor::New( emptyImage );
111 self.Add( mImageActor );
112 mImageActor.SetParentOrigin( ParentOrigin::CENTER );
115 ImageView::~ImageView()
120 void ImageView::OnControlSizeSet( const Vector3& targetSize )
122 mImageActor.SetSize( targetSize );
125 void ImageView::SetImage(const std::string& filename, ImageType type, float min, float max)
129 case Toolkit::ImageView::BitmapType:
131 SetImageBitmap(filename, min, max);
134 case Toolkit::ImageView::DistanceFieldType:
136 SetImageDistanceField(filename);
142 void ImageView::SetImageBitmap(const std::string& filename, float min, float max)
144 int minLevel = ceilf(logf(min) / logf(2.0f));
145 int maxLevel = ceilf(logf(max) / logf(2.0f));
147 ImageAttributes attributes;
148 const Vector3 size = Self().GetCurrentSize();
150 if(minLevel==maxLevel)
151 { // Single image detail level, no need for any notifications.
152 const float detail = powf(2.0f, maxLevel);
153 attributes.SetSize( size.x * detail, size.y * detail );
154 Image image = Image::New( filename, attributes);
155 mImageActor.SetImage( image );
158 { // Multi image detail level...
159 for( int level = minLevel; level <= maxLevel; level++)
161 const float minDetail = powf(2.0f, level - 1);
162 const float maxDetail = powf(2.0f, level);
163 ImageRequest req(filename, size.x * maxDetail, size.y * maxDetail );
167 AddImage(req, LessThanCondition(maxDetail) );
169 else if(level==maxLevel)
171 AddImage(req, GreaterThanCondition(minDetail) );
175 AddImage(req, InsideCondition(minDetail, maxDetail) );
181 void ImageView::SetImageDistanceField(const std::string& filename)
183 ImageAttributes attributes = Dali::ImageAttributes::NewDistanceField(1.0f, 1);
184 const Vector3 size = Self().GetCurrentSize();
186 attributes.SetSize( size.x, size.y );
187 Image image = Image::NewDistanceField(filename, attributes);
188 mImageActor.SetImage( image );
190 DistanceFieldEffect effect = DistanceFieldEffect::New();
191 mImageActor.SetShaderEffect( effect );
194 void ImageView::SetImage(Image image)
196 mImageActor.SetImage( image );
199 void ImageView::AddImage(ImageRequest& req, PropertyCondition condition)
203 PropertyNotification notification = self.AddPropertyNotification( mPropertyDetail, condition );
205 notification.NotifySignal().Connect( this, &ImageView::OnDetailChange );
207 mNotifications[notification] = req;
210 void ImageView::SetDetail(float detail)
212 Self().SetProperty( mPropertyDetail, detail );
215 void ImageView::SetCameraActor(CameraActor camera, float detailFactor)
217 Constraint constraint = Constraint::New<float>( mPropertyDetail,
218 LocalSource( Actor::WORLD_POSITION ),
219 Source( camera, Actor::WORLD_POSITION ),
220 CameraDetailConstraint(detailFactor));
221 Self().RemoveConstraints();
222 Self().ApplyConstraint(constraint);
225 void ImageView::OnDetailChange( PropertyNotification& notification )
227 ImageRequest& req = mNotifications[notification];
228 Image image = Image::New( req.mFilename, req.mAttributes );
229 mImageActor.SetImage( image );
232 } // namespace Internal
234 } // namespace Toolkit