Skip to content

Commit

Permalink
Fixed #612: Friendly mouse event extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 7, 2023
1 parent 95ef833 commit 2333cc0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ object MouseEvent:
movementPosition = Point.zero,
button = MouseButton.LeftMouseButton
)
def unapply(e: Click): Option[Point] =
Option(e.position)

/** The mouse button was released.
* @param button
Expand Down Expand Up @@ -272,6 +274,8 @@ object MouseEvent:
movementPosition = Point.zero,
button = button
)
def unapply(e: MouseUp): Option[Point] =
Option(e.position)

/** The mouse button was pressed down.
* @param button
Expand Down Expand Up @@ -321,6 +325,8 @@ object MouseEvent:
movementPosition = Point.zero,
button = button
)
def unapply(e: MouseDown): Option[Point] =
Option(e.position)

/** The mouse was moved to a new position.
*/
Expand All @@ -344,6 +350,8 @@ object MouseEvent:
isShiftKeyDown = false,
movementPosition = Point.zero
)
def unapply(e: Move): Option[Point] =
Option(e.position)

/** Mouse has moved into canvas hit test boundaries. It's counterpart is [[Leave]].
*/
Expand All @@ -356,6 +364,9 @@ object MouseEvent:
isShiftKeyDown: Boolean,
movementPosition: Point
) extends MouseEvent
object Enter:
def unapply(e: Enter): Option[Point] =
Option(e.position)

/** Mouse has left canvas hit test boundaries. It's counterpart is [[Enter]].
*/
Expand All @@ -368,6 +379,9 @@ object MouseEvent:
isShiftKeyDown: Boolean,
movementPosition: Point
) extends MouseEvent
object Leave:
def unapply(e: Leave): Option[Point] =
Option(e.position)

/** The mouse wheel was rotated a certain amount into the Y axis.
*
Expand Down Expand Up @@ -396,6 +410,8 @@ object MouseEvent:
movementPosition = Point.zero,
amount = amount
)
def unapply(e: Wheel): Option[(Point, Double)] =
Option((e.position, e.amount))

end MouseEvent

Expand Down Expand Up @@ -479,6 +495,9 @@ object PointerEvent:
pointerType: PointerType,
isPrimary: Boolean
) extends PointerEvent
object PointerEnter:
def unapply(e: PointerEnter): Option[Point] =
Option(e.position)

/** Pointing device left canvas hit test boundaries. It's counterpart is [[PointerEnter]].
*/
Expand All @@ -501,6 +520,9 @@ object PointerEvent:
pointerType: PointerType,
isPrimary: Boolean
) extends PointerEvent
object PointerLeave:
def unapply(e: PointerLeave): Option[Point] =
Option(e.position)

/** Pointing device is in active buttons state.
*/
Expand All @@ -524,6 +546,9 @@ object PointerEvent:
isPrimary: Boolean,
button: Option[MouseButton]
) extends PointerEvent
object PointerDown:
def unapply(e: PointerDown): Option[Point] =
Option(e.position)

/** Pointing device is no longer in active buttons state.
*/
Expand All @@ -547,6 +572,9 @@ object PointerEvent:
isPrimary: Boolean,
button: Option[MouseButton]
) extends PointerEvent
object PointerUp:
def unapply(e: PointerUp): Option[Point] =
Option(e.position)

/** Pointing device changed coordinates.
*/
Expand All @@ -569,6 +597,9 @@ object PointerEvent:
pointerType: PointerType,
isPrimary: Boolean
) extends PointerEvent
object PointerMove:
def unapply(e: PointerMove): Option[Point] =
Option(e.position)

/** The ongoing interactions was cancelled due to:
* - the pointer device being disconnected
Expand All @@ -595,6 +626,9 @@ object PointerEvent:
pointerType: PointerType,
isPrimary: Boolean
) extends PointerEvent
object PointerCancel:
def unapply(e: PointerCancel): Option[Point] =
Option(e.position)

/** Represents all keyboard events
*/
Expand Down
10 changes: 5 additions & 5 deletions indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class Mouse(

lazy val scrolled: Option[MouseWheel] =
val amount = mouseEvents.foldLeft(0d) {
case (acc, MouseEvent.Wheel(_, _, _, _, _, _, _, deltaY)) => acc + deltaY
case (acc, MouseEvent.Wheel(_, deltaY)) => acc + deltaY
case (acc, _) => acc
}

Expand Down Expand Up @@ -158,10 +158,10 @@ object Mouse:
remaining match
case Nil =>
buttonsDownAcc
case MouseEvent.MouseDown(_, _, _, _, _, _, _, button) :: moreEvents =>
rec(moreEvents, buttonsDownAcc + button)
case MouseEvent.MouseUp(_, _, _, _, _, _, _, button) :: moreEvents =>
rec(moreEvents, buttonsDownAcc - button)
case (e: MouseEvent.MouseDown) :: moreEvents =>
rec(moreEvents, buttonsDownAcc + e.button)
case (e: MouseEvent.MouseUp) :: moreEvents =>
rec(moreEvents, buttonsDownAcc - e.button)
case _ :: moreEvents =>
rec(moreEvents, buttonsDownAcc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object SandboxView:
Text("AB!\n!C", 100, 2, 5, Fonts.fontKey, SandboxAssets.fontMaterial.withAlpha(0.5)).alignCenter,
Text("AB!\n!C", 200, 2, 5, Fonts.fontKey, SandboxAssets.fontMaterial.withAlpha(0.5)).alignRight
.withEventHandler {
case (txt, MouseEvent.Click(pt, _, _, _, _, _, _, _)) if bl.bounds(txt).contains(pt) =>
case (txt, MouseEvent.Click(pt)) if bl.bounds(txt).contains(pt) =>
println("Clicked me!")
None

Expand Down

0 comments on commit 2333cc0

Please sign in to comment.