Skip to content

Commit

Permalink
fix casing
Browse files Browse the repository at this point in the history
  • Loading branch information
hf02 committed Jul 24, 2023
1 parent 55e23a8 commit 1c974e1
Show file tree
Hide file tree
Showing 15 changed files with 910 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/java/com/github/hf02/scrollForWorldEdit/client/Key.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.hf02.scrollForWorldEdit.client;

import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;

public class Key {

public final String code;
public final String name;
public final String command;
public final KeyBinding keybinding;
public final KeyManager keyManager;
private final ScrollForWorldEditClient scrollClient;
public final KeyHandler handler;

public Key(
KeyManager keyManager,
String code,
String command,
String name,
String keybindingName,
net.minecraft.client.util.InputUtil.Type type,
int defaultCode,
String category,
KeyHandler handler
) {
this.keyManager = keyManager;
this.code = code;
this.command = command;
this.name = name;
this.scrollClient = keyManager.scrollClient;
this.handler = handler;
this.keybinding =
KeyBindingHelper.registerKeyBinding(
new KeyBinding(keybindingName, type, defaultCode, category)
);
}

public void run(TakeScroll scroll) {
boolean isActive = keyManager.isKeyActive(this.code);
if (isActive) {
scrollClient.mouseHandler.capturingScroll = true;
this.handler.callback(this, scroll);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.hf02.scrollForWorldEdit.client;

public interface KeyHandler {
void callback(Key key, TakeScroll scroll);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
package com.github.hf02.scrollForWorldEdit.client;

import com.github.hf02.scrollForWorldEdit.ScrollForWorldEdit;
import java.util.Optional;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;

public class KeyManager {

private Key[] keys;

public final int count;

public final KeyBinding modeKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding(
"key.scroll-for-worldedit.mode",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_LEFT_CONTROL,
"key.scroll-for-worldedit.main"
)
);
private boolean modeKeyToggle = false;

public final KeyBinding useKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding(
"key.scroll-for-worldedit.use",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_LEFT_ALT,
"key.scroll-for-worldedit.main"
)
);
private boolean useKeyToggle = false;

public final KeyBinding modifierKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding(
"key.scroll-for-worldedit.modifier",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_Z,
"key.scroll-for-worldedit.main"
)
);

private Optional<Key> getKey(String code) {
for (Key key : keys) {
if (key.code == code) {
return Optional.of(key);
}
}
return Optional.empty();
}

public boolean isUseKeyActive() {
return ScrollForWorldEditClient.config.useKeyToggles
? useKeyToggle
: useKey.isPressed();
}

public boolean isModeKeyActive() {
boolean modeKeyAllowed = ScrollForWorldEditClient.config.mustHoldUseKeyForModeKey
? isUseKeyActive()
: true;
boolean modeKeyPressed = ScrollForWorldEditClient.config.modeKeyToggles
? modeKeyToggle
: modeKey.isPressed();
return modeKeyAllowed && modeKeyPressed;
}

private int activeKeyIndex = 0;
private Key activeKey;

public void setActiveKey(int index) {
activeKeyIndex = index % count;
if (activeKeyIndex < 0) {
activeKeyIndex = count + activeKeyIndex;
}
activeKey = keys[activeKeyIndex];
}

public Key getActiveKey() {
return activeKey;
}

public int getActiveKeyIndex() {
return activeKeyIndex;
}

public final ScrollForWorldEditClient scrollClient;

public KeyManager(ScrollForWorldEditClient scrollForWorldEditClient) {
this.scrollClient = scrollForWorldEditClient;

ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (useKey.wasPressed()) {
useKeyToggle =
ScrollForWorldEditClient.config.useKeyToggles
? !useKeyToggle
: false;
}
while (modeKey.wasPressed()) {
modeKeyToggle =
ScrollForWorldEditClient.config.modeKeyToggles
? !modeKeyToggle
: false;
}
});

keys =
new Key[] {
new Key(
this,
"move",
"/move %s %s -s",
"scroll-for-worldedit.mode.move",
"key.scroll-for-worldedit.move",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_UNKNOWN,
"key.scroll-for-worldedit.main",
this::runNonNegativeMove
),
new Key(
this,
"expand",
"/expand %s %s",
"scroll-for-worldedit.mode.expand",
"key.scroll-for-worldedit.expand",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_UNKNOWN,
"key.scroll-for-worldedit.main",
this::runMove
),
new Key(
this,
"contract",
"/contract %s %s",
"scroll-for-worldedit.mode.contract",
"key.scroll-for-worldedit.contract",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_UNKNOWN,
"key.scroll-for-worldedit.main",
this::runMove
),
new Key(
this,
"shift",
"/shift %s %s",
"scroll-for-worldedit.mode.shift",
"key.scroll-for-worldedit.shift",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_UNKNOWN,
"key.scroll-for-worldedit.main",
this::runMove
),
new Key(
this,
"stack",
"/stack %s %s -s",
"scroll-for-worldedit.mode.stack",
"key.scroll-for-worldedit.stack",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_UNKNOWN,
"key.scroll-for-worldedit.main",
this::runNonNegativeMove
),
};
count = keys.length;
setActiveKey(0);
}

public boolean isKeyActive(String keyName) {
Optional<Key> key = getKey(keyName);
if (!key.isPresent()) {
ScrollForWorldEdit.LOGGER.warn(
String.format("isKeyActive(): Unknown key %s", keyName)
);
return false;
}
if (isUseKeyActive() && activeKey == key.get()) {
return true;
}
return key.get().keybinding.isPressed();
}

public void processKeys(TakeScroll scroll) {
for (Key key : keys) {
key.run(scroll);
}
}

private void sendCommand(String command) {
scrollClient.client.player.networkHandler.sendChatCommand(command);
}

// methods to handle Keys

private void runNonNegativeMove(Key key, TakeScroll scroll) {
if (scroll.scrollY > 0) {
sendCommand(
String.format(
key.command,
scroll.scrollY,
scrollClient.primaryTextDirection()
)
);
} else if (scroll.scrollY < 0) {
sendCommand(
String.format(
key.command,
-scroll.scrollY,
scrollClient.primaryTextDirection(true)
)
);
}

if (scroll.scrollX > 0) {
sendCommand(
String.format(
key.command,
scroll.scrollX,
scrollClient.secondaryTextDirection()
)
);
} else if (scroll.scrollX < 0) {
sendCommand(
String.format(
key.command,
-scroll.scrollX,
scrollClient.secondaryTextDirection(true)
)
);
}
}

private void runMove(Key key, TakeScroll scroll) {
if (scroll.scrollY != 0) {
sendCommand(
String.format(
key.command,
scroll.scrollY,
scrollClient.primaryTextDirection()
)
);
}

if (scroll.scrollX != 0) {
sendCommand(
String.format(
key.command,
scroll.scrollX,
scrollClient.secondaryTextDirection()
)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.hf02.scrollForWorldEdit.client;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.util.ActionResult;

/**
* callback for the mouse scrolling.
* returning SUCCESS will cancel minecraft's handling of the mouse
* wheel.
*/
public interface MouseScrollCallback {
Event<MouseScrollCallback> EVENT = EventFactory.createArrayBacked(
MouseScrollCallback.class,
listeners ->
(horizontal, vertical) -> {
for (MouseScrollCallback listener : listeners) {
ActionResult result = listener.interact(
horizontal,
vertical
);

if (result != ActionResult.PASS) {
return result;
}
}
return ActionResult.PASS;
}
);

ActionResult interact(double horizontal, double vertical);
}
Loading

0 comments on commit 1c974e1

Please sign in to comment.