Skip to content

Commit

Permalink
hook to rei for """better""" auto panel positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Nov 29, 2020
1 parent ed0a2ef commit 00f0ebb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
46 changes: 34 additions & 12 deletions src/main/java/badasintended/cpas/client/ClientUtils.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package badasintended.cpas.client;

import java.util.function.BiFunction;
import java.util.function.Consumer;

import badasintended.cpas.api.SlotType;
import badasintended.cpas.client.api.CpasTarget;
import badasintended.cpas.client.toast.HelpToast;
import badasintended.cpas.client.widget.AbstractPanelWidget;
import badasintended.cpas.client.widget.ArmorPanelWidget;
import badasintended.cpas.client.widget.ArmorSlotWidget;
import badasintended.cpas.client.widget.EditorScreenWidget;
import badasintended.cpas.client.widget.TrinketSlotWidget;
import badasintended.cpas.config.CpasConfig;
import badasintended.cpas.mixin.AccessorHandledScreen;
import io.netty.buffer.Unpooled;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.BaseBoundsHandler;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
Expand Down Expand Up @@ -151,23 +155,41 @@ public static void injectCpasWidget(Screen screen) {
target.cpas$setEditorScreen(new EditorScreenWidget(scaledW, scaledH, entry, handledScreen));

if (entry.isEnabled()) {
int panelX;
int panelY;

if (entry.isAuto()) {
panelX = accessor.getX() - 35;
panelY = accessor.getY() + accessor.getBackgroundHeight() - ((5 * 18) + 18);
} else {
panelX = scaledW / 2 + entry.getX();
panelY = scaledH / 2 + entry.getY();
}

target.cpas$setArmorPanel(new ArmorPanelWidget(panelX, panelY, 32, 5 * 18 + 18, accessor.getPlayerInventory()));
target.cpas$setArmorPanel(panel(handledScreen, entry, (x, y) -> new ArmorPanelWidget(x, y, accessor.getPlayerInventory())));
} else {
target.cpas$setArmorPanel(null);
}
}
}
}

