This repository was archived by the owner on Sep 17, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMouse.java
More file actions
137 lines (109 loc) · 3.4 KB
/
Copy pathMouse.java
File metadata and controls
137 lines (109 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package Gprocessing.input;
import Gprocessing.graphics.Window;
import Gprocessing.physics.Vector2;
import imgui.ImGui;
import imgui.ImGuiIO;
import org.joml.Vector2f;
import org.lwjgl.BufferUtils;
import java.nio.DoubleBuffer;
import static org.lwjgl.glfw.GLFW.*;
public class Mouse {
public static Vector2 mouse;
public static long mouseX = 0;
public static long mouseY = 0;
public static Vector2 pmouse;
public static long pmouseX = 0;
public static long pmouseY = 0;
public static Vector2 mouseScroll;
public static double scrollX = 0;
public static double scrollY = 0;
public static boolean mouseButton[] = new boolean[3];
public static boolean mouseDragged;
private static int _button;
private static int _action;
static ImGuiIO io = Gprocessing.ImGui.ImGuiLayer.io;
public static void pollMouseButtons() {
glfwSetMouseButtonCallback(Window.window, (w, button, action, mods) -> {
// ImGui Input
final boolean[] mouseDown = new boolean[5];
mouseDown[0] = button == GLFW_MOUSE_BUTTON_1 && action != GLFW_RELEASE;
mouseDown[1] = button == GLFW_MOUSE_BUTTON_2 && action != GLFW_RELEASE;
mouseDown[2] = button == GLFW_MOUSE_BUTTON_3 && action != GLFW_RELEASE;
mouseDown[3] = button == GLFW_MOUSE_BUTTON_4 && action != GLFW_RELEASE;
mouseDown[4] = button == GLFW_MOUSE_BUTTON_5 && action != GLFW_RELEASE;
io.setMouseDown(mouseDown);
if (!io.getWantCaptureMouse() && mouseDown[1]) {
ImGui.setWindowFocus(null);
}
// Gprocessing input
_button = button;
_action = action;
});
if (_action == GLFW_PRESS) {
if (_button < mouseButton.length)
mouseButton[_button] = true;
} else if (_action == GLFW_RELEASE) {
if (_button < mouseButton.length) {
mouseButton[_button] = false;
mouseDragged = false;
}
}
}
public static void pollMouseScroll() {
glfwSetScrollCallback(Window.window, (w, xOffset, yOffset) -> {
io.setMouseWheelH(io.getMouseWheelH() + (float) xOffset);
io.setMouseWheel(io.getMouseWheel() + (float) yOffset);
scrollX = xOffset;
scrollY = yOffset;
mouseScroll = new Vector2(scrollX, scrollY);
});
}
public static void update() {
pollMouseButtons();
pollMouseScroll();
DoubleBuffer x = BufferUtils.createDoubleBuffer(1);
DoubleBuffer y = BufferUtils.createDoubleBuffer(1);
glfwGetCursorPos(Window.window, x, y);
x.rewind();
y.rewind();
long pmouseX = mouseX;
long pmouseY = mouseY;
pmouse = new Vector2(pmouseX, pmouseY);
mouseX = (long) x.get();
mouseY = (long) y.get();
mouse = new Vector2(mouseX, mouseY);
if (mouseX != pmouseX || mouseY != pmouseY) {
mouseDragged = mouseButton[0] || mouseButton[1] || mouseButton[2];
}
}
public static boolean mouseButtonDown (int button) {
if (button < mouseButton.length) {
return mouseButton[button];
}
return false;
}
static boolean mouseDown = false;
public static boolean mouseButtonClicked (int button) {
if (!mouseDown) {
if (button < mouseButton.length) {
mouseDown = true;
return mouseButton[button];
}
if (mouseButton[button] == false) {
mouseDown = false;
}
}
return false;
}
public static void clearMouseInput () {
scrollX = 0;
scrollY = 0;
mouseScroll = new Vector2(scrollX, scrollY);
pmouseX = mouseX;
pmouseY = mouseY;
pmouse = new Vector2(pmouseX, pmouseY);
}
public static void printMouseLocation() {
Gprocessing.util.Engine.println("mouseX: " + mouseX + "\tmouseY: " + mouseY);
};
}