BouncingEffect for overscroll
authorXiangyin Ma <x1.ma@samsung.com>
Wed, 7 May 2014 13:05:41 +0000 (14:05 +0100)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Thu, 15 May 2014 11:53:23 +0000 (12:53 +0100)
[Issue] N/A
[Problem] new feature
[Cause] N/A
[Solution] the overscroll effect implemented with shader

test codes:
    ImageActor imageActor = ImageActor::New( BitmapImage::New( 1, 1 ) );
    imageActor.SetSize(720.f,58.f);
    Toolkit::BouncingEffect bouncingEffect = Toolkit::BouncingEffect::New( Vector4(0.f,1.f,1.f,0.5f) );
    imageActor.SetShaderEffect( bouncingEffect );
    imageActor.SetParentOrigin( ParentOrigin::CENTER );
    Stage::GetCurrent().Add( imageActor );

    Animation animation = Animation::New(1.f);
    animation.AnimateTo( Property( bouncingEffect, bouncingEffect.GetProgressRatePropertyName() ), 1.f,
                         AlphaFunctions::Bounce );
    animation.Play();

Signed-off-by: Xiangyin Ma <x1.ma@samsung.com>
base/dali-toolkit/public-api/file.list
base/dali-toolkit/public-api/shader-effects/bouncing-effect.cpp [new file with mode: 0644]
base/dali-toolkit/public-api/shader-effects/bouncing-effect.h [new file with mode: 0644]
build/slp/dali-toolkit/Makefile.am
optional/dali-toolkit/dali-toolkit.h

index 5dd1163..396557f 100755 (executable)
@@ -47,7 +47,8 @@ public_api_base_src_files = \
   $(public_api_base_src_dir)/focus-manager/keyboard-focus-manager.cpp \
   $(public_api_base_src_dir)/focus-manager/keyinput-focus-manager.cpp \
   $(public_api_base_src_dir)/markup-processor/markup-processor.cpp \
-  $(public_api_base_src_dir)/shader-effects/image-region-effect.cpp
+  $(public_api_base_src_dir)/shader-effects/image-region-effect.cpp \
+  $(public_api_base_src_dir)/shader-effects/bouncing-effect.cpp
 
 # Add public header files here
 
@@ -101,3 +102,6 @@ public_api_base_focus_manager_header_files = \
 
 public_api_base_markup_processor_header_files =
 
