[Editor] Enable ImageLayer positioning
This commit is contained in:
@@ -18,6 +18,14 @@ class IntegerParameter(
|
||||
editor.textFormatter = TextFieldUtil.integerFormatter(initialValue)
|
||||
}
|
||||
|
||||
constructor(
|
||||
key: String,
|
||||
initialValue: Int,
|
||||
editable: Boolean = true,
|
||||
autocommit: Boolean = false,
|
||||
onCommit: (oldValue: Int, newValue: Int, submit: () -> Unit) -> Unit = { _, _, submit -> submit() }
|
||||
) : this(key, initialValue, Integer.MIN_VALUE, Integer.MAX_VALUE, editable, autocommit, onCommit)
|
||||
|
||||
override val editorValueProperty: Property<Int>
|
||||
get() = editor.valueFactory.valueProperty()
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.bartlomiejpluta.base.editor.map.canvas
|
||||
|
||||
import com.bartlomiejpluta.base.editor.map.model.layer.ImageLayer
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
|
||||
import com.bartlomiejpluta.base.editor.render.input.MapMouseEvent
|
||||
|
||||
class ImagePositionPaintingTrace(val map: GameMapVM, override val commandName: String) : PaintingTrace {
|
||||
private var layerIndex = 0
|
||||
private var originX = 0.0
|
||||
private var originY = 0.0
|
||||
private var x = 0.0
|
||||
private var y = 0.0
|
||||
private var dx = 0.0
|
||||
private var dy = 0.0
|
||||
private var newX = 0.0
|
||||
private var newY = 0.0
|
||||
private lateinit var layer: ImageLayer
|
||||
|
||||
override fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
this.layerIndex = editorStateVM.selectedLayerIndex
|
||||
|
||||
if (layerIndex < 0) {
|
||||
return
|
||||
}
|
||||
|
||||
this.layer = (map.layers[layerIndex] as ImageLayer)
|
||||
|
||||
originX = layer.x.toDouble()
|
||||
originY = layer.y.toDouble()
|
||||
|
||||
x = mouseEvent.event.x
|
||||
y = mouseEvent.event.y
|
||||
}
|
||||
|
||||
override fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
dx = mouseEvent.event.x - x
|
||||
dy = mouseEvent.event.y - y
|
||||
|
||||
newX = originX + dx
|
||||
newY = originY + dy
|
||||
|
||||
layer.x = newX.toInt()
|
||||
layer.y = newY.toInt()
|
||||
}
|
||||
|
||||
override fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
dx = mouseEvent.event.x - x
|
||||
dy = mouseEvent.event.y - y
|
||||
|
||||
newX = originX + dx
|
||||
newY = originY + dy
|
||||
|
||||
layer.x = newX.toInt()
|
||||
layer.y = newY.toInt()
|
||||
}
|
||||
|
||||
override fun undo() {
|
||||
layer.x = originX.toInt()
|
||||
layer.y = originY.toInt()
|
||||
}
|
||||
|
||||
override fun redo() {
|
||||
layer.x = newX.toInt()
|
||||
layer.y = newY.toInt()
|
||||
}
|
||||
}
|
||||
@@ -135,10 +135,12 @@ class MapCanvas(val map: GameMapVM, private val editorStateVM: EditorStateVM, pr
|
||||
val alpha = gc.globalAlpha
|
||||
gc.globalAlpha = imageLayer.opacity / 100.0
|
||||
|
||||
val x = imageLayer.x.toDouble()
|
||||
val y = imageLayer.y.toDouble()
|
||||
when (imageLayer.mode) {
|
||||
ImageLayerMode.NORMAL -> gc.drawImage(imageLayer.image, 0.0, 0.0)
|
||||
ImageLayerMode.FIT_SCREEN -> gc.drawImage(imageLayer.image, 0.0, 0.0)
|
||||
ImageLayerMode.FIT_MAP -> gc.drawImage(imageLayer.image, 0.0, 0.0, map.width, map.height)
|
||||
ImageLayerMode.NORMAL -> gc.drawImage(imageLayer.image, x, y)
|
||||
ImageLayerMode.FIT_SCREEN -> gc.drawImage(imageLayer.image, x, y)
|
||||
ImageLayerMode.FIT_MAP -> gc.drawImage(imageLayer.image, x, y, map.width, map.height)
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.map.canvas
|
||||
|
||||
import com.bartlomiejpluta.base.editor.map.model.layer.ImageLayer
|
||||
import com.bartlomiejpluta.base.editor.map.model.layer.ObjectLayer
|
||||
import com.bartlomiejpluta.base.editor.map.model.layer.TileLayer
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.BrushVM
|
||||
@@ -59,21 +60,22 @@ class MapPainter(
|
||||
currentTrace = when (editorStateVM.selectedLayer) {
|
||||
is TileLayer -> TilePaintingTrace(mapVM, "Paint trace")
|
||||
is ObjectLayer -> ObjectPaintingTrace(mapVM, "Toggle passage")
|
||||
is ImageLayer -> ImagePositionPaintingTrace(mapVM, "Move Image Layer")
|
||||
else -> null
|
||||
}?.apply { beginTrace(editorStateVM, brushVM) }
|
||||
}?.apply { beginTrace(editorStateVM, brushVM, event) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun proceedTrace(event: MapMouseEvent) {
|
||||
if (event.button == MouseButton.PRIMARY) {
|
||||
currentTrace?.proceedTrace(editorStateVM, brushVM)
|
||||
currentTrace?.proceedTrace(editorStateVM, brushVM, event)
|
||||
}
|
||||
}
|
||||
|
||||
private fun commitTrace(event: MapMouseEvent) {
|
||||
if (event.button == MouseButton.PRIMARY) {
|
||||
currentTrace?.let {
|
||||
it.commitTrace(editorStateVM, brushVM)
|
||||
it.commitTrace(editorStateVM, brushVM, event)
|
||||
paintingCallback(it)
|
||||
currentTrace = null
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.bartlomiejpluta.base.editor.map.model.layer.ObjectLayer
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
|
||||
import com.bartlomiejpluta.base.editor.render.input.MapMouseEvent
|
||||
|
||||
class ObjectPaintingTrace(val map: GameMapVM, override val commandName: String) : PaintingTrace {
|
||||
private var layerIndex = 0
|
||||
@@ -16,15 +17,15 @@ class ObjectPaintingTrace(val map: GameMapVM, override val commandName: String)
|
||||
private lateinit var passageAbility: PassageAbility
|
||||
|
||||
|
||||
override fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM) {
|
||||
override fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
override fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM) {
|
||||
override fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
override fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM) {
|
||||
override fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
this.layerIndex = editorStateVM.selectedLayerIndex
|
||||
this.row = editorStateVM.cursorRow
|
||||
this.column = editorStateVM.cursorColumn
|
||||
|
||||
@@ -3,9 +3,10 @@ package com.bartlomiejpluta.base.editor.map.canvas
|
||||
import com.bartlomiejpluta.base.editor.command.model.base.Undoable
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM
|
||||
import com.bartlomiejpluta.base.editor.render.input.MapMouseEvent
|
||||
|
||||
interface PaintingTrace : Undoable {
|
||||
fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM)
|
||||
fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM)
|
||||
fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM)
|
||||
fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent)
|
||||
fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent)
|
||||
fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.bartlomiejpluta.base.editor.map.model.layer.TileLayer
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
|
||||
import com.bartlomiejpluta.base.editor.render.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.tileset.model.Tile
|
||||
|
||||
|
||||
@@ -33,7 +34,7 @@ data class TilePaintingTrace(val map: GameMapVM, override val commandName: Strin
|
||||
}
|
||||
}
|
||||
|
||||
override fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM) {
|
||||
override fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
brushVM.forEach { row, column, centerRow, centerColumn, tile ->
|
||||
paint(
|
||||
editorStateVM.selectedLayerIndex,
|
||||
@@ -44,7 +45,7 @@ data class TilePaintingTrace(val map: GameMapVM, override val commandName: Strin
|
||||
}
|
||||
}
|
||||
|
||||
override fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM) {
|
||||
override fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
brushVM.forEach { row, column, centerRow, centerColumn, tile ->
|
||||
paint(
|
||||
editorStateVM.selectedLayerIndex,
|
||||
@@ -55,7 +56,7 @@ data class TilePaintingTrace(val map: GameMapVM, override val commandName: Strin
|
||||
}
|
||||
}
|
||||
|
||||
override fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM) {
|
||||
override fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import javafx.scene.image.Image
|
||||
import tornadofx.getValue
|
||||
import tornadofx.setValue
|
||||
|
||||
class ImageLayer(name: String, imageAsset: ImageAsset, mode: ImageLayerMode, opacity: Int) : Layer {
|
||||
class ImageLayer(name: String, imageAsset: ImageAsset, x: Int, y: Int, mode: ImageLayerMode, opacity: Int) : Layer {
|
||||
override val nameProperty = SimpleStringProperty(name)
|
||||
|
||||
override var name by nameProperty
|
||||
@@ -29,6 +29,13 @@ class ImageLayer(name: String, imageAsset: ImageAsset, mode: ImageLayerMode, opa
|
||||
val modeProperty = SimpleObjectProperty(mode)
|
||||
var mode by modeProperty
|
||||
|
||||
val xProperty = SimpleObjectProperty(x)
|
||||
var x by xProperty
|
||||
|
||||
val yProperty = SimpleObjectProperty(y)
|
||||
var y by yProperty
|
||||
|
||||
|
||||
override fun resize(rows: Int, columns: Int) {
|
||||
// We essentially need to do nothing
|
||||
}
|
||||
|
||||
@@ -32,10 +32,20 @@ class ImageLayerParametersBinder : LayerParametersBinder<ImageLayer> {
|
||||
onCommit()
|
||||
}
|
||||
|
||||
val x = IntegerParameter("x", 0, autocommit = true) { _, _, _ ->
|
||||
onCommit()
|
||||
}
|
||||
|
||||
val y = IntegerParameter("y", 0, autocommit = true) { _, _, _ ->
|
||||
onCommit()
|
||||
}
|
||||
|
||||
image.bindBidirectional(layer.imageAssetProperty)
|
||||
opacity.bindBidirectional(layer.opacityProperty)
|
||||
mode.bindBidirectional(layer.modeProperty)
|
||||
x.bindBidirectional(layer.xProperty)
|
||||
y.bindBidirectional(layer.yProperty)
|
||||
|
||||
parameters.addAll(image, opacity, mode)
|
||||
parameters.addAll(image, opacity, mode, x, y)
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,14 @@ class MapLayersView : View() {
|
||||
find<SelectGraphicAssetFragment>(scope, SelectGraphicAssetFragment::assets to projectContext.project?.images!!).apply {
|
||||
onComplete {
|
||||
val layer =
|
||||
ImageLayer("Layer ${mapVM.layers.size + 1}", it as ImageAsset, ImageLayerMode.NORMAL, 100)
|
||||
ImageLayer(
|
||||
"Layer ${mapVM.layers.size + 1}",
|
||||
it as ImageAsset,
|
||||
0,
|
||||
0,
|
||||
ImageLayerMode.NORMAL,
|
||||
100
|
||||
)
|
||||
val command = CreateLayerCommand(mapVM.item, layer)
|
||||
command.execute()
|
||||
layersPane.selectionModel.select(mapVM.layers.size - 1)
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
package com.bartlomiejpluta.base.editor.render.input
|
||||
|
||||
import com.bartlomiejpluta.base.editor.tileset.model.TileSet
|
||||
import javafx.event.EventType
|
||||
import javafx.scene.input.MouseButton
|
||||
import javafx.scene.input.MouseEvent
|
||||
|
||||
class MapMouseEvent(val row: Int, val column: Int, val type: EventType<out MouseEvent>, val button: MouseButton) {
|
||||
class MapMouseEvent(val row: Int, val column: Int, val event: MouseEvent) {
|
||||
val type = event.eventType
|
||||
val button = event.button
|
||||
|
||||
companion object {
|
||||
fun of(event: MouseEvent, tileSet: TileSet) = MapMouseEvent(
|
||||
(event.y / tileSet.tileHeight).toInt(),
|
||||
(event.x / tileSet.tileWidth).toInt(),
|
||||
event.eventType,
|
||||
event.button
|
||||
event
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user