All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.korlibs.render.TouchEventHandler.kt Maven / Gradle / Ivy

There is a newer version: 4.0.10
Show newest version
package korlibs.render

import korlibs.datastructure.*
import korlibs.datastructure.lock.*
import korlibs.event.*

class TouchEventHandler {
    @PublishedApi
    internal val lock = NonRecursiveLock()
    @PublishedApi
    internal val touchesEventPool = Pool { TouchEvent() }
    @PublishedApi
    internal var lastTouchEvent: TouchEvent = TouchEvent()

    inline fun handleEvent(gameWindow: GameWindow, kind: TouchEvent.Type, emitter: (TouchEvent) -> Unit) {
        val currentTouchEvent = lock {
            val currentTouchEvent = touchesEventPool.alloc()
            currentTouchEvent.copyFrom(lastTouchEvent)
            currentTouchEvent.startFrame(kind)
            emitter(currentTouchEvent)
            currentTouchEvent.endFrame()

            lastTouchEvent.copyFrom(currentTouchEvent)
            currentTouchEvent
        }

        gameWindow.queue {
            try {
                gameWindow.dispatch(currentTouchEvent)
            } finally {
                lock { touchesEventPool.free(currentTouchEvent) }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy