void Update() { if (isWinTouchDevice && hHook != 0) { // clean previous touches List<W7Touch> touchesToDelete = new List<W7Touch>(); // remove duplicate touch ids (down + move, move + move, ...) Dictionary<uint, W7Touch> duplicateTouches = new Dictionary<uint, W7Touch>(); foreach (W7Touch touch in touches) { if (duplicateTouches.ContainsKey(touch.Id)) { touchesToDelete.Add(duplicateTouches[touch.Id]); duplicateTouches[touch.Id] = touch; } else { duplicateTouches.Add(touch.Id, touch); } } foreach (W7Touch touch in touchesToDelete) { touches.Remove(touch); } touchesToDelete.Clear(); // remove ended touches foreach (W7Touch touch in touches) { if (touch.Phase == TouchPhase.Ended || touch.Phase == TouchPhase.Canceled) { touchesToDelete.Add(touch); touchIdMap.Remove(touch.Id); } else if (touch.Phase == TouchPhase.Began) { touch.Phase = TouchPhase.Stationary; } } foreach (W7Touch touch in touchesToDelete) { touches.Remove(touch); } touchesToDelete.Clear(); lock (rawTouchesQueue) { while (rawTouchesQueue.Count > 0) { TOUCHINPUT[] rawTouches = rawTouchesQueue.Dequeue(); for (uint i = 0; i < rawTouches.Length; ++i) { TOUCHINPUT rawTouch = rawTouches[i]; //Debug.Log(rawTouch.dwID + ": " + rawTouch.dwFlags + " at " + rawTouch.x + "," + rawTouch.y); if (!touchIdMap.ContainsKey(rawTouch.dwID)) { touchIdMap.Add(rawTouch.dwID, lastTouchId); lastTouchId++; } W7Touch touch; try { touch = touches.Find(t => t.Id == touchIdMap[rawTouch.dwID]); } catch { touch = null; } Vector2 pos = new Vector2(rawTouch.x * 0.01f, rawTouch.y * 0.01f); if (touch != null) { if ((rawTouch.dwFlags & TOUCHEVENTF_DOWN) == TOUCHEVENTF_DOWN) { // duplicate down event, ignore continue; } else if ((rawTouch.dwFlags & TOUCHEVENTF_MOVE) == TOUCHEVENTF_MOVE) { if (touch.Phase == TouchPhase.Canceled || touch.Phase == TouchPhase.Ended) { // moving an ended touch? do nothing, which means resume movement touch.UpdateTouch(pos); } else if (touch.Phase == TouchPhase.Began) { // preserve began phase but update position; touch.UpdateTouch(pos); touch.Phase = TouchPhase.Began; } else { touch.UpdateTouch(pos); } } else if ((rawTouch.dwFlags & TOUCHEVENTF_UP) == TOUCHEVENTF_UP) { touch.EndTouch(); } } else { touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touches.Add(touch); if ((rawTouch.dwFlags & TOUCHEVENTF_DOWN) == TOUCHEVENTF_DOWN) { } else if ((rawTouch.dwFlags & TOUCHEVENTF_MOVE) == TOUCHEVENTF_MOVE) { // move without down? add a new movement touch touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touch.UpdateTouch(pos); touches.Add(touch); } else if ((rawTouch.dwFlags & TOUCHEVENTF_UP) == TOUCHEVENTF_UP) { // up without down? add a new end touch touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touch.EndTouch(); touches.Add(touch); } } } } } foreach (W7Touch touch in touches) { touch.Update(); if (touch.Phase == TouchPhase.Canceled) { var k = (from t in touchIdMap where t.Value == touch.Id select t.Key); if (k.Count() > 0) { touchIdMap.Remove(k.First()); } } } } }
void Update() { if (hHook != 0) { // clean previous touches List<W7Touch> touchesToDelete = new List<W7Touch>(); // remove duplicate touch ids (down + move, move + move, ...) Dictionary<uint, W7Touch> duplicateTouches = new Dictionary<uint, W7Touch>(); foreach (W7Touch touch in touches) { if (duplicateTouches.ContainsKey(touch.Id)) { touchesToDelete.Add(duplicateTouches[touch.Id]); duplicateTouches[touch.Id] = touch; } else { duplicateTouches.Add(touch.Id, touch); } } foreach (W7Touch touch in touchesToDelete) { touches.Remove(touch); } touchesToDelete.Clear(); // remove ended touches foreach (W7Touch touch in touches) { if (touch.Phase == TouchPhase.Ended || touch.Phase == TouchPhase.Canceled) { touchesToDelete.Add(touch); } } foreach (W7Touch touch in touchesToDelete) { touches.Remove(touch); } touchesToDelete.Clear(); lock (rawTouchesQueue) { while (rawTouchesQueue.Count > 0) { TOUCHINPUT[] rawTouches = rawTouchesQueue.Dequeue(); for (uint i = 0; i < rawTouches.Length; ++i) { TOUCHINPUT rawTouch = rawTouches[i]; if (!touchIdMap.ContainsKey(rawTouch.dwID)) { touchIdMap.Add(rawTouch.dwID, lastTouchId); lastTouchId++; } W7Touch touch; try { touch = touches.Find(t => t.Id == touchIdMap[rawTouch.dwID]); } catch { touch = null; } Vector2 pos = new Vector2(rawTouch.x * 0.01f, rawTouch.y * 0.01f); if (touch != null) { if ((rawTouch.dwFlags & TOUCHEVENTF_DOWN) == TOUCHEVENTF_DOWN) { touch.EndTouch(); touchIdMap.Add(rawTouch.dwID, lastTouchId); lastTouchId++; touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touches.Add(touch); } else if ((rawTouch.dwFlags & TOUCHEVENTF_MOVE) == TOUCHEVENTF_MOVE) { if (touch.Phase == TouchPhase.Moved || touch.Phase == TouchPhase.Stationary) { touch.UpdateTouch(pos); } else { touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touch.UpdateTouch(pos); touches.Add(touch); } } else if ((rawTouch.dwFlags & TOUCHEVENTF_UP) == TOUCHEVENTF_UP) { touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touch.EndTouch(); touches.Add(touch); touchIdMap.Remove(rawTouch.dwID); } } else { touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touches.Add(touch); if ((rawTouch.dwFlags & TOUCHEVENTF_DOWN) == TOUCHEVENTF_DOWN) { } else if ((rawTouch.dwFlags & TOUCHEVENTF_MOVE) == TOUCHEVENTF_MOVE) { touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touch.UpdateTouch(pos); touches.Add(touch); } else if ((rawTouch.dwFlags & TOUCHEVENTF_UP) == TOUCHEVENTF_UP) { touch = new W7Touch(touchIdMap[rawTouch.dwID], pos); touch.EndTouch(); touches.Add(touch); touchIdMap.Remove(rawTouch.dwID); } } } } } foreach (W7Touch touch in touches) { touch.Update(); if (touch.Phase == TouchPhase.Canceled) { var k = (from t in touchIdMap where t.Value == touch.Id select t.Key); if (k.Count() > 0) { touchIdMap.Remove(k.First()); } } } } }