2 * Copyright (c) 2020 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/integration-api/events/touch-event-combiner.h>
23 #include <cmath> // abs<float>
26 #include <dali/integration-api/events/hover-event-integ.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/public-api/common/dali-common.h>
36 const unsigned long DEFAULT_MINIMUM_MOTION_TIME(1u);
37 const Vector2 DEFAULT_MINIMUM_MOTION_DISTANCE(1.0f, 1.0f);
38 } // unnamed namespace
40 struct TouchEventCombiner::PointInfo
46 * @param[in] touchPoint The point to add.
47 * @param[in] pointTime The time of the point event.
49 PointInfo(const Point& touchPoint, uint32_t pointTime)
57 Point point; ///< The point.
58 uint32_t time; ///< The time the point event took place.
61 TouchEventCombiner::TouchEventCombiner()
62 : mMinMotionTime(DEFAULT_MINIMUM_MOTION_TIME),
63 mMinMotionDistance(DEFAULT_MINIMUM_MOTION_DISTANCE)
67 TouchEventCombiner::TouchEventCombiner(uint32_t minMotionTime, float minMotionXDistance, float minMotionYDistance)
68 : mMinMotionTime(minMotionTime),
69 mMinMotionDistance(minMotionXDistance, minMotionYDistance)
71 DALI_ASSERT_ALWAYS(minMotionXDistance >= 0.0f && minMotionYDistance >= 0.0f && "Negative values not allowed\n");
74 TouchEventCombiner::TouchEventCombiner(uint32_t minMotionTime, Vector2 minMotionDistance)
75 : mMinMotionTime(minMotionTime),
76 mMinMotionDistance(minMotionDistance)
78 DALI_ASSERT_ALWAYS(minMotionDistance.x >= 0.0f && minMotionDistance.y >= 0.0f && "Negative values not allowed\n");
81 TouchEventCombiner::~TouchEventCombiner() = default;
83 TouchEventCombiner::EventDispatchType TouchEventCombiner::GetNextTouchEvent(const Point& point, uint32_t time, TouchEvent& touchEvent, HoverEvent& hoverEvent)
85 TouchEventCombiner::EventDispatchType dispatchEvent(TouchEventCombiner::DISPATCH_NONE);
86 const PointState::Type state = point.GetState();
87 const int deviceId = point.GetDeviceId();
91 case PointState::STARTED:
93 touchEvent.time = time;
94 bool addToContainer(true);
96 // Iterate through already stored touch points and add to TouchEvent
97 for(PointInfoContainer::iterator iter = mPressedPoints.begin(), endIter = mPressedPoints.end(); iter != endIter; ++iter)
99 if(iter->point.GetDeviceId() != deviceId)
101 iter->point.SetState(PointState::STATIONARY);
105 // System has sent us two down points for the same point ID, update our stored data to latest.
106 // We do not want to emit another down event for this Point Device ID.
108 addToContainer = false;
112 touchEvent.AddPoint(iter->point);
115 // Add new touch point to the list and to the TouchEvent
118 mPressedPoints.push_back(PointInfo(point, time));
119 touchEvent.AddPoint(point);
120 dispatchEvent = TouchEventCombiner::DISPATCH_TOUCH; // Only dispatch touch event if just added to container
122 // Check whether hover event was dispatched previously
123 if(!mHoveredPoints.empty())
125 hoverEvent.time = time;
127 PointInfoContainer::iterator match(mHoveredPoints.end());
128 for(PointInfoContainer::iterator iter = mHoveredPoints.begin(), endIter = mHoveredPoints.end(); iter != endIter; ++iter)
130 if(deviceId == iter->point.GetDeviceId())
133 // Add new point to the HoverEvent
134 iter->point.SetState(PointState::FINISHED);
135 hoverEvent.AddPoint(iter->point);
139 iter->point.SetState(PointState::STATIONARY);
140 hoverEvent.AddPoint(iter->point);
144 if(match != mHoveredPoints.end())
146 mHoveredPoints.erase(match);
147 dispatchEvent = TouchEventCombiner::DISPATCH_BOTH; // We should only dispatch hover events if the point was actually hovered in this window
155 case PointState::FINISHED:
157 touchEvent.time = time;
159 // Find pressed touch point in local list (while also adding the stored points to the touchEvent)
160 PointInfoContainer::iterator match(mPressedPoints.end());
161 for(PointInfoContainer::iterator iter = mPressedPoints.begin(), endIter = mPressedPoints.end(); iter != endIter; ++iter)
163 if(deviceId == iter->point.GetDeviceId())
167 // Add new point to the TouchEvent
168 touchEvent.AddPoint(point);
172 iter->point.SetState(PointState::STATIONARY);
173 touchEvent.AddPoint(iter->point);
177 if(match != mPressedPoints.end())
179 mPressedPoints.erase(match);
180 dispatchEvent = TouchEventCombiner::DISPATCH_TOUCH; // We should only dispatch touch events if the point was actually pressed in this window
182 // Iterate through already stored touch points for HoverEvent and delete them
183 for(PointInfoContainer::iterator iter = mHoveredPoints.begin(), endIter = mHoveredPoints.end(); iter != endIter; ++iter)
185 if(iter->point.GetDeviceId() == deviceId)
187 iter = mHoveredPoints.erase(iter);
194 case PointState::MOTION:
196 bool fromNewDeviceId = false;
198 if(!mPressedPoints.empty())
200 touchEvent.time = time;
203 PointInfoContainer::iterator match = mPressedPoints.end();
204 const Vector2& pointScreenPosition = point.GetScreenPosition();
205 for(PointInfoContainer::iterator iter = mPressedPoints.begin(), endIter = mPressedPoints.end(); iter != endIter; ++iter)
207 if(deviceId == iter->point.GetDeviceId())
209 uint32_t timeDiff(time - iter->time);
211 if(timeDiff < mMinMotionTime)
213 // Motion event sent too soon after previous event so ignore
218 const Vector2& currentScreenPosition = iter->point.GetScreenPosition();
219 if((std::abs(pointScreenPosition.x - currentScreenPosition.x) < mMinMotionDistance.x) &&
220 (std::abs(pointScreenPosition.y - currentScreenPosition.y) < mMinMotionDistance.y))
222 // Not enough positional change from last event so ignore
229 // Add new touch point to the TouchEvent
230 touchEvent.AddPoint(point);
234 iter->point.SetState(PointState::STATIONARY);
235 touchEvent.AddPoint(iter->point);
239 if(match != mPressedPoints.end())
241 PointInfo matchedPoint(point, time);
242 std::swap(*match, matchedPoint);
244 dispatchEvent = TouchEventCombiner::DISPATCH_TOUCH; // Dispatch touch event
248 fromNewDeviceId = true;
252 // Dispatch hover event if no previous down event received or the motion event comes from a new device ID
253 if(mPressedPoints.empty() || fromNewDeviceId)
255 hoverEvent.time = time;
257 // Iterate through already stored touch points and add to HoverEvent
259 PointInfoContainer::iterator match = mHoveredPoints.end();
260 const Vector2& pointScreenPosition = point.GetScreenPosition();
261 for(PointInfoContainer::iterator iter = mHoveredPoints.begin(), endIter = mHoveredPoints.end(); iter != endIter; ++iter)
263 if(iter->point.GetDeviceId() == deviceId)
265 uint32_t timeDiff(time - iter->time);
267 if(timeDiff < mMinMotionTime)
269 // Motion event sent too soon after previous event so ignore
274 const Vector2& currentScreenPosition = iter->point.GetScreenPosition();
275 if((std::abs(pointScreenPosition.x - currentScreenPosition.x) < mMinMotionDistance.x) &&
276 (std::abs(pointScreenPosition.y - currentScreenPosition.y) < mMinMotionDistance.y))
278 // Not enough positional change from last event so ignore
285 // Add new touch point to the HoverEvent
286 hoverEvent.AddPoint(point);
290 iter->point.SetState(PointState::STATIONARY);
291 hoverEvent.AddPoint(iter->point);
295 // Add new hover point to the list and to the HoverEvent
296 if(!ignore) // Only dispatch hover event when it should not be ignored
298 if(match == mHoveredPoints.end())
300 Point hoverPoint(point);
301 hoverPoint.SetState(PointState::STARTED); // The first hover event received
302 mHoveredPoints.push_back(PointInfo(hoverPoint, time));
303 hoverEvent.AddPoint(hoverPoint);
307 PointInfo matchedPoint(point, time);
308 std::swap(*match, matchedPoint);
311 if(dispatchEvent == TouchEventCombiner::DISPATCH_TOUCH)
313 dispatchEvent = TouchEventCombiner::DISPATCH_BOTH;
317 dispatchEvent = TouchEventCombiner::DISPATCH_HOVER;
324 case PointState::INTERRUPTED:
328 // We should still tell core about the interruption.
329 touchEvent.AddPoint(point);
330 hoverEvent.AddPoint(point);
331 dispatchEvent = TouchEventCombiner::DISPATCH_BOTH;
339 return dispatchEvent;
342 void TouchEventCombiner::SetMinimumMotionTimeThreshold(uint32_t minTime)
344 mMinMotionTime = minTime;
347 void TouchEventCombiner::SetMinimumMotionDistanceThreshold(float minDistance)
349 DALI_ASSERT_ALWAYS(minDistance >= 0.0f && "Negative values not allowed\n");
351 mMinMotionDistance.x = mMinMotionDistance.y = minDistance;
354 void TouchEventCombiner::SetMinimumMotionDistanceThreshold(float minXDistance, float minYDistance)
356 DALI_ASSERT_ALWAYS(minXDistance >= 0.0f && minYDistance >= 0.0f && "Negative values not allowed\n");
358 mMinMotionDistance.x = minXDistance;
359 mMinMotionDistance.y = minYDistance;
362 void TouchEventCombiner::SetMinimumMotionDistanceThreshold(Vector2 minDistance)
364 DALI_ASSERT_ALWAYS(minDistance.x >= 0.0f && minDistance.y >= 0.0f && "Negative values not allowed\n");
366 mMinMotionDistance = minDistance;
369 unsigned long TouchEventCombiner::GetMinimumMotionTimeThreshold() const
371 return mMinMotionTime;
374 Vector2 TouchEventCombiner::GetMinimumMotionDistanceThreshold() const
376 return mMinMotionDistance;
379 void TouchEventCombiner::Reset()
381 mPressedPoints.clear();
382 mHoveredPoints.clear();
385 } // namespace Integration