Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package nl.ndat.isometric.ui
import nl.ndat.isometric.Bootstrap
import nl.ndat.isometric.core.IView
import org.w3c.dom.CanvasRenderingContext2D
import org.w3c.dom.Path2D
class Tile : IView() {
val styleInside = "#8BB255"
val styleInsideHover = "#54b2a5"
val styleBottomLeft = "#7DA14D"
val styleBottomRight = "#749548"
val tileThickness = 15.0
val tileWidth = 200.0
val tileHeight = 100.0
private var hover = false
val pathTop: Path2D = Path2D()
val pathBottomLeft: Path2D = Path2D()
val pathBottomRight: Path2D = Path2D()
override var width: Double = 0.0
get() = tileWidth
override var height: Double = 0.0
get() = tileHeight + tileThickness
init {
pathTop.run {
moveTo(tileWidth / 2, 0.0)
lineTo(tileWidth, tileHeight / 2)
lineTo(tileWidth / 2, tileHeight + 1)
lineTo(0.0, tileHeight / 2)
closePath()
}
pathBottomLeft.run {
moveTo(tileWidth / 2, tileHeight + tileThickness)
lineTo(0.0, tileHeight / 2 + tileThickness)
lineTo(0.0, tileHeight / 2)
lineTo(tileWidth / 2, tileHeight)
closePath()
}
pathBottomRight.run {
moveTo(tileWidth, tileHeight / 2)
lineTo(tileWidth, tileHeight / 2 + tileThickness)
lineTo(tileWidth / 2, tileHeight + tileThickness)
lineTo(tileWidth / 2, tileHeight)
closePath()
}
}
override fun tick(bootstrap: Bootstrap) {
hover = false
var (x, y) = this.getCalculatedPosition(bootstrap.context)
x += this.x
y += this.y
hover = bootstrap.context.isPointInPath(pathTop, bootstrap.mouseX, bootstrap.mouseY)
}
override fun draw(context: CanvasRenderingContext2D) {
if (tileThickness > 0) {
context.fillStyle = styleBottomLeft
context.fill(pathBottomLeft)
context.fillStyle = styleBottomRight
context.beginPath()
context.fill(pathBottomRight)
}
context.fillStyle = if (hover) styleInside else styleInsideHover
context.fill(pathTop)
super.draw(context)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.