Conversion to Apache 2.0 license
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-processor.h
1 #ifndef __DALI_INTERNAL_GESTURE_PROCESSOR_H__
2 #define __DALI_INTERNAL_GESTURE_PROCESSOR_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/events/gesture-detector-impl.h>
23 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
24 #include <dali/internal/event/common/proxy-object.h>
25
26 namespace Dali
27 {
28
29 class Actor;
30
31 namespace Internal
32 {
33
34 /**
35  * Base class for the different Gesture Processors.
36  */
37 class GestureProcessor : public ProxyObject::Observer
38 {
39 protected: // Construction & Destruction
40
41   /**
42    * Protected constructor.  Cannot create an instance of GestureProcessor
43    */
44   GestureProcessor( Gesture::Type type );
45
46   /**
47    * Virtual protected destructor.
48    */
49   virtual ~GestureProcessor();
50
51   // Templates and types for deriving classes
52
53   /**
54    * Given a container of derived pointer types, this populates an equivalent container of base pointer types.
55    * @param[in]   derivedContainer  A const reference to the container with pointers to the derived class.
56    * @param[out]  baseContainer     A reference to the container to populate with equivalent pointers to the base class.
57    * @pre Ensure the baseContainer is empty.
58    */
59   template< typename Detector >
60   static void UpCastContainer( const typename DerivedGestureDetectorContainer<Detector>::type& derivedContainer, GestureDetectorContainer& baseContainer )
61   {
62     baseContainer.assign( derivedContainer.begin(), derivedContainer.end() );
63   }
64
65   /**
66    * Given a container of base pointer types, this populates an equivalent container of derived pointer types.
67    * @param[in]   baseContainer     A const reference to the container with pointers to the base class.
68    * @param[out]  derivedContainer  A reference to the container to populate with equivalent pointers to the derived class.
69    * @pre Ensure the derivedContainer is empty.
70    */
71   template< typename Detector >
72   static void DownCastContainer( const GestureDetectorContainer& baseContainer, typename DerivedGestureDetectorContainer<Detector>::type& derivedContainer )
73   {
74     for ( GestureDetectorContainer::const_iterator iter = baseContainer.begin(), endIter = baseContainer.end(); iter != endIter; ++iter )
75     {
76       derivedContainer.push_back( static_cast< Detector* >( *iter ) );
77     }
78   }
79
80   /**
81    * Functor to use in GetGesturedActor() and ProcessAndEmit() methods.
82    */
83   struct Functor
84   {
85     /**
86      * This operator should be overridden to check if the gesture detector meets the parameters of the current gesture.
87      * @param[in]  detector  The gesture detector to check.
88      * @param[in]  actor     The actor that has been gestured.
89      * @return true, if it meets the parameters, false otherwise.
90      */
91     virtual bool operator() ( GestureDetector* detector, Actor* actor ) = 0;
92
93     /**
94      * This operator should be overridden to emit the gesture signal on the provided container of gesture detectors along with the actor
95      * the gesture has occurred on.
96      * @param[in]  actor             The actor which has been gestured.
97      * @param[in]  gestureDetectors  The detectors that should emit the signal.
98      * @param[in]  actorCoordinates  The local actor coordinates where the gesture took place.
99      */
100     virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates ) = 0;
101   };
102
103   // Methods for deriving classes
104
105   /**
106    * Given the hit actor and attached detectors, this walks up the actor tree to determine the actor that is connected to one (or several) gesture
107    * detectors. The functor is used to check whether the gesture falls within the gesture detector's parameters.
108    * Derived classes need to provide this functor.
109    *
110    * @param[in,out]  actor               The hit actor. When this function returns, this is the actor that has been hit by the gesture.
111    * @param[in]      connectedDetectors  Reference to the detectors connected to the derived processor
112    * @param[out]     gestureDetectors    A container containing all the gesture detectors that have the hit actor attached and satisfy the functor parameters.
113    * @param[in]      functor             A reference to Functor.  The operator() (GestureDetector*) should be used to check if the connected gesture detector
114    *                                     meets the current gesture's parameters.
115    */
116   void GetGesturedActor( Dali::Actor& actor, const GestureDetectorContainer& connectedDetectors, GestureDetectorContainer& gestureDetectors, Functor& functor );
117
118   /**
119    * This does what GetGesturedActor() does but also starts emission of the gesture (using the functor).
120    *
121    * @param[in]  hitTestResults      The Hit Test Results.
122    * @param[in]  connectedDetectors  The detectors attached to the gesture processor.
123    * @param[in]  functor             A reference to the functor which should provide an operator() to check if detector satisfies current gesture and another
124    *                                 operator() which will be called when all conditions are met and the gesture should be emitted for the actor and detectors.
125    *
126    * @pre Hit Testing should already be done.
127    */
128   void ProcessAndEmit( const HitTestAlgorithm::Results& hitTestResults, const GestureDetectorContainer& connectedDetectors, Functor& functor );
129
130   /**
131    * Hit test the screen coordinates, and place the results in hitTestResults.
132    * @param[in] stage Stage.
133    * @param[in] screenCoordinates The screen coordinates to test.
134    * @param[out] hitTestResults Structure to write results into.
135    * @return false if the system overlay was hit or no actor was hit.
136    */
137   virtual bool HitTest(Stage& stage, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults);
138
139   /**
140    * Sets the mCurrentGesturedActor and connects to the required signals.
141    * @actor  actor  The actor so set.
142    */
143   void SetActor( Dali::Actor actor );
144
145   /**
146    * Resets the set actor and disconnects any connected signals.
147    */
148   void ResetActor();
149
150   /**
151    * Returns the current gestured actor if it is on stage
152    *
153    * @return The current gestured actor
154    */
155   Actor* GetCurrentGesturedActor();
156
157   // For derived classes to override
158
159   /**
160    * Called when the gestured actor is removed from the stage.
161    */
162   virtual void OnGesturedActorStageDisconnection() = 0;
163
164 private:
165
166   // Undefined
167   GestureProcessor( const GestureProcessor& );
168   GestureProcessor& operator=( const GestureProcessor& );
169
170 private:
171
172   /**
173    * This will never get called as we do not observe objects that have not been added to the scene.
174    * @param[in] proxy The proxy object.
175    * @see ProxyObject::Observer::SceneObjectAdded()
176    */
177   virtual void SceneObjectAdded(ProxyObject& proxy) { }
178
179   /**
180    * This will be called when the actor is removed from the stage, we should clear and stop
181    * observing it.
182    * @param[in] proxy The proxy object.
183    * @see ProxyObject::Observer::SceneObjectRemoved()
184    */
185   virtual void SceneObjectRemoved(ProxyObject& proxy);
186
187   /**
188    * This will be called when the actor is destroyed. We should clear the actor.
189    * No need to stop observing as the object is being destroyed anyway.
190    * @see ProxyObject::Observer::ProxyDestroyed()
191    */
192   virtual void ProxyDestroyed(ProxyObject& proxy);
193
194   // Signal Handlers
195
196   /**
197    * Signal handler called when the actor is removed from the stage.
198    * @param[in]  actor  The actor removed from the stage.
199    */
200   void OnStageDisconnection( Dali::Actor actor );
201
202 private: // Data
203
204   Gesture::Type mType;                 ///< Type of GestureProcessor
205   Actor* mCurrentGesturedActor;        ///< The current actor that has been gestured.
206   bool   mGesturedActorDisconnected:1; ///< Indicates whether the gestured actor has been disconnected from the scene
207 };
208
209 } // namespace Internal
210
211 } // namespace Dali
212
213 #endif // __DALI_INTERNAL_GESTURE_PROCESSOR_H__