From f93d96d4a59b6cf4c385d9c356e9a8fe2ba28310 Mon Sep 17 00:00:00 2001
From: Jorge Galvao <gapspt@users.noreply.github.com>
Date: Tue, 3 Dec 2024 22:40:30 +0200
Subject: [PATCH 1/2] Fix issue "New Input System - All mouse buttons have the
 same pointer Id"

Fixes the following bug:

When using the IPointerDownHandler (OnPointerDown method) with the New Input System, the eventData.pointerId is always the same, corresponding to the deviceId.

According to the documentation [1] for Unity 2018: "When using a mouse the pointerId returns -1, -2, or -3. These are the left, right and center mouse buttons respectively."

I mention the 2018 because the documentation on the standalone Unity UI package [2] is lacking the values for the mouse buttons.

[1] https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.PointerEventData-pointerId.html
[2] https://docs.unity3d.com/Packages/com.unity.ugui@3.0/api/UnityEngine.EventSystems.PointerEventData.html
---
 .../InputSystem/Plugins/UI/InputSystemUIInputModule.cs         | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs
index c72e043c71..88f714cb9b 100644
--- a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs
+++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs
@@ -346,6 +346,7 @@ private void ProcessPointer(ref PointerModel state)
             // Left mouse button. Movement and scrolling is processed with event set left button.
             eventData.button = PointerEventData.InputButton.Left;
             state.leftButton.CopyPressStateTo(eventData);
+            eventData.pointerId = PointerInputModule.kMouseLeftId;
 
             // Unlike StandaloneInputModule, we process moves before processing buttons. This way
             // UI elements get pointer enters/exits before they get button ups/downs and clicks.
@@ -366,6 +367,7 @@ private void ProcessPointer(ref PointerModel state)
             // Right mouse button.
             eventData.button = PointerEventData.InputButton.Right;
             state.rightButton.CopyPressStateTo(eventData);
+            eventData.pointerId = PointerInputModule.kMouseRightId;
 
             ProcessPointerButton(ref state.rightButton, eventData);
             ProcessPointerButtonDrag(ref state.rightButton, eventData);
@@ -373,6 +375,7 @@ private void ProcessPointer(ref PointerModel state)
             // Middle mouse button.
             eventData.button = PointerEventData.InputButton.Middle;
             state.middleButton.CopyPressStateTo(eventData);
+            eventData.pointerId = PointerInputModule.kMouseMiddleId;
 
             ProcessPointerButton(ref state.middleButton, eventData);
             ProcessPointerButtonDrag(ref state.middleButton, eventData);

From 5e1e1e50380e674ad76912c858b1dbff4b4164c9 Mon Sep 17 00:00:00 2001
From: Jorge Galvao <gapspt@users.noreply.github.com>
Date: Tue, 3 Dec 2024 23:36:13 +0200
Subject: [PATCH 2/2] Update InputSystemUIInputModule.cs

Restore the original pointerId, since the eventData is reused, to keep it correct at all times.
---
 .../InputSystem/Plugins/UI/InputSystemUIInputModule.cs   | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs
index 88f714cb9b..b922c52aa8 100644
--- a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs
+++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs
@@ -343,6 +343,8 @@ private void ProcessPointer(ref PointerModel state)
 
             ////REVIEW: for touch, we only need the left button; should we skip right and middle button processing? then we also don't need to copy to/from the event
 
+            var pointerId = eventData.pointerId;
+
             // Left mouse button. Movement and scrolling is processed with event set left button.
             eventData.button = PointerEventData.InputButton.Left;
             state.leftButton.CopyPressStateTo(eventData);
@@ -358,7 +360,11 @@ private void ProcessPointer(ref PointerModel state)
             // However, after that, early out at this point when there's no changes to the pointer state (except
             // for tracked pointers as the tracking origin may have moved).
             if (!state.changedThisFrame && (xrTrackingOrigin == null || state.pointerType != UIPointerType.Tracked))
+            {
+                // Restore the original pointerId
+                eventData.pointerId = pointerId;
                 return;
+            }
 
             ProcessPointerButton(ref state.leftButton, eventData);
             ProcessPointerButtonDrag(ref state.leftButton, eventData);
@@ -379,6 +385,9 @@ private void ProcessPointer(ref PointerModel state)
 
             ProcessPointerButton(ref state.middleButton, eventData);
             ProcessPointerButtonDrag(ref state.middleButton, eventData);
+
+            // Restore the original pointerId
+            eventData.pointerId = pointerId;
         }
 
         // if we are using a MultiplayerEventSystem, ignore any transforms