From d11c41bf4d57d41ae51d4e7fd348013c5eb75828 Mon Sep 17 00:00:00 2001 From: rafern Date: Tue, 24 Aug 2021 00:32:18 +0100 Subject: [PATCH] Re-done crosshair module to support a tone of stuff - also removed unused import in colorcomponent --- .../api/gui/hud/component/ColorComponent.java | 1 - .../impl/module/render/CrosshairModule.java | 203 ++++++++++++------ 2 files changed, 133 insertions(+), 71 deletions(-) diff --git a/src/main/java/me/rigamortis/seppuku/api/gui/hud/component/ColorComponent.java b/src/main/java/me/rigamortis/seppuku/api/gui/hud/component/ColorComponent.java index ab64718..93cbafc 100644 --- a/src/main/java/me/rigamortis/seppuku/api/gui/hud/component/ColorComponent.java +++ b/src/main/java/me/rigamortis/seppuku/api/gui/hud/component/ColorComponent.java @@ -4,7 +4,6 @@ import me.rigamortis.seppuku.Seppuku; import me.rigamortis.seppuku.api.texture.Texture; import me.rigamortis.seppuku.api.util.ColorUtil; import me.rigamortis.seppuku.api.util.RenderUtil; -import net.minecraft.client.Minecraft; import java.awt.*; diff --git a/src/main/java/me/rigamortis/seppuku/impl/module/render/CrosshairModule.java b/src/main/java/me/rigamortis/seppuku/impl/module/render/CrosshairModule.java index d65a4b3..23e581f 100644 --- a/src/main/java/me/rigamortis/seppuku/impl/module/render/CrosshairModule.java +++ b/src/main/java/me/rigamortis/seppuku/impl/module/render/CrosshairModule.java @@ -1,30 +1,34 @@ package me.rigamortis.seppuku.impl.module.render; -import me.rigamortis.seppuku.api.event.client.EventSaveConfig; import me.rigamortis.seppuku.api.event.render.EventRender2D; import me.rigamortis.seppuku.api.event.render.EventRenderCrosshairs; -import me.rigamortis.seppuku.api.event.world.EventLoadWorld; import me.rigamortis.seppuku.api.module.Module; -import me.rigamortis.seppuku.api.util.ColorUtil; -import me.rigamortis.seppuku.api.util.RenderUtil; import me.rigamortis.seppuku.api.value.Value; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import team.stiff.pomelo.impl.annotated.handler.annotation.Listener; import java.awt.*; +import static org.lwjgl.opengl.GL11.*; + public final class CrosshairModule extends Module { - public final Value size = new Value("Size", new String[]{"chsize", "CrosshairSize", "CrosshairScale", "size", "scale", "crosssize", "cs"}, "The size of the crosshair in pixels.", 5.0f, 0.1f, 15.0f, 0.1f); - public final Value outline = new Value("Outline", new String[]{"choutline", "CrosshairOutline", "CrosshairBackground", "CrosshairBg", "outline", "out", "chout", "chbg", "crossbg", "bg"}, "Enable or disable the crosshair background/outline.", true); - public final Value color = new Value("Color", new String[]{"color", "c"}, "Change the color of the cross-hair.", new Color(255, 255, 255)); + public final Value size = new Value("Size", new String[]{"chsize", "CrosshairSize", "CrosshairScale", "size", "scale", "crosssize", "cs"}, "The size of the crosshair in pixels.", 5.0f, 0.5f, 15.0f, 0.1f); + public final Value thickness = new Value("Thickness", new String[]{"thickness", "thick", "t"}, "The thickness of the crosshair in pixels.", 1.0f, 0.5f, 15.0f, 0.1f); public final Value alpha = new Value("Alpha", new String[]{"chalpha", "CrosshairAlpha", "alpha", "ca", "cha"}, "The alpha RGBA value of the crosshair.", 255, 1, 255, 1); - private int CROSSHAIR_COLOR = 0xFFFFFFFF; - private int CROSSHAIR_OUTLINE_COLOR = 0xFF000000; + public final Value outline = new Value("Outline", new String[]{"choutline", "CrosshairOutline", "CrosshairBorder", "CrosshairB", "outline", "out", "chout", "chb", "crossb", "b"}, "Enable or disable the crosshair border/outline.", true); + public final Value outlineColor = new Value("OutlineColor", new String[]{"outlinecolor", "oc"}, "Change the color of the cross-hair's outline.", new Color(0, 0, 0)); + public final Value outlineThickness = new Value("OutlineThickness", new String[]{"outlinethickness", "outlinethick", "outlinet", "ot", "othickness", "othick"}, "The thickness of the crosshair's border/outline in pixels. Some GPUs don't support this.", 1.0f, 0.5f, 15.0f, 0.1f); - private float x, y, w, h; + public final Value fill = new Value("Fill", new String[]{"cfill", "CrosshairFill", "CrosshairBackground", "CrosshairBg", "outline", "out", "chout", "chbg", "crossbg", "bg"}, "Enable or disable the crosshair background/outline.", true); + public final Value fillColor = new Value("FillColor", new String[]{"fillcolor", "fc"}, "Change the color of the cross-hair.", new Color(255, 255, 255)); + public final Value fillInvert = new Value("FillInvert", new String[]{"FillInvert", "FInvert", "FillInv", "FInv", "FillNegative", "FNegative", "FillNeg", "FNeg", "invert", "inv", "negative", "neg", "fi", "fn"}, "Invert crosshair color like in vanilla.", true); public CrosshairModule() { super("Crosshair", new String[]{"Cross", "Xhair", "Chair"}, "NONE", -1, ModuleType.RENDER); @@ -33,77 +37,136 @@ public final class CrosshairModule extends Module { @Listener public void render2D(EventRender2D event) { + // abort if crosshair is hidden + if (!this.fill.getValue() && !this.outline.getValue()) { + return; + } + final Minecraft mc = Minecraft.getMinecraft(); final ScaledResolution sr = new ScaledResolution(mc); - this.setW(this.size.getValue()); - this.setH(this.size.getValue()); + final float alpha = (float)this.alpha.getValue() / 255; - this.setX((sr.getScaledWidth() / 2.0f) - (this.getW() / 2.0f)); - this.setY((sr.getScaledHeight() / 2.0f) - (this.getH() / 2.0f)); + float size = this.size.getValue(); + float thickness = this.thickness.getValue(); - // render bg - if (this.outline.getValue()) { - //RenderUtil.drawBorderedRect(this.getX() + (this.getW() / 2.0f) - 0.5f, this.getY() - 0.5f, this.getX() + (this.getW() / 2.0f), this.getY() + this.getH() + 0.5f, 1f, 0x00000000, CROSSHAIR_OUTLINE_COLOR); - //RenderUtil.drawBorderedRect(this.getX() - 1f, this.getY() + (this.getH() / 2.0f), this.getX() + this.getW() + 1f, this.getY() + (this.getH() / 2.0f) + 0.5f, 1f, 0x00000000, CROSSHAIR_OUTLINE_COLOR); - RenderUtil.drawRect(this.getX() + (this.getW() / 2.0f) - 1f, this.getY() - 0.5f, this.getX() + (this.getW() / 2.0f) + 0.5f, this.getY() + this.getH() + 1f, CROSSHAIR_OUTLINE_COLOR); - RenderUtil.drawRect(this.getX() - 1f, this.getY() + (this.getH() / 2.0f) - 0.5f, this.getX() + this.getW() + 0.5f, this.getY() + (this.getH() / 2.0f) + 1f, CROSSHAIR_OUTLINE_COLOR); + // if thickness > size rendering fails. swap them if this is true + if (thickness > size) { + final float temp = size; + size = thickness; + thickness = temp; } - // render plus sign - RenderUtil.drawThinLine(this.getX() + (this.getW() / 2.0f), this.getY(), this.getX() + (this.getW() / 2.0f), this.getY() + this.getH() + 0.5f, CROSSHAIR_COLOR); - RenderUtil.drawThinLine(this.getX() - 0.5f, this.getY() + (this.getH() / 2.0f), this.getX() + this.getW(), this.getY() + (this.getH() / 2.0f), CROSSHAIR_COLOR); + final float xMid = (float)sr.getScaledWidth() / 2; + final float yMid = (float)sr.getScaledHeight() / 2; + final float halfSize = size / 2; + final float left = xMid - halfSize; + final float right = xMid + halfSize; + final float top = yMid - halfSize; + final float bottom = yMid + halfSize; + final float halfThick = thickness / 2; + final float xEdgeMin = xMid - halfThick; + final float xEdgeMax = xMid + halfThick; + final float yEdgeMin = yMid - halfThick; + final float yEdgeMax = yMid + halfThick; + + // ussing tesselator directly since renderutil doesnt work with custom + // blend functions (for color inversion) and this is more efficient + // anyway + final Tessellator tessellator = Tessellator.getInstance(); + final BufferBuilder bufferbuilder = tessellator.getBuffer(); + GlStateManager.enableBlend(); + GlStateManager.disableTexture2D(); + + // crosshair fill + if (this.fill.getValue()) { + final Color fillColor = this.fillColor.getValue(); + final float red = (float)fillColor.getRed() / 255; + final float green = (float)fillColor.getGreen() / 255; + final float blue = (float)fillColor.getBlue() / 255; + + if (this.fillInvert.getValue()) { + GlStateManager.tryBlendFuncSeparate(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR, 1, 0); + } else { + GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1, 0); + } + + bufferbuilder.begin(GL_QUADS, DefaultVertexFormats.POSITION_COLOR); + + // horizontal line + bufferbuilder.pos(left, yEdgeMax, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(right, yEdgeMax, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(right, yEdgeMin, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(left, yEdgeMin, 0.0D).color(red, green, blue, alpha).endVertex(); + // vertical line top + // needs 2 quads for the vertical line so that there isn't overdraw, + // which messes with alpha and invert mode + bufferbuilder.pos(xEdgeMin, yEdgeMin, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMax, yEdgeMin, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMax, top, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMin, top, 0.0D).color(red, green, blue, alpha).endVertex(); + // vertical line bottom + bufferbuilder.pos(xEdgeMin, bottom, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMax, bottom, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMax, yEdgeMax, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMin, yEdgeMax, 0.0D).color(red, green, blue, alpha).endVertex(); + /* + bufferbuilder.pos(left, bottom, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(right, bottom, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(right, top, 0.0D).color(red, green, blue, alpha).endVertex(); + bufferbuilder.pos(left, top, 0.0D).color(red, green, blue, alpha).endVertex(); + */ + + tessellator.draw(); + } + + // crosshair outline + if (this.outline.getValue()) { + final Color outlineColor = this.outlineColor.getValue(); + final float ored = (float)outlineColor.getRed() / 255; + final float ogreen = (float)outlineColor.getGreen() / 255; + final float oblue = (float)outlineColor.getBlue() / 255; + + GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1, 0); + GlStateManager.shadeModel(GL_SMOOTH); + glLineWidth(this.outlineThickness.getValue()); + glEnable(GL_LINE_SMOOTH); + glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); + + bufferbuilder.begin(GL_LINE_LOOP, DefaultVertexFormats.POSITION_COLOR); + + // top left quadrant + bufferbuilder.pos(left, yEdgeMin, 0).color(ored, ogreen, oblue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMin, yEdgeMin, 0).color(ored, ogreen, oblue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMin, top, 0).color(ored, ogreen, oblue, alpha).endVertex(); + // top + bufferbuilder.pos(xEdgeMax, top, 0).color(ored, ogreen, oblue, alpha).endVertex(); + // top right quadrant + bufferbuilder.pos(xEdgeMax, yEdgeMin, 0).color(ored, ogreen, oblue, alpha).endVertex(); + bufferbuilder.pos(right, yEdgeMin, 0).color(ored, ogreen, oblue, alpha).endVertex(); + // right + bufferbuilder.pos(right, yEdgeMax, 0).color(ored, ogreen, oblue, alpha).endVertex(); + // bottom right quadrant + bufferbuilder.pos(xEdgeMax, yEdgeMax, 0).color(ored, ogreen, oblue, alpha).endVertex(); + bufferbuilder.pos(xEdgeMax, bottom, 0).color(ored, ogreen, oblue, alpha).endVertex(); + // bottom + bufferbuilder.pos(xEdgeMin, bottom, 0).color(ored, ogreen, oblue, alpha).endVertex(); + // bottom left quadrant (connects to left in the end) + bufferbuilder.pos(xEdgeMin, yEdgeMax, 0).color(ored, ogreen, oblue, alpha).endVertex(); + bufferbuilder.pos(left, yEdgeMax, 0).color(ored, ogreen, oblue, alpha).endVertex(); + + tessellator.draw(); + + GlStateManager.shadeModel(GL_FLAT); + glDisable(GL_LINE_SMOOTH); + } + + GlStateManager.enableTexture2D(); + GlStateManager.disableBlend(); } @Listener public void onRenderCrosshairs(EventRenderCrosshairs event) { event.setCanceled(true); } - - @Listener - public void onLoadWorld(EventLoadWorld eventLoadWorld) { - this.updateColors(); - } - - @Listener - public void onConfigSave(EventSaveConfig eventSaveConfig) { - this.updateColors(); - } - - private void updateColors() { - this.CROSSHAIR_COLOR = ColorUtil.changeAlpha(new Color(this.color.getValue().getRed(), this.color.getValue().getGreen(), this.color.getValue().getBlue()).getRGB(), this.alpha.getValue()); - this.CROSSHAIR_OUTLINE_COLOR = ColorUtil.changeAlpha(0xFF000000, this.alpha.getValue()); - } - - public float getX() { - return x; - } - - public void setX(float x) { - this.x = x; - } - - public float getY() { - return y; - } - - public void setY(float y) { - this.y = y; - } - - public float getW() { - return w; - } - - public void setW(float w) { - this.w = w; - } - - public float getH() { - return h; - } - - public void setH(float h) { - this.h = h; - } }