Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Dispatch to InputEvents from patchwork-god-classes #130

Merged
merged 4 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,26 @@
/*
* Minecraft Forge, Patchwork Project
* Copyright (c) 2016-2020, 2019-2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.patchworkmc.api.input;

public interface ForgeMouse {
boolean isMiddleDown();
double getXVelocity();
double getYVelocity();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Minecraft Forge, Patchwork Project
* Copyright (c) 2016-2020, 2019-2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.patchworkmc.impl.event.input;

import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Event;

import net.minecraft.client.Mouse;

import net.patchworkmc.api.input.ForgeMouse;

public class InputEvents {
public static void fireMouseInput(int button, int action, int mods) {
MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
}

public static void fireKeyInput(int key, int scanCode, int action, int modifiers) {
MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scanCode, action, modifiers));
}

public static boolean onMouseScroll(Mouse mouseHelper, double scrollDelta) {
final Event event = new InputEvent.MouseScrollEvent(scrollDelta, mouseHelper.wasLeftButtonClicked(), ((ForgeMouse) mouseHelper).isMiddleDown(), mouseHelper.wasRightButtonClicked(), mouseHelper.getX(), mouseHelper.getY());

return MinecraftForge.EVENT_BUS.post(event);
}

public static boolean onRawMouseClicked(int button, int action, int mods) {
return MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package net.patchworkmc.mixin.event.input;

import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.common.MinecraftForge;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -30,15 +28,17 @@
import net.minecraft.client.Keyboard;
import net.minecraft.client.MinecraftClient;

import net.patchworkmc.impl.event.input.InputEvents;

@Mixin(Keyboard.class)
public abstract class MixinKeyboard {
@Shadow
MinecraftClient client;

@Inject(method = "onKey", at = @At("RETURN"))
private void fireKeyInput(long window, int key, int scancode, int i, int j, CallbackInfo info) {
private void fireKeyInput(long window, int key, int scancode, int action, int modifiers, CallbackInfo info) {
if (window == this.client.window.getHandle()) {
MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scancode, i, j));
InputEvents.fireKeyInput(key, scancode, action, modifiers);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

package net.patchworkmc.mixin.event.input;

import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Event;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -32,37 +29,52 @@

import net.minecraft.client.Mouse;

@Mixin(Mouse.class)
public abstract class MixinMouse {
@Shadow
boolean middleButtonClicked;
@Shadow
abstract boolean wasLeftButtonClicked();
@Shadow
abstract boolean wasRightButtonClicked();
@Shadow
abstract double getX();
@Shadow
abstract double getY();
import net.patchworkmc.api.input.ForgeMouse;
import net.patchworkmc.impl.event.input.InputEvents;

@Mixin(Mouse.class)
public abstract class MixinMouse implements ForgeMouse {
kitlith marked this conversation as resolved.
Show resolved Hide resolved
@Inject(method = "onMouseButton", at = @At("RETURN"), cancellable = true)
private void fireMouseInput(long window, int button, int action, int mods, CallbackInfo info) {
MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
InputEvents.fireMouseInput(button, action, mods);
}

@Inject(method = "onMouseButton", at = @At(value = "FIELD", ordinal = 3, target = "Lnet/minecraft/client/Mouse;client:Lnet/minecraft/client/MinecraftClient;", shift = Shift.BEFORE), cancellable = true)
private void onRawMouseClicked(long window, int button, int action, int mods, CallbackInfo info) {
if (MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods))) {
if (InputEvents.onRawMouseClicked(button, action, mods)) {
info.cancel();
}
}

@Inject(method = "onMouseScroll", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isSpectator()Z", shift = Shift.BEFORE), cancellable = true)
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double f, float i) {
final Event event = new InputEvent.MouseScrollEvent(f, wasLeftButtonClicked(), middleButtonClicked, wasRightButtonClicked(), getX(), getY());

if (MinecraftForge.EVENT_BUS.post(event)) {
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double scrollDelta, float i) {
if (InputEvents.onMouseScroll((Mouse) (Object) this, scrollDelta)) {
info.cancel();
}
}

// Methods added by forge
@Shadow
boolean middleButtonClicked;

@Shadow
double cursorDeltaX;

@Shadow
double cursorDeltaY;

@Override
public boolean isMiddleDown() {
return middleButtonClicked;
}

@Override
public double getXVelocity() {
return cursorDeltaX;
}

@Override
public double getYVelocity() {
return cursorDeltaY;
}
}
1 change: 1 addition & 0 deletions patchwork-god-classes/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
compile project(path: ':patchwork-fml', configuration: 'dev')
compile project(path: ':patchwork-capabilities', configuration: 'dev')
compile project(path: ':patchwork-events-entity', configuration: 'dev')
compile project(path: ':patchwork-events-input', configuration: 'dev')
compile project(path: ':patchwork-events-lifecycle', configuration: 'dev')
compile project(path: ':patchwork-events-rendering', configuration: 'dev')
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,32 @@
import net.minecraft.client.color.item.ItemColors;
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.util.Identifier;
import net.minecraft.client.Mouse;

import net.patchworkmc.impl.event.input.InputEvents;
import net.patchworkmc.impl.event.render.RenderEvents;

/*
* Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules.
* Do not keep implementation details here, methods should be thin wrappers around methods in other modules.
*/
public class ForgeHooksClient {
public static void fireMouseInput(int button, int action, int mods) {
InputEvents.fireMouseInput(button, action, mods);
}

public static void fireKeyInput(int key, int scanCode, int action, int modifiers) {
InputEvents.fireKeyInput(key, scanCode, action, modifiers);
}

public static boolean onMouseScroll(Mouse mouseHelper, double scrollDelta) {
return InputEvents.onMouseScroll(mouseHelper, scrollDelta);
}

public static boolean onRawMouseClicked(int button, int action, int mods) {
return InputEvents.onRawMouseClicked(button, action, mods);
}

public static void onBlockColorsInit(BlockColors blockColors) {
RenderEvents.onBlockColorsInit(blockColors);
}
Expand Down
1 change: 1 addition & 0 deletions patchwork-god-classes/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"fabricloader": ">=0.8.4",
"patchwork-fml": "*",
"patchwork-capabilities": "*",
"patchwork-events-input": "*",
"patchwork-events-lifecycle": "*",
"patchwork-events-rendering": "*"
},
Expand Down