Skip to content

Commit

Permalink
Scroll and EventHandler fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
smilefx committed Jan 26, 2025
1 parent 42a4236 commit 6bdb21e
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import tools.aqua.bgw.util.Coordinate
*
* @since 0.1
*
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.button
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.greyToken
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.movementAnimationTo
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.playMovementAnimation
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.playMovementAnimationTo
*/
class MovementAnimation<T : ComponentView>(
componentView: T,
Expand Down Expand Up @@ -84,9 +84,9 @@ class MovementAnimation<T : ComponentView>(
*
* @since 0.1
*
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.button
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.redToken
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.movementAnimationBy
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.playMovementAnimation
* @sample tools.aqua.bgw.main.examples.ExampleAnimationScene.playMovementAnimationBy
*/
constructor(
componentView: T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ internal val internalChannel: Channel =

internal fun HTML.index() {
body {
style = "width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none;"
div {
classes = setOf("bgw-root-container")
div {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ class HexagonGrid<T : HexagonView>(
* @param component The hexagon component to set.
*/
operator fun set(columnIndex: Int, rowIndex: Int, component: T) {
map[columnIndex to rowIndex]?.run { observableComponents.remove(this) }
map[columnIndex to rowIndex]?.run {
observableComponents.remove(this)
onRemove?.invoke(this)
}
component.orientation = orientation
component.parent = this
map[columnIndex to rowIndex] = component
observableComponents.add(component)
onAdd?.invoke(component)
}

/**
Expand Down Expand Up @@ -154,6 +158,11 @@ class HexagonGrid<T : HexagonView>(

widthProperty.setSilent(maxX - minX)
heightProperty.setSilent(maxY - minY)

components.forEach {
it.posXProperty.setSilent(it.posXProperty.value - minX)
it.posYProperty.setSilent(it.posYProperty.value - minY)
}
}

override fun T.onRemove() {
Expand Down
5 changes: 3 additions & 2 deletions bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/main/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import tools.aqua.bgw.main.view.Application

internal fun main() {
if (Config.GENERATE_SAMPLES) {
ExampleApplication.showGameScene(ExampleApplication.exampleUIScene)
val jsonData = Json.encodeToString(ExampleApplication.exampleUIScene.map)
val jsonData =
Json.encodeToString(
ExampleApplication.exampleUIScene.map + ExampleApplication.exampleAnimationScene.map)
File("build/examples").mkdirs()
File("build/examples/bgwSamples.json").writeText(jsonData)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,44 @@ import kotlinx.serialization.encodeToString
import tools.aqua.bgw.animation.Animation
import tools.aqua.bgw.animation.MovementAnimation
import tools.aqua.bgw.components.ComponentView
import tools.aqua.bgw.components.uicomponents.*
import tools.aqua.bgw.core.*
import tools.aqua.bgw.main.examples.ExampleUIScene as AnimationScene
import tools.aqua.bgw.components.gamecomponentviews.CardView
import tools.aqua.bgw.components.gamecomponentviews.TokenView
import tools.aqua.bgw.core.BoardGameScene
import tools.aqua.bgw.main.examples.ExampleAnimationScene as AnimationScene
import tools.aqua.bgw.mapper.AnimationMapper
import tools.aqua.bgw.visual.ColorVisual

/** Metadata: [AnimationScene] */
internal class ExampleAnimationScene : BoardGameScene(width = 622, height = 300) {
val map = mutableMapOf<String, String>()

val button =
Button(
posX = 100,
posY = 50,
width = 200,
height = 100,
text = "I am a Button.",
visual = ColorVisual.LIGHT_GRAY)

val button2 =
Button(
posX = 0,
posY = 0,
width = 200,
height = 200,
text = "Also a Button.",
visual = ColorVisual.RED)

val movementAnimation =
MovementAnimation(componentView = button, fromX = 100.0, toX = 200.0, duration = 1000)
val greyToken =
TokenView(posX = 100, posY = 50, width = 100, height = 160, visual = ColorVisual.LIGHT_GRAY)

val redToken =
TokenView(posX = 100, posY = 50, width = 100, height = 160, visual = ColorVisual.LIGHT_GRAY)

/** Creates a movement animation that moves the token from x = 100 to x = 200 in 1 second. */
val movementAnimationTo =
MovementAnimation(componentView = greyToken, fromX = 100.0, toX = 200.0, duration = 1000)

/** Creates a movement animation that moves the token by x = 200 and y = 50 in 2 seconds. */
val movementAnimationBy =
MovementAnimation(componentView = button, byX = 100.0, byY = 100.0, duration = 1000)
MovementAnimation(componentView = redToken, byX = 200.0, byY = 50.0, duration = 2000)

init {
playAnimationAndSerialize(::movementAnimation, ::button)
setComponentAndSerialize(::greyToken)
setComponentAndSerialize(::redToken)
playAnimationAndSerialize(::movementAnimationTo, ::greyToken)
playAnimationAndSerialize(::movementAnimationBy, ::redToken)
}

fun playMovementAnimation() {
playAnimation(movementAnimation)
fun playMovementAnimationTo() {
playAnimation(movementAnimationTo)
}

private fun <T : ComponentView> setComponentAndSerialize(prop: KProperty0<T>) {
val comp = prop.get()

clearComponents()
addComponents(comp)

val fullyQualifiedName = "${this::class.qualifiedName}.${prop.name}"

val appData = SceneMapper.map(menuScene = null, gameScene = this)
val json = jsonMapper.encodeToString(PropData(appData))
map[fullyQualifiedName] = json

clearComponents()
fun playMovementAnimationBy() {
playAnimation(movementAnimationBy)
}

private fun <T : ComponentView> playAnimationAndSerialize(
Expand All @@ -96,7 +79,20 @@ internal class ExampleAnimationScene : BoardGameScene(width = 622, height = 300)
val animationData = AnimationMapper.map(animation)
val json = jsonMapper.encodeToString(PropData(animationData))
map[fullyQualifiedName] = json
}

internal fun <T : ComponentView> setComponentAndSerialize(prop: KProperty0<T>) {
val comp = prop.get()

println(map[fullyQualifiedName])
clearComponents()
addComponents(comp)

val fullyQualifiedName = "${this::class.qualifiedName}.${prop.name}"

val appData = SceneMapper.map(menuScene = null, gameScene = this)
val json = jsonMapper.encodeToString(PropData(appData))
map[fullyQualifiedName] = json

clearComponents()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package tools.aqua.bgw.main.examples

import tools.aqua.bgw.core.BoardGameApplication

internal object ExampleApplication : BoardGameApplication() {
internal object ExampleApplication : BoardGameApplication(width = 622, height = 300) {
val exampleUIScene = ExampleUIScene()
val exampleAnimationScene = ExampleAnimationScene()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import tools.aqua.bgw.components.ComponentView
import tools.aqua.bgw.components.uicomponents.*
import tools.aqua.bgw.core.*
import tools.aqua.bgw.main.examples.ExampleUIScene as Scene
import tools.aqua.bgw.style.BorderRadius
import tools.aqua.bgw.visual.ColorVisual

/** Metadata: [Scene] */
internal class ExampleUIScene : BoardGameScene(width = 622, height = 300) {
val map = mutableMapOf<String, String>()

/** Create a light gray button. */
val button =
Button(
posX = 100,
Expand All @@ -40,14 +42,15 @@ internal class ExampleUIScene : BoardGameScene(width = 622, height = 300) {
text = "I am a Button.",
visual = ColorVisual.LIGHT_GRAY)

/** Creates a red button with large border radius. */
val button2 =
Button(
posX = 0,
posY = 0,
width = 200,
height = 200,
text = "Also a Button.",
visual = ColorVisual.RED)
visual = ColorVisual.RED.apply { style.borderRadius = BorderRadius.LARGE })

init {
setComponentAndSerialize(::button)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import tools.aqua.bgw.components.ComponentView
import tools.aqua.bgw.components.container.HexagonGrid
import tools.aqua.bgw.components.container.Satchel
import tools.aqua.bgw.components.gamecomponentviews.HexagonView
import tools.aqua.bgw.components.gamecomponentviews.TokenView
import tools.aqua.bgw.components.layoutviews.CameraPane
import tools.aqua.bgw.components.layoutviews.Pane
import tools.aqua.bgw.components.uicomponents.Button
Expand All @@ -37,6 +38,7 @@ import tools.aqua.bgw.util.BidirectionalMap
import tools.aqua.bgw.visual.ColorVisual
import tools.aqua.bgw.visual.ImageVisual
import tools.aqua.bgw.visual.Visual
import kotlin.math.sqrt

internal class HexGridGameScene : BoardGameScene() {
private val hexGrid =
Expand All @@ -47,7 +49,9 @@ internal class HexGridGameScene : BoardGameScene() {
posY = 0,
coordinateSystem = HexagonGrid.CoordinateSystem.OFFSET,
visual = Visual.EMPTY,
orientation = HexOrientation.POINTY_TOP)
orientation = HexOrientation.POINTY_TOP).apply {
isLayoutFromCenter = false
}

private val satchel =
Satchel<HexagonView>(
Expand Down Expand Up @@ -105,20 +109,13 @@ internal class HexGridGameScene : BoardGameScene() {
CameraPane(
width = 1200,
height = 800,
target =
Pane<ComponentView>(
width = 1920,
height = 1080,
posX = 0,
posY = 0,
visual = ImageVisual("assets/3.jpg")),
target = targetPane,
posX = 200,
posY = 100,
visual = ColorVisual.YELLOW,
limitBounds = true)
limitBounds = false)
.apply {
interactive = true

onZoomed = { println("Zoomed to $it") }
}

Expand Down Expand Up @@ -248,14 +245,42 @@ internal class HexGridGameScene : BoardGameScene() {
targetPane.height = hexGrid.height
}

/** Function to calculate the bounds of the hex grid */
private fun calculateHexGridBounds(grid : HexagonGrid<HexagonView>) : Pair<Double, Double> {
var minX = Double.MAX_VALUE
var minY = Double.MAX_VALUE

grid.components.forEach {
if(it.actualPosX < minX) minX = it.actualPosX
if(it.actualPosY < minY) minY = it.actualPosY
}

return Pair(minX, minY)
}

/* Function to build tokens on the hex grid at (0,0) of each HexagonView. */
fun buildTokens() {

hexGrid.components.forEach {
val token = TokenView(visual = ColorVisual.RED, width = 20, height = 20, posX = it.actualPosX, posY = it.actualPosY)
targetPane.add(token)
}
}

fun refreshHexGrid() {
buildHexGrid()
}

init {
buildHexGrid()

hexGrid.components.forEach {
println("${it.actualPosX} - ${it.actualPosY}")
}

buildTokens()

targetPane.add(singleHex)
addComponents(targetPane, panButton, panZeroButton, zoomButton, centerDot)
addComponents(cameraPane, panButton, panZeroButton, zoomButton, centerDot)
}
}

0 comments on commit 6bdb21e

Please sign in to comment.