[Editor] Apply TileSetPane to the MapFragment
This commit is contained in:
@@ -8,10 +8,11 @@ import java.nio.ByteBuffer
|
|||||||
|
|
||||||
class TileSet(private val image: Image, val rows: Int, val columns: Int) {
|
class TileSet(private val image: Image, val rows: Int, val columns: Int) {
|
||||||
val tileWidth = image.width.toInt() / columns
|
val tileWidth = image.width.toInt() / columns
|
||||||
|
|
||||||
val tileHeight = image.height.toInt() / rows
|
val tileHeight = image.height.toInt() / rows
|
||||||
|
val width = tileWidth * columns
|
||||||
|
val height = tileHeight * rows
|
||||||
|
|
||||||
private val tiles: Array<Array<Tile>> =
|
val tiles: Array<Array<Tile>> =
|
||||||
Array(rows) { row -> Array(columns) { column -> cropTile(row, column) } }
|
Array(rows) { row -> Array(columns) { column -> cropTile(row, column) } }
|
||||||
|
|
||||||
private fun cropTile(row: Int, column: Int): Tile {
|
private fun cropTile(row: Int, column: Int): Tile {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
|||||||
import com.bartlomiejpluta.base.editor.model.tileset.Tile
|
import com.bartlomiejpluta.base.editor.model.tileset.Tile
|
||||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||||
import javafx.scene.canvas.GraphicsContext
|
import javafx.scene.canvas.GraphicsContext
|
||||||
|
import javafx.scene.input.MouseButton
|
||||||
import javafx.scene.input.MouseEvent
|
import javafx.scene.input.MouseEvent
|
||||||
|
|
||||||
class MapBrush(
|
class MapBrush(
|
||||||
@@ -57,17 +58,19 @@ class MapBrush(
|
|||||||
mouseColumn = event.column
|
mouseColumn = event.column
|
||||||
|
|
||||||
when (event.type) {
|
when (event.type) {
|
||||||
MouseEvent.MOUSE_PRESSED -> beginTrace()
|
MouseEvent.MOUSE_PRESSED -> beginTrace(event)
|
||||||
MouseEvent.MOUSE_DRAGGED -> proceedTrace()
|
MouseEvent.MOUSE_DRAGGED -> proceedTrace()
|
||||||
MouseEvent.MOUSE_RELEASED -> commitTrace()
|
MouseEvent.MOUSE_RELEASED -> commitTrace(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun beginTrace() {
|
private fun beginTrace(event: MapMouseEvent) {
|
||||||
currentTrace = MapPaintingTrace(map, "Paint trace").apply {
|
if (event.button == MouseButton.PRIMARY) {
|
||||||
for ((row, columns) in brush.withIndex()) {
|
currentTrace = MapPaintingTrace(map, "Paint trace").apply {
|
||||||
for ((column, tile) in columns.withIndex()) {
|
for ((row, columns) in brush.withIndex()) {
|
||||||
paint(0, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile)
|
for ((column, tile) in columns.withIndex()) {
|
||||||
|
paint(0, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,10 +86,12 @@ class MapBrush(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun commitTrace() {
|
private fun commitTrace(event: MapMouseEvent) {
|
||||||
currentTrace?.let {
|
if (event.button == MouseButton.PRIMARY) {
|
||||||
paintingCallback(it)
|
currentTrace?.let {
|
||||||
currentTrace = null
|
paintingCallback(it)
|
||||||
|
currentTrace = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,15 +2,17 @@ package com.bartlomiejpluta.base.editor.render.canvas.map
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
||||||
import javafx.event.EventType
|
import javafx.event.EventType
|
||||||
|
import javafx.scene.input.MouseButton
|
||||||
import javafx.scene.input.MouseEvent
|
import javafx.scene.input.MouseEvent
|
||||||
|
|
||||||
class MapMouseEvent(val row: Int, val column: Int, val type: EventType<out MouseEvent>) {
|
class MapMouseEvent(val row: Int, val column: Int, val type: EventType<out MouseEvent>, val button: MouseButton) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun of(event: MouseEvent, tileSet: TileSet) = MapMouseEvent(
|
fun of(event: MouseEvent, tileSet: TileSet) = MapMouseEvent(
|
||||||
(event.y / tileSet.tileHeight).toInt(),
|
(event.y / tileSet.tileHeight).toInt(),
|
||||||
(event.x / tileSet.tileWidth).toInt(),
|
(event.x / tileSet.tileWidth).toInt(),
|
||||||
event.eventType
|
event.eventType,
|
||||||
|
event.button
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.render.canvas.tileset
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
||||||
|
import com.bartlomiejpluta.base.editor.render.canvas.map.MapMouseEvent
|
||||||
|
import com.bartlomiejpluta.base.editor.render.canvas.map.MapMouseEventHandler
|
||||||
|
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||||
|
import javafx.scene.canvas.GraphicsContext
|
||||||
|
|
||||||
|
class TileSetCanvas(private val tileSet: TileSet) : Renderable, MapMouseEventHandler {
|
||||||
|
private val tiles = tileSet.tiles
|
||||||
|
|
||||||
|
override fun render(gc: GraphicsContext) {
|
||||||
|
renderTiles(gc)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun renderTiles(gc: GraphicsContext) {
|
||||||
|
for ((row, columns) in tiles.withIndex()) {
|
||||||
|
for ((column, tile) in columns.withIndex()) {
|
||||||
|
gc.drawImage(tile.image, column * tile.image.width, row * tile.image.height)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun handleMouseInput(event: MapMouseEvent) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.view.component.tileset
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
||||||
|
import com.bartlomiejpluta.base.editor.render.canvas.map.MapMouseEvent
|
||||||
|
import com.bartlomiejpluta.base.editor.render.canvas.tileset.TileSetCanvas
|
||||||
|
import javafx.event.EventHandler
|
||||||
|
import javafx.scene.canvas.Canvas
|
||||||
|
import javafx.scene.input.MouseEvent
|
||||||
|
|
||||||
|
class TileSetPane(private val tileSet: TileSet) : Canvas(), EventHandler<MouseEvent> {
|
||||||
|
private val tileSetCanvas = TileSetCanvas(tileSet)
|
||||||
|
|
||||||
|
init {
|
||||||
|
onMouseMoved = this
|
||||||
|
onMouseDragged = this
|
||||||
|
onMousePressed = this
|
||||||
|
onMouseReleased = this
|
||||||
|
|
||||||
|
width = tileSet.width.toDouble()
|
||||||
|
height = tileSet.height.toDouble()
|
||||||
|
|
||||||
|
render()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun render() {
|
||||||
|
tileSetCanvas.render(graphicsContext2D)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun handle(event: MouseEvent?) {
|
||||||
|
if (event != null) {
|
||||||
|
tileSetCanvas.handleMouseInput(MapMouseEvent.of(event, tileSet))
|
||||||
|
}
|
||||||
|
|
||||||
|
tileSetCanvas.render(graphicsContext2D)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,10 @@ import com.bartlomiejpluta.base.editor.command.service.UndoRedoService
|
|||||||
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
|
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
|
||||||
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||||
import com.bartlomiejpluta.base.editor.view.component.map.MapPane
|
import com.bartlomiejpluta.base.editor.view.component.map.MapPane
|
||||||
|
import com.bartlomiejpluta.base.editor.view.component.tileset.TileSetPane
|
||||||
import javafx.beans.property.SimpleDoubleProperty
|
import javafx.beans.property.SimpleDoubleProperty
|
||||||
|
import javafx.scene.input.MouseButton
|
||||||
|
import javafx.scene.input.MouseEvent
|
||||||
import javafx.scene.transform.Scale
|
import javafx.scene.transform.Scale
|
||||||
import tornadofx.*
|
import tornadofx.*
|
||||||
|
|
||||||
@@ -25,15 +28,30 @@ class MapFragment : Fragment() {
|
|||||||
subscribe<RedrawMapRequestEvent> { pane.render() }
|
subscribe<RedrawMapRequestEvent> { pane.render() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override val root = scrollpane {
|
override val root = borderpane {
|
||||||
prefWidth = 640.0
|
center = scrollpane {
|
||||||
prefHeight = 480.0
|
prefWidth = 640.0
|
||||||
|
prefHeight = 480.0
|
||||||
|
isPannable = true
|
||||||
|
|
||||||
group {
|
|
||||||
group {
|
group {
|
||||||
this += pane
|
|
||||||
transforms += transformation
|
// Let the ScrollPane.viewRect only pan on middle button.
|
||||||
|
addEventHandler(MouseEvent.ANY) {
|
||||||
|
if(it.button != MouseButton.MIDDLE) {
|
||||||
|
it.consume()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
group {
|
||||||
|
this += pane
|
||||||
|
transforms += transformation
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
right = scrollpane {
|
||||||
|
this += TileSetPane(map.tileSet)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user