Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / events / long-press-gesture-processor.cpp
index 1a83a67..d2d2e2e 100644 (file)
@@ -1,18 +1,19 @@
-//
-// 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.
-//
+/*
+ * Copyright (c) 2014 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.
+ *
+ */
 
 // CLASS HEADER
 #include "long-press-gesture-processor.h"
@@ -48,8 +49,8 @@ namespace
  * @param[in]  localPoint        Relative to the actor attached to the detector.
  */
 void EmitLongPressSignal(
-    Dali::Actor actor,
-    LongPressGestureDetectorContainer& gestureDetectors,
+    Actor* actor,
+    const GestureDetectorContainer& gestureDetectors,
     const Integration::LongPressGestureEvent& longPressEvent,
     Vector2 localPoint)
 {
@@ -59,9 +60,11 @@ void EmitLongPressSignal(
   longPress.screenPoint = longPressEvent.point;
   longPress.localPoint = localPoint;
 
-  for ( LongPressGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
+  Dali::Actor actorHandle( actor );
+  const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
+  for ( GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter )
   {
-    (*iter)->EmitLongPressGestureSignal(actor, longPress);
+    static_cast< LongPressGestureDetector* >( *iter )->EmitLongPressGestureSignal( actorHandle, longPress );
   }
 }
 
@@ -85,7 +88,7 @@ struct IsNotAttachedFunctor
    * @param[in]  detector  The detector to check.
    * @return true, if not attached, false otherwise.
    */
-  bool operator()( const LongPressGestureDetector* detector ) const
+  bool operator()( const GestureDetector* detector ) const
   {
     return !detector->IsAttached( *actorToCheck );
   }
@@ -95,65 +98,16 @@ struct IsNotAttachedFunctor
 
 } // unnamed namespace
 
-struct LongPressGestureProcessor::LongPressEventFunctor : public GestureProcessor::Functor
-{
-  /**
-   * Constructor
-   * @param[in]  event      The current gesture event.
-   * @param[in]  processor  Reference to the processor.
-   */
-  LongPressEventFunctor( const Integration::LongPressGestureEvent& event, LongPressGestureProcessor& processor )
-  : longPressEvent( event ),
-    processor( processor )
-  {
-  }
-
-  /**
-   * Check if the detector meets the current gesture event parameters.
-   */
-  virtual bool operator() ( GestureDetector* detector, Actor* )
-  {
-    LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
-
-    return ( longPressDetector->GetMinimumTouchesRequired() <= longPressEvent.numberOfTouches ) &&
-           ( longPressDetector->GetMaximumTouchesRequired() >= longPressEvent.numberOfTouches );
-  }
-
-  /**
-   * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
-   */
-  virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
-  {
-    LongPressGestureDetectorContainer derivedContainer;
-    DownCastContainer<LongPressGestureDetector>( gestureDetectors, derivedContainer );
-
-    // Clear actor as
-    processor.mCurrentEmitters.clear();
-    processor.ResetActor();
-
-    EmitLongPressSignal( actor, derivedContainer, longPressEvent, actorCoordinates );
-
-    if ( actor.OnStage() )
-    {
-      processor.mCurrentEmitters = derivedContainer;
-      processor.SetActor( actor );
-    }
-  }
-
-  const Integration::LongPressGestureEvent& longPressEvent;
-  LongPressGestureProcessor& processor;
-};
-
-LongPressGestureProcessor::LongPressGestureProcessor(
-    Stage& stage,
-    Integration::GestureManager& gestureManager)
-: mStage( stage ),
+LongPressGestureProcessor::LongPressGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager)
+: GestureProcessor( Gesture::LongPress ),
+  mStage( stage ),
   mGestureManager( gestureManager ),
   mGestureDetectors(),
   mCurrentEmitters(),
   mCurrentRenderTask(),
   mMinTouchesRequired( 1 ),
-  mMaxTouchesRequired( 1 )
+  mMaxTouchesRequired( 1 ),
+  mCurrentLongPressEvent( NULL )
 {
 }
 
@@ -173,7 +127,7 @@ void LongPressGestureProcessor::Process( const Integration::LongPressGestureEven
       HitTestAlgorithm::Results hitTestResults;
       if( HitTest( mStage, longPressEvent.point, hitTestResults ) )
       {
-        SetActor( hitTestResults.actor );
+        SetActor( &GetImplementation( hitTestResults.actor ) );
       }
       break;
     }
