Skip to content

Commit

Permalink
Improved nineslice sandbox demo
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 26, 2024
1 parent 767af84 commit a096f11
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 13 deletions.
Binary file added indigo/sandbox/assets/platform.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added indigo/sandbox/assets/window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ object SandboxAssets {
val light: AssetName = AssetName("light")
val dots: AssetName = AssetName("dots")
val cameraIcon: AssetName = AssetName("camera_icon")
val nineSlice: AssetName = AssetName("nineslice")

// NineSlice assets
val nineSlice: AssetName = AssetName("nineslice")
val platform: AssetName = AssetName("platform")
val window: AssetName = AssetName("window")

val fontMaterial: Material.ImageEffects = Material.ImageEffects(smallFontName)
val lightMaterial: Material.ImageEffects = Material.ImageEffects(light)
Expand Down Expand Up @@ -98,7 +102,9 @@ object SandboxAssets {
AssetType.Image(cratesDiffuseName, AssetPath("assets/" + cratesDiffuseName + ".png")),
AssetType.Image(cratesNormalName, AssetPath("assets/" + cratesNormalName + ".png")),
AssetType.Image(cameraIcon, AssetPath("assets/camera_icon.png")),
AssetType.Image(nineSlice, AssetPath("assets/nineslice.png"))
AssetType.Image(nineSlice, AssetPath("assets/nineslice.png")),
AssetType.Image(platform, AssetPath("assets/platform.png")),
AssetType.Image(window, AssetPath("assets/window.png"))
)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.example.sandbox.scenes.LightsScene
import com.example.sandbox.scenes.LineReflectionScene
import com.example.sandbox.scenes.ManyEventHandlers
import com.example.sandbox.scenes.MutantsScene
import com.example.sandbox.scenes.NineSliceScene
import com.example.sandbox.scenes.OriginalScene
import com.example.sandbox.scenes.PathFindingScene
import com.example.sandbox.scenes.PointersScene
Expand Down Expand Up @@ -52,7 +53,7 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
val viewportHeight: Int = gameHeight * magnificationLevel // 256

def initialScene(bootData: SandboxBootData): Option[SceneName] =
Some(TextureTileScene.name)
Some(NineSliceScene.name)

def scenes(bootData: SandboxBootData): NonEmptyList[Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]] =
NonEmptyList(
Expand Down Expand Up @@ -80,7 +81,8 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
LineReflectionScene,
CameraWithCloneTilesScene,
PathFindingScene,
CaptureScreenScene
CaptureScreenScene,
NineSliceScene
)

val eventFilters: EventFilters = EventFilters.Permissive
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.example.sandbox.scenes

import com.example.sandbox.SandboxAssets
import com.example.sandbox.SandboxGameModel
import com.example.sandbox.SandboxStartupData
import com.example.sandbox.SandboxViewModel
import indigo._
import indigo.scenes._
import indigoextras.effectmaterials.Refraction

object NineSliceScene extends Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel] {

type SceneModel = SandboxGameModel
type SceneViewModel = SandboxViewModel

def eventFilters: EventFilters =
EventFilters.Restricted

def modelLens: Lens[SandboxGameModel, SandboxGameModel] =
Lens.keepOriginal

def viewModelLens: Lens[SandboxViewModel, SandboxViewModel] =
Lens.keepOriginal

def name: SceneName =
SceneName("nine slice scene")

def subSystems: Set[SubSystem[SandboxGameModel]] =
Set()

def updateModel(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel
): GlobalEvent => Outcome[SandboxGameModel] =
_ => Outcome(model)

def updateViewModel(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel,
viewModel: SandboxViewModel
): GlobalEvent => Outcome[SandboxViewModel] =
_ => Outcome(viewModel)

def fit(originalSize: Vector2, screenSize: Vector2): Vector2 =
Vector2(Math.max(screenSize.x / originalSize.x, screenSize.y / originalSize.y))

def boxSize(t: Seconds): Int = Signal.SmoothPulse.map(d => (d * 64) + 32).map(_.toInt).at(t)

def present(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel,
viewModel: SandboxViewModel
): Outcome[SceneUpdateFragment] = {
val viewCenter: Point = context.startUpData.viewportCenter

val boxSizeValue: Int = boxSize(context.frame.time.running)

Outcome(
SceneUpdateFragment.empty
.addLayers(
Layer(
Graphic(
boxSizeValue,
boxSizeValue,
Material.Bitmap(SandboxAssets.nineSlice).nineSlice(Rectangle(16, 16, 32, 32))
).moveTo(5, 5),
// Shape
// .Box(Rectangle(boxSizeValue, boxSizeValue), Fill.None, Stroke(1, RGBA.Green))
// .moveTo(5, 5),
Graphic(
boxSizeValue,
boxSizeValue,
Material.Bitmap(SandboxAssets.platform).nineSlice(Rectangle(8, 20, 112, 40))
).moveTo(100, 5),
// Shape
// .Box(Rectangle(boxSizeValue, boxSizeValue), Fill.None, Stroke(1, RGBA.Green))
// .moveTo(75, 5),
Graphic(
boxSizeValue,
boxSizeValue,
Material.Bitmap(SandboxAssets.window).nineSlice(Rectangle(3, 15, 121, 41))
).moveTo(5, 100)
// Shape
// .Box(Rectangle(boxSizeValue, boxSizeValue), Fill.None, Stroke(1, RGBA.Green))
// .moveTo(5, 75),
)
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ object TextureTileScene extends Scene[SandboxStartupData, SandboxGameModel, Sand
.modifyMaterial(_.withAlpha(0.2)),
Graphic(64, 64, Material.Bitmap(SandboxAssets.dots).normal).moveTo(10, 90),
Graphic(200, 75, Material.Bitmap(SandboxAssets.dots).tile).moveTo(10, 10),
Graphic(50, 75, Material.Bitmap(SandboxAssets.dots).stretch).moveTo(100, 75),
Graphic(
boxSizeValue,
boxSizeValue,
Material.Bitmap(SandboxAssets.nineSlice).nineSlice(Rectangle(16, 16, 32, 32))
).moveTo(10, 50),
Shape
.Box(Rectangle(boxSizeValue, boxSizeValue), Fill.None, Stroke(1, RGBA.Green))
.moveTo(10, 50)
Graphic(50, 75, Material.Bitmap(SandboxAssets.dots).stretch).moveTo(100, 75)
)
)
)
Expand Down

0 comments on commit a096f11

Please sign in to comment.