Skip to content

Commit

Permalink
refactor TouchPanelStrategy.AddEvent(...) (#2220)
Browse files Browse the repository at this point in the history
* rename nativeTouchId

* inline LegacyAddEvent(...)

* TouchPanelStrategy Add events

* AddCanceledEvent(...)

* non virtual InvalidateTouches()
  • Loading branch information
nkast authored Feb 17, 2025
1 parent 7560487 commit bc86fe7
Show file tree
Hide file tree
Showing 16 changed files with 391 additions and 170 deletions.
10 changes: 5 additions & 5 deletions Platforms/Game/.Android/TouchEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool View.IOnTouchListener.OnTouch(View v, MotionEvent e)
// DOWN
case MotionEventActions.Down:
case MotionEventActions.PointerDown:
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Pressed, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddPressedEvent(id, position);
break;

// MOVE
Expand All @@ -44,22 +44,22 @@ bool View.IOnTouchListener.OnTouch(View v, MotionEvent e)
id = e.GetPointerId(i);
position.X = e.GetX(i);
position.Y = e.GetY(i);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Moved, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddMovedEvent(id, position);
}
break;

// UP
case MotionEventActions.Up:
case MotionEventActions.PointerUp:
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Released, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddReleasedEvent(id, position);
break;

// OUTSIDE
case MotionEventActions.Outside:
for (int i = 0; i < e.PointerCount; i++)
{
id = e.GetPointerId(i);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Released, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddReleasedEvent(id, position);
}
break;

Expand All @@ -68,7 +68,7 @@ bool View.IOnTouchListener.OnTouch(View v, MotionEvent e)
for (int i = 0; i < e.PointerCount; i++)
{
id = e.GetPointerId(i);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Released, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddCanceledEvent(id, position);
}
break;
}
Expand Down
6 changes: 3 additions & 3 deletions Platforms/Game/.Blazor/BlazorGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ internal BlazorGameWindow(ConcreteGame concreteGame)
TouchPanel.WindowHandle = this.Handle;
_window.OnTouchStart += (object sender, float x, float y, int identifier) =>
{
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(identifier, TouchLocationState.Pressed, new Vector2(x, y));
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddPressedEvent(identifier, new Vector2(x, y));
};
_window.OnTouchMove += (object sender, float x, float y, int identifier) =>
{
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(identifier, TouchLocationState.Moved, new Vector2(x, y));
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddMovedEvent(identifier, new Vector2(x, y));
};
_window.OnTouchEnd += (object sender, float x, float y, int identifier) =>
{
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(identifier, TouchLocationState.Released, new Vector2(x, y));
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddReleasedEvent(identifier, new Vector2(x, y));
};
_window.OnTouchCancel += (object sender) =>
{
Expand Down
8 changes: 4 additions & 4 deletions Platforms/Game/.UAP/InputEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void PointerPressed(PointerPoint pointerPoint, UIElement target, Pointer
bool isTouch = pointerPoint.PointerDevice.PointerDeviceType == PointerDeviceType.Touch;

if (isTouch)
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent((int)pointerPoint.PointerId, TouchLocationState.Pressed, pos);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddPressedEvent((int)pointerPoint.PointerId, pos);

if (!isTouch)
{
Expand All @@ -226,7 +226,7 @@ private void PointerMoved(PointerPoint pointerPoint)

if (isTouch && touchIsDown)
{
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent((int)pointerPoint.PointerId, TouchLocationState.Moved, pos);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddMovedEvent((int)pointerPoint.PointerId, pos);
}

if (!isTouch)
Expand All @@ -243,7 +243,7 @@ private void PointerReleased(PointerPoint pointerPoint, UIElement target, Pointe
bool isTouch = pointerPoint.PointerDevice.PointerDeviceType == PointerDeviceType.Touch;

if (isTouch)
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent((int)pointerPoint.PointerId, TouchLocationState.Released, pos);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddReleasedEvent((int)pointerPoint.PointerId, pos);

if (!isTouch)
{
Expand All @@ -263,7 +263,7 @@ private void PointerCanceled(PointerPoint pointerPoint, UIElement target, Pointe
bool isTouch = pointerPoint.PointerDevice.PointerDeviceType == PointerDeviceType.Touch;

if (isTouch)
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent((int)pointerPoint.PointerId, TouchLocationState.Released, pos);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddCanceledEvent((int)pointerPoint.PointerId, pos);

if (!isTouch)
{
Expand Down
6 changes: 3 additions & 3 deletions Platforms/Game/.WindowsDX11/WinFormsGameForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected override void WndProc(ref Message m)
var position = m.GetPointerLocation();
position = PointToClient(position);
Vector2 vec = new Vector2(position.X, position.Y);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Pressed, vec);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddPressedEvent(id, vec);
}
break;
case WM_POINTERUPDATE:
Expand All @@ -207,7 +207,7 @@ protected override void WndProc(ref Message m)
var position = m.GetPointerLocation();
position = PointToClient(position);
Vector2 vec = new Vector2(position.X, position.Y);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Moved, vec);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddMovedEvent(id, vec);
}
break;
case WM_POINTERUP:
Expand All @@ -216,7 +216,7 @@ protected override void WndProc(ref Message m)
var position = m.GetPointerLocation();
position = PointToClient(position);
Vector2 vec = new Vector2(position.X, position.Y);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddEvent(id, TouchLocationState.Released, vec);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<ConcreteTouchPanel>().AddReleasedEvent(id, vec);
}
break;

Expand Down
8 changes: 4 additions & 4 deletions Platforms/Game/.iOS/iOSGameView_Touch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ private void FillTouchCollection(NSSet touches)
switch (touch.Phase)
{
case UITouchPhase.Began:
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddEvent(id, TouchLocationState.Pressed, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddPressedEvent(id, position);
break;
//case UITouchPhase.Stationary:
case UITouchPhase.Moved:
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddEvent(id, TouchLocationState.Moved, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddMovedEvent(id, position);
break;
case UITouchPhase.Ended:
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddEvent(id, TouchLocationState.Released, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddReleasedEvent(id, position);
break;
case UITouchPhase.Cancelled:
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddEvent(id, TouchLocationState.Released, position);
((IPlatformTouchPanel)TouchPanel.Current).GetStrategy<TouchPanelStrategy>().AddCanceledEvent(id, position);
break;
default:
break;
Expand Down
42 changes: 38 additions & 4 deletions Platforms/Input/.Android/Touch/ConcreteTouchPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,56 @@ public override GestureSample ReadGesture()
return base.ReadGesture();
}

public override void AddEvent(int id, TouchLocationState state, Vector2 position)
public override void AddPressedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = AndroidGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddPressedEvent(nativeTouchId, position, winSize);
}
}

public override void AddMovedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = AndroidGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);
base.LegacyAddEvent(id, state, position, winSize);

base.AddMovedEvent(nativeTouchId, position, winSize);
}
}

