From 7a7eb961438770369c01ba325f327622f51008b6 Mon Sep 17 00:00:00 2001 From: tyra314 Date: Wed, 14 Sep 2022 14:26:42 +0200 Subject: [PATCH] Refactoring of player drawing in atlas gui --- .../mc/impl/atlas/client/gui/GuiAtlas.java | 50 ++++++++----------- .../impl/atlas/client/texture/ATexture.java | 14 +++++- .../impl/atlas/client/texture/ITexture.java | 2 + 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/common/src/main/java/hunternif/mc/impl/atlas/client/gui/GuiAtlas.java b/common/src/main/java/hunternif/mc/impl/atlas/client/gui/GuiAtlas.java index 25f2f814a..dccc40cf5 100644 --- a/common/src/main/java/hunternif/mc/impl/atlas/client/gui/GuiAtlas.java +++ b/common/src/main/java/hunternif/mc/impl/atlas/client/gui/GuiAtlas.java @@ -368,7 +368,7 @@ public boolean isEnabled() { if (hasShiftDown()) { markerFinalizer.setMarkerData(player.getEntityWorld(), getAtlasID(), - (int) player.getX(), (int) player.getZ()); + player.getBlockX(), player.getBlockZ()); addChild(markerFinalizer); blinkingIcon.setTexture(markerFinalizer.selectedType.getTexture(), @@ -493,9 +493,9 @@ public void init() { public void updateBookmarkerList() { markers.removeAllContent(); - markers.scrollTo(0,0); + markers.scrollTo(0, 0); - if(localMarkersData == null) return; + if (localMarkersData == null) return; int contentY = 0; @@ -506,12 +506,11 @@ public void updateBookmarkerList() { GuiMarkerBookmark bookmark = new GuiMarkerBookmark(marker); bookmark.addListener(button -> { - if(state.is(NORMAL)) { + if (state.is(NORMAL)) { setTargetPosition(marker.getX(), marker.getZ()); followPlayer = false; btnPosition.setEnabled(true); - } - else if(state.is(DELETING_MARKER)) { + } else if (state.is(DELETING_MARKER)) { AtlasClientAPI.getMarkerAPI().deleteMarker(player.getEntityWorld(), getAtlasID(), marker.getId()); player.getEntityWorld().playSound(player, player.getBlockPos(), @@ -761,7 +760,7 @@ public void tick() { super.tick(); if (player == null) return; if (followPlayer) { - setMapPosition((int)player.getX(), (int)player.getZ()); + setMapPosition(player.getBlockX(), player.getBlockZ()); } if (player.getEntityWorld().getTime() > timeButtonPressed + BUTTON_PAUSE) { navigateByButton(selectedButton); @@ -847,11 +846,11 @@ private void setTargetPosition(int x, int z) { } private int getTargetPositionX() { - return (int)(-targetOffsetX * mapScale); + return (int) (-targetOffsetX * mapScale); } private int getTargetPositionY() { - return (int)(-targetOffsetY * mapScale); + return (int) (-targetOffsetY * mapScale); } @@ -952,7 +951,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float par3) { matrices.push(); matrices.translate(mapStartScreenX, mapStartScreenY, 0); - for(SubTileQuartet subtiles : tiles) { + for (SubTileQuartet subtiles : tiles) { for (SubTile subtile : subtiles) { if (subtile == null || subtile.tile == null) continue; ITexture texture = TileTextureMap.instance().getTexture(subtile); @@ -1023,10 +1022,10 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float par3) { } else { String texture_set = TileTextureMap.instance().getTextureSet(tile).name.toString(); drawTooltip(Arrays.asList( - new LiteralText(coords), - new LiteralText(chunks), - new LiteralText("Tile: " + tile.toString()), - new LiteralText("TSet: " + texture_set)), + new LiteralText(coords), + new LiteralText(chunks), + new LiteralText("Tile: " + tile), + new LiteralText("TSet: " + texture_set)), textRenderer); } } @@ -1039,24 +1038,17 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float par3) { } private void renderPlayer(MatrixStack matrices, double iconScale) { - // How much the player has moved from the top left corner of the map, in pixels: - int playerOffsetX = (int) (player.getX() * mapScale) + mapOffsetX; - int playerOffsetZ = (int) (player.getZ() * mapScale) + mapOffsetY; - playerOffsetX = MathHelper.clamp(playerOffsetX, -MAP_WIDTH / 2, MAP_WIDTH / 2); - playerOffsetZ = MathHelper.clamp(playerOffsetZ, -MAP_HEIGHT / 2, MAP_HEIGHT / 2 - 2); + int playerOffsetX = worldXToScreenX(player.getBlockX()); + int playerOffsetY = worldZToScreenY(player.getBlockZ()); + + playerOffsetX = MathHelper.clamp(playerOffsetX, getGuiX() + MAP_BORDER_WIDTH, getGuiX() + MAP_WIDTH + MAP_BORDER_WIDTH); + playerOffsetY = MathHelper.clamp(playerOffsetY, getGuiY() + MAP_BORDER_HEIGHT, getGuiY() + MAP_HEIGHT + MAP_BORDER_HEIGHT); // Draw the icon: RenderSystem.setShaderColor(1, 1, 1, state.is(PLACING_MARKER) ? 0.5f : 1); - matrices.push(); - - matrices.translate(getGuiX() + WIDTH / 2 + playerOffsetX, getGuiY() + HEIGHT / 2 + playerOffsetZ, 0); float playerRotation = (float) Math.round(player.getYaw() / 360f * PLAYER_ROTATION_STEPS) / PLAYER_ROTATION_STEPS * 360f; - matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(180 + playerRotation)); - matrices.translate((float) (-PLAYER_ICON_WIDTH / 2 * iconScale), (float) (-PLAYER_ICON_HEIGHT / 2 * iconScale), 0f); - - Textures.PLAYER.draw(matrices, 0, 0, (int) Math.round(PLAYER_ICON_WIDTH * iconScale), (int) Math.round(PLAYER_ICON_HEIGHT * iconScale)); - matrices.pop(); + Textures.PLAYER.drawCenteredWithRotation(matrices, playerOffsetX, playerOffsetY, (int) Math.round(PLAYER_ICON_WIDTH * iconScale), (int) Math.round(PLAYER_ICON_HEIGHT * iconScale), playerRotation); RenderSystem.setShaderColor(1, 1, 1, 1); } @@ -1174,8 +1166,8 @@ private void renderMarker(MatrixStack matrices, Marker marker, double scale) { info.scale(0.8); } - markerX = MathHelper.clamp(markerX, getGuiX() + MAP_BORDER_WIDTH, getGuiX() + MAP_WIDTH + MAP_BORDER_WIDTH); - markerY = MathHelper.clamp(markerY, getGuiY() + MAP_BORDER_HEIGHT, getGuiY() + MAP_HEIGHT + MAP_BORDER_HEIGHT); + markerX = MathHelper.clamp(markerX, getGuiX() + MAP_BORDER_WIDTH, getGuiX() + MAP_WIDTH + MAP_BORDER_WIDTH); + markerY = MathHelper.clamp(markerY, getGuiY() + MAP_BORDER_HEIGHT, getGuiY() + MAP_HEIGHT + MAP_BORDER_HEIGHT); info.tex.draw(matrices, markerX + info.x, markerY + info.y, info.width, info.height); diff --git a/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ATexture.java b/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ATexture.java index 1e0e097c3..dbb04e4c8 100644 --- a/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ATexture.java +++ b/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ATexture.java @@ -3,7 +3,6 @@ import com.mojang.blaze3d.systems.RenderSystem; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.render.RenderLayer; import net.minecraft.client.render.VertexConsumer; @@ -11,6 +10,7 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; import net.minecraft.util.math.Matrix4f; +import net.minecraft.util.math.Vec3f; /** * An abstract base class, which implements the ITexture interface using @@ -38,7 +38,6 @@ public Identifier getTexture() { } public void bind() { -// MinecraftClient.getInstance().getTextureManager().bindTexture(this.texture); RenderSystem.setShaderTexture(0, texture); } @@ -61,6 +60,17 @@ public void draw(MatrixStack matrices, int x, int y, int width, int height, int DrawableHelper.drawTexture(matrices, x, y, width, height, u, v, regionWidth, regionHeight, this.width(), this.height()); } + public void drawCenteredWithRotation(MatrixStack matrices, int x, int y, int width, int height, float rotation) { + matrices.push(); + matrices.translate(x, y, 0); + matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(180 + rotation)); + matrices.translate(-width / 2f, -height / 2f, 0f); + + draw(matrices, 0,0, width, height); + + matrices.pop(); + } + public void drawWithLight(VertexConsumerProvider consumer, MatrixStack matrices, int x, int y, int width, int height, int light) { drawWithLight(consumer, matrices, x, y, width, height, 0, 0, this.width(), this.height(), light); } diff --git a/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ITexture.java b/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ITexture.java index c3d475992..615e11420 100644 --- a/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ITexture.java +++ b/common/src/main/java/hunternif/mc/impl/atlas/client/texture/ITexture.java @@ -30,6 +30,8 @@ public interface ITexture { void draw(MatrixStack matrices, int x, int y, int width, int height); + void drawCenteredWithRotation(MatrixStack matrices, int x, int y, int width, int height, float rotation); + void draw(MatrixStack matrices, int x, int y, int width, int height, int u, int v, int regionWidth, int regionHeight); void draw(MatrixStack matrices, int x, int y, int u, int v, int regionWidth, int regionHeight);