From 215b0ca147f58dd82b48007e139a84e74b048721 Mon Sep 17 00:00:00 2001 From: David North Date: Mon, 28 Oct 2024 20:55:09 +0000 Subject: [PATCH] Starts to add Pointer helper events --- .../scala/indigo/shared/FrameContext.scala | 1 + .../scala/indigo/shared/input/Pointers.scala | 68 ++++++++++++++++--- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/indigo/indigo/src/main/scala/indigo/shared/FrameContext.scala b/indigo/indigo/src/main/scala/indigo/shared/FrameContext.scala index e76dd1d9b..e436aef56 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/FrameContext.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/FrameContext.scala @@ -39,5 +39,6 @@ final class FrameContext[StartUpData]( export inputState.mouse export inputState.keyboard export inputState.gamepad + export inputState.pointers export boundaryLocator.findBounds export boundaryLocator.bounds diff --git a/indigo/indigo/src/main/scala/indigo/shared/input/Pointers.scala b/indigo/indigo/src/main/scala/indigo/shared/input/Pointers.scala index 929005d46..d660539d0 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/input/Pointers.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/input/Pointers.scala @@ -4,21 +4,69 @@ import indigo.shared.collections.Batch import indigo.shared.datatypes.Point import indigo.shared.datatypes.Rectangle import indigo.shared.events.PointerEvent +import indigo.shared.events.PointerEvent.PointerId +import indigo.shared.collections.Batch.toBatch +import indigo.MouseButton final class Pointers( - val pointerEvents: Batch[PointerEvent], - val position: Point -) + private val pointerBatch: Batch[Pointer], + private val pointerEvents: Batch[PointerEvent] +) { + def isButtonDown(button: MouseButton): Boolean = pointerBatch.map(_.buttonsDown).contains(button) + + lazy val isLeftDown: Boolean = isButtonDown(MouseButton.LeftMouseButton) + lazy val isRightDown: Boolean = isButtonDown(MouseButton.RightMouseButton) + lazy val isMiddleDown: Boolean = isButtonDown(MouseButton.MiddleMouseButton) + + lazy val pointerPressed: Boolean = pressed(MouseButton.LeftMouseButton) + lazy val pointerReleased: Boolean = released(MouseButton.LeftMouseButton) + + def pressed(button: MouseButton): Boolean = + pointerEvents.exists { + case md: PointerEvent.PointerDown if md.button == Some(button) => true + case _ => false + } + + def released(button: MouseButton): Boolean = + pointerEvents.exists { + case mu: PointerEvent.PointerUp if mu.button == Some(button) => true + case _ => false + } +} object Pointers: val default: Pointers = - Pointers(Batch.empty, Point.zero) + Pointers(Batch.empty, Batch.empty) def calculateNext(previous: Pointers, events: Batch[PointerEvent]): Pointers = - Pointers( - pointerEvents = events, - position = lastPointerPosition(previous.position, events) - ) + Pointers(updatePointers(events, previous), events) + + private def updatePointers(events: Batch[PointerEvent], previous: Pointers): Batch[Pointer] = + events + .map(_ match { + case e: PointerEvent.PointerCancel => None + case e => Some(e) + }) + .collect { case Some(e) => e } + .map(e => updatePointer(e, previous.pointerBatch.find(_.id == e.pointerId))) + + private def updatePointer(event: PointerEvent, previous: Option[Pointer]): Pointer = + previous match + case None => + Pointer( + event.pointerId, + event match { + case e: PointerEvent.PointerDown => e.buttons + case _ => Batch.empty + }, + event.position + ) + + case Some(p) => + Pointer( + p.id, + event.buttons, + event.position + ) - private def lastPointerPosition(previous: Point, events: Batch[PointerEvent]): Point = - events.collect { case mp: PointerEvent.PointerMove => mp.position }.lastOption.fold(previous)(identity) +final case class Pointer(id: PointerId, buttonsDown: Batch[MouseButton], position: Point)