korge: add basic interaction

16
Timo Bryant 2024-01-05 18:01:48 +01:00
parent 8dbe0c59fa
commit 24fca8c62c
4 changed files with 57 additions and 44 deletions

View File

@ -164,28 +164,6 @@ fun <T> ZoomedImage(
fun loadImageBitmap(file: File): ImageBitmap = fun loadImageBitmap(file: File): ImageBitmap =
file.inputStream().buffered().use(::loadImageBitmap) file.inputStream().buffered().use(::loadImageBitmap)
data class PointConverter(
val docWidth: Int,
val docHeight: Int,
val canvasWidth: Float,
val canvasHeight: Float
) {
fun convertX(x: Int): Float {
val xf = x.toFloat()
val relXf = docWidth / xf
val scaledXf = canvasWidth * relXf
// println("X: $scaledXf")
return scaledXf
}
fun convertY(y: Int): Float {
val yf = y.toFloat()
val relYf = docHeight / yf
val scaledYf = canvasHeight * relYf
// println("Y: $scaledYf")
return scaledYf
}
}
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun shapes(zoomableState: ZoomableState) { fun shapes(zoomableState: ZoomableState) {
@ -215,13 +193,6 @@ fun canvas(zoomableState: ZoomableState, first: OcrPage) {
// } // }
) )
{ {
val converter = PointConverter(
docWidth = 2481,
docHeight = 3507,
canvasWidth = this.size.width,
canvasHeight = this.size.height
)
first.words.forEach { word -> first.words.forEach { word ->
val rect = word.rectangle val rect = word.rectangle
drawRect( drawRect(

View File

@ -0,0 +1,2 @@
dependencies:
- https://github.com/korlibs/korge-box2d/tree/v0.1.2/korge-box2d##b1737ab3985c0bd3e2f002346ff2ac43ca1ebf48

View File

@ -1,37 +1,76 @@
import korlibs.time.* import korlibs.event.Key
import korlibs.image.bitmap.context2d
import korlibs.korge.* import korlibs.korge.*
import korlibs.korge.scene.* import korlibs.korge.scene.*
import korlibs.korge.tween.*
import korlibs.korge.view.* import korlibs.korge.view.*
import korlibs.image.color.* import korlibs.image.color.*
import korlibs.image.font.readBitmapFont
import korlibs.image.format.* import korlibs.image.format.*
import korlibs.io.file.std.* import korlibs.io.file.std.*
import korlibs.korge.input.*
import korlibs.korge.tween.get
import korlibs.korge.tween.tween
import korlibs.korge.ui.uiButton
import korlibs.korge.ui.uiCheckBox
import korlibs.math.geom.* import korlibs.math.geom.*
import korlibs.math.interpolation.* import korlibs.math.interpolation.Easing
import korlibs.render.platform.DummyOpenglContext.scaleFactor
import kotlin.time.Duration.Companion.seconds
suspend fun main() { suspend fun main() {
Korge(windowSize = Size(512, 512), backgroundColor = Colors["#2b2b2b"]) { Korge(windowSize = Size(512, 512), backgroundColor = Colors["#2b2b2b"]) {
val sceneContainer = sceneContainer() val sceneContainer = sceneContainer()
sceneContainer.changeTo({ MyScene() }) sceneContainer.changeTo { ViewDocument() }
} }
} }
class MyScene : Scene() { class ViewDocument : Scene() {
override suspend fun SContainer.sceneMain() { override suspend fun SContainer.sceneMain() {
val minDegrees = (-18).degrees var scaleFactor = 0.5
val maxDegrees = (+16).degrees var offset = Point(0.0,0.0)
val moveFactor = 50.0
scale(scaleFactor)
val image = image(resourcesVfs["kitten_pixelated.png"].readBitmap()) { suspend fun zoom(amount: Double) {
rotation = maxDegrees scaleFactor += amount
anchor(.5, .5) tween(
scale(0.8) this@sceneMain::scale[scaleFactor],
position(256, 256) time = 0.1.seconds,
easing = Easing.EASE_IN_OUT
)
} }
while (true) { suspend fun moveX(amount: Double) {
image.tween(image::rotation[minDegrees], time = 1.seconds, easing = Easing.EASE_IN_OUT) offset = offset.copy(x = offset.x + amount)
image.tween(image::rotation[maxDegrees], time = 1.seconds, easing = Easing.EASE_IN_OUT) tween(
this@sceneMain::x[offset.x],
time = 0.1.seconds,
easing = Easing.EASE_IN_OUT)
}
suspend fun moveY(amount: Double) {
offset = offset.copy(y = offset.y + amount)
tween(
this@sceneMain::y[offset.y],
time = 0.1.seconds,
easing = Easing.EASE_IN_OUT)
}
keys {
down { key ->
when (key.key) {
Key.EQUAL -> zoom(0.05)
Key.MINUS -> zoom(-0.05)
Key.UP -> moveY(moveFactor)
Key.DOWN -> moveY(-moveFactor)
Key.RIGHT -> moveX(moveFactor)
Key.LEFT -> moveX(-moveFactor)
else -> {}
}
}
val imageFile = localCurrentDirVfs["assets/xs-reg/00001.jpg"].readBitmap()
image(imageFile)
} }
} }
} }

File diff suppressed because one or more lines are too long