From 935923bee25dc52691eb4cce5447593f328657ab Mon Sep 17 00:00:00 2001 From: Mathieu Prevel Date: Thu, 30 Nov 2023 00:34:23 +0100 Subject: [PATCH] minor improvements --- .../src/main/scala/indigo/physics/World.scala | 2 +- .../platform/events/GlobalEventStream.scala | 2 +- .../main/scala/indigo/shared/QuickCache.scala | 5 ++-- .../shared/animation/timeline/Timeline.scala | 2 +- .../indigo/shared/collections/Batch.scala | 6 ++++ .../indigo/shared/events/EventFilters.scala | 28 ++++++------------- .../indigo/shared/events/GlobalEvent.scala | 2 +- .../indigo/shared/events/InputMapping.scala | 4 +-- .../scala/indigo/shared/input/Mouse.scala | 4 +-- .../scala/indigo/shared/input/Pointers.scala | 2 +- .../platform/DisplayObjectConversions.scala | 1 + .../indigo/shared/scenegraph/Camera.scala | 6 ++-- .../indigo/shared/scenegraph/SceneAudio.scala | 2 +- 13 files changed, 32 insertions(+), 34 deletions(-) diff --git a/indigo/indigo/src/main/scala/indigo/physics/World.scala b/indigo/indigo/src/main/scala/indigo/physics/World.scala index f2082153e..839dd9e8b 100644 --- a/indigo/indigo/src/main/scala/indigo/physics/World.scala +++ b/indigo/indigo/src/main/scala/indigo/physics/World.scala @@ -32,7 +32,7 @@ final case class World[A](colliders: Batch[Collider[A]], forces: Batch[Vector2], def removeByTag(tag: A)(using CanEqual[A, A]): World[A] = this.copy(colliders = colliders.filterNot(_.tag == tag)) - def modifyByTag(tag: A)(f: Collider[A] => Collider[A])(using CanEqual[A, A]) = + def modifyByTag(tag: A)(f: Collider[A] => Collider[A])(using CanEqual[A, A]): World[A] = this.copy(colliders = colliders.map(c => if c.tag == tag then f(c) else c)) def findFirstAt(position: Vertex): Option[Collider[A]] = diff --git a/indigo/indigo/src/main/scala/indigo/platform/events/GlobalEventStream.scala b/indigo/indigo/src/main/scala/indigo/platform/events/GlobalEventStream.scala index 9b24f79bd..12bdf6457 100644 --- a/indigo/indigo/src/main/scala/indigo/platform/events/GlobalEventStream.scala +++ b/indigo/indigo/src/main/scala/indigo/platform/events/GlobalEventStream.scala @@ -62,7 +62,7 @@ final class GlobalEventStream( } } - if errors.length > 0 then errors foreach (e => eventQueue.enqueue(e)) + if errors.nonEmpty then errors foreach (e => eventQueue.enqueue(e)) else eventQueue.enqueue(StorageEvent.KeysFound(keys.flatMap { _ match { diff --git a/indigo/indigo/src/main/scala/indigo/shared/QuickCache.scala b/indigo/indigo/src/main/scala/indigo/shared/QuickCache.scala index 5357e7771..e24dcce35 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/QuickCache.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/QuickCache.scala @@ -15,8 +15,9 @@ final class QuickCache[A](private val cache: scalajs.js.Dictionary[A]): cache.get(key.toString) def add(key: CacheKey, value: => A): A = { - cache.update(key.toString, value) - value + val v = value + cache.update(key.toString, v) + v } def fetchOrAdd(key: CacheKey, disabled: Boolean, value: => A): A = diff --git a/indigo/indigo/src/main/scala/indigo/shared/animation/timeline/Timeline.scala b/indigo/indigo/src/main/scala/indigo/shared/animation/timeline/Timeline.scala index 93d863ca1..cdc401c24 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/animation/timeline/Timeline.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/animation/timeline/Timeline.scala @@ -136,7 +136,7 @@ object Timeline: * `Seconds` */ def duration: Seconds = - tl.map(_.end).sortWith(_ > _).headOption.getOrElse(Seconds.zero) + tl.maxByOption(_.end.toDouble).fold(Seconds.zero)(_.end) /** Alias for duration, returns the total time duration of the animation in seconds, including any initial delays. * diff --git a/indigo/indigo/src/main/scala/indigo/shared/collections/Batch.scala b/indigo/indigo/src/main/scala/indigo/shared/collections/Batch.scala index 4a57e5523..866c20fe3 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/collections/Batch.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/collections/Batch.scala @@ -142,6 +142,12 @@ sealed trait Batch[+A]: def map[B](f: A => B): Batch[B] = Batch.Wrapped(_jsArray.map(f)) + def maxBy[B](f: A => B)(using ord: Ordering[B]): A = + _jsArray.maxBy(f)(ord) + + def maxByOption[B](f: A => B)(using ord: Ordering[B]): Option[A] = + Option.when(_jsArray.nonEmpty)(_jsArray.maxBy(f)(ord)) + /** Converts the batch into a String` * @return * `String` diff --git a/indigo/indigo/src/main/scala/indigo/shared/events/EventFilters.scala b/indigo/indigo/src/main/scala/indigo/shared/events/EventFilters.scala index aa560c8eb..4f9dc71bf 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/events/EventFilters.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/events/EventFilters.scala @@ -33,25 +33,15 @@ final case class EventFilters( object EventFilters { private def fromAccessControl(ac: AccessControl): GlobalEvent => Option[GlobalEvent] = { - case e: AssetEvent if ac.allowAssetEvents => Some(e) - case _: AssetEvent => None - case FrameTick if ac.allowFrameTick => Some(FrameTick) - case FrameTick => None - case e: KeyboardEvent if ac.allowKeyboardEvents => Some(e) - case _: KeyboardEvent => None - case e: MouseEvent if ac.allowMouseEvents => Some(e) - case _: MouseEvent => None - case e: NetworkReceiveEvent if ac.allowNetworkEvents => Some(e) - case _: NetworkReceiveEvent => None - case e: StorageEvent if ac.allowStorageEvents => Some(e) - case _: StorageEvent => None - case e: SubSystemEvent if ac.allowSubSystemEvents => Some(e) - case _: SubSystemEvent => None - case e: ViewEvent if ac.allowViewEvents => Some(e) - case _: ViewEvent => None - case e if ac.allowCustomEvents => Some(e) - case _ if ac.allowCustomEvents => None - case _ => None + case FrameTick => Option.when(ac.allowFrameTick)(FrameTick) + case e: KeyboardEvent => Option.when(ac.allowKeyboardEvents)(e) + case e: MouseEvent => Option.when(ac.allowMouseEvents)(e) + case e: NetworkReceiveEvent => Option.when(ac.allowNetworkEvents)(e) + case e: StorageEvent => Option.when(ac.allowStorageEvents)(e) + case e: SubSystemEvent => Option.when(ac.allowSubSystemEvents)(e) + case e: ViewEvent => Option.when(ac.allowViewEvents)(e) + case e: AssetEvent => Option.when(ac.allowAssetEvents)(e) + case e => Option.when(ac.allowCustomEvents)(e) } /** Access controlled event filters are a convienient way to have explicit control which events arrive at which 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 831ab86b0..70b6c7c74 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/events/GlobalEvent.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/events/GlobalEvent.scala @@ -150,7 +150,7 @@ trait MouseOrPointerEvent: /** Indicates whether buttons are in active state */ - def isActive: Boolean = buttons.isEmpty == false + def isActive: Boolean = !buttons.isEmpty /** Whether the `alt` key was pressed when the event was fired */ diff --git a/indigo/indigo/src/main/scala/indigo/shared/events/InputMapping.scala b/indigo/indigo/src/main/scala/indigo/shared/events/InputMapping.scala index 8b6a19486..fe1e0fd13 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/events/InputMapping.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/events/InputMapping.scala @@ -26,8 +26,8 @@ final case class InputMapping[A](oneOf: List[(Combo, A)]) { case MouseInput.MouseAt(pt) => mouse.position == pt case MouseInput.MouseButtonUp(button) => mouse.released(button) case MouseInput.MouseButtonDown(button) => mouse.pressed(button) - case MouseInput.MouseWheelDown => mouse.scrolled.exists(_ == MouseWheel.ScrollDown) - case MouseInput.MouseWheelUp => mouse.scrolled.exists(_ == MouseWheel.ScrollUp) + case MouseInput.MouseWheelDown => mouse.scrolled.contains(MouseWheel.ScrollDown) + case MouseInput.MouseWheelUp => mouse.scrolled.contains(MouseWheel.ScrollUp) } && c._1.keyInputs.forall(k => keyboard.keysDown.contains(k)) && c._1.gamepadInputs.forall { 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 6adcf7077..486271737 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala @@ -33,8 +33,8 @@ final class Mouse( case _: MouseEvent.Click => true case _ => false } - lazy val mousePressed = pressed(MouseButton.LeftMouseButton) - lazy val mouseReleased = released(MouseButton.LeftMouseButton) + lazy val mousePressed: Boolean = pressed(MouseButton.LeftMouseButton) + lazy val mouseReleased: Boolean = released(MouseButton.LeftMouseButton) def pressed(button: MouseButton): Boolean = mouseEvents.exists { 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 85ad45960..929005d46 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/input/Pointers.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/input/Pointers.scala @@ -14,7 +14,7 @@ object Pointers: val default: Pointers = Pointers(Batch.empty, Point.zero) - def calculateNext(previous: Pointers, events: Batch[PointerEvent]) = + def calculateNext(previous: Pointers, events: Batch[PointerEvent]): Pointers = Pointers( pointerEvents = events, position = lastPointerPosition(previous.position, events) diff --git a/indigo/indigo/src/main/scala/indigo/shared/platform/DisplayObjectConversions.scala b/indigo/indigo/src/main/scala/indigo/shared/platform/DisplayObjectConversions.scala index 33562480b..c6c08e766 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/platform/DisplayObjectConversions.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/platform/DisplayObjectConversions.scala @@ -939,6 +939,7 @@ object DisplayObjectConversions { cacheKey: String, disableCache: Boolean )(using QuickCache[scalajs.js.Array[Float]]): scalajs.js.Array[Float] = { + @tailrec def rec( remaining: scalajs.js.Array[ShaderPrimitive], current: scalajs.js.Array[Float], diff --git a/indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala b/indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala index 7818f9be5..f6bc28a21 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala @@ -24,8 +24,8 @@ object Camera: * while controlling the position, zoom and rotation. */ final case class Fixed(position: Point, zoom: Zoom, rotation: Radians) extends Camera: - def topLeft(viewport: GameViewport) = position - val isLookAt: Boolean = false + def topLeft(viewport: GameViewport): Point = position + val isLookAt: Boolean = false def withX(newX: Int): Fixed = this.copy(position = position.withX(newX)) @@ -68,7 +68,7 @@ object Camera: final case class LookAt(target: Point, zoom: Zoom, rotation: Radians) extends Camera: val isLookAt: Boolean = true val position: Point = target - def topLeft(viewport: GameViewport) = + def topLeft(viewport: GameViewport): Point = target - Point(viewport.width / 2, viewport.height / 2) / zoom.toDouble.toInt def withTarget(newTarget: Point): LookAt = diff --git a/indigo/indigo/src/main/scala/indigo/shared/scenegraph/SceneAudio.scala b/indigo/indigo/src/main/scala/indigo/shared/scenegraph/SceneAudio.scala index 191fd4a51..e0644080d 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/scenegraph/SceneAudio.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/scenegraph/SceneAudio.scala @@ -13,7 +13,7 @@ final case class SceneAudio( object SceneAudio: - val Mute = SceneAudio(None, None, None) + val Mute: SceneAudio = SceneAudio(None, None, None) def apply(sourceA: SceneAudioSource): SceneAudio = SceneAudio(Some(sourceA), None, None)