package ru.dbotthepony.mc.otm.client.screen.panels.util

import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel

open class DraggableCanvasPanel<out S : Screen> @JvmOverloads constructor(
	screen: S,
	parent: EditablePanel<*>?,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = 10f,
	height: Float = 10f
) : EditablePanel<S>(screen, parent, x, y, width, height) {
	protected var dragging = false

	override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean {
		dragging = true

		if (tryToGrabMouseInput()) {
			grabMouseInput = true
		}

		return true
	}

	override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean {
		dragging = false
		grabMouseInput = false
		return true
	}

	override fun mouseDraggedInner(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean {
		if (dragging) {
			xOffset += xDelta.toFloat()
			yOffset += yDelta.toFloat()
		}

		return true
	}

	init {
		scissor = true
	}
}
