From 2333cc08a2d3fd61dc35e7d95a154e6f6aaf6dc7 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Sun, 5 Nov 2023 12:59:47 +0000 Subject: [PATCH] Fixed #612: Friendly mouse event extractors --- .../indigo/shared/events/GlobalEvent.scala | 34 +++++++++++++++++++ .../scala/indigo/shared/input/Mouse.scala | 10 +++--- .../com/example/sandbox/SandboxView.scala | 2 +- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/indigo/indigo/src/main/scala/indigo/shared/events/GlobalEvent.scala b/indigo/indigo/src/main/scala/indigo/shared/events/GlobalEvent.scala index 1c1e1cf82..831ab86b0 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/events/GlobalEvent.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/events/GlobalEvent.scala @@ -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 @@ -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 @@ -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. */ @@ -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]]. */ @@ -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]]. */ @@ -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. * @@ -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 @@ -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]]. */ @@ -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. */ @@ -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. */ @@ -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. */ @@ -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 @@ -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 */ diff --git a/indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala b/indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala index 17468986f..14ac57808 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala @@ -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 } @@ -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) diff --git a/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxView.scala b/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxView.scala index 571134a23..bde044f17 100644 --- a/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxView.scala +++ b/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxView.scala @@ -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