Skip to content

Commit

Permalink
Added fix for pane not expanding
Browse files Browse the repository at this point in the history
  • Loading branch information
smilefx committed Nov 30, 2024
1 parent 46bbb7f commit 4177fef
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ class HexagonGrid<T : HexagonView>(
* @param coordinateSystem The coordinate system to use for the layout.
*/
private fun layout(coordinateSystem: CoordinateSystem) {
var minX = Double.MAX_VALUE
var minY = Double.MAX_VALUE

var maxX = Double.MIN_VALUE
var maxY = Double.MIN_VALUE

map.forEach { (coords, hexagon) ->
val (x, y) = coords
val (q, r) =
Expand All @@ -106,17 +112,32 @@ class HexagonGrid<T : HexagonView>(
with(hexagon) {
if (orientation == HexOrientation.POINTY_TOP) {
hexagon.orientation = HexOrientation.POINTY_TOP
val actualWidth = width / 2 * sqrt(3.0)
posXProperty.setSilent(actualWidth * q + if (r % 2 == 0) 0.0 else actualWidth / 2)
val hexWidth = width / 2 * sqrt(3.0)
posXProperty.setSilent(hexWidth * q + if (r % 2 == 0) 0.0 else hexWidth / 2)
posYProperty.setSilent(height * r - r * height / 4)

if (posXProperty.value < minX) minX = posXProperty.value
if (posYProperty.value < minY) minY = posYProperty.value

if (posXProperty.value + hexWidth > maxX) maxX = posXProperty.value + hexWidth
if (posYProperty.value + height > maxY) maxY = posYProperty.value + height
} else {
hexagon.orientation = HexOrientation.FLAT_TOP
val actualHeight = height / 2 * sqrt(3.0)
posYProperty.setSilent(actualHeight * q + if (r % 2 == 0) 0.0 else actualHeight / 2)
val hexHeight = height / 2 * sqrt(3.0)
posYProperty.setSilent(hexHeight * q + if (r % 2 == 0) 0.0 else hexHeight / 2)
posXProperty.setSilent(width * r - r * width / 4)

if (posXProperty.value < minX) minX = posXProperty.value
if (posYProperty.value < minY) minY = posYProperty.value

if (posXProperty.value + width > maxX) maxX = posXProperty.value + width
if (posYProperty.value + hexHeight > maxY) maxY = posYProperty.value + hexHeight
}
}
}

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

override fun T.onRemove() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ internal object Application : BoardGameApplication() {

init {
loadFont("Rubik.ttf")
loadFont("Staatliches-Regular.ttf", "Staatliches", Font.FontWeight.NORMAL)
loadFont("JetBrainsMono-ExtraBold.ttf", "JetBrainsMono", Font.FontWeight.EXTRA_BOLD)
// showGameScene(cardLayoutScene)
// showGameScene(hexGrid)
showGameScene(hexGrid)
// showGameScene(animation)
// showGameScene(grid)
// showGameScene(dragDropScene)
showMenuScene(uiScene)
// showMenuScene(uiScene)
// showGameScene(visualScene)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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.core.BoardGameScene
import tools.aqua.bgw.core.Color
import tools.aqua.bgw.core.HexOrientation
Expand All @@ -19,10 +21,32 @@ internal class HexGridGameScene : BoardGameScene() {
height = 500,
posX = 0,
posY = 0,
coordinateSystem = HexagonGrid.CoordinateSystem.AXIAL,
coordinateSystem = HexagonGrid.CoordinateSystem.OFFSET,
visual = Visual.EMPTY,
orientation = HexOrientation.FLAT_TOP
)
orientation = HexOrientation.POINTY_TOP
).apply {
onMouseClicked = {
placeOnHexGrid(HexagonView(visual = ColorVisual(Color(0, 0, 255)), size = 40))
}
}

private val targetPane = Pane<HexagonGrid<HexagonView>>(
width = 900,
height = 900,
posX = 0,
posY = 0,
visual = ColorVisual.RED
).apply {
add(hexGrid)
}

private val cameraPane = CameraPane(
width = 1920,
height = 1080,
target = targetPane,
).apply {
interactive = true
}

private val satchel = Satchel<HexagonView>(
posX = 800,
Expand All @@ -43,10 +67,9 @@ internal class HexGridGameScene : BoardGameScene() {
private val hexFlat = HexagonView(posX = 900, posY = 200, visual = ColorVisual(Color(0, 255, 0)), size = 50, orientation = HexOrientation.FLAT_TOP)

fun placeOnHexGrid(hexagon: HexagonView) {
satchel.remove(hexagon)
hexGrid[Random.nextInt(-5, 5), Random.nextInt(-5, 5)] = hexagon.apply {
isDraggable = true
}
hexGrid[10,0] = HexagonView(visual = ColorVisual(Color(255, 0, 0)), size = 50, orientation = HexOrientation.POINTY_TOP)
targetPane.width = hexGrid.width
targetPane.height = hexGrid.height
}

fun placeInSatchel(hexagon: HexagonView) {
Expand Down Expand Up @@ -82,12 +105,23 @@ internal class HexGridGameScene : BoardGameScene() {
onDragGestureExited = {
visual = ColorVisual.LIGHT_GRAY
}

onMouseEntered = {
visual = ColorVisual.GRAY
}

onMouseExited = {
visual = ColorVisual.LIGHT_GRAY
}
}
hexGrid[q,r] = hexagon
hexMap[Pair(q,r)] = hexagon
}
}
}

targetPane.width = hexGrid.width
targetPane.height = hexGrid.height
}

fun refreshHexGrid() {
Expand All @@ -97,7 +131,7 @@ internal class HexGridGameScene : BoardGameScene() {
init {
buildHexGrid()

addComponents(hexGrid, satchel)
addComponents(cameraPane, satchel)
repeat(20) {
val hexagon = HexagonView(posX = 800, posY = 800, visual = ImageVisual("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSwc4YbxNBYXWRkgqzh9tbaSQh2Uy-f4e1Nl0teHHWFisub3gxv4rxn1eFjgVUUMASaNSg&usqp=CAU"), size = 40, orientation = HexOrientation.FLAT_TOP).apply {
isDraggable = true
Expand Down

0 comments on commit 4177fef

Please sign in to comment.