Skip to content

Commit

Permalink
Starts to add Pointer helper events
Browse files Browse the repository at this point in the history
  • Loading branch information
hobnob committed Oct 28, 2024
1 parent 5fb75af commit 215b0ca
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
68 changes: 58 additions & 10 deletions indigo/indigo/src/main/scala/indigo/shared/input/Pointers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 215b0ca

Please sign in to comment.