Skip to content

Commit

Permalink
Updates UI to use Pointer events rather than Mouse
Browse files Browse the repository at this point in the history
Fixes #747
  • Loading branch information
hobnob committed Nov 1, 2024
1 parent 29f6223 commit 65b37b3
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ object ConfigGen {
| advanced = AdvancedGameConfig
| .default
| .withAntiAliasing(${indigoOptions.metadata.antiAliasing.toString})
| )
| )
|""".stripMargin

Expand Down
47 changes: 25 additions & 22 deletions indigo/indigo-extras/src/main/scala/indigoextras/ui/Button.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import indigo.shared.datatypes.Point
import indigo.shared.datatypes.Rectangle
import indigo.shared.datatypes.Size
import indigo.shared.events.GlobalEvent
import indigo.shared.input.Mouse
import indigo.shared.input.PointerState
import indigo.shared.scenegraph.EntityNode
import indigo.shared.scenegraph.Graphic
import indigo.shared.scenegraph.Group
Expand Down Expand Up @@ -51,52 +51,55 @@ final case class Button(
def withDepth(newDepth: Depth): Button =
this.copy(depth = newDepth)

def update(mouse: Mouse): Outcome[Button] = {
val mouseInBounds = bounds.isPointWithin(mouse.position)
def update(pointer: PointerState): Outcome[Button] = {
val pointerInBounds = pointer.positions.exists(p => bounds.isPointWithin(p))

println(
s"Pointer in bounds: $pointerInBounds released: ${pointer.isReleased} clicked: ${pointer.isClicked} pressed: ${pointer.isPressed}"
)
val upEvents: Batch[GlobalEvent] =
if mouseInBounds && mouse.mouseReleased then onUp()
if pointerInBounds && pointer.released then onUp()
else Batch.empty

val clickEvents: Batch[GlobalEvent] =
if mouseInBounds && mouse.mouseClicked then onClick()
if pointerInBounds && pointer.isClicked then onClick()
else Batch.empty

val downEvents: Batch[GlobalEvent] =
if mouseInBounds && mouse.mousePressed then onDown()
if pointerInBounds && pointer.pressed then onDown()
else Batch.empty

val mouseButtonEvents: Batch[GlobalEvent] =
val pointerButtonEvents: Batch[GlobalEvent] =
downEvents ++ upEvents ++ clickEvents

state match
// Stay in Down state
case ButtonState.Down if mouseInBounds && mouse.isLeftDown =>
Outcome(this).addGlobalEvents(onHoldDown() ++ mouseButtonEvents)
case ButtonState.Down if pointerInBounds && pointer.isLeftDown =>
Outcome(this).addGlobalEvents(onHoldDown() ++ pointerButtonEvents)

// Move to Down state
case ButtonState.Up if mouseInBounds && mouse.mousePressed =>
Outcome(toDownState).addGlobalEvents(onHoverOver() ++ mouseButtonEvents)
case ButtonState.Up if pointerInBounds && pointer.pressed =>
Outcome(toDownState).addGlobalEvents(onHoverOver() ++ pointerButtonEvents)

case ButtonState.Over if mouseInBounds && mouse.mousePressed =>
Outcome(toDownState).addGlobalEvents(mouseButtonEvents)
case ButtonState.Over if pointerInBounds && pointer.pressed =>
Outcome(toDownState).addGlobalEvents(pointerButtonEvents)

// Out of Down state
case ButtonState.Down if mouseInBounds && !mouse.isLeftDown =>
Outcome(toOverState).addGlobalEvents(onHoverOver() ++ mouseButtonEvents)
case ButtonState.Down if pointerInBounds && !pointer.isLeftDown =>
Outcome(toOverState).addGlobalEvents(onHoverOver() ++ pointerButtonEvents)

case ButtonState.Down if !mouseInBounds && !mouse.isLeftDown =>
Outcome(toUpState).addGlobalEvents(onHoverOut() ++ mouseButtonEvents)
case ButtonState.Down if !pointerInBounds && !pointer.isLeftDown =>
Outcome(toUpState).addGlobalEvents(onHoverOut() ++ pointerButtonEvents)

//
case ButtonState.Up if mouseInBounds =>
Outcome(toOverState).addGlobalEvents(onHoverOver() ++ mouseButtonEvents)
case ButtonState.Up if pointerInBounds =>
Outcome(toOverState).addGlobalEvents(onHoverOver() ++ pointerButtonEvents)

case ButtonState.Over if !mouseInBounds =>
Outcome(toUpState).addGlobalEvents(onHoverOut() ++ mouseButtonEvents)
case ButtonState.Over if !pointerInBounds =>
Outcome(toUpState).addGlobalEvents(onHoverOut() ++ pointerButtonEvents)

case _ =>
Outcome(this).addGlobalEvents(mouseButtonEvents)
Outcome(this).addGlobalEvents(pointerButtonEvents)
}

private def applyPositionAndDepth(sceneNode: SceneNode, pt: Point, d: Depth): SceneNode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@ final case class HitArea(
onHoldDown: () => Batch[GlobalEvent]
) derives CanEqual:

def update(pointers: PointerState): Outcome[HitArea] = {
val pointerInBounds = pointers.positions.exists(p => area.contains(Vertex.fromPoint(p)))
def update(pointer: PointerState): Outcome[HitArea] = {
val pointerInBounds = pointer.positions.exists(p => area.contains(Vertex.fromPoint(p)))

val upEvents: Batch[GlobalEvent] =
if pointerInBounds && pointers.released then onUp()
if pointerInBounds && pointer.released then onUp()
else Batch.empty

val clickEvents: Batch[GlobalEvent] =
if pointerInBounds && pointers.isClicked then onClick()
if pointerInBounds && pointer.isClicked then onClick()
else Batch.empty

val downEvents: Batch[GlobalEvent] =
if pointerInBounds && pointers.pressed then onDown()
if pointerInBounds && pointer.pressed then onDown()
else Batch.empty

val pointerButtonEvents: Batch[GlobalEvent] =
downEvents ++ upEvents ++ clickEvents

state match
case ButtonState.Down if pointerInBounds && pointers.isLeftDown =>
case ButtonState.Down if pointerInBounds && pointer.isLeftDown =>
Outcome(this).addGlobalEvents(onHoldDown() ++ pointerButtonEvents)

case ButtonState.Up if pointerInBounds =>
Outcome(toOverState).addGlobalEvents(onHoverOver() ++ pointerButtonEvents)

case ButtonState.Over if pointerInBounds && pointers.pressed =>
case ButtonState.Over if pointerInBounds && pointer.pressed =>
Outcome(toDownState).addGlobalEvents(pointerButtonEvents)

case ButtonState.Down if pointerInBounds && !pointers.isLeftDown =>
case ButtonState.Down if pointerInBounds && !pointer.isLeftDown =>
Outcome(toOverState).addGlobalEvents(onHoverOver() ++ pointerButtonEvents)

case ButtonState.Over if !pointerInBounds =>
Expand Down
Loading

0 comments on commit 65b37b3

Please sign in to comment.