Browse Source

Normalize touch ID's to touch indices to ensure the first touch always works as the "left mousebutton" in the UI.

Lasse Öörni 11 years ago
parent
commit
aa8faf79ec
2 changed files with 18 additions and 3 deletions
  1. 16 3
      Source/Engine/UI/UI.cpp
  2. 2 0
      Source/Engine/UI/UI.h

+ 16 - 3
Source/Engine/UI/UI.cpp

@@ -1481,7 +1481,7 @@ void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
     usingTouchInput_ = true;
 
-    int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
+    int touchId = TOUCHID_MASK(GetTouchIndexFromID(eventData[P_TOUCHID].GetInt()));
     WeakPtr<UIElement> element(GetElementAt(pos));
 
     if (element)
@@ -1498,7 +1498,7 @@ void UI::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
     using namespace TouchEnd;
 
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
-    int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
+    int touchId = TOUCHID_MASK(GetTouchIndexFromID(eventData[P_TOUCHID].GetInt()));
 
     // Transmit hover end to the position where the finger was lifted
     WeakPtr<UIElement> element(GetElementAt(pos));
@@ -1527,7 +1527,7 @@ void UI::HandleTouchMove(StringHash eventType, VariantMap& eventData)
     IntVector2 deltaPos(eventData[P_DX].GetInt(), eventData[P_DY].GetInt());
     usingTouchInput_ = true;
 
-    int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
+    int touchId = TOUCHID_MASK(GetTouchIndexFromID(eventData[P_TOUCHID].GetInt()));
 
     ProcessMove(pos, deltaPos, touchId, 0, 0, true);
 }
@@ -1736,6 +1736,19 @@ IntVector2 UI::SumTouchPositions(UI::DragData* dragData, const IntVector2& oldSe
     return sendPos;
 }
 
+unsigned UI::GetTouchIndexFromID(int touchID)
+{
+    Input* input = GetSubsystem<Input>();
+    unsigned numTouches = input->GetNumTouches();
+    for (unsigned i = 0; i < numTouches; ++i)
+    {
+        TouchState* touch = input->GetTouch(i);
+        if (touch->touchID_ == touchID)
+            return i;
+    }
+    return 0; // Should not happen
+}
+
 void RegisterUILibrary(Context* context)
 {
     Font::RegisterObject(context);

+ 2 - 0
Source/Engine/UI/UI.h

@@ -229,6 +229,8 @@ private:
     void ProcessDragCancel();
     /// Sum touch positions and return the begin position ready to send.
     IntVector2 SumTouchPositions(UI::DragData* dragData, const IntVector2& oldSendPos);
+    /// Get the index of a touch based on the touch ID.
+    unsigned GetTouchIndexFromID(int touchID);
 
     /// Graphics subsystem.
     WeakPtr<Graphics> graphics_;