2 * Copyright (c) 2022 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 "image-view-impl.h"
22 #include <dali/devel-api/scripting/scripting.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/public-api/object/type-registry.h>
27 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
28 #include <dali-toolkit/devel-api/controls/control-devel.h>
29 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
30 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
45 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
49 return Toolkit::ImageView::New();
52 // Setup properties, signals and actions using the type-registry.
53 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::ImageView, Toolkit::Control, Create);
54 DALI_PROPERTY_REGISTRATION(Toolkit, ImageView, "image", MAP, IMAGE)
55 DALI_PROPERTY_REGISTRATION(Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA)
57 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA)
58 DALI_TYPE_REGISTRATION_END()
60 } // anonymous namespace
64 ImageView::ImageView(ControlBehaviour additionalBehaviour)
65 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT | additionalBehaviour)),
67 mImageVisualPaddingSetByTransform(false),
68 mImageViewPixelAreaSetByFittingMode(false)
72 ImageView::~ImageView()
76 Toolkit::ImageView ImageView::New(ControlBehaviour additionalBehaviour)
78 ImageView* impl = new ImageView(additionalBehaviour);
80 Toolkit::ImageView handle = Toolkit::ImageView(*impl);
82 // Second-phase init of the implementation
83 // This can only be done after the CustomActor connection has been made...
89 /////////////////////////////////////////////////////////////
91 void ImageView::OnInitialize()
93 // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
94 Dali::Toolkit::Control handle(GetOwner());
95 handle.ResourceReadySignal().Connect(this, &ImageView::OnResourceReady);
97 Self().SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::IMAGE);
100 void ImageView::SetImage(const Property::Map& map)
102 // Comparing a property map is too expensive so just creating a new visual
106 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual(mPropertyMap);
109 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
115 if(!mShaderMap.Empty())
117 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation(visual);
118 visualImpl.SetCustomShader(mShaderMap);
121 DevelControl::RegisterVisual(*this, Toolkit::ImageView::Property::IMAGE, visual);
125 // Unregister the exsiting visual
126 DevelControl::UnregisterVisual(*this, Toolkit::ImageView::Property::IMAGE);
128 // Trigger a size negotiation request that may be needed when unregistering a visual.
132 // Signal that a Relayout may be needed
135 void ImageView::SetImage(const std::string& url, ImageDimensions size)
137 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
140 mPropertyMap.Clear();
142 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
143 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual(url, size);
151 if(!mShaderMap.Empty())
153 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation(visual);
154 visualImpl.SetCustomShader(mShaderMap);
157 DevelControl::RegisterVisual(*this, Toolkit::ImageView::Property::IMAGE, visual);
161 // Unregister the exsiting visual
162 DevelControl::UnregisterVisual(*this, Toolkit::ImageView::Property::IMAGE);
164 // Trigger a size negotiation request that may be needed when unregistering a visual.
168 // Signal that a Relayout may be needed
171 void ImageView::ClearImageVisual()
173 // Clear cached properties
174 mPropertyMap.Clear();
177 // Unregister the exsiting visual
178 DevelControl::UnregisterVisual(*this, Toolkit::ImageView::Property::IMAGE);
180 // Trigger a size negotiation request that may be needed when unregistering a visual.
184 void ImageView::EnablePreMultipliedAlpha(bool preMultipled)
188 Toolkit::GetImplementation(mVisual).EnablePreMultipliedAlpha(preMultipled);
192 bool ImageView::IsPreMultipliedAlphaEnabled() const
196 return Toolkit::GetImplementation(mVisual).IsPreMultipliedAlphaEnabled();
201 void ImageView::SetDepthIndex(int depthIndex)
205 mVisual.SetDepthIndex(depthIndex);
209 Vector3 ImageView::GetNaturalSize()
213 Vector2 rendererNaturalSize;
214 mVisual.GetNaturalSize(rendererNaturalSize);
217 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
219 rendererNaturalSize.width += (padding.start + padding.end);
220 rendererNaturalSize.height += (padding.top + padding.bottom);
221 return Vector3(rendererNaturalSize);
224 // if no visual then use Control's natural size
225 return Control::GetNaturalSize();
228 float ImageView::GetHeightForWidth(float width)
231 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
235 return mVisual.GetHeightForWidth(width) + padding.top + padding.bottom;
239 return Control::GetHeightForWidth(width) + padding.top + padding.bottom;
243 float ImageView::GetWidthForHeight(float height)
246 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
250 return mVisual.GetWidthForHeight(height) + padding.start + padding.end;
254 return Control::GetWidthForHeight(height) + padding.start + padding.end;
258 void ImageView::OnRelayout(const Vector2& size, RelayoutContainer& container)
260 Control::OnRelayout(size, container);
263 Property::Map transformMap = Property::Map();
265 Extents padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
267 bool zeroPadding = (padding == Extents());
270 mVisual.GetNaturalSize(naturalSize);
272 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
273 Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
274 if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
276 std::swap(padding.start, padding.end);
279 // remove padding from the size to know how much is left for the visual
280 Vector2 finalSize = size - Vector2(padding.start + padding.end, padding.top + padding.bottom);
281 Vector2 finalOffset = Vector2(padding.start, padding.top);
283 ApplyFittingMode(finalSize, naturalSize, finalOffset, zeroPadding, transformMap);
285 mVisual.SetTransformAndSize(transformMap, size);
287 // mVisual is not updated util the resource is ready in the case of visual replacement.
288 // in this case, the Property Map must be initialized so that the previous value is not reused.
289 // after mVisual is updated, the correct value will be reset.
290 Toolkit::Visual::Base visual = DevelControl::GetVisual(*this, Toolkit::ImageView::Property::IMAGE);
291 if(visual && visual != mVisual)
293 visual.SetTransformAndSize(Property::Map(), size);
298 void ImageView::OnCreateTransitions(std::vector<std::pair<Dali::Property::Index, Dali::Property::Map>>& sourceProperties,
299 std::vector<std::pair<Dali::Property::Index, Dali::Property::Map>>& destinationProperties,
300 Dali::Toolkit::Control source,
301 Dali::Toolkit::Control destination)
303 // Retrieves image properties to be transitioned.
304 Dali::Property::Map imageSourcePropertyMap, imageDestinationPropertyMap;
305 MakeVisualTransition(imageSourcePropertyMap, imageDestinationPropertyMap, source, destination, Toolkit::ImageView::Property::IMAGE);
306 if(imageSourcePropertyMap.Count() > 0)
308 sourceProperties.push_back(std::pair<Dali::Property::Index, Dali::Property::Map>(Toolkit::ImageView::Property::IMAGE, imageSourcePropertyMap));
309 destinationProperties.push_back(std::pair<Dali::Property::Index, Dali::Property::Map>(Toolkit::ImageView::Property::IMAGE, imageDestinationPropertyMap));
313 void ImageView::OnUpdateVisualProperties(const std::vector<std::pair<Dali::Property::Index, Dali::Property::Map>>& properties)
315 Toolkit::Visual::Base visual = DevelControl::GetVisual(*this, Toolkit::ImageView::Property::IMAGE);
318 Dali::Toolkit::Control handle(GetOwner());
320 for(auto&& data : properties)
322 if(data.first == Toolkit::ImageView::Property::IMAGE)
324 DevelControl::DoAction(handle, Toolkit::ImageView::Property::IMAGE, DevelVisual::Action::UPDATE_PROPERTY, data.second);
331 void ImageView::OnResourceReady(Toolkit::Control control)
333 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
334 mVisual = DevelControl::GetVisual(*this, Toolkit::ImageView::Property::IMAGE);
335 // Signal that a Relayout may be needed
338 void ImageView::SetTransformMapForFittingMode(Vector2 finalSize, Vector2 naturalSize, Vector2 finalOffset, Visual::FittingMode fittingMode, Property::Map& transformMap)
342 case Visual::FittingMode::FIT_KEEP_ASPECT_RATIO:
344 auto availableVisualSize = finalSize;
346 // scale to fit the padded area
347 finalSize = naturalSize * std::min((naturalSize.width ? (availableVisualSize.width / naturalSize.width) : 0),
348 (naturalSize.height ? (availableVisualSize.height / naturalSize.height) : 0));
350 // calculate final offset within the padded area
351 finalOffset += (availableVisualSize - finalSize) * .5f;
353 // populate the transform map
354 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, finalOffset)
355 .Add(Toolkit::Visual::Transform::Property::SIZE, finalSize);
358 case Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO:
360 mImageViewPixelAreaSetByFittingMode = true;
361 auto availableVisualSize = finalSize;
362 finalSize = naturalSize * std::max((naturalSize.width ? (availableVisualSize.width / naturalSize.width) : 0),
363 (naturalSize.height ? (availableVisualSize.height / naturalSize.height) : 0));
365 auto originalOffset = finalOffset;
366 finalOffset += (availableVisualSize - finalSize) * .5f;
368 float x = abs((availableVisualSize.width - finalSize.width) / finalSize.width) * .5f;
369 float y = abs((availableVisualSize.height - finalSize.height) / finalSize.height) * .5f;
370 float widthRatio = 1.f - abs((availableVisualSize.width - finalSize.width) / finalSize.width);
371 float heightRatio = 1.f - abs((availableVisualSize.height - finalSize.height) / finalSize.height);
372 Vector4 pixelArea = Vector4(x, y, widthRatio, heightRatio);
373 Self().SetProperty(Toolkit::ImageView::Property::PIXEL_AREA, pixelArea);
375 // populate the transform map
376 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, originalOffset)
377 .Add(Toolkit::Visual::Transform::Property::SIZE, availableVisualSize);
380 case Visual::FittingMode::CENTER:
382 auto availableVisualSize = finalSize;
383 if(availableVisualSize.width > naturalSize.width && availableVisualSize.height > naturalSize.height)
385 finalSize = naturalSize;
389 finalSize = naturalSize * std::min((naturalSize.width ? (availableVisualSize.width / naturalSize.width) : 0),
390 (naturalSize.height ? (availableVisualSize.height / naturalSize.height) : 0));
393 finalOffset += (availableVisualSize - finalSize) * .5f;
395 // populate the transform map
396 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, finalOffset)
397 .Add(Toolkit::Visual::Transform::Property::SIZE, finalSize);
400 case Visual::FittingMode::FILL:
402 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, finalOffset)
403 .Add(Toolkit::Visual::Transform::Property::SIZE, finalSize);
406 case Visual::FittingMode::FIT_WIDTH:
407 case Visual::FittingMode::FIT_HEIGHT:
409 // This FittingMode already converted
415 void ImageView::ApplyFittingMode(Vector2 finalSize, Vector2 naturalSize, Vector2 finalOffset, bool zeroPadding, Property::Map& transformMap)
417 Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
419 // Reset PIXEL_AREA after using OVER_FIT_KEEP_ASPECT_RATIO
420 if(mImageViewPixelAreaSetByFittingMode)
422 Self().SetProperty(Toolkit::ImageView::Property::PIXEL_AREA, FULL_TEXTURE_RECT);
423 mImageViewPixelAreaSetByFittingMode = false;
426 if((!zeroPadding) || // If padding is not zero
427 (fittingMode != Visual::FittingMode::FILL))
429 mImageVisualPaddingSetByTransform = true;
431 // If FittingMode use FIT_WIDTH or FIT_HEIGTH, it need to change proper fittingMode
432 if(fittingMode == Visual::FittingMode::FIT_WIDTH)
434 fittingMode = (finalSize.height / naturalSize.height) < (finalSize.width / naturalSize.width) ? Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO : Visual::FittingMode::FIT_KEEP_ASPECT_RATIO;
436 else if(fittingMode == Visual::FittingMode::FIT_HEIGHT)
438 fittingMode = (finalSize.height / naturalSize.height) < (finalSize.width / naturalSize.width) ? Visual::FittingMode::FIT_KEEP_ASPECT_RATIO : Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO;
441 SetTransformMapForFittingMode(finalSize, naturalSize, finalOffset, fittingMode, transformMap);
443 // Set extra value for applying transformMap
444 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY,
445 Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
446 .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
447 .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN)
448 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY,
449 Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE));
451 else if(mImageVisualPaddingSetByTransform && zeroPadding) // Reset offset to zero only if padding applied previously
453 mImageVisualPaddingSetByTransform = false;
454 // Reset the transform map
455 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, Vector2::ZERO)
456 .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY,
457 Vector2(Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE))
458 .Add(Toolkit::Visual::Transform::Property::SIZE, Vector2::ONE)
459 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY,
460 Vector2(Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE));
464 ///////////////////////////////////////////////////////////
469 void ImageView::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
471 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast(Dali::BaseHandle(object));
475 ImageView& impl = GetImpl(imageView);
478 case Toolkit::ImageView::Property::IMAGE:
480 std::string imageUrl;
481 const Property::Map* map;
482 if(value.Get(imageUrl))
484 impl.SetImage(imageUrl, ImageDimensions());
486 // if its not a string then get a Property::Map from the property if possible.
489 map = value.GetMap();
492 // the property map is emtpy map. Unregister visual.
493 if(DALI_UNLIKELY(map->Count() == 0u))
495 impl.ClearImageVisual();
499 Property::Value* shaderValue = map->Find(Toolkit::Visual::Property::SHADER, CUSTOM_SHADER);
500 // set image only if property map contains image information other than custom shader
501 if(map->Count() > 1u || !shaderValue)
505 // the property map contains only the custom shader
506 else if((map->Count() == 1u) && (shaderValue))
508 Property::Map* shaderMap = shaderValue->GetMap();
511 impl.mShaderMap = *shaderMap;
513 if(!impl.mUrl.empty())
515 impl.SetImage(impl.mUrl, impl.mImageSize);
517 else if(!impl.mPropertyMap.Empty())
519 impl.SetImage(impl.mPropertyMap);
527 // invalid property value comes. Unregister visual.
528 impl.ClearImageVisual();
534 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
539 impl.EnablePreMultipliedAlpha(isPre);
547 Property::Value ImageView::GetProperty(BaseObject* object, Property::Index propertyIndex)
549 Property::Value value;
551 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast(Dali::BaseHandle(object));
555 ImageView& impl = GetImpl(imageview);
556 switch(propertyIndex)
558 case Toolkit::ImageView::Property::IMAGE:
560 if(!impl.mUrl.empty())
567 Toolkit::Visual::Base visual = DevelControl::GetVisual(impl, Toolkit::ImageView::Property::IMAGE);
570 visual.CreatePropertyMap(map);
577 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
579 value = impl.IsPreMultipliedAlphaEnabled();
588 } // namespace Internal
589 } // namespace Toolkit