+public_api_base_shader_effects_header_files = \
+  $(public_api_base_src_dir)/shader-effects/bouncing-effect.h
+
diff --git a/base/dali-toolkit/public-api/shader-effects/bouncing-effect.cpp b/base/dali-toolkit/public-api/shader-effects/bouncing-effect.cpp
new file mode 100644 (file)
index 0000000..7698dda
--- /dev/null
@@ -0,0 +1,105 @@
+//
+// Copyright (c) 2014 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Flora License, Version 1.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://floralicense.org/license/
+//
+// 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/public-api/shader-effects/bouncing-effect.h>
+
+namespace Dali
+{
+
+namespace Toolkit
+{
+
+namespace
+{
+
+#define MAKE_STRING(A)#A
+
+const std::string PROGRESS_RATE_PROPERTY_NAME( "uProgressRate" );
+
+} // namespace
+
+BouncingEffect::BouncingEffect()
+{
+}
+
+BouncingEffect::BouncingEffect( ShaderEffect handle )
+:ShaderEffect( handle )
+{
+}
+
+BouncingEffect::~BouncingEffect()
+{
+}
+
+BouncingEffect BouncingEffect::New( const Vector4& color )
+{
+  std::string fragmentShader = MAKE_STRING(
+      uniform float uProgressRate;\n
+      uniform vec4 uAssignedColor;\n
+      void main()\n
+      {\n
+        float progressRate = abs(uProgressRate)*0.5;\n
+        float amplitude = 0.15 - progressRate*0.15 ;\n
+        float x1 = 7.5 * (vTexCoord.x - progressRate);\n
+        float x2 = 7.5 * (vTexCoord.x - 1.0 + progressRate);\n
+        float height1 = max(0.00001, 0.3 - amplitude * ( exp(x1) + exp(-x1) ) );\n
+        float height2 = max(0.00001, 0.3 - amplitude * ( exp(x2) + exp(-x2) ) );\n
+        float height3 = max(0.00001, 1.0 - 3.0 * amplitude * ( exp(x1*0.5) + exp(-x1*0.5) ) );\n
+        float height4 = max(0.00001, 1.0 - 3.0 * amplitude * ( exp(x2*0.5) + exp(-x2*0.5) ) );\n
+        vec4 fragColor = vec4(0.0);\n
+        float y = vTexCoord.y/(height1+height2);\n
+        float y2 = vTexCoord.y/max(height3,height4);\n
+        float coef = max(height1,height2)*5.0/( 1.0+exp(y*12.0-6.0) );\n
+        float alpha = pow( max(0.0,(1.0-y2))*(1.0-min(abs(x1),abs(x2))/5.0), 2.0);\n
+        if( vTexCoord.y < 0.075 )\n
+        {\n
+          fragColor= mix(uAssignedColor, vec4(1.0), coef);\n
+          fragColor += (vec4(1.0)-fragColor) * alpha;\n
+        }\n
+        else if (y2<1.0)\n
+        {\n
+          fragColor =vec4(1.0,1.0,1.0, alpha + (1.0-alpha)*coef);\n
+          fragColor.rgb -= ( vec3(1.0)-uAssignedColor.rgb )*min(clamp(y*1.2-0.3, 0.0, 0.3),clamp(0.9-y*1.2,0.0,0.3));\n
+        }\n
+        fragColor.a *= 10.0*min(min(vTexCoord.x, 1.0-vTexCoord.x),0.1)*min(1.0, progressRate/0.2);\n
+        gl_FragColor =  fragColor;\n
+      }
+  );
+
+  ShaderEffect shaderEffect;
+  shaderEffect = ShaderEffect::New( "", fragmentShader, GeometryType( GEOMETRY_TYPE_IMAGE),
+                                    ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
+  BouncingEffect handle( shaderEffect );
+
+  handle.SetUniform( "uAssignedColor", color );
+  handle.SetProgressRate( 0.f );
+
+  return handle;
+}
+
+void BouncingEffect::SetProgressRate( float progress )
+{
+  SetUniform( PROGRESS_RATE_PROPERTY_NAME, progress );
+}
+
+const std::string& BouncingEffect::GetProgressRatePropertyName() const
+{
+  return PROGRESS_RATE_PROPERTY_NAME;
+}
+
+} // namespace Toolkit
+
+} // namespace Dali
diff --git a/base/dali-toolkit/public-api/shader-effects/bouncing-effect.h b/base/dali-toolkit/public-api/shader-effects/bouncing-effect.h
new file mode 100644 (file)
index 0000000..3b63fdc
--- /dev/null
@@ -0,0 +1,96 @@
+#ifndef __DALI_TOOLKIT_SHADER_EFFECT_BOUNCING_H__
+#define __DALI_TOOLKIT_SHADER_EFFECT_BOUNCING_H__
+
+//
+// Copyright (c) 2014 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Flora License, Version 1.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://floralicense.org/license/
+//
+// 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.
+//
+
+// INTERNAL INCLUDES
+#include <dali/dali.h>
+
+namespace Dali DALI_IMPORT_API
+{
+
+namespace Toolkit
+{
+
+/**
+ * @brief BouncingEffect is a custom overscroll effect with two waves appearing at two sides then moving towards center and overlapping.
+ *
+ * Usage Example:
+ *
+ *  // Create the an imageActor, set shader effect, and add it to the stage
+ *  ImageActor imageActor = ImageActor::New( BitmapImage::New( 1, 1 ) );
+ *  imageActor.SetSize(720.f,58.f);
+ *  Toolkit::BouncingEffect bouncingEffect = Toolkit::BouncingEffect::New( Vector4(0.f,1.f,1.f,0.5f) );
+ *  imageActor.SetShaderEffect( bouncingEffect );
+ *  imageActor.SetParentOrigin( ParentOrigin::CENTER );
+ *  Stage::GetCurrent().Add( imageActor );
+ *
+ *   // Start the animation
+ *   Animation animation = Animation::New(1.f);
+ *   animation.AnimateTo( Property( bouncingEffect, bouncingEffect.GetProgressRatePropertyName() ),
+ *                        1.f, AlphaFunctions::Bounce );
+ *   animation.Play();
+ */
+class BouncingEffect : public ShaderEffect
+{
+public:
+
+  /**
+   * @brief Creates an empty BouncingEffect handle
+   */
+  BouncingEffect();
+
+  /**
+   * @brief Virtual destructor
+   */
+  virtual ~BouncingEffect();
+
+  /**
+   * @brief Create a BouncingEffect object
+   *
+   * @param[in] color The color used on the bouncing stripe
+   * @return A handle to a newly allocated Dali resource.
+   */
+  static BouncingEffect New( const Vector4& color );
+
+  /**
+   * @brief Set the progress rate to the effect.
+   *
+   * The whole progress ( with progress rate from 0.0 to 1.0 ):
+   *      two waves appear at two sides; move towards center and overlap.
+   * @param[in] progressRate The progress rate value.
+   */
+  void SetProgressRate( float progressRate );
+
+  /**
+   * @brief Get the name for the progress rate property.
+   *
+   * @return A std::string containing the property name.
+   */
+  const std::string& GetProgressRatePropertyName() const;
+
+
+private: // Not intended for application developers
+  BouncingEffect( ShaderEffect handle );
+
+};
+
+} // namespace Toolkit
+
+} // namespace Dali
+
+#endif // __DALI_TOOLKIT_SHADER_EFFECT_BOUNCING_H__
index 729ce52..1805f7e 100644 (file)
@@ -96,6 +96,7 @@ publicapibasetextinputdir = $(publicapibasedir)/controls/text-input
 publicapibasefactorydir = $(publicapibasedir)/factory
 publicapibasefocusmanagerdir = $(publicapibasedir)/focus-manager
 publicapibasemarkupprocessordir = $(publicapibasedir)/markup-processor
+publicapibaseshadereffectsdir = $(publicapibasedir)/shader-effects
 
 publicapibase_HEADERS = $(public_api_base_header_files)
 publicapibasecontrols_HEADERS = $(public_api_base_controls_header_files)
@@ -114,6 +115,7 @@ publicapibasetextinput_HEADERS = $(public_api_base_text_input_header_files)
 publicapibasefactory_HEADERS = $(public_api_base_factory_header_files)
 publicapibasefocusmanager_HEADERS = $(public_api_base_focus_manager_header_files)
 publicapibasemarkupprocessor_HEADERS = $(public_api_base_markup_processor_header_files)
+publicapibaseshadereffects_HEADERS = $(public_api_base_shader_effects_header_files)
 
 # Install Optional headers
 
index 7a7d089..ed080fe 100644 (file)
@@ -67,6 +67,7 @@
 #include <dali-toolkit/public-api/shader-effects/alpha-discard-effect.h>
 #include <dali-toolkit/public-api/shader-effects/bendy-effect.h>
 #include <dali-toolkit/public-api/shader-effects/blind-effect.h>
+#include <dali-toolkit/public-api/shader-effects/bouncing-effect.h>
 #include <dali-toolkit/public-api/shader-effects/carousel-effect.h>
 #include <dali-toolkit/public-api/shader-effects/displacement-effect.h>
 #include <dali-toolkit/public-api/shader-effects/dissolve-local-effect.h>