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