Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VirtualInput] Fixed scroll wheel not updating correctly #198

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void getDifference(VirtualMouse nextMouse, Queue<VirtualMouseEvent> refer
* ...but scrollWheel, cursorX or cursorY are different.
* Without this, the scrollWheel would only work if a mouse button is pressed at the same time.
*/
if(!equals(nextMouse)) {
if(!equals(nextMouse) || scrollWheel != 0) {
reference.add(new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, nextMouse.scrollWheel, nextMouse.cursorX, nextMouse.cursorY));
}
return;
Expand Down
142 changes: 75 additions & 67 deletions src/main/java/com/minecrafttas/tasmod/virtual/VirtualPeripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,98 +12,104 @@
* Base class for {@link VirtualKeyboard} and {@link VirtualMouse}<br>
* <br>
* Contains the shared code for keeping track of which buttons are pressed.<br>
* This works by storing the keycodes of the buttons in a set, as keycodes are supposed to be unique<br>
* This works by storing the keycodes of the buttons in a set, as keycodes are
* supposed to be unique<br>
* <br>
* Generating {@link VirtualEvent}s is handled in the child classes.
*
* @author Scribble
*/
public abstract class VirtualPeripheral<T extends VirtualPeripheral<T>> extends Subtickable<T> implements Serializable {

/**
* The list of keycodes that are currently pressed on this peripheral.
*/
protected final Set<Integer> pressedKeys;

/**
* Creates a VirtualPeripheral
* @param pressedKeys The {@link #pressedKeys}
* @param subtickList The {@link #subtickList}
* @param ignoreFirstUpdate The {@link #ignoreFirstUpdate} state
*/
protected VirtualPeripheral(Set<Integer> pressedKeys, List<T> subtickList, boolean ignoreFirstUpdate) {
super(subtickList, ignoreFirstUpdate);
this.pressedKeys = pressedKeys;
}
/**
* The list of keycodes that are currently pressed on this peripheral.
*/
protected final Set<Integer> pressedKeys;

/**
* Set the specified keycode to pressed
* @param keycode The keycode to check
* @param keystate The keystate of the keycode
*/
protected void setPressed(int keycode, boolean keystate) {
if (VirtualKeybindings.isKeyCodeAlwaysBlocked(keycode)) { //TODO Maybe a better system?
/**
* Creates a VirtualPeripheral
*
* @param pressedKeys The {@link #pressedKeys}
* @param subtickList The {@link #subtickList}
* @param ignoreFirstUpdate The {@link #ignoreFirstUpdate} state
*/
protected VirtualPeripheral(Set<Integer> pressedKeys, List<T> subtickList, boolean ignoreFirstUpdate) {
super(subtickList, ignoreFirstUpdate);
this.pressedKeys = pressedKeys;
}

/**
* Set the specified keycode to pressed
*
* @param keycode The keycode to check
* @param keystate The keystate of the keycode
*/
protected void setPressed(int keycode, boolean keystate) {
if (VirtualKeybindings.isKeyCodeAlwaysBlocked(keycode)) { // TODO Maybe a better system?
return;
}
if (keystate)
pressedKeys.add(keycode);
else
pressedKeys.remove(keycode);
}

/**
* Set the specified keyname to pressed
* @param keyname The keyname to check
* @param keystate The keystate of the keyname
*/
public void setPressed(String keyname, boolean keystate) {
Integer keycode = VirtualKey.getKeycode(keyname);
if (keycode != null) {
setPressed(keycode, keystate);
}
}
if (keystate)
pressedKeys.add(keycode);
else
pressedKeys.remove(keycode);
}

/**
* @return A list of all currently pressed keynames
*/
public List<String> getCurrentPresses() {
List<String> out = new ArrayList<>();
pressedKeys.forEach(keycode -> {
out.add(VirtualKey.getName(keycode));
});
return out;
}
/**
* Set the specified keyname to pressed
*
* @param keyname The keyname to check
* @param keystate The keystate of the keyname
*/
public void setPressed(String keyname, boolean keystate) {
Integer keycode = VirtualKey.getKeycode(keyname);
if (keycode != null) {
setPressed(keycode, keystate);
}
}

@Override
public String toString() {
return String.join(",", getCurrentPresses());
}
/**
* @return A list of all currently pressed keynames
*/
public List<String> getCurrentPresses() {
List<String> out = new ArrayList<>();
pressedKeys.forEach(keycode -> {
out.add(VirtualKey.getName(keycode));
});
return out;
}

/**
* @return An immutable set of pressed keycodes
*/
@Override
public String toString() {
return String.join(",", getCurrentPresses());
}

/**
* @return An immutable set of pressed keycodes
*/
public Set<Integer> getPressedKeys() {
return ImmutableSet.copyOf(pressedKeys);
}

/**
* If the key is available in {@link #pressedKeys}
*
* @param keycode The keycode in question
* @return If the key is pressed
*/
public boolean isKeyDown(int keycode) {
return pressedKeys.contains(keycode);
}

/**
* If the key is available in {@link #pressedKeys}
*
* @param keyname The keyname in question
* @return If the key is pressed
*/
public boolean isKeyDown(String keyname) {
return pressedKeys.contains(VirtualKey.getKeycode(keyname));
}

/**
* Clears pressed keys and subticks
*/
Expand All @@ -112,25 +118,27 @@ protected void clear() {
pressedKeys.clear();
super.clear();
}

@Override
public boolean equals(Object obj) {
if(obj instanceof VirtualPeripheral) {
if (obj instanceof VirtualPeripheral) {
VirtualPeripheral<?> peripheral = (VirtualPeripheral<?>) obj;
for (Integer keycode : pressedKeys) {
if(!peripheral.pressedKeys.contains(keycode)) {
if (!peripheral.pressedKeys.contains(keycode)) {
return false;
}
}
return true;
}
return super.equals(obj);
}

/**
* Copies the data from another virtual peripheral into this peripheral without creating a new object.
* @param peripheral The peripheral to move from
*/

/**
* Copies the data from another virtual peripheral into this peripheral without
* creating a new object.
*
* @param peripheral The peripheral to move from
*/
protected void copyFrom(T peripheral) {
this.pressedKeys.clear();
this.pressedKeys.addAll(peripheral.pressedKeys);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/tasmod.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

//Playbackhooks
"playbackhooks.MixinMinecraft",
"playbackhooks.MixinEntityRenderer",
"playbackhooks.MixinEntityRenderer",
"playbackhooks.MixinGuiScreen",
"playbackhooks.MixinGameSettings",
"playbackhooks.MixinGuiChat",
Expand Down
31 changes: 16 additions & 15 deletions src/test/java/tasmod/virtual/VirtualMouseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,38 +291,39 @@ void testGetVirtualEventsUnpress() {
}

/**
* Tests 2 updates having the same value. Should return no additional button events
* Tests 2 updates having the same value and the scroll wheel is 0. Should return no additional button events
*/
@Test
void testSameUpdate() {
VirtualMouse unpressed = new VirtualMouse();

VirtualMouse pressed = new VirtualMouse();
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 0, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 0, 10, 12);

// Load actual with the events
Queue<VirtualMouseEvent> actual = new ConcurrentLinkedQueue<>();
unpressed.getVirtualEvents(pressed, actual);

// Load expected
List<VirtualMouseEvent> expected = Arrays.asList(
new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12) // Should only have one keyboard event
new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 0, 10, 12) // Should only have one keyboard event
);

assertIterableEquals(expected, actual);
}


/**
* Tests 2 updates having the same pressed keys, but scrollWheel is different
* Tests 2 updates having the same pressed keys, but scrollWheel != 0
*/
@Test
void testScrollWheelDifferent() {
VirtualMouse unpressed = new VirtualMouse();

VirtualMouse pressed = new VirtualMouse();
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, -30, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12);

// Load actual with the events
Queue<VirtualMouseEvent> actual = new ConcurrentLinkedQueue<>();
Expand All @@ -331,7 +332,7 @@ void testScrollWheelDifferent() {
// Load expected
List<VirtualMouseEvent> expected = Arrays.asList(
new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12),
new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, -30, 10, 12) // Adds an additional "MOUSEMOVED" event with the scroll wheel
new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, 15, 10, 12) // Adds an additional "MOUSEMOVED" event with the scroll wheel
);

assertIterableEquals(expected, actual);
Expand All @@ -345,17 +346,17 @@ void testCursorXDifferent() {
VirtualMouse unpressed = new VirtualMouse();

VirtualMouse pressed = new VirtualMouse();
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 11, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 0, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 0, 11, 12);

// Load actual with the events
Queue<VirtualMouseEvent> actual = new ConcurrentLinkedQueue<>();
unpressed.getVirtualEvents(pressed, actual);

// Load expected
List<VirtualMouseEvent> expected = Arrays.asList(
new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12),
new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, 15, 11, 12) // Adds an additional "MOUSEMOVED" event with the cursorX
new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 0, 10, 12),
new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, 0, 11, 12) // Adds an additional "MOUSEMOVED" event with the cursorX
);

assertIterableEquals(expected, actual);
Expand All @@ -369,17 +370,17 @@ void testCursorYDifferent() {
VirtualMouse unpressed = new VirtualMouse();

VirtualMouse pressed = new VirtualMouse();
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 15, 10, 120);
pressed.update(VirtualKey.LC.getKeycode(), true, 0, 10, 12);
pressed.update(VirtualKey.LC.getKeycode(), true, 0, 10, 120);

// Load actual with the events
Queue<VirtualMouseEvent> actual = new ConcurrentLinkedQueue<>();
unpressed.getVirtualEvents(pressed, actual);

// Load expected
List<VirtualMouseEvent> expected = Arrays.asList(
new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 15, 10, 12),
new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, 15, 10, 120) // Adds an additional "MOUSEMOVED" event with the cursorY
new VirtualMouseEvent(VirtualKey.LC.getKeycode(), true, 0, 10, 12),
new VirtualMouseEvent(VirtualKey.MOUSEMOVED.getKeycode(), false, 0, 10, 120) // Adds an additional "MOUSEMOVED" event with the cursorY
);

assertIterableEquals(expected, actual);
Expand Down
Loading