Skip to content

Commit

Permalink
Refactor rendering code to use integers
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <[email protected]>
  • Loading branch information
Octol1ttle committed Feb 23, 2024
1 parent 35657ea commit 063b525
Show file tree
Hide file tree
Showing 26 changed files with 139 additions and 131 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ru/octol1ttle/flightassistant/Dimensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class Dimensions {
public int bFrame;

public void update(DrawContext context, double fov) {
hScreen = MathHelper.ceil(context.getScaledWindowHeight() / FAConfig.get().hudScale);
wScreen = MathHelper.ceil(context.getScaledWindowWidth() / FAConfig.get().hudScale);
hScreen = MathHelper.floor(context.getScaledWindowHeight() / FAConfig.get().hudScale);
wScreen = MathHelper.floor(context.getScaledWindowWidth() / FAConfig.get().hudScale);

degreesPerPixel = MathHelper.ceil(hScreen / fov);
degreesPerPixel = MathHelper.floor(hScreen / fov);
xMid = wScreen / 2;
yMid = hScreen / 2;

Expand Down
47 changes: 24 additions & 23 deletions src/main/java/ru/octol1ttle/flightassistant/HudComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ public static MutableText asText(String format, Object... args) {
return Text.literal(String.format(format, args));
}

public static void fill(DrawContext context, float x1, float y1, float x2, float y2, Color color) {
context.fill(Math.round(x1), Math.round(y1), Math.round(x2), Math.round(y2), color.getRGB());
public static void fill(DrawContext context, int x1, int y1, int x2, int y2, Color color) {
context.fill(x1, y1, x2, y2, color.getRGB());
}

protected static void drawRightAlignedText(TextRenderer textRenderer, DrawContext context, Text text, float x, float y, Color color) {
protected static void drawRightAlignedText(TextRenderer textRenderer, DrawContext context, Text text, int x, int y, Color color) {
drawText(textRenderer, context, text, x - textRenderer.getWidth(text), y, color);
}

public static int drawText(TextRenderer textRenderer, DrawContext context, Text text, float x, float y, Color color) {
context.drawText(textRenderer, text, Math.round(x), Math.round(y), color.getRGB(), false);
public static int drawText(TextRenderer textRenderer, DrawContext context, Text text, int x, int y, Color color) {
context.drawText(textRenderer, text, x, y, color.getRGB(), false);
return SINGLE_LINE_DRAWN;
}

public static void drawMiddleAlignedText(TextRenderer textRenderer, DrawContext context, Text text, float x, float y, Color color) {
drawText(textRenderer, context, text, x - textRenderer.getWidth(text) * 0.5f, y, color);
public static void drawMiddleAlignedText(TextRenderer textRenderer, DrawContext context, Text text, int x, int y, Color color) {
drawText(textRenderer, context, text, x - textRenderer.getWidth(text) / 2, y, color);
}

public static int drawHighlightedText(TextRenderer textRenderer, DrawContext context, Text text, float x, float y, Color color, boolean highlight) {
public static int drawHighlightedText(TextRenderer textRenderer, DrawContext context, Text text, int x, int y, Color color, boolean highlight) {
drawUnbatched(() -> {
if (highlight) {
HudComponent.fill(context, x - 2.0f, y - 1.0f, x + textRenderer.getWidth(text) + 1.0f, y + 8.0f, color);
HudComponent.fill(context, x - 2, y - 1, x + textRenderer.getWidth(text) + 1, y + 8, color);
HudComponent.drawText(textRenderer, context, text, x, y, getContrasting(color));
} else {
HudComponent.drawText(textRenderer, context, text, x, y, color);
Expand All @@ -49,8 +49,8 @@ private static Color getContrasting(Color original) {
}

// that name doe
public static void drawHighlightedMiddleAlignedText(TextRenderer textRenderer, DrawContext context, Text text, float x, float y, Color color, boolean highlight) {
drawHighlightedText(textRenderer, context, text, x - textRenderer.getWidth(text) * 0.5f, y, color, highlight);
public static void drawHighlightedMiddleAlignedText(TextRenderer textRenderer, DrawContext context, Text text, int x, int y, Color color, boolean highlight) {
drawHighlightedText(textRenderer, context, text, x - textRenderer.getWidth(text) / 2, y, color, highlight);
}

public static void drawUnbatched(Runnable draw) {
Expand All @@ -63,35 +63,36 @@ public static void drawUnbatched(Runnable draw) {
}
}

public static void drawHorizontalLine(DrawContext context, float x1, float x2, float y, Color color) {
context.drawHorizontalLine(Math.round(x1), Math.round(x2), Math.round(y), color.getRGB());
public static void drawHorizontalLine(DrawContext context, int x1, int x2, int y, Color color) {
context.drawHorizontalLine(x1, x2, y, color.getRGB());
}

public static void drawVerticalLine(DrawContext context, float x, float y1, float y2, Color color) {
context.drawVerticalLine(Math.round(x), Math.round(y1), Math.round(y2), color.getRGB());
public static void drawVerticalLine(DrawContext context, int x, int y1, int y2, Color color) {
context.drawVerticalLine(x, y1, y2, color.getRGB());
}

public static void drawBorder(DrawContext context, float x, float y, float w, Color color) {
context.drawBorder(Math.round(x), Math.round(y), Math.round(w), 11 /* off-by-one my beloved */, color.getRGB());
public static void drawBorder(DrawContext context, int x, int y, int w, Color color) {
context.drawBorder(x, y, w, 11, color.getRGB());
}

protected static void drawHorizontalLineDashed(DrawContext context, float x1, float x2, float y,
protected static void drawHorizontalLineDashed(DrawContext context, int x1, int x2, int y,
int dashCount, Color color) {
float width = x2 - x1;
int width = x2 - x1;
int segmentCount = dashCount * 2 - 1;
float dashSize = width / (float) segmentCount;
int dashSize = width / segmentCount;
for (int i = 0; i < segmentCount; i++) {
if (i % 2 != 0) {
continue;
}
float dx1 = i * dashSize + x1;
float dx2;
int dx1 = i * dashSize + x1;

int dx2;
if (i == segmentCount - 1) {
dx2 = x2;
} else {
dx2 = ((i + 1) * dashSize) + x1;
}
context.drawHorizontalLine(Math.round(dx1), Math.round(dx2), Math.round(y), color.getRGB());
context.drawHorizontalLine(dx1, dx2, y, color.getRGB());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public abstract class AbstractAlert {
@NotNull
public abstract AlertSoundData getAlertSoundData();

public boolean renderCentered(TextRenderer textRenderer, DrawContext context, float width, float y, boolean highlight) {
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, int width, int y, boolean highlight) {
return false;
}

public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return HudComponent.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.autoflight.auto_firework_off"), x, y,
FAConfig.hud().cautionColor,
highlight && autoflight.afrwkDisconnectionForced);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return HudComponent.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.autoflight.autopilot_off"), x, y,
FAConfig.hud().warningColor,
highlight && autoflight.apDisconnectionForced);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
int i = 0;
for (IComputer computer : host.faulted) {
i += HudComponent.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.fault.computers." + computer.getId()), x, y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
int i = 0;
for (HudComponent component : renderer.faulted) {
i += HudComponent.drawText(textRenderer, context, Text.translatable("alerts.flightassistant.fault.indicators." + component.getId()), x, y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return HudComponent.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.firework.no_response"), x, y,
FAConfig.hud().warningColor, highlight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return HudComponent.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.firework.unsafe"), x, y,
FAConfig.hud().warningColor, highlight && data.isFlying);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
Text text = voidLevel.status == VoidLevelComputer.VoidLevelStatus.REACHED_DAMAGE_LEVEL
? Text.translatable("alerts.flightassistant.reached_void_damage_level")
: Text.translatable("alerts.flightassistant.approaching_void_damage_level");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public boolean isTriggered() {
}

@Override
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
if (gpws.descentImpactTime <= PULL_UP_THRESHOLD) {
HudComponent.drawHighlightedMiddleAlignedText(textRenderer, context, Text.translatable("alerts.flightassistant.pull_up"), x, y,
FAConfig.hud().warningColor, highlight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean isTriggered() {
}

@Override
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
if (gpws.terrainImpactTime <= PULL_UP_THRESHOLD) {
HudComponent.drawHighlightedMiddleAlignedText(textRenderer, context, Text.translatable("alerts.flightassistant.pull_up"), x, y,
FAConfig.hud().warningColor, highlight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean isTriggered() {
}

@Override
public int renderECAM(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public int renderECAM(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
return HudComponent.drawHighlightedText(textRenderer, context, Text.translatable("alerts.flightassistant.elytra_health_low"), x, y,
FAConfig.hud().warningColor, highlight && data.isFlying);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public boolean isTriggered() {
}

@Override
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, float x, float y, boolean highlight) {
public boolean renderCentered(TextRenderer textRenderer, DrawContext context, int x, int y, boolean highlight) {
HudComponent.drawHighlightedMiddleAlignedText(textRenderer, context, Text.translatable("alerts.flightassistant.stall"), x, y, FAConfig.hud().warningColor, highlight);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void render(DrawContext context, TextRenderer textRenderer) {
return;
}
boolean renderedCentered = false;
float x = dim.lFrame + 5;
float y = dim.tFrame + 15;
int x = dim.lFrame + 5;
int y = dim.tFrame + 15;

for (AbstractAlert alert : alert.activeAlerts) {
if (!renderedCentered) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,36 @@ public AltitudeIndicator(Dimensions dim, AirDataComputer data, AutoFlightCompute

@Override
public void render(DrawContext context, TextRenderer textRenderer) {
float top = dim.tFrame;
float bottom = dim.bFrame;
int top = dim.tFrame;
int bottom = dim.bFrame;

float right = dim.rFrame + 2;
float left = dim.rFrame;
int right = dim.rFrame + 2;
int left = dim.rFrame;

float blocksPerPixel = 1;
int blocksPerPixel = 1;

float floorOffset = Math.round(data.altitude * blocksPerPixel);
float yFloor = dim.yMid - floorOffset;
float xAltText = right + 5;
int floorOffset = Math.round(data.altitude * blocksPerPixel);
int yFloor = dim.yMid - floorOffset;
int xAltText = right + 5;

int safeLevel = data.groundLevel == data.voidLevel ? data.voidLevel + 16 : data.groundLevel;

if (FAConfig.hud().showAltitudeReadout) {
Color color = getAltitudeColor(safeLevel, data.altitude);
drawText(textRenderer, context, asText("%.0f", data.altitude), xAltText, dim.yMid - 3, color);
drawBorder(context, xAltText - 2, dim.yMid - 5.0f, 28, color);
drawBorder(context, xAltText - 2, dim.yMid - 5, 28, color);
}

if (FAConfig.hud().showGroundAltitude) {
Color color = data.altitude < safeLevel ? FAConfig.hud().warningColor : FAConfig.hud().frameColor;
drawText(textRenderer, context, Text.translatable(data.groundLevel == data.voidLevel ? "flightassistant.void_level" : "flightassistant.ground_level"), xAltText - 10, bottom, color);
drawText(textRenderer, context, asText("%d", MathHelper.floor(data.heightAboveGround)), xAltText, bottom, color);
drawBorder(context, xAltText - 2, bottom - 2.0f, 28, color);
drawBorder(context, xAltText - 2, bottom - 2, 28, color);
}

if (FAConfig.hud().showAltitudeScale) {
for (int i = -150; i < 1000; i++) {
float y = (dim.hScreen - i * blocksPerPixel) - yFloor;
int y = (dim.hScreen - i * blocksPerPixel) - yFloor;
if (y > (bottom - 5) || i < data.groundLevel) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public ElytraHealthIndicator(Dimensions dim, AirDataComputer data) {

@Override
public void render(DrawContext context, TextRenderer textRenderer) {
float x = dim.xMid;
float y = dim.bFrame;
int x = dim.xMid;
int y = dim.bFrame;

if (FAConfig.hud().showElytraHealth && data.elytraHealth != null) {
Color color;
Expand All @@ -32,7 +32,7 @@ public void render(DrawContext context, TextRenderer textRenderer) {
} else {
color = data.elytraHealth <= 10.0f ? FAConfig.hud().cautionColor : FAConfig.hud().frameColor;
}
drawBorder(context, x - 3.5f, y - 2.0f, 30, color);
drawBorder(context, x - 3, y - 2, 30, color);
drawText(textRenderer, context, Text.translatable("flightassistant.elytra_short"), x - 10, y, color);

drawText(textRenderer, context, asText("%d", MathHelper.ceil(data.elytraHealth)).append("%"), x, y, color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void render(DrawContext context, TextRenderer textRenderer) {

if (autoflight.getTargetPitch() != null) {
float deltaPitch = autoflight.getTargetPitch() - data.pitch;
float fdY = MathHelper.clamp(dim.yMid - Math.round(deltaPitch * dim.degreesPerPixel), dim.tFrame - 10, dim.bFrame + 10);
drawHorizontalLine(context, dim.xMid - dim.wFrame * 0.1f, dim.xMid + dim.wFrame * 0.1f, fdY, FAConfig.hud().advisoryColor);
int fdY = MathHelper.clamp(MathHelper.floor(dim.yMid - deltaPitch * dim.degreesPerPixel), dim.tFrame - 10, dim.bFrame + 10);
drawHorizontalLine(context, dim.xMid - dim.wFrame / 10, dim.xMid + dim.wFrame / 10, fdY, FAConfig.hud().advisoryColor);
}

if (autoflight.getTargetHeading() != null) {
Expand All @@ -42,8 +42,8 @@ public void render(DrawContext context, TextRenderer textRenderer) {
deltaHeading -= 360.0f;
}

float fdX = MathHelper.clamp(dim.xMid + Math.round(deltaHeading * dim.degreesPerPixel), dim.lFrame + 10, dim.rFrame - 10);
drawVerticalLine(context, fdX, dim.yMid - dim.hFrame * 0.15f, dim.yMid + dim.hFrame * 0.15f, FAConfig.hud().advisoryColor);
int fdX = MathHelper.clamp(MathHelper.floor(dim.xMid + deltaHeading * dim.degreesPerPixel), dim.lFrame + 10, dim.rFrame - 10);
drawVerticalLine(context, fdX, dim.yMid - dim.hFrame / 7, dim.yMid + dim.hFrame / 7, FAConfig.hud().advisoryColor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void update(Text newText, boolean forceFlash) {
lastText = newText;
}

public void render(DrawContext context, TextRenderer textRenderer, float x, float y) {
public void render(DrawContext context, TextRenderer textRenderer, int x, int y) {
if (lastUpdateTime == null) {
throw new IllegalStateException("Called render before updating");
}
Expand Down
Loading

0 comments on commit 063b525

Please sign in to comment.