[dali_2.3.27] 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/public-api/common/dali-common.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/events/gesture-detector.h>
26 #include <dali/public-api/events/gesture.h>
27 #include <dali/public-api/signals/slot-delegate.h>
28
29 namespace Dali
30 {
31 namespace Integration
32 {
33 struct GestureEvent;
34 }
35
36 namespace Internal
37 {
38 class GestureDetector;
39 class GestureEventProcessor;
40
41 using GestureDetectorPtr            = IntrusivePtr<GestureDetector>;
42 using GestureDetectorContainer      = std::vector<GestureDetector*>;
43 using GestureDetectorActorContainer = std::vector<Actor*>;
44
45 /**
46  * This is a type trait that should be used by deriving gesture detectors for their container type.
47  */
48 template<typename Detector>
49 struct DerivedGestureDetectorContainer
50 {
51   using type = std::vector<Detector*>;
52 };
53
54 /**
55  * @copydoc Dali::GestureDetector
56  */
57 class GestureDetector : public Object, public Object::Observer
58 {
59 public:
60   /**
61    * @copydoc Dali::GestureDetector::Attach()
62    */
63   void Attach(Actor& actor);
64
65   /**
66    * @copydoc Dali::GestureDetector::Detach()
67    */
68   void Detach(Actor& actor);
69
70   /**
71    * @copydoc Dali::GestureDetector::DetachAll()
72    */
73   void DetachAll();
74
75   /**
76    * @copydoc Dali::GestureDetector::GetAttachedActorCount() const
77    */
78   size_t GetAttachedActorCount() const;
79
80   /**
81    * @copydoc Dali::GestureDetector::GetAttachedActor() const
82    */
83   Dali::Actor GetAttachedActor(size_t index) const;
84
85   /**
86    * @copydoc Dali::GestureDetector::FeedTouch()
87    */
88   bool FeedTouch(Dali::Actor& actor, Dali::TouchEvent& touch);
89
90   /**
91    * Returns a const reference to the container of attached actor pointers.
92    * @return A const reference to the attached internal actors.
93    */
94   const GestureDetectorActorContainer& GetAttachedActorPointers() const
95   {
96     return mAttachedActors;
97   }
98
99   /**
100    * Retrieves the type of GestureDetector
101    * @return The GestureDetector Type
102    */
103   GestureType::Value GetType() const
104   {
105     return mType;
106   }
107
108   /**
109    * Checks if the specified actor is still attached or pending attachment.
110    * @param[in]  actor  The actor to check.
111    * @return true, if the actor is attached or pending, false otherwise.
112    */
113   bool IsAttached(Actor& actor) const;
114
115   /**
116    * @brief Returns whether the gesture was detected.
117    * @return true, if the gesture was detected, false otherwise.
118    */
119   bool IsDetected() const;
120
121   /**
122    * @brief Sets whether the gesture was detected.
123    * @param detected Whether gesture detected.
124    */
125   void SetDetected(bool detected);
126
127 protected: // Creation & Destruction
128   /**
129    * Construct a new GestureDetector.
130    * @param type the type of gesture
131    * @param pointer to the scene object, nullptr if none
132    * by default GestureDetectors don't have our own scene object
133    */
134   GestureDetector(GestureType::Value type, const SceneGraph::PropertyOwner* sceneObject = nullptr);
135
136   /**
137    * A reference counted object may only be deleted by calling Unreference()
138    */
139   ~GestureDetector() override;
140
141 private:
142   // Undefined
143   GestureDetector()                       = delete;
144   GestureDetector(const GestureDetector&) = delete;
145   GestureDetector& operator=(const GestureDetector& rhs) = delete;
146
147   /**
148    * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded()
149    */
150   void SceneObjectAdded(Object& object) override;
151
152   /**
153    * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded()
154    */
155   void SceneObjectRemoved(Object& object) override
156   {
157   }
158
159   /**
160    * @copydoc Dali::Internal::Object::Observer::ObjectDestroyed()
161    */
162   void ObjectDestroyed(Object& object) override;
163
164   /**
165    * For use in derived classes, called after an actor is attached.
166    * @param[in]  actor  The actor that is being attached.
167    */
168   virtual void OnActorAttach(Actor& actor) = 0;
169
170   /**
171    * For use in derived classes, called after an actor is detached.
172    * @param[in] actor The actor that is being detached.
173    */
174   virtual void OnActorDetach(Actor& actor) = 0;
175
176   /**
177    * For use in derived classes, called when an attached actor is destroyed.
178    * @param[in] object The object (Actor's base class) that has been destroyed.
179    * @note Derived classes should not call any Actor specific APIs in this method as the Actor's
180    *       destructor would have already been called.
181    */
182   virtual void OnActorDestroyed(Object& object) = 0;
183
184 protected:
185   GestureType::Value            mType;                  ///< The gesture detector will detect this type of gesture.
186   GestureDetectorActorContainer mAttachedActors;        ///< Object::Observer is used to provide weak-pointer behaviour
187   GestureDetectorActorContainer mPendingAttachActors;   ///< Object::Observer is used to provide weak-pointer behaviour
188   GestureEventProcessor&        mGestureEventProcessor; ///< A reference to the gesture event processor.
189   bool mIsDetected : 1;                                 ///< Whether gesture detected.
190 };
191
192 } // namespace Internal
193
194 // Helpers for public-api forwarding methods
195
196 inline Internal::GestureDetector& GetImplementation(Dali::GestureDetector& detector)
197 {
198   DALI_ASSERT_ALWAYS(detector && "GestureDetector handle is empty");
199
200   BaseObject& handle = detector.GetBaseObject();
201
202   return static_cast<Internal::GestureDetector&>(handle);
203 }
204
205 inline const Internal::GestureDetector& GetImplementation(const Dali::GestureDetector& detector)
206 {
207   DALI_ASSERT_ALWAYS(detector && "GestureDetector handle is empty");
208
209   const BaseObject& handle = detector.GetBaseObject();
210
211   return static_cast<const Internal::GestureDetector&>(handle);
212 }
213
214 } // namespace Dali
215
216 #endif // DALI_INTERNAL_GESTURE_DETECTOR_H