-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new BGW hexagon game component view. (#369)
* Added hexagon view component. * Added sample builder for hexagon view. * Display hexagons according to given size. * Refactored building hexagons. Added new hexagon builder. * Build compound visuals for hexagon view. * Added safe element indexing in observable lists. * Refactored hexagon view constructor parameter to take radius instead of diameter. * Added empty hexagon view animation to animation builder. * Fixed mouse picking fo hexagon views with overlapping layout bounds. * Reformatted ComponentNodeBuilder.kt * 🎉 Added hexagon grid game component container 🎉 * Added new coordinate system enum. * Added CoordinateSystem to HexagonGrid. * Wrapped hexagon views in region for proper layouting. * Added FlipAnimation support for Hexagons. * Added documentation for hexagons. * Applied spotless. * Moved hexagon grid to own branch. * Updated CHANGELOG.md --------- Co-authored-by: Amin Bouzerda <amin.bouzerda@tu-dortmund.de>
- Loading branch information
Showing
9 changed files
with
212 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
bgw-gui/src/main/kotlin/tools/aqua/bgw/builder/HexagonBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2023 The BoardGameWork Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package tools.aqua.bgw.builder | ||
|
||
import javafx.scene.layout.Pane | ||
import javafx.scene.layout.Region | ||
import javafx.scene.layout.StackPane | ||
import javafx.scene.paint.Color | ||
import javafx.scene.paint.ImagePattern | ||
import javafx.scene.paint.Paint | ||
import javafx.scene.shape.Polygon | ||
import javafx.scene.shape.StrokeLineCap | ||
import javafx.scene.shape.StrokeLineJoin | ||
import javafx.scene.shape.StrokeType | ||
import kotlin.math.cos | ||
import kotlin.math.sin | ||
import tools.aqua.bgw.builder.FXConverters.toFXColor | ||
import tools.aqua.bgw.builder.FXConverters.toFXImage | ||
import tools.aqua.bgw.components.gamecomponentviews.HexagonView | ||
import tools.aqua.bgw.visual.* | ||
|
||
object HexagonBuilder { | ||
/** Degrees in a circle. */ | ||
private const val FULL_CIRCLE_DEGREES = 360 | ||
|
||
/** Amount of sides in a hexagon. */ | ||
private const val HEXAGON_SIDES = 6 | ||
|
||
private const val BORDER_WIDTH = 50.0 | ||
|
||
/** Builds [HexagonView]. */ | ||
internal fun buildHexagonView(hexagonView: HexagonView): Region { | ||
val points = generatePoints(hexagonView.size.toDouble()) | ||
return when (val visual = hexagonView.visual) { | ||
is TextVisual -> buildPolygon(points, visual) | ||
is SingleLayerVisual -> buildPolygon(points, visual) | ||
is CompoundVisual -> buildPolygon(points, visual) | ||
}.also { hexagonView.visual = Visual.EMPTY } | ||
} | ||
|
||
private fun buildPolygon(points: DoubleArray, compoundVisual: CompoundVisual): Region = | ||
StackPane( | ||
*compoundVisual.children | ||
.map { | ||
when (it) { | ||
is TextVisual -> buildPolygon(points, it) | ||
else -> buildPolygon(points, it) | ||
} | ||
} | ||
.toTypedArray()) | ||
.apply { isPickOnBounds = false } | ||
|
||
private fun buildPolygon(points: DoubleArray, visual: SingleLayerVisual): Region = | ||
Pane( | ||
Polygon(*points).apply { | ||
val paint = buildPaint(visual) | ||
fill = paint | ||
visual.transparencyProperty.addListenerAndInvoke(visual.transparency) { _, nV -> | ||
opacity = nV | ||
} | ||
stroke = Color.BLACK | ||
strokeType = StrokeType.INSIDE | ||
// roundCorners(paint) | ||
}) | ||
.apply { isPickOnBounds = false } | ||
|
||
private fun buildPolygon(points: DoubleArray, visual: TextVisual): Region = | ||
StackPane( | ||
buildPolygon(points, visual as SingleLayerVisual), | ||
VisualBuilder.buildVisual(visual).apply { isPickOnBounds = false }) | ||
.apply { isPickOnBounds = false } | ||
|
||
private fun Polygon.roundCorners(paint: Paint) { | ||
stroke = paint | ||
strokeWidth = BORDER_WIDTH | ||
strokeType = StrokeType.CENTERED | ||
strokeLineJoin = StrokeLineJoin.ROUND | ||
strokeLineCap = StrokeLineCap.ROUND | ||
strokeMiterLimit = BORDER_WIDTH | ||
} | ||
|
||
private fun generatePoints(size: Double): DoubleArray { | ||
val points = mutableListOf<Double>() | ||
var angle = 90.0 | ||
for (i in 0 until HEXAGON_SIDES) { | ||
val x = size * cos(Math.toRadians(angle)) + size | ||
val y = size * sin(Math.toRadians(angle)) + size | ||
angle += FULL_CIRCLE_DEGREES / HEXAGON_SIDES | ||
points.add(x) | ||
points.add(y) | ||
} | ||
return points.toDoubleArray() | ||
} | ||
|
||
private fun buildPaint(visual: SingleLayerVisual): Paint { | ||
return when (visual) { | ||
is ColorVisual -> visual.color.toFXColor() | ||
is ImageVisual -> ImagePattern(visual.image.toFXImage()) | ||
is TextVisual -> Color.TRANSPARENT | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
bgw-gui/src/main/kotlin/tools/aqua/bgw/components/gamecomponentviews/HexagonView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2023 The BoardGameWork Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package tools.aqua.bgw.components.gamecomponentviews | ||
|
||
import kotlin.math.sqrt | ||
import tools.aqua.bgw.core.DEFAULT_HEXAGON_SIZE | ||
import tools.aqua.bgw.visual.Visual | ||
|
||
/** | ||
* A [HexagonView] represents a hexagonal shaped game component view. | ||
* | ||
* @constructor Creates a [HexagonView] with a given [Visual]. | ||
* | ||
* @param posX Horizontal coordinate for this [HexagonView]. Default: 0. | ||
* @param posY Vertical coordinate for this [HexagonView]. Default: 0. | ||
* @param size Represents the radius of the outer circle of the [HexagonView] all six points lie on. | ||
* Default: [DEFAULT_HEXAGON_SIZE]. | ||
* @param visual Visual for this [HexagonView]. | ||
*/ | ||
open class HexagonView( | ||
posX: Number = 0, | ||
posY: Number = 0, | ||
val size: Number = DEFAULT_HEXAGON_SIZE, | ||
visual: Visual | ||
) : | ||
GameComponentView( | ||
posX = posX, | ||
posY = posY, | ||
width = sqrt(3.0) * size.toDouble(), | ||
height = 2 * size.toDouble(), | ||
visual = visual) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters