From 1f2225607eeb5dd96fa7e282abf0d6a470a7693b Mon Sep 17 00:00:00 2001 From: Kingsley Stephens Date: Wed, 17 Sep 2014 10:58:13 +0100 Subject: [PATCH] Add GetNaturalSize to Actor and deriving classes. Change-Id: I5562282646fb6d17b12cb0558854bffcc18cd4bb --- .../src/dali-unmanaged/utc-Dali-CustomActor.cpp | 5 +++ .../src/dali-unmanaged/utc-Dali-TypeRegistry.cpp | 5 +++ automated-tests/src/dali/utc-Dali-Actor.cpp | 12 ++++++ automated-tests/src/dali/utc-Dali-CustomActor.cpp | 15 +++++++ automated-tests/src/dali/utc-Dali-ImageActor.cpp | 19 +++++++++ automated-tests/src/dali/utc-Dali-TextActor.cpp | 16 ++++++++ dali/internal/event/actors/actor-impl.cpp | 16 ++++++-- dali/internal/event/actors/actor-impl.h | 16 ++++++++ dali/internal/event/actors/custom-actor-internal.h | 8 ++++ dali/internal/event/actors/image-actor-impl.cpp | 47 ++++++++++++++-------- dali/internal/event/actors/image-actor-impl.h | 14 +++++++ dali/internal/event/actors/text-actor-impl.cpp | 6 +++ dali/internal/event/actors/text-actor-impl.h | 7 ++++ dali/public-api/actors/actor.cpp | 5 +++ dali/public-api/actors/actor.h | 9 +++++ dali/public-api/actors/custom-actor-impl.h | 7 ++++ 16 files changed, 187 insertions(+), 20 deletions(-) diff --git a/automated-tests/src/dali-unmanaged/utc-Dali-CustomActor.cpp b/automated-tests/src/dali-unmanaged/utc-Dali-CustomActor.cpp index 93ca794..8bb6ede 100644 --- a/automated-tests/src/dali-unmanaged/utc-Dali-CustomActor.cpp +++ b/automated-tests/src/dali-unmanaged/utc-Dali-CustomActor.cpp @@ -164,6 +164,11 @@ struct TestCustomActor : public CustomActorImpl } } + virtual Vector3 GetNaturalSize() + { + return Vector3( 0.0f, 0.0f, 0.0f ); + } + void SetDaliProperty(std::string s) { Self().SetProperty(mDaliProperty, s) ; diff --git a/automated-tests/src/dali-unmanaged/utc-Dali-TypeRegistry.cpp b/automated-tests/src/dali-unmanaged/utc-Dali-TypeRegistry.cpp index 6e80cc8..53eb88c 100644 --- a/automated-tests/src/dali-unmanaged/utc-Dali-TypeRegistry.cpp +++ b/automated-tests/src/dali-unmanaged/utc-Dali-TypeRegistry.cpp @@ -242,6 +242,11 @@ struct MyTestCustomActor : public CustomActorImpl return Actor::New(); } + virtual Vector3 GetNaturalSize() + { + return Vector3( 0.0f, 0.0f, 0.0f ); + } + public: SignalType mSignal; diff --git a/automated-tests/src/dali/utc-Dali-Actor.cpp b/automated-tests/src/dali/utc-Dali-Actor.cpp index 94f37c2..38dddff 100644 --- a/automated-tests/src/dali/utc-Dali-Actor.cpp +++ b/automated-tests/src/dali/utc-Dali-Actor.cpp @@ -806,6 +806,18 @@ int UtcDaliActorGetCurrentSize(void) END_TEST; } +int UtcDaliActorGetNaturalSize(void) +{ + TestApplication application; + + Actor actor = Actor::New(); + Vector3 vector( 0.0f, 0.0f, 0.0f ); + + DALI_TEST_CHECK( actor.GetNaturalSize() == vector ); + + END_TEST; +} + // SetPosition(float x, float y) int UtcDaliActorSetPosition01(void) { diff --git a/automated-tests/src/dali/utc-Dali-CustomActor.cpp b/automated-tests/src/dali/utc-Dali-CustomActor.cpp index 3feb248..2e54d07 100644 --- a/automated-tests/src/dali/utc-Dali-CustomActor.cpp +++ b/automated-tests/src/dali/utc-Dali-CustomActor.cpp @@ -176,6 +176,11 @@ struct TestCustomActor : public CustomActorImpl } } + virtual Vector3 GetNaturalSize() + { + return Vector3( 0.0f, 0.0f, 0.0f ); + } + void SetDaliProperty(std::string s) { Self().SetProperty(mDaliProperty, s) ; @@ -470,6 +475,11 @@ public: { return Actor(); } + + virtual Vector3 GetNaturalSize() + { + return Vector3( 0.0f, 0.0f, 0.0f ); + } }; } ; // namespace Impl @@ -609,6 +619,11 @@ public: return GetImpl().mTargetSize; } + virtual Vector3 GetNaturalSize() + { + return Vector3( 0.0f, 0.0f, 0.0f ); + } + private: TestCustomActor( Impl::TestCustomActor& impl ) : CustomActor( impl ) diff --git a/automated-tests/src/dali/utc-Dali-ImageActor.cpp b/automated-tests/src/dali/utc-Dali-ImageActor.cpp index 51a428c..42c4846 100644 --- a/automated-tests/src/dali/utc-Dali-ImageActor.cpp +++ b/automated-tests/src/dali/utc-Dali-ImageActor.cpp @@ -1091,3 +1091,22 @@ int UtcDaliImageActorNinePatch04(void) END_TEST; } + +int UtcDaliImageActorGetNaturalSize(void) +{ + TestApplication application; + + // Standard image + BitmapImage img = BitmapImage::New( 10, 10 ); + ImageActor actor = ImageActor::New( img ); + + DALI_TEST_CHECK( actor.GetNaturalSize().GetVectorXY() == Vector2( 10, 10 ) ); + + // Pixel area set + ImageActor::PixelArea area( 1, 2, 3, 4 ); + actor.SetPixelArea( area ); + + DALI_TEST_CHECK( actor.GetNaturalSize().GetVectorXY() == Vector2( 3, 4 ) ); + + END_TEST; +} diff --git a/automated-tests/src/dali/utc-Dali-TextActor.cpp b/automated-tests/src/dali/utc-Dali-TextActor.cpp index 892e210..a57b2aa 100644 --- a/automated-tests/src/dali/utc-Dali-TextActor.cpp +++ b/automated-tests/src/dali/utc-Dali-TextActor.cpp @@ -1061,3 +1061,19 @@ int UtcDaliTextActorPropertyIndices(void) DALI_TEST_EQUALS( indices.size(), textActor.GetPropertyCount(), TEST_LOCATION ); END_TEST; } + +int UtcDaliTextActorGetNaturalSize(void) +{ + TestApplication application; + + TextActor actor = TextActor::New(); + std::string text( "something else" ); + actor.SetText( text ); + + Font defaultFont = Font::New(); + Vector3 naturalSize = defaultFont.MeasureText( text ); + + DALI_TEST_CHECK( actor.GetNaturalSize().GetVectorXY() == naturalSize.GetVectorXY() ); + + END_TEST; +} diff --git a/dali/internal/event/actors/actor-impl.cpp b/dali/internal/event/actors/actor-impl.cpp index 9675844..52c903a 100644 --- a/dali/internal/event/actors/actor-impl.cpp +++ b/dali/internal/event/actors/actor-impl.cpp @@ -1091,9 +1091,12 @@ void Actor::SetSize(float width, float height, float depth) void Actor::SetSize(const Vector2& size) { - Vector3 volume( size ); - volume.z = std::min( size.width, size.height ); - SetSize( volume ); + SetSize( Vector3( size.width, size.height, CalculateSizeZ( size ) ) ); +} + +float Actor::CalculateSizeZ( const Vector2& size ) const +{ + return std::min( size.width, size.height ); } void Actor::SetSize(const Vector3& size) @@ -1154,6 +1157,13 @@ const Vector3& Actor::GetCurrentSize() const return Vector3::ZERO; } +Vector3 Actor::GetNaturalSize() const +{ + // It is up to deriving classes to return the appropriate natural size + return Vector3( 0.0f, 0.0f, 0.0f ); +} + + #ifdef DYNAMICS_SUPPORT //--------------- Dynamics --------------- diff --git a/dali/internal/event/actors/actor-impl.h b/dali/internal/event/actors/actor-impl.h index 0b60100..7bee28b 100644 --- a/dali/internal/event/actors/actor-impl.h +++ b/dali/internal/event/actors/actor-impl.h @@ -301,6 +301,13 @@ public: const Vector3& GetCurrentSize() const; /** + * Return the natural size of the actor + * + * @return The actor's natural size + */ + virtual Vector3 GetNaturalSize() const; + + /** * Set the origin of an actor, within its parent's area. * This is expressed in 2D unit coordinates, such that (0.0, 0.0, 0.5) is the top-left corner of the parent, * and (1.0, 1.0, 0.5) is the bottom-right corner. @@ -1088,6 +1095,15 @@ protected: */ bool IsNodeConnected() const; + /** + * Calculate the size of the z dimension for a 2D size + * + * @param[in] size The 2D size (X, Y) to calculate Z from + * + * @return Return the Z dimension for this size + */ + float CalculateSizeZ( const Vector2& size ) const; + public: // Default property extensions from ProxyObject /** diff --git a/dali/internal/event/actors/custom-actor-internal.h b/dali/internal/event/actors/custom-actor-internal.h index f8515f3..44b3418 100644 --- a/dali/internal/event/actors/custom-actor-internal.h +++ b/dali/internal/event/actors/custom-actor-internal.h @@ -158,6 +158,14 @@ private: } /** + * @copydoc Internal::Actor::GetNaturalSize + */ + virtual Vector3 GetNaturalSize() const + { + return mImpl->GetNaturalSize(); + } + + /** * Private constructor; see also CustomActor::New() */ CustomActor(CustomActorImpl& extension); diff --git a/dali/internal/event/actors/image-actor-impl.cpp b/dali/internal/event/actors/image-actor-impl.cpp index 7a981f2..60fcfb8 100644 --- a/dali/internal/event/actors/image-actor-impl.cpp +++ b/dali/internal/event/actors/image-actor-impl.cpp @@ -237,28 +237,41 @@ void ImageActor::SetNaturalSize() { if( mUsingNaturalSize ) { - // if no image then natural size is 0 - Vector2 size; - ImagePtr image = mImageAttachment->GetImage(); - if( image ) - { - if( IsPixelAreaSet() ) - { - PixelArea area(GetPixelArea()); - size.width = area.width; - size.height = area.height; - } - else - { - size = image->GetNaturalSize(); - } - } mInternalSetSize = true; - SetSize( size ); + SetSize( CalculateNaturalSize() ); mInternalSetSize = false; } } +Vector3 ImageActor::GetNaturalSize() const +{ + Vector2 naturalSize( CalculateNaturalSize() ); + return Vector3( naturalSize.width, naturalSize.height, CalculateSizeZ( naturalSize ) ); +} + +Vector2 ImageActor::CalculateNaturalSize() const +{ + // if no image then natural size is 0 + Vector2 size( 0.0f, 0.0f ); + + ImagePtr image = mImageAttachment->GetImage(); + if( image ) + { + if( IsPixelAreaSet() ) + { + PixelArea area(GetPixelArea()); + size.width = area.width; + size.height = area.height; + } + else + { + size = image->GetNaturalSize(); + } + } + + return size; +} + void ImageActor::OnSizeSet( const Vector3& targetSize ) { if( !mInternalSetSize ) diff --git a/dali/internal/event/actors/image-actor-impl.h b/dali/internal/event/actors/image-actor-impl.h index fb6aaca..d89970c 100644 --- a/dali/internal/event/actors/image-actor-impl.h +++ b/dali/internal/event/actors/image-actor-impl.h @@ -181,6 +181,13 @@ public: // Default property extensions from ProxyObject */ virtual Property::Value GetDefaultProperty( Property::Index index ) const; +public: // From Actor + + /** + * @copydoc Dali::Actor::GetNaturalSize() + */ + virtual Vector3 GetNaturalSize() const; + private: // From RenderableActor /** @@ -208,6 +215,13 @@ private: void SetNaturalSize(); /** + * Calculate the natural size of this image actor + * + * @return Return the natural size as a Vector2 + */ + Vector2 CalculateNaturalSize() const; + + /** * From Actor. * This is called after SizeSet() has been called. */ diff --git a/dali/internal/event/actors/text-actor-impl.cpp b/dali/internal/event/actors/text-actor-impl.cpp index 5ae1921..42dd751 100644 --- a/dali/internal/event/actors/text-actor-impl.cpp +++ b/dali/internal/event/actors/text-actor-impl.cpp @@ -288,6 +288,12 @@ void TextActor::SetFont(Font& font, TextRequestMode mode ) } } +Vector3 TextActor::GetNaturalSize() const +{ + Vector2 naturalSize( mTextAttachment->GetNaturalTextSize() ); + return Vector3( naturalSize.width, naturalSize.height, CalculateSizeZ( naturalSize ) ); +} + void TextActor::OnSizeSet(const Vector3& targetSize) { if( !mInternalSetSize ) diff --git a/dali/internal/event/actors/text-actor-impl.h b/dali/internal/event/actors/text-actor-impl.h index 08494cd..7d7192b 100644 --- a/dali/internal/event/actors/text-actor-impl.h +++ b/dali/internal/event/actors/text-actor-impl.h @@ -225,6 +225,13 @@ public: */ bool IsFontDetectionAutomatic() const; +public: // From Actor + + /** + * @copydoc Dali::Actor::GetNaturalSize() + */ + virtual Vector3 GetNaturalSize() const; + private: // from Actor /** diff --git a/dali/public-api/actors/actor.cpp b/dali/public-api/actors/actor.cpp index 6469997..284e45b 100644 --- a/dali/public-api/actors/actor.cpp +++ b/dali/public-api/actors/actor.cpp @@ -230,6 +230,11 @@ Vector3 Actor::GetCurrentSize() const return GetImplementation(*this).GetCurrentSize(); } +Vector3 Actor::GetNaturalSize() const +{ + return GetImplementation(*this).GetNaturalSize(); +} + void Actor::SetPosition(float x, float y) { GetImplementation(*this).SetPosition(x, y); diff --git a/dali/public-api/actors/actor.h b/dali/public-api/actors/actor.h index da29650..631b591 100644 --- a/dali/public-api/actors/actor.h +++ b/dali/public-api/actors/actor.h @@ -621,6 +621,15 @@ public: Vector3 GetCurrentSize() const; /** + * Return the natural size of the actor. + * + * Deriving classes stipulate the natural size and by default an actor has a ZERO natural size. + * + * @return The actor's natural size + */ + Vector3 GetNaturalSize() const; + + /** * @brief Sets the position of the actor. * * The Actor's z position will be set to 0.0f. diff --git a/dali/public-api/actors/custom-actor-impl.h b/dali/public-api/actors/custom-actor-impl.h index df7ad32..071cb8f 100644 --- a/dali/public-api/actors/custom-actor-impl.h +++ b/dali/public-api/actors/custom-actor-impl.h @@ -189,6 +189,13 @@ public: */ virtual Dali::Actor GetChildByAlias(const std::string& actorAlias) = 0; + /** + * Return the natural size of the actor + * + * @return The actor's natural size + */ + virtual Vector3 GetNaturalSize() = 0; + protected: // For derived classes /** -- 2.7.4