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()
85 TouchEventCombiner::EventDispatchType TouchEventCombiner::GetNextTouchEvent(const Point& point, uint32_t time, TouchEvent& touchEvent, HoverEvent& hoverEvent)
87 TouchEventCombiner::EventDispatchType dispatchEvent(TouchEventCombiner::DISPATCH_NONE);
88 const PointState::Type state = point.GetState();
89 const int deviceId = point.GetDeviceId();
93 case PointState::STARTED:
95 touchEvent.time = time;
96 bool addToContainer(true);
98 // Iterate through already stored touch points and add to TouchEvent
99 for(PointInfoContainer::iterator iter = mPressedPoints.begin(), endIter = mPressedPoints.end(); iter != endIter; ++iter)
101 if(iter->point.GetDeviceId() != deviceId)
103 iter->point.SetState(PointState::STATIONARY);
107 // System has sent us two down points for the same point ID, update our stored data to latest.
108 // We do not want to emit another down event for this Point Device ID.
110 addToContainer = false;
114 touchEvent.AddPoint(iter->point);
117 // Add new touch point to the list and to the TouchEvent
120 mPressedPoints.push_back(PointInfo(point, time));
121 touchEvent.AddPoint(point);
122 dispatchEvent = TouchEventCombiner::DISPATCH_TOUCH; // Only dispatch touch event if just added to container
124 // Check whether hover event was dispatched previously
125 if(!mHoveredPoints.empty())
127 hoverEvent.time = time;
129 PointInfoContainer::iterator match(mHoveredPoints.end());
130 for(PointInfoContainer::iterator iter = mHoveredPoints.begin(), endIter = mHoveredPoints.end(); iter != endIter; ++iter)
132 if(deviceId == iter->point.GetDeviceId())
135 // Add new point to the HoverEvent
136 iter->point.SetState(PointState::FINISHED);
137 hoverEvent.AddPoint(iter->point);
141 iter->point.SetState(PointState::STATIONARY);
142 hoverEvent.AddPoint(iter->point);
146 if(match != mHoveredPoints.end())
148 mHoveredPoints.erase(match);
149 dispatchEvent = TouchEventCombiner::DISPATCH_BOTH; // We should only dispatch hover events if the point was actually hovered in this window
157 case PointState::FINISHED:
159 touchEvent.time = time;
161 // Find pressed touch point in local list (while also adding the stored points to the touchEvent)
162 PointInfoContainer::iterator match(mPressedPoints.end());
163 for(PointInfoContainer::iterator iter = mPressedPoints.begin(), endIter = mPressedPoints.end(); iter != endIter; ++iter)
165 if(deviceId == iter->point.GetDeviceId())
169 // Add new point to the TouchEvent
170 touchEvent.AddPoint(point);
174 iter->point.SetState(PointState::STATIONARY);
175 touchEvent.AddPoint(iter->point);
179 if(match != mPressedPoints.end())
181 mPressedPoints.erase(match);
182 dispatchEvent = TouchEventCombiner::DISPATCH_TOUCH; // We should only dispatch touch events if the point was actually pressed in this window
184 // Iterate through already stored touch points for HoverEvent and delete them
185 for(PointInfoContainer::iterator iter = mHoveredPoints.begin(), endIter = mHoveredPoints.end(); iter != endIter; ++iter)
187 if(iter->point.GetDeviceId() == deviceId)
189 iter = mHoveredPoints.erase(iter);
196 case PointState::MOTION:
198 bool fromNewDeviceId = false;
200 if(!mPressedPoints.empty())
202 touchEvent.time = time;
205 PointInfoContainer::iterator match = mPressedPoints.end();
206 const Vector2& pointScreenPosition = point.GetScreenPosition();
207 for(PointInfoContainer::iterator iter = mPressedPoints.begin(), endIter = mPressedPoints.end(); iter != endIter; ++iter)
209 if(deviceId == iter->point.GetDeviceId())
211 uint32_t timeDiff(time - iter->time);
213 if(timeDiff < mMinMotionTime)
215 // Motion event sent too soon after previous event so ignore
220 const Vector2& currentScreenPosition = iter->point.GetScreenPosition();
221 if((std::abs(pointScreenPosition.x - currentScreenPosition.x) < mMinMotionDistance.x) &&
222 (std::abs(pointScreenPosition.y - currentScreenPosition.y) < mMinMotionDistance.y))
224 // Not enough positional change from last event so ignore
231 // Add new touch point to the TouchEvent
232 touchEvent.AddPoint(point);
236 iter->point.SetState(PointState::STATIONARY);
237 touchEvent.AddPoint(iter->point);
241 if(match != mPressedPoints.end())
243 PointInfo matchedPoint(point, time);
244 std::swap(*match, matchedPoint);
246 dispatchEvent = TouchEventCombiner::DISPATCH_TOUCH; // Dispatch touch event
250 fromNewDeviceId = true;
254 // Dispatch hover event if no previous down event received or the motion event comes from a new device ID
255 if(mPressedPoints.empty() || fromNewDeviceId)
257 hoverEvent.time = time;
259 // Iterate through already stored touch points and add to HoverEvent
261 PointInfoContainer::iterator match = mHoveredPoints.end();
262 const Vector2& pointScreenPosition = point.GetScreenPosition();
263 for(PointInfoContainer::iterator iter = mHoveredPoints.begin(), endIter = mHoveredPoints.end(); iter != endIter; ++iter)
265 if(iter->point.GetDeviceId() == deviceId)
267 uint32_t timeDiff(time - iter->time);
269 if(timeDiff < mMinMotionTime)
271 // Motion event sent too soon after previous event so ignore
276 const Vector2& currentScreenPosition = iter->point.GetScreenPosition();
277 if((std::abs(pointScreenPosition.x - currentScreenPosition.x) < mMinMotionDistance.x) &&
278 (std::abs(pointScreenPosition.y - currentScreenPosition.y) < mMinMotionDistance.y))
280 // Not enough positional change from last event so ignore
287 // Add new touch point to the HoverEvent
288 hoverEvent.AddPoint(point);
292 iter->point.SetState(PointState::STATIONARY);
293 hoverEvent.AddPoint(iter->point);
297 // Add new hover point to the list and to the HoverEvent
298 if(!ignore) // Only dispatch hover event when it should not be ignored
300 if(match == mHoveredPoints.end())
302 Point hoverPoint(point);
303 hoverPoint.SetState(PointState::STARTED); // The first hover event received
304 mHoveredPoints.push_back(PointInfo(hoverPoint, time));
305 hoverEvent.AddPoint(hoverPoint);
309 PointInfo matchedPoint(point, time);
310 std::swap(*match, matchedPoint);
313 if(dispatchEvent == TouchEventCombiner::DISPATCH_TOUCH)
315 dispatchEvent = TouchEventCombiner::DISPATCH_BOTH;
319 dispatchEvent = TouchEventCombiner::DISPATCH_HOVER;
326 case PointState::INTERRUPTED:
330 // We should still tell core about the interruption.
331 touchEvent.AddPoint(point);
332 hoverEvent.AddPoint(point);
333 dispatchEvent = TouchEventCombiner::DISPATCH_BOTH;
341 return dispatchEvent;
344 void TouchEventCombiner::SetMinimumMotionTimeThreshold(uint32_t minTime)
346 mMinMotionTime = minTime;
349 void TouchEventCombiner::SetMinimumMotionDistanceThreshold(float minDistance)
351 DALI_ASSERT_ALWAYS(minDistance >= 0.0f && "Negative values not allowed\n");
353 mMinMotionDistance.x = mMinMotionDistance.y = minDistance;
356 void TouchEventCombiner::SetMinimumMotionDistanceThreshold(float minXDistance, float minYDistance)
358 DALI_ASSERT_ALWAYS(minXDistance >= 0.0f && minYDistance >= 0.0f && "Negative values not allowed\n");
360 mMinMotionDistance.x = minXDistance;
361 mMinMotionDistance.y = minYDistance;
364 void TouchEventCombiner::SetMinimumMotionDistanceThreshold(Vector2 minDistance)
366 DALI_ASSERT_ALWAYS(minDistance.x >= 0.0f && minDistance.y >= 0.0f && "Negative values not allowed\n");
368 mMinMotionDistance = minDistance;
371 unsigned long TouchEventCombiner::GetMinimumMotionTimeThreshold() const
373 return mMinMotionTime;
376 Vector2 TouchEventCombiner::GetMinimumMotionDistanceThreshold() const
378 return mMinMotionDistance;
381 void TouchEventCombiner::Reset()
383 mPressedPoints.clear();
384 mHoveredPoints.clear();
387 } // namespace Integration