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

QoL Improvements #24

Open
wants to merge 1 commit into
base: multi/1.21
Choose a base branch
from
Open
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
Expand Up @@ -19,7 +19,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public class ArmorGlowScreen extends Screen {
public class ArmorGlowScreen extends MoveableScreen {
private static final int PADDING = 6;

private ArmorGlowWidget armorListWidget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public class ArmorPosesScreen extends Screen {
public class ArmorPosesScreen extends MoveableScreen {
private enum SortType {
NORMAL,
A_TO_Z,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.phys.Vec3;

public class ArmorStandScreen extends Screen {
public class ArmorStandScreen extends MoveableScreen {
private static final WidgetSprites MIRROR_POSE_SPRITES = new WidgetSprites(
ResourceLocation.fromNamespaceAndPath(Reference.MOD_ID, "widget/mirror_pose"), ResourceLocation.fromNamespaceAndPath(Reference.MOD_ID, "widget/mirror_pose_highlighted")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;

public class DeletePoseScreen extends Screen {
public class DeletePoseScreen extends MoveableScreen {
private final ArmorStandScreen parentScreen;
private Button deleteButton;
private final PoseListWidget.ListEntry entry;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.mrbysco.armorposer.client.gui;

import com.mojang.blaze3d.platform.InputConstants;
import com.mrbysco.armorposer.mixin.KeyMappingAccessor;
import com.mrbysco.armorposer.mixin.MouseHandleAccessor;
import com.mrbysco.armorposer.platform.Services;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.lwjgl.glfw.GLFW;

public abstract class MoveableScreen extends Screen {
private static final KeyMapping DEFER_CONTROL_KEY = Services.PLATFORM.registerKeyMapping(new KeyMapping("armorposer.keybind.deferControl", GLFW.GLFW_KEY_LEFT_ALT, "armorposer.keybinds"));

private double mouseX;
private double mouseY;
private boolean isPressDown = false;

private boolean wasDeferred = false;
protected MoveableScreen(Component title) {
super(title);
}

@Override
public void afterKeyboardAction() {
super.afterKeyboardAction();
this.isPressDown = true;
}

@Override
public void removed() {
this.tickRelease();
super.removed();
}

@Override
public boolean keyPressed(int key, int scanCode, int modifiers) {
boolean isPressDown = this.isPressDown;
this.isPressDown = false;

//If a child is selected, just escape the text box when pressing escape
if (key == GLFW.GLFW_KEY_ESCAPE) {
for (var child : this.children()) {
if (child.isFocused()) {
child.setFocused(false);
return true;
}
}
}

boolean consumed = super.keyPressed(key, scanCode, modifiers);
if (consumed)
return true;

this.updateKeybind();
if (DEFER_CONTROL_KEY.isDown()) {
//Else consume it ourselves
if (!this.wasDeferred) {
var mouseHandle = this.minecraft.mouseHandler;
this.wasDeferred = true;
this.mouseX = mouseHandle.xpos();
this.mouseY = mouseHandle.ypos();
mouseHandle.setIgnoreFirstMove();
((MouseHandleAccessor)mouseHandle).setMouseGrabbed(true);
InputConstants.grabOrReleaseMouse(this.minecraft.getWindow().getWindow(), 212995, ((double) this.minecraft.getWindow().getScreenWidth() / 2), ((double) this.minecraft.getWindow().getScreenHeight() / 2));
}

var keyEntry = InputConstants.getKey(key, scanCode);
if (isPressDown) {
KeyMapping.set(keyEntry, true);
KeyMapping.click(keyEntry);
} else {
KeyMapping.set(keyEntry, false);
}
return true;
}
return false;
}

private void tickRelease() {
if (this.wasDeferred) {
this.wasDeferred = false;
this.minecraft.mouseHandler.setIgnoreFirstMove();
((MouseHandleAccessor)this.minecraft.mouseHandler).setMouseGrabbed(false);
InputConstants.grabOrReleaseMouse(this.minecraft.getWindow().getWindow(), 212993, this.mouseX, this.mouseY);
InputConstants.grabOrReleaseMouse(this.minecraft.getWindow().getWindow(), 212993, this.mouseX, this.mouseY);
}
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
//Deselect focused entry if clicked and nothing hit
if (super.mouseClicked(mouseX, mouseY, button)) {
return true;
}
for (var child : this.children()) {
if (child.isFocused()) {
child.setFocused(false);
return true;
}
}
return false;
}

@Override
public boolean keyReleased(int key, int scanCode, int modifiers) {
this.updateKeybind();
if (!DEFER_CONTROL_KEY.isDown()) {
this.tickRelease();
}
return super.keyReleased(key, scanCode, modifiers);
}

private void updateKeybind() {
var key = ((KeyMappingAccessor)DEFER_CONTROL_KEY).getKey();
if (key.getType() == InputConstants.Type.KEYSYM && key.getValue() != InputConstants.UNKNOWN.getValue()) {
DEFER_CONTROL_KEY.setDown(InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), key.getValue()));
}
}

public static void earlyInit() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;

public class SavePoseScreen extends Screen {
public class SavePoseScreen extends MoveableScreen {
private final ArmorStandScreen parentScreen;
private Button saveButton;
private EditBox nameField;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mrbysco.armorposer.mixin;

import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(KeyMapping.class)
public interface KeyMappingAccessor {
@Accessor
InputConstants.Key getKey();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mrbysco.armorposer.mixin;

import net.minecraft.client.MouseHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(MouseHandler.class)
public interface MouseHandleAccessor {
@Accessor
void setMouseGrabbed(boolean value);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mrbysco.armorposer.platform.services;

import com.mrbysco.armorposer.data.SwapData;
import net.minecraft.client.KeyMapping;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.decoration.ArmorStand;

Expand Down Expand Up @@ -46,4 +47,10 @@ public interface IPlatformHelper {
* @return The mod version
*/
String getModVersion();

/**
* Register a keyboard binding
* @return bound KeyMapping
*/
KeyMapping registerKeyMapping(KeyMapping mapping);
}
4 changes: 3 additions & 1 deletion common/src/main/resources/assets/armorposer/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@
"text.autoconfig.armorposer.option.general.resizeWhitelist.@Tooltip": "List of players that are allowed to resize the Armor Stand when restrictResizeToOP is enabled",
"text.autoconfig.armorposer.option.general.restrictResizeToOP": "Restrict Resize To OP",
"text.autoconfig.armorposer.option.general.restrictResizeToOP.@Tooltip": "Restrict the ability to resize the Armor Stand to server operators",
"text.autoconfig.armorposer.title": "Armor Poser"
"text.autoconfig.armorposer.title": "Armor Poser",
"armorposer.keybind.deferControl": "Defer Control",
"armorposer.keybinds": "Armor Poser"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mrbysco.armorposer;

import com.mrbysco.armorposer.client.gui.MoveableScreen;
import com.mrbysco.armorposer.packets.ArmorStandScreenPayload;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
Expand All @@ -8,9 +9,13 @@
import net.minecraft.world.entity.decoration.ArmorStand;

public class ArmorPoserClient implements ClientModInitializer {
static {
MoveableScreen.earlyInit();
}

@Override
public void onInitializeClient() {

ClientPlayNetworking.registerGlobalReceiver(ArmorStandScreenPayload.ID, (payload, context) -> {
int entityID = payload.entityID();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.mrbysco.armorposer.packets.ArmorStandSyncPayload;
import com.mrbysco.armorposer.platform.services.IPlatformHelper;
import me.shedaniel.autoconfig.AutoConfig;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.KeyMapping;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.decoration.ArmorStand;

Expand Down Expand Up @@ -60,4 +62,9 @@ public List<? extends String> getResizeWhitelist() {
public String getModVersion() {
return FabricLoader.getInstance().getModContainer(Reference.MOD_ID).orElseThrow().getMetadata().getVersion().getFriendlyString();
}

@Override
public KeyMapping registerKeyMapping(KeyMapping mapping) {
return KeyBindingHelper.registerKeyBinding(mapping);
}
}
4 changes: 3 additions & 1 deletion fabric/src/main/resources/armorposer.fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"ArmorStandMixin"
],
"client": [
"MinecraftMixin"
"MinecraftMixin",
"MouseHandleAccessor",
"KeyMappingAccessor"
],
"injectors": {
"defaultRequire": 1
Expand Down
15 changes: 15 additions & 0 deletions forge/src/main/java/com/mrbysco/armorposer/ArmorPoser.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.mrbysco.armorposer;

import com.mrbysco.armorposer.client.gui.MoveableScreen;
import com.mrbysco.armorposer.config.PoserConfig;
import com.mrbysco.armorposer.packets.ArmorStandScreenPayload;
import com.mrbysco.armorposer.packets.ArmorStandSwapPayload;
import com.mrbysco.armorposer.packets.ArmorStandSyncPayload;
import com.mrbysco.armorposer.packets.handler.ClientPayloadHandler;
import com.mrbysco.armorposer.packets.handler.ServerPayloadHandler;
import net.minecraft.client.KeyMapping;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig.Type;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.client.gui.ConfigurationScreen;
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.registration.PayloadRegistrar;

import java.util.ArrayList;
import java.util.List;

@Mod(Reference.MOD_ID)
public class ArmorPoser {
public static final List<KeyMapping> KEY_MAPPINGS = new ArrayList<>();

public ArmorPoser(IEventBus eventBus, ModContainer container, Dist dist) {
container.registerConfig(Type.COMMON, PoserConfig.commonSpec);
Expand All @@ -27,6 +34,9 @@ public ArmorPoser(IEventBus eventBus, ModContainer container, Dist dist) {

if (dist.isClient()) {
container.registerExtensionPoint(IConfigScreenFactory.class, ConfigurationScreen::new);

MoveableScreen.earlyInit();
eventBus.addListener(this::setupKeyMappings);
}
}

Expand All @@ -36,4 +46,9 @@ private void setupPackets(final RegisterPayloadHandlersEvent event) {
registrar.playToServer(ArmorStandSwapPayload.ID, ArmorStandSwapPayload.CODEC, ServerPayloadHandler.getInstance()::handleSwapData);
registrar.playToServer(ArmorStandSyncPayload.ID, ArmorStandSyncPayload.CODEC, ServerPayloadHandler.getInstance()::handleSyncData);
}

private void setupKeyMappings(RegisterKeyMappingsEvent event) {
KEY_MAPPINGS.forEach(event::register);
KEY_MAPPINGS.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mrbysco.armorposer.platform;

import com.mrbysco.armorposer.ArmorPoser;
import com.mrbysco.armorposer.Reference;
import com.mrbysco.armorposer.config.PoserConfig;
import com.mrbysco.armorposer.data.SwapData;
import com.mrbysco.armorposer.data.SyncData;
import com.mrbysco.armorposer.packets.ArmorStandSwapPayload;
import com.mrbysco.armorposer.packets.ArmorStandSyncPayload;
import com.mrbysco.armorposer.platform.services.IPlatformHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.neoforged.fml.ModList;
Expand Down Expand Up @@ -55,4 +57,10 @@ public List<? extends String> getResizeWhitelist() {
public String getModVersion() {
return ModList.get().getModFileById(Reference.MOD_ID).versionString();
}

@Override
public KeyMapping registerKeyMapping(KeyMapping mapping) {
ArmorPoser.KEY_MAPPINGS.add(mapping);
return mapping;
}
}
4 changes: 3 additions & 1 deletion forge/src/main/resources/armorposer.neoforge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"mixins": [
],
"client": [
"MinecraftMixin"
"MinecraftMixin",
"MouseHandleAccessor",
"KeyMappingAccessor"
],
"injectors": {
"defaultRequire": 1
Expand Down