Revert "[Tizen] Fix SVACE error in tap-gesture-detector-impl.cpp"
[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   mReceiveAllTapEvents(false)
76 {
77 }
78
79 TapGestureDetector::TapGestureDetector(unsigned int tapsRequired)
80 : GestureDetector(GestureType::TAP),
81   mMinimumTapsRequired(tapsRequired),
82   mMaximumTapsRequired(tapsRequired),
83   mTouchesRequired(DEFAULT_TOUCHES_REQUIRED),
84   mTimerId(0),
85   mTappedActor(),
86   mTap()
87 {
88 }
89
90 TapGestureDetector::~TapGestureDetector()
91 {
92   if(mTimerId != 0 && ThreadLocalStorage::Created())
93   {
94     Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
95     platformAbstraction.CancelTimer(mTimerId);
96   }
97 }
98
99 void TapGestureDetector::SetMinimumTapsRequired(unsigned int taps)
100 {
101   if(mMinimumTapsRequired != taps)
102   {
103     mMinimumTapsRequired = taps;
104
105     if(!mAttachedActors.empty())
106     {
107       mGestureEventProcessor.GestureDetectorUpdated(this);
108     }
109   }
110 }
111
112 void TapGestureDetector::SetMaximumTapsRequired(unsigned int taps)
113 {
114   if(mMaximumTapsRequired != taps)
115   {
116     mMaximumTapsRequired = taps;
117
118     if(!mAttachedActors.empty())
119     {
120       mGestureEventProcessor.GestureDetectorUpdated(this);
121     }
122   }
123 }
124
125 void TapGestureDetector::SetTouchesRequired(unsigned int touches)
126 {
127   if(mTouchesRequired != touches)
128   {
129     mTouchesRequired = touches;
130
131     if(!mAttachedActors.empty())
132     {
133       mGestureEventProcessor.GestureDetectorUpdated(this);
134     }
135   }
136 }
137
138 unsigned int TapGestureDetector::GetMinimumTapsRequired() const
139 {
140   return mMinimumTapsRequired;
141 }
142
143 unsigned int TapGestureDetector::GetMaximumTapsRequired() const
144 {
145   return mMaximumTapsRequired;
146 }
147
148 unsigned int TapGestureDetector::GetTouchesRequired() const
149 {
150   return mTouchesRequired;
151 }
152
153 void TapGestureDetector::ReceiveAllTapEvents(bool receive)
154 {
155   mReceiveAllTapEvents = receive;
156 }
157
158 void TapGestureDetector::EmitTapGestureSignal(Dali::Actor tappedActor, const Dali::TapGesture& tap)
159 {
160   Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
161   if(mTimerId != 0)
162   {
163     platformAbstraction.CancelTimer(mTimerId);
164     mTimerId = 0;
165   }
166   if(mMaximumTapsRequired > tap.GetNumberOfTaps() && !mReceiveAllTapEvents)
167   {
168     mTappedActor = tappedActor;
169
170     Internal::TapGesturePtr internalTap(new Internal::TapGesture(tap.GetState()));
171     internalTap->SetTime(tap.GetTime());
172     internalTap->SetNumberOfTaps(tap.GetNumberOfTaps());
173     internalTap->SetNumberOfTouches(tap.GetNumberOfTouches());
174     internalTap->SetScreenPoint(tap.GetScreenPoint());
175     internalTap->SetLocalPoint(tap.GetLocalPoint());
176     internalTap->SetGestureSourceType(tap.GetSourceType());
177     mTap = Dali::TapGesture(internalTap.Get());
178
179     mTimerId = platformAbstraction.StartTimer(DEFAULT_TAP_WAIT_TIME, MakeCallback(this, &TapGestureDetector::TimerCallback));
180   }
181   else
182   {
183     // Guard against destruction during signal emission
184     Dali::TapGestureDetector handle(this);
185
186     mDetectedSignal.Emit(tappedActor, tap);
187   }
188 }
189
190 bool TapGestureDetector::TimerCallback()
191 {
192   // Guard against destruction during signal emission
193   Dali::TapGestureDetector handle(this);
194
195   mDetectedSignal.Emit(mTappedActor, mTap);
196
197   mTimerId = 0;
198   return false;
199 }
200
201 bool TapGestureDetector::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
202 {
203   bool                connected(true);
204   TapGestureDetector* gesture = static_cast<TapGestureDetector*>(object); // TypeRegistry guarantees that this is the correct type.
205
206   if(0 == strcmp(signalName.c_str(), SIGNAL_TAP_DETECTED))
207   {
208     gesture->DetectedSignal().Connect(tracker, functor);
209   }
210   else
211   {
212     // signalName does not match any signal
213     connected = false;
214   }
215
216   return connected;
217 }
218
219 void TapGestureDetector::OnActorAttach(Actor& actor)
220 {
221   // Do nothing
222 }
223
224 void TapGestureDetector::OnActorDetach(Actor& actor)
225 {
226   // Do nothing
227 }
228
229 void TapGestureDetector::OnActorDestroyed(Object& object)
230 {
231   // Do nothing
232 }
233
234 } // namespace Internal
235
236 } // namespace Dali