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