Skip to content

Commit ae7bd1f

Browse files
committed
fixed mouse utils
1 parent 1cc25eb commit ae7bd1f

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

common/src/main/kotlin/com/lambda/newgui/LambdaScreen.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ class LambdaScreen(
9898
}
9999

100100
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
101-
layout.onEvent(GuiEvent.MouseClick(Mouse.Button(button), Mouse.Action.Click, rescaleMouse(mouseX, mouseY)))
101+
layout.onEvent(GuiEvent.MouseClick(Mouse.Button.fromMouseCode(button), Mouse.Action.Click, rescaleMouse(mouseX, mouseY)))
102102
return true
103103
}
104104

105105
override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean {
106-
layout.onEvent(GuiEvent.MouseClick(Mouse.Button(button), Mouse.Action.Release, rescaleMouse(mouseX, mouseY)))
106+
layout.onEvent(GuiEvent.MouseClick(Mouse.Button.fromMouseCode(button), Mouse.Action.Release, rescaleMouse(mouseX, mouseY)))
107107
return true
108108
}
109109

common/src/main/kotlin/com/lambda/util/Mouse.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
package com.lambda.util
1919

20+
import com.lambda.Lambda.mc
21+
import com.mojang.blaze3d.systems.RenderSystem
2022
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
2130
import kotlin.jvm.Throws
2231

2332
class Mouse {
@@ -64,4 +73,41 @@ class Mouse {
6473
nameMap[name.lowercase()] ?: throw IllegalArgumentException("Action name '$name' not found in nameMap.")
6574
}
6675
}
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+
}
67113
}

0 commit comments

Comments
 (0)