Skip to content

Commit

Permalink
Update UiScene to use custom viewmodel with 3 buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Jul 2, 2024
1 parent 66eaf6b commit 987fbfa
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.example.sandbox.scenes.TextureTileScene
import com.example.sandbox.scenes.TimelineScene
import com.example.sandbox.scenes.UVShaders
import com.example.sandbox.scenes.UiScene
import com.example.sandbox.scenes.UiSceneViewModel
import com.example.sandbox.scenes.UltravioletScene
import example.TestFont
import indigo.*
Expand All @@ -49,7 +50,7 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
val viewportHeight: Int = gameHeight * magnificationLevel // 256

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

def scenes(bootData: SandboxBootData): NonEmptyList[Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]] =
NonEmptyList(
Expand Down Expand Up @@ -177,38 +178,13 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
.withCrop(188, 78, 14, 23)
)

val buttonAssets: ButtonAssets =
ButtonAssets(
up = Graphic(0, 0, 16, 16, 2, Material.Bitmap(AssetName("dots"))).withCrop(0, 0, 16, 16),
over = Graphic(0, 0, 16, 16, 2, Material.Bitmap(AssetName("dots"))).withCrop(16, 0, 16, 16),
down = Graphic(0, 0, 16, 16, 2, Material.Bitmap(AssetName("dots"))).withCrop(16, 16, 16, 16)
)

Outcome(
SandboxViewModel(
Point.zero,
InputField("single", assets).withKey(BindingKey("single")).makeSingleLine,
InputField("multi\nline", assets).withKey(BindingKey("multi")).makeMultiLine.moveTo(5, 5),
true,
HitArea(Polygon.Closed(UiScene.points.map(Vertex.fromPoint)))
.moveTo(175, 10)
.withUpActions(Log("Up!"))
.withClickActions(Log("Click!"))
.withDownActions(Log("Down!"))
.withHoverOverActions(Log("Over!"))
.withHoverOutActions(Log("Out!"))
.withHoldDownActions(Log("Hold down!")),
Button(
buttonAssets = buttonAssets,
bounds = Rectangle(10, 10, 16, 16),
depth = Depth(2)
)
.withUpActions(Log("Up!"))
.withClickActions(Log("Click!"))
.withDownActions(Log("Down!"))
.withHoverOverActions(Log("Over!"))
.withHoverOutActions(Log("Out!"))
.withHoldDownActions(Log("Hold down!"))
UiSceneViewModel.initial
)
)
}
Expand Down Expand Up @@ -306,13 +282,9 @@ final case class SandboxViewModel(
single: InputField,
multi: InputField,
useLightingLayer: Boolean,
hitArea: HitArea,
button: Button
uiScene: UiSceneViewModel
):
def update(mouse: Mouse): Outcome[SandboxViewModel] =
for {
bn <- button.update(mouse)
ha <- hitArea.update(mouse)
} yield this.copy(hitArea = ha, button = bn)
uiScene.update(mouse).map(ui => this.copy(uiScene = ui))

final case class Log(message: String) extends GlobalEvent
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ import com.example.sandbox.SandboxStartupData
import com.example.sandbox.SandboxViewModel
import indigo.*
import indigo.scenes.*
import indigoextras.ui.Button
import indigoextras.ui.ButtonAssets
import indigoextras.ui.HitArea

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

type SceneModel = SandboxGameModel
type SceneViewModel = SandboxViewModel
type SceneViewModel = UiSceneViewModel

def eventFilters: EventFilters =
EventFilters.Permissive

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

def viewModelLens: Lens[SandboxViewModel, SandboxViewModel] =
Lens.keepLatest
def viewModelLens: Lens[SandboxViewModel, UiSceneViewModel] =
Lens(
_.uiScene,
(m, vm) => m.copy(uiScene = vm)
)

