From f358c4565df12ca580803273822a68b81e6138f4 Mon Sep 17 00:00:00 2001 From: David Lebl <81749624+david-lebl-adastra@users.noreply.github.com> Date: Fri, 8 Nov 2024 17:48:13 +0100 Subject: [PATCH] GameViewport is an opaque type (#778) * feat: deprecate GameViewport.scala * Revert "feat: deprecate GameViewport.scala" This reverts commit f6560cca6ac1022e76ff73d88ad6a7eb3e00bea3. * GameViewport is an opaque type * GameViewport is an opaque type - fix formating * Remove type bounds from GameViewport * OOPS: Scalafmt run --------- Co-authored-by: davesmith00000 --- .../src/main/scala/indigo/package.scala | 2 + .../indigo/shared/config/GameConfig.scala | 2 - .../indigo/shared/config/GameViewport.scala | 69 +++++++------------ .../indigo/shared/scenegraph/Camera.scala | 17 ++--- .../scala/example/game/MyAwesomeGame.scala | 4 +- 5 files changed, 31 insertions(+), 63 deletions(-) diff --git a/indigo/indigo/src/main/scala/indigo/package.scala b/indigo/indigo/src/main/scala/indigo/package.scala index d3c515aa4..487e2422b 100644 --- a/indigo/indigo/src/main/scala/indigo/package.scala +++ b/indigo/indigo/src/main/scala/indigo/package.scala @@ -74,6 +74,8 @@ object syntax: extension [A](l: List[Option[A]]) def sequence: Option[List[A]] = NonEmptyList.sequenceListOption(l) extension [A](l: NonEmptyList[Option[A]]) def sequence: Option[NonEmptyList[A]] = NonEmptyList.sequenceOption(l) + extension (s: Size) def toGameViewport: GameViewport = GameViewport(s) + // Timeline animations object animations: import indigo.shared.animation.timeline.* diff --git a/indigo/indigo/src/main/scala/indigo/shared/config/GameConfig.scala b/indigo/indigo/src/main/scala/indigo/shared/config/GameConfig.scala index 9a4fa7828..1187218aa 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/config/GameConfig.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/config/GameConfig.scala @@ -54,8 +54,6 @@ final case class GameConfig( def withViewport(width: Int, height: Int): GameConfig = this.copy(viewport = GameViewport(width, height)) - def withViewport(size: Size): GameConfig = - this.copy(viewport = GameViewport(size.width, size.height)) def withViewport(newViewport: GameViewport): GameConfig = this.copy(viewport = newViewport) diff --git a/indigo/indigo/src/main/scala/indigo/shared/config/GameViewport.scala b/indigo/indigo/src/main/scala/indigo/shared/config/GameViewport.scala index 2b11733b1..8e3ea02ab 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/config/GameViewport.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/config/GameViewport.scala @@ -4,54 +4,31 @@ import indigo.shared.datatypes.Point import indigo.shared.datatypes.Rectangle import indigo.shared.datatypes.Size -/** Respresents the initial size of the game's viewport. - * - * @param width - * Width in pixels - * @param height - * Height in pixels - */ -final case class GameViewport(size: Size) derives CanEqual: - val width: Int = size.width - val height: Int = size.height - val horizontalMiddle: Int = width / 2 - val verticalMiddle: Int = height / 2 - val center: Point = Point(horizontalMiddle, verticalMiddle) - - @deprecated("use 'toRectangle' instead") - def asRectangle: Rectangle = - toRectangle - def toRectangle: Rectangle = - Rectangle(Point.zero, size) - - def toPoint: Point = - size.toPoint - - def toSize: Size = - size - - def bounds: Rectangle = - toRectangle - - def giveDimensions(magnification: Int): Rectangle = - Rectangle(0, 0, width / magnification, height / magnification) - +opaque type GameViewport = Size object GameViewport: + def apply(size: Size): GameViewport = size + def apply(width: Int, height: Int): GameViewport = Size(width, height) + + val atWUXGA: GameViewport = GameViewport(1920, 1200) + val atWUXGABy2: GameViewport = GameViewport(960, 600) + + val at1080p: GameViewport = GameViewport(1920, 1080) + val at1080pBy2: GameViewport = GameViewport(960, 540) - def apply(width: Int, height: Int): GameViewport = - GameViewport(Size(width, height)) + val at720p: GameViewport = GameViewport(1280, 720) + val at720pBy2: GameViewport = GameViewport(640, 360) - val atWUXGA: GameViewport = - GameViewport(1920, 1200) - val atWUXGABy2: GameViewport = - GameViewport(960, 600) + extension (a: GameViewport) + def width: Int = a.width + def height: Int = a.height - val at1080p: GameViewport = - GameViewport(1920, 1080) - val at1080pBy2: GameViewport = - GameViewport(960, 540) + def horizontalMiddle: Int = a.width / 2 + def verticalMiddle: Int = a.height / 2 + def center: Point = Point(horizontalMiddle, verticalMiddle) - val at720p: GameViewport = - GameViewport(1280, 720) - val at720pBy2: GameViewport = - GameViewport(640, 360) + def toRectangle: Rectangle = Rectangle(Point.zero, a) + def size: Size = a + def toSize: Size = a + def bounds: Rectangle = toRectangle + def giveDimensions(magnification: Int): Rectangle = + Rectangle(0, 0, a.width / magnification, a.height / magnification) 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 2980a6930..e61e2bd1e 100644 --- a/indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala +++ b/indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala @@ -10,12 +10,9 @@ import indigo.shared.datatypes.Size */ sealed trait Camera: def position: Point - def topLeft(viewport: GameViewport): Point def topLeft(viewport: Size): Point - def bounds(viewport: GameViewport): Rectangle def bounds(viewport: Size): Rectangle - def frustum(viewport: GameViewport): Rectangle = bounds(viewport) - def frustum(viewport: Size): Rectangle = bounds(viewport) + def frustum(viewport: Size): Rectangle = bounds(viewport) def zoom: Zoom def rotation: Radians def isLookAt: Boolean @@ -31,11 +28,9 @@ 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): Point = position - def topLeft(viewport: Size): Point = position - def bounds(viewport: GameViewport): Rectangle = Rectangle(position, viewport.size) - def bounds(viewport: Size): Rectangle = Rectangle(position, viewport) - val isLookAt: Boolean = false + def topLeft(viewport: Size): Point = position + def bounds(viewport: Size): Rectangle = Rectangle(position, viewport) + val isLookAt: Boolean = false def withX(newX: Int): Fixed = this.copy(position = position.withX(newX)) @@ -78,12 +73,8 @@ 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): Point = - topLeft(viewport.size) def topLeft(viewport: Size): Point = target - (viewport.toPoint / 2) / zoom.toDouble.toInt - def bounds(viewport: GameViewport): Rectangle = - bounds(viewport.size) def bounds(viewport: Size): Rectangle = Rectangle(topLeft(viewport), viewport) diff --git a/indigo/tyrian-sandbox/src/main/scala/example/game/MyAwesomeGame.scala b/indigo/tyrian-sandbox/src/main/scala/example/game/MyAwesomeGame.scala index 7fdd3ced1..4f17d63ac 100644 --- a/indigo/tyrian-sandbox/src/main/scala/example/game/MyAwesomeGame.scala +++ b/indigo/tyrian-sandbox/src/main/scala/example/game/MyAwesomeGame.scala @@ -1,8 +1,8 @@ package example.game import cats.effect.IO -import indigo._ -import indigo.scenes._ +import indigo.* +import indigo.scenes.* import tyrian.TyrianSubSystem final case class MyAwesomeGame(tyrianSubSystem: TyrianSubSystem[IO, String, Unit], clockwise: Boolean)