Added ability to have strings over multiple lines in JSON using a Property Array 71/112171/6
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Thu, 26 Jan 2017 18:35:55 +0000 (18:35 +0000)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 7 Feb 2017 10:20:47 +0000 (02:20 -0800)
Change-Id: If5734cc7fc66163febd837c87bc362e4f0b214b7

automated-tests/src/dali-toolkit-internal/CMakeLists.txt
automated-tests/src/dali-toolkit-internal/utc-Dali-PropertyHelper.cpp [new file with mode: 0644]
dali-toolkit/internal/file.list
dali-toolkit/internal/helpers/property-helper.cpp [new file with mode: 0644]
dali-toolkit/internal/helpers/property-helper.h [new file with mode: 0644]
dali-toolkit/internal/visuals/visual-base-data-impl.cpp
dali-toolkit/public-api/visuals/visual-properties.h
docs/content/shared-javascript-and-cpp-documentation/visuals.md

index 4e3e0ff..f07653b 100755 (executable)
@@ -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 (file)
index 0000000..58fc510
--- /dev/null
@@ -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 <dali-toolkit-test-suite-utils.h>
+#include <dali-toolkit/internal/helpers/property-helper.h>
+
+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;
+}
index 27b079a..84f3214 100755 (executable)
@@ -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 (file)
index 0000000..94be023
--- /dev/null
@@ -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 <dali-toolkit/internal/helpers/property-helper.h>
+
+// EXTERNAL INCLUDES
+#include <dali/public-api/object/property-array.h>
+
+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 (file)
index 0000000..e91062d
--- /dev/null
@@ -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 <string>
+#include <dali/public-api/object/property.h>
+
+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
index 16044b7..8990c9a 100644 (file)
@@ -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 <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
+#include <dali-toolkit/internal/helpers/property-helper.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
 
 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 );
     }
index e5f7431..65d452a 100644 (file)
@@ -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.
index 3db0bf4..71c45ef 100644 (file)
@@ -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}