From 4bfc1d5f237df92a26659d0f55a0c451b59bc2e4 Mon Sep 17 00:00:00 2001 From: Adeel Kazmi Date: Wed, 3 Jul 2019 17:47:47 +0100 Subject: [PATCH] Add ability to set all properties on a handle Change-Id: I931d33e97cd5bd1133288d34f72741198e3f2dc8 --- automated-tests/src/dali/utc-Dali-Handle.cpp | 51 +++++++++++++++++++++++++++- dali/devel-api/object/handle-devel.cpp | 7 +++- dali/devel-api/object/handle-devel.h | 24 ++++++++++++- dali/internal/event/common/object-impl.cpp | 21 +++++++++++- dali/internal/event/common/object-impl.h | 6 ++++ 5 files changed, 105 insertions(+), 4 deletions(-) diff --git a/automated-tests/src/dali/utc-Dali-Handle.cpp b/automated-tests/src/dali/utc-Dali-Handle.cpp index 6c6c607..ec92ffa 100644 --- a/automated-tests/src/dali/utc-Dali-Handle.cpp +++ b/automated-tests/src/dali/utc-Dali-Handle.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * Copyright (c) 2019 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1666,3 +1666,52 @@ int UtcDaliHandlePropertySetSignal03(void) DALI_TEST_EQUALS( propertySetCheck.mValue, Property::Value( 29 ), TEST_LOCATION ); END_TEST; } + +int UtcDaliHandlePropertySetProperties(void) +{ + TestApplication application; + const Vector3 actorSize( 10.0f, 20.0f, 30.0f ); + const Vector3 anchorPoint( 1.0f, 0.5f, 0.0f ); + const Vector4 color( 0.1f, 0.2, 0.3f, 0.4f ); + + Handle handle = Actor::New(); + DevelHandle::SetProperties( + handle, + Property::Map + { + { Actor::Property::SIZE, actorSize }, + { Actor::Property::ANCHOR_POINT, anchorPoint }, + { "color", color }, + { "invalid", Vector2::ZERO } // It should quietly ignore invalid data + } + ); + DALI_TEST_EQUALS( handle.GetProperty( Actor::Property::SIZE ).Get< Vector3 >(), actorSize, TEST_LOCATION ); + DALI_TEST_EQUALS( handle.GetProperty( Actor::Property::ANCHOR_POINT ).Get< Vector3 >(), anchorPoint, TEST_LOCATION ); + DALI_TEST_EQUALS( handle.GetProperty( Actor::Property::COLOR ).Get< Vector4 >(), color, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliHandleTemplateNew(void) +{ + TestApplication application; + const Vector3 actorSize( 10.0f, 20.0f, 30.0f ); + const Vector3 anchorPoint( 1.0f, 0.5f, 0.0f ); + const Vector4 color( 0.1f, 0.2, 0.3f, 0.4f ); + + Handle handle = DevelHandle::New< Actor >( + Property::Map + { + { Actor::Property::SIZE, actorSize }, + { Actor::Property::ANCHOR_POINT, anchorPoint }, + { "color", color }, + { "invalid", Vector2::ZERO } // It should quietly ignore invalid data + } + ); + + DALI_TEST_EQUALS( handle.GetProperty( Actor::Property::SIZE ).Get< Vector3 >(), actorSize, TEST_LOCATION ); + DALI_TEST_EQUALS( handle.GetProperty( Actor::Property::ANCHOR_POINT ).Get< Vector3 >(), anchorPoint, TEST_LOCATION ); + DALI_TEST_EQUALS( handle.GetProperty( Actor::Property::COLOR ).Get< Vector4 >(), color, TEST_LOCATION ); + + END_TEST; +} diff --git a/dali/devel-api/object/handle-devel.cpp b/dali/devel-api/object/handle-devel.cpp index f5e8951..68ddb92 100644 --- a/dali/devel-api/object/handle-devel.cpp +++ b/dali/devel-api/object/handle-devel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * Copyright (c) 2019 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,6 +44,11 @@ Property::Index RegisterProperty( Handle handle, Property::Index key, const std: return GetImplementation( handle ).RegisterProperty( name, key, propertyValue ); } +void SetProperties( Handle handle, const Property::Map& properties ) +{ + GetImplementation( handle ).SetProperties( properties ); +} + void SetTypeInfo( Handle& handle, const TypeInfo& typeInfo ) { GetImplementation( handle ).SetTypeInfo( &GetImplementation( typeInfo ) ); diff --git a/dali/devel-api/object/handle-devel.h b/dali/devel-api/object/handle-devel.h index e8d3803..d5c2862 100644 --- a/dali/devel-api/object/handle-devel.h +++ b/dali/devel-api/object/handle-devel.h @@ -2,7 +2,7 @@ #define DALI_HANDLE_DEVEL_H /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * Copyright (c) 2019 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -99,6 +99,14 @@ DALI_CORE_API Property::Index GetPropertyIndex( const Handle& handle, Property:: DALI_CORE_API Property::Index RegisterProperty( Handle handle, Property::Index key, const std::string& name, const Property::Value& propertyValue ); /** + * @brief Sets all the properties in the given property map. + * + * @param[in] handle The handle to set the properties on + * @param[in] properties The properties to set + */ +DALI_CORE_API void SetProperties( Handle handle, const Property::Map& properties ); + +/** * @brief Set the type-info that the object is created by. * * @note This is particularly useful to link C# custom control with its correct type-info in the native side @@ -131,6 +139,20 @@ using PropertySetSignalType = Signal< void( Handle& handle, Property::Index inde */ DALI_CORE_API PropertySetSignalType& PropertySetSignal( Handle handle ); +/** + * @brief Template to create a derived handle and set properties on it. + * + * @tparam T The derived class to create + * + * @param[in] properties The properties to set + */ +template< typename T > +DALI_INTERNAL T New( const Property::Map& properties ) +{ + T handle = T::New(); + SetProperties( handle, properties ); + return handle; +} } // namespace DevelHandle diff --git a/dali/internal/event/common/object-impl.cpp b/dali/internal/event/common/object-impl.cpp index d6c5cea..5dd66f8 100644 --- a/dali/internal/event/common/object-impl.cpp +++ b/dali/internal/event/common/object-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * Copyright (c) 2019 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -581,6 +581,25 @@ Property::Index Object::RegisterProperty( const std::string& name, Property::Ind return RegisterProperty( name, key, propertyValue, Property::ANIMATABLE ); } +void Object::SetProperties( const Property::Map& properties ) +{ + const auto count = properties.Count(); + for( auto position = 0u; position < count; ++position ) + { + // GetKeyAt and GetValue both return references which means no potential copying of maps/arrays. + // Iterating twice to get the value we want should still be fairly quick in a Property::Map. + + const auto& key = properties.GetKeyAt( position ); + const auto propertyIndex = ( key.type == Property::Key::INDEX ) ? key.indexKey : GetPropertyIndex( key.stringKey ); + + if( propertyIndex != Property::INVALID_INDEX ) + { + const auto& value = properties.GetValue( position ); + SetProperty( propertyIndex, value ); + } + } +} + Property::Index Object::RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode ) { return RegisterProperty( name, Property::INVALID_KEY, propertyValue, accessMode ); diff --git a/dali/internal/event/common/object-impl.h b/dali/internal/event/common/object-impl.h index 95033e7..d66eb9e 100644 --- a/dali/internal/event/common/object-impl.h +++ b/dali/internal/event/common/object-impl.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -214,6 +215,11 @@ public: Property::Index RegisterProperty( const std::string& name, Property::Index key, const Property::Value& propertyValue ); /** + * @copydoc Dali::DevelHandle::SetProperties() + */ + void SetProperties( const Property::Map& properties ); + + /** * @copydoc Dali::Handle::RegisterProperty(std::string name, Property::Value propertyValue, Property::AccessMode accessMode) */ Property::Index RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode ); -- 2.7.4