Skip to content

Commit

Permalink
Refined effect preview code
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Sep 7, 2024
1 parent 13782b9 commit 2fbf049
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/src/main/kotlin/tripaint/app/MainStage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class MainStage(

override fun askForBlurRadius(): Int? {
val selectedImagesCoords = imageGrid.selectedImages().map { it.coords }
return DialogUtils.getValueFromDialog<Int>(
return DialogUtils.getValueFromDialog(
imagePool,
imageGrid.selectedImages(),
"Blur images",
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/kotlin/tripaint/util/channels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class Resource<T> internal constructor(init: T, source: Receiver<T>) {
}

init {
source.onEvent {
newValue ->
source.onEvent { newValue ->
val oldValue = this._value
this._value = newValue
dispatcher.notify(Pair(oldValue, newValue))
Expand All @@ -59,7 +58,7 @@ class Resource<T> internal constructor(init: T, source: Receiver<T>) {
return Pair(tx, rx)
}

fun <T> createResource(init: T): Pair<Resource<T>, (T) -> Unit> {
fun <T> createResource(init: T): Pair<Resource<T>, (value: T) -> Unit> {
val (tx, rx) = createChannel<T>()
return Pair(Resource(init, rx)) { v -> tx.send(v) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ object AskForFileOpenSettingsDialog {

val previewPaneBorder = run {
val stroke = BorderStroke(
Color.Red.toFXColor(),
BorderStrokeStyle.SOLID,
CornerRadii.EMPTY,
BorderWidths.DEFAULT
Color.Red.toFXColor(),
BorderStrokeStyle.SOLID,
CornerRadii.EMPTY,
BorderWidths.DEFAULT
)
Border(stroke)
}
Expand Down
4 changes: 2 additions & 2 deletions ui/src/main/kotlin/tripaint/view/gui/DialogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object DialogUtils {
dialog.graphic = graphic

val contentBox = VBox(*content.toTypedArray())
contentBox.setSpacing(10.0)
contentBox.spacing = 10.0

dialog.dialogPane.content = contentBox
dialog.setResultConverter { b -> resultConverter(b) }
Expand Down Expand Up @@ -123,7 +123,7 @@ object DialogUtils {
fun makeImagePreviewList(
images: List<GridCell>,
imagePool: ImagePool
): Pair<ScrollPane, ((ImageGrid) -> Unit) -> Unit> {
): Pair<ScrollPane, (effect: (ImageGrid) -> Unit) -> Unit> {
return ImagePreviewList.fromImageContent(images, TriImageForPreview.previewSize) { imagePool.locationOf(it) }
}
}
50 changes: 30 additions & 20 deletions ui/src/main/kotlin/tripaint/view/gui/ImagePreviewList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,33 @@ import tripaint.grid.ImageGrid
import tripaint.image.ImagePool
import tripaint.image.ImageStorage
import tripaint.image.format.SimpleStorageFormat
import tripaint.util.Resource
import tripaint.view.JavaFxExt.toFXColor
import tripaint.view.image.TriImageForPreview
import kotlin.math.sqrt

object ImagePreviewList {
fun fromImageContent(
images: List<GridCell>,
previewSize: Int,
locationOfImage: (ImageStorage) -> ImagePool.SaveLocation?
): Pair<ScrollPane, ((ImageGrid) -> Unit) -> Unit> {
val imageSize = if (images.isNotEmpty()) images.first().storage.imageSize else 8

fun makeContent(effect: (ImageGrid) -> Unit): List<ImageView> {
val previewImages = images.map { cloneImageContent(it) }

val previewImageGrid = ImageGrid(imageSize)
for (im in previewImages) {
previewImageGrid.set(im)
}

effect(previewImageGrid)

return previewImages.map { im -> ImagePreview.fromImageContent(im, previewSize, locationOfImage) }
}
): Pair<ScrollPane, (effect: (ImageGrid) -> Unit) -> Unit> {
val (currentEffect, setEffect) = Resource.createResource<(ImageGrid) -> Unit> { _ -> }

val p = ScrollPane()
p.maxWidth = previewSize * 5.0
p.content = HBox(*makeContent { _ -> }.toTypedArray<ImageView>())
p.minViewportHeight = previewSize * Math.sqrt(3.0) / 2
p.minViewportHeight = previewSize * sqrt(3.0) / 2

val updatePreview: ((ImageGrid) -> Unit) -> Unit = { effect ->
p.content = HBox(*makeContent(effect).toTypedArray())
currentEffect.onChange { (_, effect) ->
p.content = images
.withEffect(effect)
.map { ImagePreview.fromImageContent(it, previewSize, locationOfImage) }
.let { HBox(*it.toTypedArray()) }
}

return Pair(p, updatePreview)
setEffect { _ -> } // call handler once to set the initial content

return Pair(p, setEffect)
}

private fun cloneImageContent(content: GridCell): GridCell {
Expand All @@ -55,6 +48,23 @@ object ImagePreviewList {
val storage = ImageStorage.fromRegularImage(image, StorageCoords.from(0, 0), format, imageSize).getOrThrow()
return GridCell(content.coords, storage)
}

private fun List<GridCell>.withEffect(effect: (ImageGrid) -> Unit): List<GridCell> {
if (this.isEmpty()) return listOf()

val imageSize = this.first().storage.imageSize

val previewImages = this.map { cloneImageContent(it) }

val previewImageGrid = ImageGrid(imageSize)
for (im in previewImages) {
previewImageGrid.set(im)
}

effect(previewImageGrid)

return previewImages
}
}


Expand Down
3 changes: 2 additions & 1 deletion ui/src/main/kotlin/tripaint/view/image/ImageGridPane.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class ImageGridPane(private val imageGrid: ImageGrid, private val currentEditMod
private var xScroll: Double = 0.0
private var yScroll: Double = 0.0
private var zoom: Double = 1.0
private val drag = Drag()

private object drag {
private class Drag {
var x: Double = -1.0
var y: Double = -1.0
}
Expand Down

0 comments on commit 2fbf049

Please sign in to comment.