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