2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/events/tap-gesture/tap-gesture-detector-impl.h>
22 #include <cstring> // for strcmp
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>
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;
44 const char* const SIGNAL_TAP_DETECTED = "tapDetected";
48 return Dali::TapGestureDetector::New();
51 TypeRegistration mType(typeid(Dali::TapGestureDetector), typeid(Dali::GestureDetector), Create);
53 SignalConnectorType signalConnector1(mType, SIGNAL_TAP_DETECTED, &TapGestureDetector::DoConnectSignal);
57 TapGestureDetectorPtr TapGestureDetector::New()
59 return new TapGestureDetector;
62 TapGestureDetectorPtr TapGestureDetector::New(unsigned int tapsRequired)
64 return new TapGestureDetector(tapsRequired);
67 TapGestureDetector::TapGestureDetector()
68 : GestureDetector(GestureType::TAP),
69 mMinimumTapsRequired(DEFAULT_TAPS_REQUIRED),
70 mMaximumTapsRequired(DEFAULT_TAPS_REQUIRED),
71 mTouchesRequired(DEFAULT_TOUCHES_REQUIRED),
78 TapGestureDetector::TapGestureDetector(unsigned int tapsRequired)
79 : GestureDetector(GestureType::TAP),
80 mMinimumTapsRequired(tapsRequired),
81 mMaximumTapsRequired(tapsRequired),
82 mTouchesRequired(DEFAULT_TOUCHES_REQUIRED),
89 TapGestureDetector::~TapGestureDetector()
91 if(mTimerId != 0 && ThreadLocalStorage::Created())
93 Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
94 platformAbstraction.CancelTimer(mTimerId);
98 void TapGestureDetector::SetMinimumTapsRequired(unsigned int taps)
100 if(mMinimumTapsRequired != taps)
102 mMinimumTapsRequired = taps;
104 if(!mAttachedActors.empty())
106 mGestureEventProcessor.GestureDetectorUpdated(this);
111 void TapGestureDetector::SetMaximumTapsRequired(unsigned int taps)
113 if(mMaximumTapsRequired != taps)
115 mMaximumTapsRequired = taps;
117 if(!mAttachedActors.empty())
119 mGestureEventProcessor.GestureDetectorUpdated(this);
124 void TapGestureDetector::SetTouchesRequired(unsigned int touches)
126 if(mTouchesRequired != touches)
128 mTouchesRequired = touches;
130 if(!mAttachedActors.empty())
132 mGestureEventProcessor.GestureDetectorUpdated(this);
137 unsigned int TapGestureDetector::GetMinimumTapsRequired() const
139 return mMinimumTapsRequired;
142 unsigned int TapGestureDetector::GetMaximumTapsRequired() const
144 return mMaximumTapsRequired;
147 unsigned int TapGestureDetector::GetTouchesRequired() const
149 return mTouchesRequired;
152 void TapGestureDetector::EmitTapGestureSignal(Dali::Actor tappedActor, const Dali::TapGesture& tap)
154 Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
157 platformAbstraction.CancelTimer(mTimerId);
160 if(mMaximumTapsRequired > tap.GetNumberOfTaps())
162 mTappedActor = tappedActor;
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());
173 mTimerId = platformAbstraction.StartTimer(DEFAULT_TAP_WAIT_TIME, MakeCallback(this, &TapGestureDetector::TimerCallback));
177 // Guard against destruction during signal emission
178 Dali::TapGestureDetector handle(this);
180 mDetectedSignal.Emit(tappedActor, tap);
184 bool TapGestureDetector::TimerCallback()
186 // Guard against destruction during signal emission
187 Dali::TapGestureDetector handle(this);
189 mDetectedSignal.Emit(mTappedActor, mTap);
195 bool TapGestureDetector::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
197 bool connected(true);
198 TapGestureDetector* gesture = static_cast<TapGestureDetector*>(object); // TypeRegistry guarantees that this is the correct type.
200 if(0 == strcmp(signalName.c_str(), SIGNAL_TAP_DETECTED))
202 gesture->DetectedSignal().Connect(tracker, functor);
206 // signalName does not match any signal
213 void TapGestureDetector::OnActorAttach(Actor& actor)
218 void TapGestureDetector::OnActorDetach(Actor& actor)
223 void TapGestureDetector::OnActorDestroyed(Object& object)
228 } // namespace Internal