package com.pixelized.desktop.lwa.ui.composable.scene.cursor

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.PointerEvent
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import com.pixelized.desktop.lwa.ui.composable.scene.camera.Camera
import com.pixelized.desktop.lwa.ui.composable.scene.utils.global

@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.onCursorControl(
    camera: Camera,
    cursor: Cursor,
): Modifier = this
    .onPointerEvent(PointerEventType.Exit) { event: PointerEvent ->
        cursor.change(
            position = Offset.Unspecified,
        )
    }
    .onPointerEvent(PointerEventType.Move) { event: PointerEvent ->
        cursor.change(
            position = event.changes.first().position.global(camera = camera),
        )
    }