Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #732: Content Layers support clone blanks #734

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import indigo.shared.AnimationsRegister
import indigo.shared.BoundaryLocator
import indigo.shared.FontRegister
import indigo.shared.QuickCache
import indigo.shared.collections.Batch
import indigo.shared.config.RenderingTechnology
import indigo.shared.datatypes.Depth
import indigo.shared.datatypes.RGBA
Expand Down Expand Up @@ -94,8 +95,11 @@ final class SceneProcessor(
case _ =>
None

val gatheredCloneBlanks: Batch[CloneBlank] =
scene.cloneBlanks ++ scene.layers.flatMap(_.layer.gatherCloneBlanks)

val cloneBlankDisplayObjects: scalajs.js.Dictionary[DisplayObject] =
scene.cloneBlanks.foldLeft(scalajs.js.Dictionary.empty[DisplayObject]) { (acc, blank) =>
gatheredCloneBlanks.foldLeft(scalajs.js.Dictionary.empty[DisplayObject]) { (acc, blank) =>
val maybeDO =
if blank.isStatic then
QuickCache(blank.id.toString) {
Expand Down
52 changes: 44 additions & 8 deletions indigo/indigo/src/main/scala/indigo/shared/scenegraph/Layer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,23 @@ enum Layer derives CanEqual:
depth: Option[Depth],
visible: Option[Boolean],
blending: Option[Blending],
cloneBlanks: Batch[CloneBlank],
camera: Option[Camera]
)

/** Apply a magnification to this content layer, or all of the layers in this stack.
/** Apply a magnification to this layer and all it's child layers
*
* @param level
*/
def withMagnification(level: Int): Layer =
def withMagnificationForAll(level: Int): Layer =
this match
case l: Layer.Stack =>
l.copy(
layers = l.layers.map(_.withMagnification(level))
layers = l.layers.map(_.withMagnificationForAll(level))
)

case l: Layer.Content =>
l.copy(magnification = Option(Math.max(1, Math.min(256, level))))
l.withMagnification(level)

def toBatch: Batch[Layer.Content] =
@tailrec
Expand All @@ -91,6 +92,23 @@ enum Layer derives CanEqual:

rec(Batch(this), Batch.empty)

def gatherCloneBlanks: Batch[CloneBlank] =
@tailrec
def rec(remaining: Batch[Layer], acc: Batch[CloneBlank]): Batch[CloneBlank] =
if remaining.isEmpty then acc
else
val h = remaining.head
val t = remaining.tail

h match
case Layer.Stack(layers) =>
rec(layers ++ t, acc)

case l: Layer.Content =>
rec(t, acc ++ l.cloneBlanks)

rec(Batch(this), Batch.empty)

object Layer:

val empty: Layer.Content =
Expand All @@ -116,16 +134,16 @@ object Layer:
object Content:

val empty: Layer.Content =
Layer.Content(Batch.empty, Batch.empty, None, None, None, None, None)
Layer.Content(Batch.empty, Batch.empty, None, None, None, None, Batch.empty, None)

def apply(nodes: SceneNode*): Layer.Content =
Layer.Content(Batch.fromSeq(nodes), Batch.empty, None, None, None, None, None)
Layer.Content(Batch.fromSeq(nodes), Batch.empty, None, None, None, None, Batch.empty, None)

def apply(nodes: Batch[SceneNode]): Layer.Content =
Layer.Content(nodes, Batch.empty, None, None, None, None, None)
Layer.Content(nodes, Batch.empty, None, None, None, None, Batch.empty, None)

def apply(maybeNode: Option[SceneNode]): Layer.Content =
Layer.Content(Batch.fromOption(maybeNode), Batch.empty, None, None, None, None, None)
Layer.Content(Batch.fromOption(maybeNode), Batch.empty, None, None, None, None, Batch.empty, None)

extension (ls: Layer.Stack)
def combine(other: Layer.Stack): Layer.Stack =
Expand Down Expand Up @@ -155,6 +173,7 @@ object Layer:
a.depth.orElse(b.depth),
a.visible.orElse(b.visible),
a.blending.orElse(b.blending),
a.cloneBlanks ++ b.cloneBlanks,
a.camera.orElse(b.camera)
)

Expand Down Expand Up @@ -218,6 +237,23 @@ object Layer:
def noCamera: Layer.Content =
lc.copy(camera = None)

def withCloneBlanks(blanks: Batch[CloneBlank]): Layer.Content =
lc.copy(cloneBlanks = blanks)
def withCloneBlanks(blanks: CloneBlank*): Layer.Content =
lc.withCloneBlanks(Batch.fromSeq(blanks))

def addCloneBlanks(blanks: Batch[CloneBlank]): Layer.Content =
lc.copy(cloneBlanks = lc.cloneBlanks ++ blanks)
def addCloneBlanks(blanks: CloneBlank*): Layer.Content =
lc.addCloneBlanks(Batch.fromSeq(blanks))

/** Apply a magnification to this content layer
*
* @param level
*/
def withMagnification(level: Int): Layer.Content =
lc.copy(magnification = Option(Math.max(1, Math.min(256, level))))

def ::(stack: Layer.Stack): Layer.Stack =
stack.prepend(lc)
def +:(stack: Layer.Stack): Layer.Stack =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ enum LayerEntry:
case l: LayerEntry.Untagged => l.copy(layer = f(l.layer))
case l: LayerEntry.Tagged => l.copy(layer = f(l.layer))

def withMagnification(level: Int): LayerEntry =
/** Apply a magnification to this layer entry's layer, and all it's child layers.
*
* @param level
*/
def withMagnificationForAll(level: Int): LayerEntry =
this match
case l: LayerEntry.Untagged => l.copy(layer = l.layer.withMagnification(level))
case l: LayerEntry.Tagged => l.copy(layer = l.layer.withMagnification(level))
case l: LayerEntry.Untagged => l.copy(layer = l.layer.withMagnificationForAll(level))
case l: LayerEntry.Tagged => l.copy(layer = l.layer.withMagnificationForAll(level))

def toBatch: Batch[Layer.Content] =
layer.toBatch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,15 @@ final case class SceneUpdateFragment(
def withAudio(sceneAudio: SceneAudio): SceneUpdateFragment =
this.copy(audio = Some(sceneAudio))

def addCloneBlanks(blanks: CloneBlank*): SceneUpdateFragment =
addCloneBlanks(blanks.toBatch)
def withCloneBlanks(blanks: Batch[CloneBlank]): SceneUpdateFragment =
this.copy(cloneBlanks = blanks)
def withCloneBlanks(blanks: CloneBlank*): SceneUpdateFragment =
withCloneBlanks(blanks.toBatch)

def addCloneBlanks(blanks: Batch[CloneBlank]): SceneUpdateFragment =
this.copy(cloneBlanks = cloneBlanks ++ blanks)
def addCloneBlanks(blanks: CloneBlank*): SceneUpdateFragment =
addCloneBlanks(blanks.toBatch)

def withBlendMaterial(newBlendMaterial: BlendMaterial): SceneUpdateFragment =
this.copy(blendMaterial = Option(newBlendMaterial))
Expand All @@ -127,7 +131,7 @@ final case class SceneUpdateFragment(

def withMagnification(level: Int): SceneUpdateFragment =
this.copy(
layers = layers.map(_.withMagnification(level))
layers = layers.map(_.withMagnificationForAll(level))
)

def withCamera(newCamera: Camera): SceneUpdateFragment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class SceneUpdateFragmentTests extends munit.FunSuite {
fail("Should have been a tagged layer entry")

case l @ LayerEntry.Tagged(key, layer) =>
l.withTag(BindingKey(key.toString + "?")).withMagnification(2)
l.withTag(BindingKey(key.toString + "?")).withMagnificationForAll(2)
}

assert(actual.layers.flatMap(_.toBatch).length == 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
val viewportHeight: Int = gameHeight * magnificationLevel // 256

def initialScene(bootData: SandboxBootData): Option[SceneName] =
Some(TextScene.name)
Some(CameraWithCloneTilesScene.name)

def scenes(bootData: SandboxBootData): NonEmptyList[Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]] =
NonEmptyList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ object CameraWithCloneTilesScene extends Scene[SandboxStartupData, SandboxGameMo
): Outcome[SceneUpdateFragment] =
Outcome(
SceneUpdateFragment(
Layer(
CloneTiles(cloneId, crates)
).withCamera(Camera.LookAt(Point(96)))
Layer
.Content(
CloneTiles(cloneId, crates)
)
.withCamera(Camera.LookAt(Point(96)))
.withMagnification(2)
).addCloneBlanks(cloneBlanks)
.addCloneBlanks(cloneBlanks)
)
)
Loading