1. Revert "Sets the tap gesture timer to 330ms."
[platform/core/uifw/dali-core.git] / dali / internal / event / events / tap-gesture / tap-gesture-detector-impl.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/events/tap-gesture/tap-gesture-detector-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/platform-abstraction.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/events/gesture-event-processor.h>
29 #include <dali/internal/event/events/tap-gesture/tap-gesture-impl.h>
30 #include <dali/public-api/events/tap-gesture.h>
31 #include <dali/public-api/object/type-registry.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace
38 {
39 constexpr uint32_t DEFAULT_TAPS_REQUIRED    = 1u;
40 constexpr uint32_t DEFAULT_TOUCHES_REQUIRED = 1u;
41 constexpr uint32_t DEFAULT_TAP_WAIT_TIME    = 330u;
42
43 // Signals
44 const char* const SIGNAL_TAP_DETECTED = "tapDetected";
45
46 BaseHandle Create()
47 {
48   return Dali::TapGestureDetector::New();
49 }
50
51 TypeRegistration mType(typeid(Dali::TapGestureDetector), typeid(Dali::GestureDetector), Create);
52
53 SignalConnectorType signalConnector1(mType, SIGNAL_TAP_DETECTED, &TapGestureDetector::DoConnectSignal);
54
55 } // namespace
56
57 TapGestureDetectorPtr TapGestureDetector::New()
58 {
59   return new TapGestureDetector;
60 }
61
62 TapGestureDetectorPtr TapGestureDetector::New(unsigned int tapsRequired)
63 {
64   return new TapGestureDetector(tapsRequired);
65 }
66
67 TapGestureDetector::TapGestureDetector()
68 : GestureDetector(GestureType::TAP),
69   mMinimumTapsRequired(DEFAULT_TAPS_REQUIRED),
70   mMaximumTapsRequired(DEFAULT_TAPS_REQUIRED),
71   mTouchesRequired(DEFAULT_TOUCHES_REQUIRED),
72   mTimerId(0),
73   mTappedActor(),
74   mTap()
75 {
76 }
77
78 TapGestureDetector::TapGestureDetector(unsigned int tapsRequired)
79 : GestureDetector(GestureType::TAP),
80   mMinimumTapsRequired(tapsRequired),
81   mMaximumTapsRequired(tapsRequired),
82   mTouchesRequired(DEFAULT_TOUCHES_REQUIRED),
83   mTimerId(0),
84   mTappedActor(),
85   mTap()
86 {
87 }
88
89 TapGestureDetector::~TapGestureDetector()
90 {
91   if(mTimerId != 0 && ThreadLocalStorage::Created())
92   {
93     Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
94     platformAbstraction.CancelTimer(mTimerId);
95   }
96 }
97
98 void TapGestureDetector::SetMinimumTapsRequired(unsigned int taps)
99 {
100   if(mMinimumTapsRequired != taps)
101   {
102     mMinimumTapsRequired = taps;
103
104     if(!mAttachedActors.empty())
105     {
106       mGestureEventProcessor.GestureDetectorUpdated(this);
107     }
108   }
109 }
110
111 void TapGestureDetector::SetMaximumTapsRequired(unsigned int taps)
112 {
113   if(mMaximumTapsRequired != taps)
114   {
115     mMaximumTapsRequired = taps;
116
117     if(!mAttachedActors.empty())
118     {
119       mGestureEventProcessor.GestureDetectorUpdated(this);
120     }
121   }
122 }
123
124 void TapGestureDetector::SetTouchesRequired(unsigned int touches)
125 {
126   if(mTouchesRequired != touches)
127   {
128     mTouchesRequired = touches;
129
130     if(!mAttachedActors.empty())
131     {
132       mGestureEventProcessor.GestureDetectorUpdated(this);
133     }
134   }
135 }
136
137 unsigned int TapGestureDetector::GetMinimumTapsRequired() const
138 {
139   return mMinimumTapsRequired;
140 }
141
142 unsigned int TapGestureDetector::GetMaximumTapsRequired() const
143 {
144   return mMaximumTapsRequired;
145 }
146
147 unsigned int TapGestureDetector::GetTouchesRequired() const
148 {
149   return mTouchesRequired;
150 }
151
152 void TapGestureDetector::EmitTapGestureSignal(Dali::Actor tappedActor, const Dali::TapGesture& tap)
153 {
154   Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
155   if(mTimerId != 0)
156   {
157     platformAbstraction.CancelTimer(mTimerId);
158     mTimerId = 0;
159   }
160   if(mMaximumTapsRequired > tap.GetNumberOfTaps())
161   {
162     mTappedActor = tappedActor;
163
164     Internal::TapGesturePtr internalTap(new Internal::TapGesture(tap.GetState()));
165     internalTap->SetTime(tap.GetTime());
166     internalTap->SetNumberOfTaps(tap.GetNumberOfTaps());
167     internalTap->SetNumberOfTouches(tap.GetNumberOfTouches());
168     internalTap->SetScreenPoint(tap.GetScreenPoint());
169     internalTap->SetLocalPoint(tap.GetLocalPoint());
170     internalTap->SetGestureSourceType(tap.GetSourceType());
171     mTap = Dali::TapGesture(internalTap.Get());
172
173     mTimerId = platformAbstraction.StartTimer(DEFAULT_TAP_WAIT_TIME, MakeCallback(this, &TapGestureDetector::TimerCallback));
174   }
175   else
176   {
177     // Guard against destruction during signal emission
178     Dali::TapGestureDetector handle(this);
179
180     mDetectedSignal.Emit(tappedActor, tap);
181   }
182 }
183
184 bool TapGestureDetector::TimerCallback()
185 {
186   // Guard against destruction during signal emission
187   Dali::TapGestureDetector handle(this);
188
189   mDetectedSignal.Emit(mTappedActor, mTap);
190
191   mTimerId = 0;
192   return false;
193 }
194
195 bool TapGestureDetector::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
196 {
197   bool                connected(true);
198   TapGestureDetector* gesture = static_cast<TapGestureDetector*>(object); // TypeRegistry guarantees that this is the correct type.
199
200   if(0 == strcmp(signalName.c_str(), SIGNAL_TAP_DETECTED))
201   {
202     gesture->DetectedSignal().Connect(tracker, functor);
203   }
204   else
205   {
206     // signalName does not match any signal
207     connected = false;
208   }
209
210   return connected;
211 }
212
213 void TapGestureDetector::OnActorAttach(Actor& actor)
214 {
215   // Do nothing
216 }
217
218 void TapGestureDetector::OnActorDetach(Actor& actor)
219 {
220   // Do nothing
221 }
222
223 void TapGestureDetector::OnActorDestroyed(Object& object)
224 {
225   // Do nothing
226 }
227
228 } // namespace Internal
229
230 } // namespace Dali