[dali_2.3.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-detector-impl.h
1 #ifndef DALI_INTERNAL_GESTURE_DETECTOR_H
2 #define DALI_INTERNAL_GESTURE_DETECTOR_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/actors/actor-impl.h>
23 #include <dali/internal/event/events/actor-observer.h>
24 #include <dali/internal/event/events/gesture-event.h>
25 #include <dali/internal/event/events/gesture-recognizer.h>
26 #include <dali/internal/event/render-tasks/render-task-impl.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/common/vector-wrapper.h>
29 #include <dali/public-api/events/gesture-detector.h>
30 #include <dali/public-api/events/gesture.h>
31 #include <dali/public-api/signals/slot-delegate.h>
32
33 namespace Dali
34 {
35 namespace Integration
36 {
37 struct GestureEvent;
38 }
39
40 namespace Internal
41 {
42 class GestureDetector;
43 class GestureEventProcessor;
44
45 using GestureDetectorPtr            = IntrusivePtr<GestureDetector>;
46 using GestureDetectorContainer      = std::vector<GestureDetector*>;
47 using GestureDetectorActorContainer = std::vector<Actor*>;
48 using GestureRecognizerPtr          = IntrusivePtr<GestureRecognizer>;
49
50 /**
51  * This is a type trait that should be used by deriving gesture detectors for their container type.
52  */
53 template<typename Detector>
54 struct DerivedGestureDetectorContainer
55 {
56   using type = std::vector<Detector*>;
57 };
58
59 /**
60  * @copydoc Dali::GestureDetector
61  */
62 class GestureDetector : public Object, public Object::Observer, public ConnectionTracker
63 {
64 public:
65   /**
66    * @copydoc Dali::GestureDetector::Attach()
67    */
68   void Attach(Actor& actor);
69
70   /**
71    * @copydoc Dali::GestureDetector::Detach()
72    */
73   void Detach(Actor& actor);
74
75   /**
76    * @copydoc Dali::GestureDetector::DetachAll()
77    */
78   void DetachAll();
79
80   /**
81    * @copydoc Dali::GestureDetector::GetAttachedActorCount() const
82    */
83   size_t GetAttachedActorCount() const;
84
85   /**
86    * @copydoc Dali::GestureDetector::GetAttachedActor() const
87    */
88   Dali::Actor GetAttachedActor(size_t index) const;
89
90   /**
91    * @copydoc Dali::GestureDetector::HandleEvent()
92    */
93   bool HandleEvent(Dali::Actor& actor, Dali::TouchEvent& touch);
94
95   /**
96    * Returns a const reference to the container of attached actor pointers.
97    * @return A const reference to the attached internal actors.
98    */
99   const GestureDetectorActorContainer& GetAttachedActorPointers() const
100   {
101     return mAttachedActors;
102   }
103
104   /**
105    * Retrieves the type of GestureDetector
106    * @return The GestureDetector Type
107    */
108   GestureType::Value GetType() const
109   {
110     return mType;
111   }
112
113   /**
114    * Checks if the specified actor is still attached or pending attachment.
115    * @param[in]  actor  The actor to check.
116    * @return true, if the actor is attached or pending, false otherwise.
117    */
118   bool IsAttached(Actor& actor) const;
119
120   /**
121    * @brief This method that cancels all other gesture detectors except the current gesture detector
122    */
123   void CancelAllOtherGestureDetectors();
124
125   /**
126    * @brief Returns whether the gesture was detected.
127    * @return true, if the gesture was detected, false otherwise.
128    */
129   bool IsDetected() const;
130
131   /**
132    * @brief Sets whether the gesture was detected.
133    * @param detected Whether gesture detected.
134    */
135   void SetDetected(bool detected);
136
137   /**
138    * gesture-detector meets the parameters of the current gesture.
139    *
140    * @param[in]  gestureEvent The gesture event.
141    * @param[in]  actor        The actor that has been gestured.
142    * @param[in]  renderTask   The renderTask.
143    *
144    * @return true, if the detector meets the parameters, false otherwise.
145    */
146   virtual bool CheckGestureDetector(const GestureEvent* gestureEvent, Actor* actor, RenderTaskPtr renderTask) = 0;
147
148   /**
149    * @brief This is canceling the ongoing gesture recognition process.
150    */
151   virtual void CancelProcessing() = 0;
152
153 protected: // Creation & Destruction
154   /**
155    * Construct a new GestureDetector.
156    * @param type the type of gesture
157    * @param pointer to the scene object, nullptr if none
158    * by default GestureDetectors don't have our own scene object
159    */
160   GestureDetector(GestureType::Value type, const SceneGraph::PropertyOwner* sceneObject = nullptr);
161
162   /**
163    * A reference counted object may only be deleted by calling Unreference()
164    */
165   ~GestureDetector() override;
166
167 private:
168   // Undefined
169   GestureDetector()                       = delete;
170   GestureDetector(const GestureDetector&) = delete;
171   GestureDetector& operator=(const GestureDetector& rhs) = delete;
172
173   /**
174    * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded()
175    */
176   void SceneObjectAdded(Object& object) override;
177
178   /**
179    * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded()
180    */
181   void SceneObjectRemoved(Object& object) override
182   {
183   }
184
185   /**
186    * @copydoc Dali::Internal::Object::Observer::ObjectDestroyed()
187    */
188   void ObjectDestroyed(Object& object) override;
189
190   /**
191    * For use in derived classes, called after an actor is attached.
192    * @param[in]  actor  The actor that is being attached.
193    */
194   virtual void OnActorAttach(Actor& actor) = 0;
195
196   /**
197    * For use in derived classes, called after an actor is detached.
198    * @param[in] actor The actor that is being detached.
199    */
200   virtual void OnActorDetach(Actor& actor) = 0;
201
202   /**
203    * For use in derived classes, called when an attached actor is destroyed.
204    * @param[in] object The object (Actor's base class) that has been destroyed.
205    * @note Derived classes should not call any Actor specific APIs in this method as the Actor's
206    *       destructor would have already been called.
207    */
208   virtual void OnActorDestroyed(Object& object) = 0;
209
210   /**
211    * @brief Callback for TouchedSignal signal
212    *
213    * @param actor The actor
214    * @param touch The touch event that has occurred
215    */
216   virtual bool OnTouchEvent(Dali::Actor actor, const Dali::TouchEvent& touch) = 0;
217
218   /**
219    * @brief Delivers touch events to each detector.
220    * This is an API that is called by FeedTouch and recognizes gestures directly from the Detector without going through the Reconizer.
221    *
222    * @param scene The scene
223    * @param event The touch event that has occurred
224    */
225   virtual void ProcessTouchEvent(Scene& scene, const Integration::TouchEvent& event) = 0;
226
227   /**
228    * @brief Clears the detector.
229    */
230   void Clear();
231
232 protected:
233   GestureType::Value            mType;                  ///< The gesture detector will detect this type of gesture.
234   GestureDetectorActorContainer mAttachedActors;        ///< Object::Observer is used to provide weak-pointer behaviour
235   GestureDetectorActorContainer mPendingAttachActors;   ///< Object::Observer is used to provide weak-pointer behaviour
236   GestureEventProcessor&        mGestureEventProcessor; ///< A reference to the gesture event processor.
237   ActorObserver                 mFeededActor;           ///< The Actor that feeds touch events
238   RenderTaskPtr                 mRenderTask;            ///< The render task used to generate this touch event.
239   GestureRecognizerPtr          mGestureRecognizer;     ///< The gesture recognizer
240   bool                          mIsDetected : 1;        ///< Whether gesture detected.
241 };
242
243 } // namespace Internal
244
245 // Helpers for public-api forwarding methods
246
247 inline Internal::GestureDetector& GetImplementation(Dali::GestureDetector& detector)
248 {
249   DALI_ASSERT_ALWAYS(detector && "GestureDetector handle is empty");
250
251   BaseObject& handle = detector.GetBaseObject();
252
253   return static_cast<Internal::GestureDetector&>(handle);
254 }
255
256 inline const Internal::GestureDetector& GetImplementation(const Dali::GestureDetector& detector)
257 {
258   DALI_ASSERT_ALWAYS(detector && "GestureDetector handle is empty");
259
260   const BaseObject& handle = detector.GetBaseObject();
261
262   return static_cast<const Internal::GestureDetector&>(handle);
263 }
264
265 } // namespace Dali
266
267 #endif // DALI_INTERNAL_GESTURE_DETECTOR_H