Skip to content

Commit

Permalink
Fixed #732: Content Layers support clone blanks
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Apr 27, 2024
1 parent 3ce8281 commit 4547e0c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 20 deletions.
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 @@ -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)
)
)

0 comments on commit 4547e0c

Please sign in to comment.