Skip to content

Commit

Permalink
Remove UiDrawContext#drawPolygon()
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jul 6, 2024
1 parent 6f1e2a8 commit 7ba31b4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.smyler.smylib.Color;
import fr.thesmyler.terramap.MapContext;
import fr.thesmyler.terramap.input.KeyBindings;
import net.smyler.smylib.gui.gl.GlContext;
import net.smyler.terramap.util.geo.GeoPointReadOnly;
import net.smyler.terramap.util.geo.WebMercatorUtil;
import net.smyler.smylib.math.Mat2d;
Expand All @@ -16,6 +17,8 @@

import static java.lang.Math.*;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.gui.gl.DrawMode.TRIANGLE_FAN;
import static net.smyler.smylib.gui.gl.VertexFormat.POSITION;

/**
* Processes inputs for a map and propagates them to the map controller.
Expand All @@ -34,19 +37,23 @@ public class InputLayer extends MapLayer {

// Stuff that can be pre-computed and that's used later when drawing a polygon at the place the map rotates around.
private static final int ROTATION_POLYGON_VERTEX_COUNT = 5;
private static final double[] ROTATION_POLYGON_VERTICES_OUTER = new double[ROTATION_POLYGON_VERTEX_COUNT * 2];
private static final double[] ROTATION_POLYGON_VERTICES_INNER = new double[ROTATION_POLYGON_VERTEX_COUNT * 2];
private static final double[][] ROTATION_POLYGON_VERTICES_OUTER = new double[ROTATION_POLYGON_VERTEX_COUNT][2];
private static final double[][] ROTATION_POLYGON_VERTICES_INNER = new double[ROTATION_POLYGON_VERTEX_COUNT][2];
private static final float ROTATION_POLYGON_RADIUS_OUTER = 5;
private static final float ROTATION_POLYGON_RADIUS_INNER = 2;
static {
Vec2dMutable outer = new Vec2dMutable(0, -ROTATION_POLYGON_RADIUS_OUTER);
Vec2dMutable inner = new Vec2dMutable(0, -ROTATION_POLYGON_RADIUS_INNER);
Mat2d rot = Mat2d.forRotation(-PI*2 / ROTATION_POLYGON_VERTEX_COUNT);
for(int i = 0; i < ROTATION_POLYGON_VERTEX_COUNT; i++) {
ROTATION_POLYGON_VERTICES_OUTER[2*i] = outer.x;
ROTATION_POLYGON_VERTICES_OUTER[2*i + 1] = outer.y;
ROTATION_POLYGON_VERTICES_INNER[2*i] = inner.x;
ROTATION_POLYGON_VERTICES_INNER[2*i + 1] = inner.y;
ROTATION_POLYGON_VERTICES_OUTER[i] = new double[] {
outer.x,
outer.y
};
ROTATION_POLYGON_VERTICES_INNER[i] = new double[] {
inner.x,
inner.y
};
outer.apply(rot);
inner.apply(rot);
}
Expand All @@ -68,11 +75,7 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo

if(this.isRotating) {
// If we are processing rotation input, draw pentagons at the corresponding spot
context.gl().pushViewMatrix();
context.gl().translate(x + this.rotatePosition.x, y + this.rotatePosition.y);
context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_OUTER);
context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_INNER);
context.gl().popViewMatrix();
this.drawRotationSpot(context, x + this.rotatePosition.x, y + this.rotatePosition.y);
}

if (this.getMap().isDebugMode()) {
Expand Down Expand Up @@ -232,4 +235,24 @@ private boolean isShortcutEnabled() {
return this.map.isInteractive() && Keyboard.isKeyDown(KeyBindings.MAP_SHORTCUT.getKeyCode()) && this.map.allowsQuickTp();
}

private void drawRotationSpot(UiDrawContext context, double x, double y) {
GlContext gl = context.gl();
gl.pushViewMatrix();
gl.translate(x, y);
gl.setColor(Color.DARK_OVERLAY);
this.drawConvexPolygon(gl, ROTATION_POLYGON_VERTICES_OUTER);
this.drawConvexPolygon(gl, ROTATION_POLYGON_VERTICES_INNER);
gl.popViewMatrix();
}

private void drawConvexPolygon(GlContext gl, double[][] vertices) {
gl.startDrawing(TRIANGLE_FAN, POSITION);
gl.vertex().position(0d, 0d, 0d);
for (double[] vertex : vertices) {
gl.vertex().position(vertex[0], vertex[1], 0d).end();
}
gl.vertex().position(0d, 0d, 0d);
gl.draw();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import static net.smyler.smylib.Color.WHITE;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.gui.gl.DrawMode.QUADS;
import static net.smyler.smylib.gui.gl.VertexFormat.POSITION_TEXTURE;

abstract public class RasterMapLayer extends MapLayer {

Expand Down Expand Up @@ -211,7 +213,6 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo
dY += factorY * renderSizedSize;
}

gl.setColor(WHITE);
Identifier texture = defaultTexture;
try {
if(tile.isTextureAvailable()) texture = tile.getTexture();
Expand All @@ -221,13 +222,19 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo
parentMap.reportError(this, e.toString());
}
if (texture != null) {
context.drawTexture(
texture,
dispX, dispY,
dX, dY,
displayWidth, displayHeight,
renderSizedSize, renderSizedSize
);
double f = 1.0f / renderSizedSize;
double uLeft = dX * f;
double uRight = (dX + displayWidth) * f;
double uTop = dY * f;
double uBottom = (dY + displayHeight) * f;
gl.setTexture(texture);
gl.setColor(whiteWithAlpha);
gl.startDrawing(QUADS, POSITION_TEXTURE);
gl.vertex().position(dispX, dispY + displayHeight, 0d).texture(uLeft, uBottom).end();
gl.vertex().position(dispX + displayWidth, dispY + displayHeight, 0d).texture(uRight, uBottom).end();
gl.vertex().position(dispX + displayWidth, dispY, 0d).texture(uRight, uTop).end();
gl.vertex().position(dispX, dispY, 0d).texture(uLeft, uTop).end();
gl.draw();
}
if(debug) {
Color lineColor = texture == null? Color.GREEN: lowerResRender? unlockedZoomRender? Color.BLUE: Color.RED : WHITE;
Expand All @@ -241,7 +248,6 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo
smallFont.draw((float)dispX + 2, (float)(dispY + displayHeight/2), GeoServices.formatGeoCoordForDisplay(dispX), lineColor, false);
smallFont.drawCentered((float)(dispX + displayWidth/2), (float)dispY + 2, GeoServices.formatGeoCoordForDisplay(dispY), lineColor, false);
}
gl.setColor(WHITE);
}
}

Expand Down
11 changes: 0 additions & 11 deletions smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ default void drawGradientRectangle(double xLeft, double yTop, double xRight, dou
this.drawGradientRectangle(0d, xLeft, yTop, xRight, yBottom, upperLeftColor, lowerLeftColor, lowerRightColor, upperRightColor);
}

@Deprecated
void drawPolygon(double z, Color color, double... points);

@Deprecated
default void drawPolygon(Color color, double... points) {
this.drawPolygon(0d, color, points);
}

void drawStrokeLine(double z, Color color, float size, double... points);

default void drawStrokeLine(Color color, float size, double... points) {
Expand Down Expand Up @@ -79,9 +71,6 @@ default void drawSpriteCropped(double x, double y, Sprite sprite, double leftCro
//TODO use Text in drawTooltip
void drawTooltip(String text, double x, double y);

@Deprecated
void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight);

Identifier loadDynamicTexture(BufferedImage image);

void unloadDynamicTexture(Identifier texture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ public void drawGradientRectangle(double z, double xLeft, double yTop, double xR
GlStateManager.enableTexture2D();
}

@Override
public void drawPolygon(double z, Color color, double... points) {
this.drawMultiPointsGeometry(GL11.GL_POLYGON, z, color, points);
}

@Override
public void drawStrokeLine(double z, Color color, float size, double... points) {
GL11.glLineWidth(size * getGameClient().scaleFactor());
Expand Down Expand Up @@ -124,25 +119,6 @@ public void drawTooltip(String text, double x, double y) {
if(!lighting) GlStateManager.disableLighting();
}

@Override
public void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight) {
getMinecraft().getTextureManager().bindTexture(new ResourceLocation(texture.namespace, texture.path));
double f = 1.0f / textureWidth;
double f1 = 1.0f / textureHeight;
GlStateManager.enableAlpha();
GlStateManager.enableBlend();
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder builder = tessellator.getBuffer();
builder.begin(7, DefaultVertexFormats.POSITION_TEX);
builder.pos(x, y + height, 0d).tex(u * f, (v + height) * f1).endVertex();
builder.pos(x + width, y + height, 0d).tex((u + width) * f, (v + height) * f1).endVertex();
builder.pos(x + width, y, 0d).tex((u + width) * f, v * f1).endVertex();
builder.pos(x, y, 0d).tex(u * f, v * f1).endVertex();
tessellator.draw();
GlStateManager.disableAlpha();
GlStateManager.disableBlend();
}

@Override
public Identifier loadDynamicTexture(BufferedImage image) {
DynamicTexture dynamicTexture = new DynamicTexture(image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public void drawGradientRectangle(double z, double xLeft, double yTop, double xR

}

@Override
public void drawPolygon(double z, Color color, double... points) {

}

@Override
public void drawStrokeLine(double z, Color color, float size, double... points) {

Expand All @@ -57,11 +52,6 @@ public void drawTooltip(String text, double x, double y) {

}

@Override
public void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight) {

}

@Override
public Identifier loadDynamicTexture(BufferedImage image) {
Identifier id = new Identifier(
Expand Down

0 comments on commit 7ba31b4

Please sign in to comment.