[Editor] Enable controlling color and opacity of ImageLayer via parameters
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.map.canvas
|
package com.bartlomiejpluta.base.editor.map.canvas
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.map.model.layer.ImageLayer
|
||||||
import com.bartlomiejpluta.base.editor.map.model.layer.Layer
|
import com.bartlomiejpluta.base.editor.map.model.layer.Layer
|
||||||
import com.bartlomiejpluta.base.editor.map.model.layer.ObjectLayer
|
import com.bartlomiejpluta.base.editor.map.model.layer.ObjectLayer
|
||||||
import com.bartlomiejpluta.base.editor.map.model.layer.TileLayer
|
import com.bartlomiejpluta.base.editor.map.model.layer.TileLayer
|
||||||
@@ -50,6 +51,7 @@ class MapCanvas(val map: GameMapVM, private val editorStateVM: EditorStateVM, pr
|
|||||||
when (layer) {
|
when (layer) {
|
||||||
is TileLayer -> renderTileLayer(gc, layer)
|
is TileLayer -> renderTileLayer(gc, layer)
|
||||||
is ObjectLayer -> renderObjectPassageMap(gc, layer)
|
is ObjectLayer -> renderObjectPassageMap(gc, layer)
|
||||||
|
is ImageLayer -> renderImageLayer(gc, layer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +86,18 @@ class MapCanvas(val map: GameMapVM, private val editorStateVM: EditorStateVM, pr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun renderImageLayer(gc: GraphicsContext, imageLayer: ImageLayer) {
|
||||||
|
val alpha = gc.globalAlpha
|
||||||
|
val color = gc.fill
|
||||||
|
|
||||||
|
gc.globalAlpha = imageLayer.alpha / 100.0
|
||||||
|
gc.fill = Color.color(imageLayer.red / 100.0, imageLayer.green / 100.0, imageLayer.blue / 100.0)
|
||||||
|
gc.fillRect(0.0, 0.0, map.width, map.height)
|
||||||
|
|
||||||
|
gc.globalAlpha = alpha
|
||||||
|
gc.fill = color
|
||||||
|
}
|
||||||
|
|
||||||
private fun renderGrid(gc: GraphicsContext) {
|
private fun renderGrid(gc: GraphicsContext) {
|
||||||
if (!editorStateVM.showGrid) {
|
if (!editorStateVM.showGrid) {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.map.model.layer
|
package com.bartlomiejpluta.base.editor.map.model.layer
|
||||||
|
|
||||||
|
import javafx.beans.property.SimpleObjectProperty
|
||||||
import javafx.beans.property.SimpleStringProperty
|
import javafx.beans.property.SimpleStringProperty
|
||||||
import tornadofx.getValue
|
import tornadofx.getValue
|
||||||
import tornadofx.setValue
|
import tornadofx.setValue
|
||||||
@@ -7,9 +8,21 @@ import tornadofx.setValue
|
|||||||
class ImageLayer(name: String) : Layer {
|
class ImageLayer(name: String) : Layer {
|
||||||
override val nameProperty = SimpleStringProperty(name)
|
override val nameProperty = SimpleStringProperty(name)
|
||||||
|
|
||||||
|
override var name by nameProperty
|
||||||
|
|
||||||
|
val redProperty = SimpleObjectProperty(100)
|
||||||
|
var red by redProperty
|
||||||
|
|
||||||
|
val greenProperty = SimpleObjectProperty(100)
|
||||||
|
var green by greenProperty
|
||||||
|
|
||||||
|
val blueProperty = SimpleObjectProperty(100)
|
||||||
|
var blue by blueProperty
|
||||||
|
|
||||||
|
val alphaProperty = SimpleObjectProperty(100)
|
||||||
|
var alpha by alphaProperty
|
||||||
|
|
||||||
override fun resize(rows: Int, columns: Int) {
|
override fun resize(rows: Int, columns: Int) {
|
||||||
// We essentially need to do nothing
|
// We essentially need to do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
override var name by nameProperty
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.map.parameter.layer
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.model.IntegerParameter
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.model.Parameter
|
||||||
|
import com.bartlomiejpluta.base.editor.map.model.layer.ImageLayer
|
||||||
|
import javafx.collections.ObservableList
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class ImageLayerParametersBinder : LayerParametersBinder<ImageLayer> {
|
||||||
|
override fun bind(layer: ImageLayer, parameters: ObservableList<Parameter<*>>, onCommit: () -> Unit) {
|
||||||
|
val red = IntegerParameter("red", 100, 0, 100, autocommit = true) { _, _, _ -> onCommit() }
|
||||||
|
val green = IntegerParameter("green", 100, 0, 100, autocommit = true) { _, _, _ -> onCommit() }
|
||||||
|
val blue = IntegerParameter("blue", 100, 0, 100, autocommit = true) { _, _, _ -> onCommit() }
|
||||||
|
val alpha = IntegerParameter("alpha", 100, 0, 100, autocommit = true) { _, _, _ -> onCommit() }
|
||||||
|
|
||||||
|
red.valueProperty.bindBidirectional(layer.redProperty)
|
||||||
|
green.valueProperty.bindBidirectional(layer.greenProperty)
|
||||||
|
blue.valueProperty.bindBidirectional(layer.blueProperty)
|
||||||
|
alpha.valueProperty.bindBidirectional(layer.alphaProperty)
|
||||||
|
|
||||||
|
parameters.addAll(red, green, blue, alpha)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun unbind(layer: ImageLayer, parameters: ObservableList<Parameter<*>>) {
|
||||||
|
(parameters[0] as IntegerParameter).valueProperty.unbindBidirectional(layer.redProperty)
|
||||||
|
(parameters[1] as IntegerParameter).valueProperty.unbindBidirectional(layer.greenProperty)
|
||||||
|
(parameters[2] as IntegerParameter).valueProperty.unbindBidirectional(layer.blueProperty)
|
||||||
|
(parameters[3] as IntegerParameter).valueProperty.unbindBidirectional(layer.alphaProperty)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.map.parameter.layer
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.model.Parameter
|
||||||
|
import com.bartlomiejpluta.base.editor.map.model.layer.Layer
|
||||||
|
import javafx.collections.ObservableList
|
||||||
|
|
||||||
|
interface LayerParametersBinder<T : Layer> {
|
||||||
|
fun bind(layer: T, parameters: ObservableList<Parameter<*>>, onCommit: () -> Unit)
|
||||||
|
fun unbind(layer: T, parameters: ObservableList<Parameter<*>>)
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ class MapFragment : Fragment() {
|
|||||||
private val tileSetView = find<TileSetView>()
|
private val tileSetView = find<TileSetView>()
|
||||||
private val toolbarView = find<MapToolbarView>()
|
private val toolbarView = find<MapToolbarView>()
|
||||||
private val statusBarView = find<MapStatusBarView>()
|
private val statusBarView = find<MapStatusBarView>()
|
||||||
|
private val layerParameters = find<MapLayerParameters>()
|
||||||
|
|
||||||
private val isTileLayerSelected = Bindings.createBooleanBinding(
|
private val isTileLayerSelected = Bindings.createBooleanBinding(
|
||||||
{ editorStateVM.selectedLayer is TileLayer },
|
{ editorStateVM.selectedLayer is TileLayer },
|
||||||
@@ -41,6 +42,10 @@ class MapFragment : Fragment() {
|
|||||||
maxHeightProperty().bind(this@item.heightProperty())
|
maxHeightProperty().bind(this@item.heightProperty())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
item("Layer Parameters", expanded = false) {
|
||||||
|
this += layerParameters
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bottom = statusBarView.root
|
bottom = statusBarView.root
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.map.view.editor
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.model.Parameter
|
||||||
|
import com.bartlomiejpluta.base.editor.common.parameter.view.ParametersTableFragment
|
||||||
|
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
|
||||||
|
import com.bartlomiejpluta.base.editor.map.model.layer.ImageLayer
|
||||||
|
import com.bartlomiejpluta.base.editor.map.parameter.layer.ImageLayerParametersBinder
|
||||||
|
import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM
|
||||||
|
import tornadofx.View
|
||||||
|
import tornadofx.observableListOf
|
||||||
|
|
||||||
|
class MapLayerParameters : View() {
|
||||||
|
private val editorStateVM = find<EditorStateVM>()
|
||||||
|
|
||||||
|
// For some reason Spring does not want to autowire a list of beans
|
||||||
|
// of LayerParametersBinder<> type
|
||||||
|
private val imageLayerParametersBinder: ImageLayerParametersBinder by di()
|
||||||
|
|
||||||
|
private val parameters = observableListOf<Parameter<*>>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
editorStateVM.selectedLayerProperty.addListener { _, previousLayer, layer ->
|
||||||
|
when (previousLayer) {
|
||||||
|
is ImageLayer -> imageLayerParametersBinder.unbind(previousLayer, parameters)
|
||||||
|
}
|
||||||
|
|
||||||
|
parameters.clear()
|
||||||
|
|
||||||
|
when (layer) {
|
||||||
|
is ImageLayer -> imageLayerParametersBinder.bind(layer, parameters) { fire(RedrawMapRequestEvent) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override val root = find<ParametersTableFragment>(ParametersTableFragment::parameters to parameters).root
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user