public override void AddReleasedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = AndroidGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

public override void InvalidateTouches()
base.AddReleasedEvent(nativeTouchId, position, winSize);
}
}

public override void AddCanceledEvent(int nativeTouchId, Vector2 position)
{
base.InvalidateTouches();
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = AndroidGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddReleasedEvent(nativeTouchId, position, winSize);
}
}

}
Expand Down
41 changes: 37 additions & 4 deletions Platforms/Input/.Blazor/Touch/ConcreteTouchPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,55 @@ public override GestureSample ReadGesture()
return base.ReadGesture();
}

public override void AddEvent(int id, TouchLocationState state, Vector2 position)
public override void AddPressedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = BlazorGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddPressedEvent(nativeTouchId, position, winSize);
}
}

public override void AddMovedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = BlazorGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);
base.LegacyAddEvent(id, state, position, winSize);

base.AddMovedEvent(nativeTouchId, position, winSize);
}
}

public override void InvalidateTouches()
public override void AddReleasedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = BlazorGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddReleasedEvent(nativeTouchId, position, winSize);
}
}
public override void AddCanceledEvent(int nativeTouchId, Vector2 position)
{
base.InvalidateTouches();
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = BlazorGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddReleasedEvent(nativeTouchId, position, winSize);
}
}

}
Expand Down
14 changes: 12 additions & 2 deletions Platforms/Input/.Ref/Touch/ConcreteTouchPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,22 @@ public override GestureSample ReadGesture()
throw new PlatformNotSupportedException();
}

public override void AddEvent(int id, TouchLocationState state, Vector2 position)
public override void AddPressedEvent(int nativeTouchId, Vector2 position)
{
throw new PlatformNotSupportedException();
}

public override void InvalidateTouches()
public override void AddMovedEvent(int nativeTouchId, Vector2 position)
{
throw new PlatformNotSupportedException();
}

public override void AddReleasedEvent(int nativeTouchId, Vector2 position)
{
throw new PlatformNotSupportedException();
}

public override void AddCanceledEvent(int nativeTouchId, Vector2 position)
{
throw new PlatformNotSupportedException();
}
Expand Down
41 changes: 37 additions & 4 deletions Platforms/Input/.SDL2/Touch/ConcreteTouchPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,55 @@ public override GestureSample ReadGesture()
return base.ReadGesture();
}

public override void AddEvent(int id, TouchLocationState state, Vector2 position)
public override void AddPressedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = SdlGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddPressedEvent(nativeTouchId, position, winSize);
}
}

public override void AddMovedEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = SdlGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);
base.LegacyAddEvent(id, state, position, winSize);

base.AddMovedEvent(nativeTouchId, position, winSize);
}
}

public override void InvalidateTouches()
public override void AddReleasedEvent(int nativeTouchId, Vector2 position)
{
base.InvalidateTouches();
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = SdlGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddReleasedEvent(nativeTouchId, position, winSize);
}
}
public override void AddCanceledEvent(int nativeTouchId, Vector2 position)
{
IntPtr wndHandle = this.WindowHandle;
if (wndHandle != IntPtr.Zero)
{
GameWindow gameWindow = SdlGameWindow.FromHandle(wndHandle);
Rectangle windowsBounds = gameWindow.ClientBounds;
Point winSize = new Point(windowsBounds.Width, windowsBounds.Height);

base.AddReleasedEvent(nativeTouchId, position, winSize);
}
}

}
Expand Down
Loading

0 comments on commit bc86fe7

Please sign in to comment.