Skip to content

Commit

Permalink
feat: Re-add IO Config show/hide neighbour button
Browse files Browse the repository at this point in the history
  • Loading branch information
dphaldes authored Sep 28, 2024
1 parent b7dd5a7 commit 78f86ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import javax.annotation.Nullable;
import java.util.function.Consumer;

// TODO: Redesign this
public class IOConfigButton extends EnderButton {
public static final ResourceLocation IO_CONFIG = EnderIOBase.loc("buttons/io_config");
public static final ResourceLocation NEIGHBOURS = EnderIOBase.loc("buttons/neighbour");
private final IOConfigOverlay configRenderer;
@Nullable private final Consumer<Boolean> callback;

Expand All @@ -28,13 +26,6 @@ public IOConfigButton(int x, int y, IOConfigOverlay configRenderer, @Nullable Co
this.configRenderer = configRenderer;
this.callback = callback;
setTooltip(Tooltip.create(EIOLang.IOCONFIG.copy().withStyle(ChatFormatting.WHITE)));

// TODO: This should be a child of the config widget.
/*neighbourButton = new ImageButton(screen.getGuiLeft() + screen.getXSize() - 5 - 16, screen.getGuiTop() + screen.getYSize() - 5 - 16, 16, 16,
new WidgetSprites(NEIGHBOURS, NEIGHBOURS), (b) -> configRenderer.toggleNeighbourVisibility(), EIOLang.TOGGLE_NEIGHBOUR);
neighbourButton.setTooltip(Tooltip.create(EIOLang.TOGGLE_NEIGHBOUR.copy().withStyle(ChatFormatting.WHITE)));
neighbourButton.visible = show;
addRenderableWidget.apply(neighbourButton);*/
}

@Override
Expand All @@ -54,6 +45,4 @@ public void onPress() {
@Override
public void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {}

public record Inset(int left, int right, int top, int bottom) {}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.enderio.machines.client.gui.widget.ioconfig;

import com.enderio.EnderIOBase;
import com.enderio.base.common.lang.EIOLang;
import com.enderio.core.client.gui.screen.BaseOverlay;
import com.enderio.machines.client.rendering.model.ModelRenderUtil;
import com.enderio.machines.common.blockentity.base.MachineBlockEntity;
Expand All @@ -16,6 +17,7 @@
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Axis;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.narration.NarrationElementOutput;
Expand Down Expand Up @@ -89,6 +91,10 @@ public class IOConfigOverlay extends BaseOverlay {
private boolean neighbourVisible = true;
private Optional<SelectedFace> selection = Optional.empty();

//Neighbour Button
public static final ResourceLocation NEIGHBOURS_BTN = EnderIOBase.loc("buttons/neighbour");
private final Rect2i neighBtnRect;

public IOConfigOverlay(int x, int y, int width, int height, List<BlockPos> _configurable) {
super(x, y, width, height, Component.empty());
this.configurable.addAll(_configurable);
Expand Down Expand Up @@ -128,6 +134,7 @@ public IOConfigOverlay(int x, int y, int width, int height, List<BlockPos> _conf
yaw = MINECRAFT.player.getYRot();

initBuffers(MINECRAFT.renderBuffers().bufferSource());
neighBtnRect = new Rect2i(getX() + getWidth() - 2 - 16, getY() + getHeight() - 2 -16, 16, 16);
}

@Override
Expand Down Expand Up @@ -200,13 +207,20 @@ public void toggleNeighbourVisibility() {
@Override
public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) {
if (this.active && this.visible) {
if (pButton == 0) {
if(neighBtnRect.contains((int) pMouseX, (int) pMouseY)) {
toggleNeighbourVisibility();
this.playDownSound(MINECRAFT.getSoundManager());
return true;
}
}
if (pButton == 1) {
if (selection.isPresent()) {
var selectedFace = selection.get();
BlockEntity entity = MINECRAFT.level.getBlockEntity(selectedFace.blockPos);
if (entity instanceof MachineBlockEntity machine) {
machine.cycleIOMode(selectedFace.side);
this.playDownSound(Minecraft.getInstance().getSoundManager());
this.playDownSound(MINECRAFT.getSoundManager());
return true;
}
}
Expand All @@ -217,10 +231,9 @@ public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) {

@Override
public boolean mouseDragged(double pMouseX, double pMouseY, int pButton, double pDragX, double pDragY) {
if (visible && isValidClickButton(pButton) && isMouseOver(pMouseX, pMouseY)) {
Minecraft minecraft = Minecraft.getInstance();
double dx = pDragX / (double) minecraft.getWindow().getGuiScaledWidth();
double dy = pDragY / (double) minecraft.getWindow().getGuiScaledHeight();
if (visible && isValidClickButton(pButton) && isMouseOver(pMouseX, pMouseY) && !neighBtnRect.contains((int) pMouseX, (int) pMouseY)) {
double dx = pDragX / (double) MINECRAFT.getWindow().getGuiScaledWidth();
double dy = pDragY / (double) MINECRAFT.getWindow().getGuiScaledHeight();
yaw += 4 * (float)dx * 180;
pitch += 2 * (float)dy * 180;

Expand Down Expand Up @@ -300,6 +313,9 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float
renderOverlay(guiGraphics);

guiGraphics.disableScissor();

// after scissor to prevent clipping the tooltip
renderNeighbourButton(guiGraphics, mouseX, mouseY);
}
}

Expand Down Expand Up @@ -397,24 +413,29 @@ private void renderSelection(GuiGraphics guiGraphics, int centerX, int centerY,

private void renderOverlay(GuiGraphics guiGraphics) {
if (selection.isPresent()) {
Minecraft minecraft = Minecraft.getInstance();

var selectedFace = selection.get();
BlockEntity entity = MINECRAFT.level.getBlockEntity(selectedFace.blockPos);
if (entity instanceof MachineBlockEntity machine) {
var ioMode = machine.getIOMode(selectedFace.side);
IOModeMap map = IOModeMap.getMapFromMode(ioMode);
Rect2i iconBounds = map.getRect();
guiGraphics.blitSprite(IO_CONFIG_OVERLAY, 48, 16, iconBounds.getX(), iconBounds.getY(), getX() + 4,
getY() + height - 4 - minecraft.font.lineHeight - iconBounds.getHeight(), iconBounds.getWidth(), iconBounds.getHeight());
getY() + height - 4 - MINECRAFT.font.lineHeight - iconBounds.getHeight(), iconBounds.getWidth(), iconBounds.getHeight());
guiGraphics.pose().pushPose();
guiGraphics.pose().translate(0, 0, OVERLAY_Z_OFFSET); // to ensure that string is drawn on top
guiGraphics.drawString(minecraft.font, map.getComponent(), getX() + 4, getY() + height - 2 - minecraft.font.lineHeight, 0xFFFFFFFF);
guiGraphics.drawString(MINECRAFT.font, map.getComponent(), getX() + 4, getY() + height - 2 - MINECRAFT.font.lineHeight, 0xFFFFFFFF);
guiGraphics.pose().popPose();
}
}
}

private void renderNeighbourButton(GuiGraphics guiGraphics, int mouseX, int mouseY) {
guiGraphics.blitSprite(NEIGHBOURS_BTN, neighBtnRect.getX(), neighBtnRect.getY(), 16, 16);
if(neighBtnRect.contains(mouseX, mouseY)) {
guiGraphics.renderTooltip(MINECRAFT.font, EIOLang.TOGGLE_NEIGHBOUR.copy().withStyle(ChatFormatting.WHITE), mouseX, mouseY);
}
}

@Override
protected void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {
}
Expand Down

0 comments on commit 78f86ca

Please sign in to comment.