2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <dali-toolkit/internal/controls/image-view/image-view-impl.h>
18 #include <dali-toolkit/public-api/shader-effects/distance-field-effect.h>
27 return Toolkit::ImageView::New();
29 TypeRegistration mType( typeid(Toolkit::ImageView), typeid(Toolkit::Control), Create );
32 * CameraDetailConstraint, generates detail value
33 * based on camera's position and ImageView's position.
35 struct CameraDetailConstraint
37 CameraDetailConstraint(float detailFactor)
38 : mDetailFactor(detailFactor)
43 float operator()(const float& current,
44 const PropertyInput& propertyTargetPosition,
45 const PropertyInput& propertySourcePosition)
47 const Vector3& targetPosition = propertyTargetPosition.GetVector3();
48 const Vector3& sourcePosition = propertySourcePosition.GetVector3();
49 const float distance = (targetPosition - sourcePosition).Length();
50 const float detail = mDetailFactor / distance;
55 const float mDetailFactor;
58 } // unnamed namespace
69 ///////////////////////////////////////////////////////////////////////////////////////////////////
71 ///////////////////////////////////////////////////////////////////////////////////////////////////
73 Dali::Toolkit::ImageView ImageView::New()
75 // Create the implementation
76 ImageViewPtr imageView(new ImageView());
78 // Pass ownership to CustomActor via derived handle
79 Dali::Toolkit::ImageView handle(*imageView);
81 // Second-phase init of the implementation
82 // This can only be done after the CustomActor connection has been made...
83 imageView->Initialize();
88 ImageView::ImageView()
93 void ImageView::Initialize()
96 // Register property that represents the level of detail.
97 mPropertyDetail = self.RegisterProperty(Toolkit::ImageView::DETAIL_PROPERTY_NAME, 0.0f);
99 // Create an empty image actor, filling the entire size of this ImageView.
101 mImageActor = ImageActor::New( emptyImage );
102 self.Add( mImageActor );
103 mImageActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
104 mImageActor.SetParentOrigin( ParentOrigin::CENTER );
107 ImageView::~ImageView()
112 void ImageView::SetImage(const std::string& filename, ImageType type, float min, float max)
116 case Toolkit::ImageView::BitmapType:
118 SetImageBitmap(filename, min, max);
121 case Toolkit::ImageView::DistanceFieldType:
123 SetImageDistanceField(filename);
129 void ImageView::SetImageBitmap(const std::string& filename, float min, float max)
131 int minLevel = ceilf(logf(min) / logf(2.0f));
132 int maxLevel = ceilf(logf(max) / logf(2.0f));
134 ImageAttributes attributes;
135 const Vector3 size = Self().GetCurrentSize();
137 if(minLevel==maxLevel)
138 { // Single image detail level, no need for any notifications.
139 const float detail = powf(2.0f, maxLevel);
140 attributes.SetSize( size.x * detail, size.y * detail );
141 Image image = Image::New( filename, attributes);
142 mImageActor.SetImage( image );
145 { // Multi image detail level...
146 for( int level = minLevel; level <= maxLevel; level++)
148 const float minDetail = powf(2.0f, level - 1);
149 const float maxDetail = powf(2.0f, level);
150 ImageRequest req(filename, size.x * maxDetail, size.y * maxDetail );
154 AddImage(req, LessThanCondition(maxDetail) );
156 else if(level==maxLevel)
158 AddImage(req, GreaterThanCondition(minDetail) );
162 AddImage(req, InsideCondition(minDetail, maxDetail) );
168 void ImageView::SetImageDistanceField(const std::string& filename)
170 ImageAttributes attributes = Dali::ImageAttributes::NewDistanceField(1.0f, 1);
171 const Vector3 size = Self().GetCurrentSize();
173 attributes.SetSize( size.x, size.y );
174 Image image = Image::NewDistanceField(filename, attributes);
175 mImageActor.SetImage( image );
177 DistanceFieldEffect effect = DistanceFieldEffect::New();
178 Self().SetShaderEffect( effect );
181 void ImageView::SetImage(Image image)
183 mImageActor.SetImage( image );
186 void ImageView::AddImage(ImageRequest& req, PropertyCondition condition)
190 PropertyNotification notification = self.AddPropertyNotification( mPropertyDetail, condition );
192 notification.NotifySignal().Connect( this, &ImageView::OnDetailChange );
194 mNotifications[notification] = req;
197 void ImageView::SetDetail(float detail)
199 Self().SetProperty( mPropertyDetail, detail );
202 void ImageView::SetCameraActor(CameraActor camera, float detailFactor)
204 Constraint constraint = Constraint::New<float>( mPropertyDetail,
205 LocalSource( Actor::WORLD_POSITION ),
206 Source( camera, Actor::WORLD_POSITION ),
207 CameraDetailConstraint(detailFactor));
208 Self().RemoveConstraints();
209 Self().ApplyConstraint(constraint);
212 void ImageView::OnDetailChange( PropertyNotification& notification )
214 ImageRequest& req = mNotifications[notification];
215 Image image = Image::New( req.mFilename, req.mAttributes );
216 mImageActor.SetImage( image );
219 } // namespace Internal
221 } // namespace Toolkit