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