def name: SceneName =
SceneName("ui")
Expand All @@ -37,8 +42,8 @@ object UiScene extends Scene[SandboxStartupData, SandboxGameModel, SandboxViewMo
def updateViewModel(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel,
viewModel: SandboxViewModel
): GlobalEvent => Outcome[SandboxViewModel] =
viewModel: UiSceneViewModel
): GlobalEvent => Outcome[UiSceneViewModel] =
case FrameTick =>
viewModel.update(context.mouse)

Expand All @@ -60,7 +65,7 @@ object UiScene extends Scene[SandboxStartupData, SandboxGameModel, SandboxViewMo
def present(
context: SceneContext[SandboxStartupData],
model: SandboxGameModel,
viewModel: SandboxViewModel
viewModel: UiSceneViewModel
): Outcome[SceneUpdateFragment] =
Outcome(
SceneUpdateFragment(
Expand All @@ -71,8 +76,81 @@ object UiScene extends Scene[SandboxStartupData, SandboxGameModel, SandboxViewMo
Stroke(4, RGBA.Black.withAlpha(0.75))
)
.moveTo(175, 10),
viewModel.button.draw
viewModel.button1.draw,
viewModel.button2.draw,
viewModel.button3.draw
)
)

}

final case class UiSceneViewModel(
hitArea: HitArea,
button1: Button,
button2: Button,
button3: Button
):
def update(mouse: Mouse): Outcome[UiSceneViewModel] =
for {
ha <- hitArea.update(mouse)
bn1 <- button1.update(mouse)
bn2 <- button2.update(mouse)
bn3 <- button3.update(mouse)
} yield this.copy(hitArea = ha, button1 = bn1, button2 = bn2, button3 = bn3)

object UiSceneViewModel:

val buttonAssets: ButtonAssets =
ButtonAssets(
up = Graphic(0, 0, 16, 16, 2, Material.Bitmap(AssetName("dots"))).withCrop(0, 0, 16, 16),
over = Graphic(0, 0, 16, 16, 2, Material.Bitmap(AssetName("dots"))).withCrop(16, 0, 16, 16),
down = Graphic(0, 0, 16, 16, 2, Material.Bitmap(AssetName("dots"))).withCrop(16, 16, 16, 16)
)

val initial: UiSceneViewModel =
UiSceneViewModel(
HitArea(Polygon.Closed(UiScene.points.map(Vertex.fromPoint)))
.moveTo(175, 10)
.withUpActions(Log("Up!"))
.withClickActions(Log("Click!"))
.withDownActions(Log("Down!"))
.withHoverOverActions(Log("Over!"))
.withHoverOutActions(Log("Out!"))
.withHoldDownActions(Log("Hold down!")),
Button(
buttonAssets = buttonAssets,
bounds = Rectangle(0, 0, 16, 16),
depth = Depth(2)
)
.withUpActions(Log("Up! 1"))
.withClickActions(Log("Click! 1"))
.withDownActions(Log("Down! 1"))
.withHoverOverActions(Log("Over! 1"))
.withHoverOutActions(Log("Out! 1"))
.withHoldDownActions(Log("Hold down! 1"))
.moveTo(16, 16),
Button(
buttonAssets = buttonAssets,
bounds = Rectangle(0, 0, 16, 16),
depth = Depth(2)
)
.withUpActions(Log("Up! 2"))
.withClickActions(Log("Click! 2"))
.withDownActions(Log("Down! 2"))
.withHoverOverActions(Log("Over! 2"))
.withHoverOutActions(Log("Out! 2"))
.withHoldDownActions(Log("Hold down! 2"))
.moveTo(48, 16),
Button(
buttonAssets = buttonAssets,
bounds = Rectangle(0, 0, 16, 16),
depth = Depth(2)
)
.withUpActions(Log("Up! 3"))
.withClickActions(Log("Click! 3"))
.withDownActions(Log("Down! 3"))
.withHoverOverActions(Log("Over! 3"))
.withHoverOutActions(Log("Out! 3"))
.withHoldDownActions(Log("Hold down! 3"))
.moveTo(80, 16)
)

0 comments on commit 987fbfa

Please sign in to comment.