File drops
OPENRNDR programs can listen to file drop events. File drop events are generated by a user dragging and releasing one or more file icons onto the program window.
import org.openrndr.application
import org.openrndr.draw.loadImage
import org.openrndr.drawImage
import org.openrndr.extra.imageFit.FitMethod
import org.openrndr.extra.imageFit.imageFit
import java.io.File
fun main() = application {
program {
window.drop.listen {
println("${it.files.size} files dropped at ${it.position}")
}
}
}
The following program displays PNG, JPG or TIF images dropped onto its window. If several files are dropped at once, it displays the first of them that is an image.
import org.openrndr.application
import org.openrndr.draw.loadImage
import org.openrndr.drawImage
import org.openrndr.extra.imageFit.FitMethod
import org.openrndr.extra.imageFit.imageFit
import java.io.File
fun main() = application {
program {
// Create a dummy image
var img = drawImage(16, 16) { }
window.drop.listen { dropped ->
val firstImage = dropped.files.firstOrNull {
File(it).extension.lowercase() in listOf("png", "jpg", "tif")
}
if(firstImage != null) {
// Call .destroy() to avoid leaking memory
img.destroy()
// Then load a new image into img
img = loadImage(firstImage)
}
}
extend {
drawer.imageFit(img, drawer.bounds, fitMethod = FitMethod.Contain)
}
}
}
By using drawer.imageFit the image is centered and made to fit the available window size.