Skip to content

Commit

Permalink
Deduplicate the very common x, y, and scale parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 28, 2024
1 parent 0f5f4fd commit 51803ee
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,14 @@ private static void registerEventHandlers() {
if (clientGame == null || !(screen instanceof ChatScreen)) {
return false;
}
final Window window = Minecraft.getInstance().getWindow();
final float scale = CONFIG.getBoardScale();
final float x = CONFIG.getBoardCorner().getX(window.getGuiScaledWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(window.getGuiScaledHeight(), scale);
return detectPress(keyCode, scanCode, x, y, scale);
return detectPress(keyCode, scanCode, getBoardPosition());
});

ClientEvents.MOUSE_RELEASED_PRE.register((screen, mouseX, mouseY, button) -> {
if (clientGame == null || !(screen instanceof ChatScreen)) {
return false;
}
final Window window = Minecraft.getInstance().getWindow();
final float scale = CONFIG.getBoardScale();
final float x = CONFIG.getBoardCorner().getX(window.getGuiScaledWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(window.getGuiScaledHeight(), scale);
return detectClick(button, x, y, scale);
return detectClick(button, getBoardPosition());
});

ClientEvents.PLAYER_QUIT.register(player -> {
Expand All @@ -122,6 +114,14 @@ private static void registerEventHandlers() {
});
}

public static PositionAndScale getBoardPosition() {
final Window window = Minecraft.getInstance().getWindow();
final float scale = CONFIG.getBoardScale();
final float x = CONFIG.getBoardCorner().getX(window.getGuiScaledWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(window.getGuiScaledHeight(), scale);
return new PositionAndScale(x, y, scale);
}

public static RecipeViewerPlugin getRecipeViewerPlugin() {
if (recipeViewerPlugin == null) {
recipeViewerPlugin = RecipeViewerPlugin.detect();
Expand All @@ -133,10 +133,8 @@ public static void renderBoardOnHud(Minecraft minecraft, GuiGraphics graphics) {
if (clientGame == null || minecraft.getDebugOverlay().showDebugScreen() || minecraft.screen instanceof BoardScreen) {
return;
}
final float scale = CONFIG.getBoardScale();
final float x = CONFIG.getBoardCorner().getX(graphics.guiWidth(), scale);
final float y = CONFIG.getBoardCorner().getY(graphics.guiHeight(), scale);
renderBingo(graphics, minecraft.screen instanceof ChatScreen, x, y, scale);
final PositionAndScale pos = getBoardPosition();
renderBingo(graphics, minecraft.screen instanceof ChatScreen, pos);

if (CONFIG.isShowScoreCounter() && clientGame.renderMode() == BingoGameMode.RenderMode.ALL_TEAMS) {
class TeamValue {
Expand Down Expand Up @@ -164,12 +162,12 @@ class TeamValue {
Arrays.sort(teams, Comparator.comparing(v -> -v.score)); // Sort in reverse

final Font font = minecraft.font;
final int scoreX = (int)(x * scale + getBoardWidth() * scale / 2);
final int scoreX = (int)(pos.x() * pos.scale() + getBoardWidth() * pos.scale() / 2);
int scoreY;
if (CONFIG.getBoardCorner().isOnBottom) {
scoreY = (int)((y - BOARD_OFFSET) * scale - font.lineHeight);
scoreY = (int)((pos.y() - BOARD_OFFSET) * pos.scale() - font.lineHeight);
} else {
scoreY = (int)(y * scale + (getBoardHeight() + BOARD_OFFSET) * scale);
scoreY = (int)(pos.y() * pos.scale() + (getBoardHeight() + BOARD_OFFSET) * pos.scale());
}
final int shift = CONFIG.getBoardCorner().isOnBottom ? -12 : 12;
for (final TeamValue teamValue : teams) {
Expand Down Expand Up @@ -201,18 +199,18 @@ public static int getBoardHeight() {
return 24 + 18 * clientGame.size();
}

public static void renderBingo(GuiGraphics graphics, boolean mouseHover, float x, float y, float scale) {
public static void renderBingo(GuiGraphics graphics, boolean mouseHover, PositionAndScale pos) {
if (clientGame == null) {
Bingo.LOGGER.warn("BingoClient.renderBingo() called when Bingo.clientGame == null!");
return;
}
final Minecraft minecraft = Minecraft.getInstance();

graphics.pose().pushPose();
graphics.pose().scale(scale, scale, 1);
graphics.pose().translate(x, y, 0);
graphics.pose().scale(pos.scale(), pos.scale(), 1);
graphics.pose().translate(pos.x(), pos.y(), 0);

final BingoMousePos mousePos = mouseHover ? BingoMousePos.getPos(minecraft, clientGame.size(), x, y, scale) : null;
final BingoMousePos mousePos = mouseHover ? BingoMousePos.getPos(minecraft, clientGame.size(), pos) : null;

graphics.blitSprite(
BOARD_TEXTURE, 0, 0,
Expand Down Expand Up @@ -315,20 +313,20 @@ public static FormattedCharSequence getVisualOrderWithEllipses(Component text, F
return Language.getInstance().getVisualOrder(combinedText);
}

public static boolean detectClick(int button, float x, float y, float scale) {
return detectClickOrPress(InputConstants.Type.MOUSE.getOrCreate(button), x, y, scale);
public static boolean detectClick(int button, PositionAndScale boardPos) {
return detectClickOrPress(InputConstants.Type.MOUSE.getOrCreate(button), boardPos);
}

public static boolean detectPress(int keyCode, int scanCode, float x, float y, float scale) {
return detectClickOrPress(InputConstants.getKey(keyCode, scanCode), x, y, scale);
public static boolean detectPress(int keyCode, int scanCode, PositionAndScale boardPos) {
return detectClickOrPress(InputConstants.getKey(keyCode, scanCode), boardPos);
}

public static boolean detectClickOrPress(InputConstants.Key key, float x, float y, float scale) {
public static boolean detectClickOrPress(InputConstants.Key key, PositionAndScale boardPos) {
if (clientGame == null) {
return false;
}

final BingoMousePos mousePos = BingoMousePos.getPos(Minecraft.getInstance(), clientGame.size(), x, y, scale);
final BingoMousePos mousePos = BingoMousePos.getPos(Minecraft.getInstance(), clientGame.size(), boardPos);
if (!mousePos.hasSlotPos()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import net.minecraft.client.Minecraft;

public record BingoMousePos(double mouseX, double mouseY, int slotIdX, int slotIdY) {
public static BingoMousePos getPos(Minecraft minecraft, int size, float x, float y, float scale) {
public static BingoMousePos getPos(Minecraft minecraft, int size, PositionAndScale boardPos) {
final double mouseX = minecraft.mouseHandler.xpos() * minecraft.getWindow().getGuiScaledWidth() / minecraft.getWindow().getScreenWidth();
final double mouseY = minecraft.mouseHandler.ypos() * minecraft.getWindow().getGuiScaledHeight() / minecraft.getWindow().getScreenHeight();
final double relX = (mouseX - x * scale) / scale;
final double relY = (mouseY - y * scale) / scale;
final double relX = (mouseX - boardPos.x() * boardPos.scale()) / boardPos.scale();
final double relY = (mouseY - boardPos.y() * boardPos.scale()) / boardPos.scale();
final double slotIdXD = (relX - 7) / 18;
final double slotIdYD = (relY - 17) / 18;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@ protected void init() {
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
if (BingoClient.clientGame == null) return;
final float x = width / 2f - BingoClient.getBoardWidth() / 2f;
final float y = height / 2f - BingoClient.getBoardHeight() / 2f;
BingoClient.renderBingo(graphics, true, x, y, 1f);
final PositionAndScale pos = getPosition();
BingoClient.renderBingo(graphics, true, pos);
assert minecraft != null;
if (minecraft.player != null && minecraft.player.isSpectator()) {
final PlayerTeam team = BingoClient.clientGame.teams()[BingoClient.clientTeam.getFirstIndex()];
final Integer color = team.getColor().getColor();
graphics.drawCenteredString(
font,
BingoClient.getDisplayName(team),
width / 2, (int)y + BingoClient.getBoardHeight() + BingoClient.BOARD_OFFSET,
width / 2, (int)pos.y() + BingoClient.getBoardHeight() + BingoClient.BOARD_OFFSET,
color != null ? color : 0xffffff
);
}
Expand All @@ -56,12 +55,7 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTi
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
assert minecraft != null;
if (BingoClient.clientGame != null && BingoClient.detectPress(
keyCode, scanCode,
width / 2f - BingoClient.getBoardWidth() / 2f,
height / 2f - BingoClient.getBoardHeight() / 2f,
1f
)) {
if (BingoClient.clientGame != null && BingoClient.detectPress(keyCode, scanCode, getPosition())) {
return true;
} else if (leftButton.visible && minecraft.options.keyLeft.matches(keyCode, scanCode)) {
switchTeam(-1);
Expand All @@ -73,12 +67,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (BingoClient.clientGame != null && BingoClient.detectClick(
button,
width / 2f - BingoClient.getBoardWidth() / 2f,
height / 2f - BingoClient.getBoardHeight() / 2f,
1f
)) {
if (BingoClient.clientGame != null && BingoClient.detectClick(button, getPosition())) {
return true;
}
return super.mouseClicked(mouseX, mouseY, button);
Expand All @@ -99,6 +88,14 @@ public boolean isPauseScreen() {
return false;
}

public PositionAndScale getPosition() {
return new PositionAndScale(
width / 2f - BingoClient.getBoardWidth() / 2f,
height / 2f - BingoClient.getBoardHeight() / 2f,
1f
);
}

private void updateButtonVisibility() {
assert minecraft != null;
final boolean isSpectator = minecraft.player != null && minecraft.player.isSpectator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.gaming32.bingo.client;

public record PositionAndScale(float x, float y, float scale) {
}

0 comments on commit 51803ee

Please sign in to comment.