[Editor] Enable tiles selection in TileSetCanvas
This commit is contained in:
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.editor.command.service
|
||||
|
||||
import com.bartlomiejpluta.base.editor.command.model.Undoable
|
||||
import org.apache.commons.logging.LogFactory
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Component
|
||||
import java.util.*
|
||||
|
||||
@@ -65,6 +66,6 @@ class DefaultUndoRedoService : UndoRedoService {
|
||||
get() = redo.last?.commandName ?: ""
|
||||
|
||||
companion object {
|
||||
private val log = LogFactory.getLog(DefaultUndoRedoService::class.java)
|
||||
private val log = LoggerFactory.getLogger(DefaultUndoRedoService::class.java)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.bartlomiejpluta.base.editor.render.canvas.map
|
||||
package com.bartlomiejpluta.base.editor.render.canvas.input
|
||||
|
||||
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
||||
import javafx.event.EventType
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.bartlomiejpluta.base.editor.render.canvas.map
|
||||
package com.bartlomiejpluta.base.editor.render.canvas.input
|
||||
|
||||
interface MapMouseEventHandler {
|
||||
fun handleMouseInput(event: MapMouseEvent)
|
||||
@@ -2,6 +2,8 @@ package com.bartlomiejpluta.base.editor.render.canvas.map
|
||||
|
||||
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||
import com.bartlomiejpluta.base.editor.model.tileset.Tile
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEventHandler
|
||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
import javafx.scene.input.MouseButton
|
||||
@@ -59,7 +61,7 @@ class MapBrush(
|
||||
|
||||
when (event.type) {
|
||||
MouseEvent.MOUSE_PRESSED -> beginTrace(event)
|
||||
MouseEvent.MOUSE_DRAGGED -> proceedTrace()
|
||||
MouseEvent.MOUSE_DRAGGED -> proceedTrace(event)
|
||||
MouseEvent.MOUSE_RELEASED -> commitTrace(event)
|
||||
}
|
||||
}
|
||||
@@ -76,7 +78,8 @@ class MapBrush(
|
||||
}
|
||||
}
|
||||
|
||||
private fun proceedTrace() {
|
||||
private fun proceedTrace(event: MapMouseEvent) {
|
||||
if (event.button == MouseButton.PRIMARY) {
|
||||
currentTrace?.apply {
|
||||
for ((row, columns) in brush.withIndex()) {
|
||||
for ((column, tile) in columns.withIndex()) {
|
||||
@@ -85,6 +88,7 @@ class MapBrush(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun commitTrace(event: MapMouseEvent) {
|
||||
if (event.button == MouseButton.PRIMARY) {
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.bartlomiejpluta.base.editor.model.map.layer.Layer
|
||||
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
|
||||
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||
import com.bartlomiejpluta.base.editor.model.tileset.Tile
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEventHandler
|
||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
|
||||
|
||||
@@ -1,16 +1,26 @@
|
||||
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.canvas.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEventHandler
|
||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
import javafx.scene.input.MouseButton
|
||||
import javafx.scene.input.MouseEvent
|
||||
import javafx.scene.paint.Color
|
||||
|
||||
class TileSetCanvas(private val tileSet: TileSet) : Renderable, MapMouseEventHandler {
|
||||
private val tiles = tileSet.tiles
|
||||
private var selection = TileSetSelection(tileSet)
|
||||
|
||||
private var mouseRow = -1
|
||||
private var mouseColumn = -1
|
||||
|
||||
override fun render(gc: GraphicsContext) {
|
||||
gc.clearRect(0.0, 0.0, gc.canvas.width, gc.canvas.height);
|
||||
|
||||
renderTiles(gc)
|
||||
selection.render(gc)
|
||||
}
|
||||
|
||||
private fun renderTiles(gc: GraphicsContext) {
|
||||
@@ -22,6 +32,24 @@ class TileSetCanvas(private val tileSet: TileSet) : Renderable, MapMouseEventHan
|
||||
}
|
||||
|
||||
override fun handleMouseInput(event: MapMouseEvent) {
|
||||
mouseRow = event.row
|
||||
mouseColumn = event.column
|
||||
|
||||
when (event.type) {
|
||||
MouseEvent.MOUSE_PRESSED -> beginSelection(event)
|
||||
MouseEvent.MOUSE_DRAGGED -> proceedSelection(event)
|
||||
}
|
||||
}
|
||||
|
||||
private fun beginSelection(event: MapMouseEvent) {
|
||||
if(event.button == MouseButton.PRIMARY) {
|
||||
selection.begin(event.row.toDouble(), event.column.toDouble())
|
||||
}
|
||||
}
|
||||
|
||||
private fun proceedSelection(event: MapMouseEvent) {
|
||||
if(event.button == MouseButton.PRIMARY) {
|
||||
selection.finish(event.row.toDouble(), event.column.toDouble())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.bartlomiejpluta.base.editor.render.canvas.tileset
|
||||
|
||||
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
import javafx.scene.paint.Color
|
||||
import org.apache.commons.logging.LogFactory
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.min
|
||||
|
||||
|
||||
class TileSetSelection(tileSet: TileSet) : Renderable {
|
||||
private val tileWidth = tileSet.tileWidth.toDouble()
|
||||
private val tileHeight = tileSet.tileHeight.toDouble()
|
||||
|
||||
private var startRow = 0.0
|
||||
private var startColumn = 0.0
|
||||
private var offsetRow = 0.0
|
||||
private var offsetColumn = 0.0
|
||||
private var x = 0.0
|
||||
private var y = 0.0
|
||||
private var width = tileWidth
|
||||
private var height = tileHeight
|
||||
|
||||
fun begin(row: Double, column: Double) {
|
||||
startRow = row
|
||||
offsetRow = 0.0
|
||||
startColumn = column
|
||||
offsetColumn = 0.0
|
||||
|
||||
updateRect(row, column)
|
||||
}
|
||||
|
||||
private fun updateRect(row: Double, column: Double) {
|
||||
x = min(column, startColumn) * tileWidth
|
||||
y = min(row, startRow) * tileHeight
|
||||
width = (offsetColumn + 1) * tileWidth
|
||||
height = (offsetRow + 1) * tileHeight
|
||||
}
|
||||
|
||||
fun finish(row: Double, column: Double) {
|
||||
offsetRow = abs(row - startRow)
|
||||
offsetColumn = abs(column - startColumn)
|
||||
|
||||
updateRect(row, column)
|
||||
}
|
||||
|
||||
override fun render(gc: GraphicsContext) {
|
||||
gc.fill = SELECTION_FILL_COLOR
|
||||
gc.fillRect(x, y, width, height)
|
||||
|
||||
gc.stroke = SELECTION_STROKE_COLOR
|
||||
gc.strokeRect(x, y, width, height)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val SELECTION_FILL_COLOR = Color.color(0.0, 0.7, 1.0, 0.4)
|
||||
private val SELECTION_STROKE_COLOR = Color.color(0.0, 0.7, 1.0, 1.0)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.bartlomiejpluta.base.editor.view.component.map
|
||||
|
||||
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.map.MapCanvas
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.map.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace
|
||||
import javafx.event.EventHandler
|
||||
import javafx.scene.canvas.Canvas
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.tileset.TileSetCanvas
|
||||
import javafx.event.EventHandler
|
||||
import javafx.scene.canvas.Canvas
|
||||
|
||||
Reference in New Issue
Block a user