[dali_2.3.6] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-recognizer.h
1 #ifndef DALI_INTERNAL_GESTURE_RECOGNIZER_H
2 #define DALI_INTERNAL_GESTURE_RECOGNIZER_H
3
4 /*
5  * Copyright (c) 2023 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 // EXTERNAL INCLUDES
22 #include <dali/integration-api/events/touch-event-integ.h>
23 #include <dali/internal/event/events/actor-observer.h>
24 #include <dali/internal/event/events/gesture-event.h>
25 #include <dali/internal/event/render-tasks/render-task-impl.h>
26 #include <dali/public-api/common/vector-wrapper.h>
27 #include <dali/public-api/events/gesture.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/public-api/object/ref-object.h>
30
31 namespace Dali
32 {
33 namespace Integration
34 {
35 struct TouchEvent;
36 }
37
38 namespace Internal
39 {
40 struct GestureRequest;
41 class Scene;
42
43 template<typename T>
44 class RecognizerObserver
45 {
46 public:
47   virtual void Process(Scene& scene, const T& event, Actor* actor = nullptr) = 0;
48
49   virtual ~RecognizerObserver() = default;
50   ;
51 };
52
53 /**
54  * Abstract Base class for all adaptor gesture detectors.
55  *
56  * @note this may be replaced by gesture events sent directly from X.
57  */
58 class GestureRecognizer : public RefObject
59 {
60 public:
61   /**
62    * Called when it gets a touch event.  The gesture recognizer should
63    * evaluate this event along with previously received events to determine
64    * whether the gesture they require has taken place.
65    * @param[in]  event  The latest touch event.
66    */
67   virtual void SendEvent(const Integration::TouchEvent& event) = 0;
68
69   /**
70    * Called when Core updates the gesture's detection requirements.
71    * @param[in]  request  The updated detection requirements.
72    */
73   virtual void Update(const GestureRequest& request) = 0;
74
75   /**
76    * Returns the type of gesture detector.
77    * @return Type of gesture detector.
78    */
79   GestureType::Value GetType() const
80   {
81     return mType;
82   }
83
84   /**
85    * Called when we get a touch event.
86    * @param[in]  scene  The scene the touch event has occurred on
87    * @param[in]  event  The latest touch event
88    */
89   void SendEvent(Scene& scene, const Integration::TouchEvent& event)
90   {
91     mScene = &scene;
92     if(event.GetPointCount() > 0)
93     {
94       const Integration::Point& point       = event.points[0];
95       MouseButton::Type         mouseButton = point.GetMouseButton();
96       if(mouseButton != MouseButton::INVALID)
97       {
98         Device::Class::Type type = point.GetDeviceClass();
99         if(type == Device::Class::Type::MOUSE)
100         {
101           mSourceType = GestureSourceType::MOUSE;
102         }
103         else if(type == Device::Class::Type::TOUCH)
104         {
105           mSourceType = GestureSourceType::TOUCH;
106         }
107         switch(mouseButton)
108         {
109           case MouseButton::PRIMARY:
110           {
111             mSourceData = GestureSourceData::MOUSE_PRIMARY;
112             break;
113           }
114           case MouseButton::SECONDARY:
115           {
116             mSourceData = GestureSourceData::MOUSE_SECONDARY;
117             break;
118           }
119           case MouseButton::TERTIARY:
120           {
121             mSourceData = GestureSourceData::MOUSE_TERTIARY;
122             break;
123           }
124           default:
125           {
126             mSourceData = GestureSourceData::INVALID;
127             break;
128           }
129         }
130       }
131     }
132     SendEvent(event);
133   }
134
135   /**
136    * Called when we get a touch event.
137    * @param[in]  actor  The actor the touch event has occurred on
138    * @param[in]  renderTask  The renderTask the touch event has occurred on
139    * @param[in]  scene  The scene the touch event has occurred on
140    * @param[in]  event  The latest touch event
141    */
142   void SendEvent(Actor& actor, Dali::Internal::RenderTask& renderTask, Scene& scene, const Integration::TouchEvent& event)
143   {
144     mActor.SetActor(&actor);
145     mRenderTask = &renderTask;
146     SendEvent(scene, event);
147   }
148
149 protected:
150   /**
151    * Protected Constructor. Should only be able to create derived class objects.
152    * @param[in]  screenSize    The size of the screen.
153    * @param[in]  detectorType  The type of gesture detector.
154    */
155   GestureRecognizer(Vector2 screenSize, GestureType::Value detectorType)
156   : mScreenSize(screenSize),
157     mType(detectorType),
158     mScene(nullptr),
159     mSourceType(GestureSourceType::INVALID),
160     mSourceData(GestureSourceData::INVALID),
161     mActor(),
162     mRenderTask()
163   {
164   }
165
166   /**
167    * Protected Constructor. Should only be able to create derived class objects.
168    *
169    * Use this constructor with the screen size is not used in the dereived class.
170    * @param[in]  detectorType  The type of gesture detector.
171    */
172   GestureRecognizer(GestureType::Value detectorType)
173   : GestureRecognizer(Vector2::ZERO, detectorType)
174   {
175   }
176
177   /**
178    * Virtual destructor.
179    */
180   ~GestureRecognizer() override = default;
181
182 protected:
183   Vector2                     mScreenSize;
184   GestureType::Value          mType;
185   Scene*                      mScene;
186   GestureSourceType           mSourceType; /// < Gesture input source type.
187   GestureSourceData           mSourceData; /// < Gesture input source data.
188   ActorObserver               mActor;      /// < The actor used to generate this touch event.
189   RenderTaskPtr               mRenderTask; /// < The render task used to generate this touch event.
190 };
191
192 using GestureRecognizerPtr = IntrusivePtr<GestureRecognizer>;
193
194 } // namespace Internal
195
196 } // namespace Dali
197
198 #endif // DALI_INTERNAL_GESTURE_RECOGNIZER_H