From 6b6c1a325a1a66f17225e712f3b94054a771e7c8 Mon Sep 17 00:00:00 2001 From: Adeel Kazmi Date: Thu, 26 Jan 2017 18:35:55 +0000 Subject: [PATCH] Added ability to have strings over multiple lines in JSON using a Property Array Change-Id: If5734cc7fc66163febd837c87bc362e4f0b214b7 --- .../src/dali-toolkit-internal/CMakeLists.txt | 1 + .../utc-Dali-PropertyHelper.cpp | 103 ++++++++++++++++++ dali-toolkit/internal/file.list | 1 + .../internal/helpers/property-helper.cpp | 72 ++++++++++++ .../internal/helpers/property-helper.h | 52 +++++++++ .../visuals/visual-base-data-impl.cpp | 7 +- .../public-api/visuals/visual-properties.h | 8 +- .../visuals.md | 14 +-- 8 files changed, 245 insertions(+), 13 deletions(-) create mode 100644 automated-tests/src/dali-toolkit-internal/utc-Dali-PropertyHelper.cpp create mode 100644 dali-toolkit/internal/helpers/property-helper.cpp create mode 100644 dali-toolkit/internal/helpers/property-helper.h diff --git a/automated-tests/src/dali-toolkit-internal/CMakeLists.txt b/automated-tests/src/dali-toolkit-internal/CMakeLists.txt index 4e3e0ffe96..f07653b74b 100755 --- a/automated-tests/src/dali-toolkit-internal/CMakeLists.txt +++ b/automated-tests/src/dali-toolkit-internal/CMakeLists.txt @@ -23,6 +23,7 @@ SET(TC_SOURCES utc-Dali-Text-ViewModel.cpp utc-Dali-DebugRendering.cpp utc-Dali-ItemView-internal.cpp + utc-Dali-PropertyHelper.cpp ) # Append list of test harness files (Won't get parsed for test cases) diff --git a/automated-tests/src/dali-toolkit-internal/utc-Dali-PropertyHelper.cpp b/automated-tests/src/dali-toolkit-internal/utc-Dali-PropertyHelper.cpp new file mode 100644 index 0000000000..58fc510ece --- /dev/null +++ b/automated-tests/src/dali-toolkit-internal/utc-Dali-PropertyHelper.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +using namespace Dali; +using namespace Dali::Toolkit; + +void dali_property_helper_startup(void) +{ + test_return_value = TET_UNDEF; +} + +void dali_property_helper_cleanup(void) +{ + test_return_value = TET_PASS; +} + +int UtcDaliPropertyHelperGetStringFromPropertyWithString(void) +{ + tet_infoline( "Test to check if a simple string is parsed correctly" ); + + const std::string inputString = "Hello World"; + Property::Value value( inputString ); + + std::string output; + DALI_TEST_CHECK( Toolkit::Internal::GetStringFromProperty( value, output ) ); + DALI_TEST_EQUALS( output, inputString, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliPropertyHelperGetStringFromPropertyWithEmptyValue(void) +{ + tet_infoline( "Test to ensure if an empty value returns false" ); + + std::string output; + DALI_TEST_CHECK( ! Toolkit::Internal::GetStringFromProperty( Property::Value(), output ) ); + + END_TEST; +} + +int UtcDaliPropertyHelperGetStringFromPropertyWithStringArray(void) +{ + tet_infoline( "Test to check if a string array is parsed correctly and adds new line characters too" ); + + Property::Value value( Property::Array().Add( "Hello World" ) + .Add( "The Quick Brown Fox" ) + .Add( "Jumps over the lazy dog" ) ); + + std::string output; + DALI_TEST_CHECK( Toolkit::Internal::GetStringFromProperty( value, output ) ); + DALI_TEST_CHECK( output.find( "Hello World\n" ) != std::string::npos ); + DALI_TEST_CHECK( output.find( "The Quick Brown Fox\n" ) != std::string::npos ); + DALI_TEST_CHECK( output.find( "Jumps over the lazy dog\n" ) != std::string::npos ); + + END_TEST; +} + +int UtcDaliPropertyHelperGetStringFromPropertyWithEmptyArray(void) +{ + tet_infoline( "Test to check if an empty array returns false" ); + + Property::Array array; + + std::string output; + DALI_TEST_CHECK( ! Toolkit::Internal::GetStringFromProperty( Property::Value( array ), output ) ); + + END_TEST; +} + +int UtcDaliPropertyHelperGetStringFromPropertyWithMultipleTypesInArray(void) +{ + tet_infoline( "Test to ensure an array with multiple types returns false" ); + + Property::Value value( Property::Array().Add( "Hello World" ) + .Add( "The Quick Brown Fox" ) + .Add( 1 ) + .Add( "Jumps" ) + .Add( 25 ) + .Add( "Over" ) ); + + std::string output; + DALI_TEST_CHECK( ! Toolkit::Internal::GetStringFromProperty( value, output ) ); + DALI_TEST_CHECK( output.empty() ); + + END_TEST; +} diff --git a/dali-toolkit/internal/file.list b/dali-toolkit/internal/file.list index 27b079a2db..84f321422a 100755 --- a/dali-toolkit/internal/file.list +++ b/dali-toolkit/internal/file.list @@ -88,6 +88,7 @@ toolkit_src_files = \ \ $(toolkit_src_dir)/focus-manager/keyboard-focus-manager-impl.cpp \ $(toolkit_src_dir)/focus-manager/keyinput-focus-manager-impl.cpp \ + $(toolkit_src_dir)/helpers/property-helper.cpp \ $(toolkit_src_dir)/filters/blur-two-pass-filter.cpp \ $(toolkit_src_dir)/filters/emboss-filter.cpp \ $(toolkit_src_dir)/filters/image-filter.cpp \ diff --git a/dali-toolkit/internal/helpers/property-helper.cpp b/dali-toolkit/internal/helpers/property-helper.cpp new file mode 100644 index 0000000000..94be0237c0 --- /dev/null +++ b/dali-toolkit/internal/helpers/property-helper.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// HEADER +#include + +// EXTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace Toolkit +{ + +namespace Internal +{ + +bool GetStringFromProperty( const Property::Value& value, std::string& output ) +{ + bool extracted = false; + if( value.Get( output ) ) + { + extracted = true; + } + else + { + Property::Array* array = value.GetArray(); + if( array ) + { + const unsigned int arraySize = array->Size(); + for( unsigned int i = 0; i < arraySize; ++i ) + { + std::string element; + if( array->GetElementAt( i ).Get( element ) ) + { + extracted = true; + output += element + '\n'; + } + else + { + // If property in array is anything other than a string, then it is invalid so break and clear output. + output.clear(); + extracted = false; + break; + } + } + } + } + + return extracted; +} + +} // namespace Internal + +} // namespace Toolkit + +} // namespace Dali diff --git a/dali-toolkit/internal/helpers/property-helper.h b/dali-toolkit/internal/helpers/property-helper.h new file mode 100644 index 0000000000..e91062d5a2 --- /dev/null +++ b/dali-toolkit/internal/helpers/property-helper.h @@ -0,0 +1,52 @@ +#ifndef DALI_TOOLKIT_INTERNAL_PROPERTY_HELPER_H +#define DALI_TOOLKIT_INTERNAL_PROPERTY_HELPER_H + +/* + * Copyright (c) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +namespace Toolkit +{ + +namespace Internal +{ + +/** + * @brief Parses a Property::Value to retrieve the string. + * + * If value is a Property::STRING, then it simply extracts the required string. + * If value is a Property::ARRAY, then it combines all the strings it contains into one adding a newline character to each line. + * The second option allows users to write long strings over several lines in a JSON file. + * + * @return True if a string was extracted successfully. + */ +bool GetStringFromProperty( const Property::Value& value, std::string& output ); + +} // namespace Internal + +} // namespace Toolkit + +} // namespace Dali + + +#endif // DALI_TOOLKIT_INTERNAL_PROPERTY_HELPER_H diff --git a/dali-toolkit/internal/visuals/visual-base-data-impl.cpp b/dali-toolkit/internal/visuals/visual-base-data-impl.cpp index 16044b7c95..8990c9a02a 100644 --- a/dali-toolkit/internal/visuals/visual-base-data-impl.cpp +++ b/dali-toolkit/internal/visuals/visual-base-data-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2017 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. @@ -26,6 +26,7 @@ // INTERNAL INCLUDES #include +#include #include namespace Dali @@ -116,7 +117,7 @@ void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property: Property::Value* vertexShaderValue = shaderMap.Find( Toolkit::Visual::Shader::Property::VERTEX_SHADER, CUSTOM_VERTEX_SHADER ); if( vertexShaderValue ) { - if( !vertexShaderValue->Get( mVertexShader ) ) + if( ! GetStringFromProperty( *vertexShaderValue, mVertexShader ) ) { DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string\n", CUSTOM_VERTEX_SHADER ); } @@ -125,7 +126,7 @@ void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property: Property::Value* fragmentShaderValue = shaderMap.Find( Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, CUSTOM_FRAGMENT_SHADER ); if( fragmentShaderValue ) { - if( !fragmentShaderValue->Get( mFragmentShader ) ) + if( ! GetStringFromProperty( *fragmentShaderValue, mFragmentShader ) ) { DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string\n", CUSTOM_FRAGMENT_SHADER ); } diff --git a/dali-toolkit/public-api/visuals/visual-properties.h b/dali-toolkit/public-api/visuals/visual-properties.h index e5f7431b33..65d452af05 100644 --- a/dali-toolkit/public-api/visuals/visual-properties.h +++ b/dali-toolkit/public-api/visuals/visual-properties.h @@ -2,7 +2,7 @@ #define DALI_TOOLKIT_VISUAL_PROPERTIES_H /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2017 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. @@ -87,7 +87,8 @@ enum { /** * @brief The vertex shader. - * @details Name "vertexShader", type Property::STRING. + * @details Name "vertexShader", type Property::STRING or Property::ARRAY of Property::STRING. + * A Property::ARRAY of Property::STRING values can be used to split the shader string over multiple lines. * @SINCE_1_1.45 * @note Optional * @note If not supplied, the visual's already set vertex shader is used. @@ -96,7 +97,8 @@ enum /** * @brief The fragment shader. - * @details Name "fragmentShader", type Property::STRING. + * @details Name "fragmentShader", type Property::STRING or Property::ARRAY of Property::STRING. + * A Property::ARRAY of Property::STRING values can be used to split the shader string over multiple lines. * @SINCE_1_1.45 * @note Optional * @note If not supplied, the visual's already set fragment shader is used. diff --git a/docs/content/shared-javascript-and-cpp-documentation/visuals.md b/docs/content/shared-javascript-and-cpp-documentation/visuals.md index 3db0bf484c..71c45ef431 100644 --- a/docs/content/shared-javascript-and-cpp-documentation/visuals.md +++ b/docs/content/shared-javascript-and-cpp-documentation/visuals.md @@ -60,13 +60,13 @@ For example, an offsetSizeMode of [0, 0, 1, 1], an offset of (0, 0.25) and a siz Visuals also have a custom **shader** property. Whilst it's possible to change the shader, please note that some visuals rely on the vertex shader to perform certain functions. For example, the NPatch visual uses the vertex shader to perform the stretching. The **shader** property is a Property::Map with the following keys: -| Property | String | Type | Required | Description | -|-------------------------------------------------|----------|:-------:|:--------:|---------------------------| -| Dali::Toolkit::Visual::Shader::Property::VERTEX_SHADER | vertexShader | STRING | No | The vertex shader code. | -| Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER | fragmentShader | STRING | No | The fragment shader code. | -| Dali::Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_X | subdivideGridX | INTEGER | No | How to subdivide the grid along the X-Axis. Defaults to 1 | -| Dali::Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_Y | subdivideGridY | INTEGER | No | How to subdivide the grid along the Y-Axis. Defaults to 1 | -| Dali::Toolkit::Visual::Shader::Property::HINTS | hints | INTEGER or ARRAY of STRING | No | Shader hints bitmask [More info](@ref shader-hints) | +| Property | String | Type | Required | Description | +|-----------------------------------------------------------|----------------|:--------------------------:|:--------:|--------------------------------------------------------------------------------------------| +| Dali::Toolkit::Visual::Shader::Property::VERTEX_SHADER | vertexShader | STRING or ARRAY of STRING | No | The vertex shader code. Can use an array of strings to split shader over multiple lines. | +| Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER | fragmentShader | STRING or ARRAY of STRING | No | The fragment shader code. Can use an array of strings to split shader over multiple lines. | +| Dali::Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_X | subdivideGridX | INTEGER | No | How to subdivide the grid along the X-Axis. Defaults to 1. | +| Dali::Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_Y | subdivideGridY | INTEGER | No | How to subdivide the grid along the Y-Axis. Defaults to 1. | +| Dali::Toolkit::Visual::Shader::Property::HINTS | hints | INTEGER or ARRAY of STRING | No | Shader hints bitmask [More info](@ref shader-hints) | ## Shader hints {#shader-hints} -- 2.34.1