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 "long-press-gesture-detector-impl.h"
22 #include <cstring> // for strcmp
26 #include <dali/internal/event/events/gesture-event-processor.h>
27 #include <dali/public-api/object/type-registry.h>
37 const char* const SIGNAL_LONG_PRESS_DETECTED = "longPressDetected";
41 return Dali::LongPressGestureDetector::New();
44 TypeRegistration mType(typeid(Dali::LongPressGestureDetector), typeid(Dali::GestureDetector), Create);
46 SignalConnectorType signalConnector1(mType, SIGNAL_LONG_PRESS_DETECTED, &LongPressGestureDetector::DoConnectSignal);
52 const unsigned int DEFAULT_TOUCHES_REQUIRED = 1;
53 } // unnamed namespace
55 LongPressGestureDetectorPtr LongPressGestureDetector::New()
57 return new LongPressGestureDetector;
60 LongPressGestureDetectorPtr LongPressGestureDetector::New(unsigned int touchesRequired)
62 return new LongPressGestureDetector(touchesRequired, touchesRequired);
65 LongPressGestureDetectorPtr LongPressGestureDetector::New(unsigned int minTouches, unsigned int maxTouches)
67 return new LongPressGestureDetector(minTouches, maxTouches);
70 LongPressGestureDetector::LongPressGestureDetector()
71 : GestureDetector(GestureType::LONG_PRESS),
72 mMinimumTouchesRequired(DEFAULT_TOUCHES_REQUIRED),
73 mMaximumTouchesRequired(DEFAULT_TOUCHES_REQUIRED)
77 LongPressGestureDetector::LongPressGestureDetector(unsigned int minTouches, unsigned int maxTouches)
78 : GestureDetector(GestureType::LONG_PRESS),
79 mMinimumTouchesRequired(minTouches),
80 mMaximumTouchesRequired(maxTouches)
84 LongPressGestureDetector::~LongPressGestureDetector() = default;
86 void LongPressGestureDetector::SetTouchesRequired(unsigned int touches)
88 DALI_ASSERT_ALWAYS(touches > 0 && "Can only set a positive number of required touches");
90 if(mMinimumTouchesRequired != touches || mMaximumTouchesRequired != touches)
92 mMinimumTouchesRequired = mMaximumTouchesRequired = touches;
94 if(!mAttachedActors.empty())
96 mGestureEventProcessor.GestureDetectorUpdated(this);
101 void LongPressGestureDetector::SetTouchesRequired(unsigned int minTouches, unsigned int maxTouches)
103 DALI_ASSERT_ALWAYS(minTouches > 0 && "Can only set a positive number of minimum touches");
104 DALI_ASSERT_ALWAYS(maxTouches > 0 && "Can only set a positive number of minimum touches");
105 DALI_ASSERT_ALWAYS(minTouches <= maxTouches && "Number of minimum touches must be less than maximum");
107 if(mMinimumTouchesRequired != minTouches || mMaximumTouchesRequired != maxTouches)
109 mMinimumTouchesRequired = minTouches;
110 mMaximumTouchesRequired = maxTouches;
112 if(!mAttachedActors.empty())
114 mGestureEventProcessor.GestureDetectorUpdated(this);
119 unsigned int LongPressGestureDetector::GetMinimumTouchesRequired() const
121 return mMinimumTouchesRequired;
124 unsigned int LongPressGestureDetector::GetMaximumTouchesRequired() const
126 return mMaximumTouchesRequired;
129 void LongPressGestureDetector::EmitLongPressGestureSignal(Dali::Actor pressedActor, const Dali::LongPressGesture& longPress)
131 // Guard against destruction during signal emission
132 Dali::LongPressGestureDetector handle(this);
134 mDetectedSignal.Emit(pressedActor, longPress);
137 bool LongPressGestureDetector::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
139 bool connected(true);
140 LongPressGestureDetector* gesture = static_cast<LongPressGestureDetector*>(object); // TypeRegistry guarantees that this is the correct type.
142 if(0 == strcmp(signalName.c_str(), SIGNAL_LONG_PRESS_DETECTED))
144 gesture->DetectedSignal().Connect(tracker, functor);
148 // signalName does not match any signal
155 void LongPressGestureDetector::OnActorAttach(Actor& actor)
160 void LongPressGestureDetector::OnActorDetach(Actor& actor)
165 void LongPressGestureDetector::OnActorDestroyed(Object& object)
170 uint32_t LongPressGestureDetector::GetMinimumHoldingTime() const
172 return mGestureEventProcessor.GetLongPressMinimumHoldingTime();
175 } // namespace Internal