diff --git a/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxGame.scala b/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxGame.scala index 6f4634c58..0e59f9867 100644 --- a/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxGame.scala +++ b/indigo/sandbox/src/main/scala/com/example/sandbox/SandboxGame.scala @@ -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.* @@ -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( @@ -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 ) ) } @@ -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 diff --git a/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/UiScene.scala b/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/UiScene.scala index 344e8d312..78e93eaa5 100644 --- a/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/UiScene.scala +++ b/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/UiScene.scala @@ -6,12 +6,14 @@ 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 @@ -19,8 +21,11 @@ object UiScene extends Scene[SandboxStartupData, SandboxGameModel, SandboxViewMo 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") @@ -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) @@ -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( @@ -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) + )