|
17 | 17 |
|
18 | 18 | package com.lambda.util |
19 | 19 |
|
| 20 | +import com.lambda.Lambda.mc |
| 21 | +import com.mojang.blaze3d.systems.RenderSystem |
20 | 22 | import org.lwjgl.glfw.GLFW |
| 23 | +import org.lwjgl.glfw.GLFW.GLFW_ARROW_CURSOR |
| 24 | +import org.lwjgl.glfw.GLFW.GLFW_POINTING_HAND_CURSOR |
| 25 | +import org.lwjgl.glfw.GLFW.GLFW_RESIZE_EW_CURSOR |
| 26 | +import org.lwjgl.glfw.GLFW.GLFW_RESIZE_NS_CURSOR |
| 27 | +import org.lwjgl.glfw.GLFW.GLFW_RESIZE_NWSE_CURSOR |
| 28 | +import org.lwjgl.glfw.GLFW.glfwCreateStandardCursor |
| 29 | +import org.lwjgl.glfw.GLFW.glfwSetCursor |
21 | 30 | import kotlin.jvm.Throws |
22 | 31 |
|
23 | 32 | class Mouse { |
@@ -64,4 +73,41 @@ class Mouse { |
64 | 73 | nameMap[name.lowercase()] ?: throw IllegalArgumentException("Action name '$name' not found in nameMap.") |
65 | 74 | } |
66 | 75 | } |
| 76 | + |
| 77 | + enum class Cursor(private val getCursorPointer: () -> Long) { |
| 78 | + Arrow(::arrow), |
| 79 | + Pointer(::pointer), |
| 80 | + ResizeH(::resizeH), ResizeV(::resizeV), ResizeHV(::resizeHV); |
| 81 | + |
| 82 | + fun set() { |
| 83 | + if (lastCursor == this) return |
| 84 | + lastCursor = this |
| 85 | + |
| 86 | + RenderSystem.assertOnRenderThread() |
| 87 | + glfwSetCursor(mc.window.handle, getCursorPointer()) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + class CursorController { |
| 92 | + private var lastSetCursor: Cursor? = null |
| 93 | + |
| 94 | + fun setCursor(cursor: Cursor) { |
| 95 | + // We're doing this to let other controllers be able to set the cursor when this one doesn't change |
| 96 | + if (lastSetCursor == cursor && cursor == Cursor.Arrow) return |
| 97 | + |
| 98 | + cursor.set() |
| 99 | + lastSetCursor = cursor |
| 100 | + } |
| 101 | + |
| 102 | + fun reset() = setCursor(Cursor.Arrow) |
| 103 | + } |
| 104 | + |
| 105 | + companion object { |
| 106 | + private val arrow by lazy { glfwCreateStandardCursor(GLFW_ARROW_CURSOR) } |
| 107 | + private val pointer by lazy { glfwCreateStandardCursor(GLFW_POINTING_HAND_CURSOR) } |
| 108 | + private val resizeH by lazy { glfwCreateStandardCursor(GLFW_RESIZE_EW_CURSOR) } |
| 109 | + private val resizeV by lazy { glfwCreateStandardCursor(GLFW_RESIZE_NS_CURSOR) } |
| 110 | + private val resizeHV by lazy { glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR) } |
| 111 | + var lastCursor = Cursor.Arrow |
| 112 | + } |
67 | 113 | } |
0 commit comments