public static <T extends AbstractPanelWidget> T panel(HandledScreen<?> screen, CpasConfig.Entry entry, BiFunction<Integer, Integer, T> func) {
AccessorHandledScreen accessor = (AccessorHandledScreen) screen;

int scaledW = client().getWindow().getScaledWidth(),
scaledH = client().getWindow().getScaledHeight();
int panelX, panelY;

if (entry.isAuto()) {
panelX = accessor.getX() - 35;
panelY = accessor.getY() + accessor.getBackgroundHeight() - ((5 * 18) + 18);

if (hasMod("roughlyenoughitems")) {
for (Rectangle zone : BaseBoundsHandler.getInstance().getExclusionZones(screen.getClass())) {
if (
((zone.getMinX() < panelX && panelX < zone.getMaxX()) || (panelX < zone.getMinX() && zone.getMinX() < panelX + 32))
&& ((zone.getMinY() < panelY && panelY < zone.getMaxY()) || (panelY < zone.getMinY() && zone.getMinY() < panelY + 108))
) {
panelX = zone.getMinX() - 35;
}
}
}
} else {
panelX = scaledW / 2 + entry.getX();
panelY = scaledH / 2 + entry.getY();
}

return func.apply(panelX, panelY);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@Environment(EnvType.CLIENT)
public class ArmorPanelWidget extends AbstractPanelWidget {

public ArmorPanelWidget(int x, int y, int width, int height, PlayerInventory playerInventory) {
super(x, y, width, height);
public ArmorPanelWidget(int x, int y, PlayerInventory playerInventory) {
super(x, y, 32, 5 * 18 + 18);

for (int i = 0; i < 4; i++) {
children().add(createArmorSlot(x + 7, i * 18 + y + 7, playerInventory, SlotType.of(i)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import badasintended.cpas.Cpas;
import badasintended.cpas.client.CpasClient;
import badasintended.cpas.config.CpasConfig;
import badasintended.cpas.mixin.AccessorHandledScreen;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
Expand All @@ -13,6 +12,7 @@
import static badasintended.cpas.client.ClientUtils.drawText;
import static badasintended.cpas.client.ClientUtils.getScreenName;
import static badasintended.cpas.client.ClientUtils.injectCpasWidget;
import static badasintended.cpas.client.ClientUtils.panel;
import static badasintended.cpas.client.ClientUtils.textRenderer;

@Environment(EnvType.CLIENT)
Expand All @@ -21,7 +21,6 @@ public class EditorScreenWidget extends AbstractParentWidget {
private final int scaledW, scaledH;
private final CpasConfig.Entry entry;
private final HandledScreen<?> screen;
private final AccessorHandledScreen accessor;
private final String screenName;

private boolean enabled;
Expand All @@ -36,7 +35,6 @@ public EditorScreenWidget(int scaledW, int scaledH, CpasConfig.Entry entry, Hand
this.scaledH = scaledH;
this.entry = entry;
this.screen = screen;
this.accessor = (AccessorHandledScreen) screen;
this.screenName = getScreenName(screen);
}

Expand All @@ -49,20 +47,10 @@ public void toggle() {
int minX = 20,
minY = 20;

int panelX, panelY;

enabled = entry.isEnabled();
auto = entry.isAuto();

if (entry.isAuto()) {
panelX = accessor.getX() - 35;
panelY = accessor.getY() + accessor.getBackgroundHeight() - ((5 * 18) + 18);
} else {
panelX = scaledW / 2 + entry.getX();
panelY = scaledH / 2 + entry.getY();
}

panel = new MovablePanelWidget(panelX, panelY, 32, 5 * 18 + 18);
panel = panel(screen, entry, MovablePanelWidget::new);

ToggleButtonWidget autoButton = new ToggleButtonWidget(minX, minY + 50, b -> {
auto = b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class MovablePanelWidget extends AbstractPanelWidget {
distanceX = 0,
distanceY = 0;

public MovablePanelWidget(int x, int y, int width, int height) {
super(x, y, width, height);
public MovablePanelWidget(int x, int y) {
super(x, y, 32, 5 * 18 + 18);
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/badasintended/cpas/mixin/MixinKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import badasintended.cpas.client.widget.EditorScreenWidget;
import net.minecraft.client.Keyboard;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.ParentElement;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -14,7 +13,6 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(Keyboard.class)
public abstract class MixinKeyboard {
Expand All @@ -27,15 +25,13 @@ public abstract class MixinKeyboard {
method = "onKey",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/screen/Screen;wrapScreenError(Ljava/lang/Runnable;Ljava/lang/String;Ljava/lang/String;)V",
shift = At.Shift.AFTER
target = "Lnet/minecraft/client/gui/screen/Screen;wrapScreenError(Ljava/lang/Runnable;Ljava/lang/String;Ljava/lang/String;)V"
),
locals = LocalCapture.CAPTURE_FAILHARD,
cancellable = true
)
private void onKey(long window, int key, int scancode, int i, int j, CallbackInfo ci, ParentElement parentElement, boolean[] bls) {
private void onKey(long window, int key, int scancode, int i, int j, CallbackInfo ci) {
Screen screen = client.currentScreen;
if (screen instanceof HandledScreen<?> && i != 0 && bls[0]) {
if (screen instanceof HandledScreen<?> && i != 0) {
EditorScreenWidget editor = ((CpasTarget) screen).cpas$getEditorScreen();
if (editor != null && CpasClient.EDIT.matchesKey(key, scancode) && (editor.visible || Screen.hasControlDown())) {
editor.toggle();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/cpas.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"minVersion" : "0.8",
"package" : "badasintended.cpas.mixin",
"compatibilityLevel": "JAVA_8",
"priority" : 999,
"mixins" : [],
"client" : [
"AccessorHandledScreen",
Expand Down

0 comments on commit 00f0ebb

Please sign in to comment.