@@ -184,17 +138,17 @@ void LongPressGestureProcessor::Process( const Integration::LongPressGestureEven
       if ( currentGesturedActor )
       {
         HitTestAlgorithm::Results hitTestResults;
-        HitTestAlgorithm::HitTest( mStage, longPressEvent.point, hitTestResults );
+        HitTest( mStage, longPressEvent.point, hitTestResults );
 
         if ( hitTestResults.actor && ( currentGesturedActor == &GetImplementation( hitTestResults.actor ) ) )
         {
           // Record the current render-task for Screen->Actor coordinate conversions
           mCurrentRenderTask = hitTestResults.renderTask;
 
-          LongPressEventFunctor functor( longPressEvent, *this );
-          GestureDetectorContainer gestureDetectors;
-          UpCastContainer<LongPressGestureDetector>( mGestureDetectors, gestureDetectors );
-          ProcessAndEmit( hitTestResults, gestureDetectors, functor );
+          // Set mCurrentLongPressEvent to use inside overridden methods called from ProcessAndEmit()
+          mCurrentLongPressEvent = &longPressEvent;
+          ProcessAndEmit( hitTestResults );
+          mCurrentLongPressEvent = NULL;
         }
         else
         {
@@ -219,7 +173,7 @@ void LongPressGestureProcessor::Process( const Integration::LongPressGestureEven
         if ( currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask )
         {
           // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
-          LongPressGestureDetectorContainer::iterator endIter = std::remove_if( mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
+          GestureDetectorContainer::iterator endIter = std::remove_if( mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
           mCurrentEmitters.erase( endIter, mCurrentEmitters.end() );
 
           if ( !mCurrentEmitters.empty() )
@@ -228,7 +182,7 @@ void LongPressGestureProcessor::Process( const Integration::LongPressGestureEven
             RenderTask& renderTaskImpl( GetImplementation( mCurrentRenderTask ) );
             currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y );
 
-            EmitLongPressSignal( Dali::Actor( currentGesturedActor ), mCurrentEmitters, longPressEvent, actorCoords );
+            EmitLongPressSignal( currentGesturedActor, mCurrentEmitters, longPressEvent, actorCoords );
           }
         }
 
@@ -247,12 +201,15 @@ void LongPressGestureProcessor::Process( const Integration::LongPressGestureEven
     }
 
     case Gesture::Continuing:
-      DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Continuing\n" );
+    {
+      DALI_ABORT( "Incorrect state received from Integration layer: Continuing\n" );
       break;
-
+    }
     case Gesture::Clear:
-      DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
+    {
+      DALI_ABORT( "Incorrect state received from Integration layer: Clear\n" );
       break;
+    }
   }
 }
 
@@ -316,16 +273,19 @@ void LongPressGestureProcessor::UpdateDetection()
   {
     LongPressGestureDetector* current(*iter);
 
-    unsigned int minimum = current->GetMinimumTouchesRequired();
-    if (minimum < minimumRequired)
+    if( current )
     {
-      minimumRequired = minimum;
-    }
+      unsigned int minimum = current->GetMinimumTouchesRequired();
+      if (minimum < minimumRequired)
+      {
+        minimumRequired = minimum;
+      }
 
-    unsigned int maximum = current->GetMaximumTouchesRequired();
-    if ( maximum > maximumRequired )
-    {
-      maximumRequired = maximum;
+      unsigned int maximum = current->GetMaximumTouchesRequired();
+      if ( maximum > maximumRequired )
+      {
+        maximumRequired = maximum;
+      }
     }
   }
 
@@ -346,6 +306,32 @@ void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
   mCurrentEmitters.clear();
 }
 
+bool LongPressGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
+{
+  DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
+
+  LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
+
+  return ( longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches ) &&
+         ( longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches );
+}
+
+void LongPressGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
+{
+  DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
+
+  mCurrentEmitters.clear();
+  ResetActor();
+
+  EmitLongPressSignal( actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates );
+
+  if ( actor->OnStage() )
+  {
+    mCurrentEmitters = gestureDetectors;
+    SetActor( actor );
+  }
+}
+
 } // namespace Internal
 
 } // namespace Dali