From 6c288da80704e7db5ac1989142f1c0f84269084e Mon Sep 17 00:00:00 2001 From: bushtail Date: Fri, 5 May 2023 18:39:37 -0400 Subject: [PATCH] InputQOLCtrlZoom --- .../kotlin/BasicInputQOLCtrlZoom/plugin.kt | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 plugin-playground/src/main/kotlin/BasicInputQOLCtrlZoom/plugin.kt diff --git a/plugin-playground/src/main/kotlin/BasicInputQOLCtrlZoom/plugin.kt b/plugin-playground/src/main/kotlin/BasicInputQOLCtrlZoom/plugin.kt new file mode 100644 index 0000000..07cda7d --- /dev/null +++ b/plugin-playground/src/main/kotlin/BasicInputQOLCtrlZoom/plugin.kt @@ -0,0 +1,114 @@ +package BasicInputQOLCtrlZoom + +import plugin.Plugin +import plugin.annotations.PluginMeta +import plugin.api.* +import rt4.Keyboard +import java.awt.event.* +import javax.swing.SwingUtilities + +@PluginMeta( + author = "Ceikry", + description = "Provides some basic input QOL like ctrl scroll zoom, middle click panning, etc. Incompatible with BasicInputQOL.", + version = 1.0 +) +class plugin : Plugin() { + private var cameraDebugEnabled = false + private var mouseDebugEnabled = false + + companion object { + private var lastMouseWheelX = 0 + private var lastMouseWheelY = 0 + private val defaultCameraPYZ = Triple(128.0, 0.0, 600) + } + + override fun Init() { + API.AddMouseListener(MouseCallbacks) + API.AddMouseWheelListener(MouseWheelCallbacks) + } + + override fun ProcessCommand(commandStr: String?, args: Array?) { + commandStr ?: return + if (API.PlayerHasPrivilege(Privileges.JMOD)) { + when(commandStr) { + "::mousedebug" -> mouseDebugEnabled = !mouseDebugEnabled + "::cameradebug" -> cameraDebugEnabled = !cameraDebugEnabled + } + } + } + + override fun Draw(timeDelta: Long) { + if (mouseDebugEnabled) { + API.DrawText( + FontType.SMALL, + FontColor.YELLOW, + TextModifier.LEFT, + "Mouse Coords: (${API.GetMouseX()}, ${API.GetMouseY()})", + 10, + 40 + ) + } + if (cameraDebugEnabled) { + API.DrawText( + FontType.SMALL, + FontColor.YELLOW, + TextModifier.LEFT, + "Camera: [P=${API.GetCameraPitch()}, Y=${API.GetCameraYaw()}, Z=${API.GetCameraZoom()}]", + 10, + 50 + ) + } + } + + object MouseWheelCallbacks : MouseWheelListener { + override fun mouseWheelMoved(e: MouseWheelEvent?) { + e ?: return + if (API.IsKeyPressed(Keyboard.KEY_CTRL)) { + val previous = API.GetPreviousMouseWheelRotation() + val current = API.GetMouseWheelRotation() + val diff = current - previous + API.UpdateCameraZoom(diff) + } + } + } + + object MouseCallbacks : MouseAdapter() { + override fun mouseDragged(e: MouseEvent?) { + e ?: return + if (SwingUtilities.isMiddleMouseButton(e)) { + val x = e.x + val y = e.y + val accelX = lastMouseWheelX - x + val accelY = lastMouseWheelY - y + lastMouseWheelX = x + lastMouseWheelY = y + API.UpdateCameraYaw(accelX * 2.0) + API.UpdateCameraPitch(-accelY * 2.0) + } + } + + override fun mouseClicked(e: MouseEvent?) { + e ?: return + + /* val width = API.GetWindowDimensions().width; + val compassBordersX = intArrayOf(width - 165, width - 125) + val compassBordersY = intArrayOf(0, 45) + if ( + e.x in compassBordersX[0]..compassBordersX[1] + && e.y in compassBordersY[0]..compassBordersY[1] + ) + { + API.SetCameraPitch(defaultCameraPYZ.first) + API.SetCameraYaw(defaultCameraPYZ.second) + }*/ + } + + override fun mousePressed(e: MouseEvent?) { + e ?: return + if (SwingUtilities.isMiddleMouseButton(e)) { + lastMouseWheelX = e.x + lastMouseWheelY = e.y + } + } + } +} \ No newline at end of file