47e5dabeaba5b739a383727b9d791bc80ea933df
[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) 2020 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/events/gesture-recognizer.h>
25 #include <dali/internal/event/common/object-impl.h>
26
27 namespace Dali
28 {
29
30 class Actor;
31
32 namespace Internal
33 {
34
35 /**
36  * Base class for the different Gesture Processors.
37  */
38 class GestureProcessor : public Object::Observer
39 {
40 public:
41
42   /**
43    * Process the touch event in the attached recognizer
44    * @param[in] scene Scene.
45    * @param[in] event Touch event to process
46    */
47   void ProcessTouch( Scene& scene, const Integration::TouchEvent& event );
48
49   /**
50    * Returns whether any GestureDetector requires a Core::Update
51    * @return true if update required
52    */
53   inline bool NeedsUpdate()
54   {
55     bool updateRequired = mNeedsUpdate;
56     mNeedsUpdate = false;
57     return updateRequired;
58   }
59
60 protected:
61
62   // Construction & Destruction
63
64   /**
65    * Protected constructor.  Cannot create an instance of GestureProcessor
66    */
67   GestureProcessor( Gesture::Type type );
68
69   /**
70    * Virtual protected destructor.
71    */
72   virtual ~GestureProcessor();
73
74   // Methods to be used by deriving classes
75
76   /**
77    * Given the hit actor, this walks up the actor tree to determine the actor that is connected to one (or several) gesture detectors.
78    *
79    * @param[in,out]  actor               The gestured actor. When this function returns, this is the actor that has been hit by the gesture.
80    * @param[out]     gestureDetectors    A container containing all the gesture detectors that have the hit actor attached and satisfy the functor parameters.
81    *
82    * @note Uses CheckGestureDetector() to check if a the current gesture matches the criteria the gesture detector requires.
83    * @pre gestureDetectors should be empty.
84    */
85   void GetGesturedActor( Actor*& actor, GestureDetectorContainer& gestureDetectors );
86
87   /**
88    * Calls the emission method in the deriving class for matching gesture-detectors with the hit-actor (or one of its parents).
89    *
90    * @param[in]  hitTestResults      The Hit Test Results.
91    *
92    * @note Uses the CheckGestureDetector() to check if the gesture matches the criteria of the given gesture detector
93    *       and EmitGestureSignal() to emit the signal.
94    * @pre Hit Testing should already be done.
95    */
96   void ProcessAndEmit( HitTestAlgorithm::Results& hitTestResults );
97
98   /**
99    * Hit test the screen coordinates, and place the results in hitTestResults.
100    * @param[in] scene Scene.
101    * @param[in] screenCoordinates The screen coordinates to test.
102    * @param[out] hitTestResults Structure to write results into.
103    * @return false if the system overlay was hit or no actor was hit.
104    */
105   virtual bool HitTest( Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults);
106
107   /**
108    * Sets the mCurrentGesturedActor and connects to the required signals.
109    * @actor  actor  The actor so set.
110    */
111   void SetActor( Actor* actor );
112
113   /**
114    * Resets the set actor and disconnects any connected signals.
115    */
116   void ResetActor();
117
118   /**
119    * Returns the current gestured actor if it is on stage
120    *
121    * @return The current gestured actor
122    */
123   Actor* GetCurrentGesturedActor();
124
125 private:
126
127   // For derived classes to override
128
129   /**
130    * Called when the gestured actor is removed from the stage.
131    */
132   virtual void OnGesturedActorStageDisconnection() = 0;
133
134   /**
135    * Called by the ProcessAndEmit() & GetGesturedActor() methods to check if the provided
136    * gesture-detector meets the parameters of the current gesture.
137    *
138    * @param[in]  detector  The gesture detector to check.
139    * @param[in]  actor     The actor that has been gestured.
140    *
141    * @return true, if the detector meets the parameters, false otherwise.
142    */
143   virtual bool CheckGestureDetector( GestureDetector* detector, Actor* actor ) = 0;
144
145   /**
146    * Called by the ProcessAndEmit() method when the gesture meets all applicable criteria and
147    * should be overridden by deriving classes to emit the gesture signal on gesture-detectors
148    * provided for the actor the gesture has occurred on.
149    *
150    * @param[in]  actor             The actor which has been gestured.
151    * @param[in]  gestureDetectors  The detectors that should emit the signal.
152    * @param[in]  actorCoordinates  The local actor coordinates where the gesture took place.
153    */
154   virtual void EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates ) = 0;
155
156   // Undefined
157
158   GestureProcessor( const GestureProcessor& );
159   GestureProcessor& operator=( const GestureProcessor& );
160
161   // SceneObject overrides
162
163   /**
164    * This will never get called as we do not observe objects that have not been added to the scene.
165    * @param[in] object The object object.
166    * @see Object::Observer::SceneObjectAdded()
167    */
168   virtual void SceneObjectAdded(Object& object) { }
169
170   /**
171    * This will be called when the actor is removed from the stage, we should clear and stop
172    * observing it.
173    * @param[in] object The object object.
174    * @see Object::Observer::SceneObjectRemoved()
175    */
176   virtual void SceneObjectRemoved(Object& object);
177
178   /**
179    * This will be called when the actor is destroyed. We should clear the actor.
180    * No need to stop observing as the object is being destroyed anyway.
181    * @see Object::Observer::ObjectDestroyed()
182    */
183   virtual void ObjectDestroyed(Object& object);
184
185
186 protected:  //Data
187
188   GestureRecognizerPtr mGestureRecognizer;  ///< The gesture recognizer
189   bool   mNeedsUpdate;                 ///< Indicates if any GestureDetector requires a Core::Update
190
191 private: // Data
192
193   Gesture::Type mType;                 ///< Type of GestureProcessor
194   Actor* mCurrentGesturedActor;        ///< The current actor that has been gestured.
195   bool   mGesturedActorDisconnected:1; ///< Indicates whether the gestured actor has been disconnected from the scene
196 };
197
198 } // namespace Internal
199
200 } // namespace Dali
201
202 #endif // DALI_INTERNAL_GESTURE_PROCESSOR_H