Skip to content

Commit

Permalink
Redo Internal PIDS System
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Feb 18, 2024
1 parent 60c0392 commit 7766a5f
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void onServerUse(BlockState state, World world, BlockPos pos, PlayerEntit
RVPIDSBlockEntity thisEntity = (RVPIDSBlockEntity) world.getBlockEntity(pos).data;

if (JCMUtil.playerHoldingBrush(player)) {
Networking.sendPacketToClient(player, new PIDSGUIPacket(pos, thisEntity.getCustomMessages(), thisEntity.getRowHidden(), thisEntity.getHidePlatformNumber(), thisEntity.getPresetId()));
Networking.sendPacketToClient(player, new PIDSGUIPacket(pos, thisEntity.getCustomMessages(), thisEntity.getRowHidden(), thisEntity.platformNumberHidden(), thisEntity.getPresetId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void setData(String[] customMessages, boolean[] rowHidden, boolean hidePl
this.markDirty2();
}

public boolean getHidePlatformNumber() {
public boolean platformNumberHidden() {
return hidePlatformNumber;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import com.lx862.jcm.mod.block.entity.PIDSBlockEntity;
import com.lx862.jcm.mod.config.ConfigEntry;
import com.lx862.jcm.mod.data.pids.preset.drawcalls.*;
import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextDrawCall;
import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextureDrawCall;
import com.lx862.jcm.mod.render.RenderHelper;
import com.lx862.jcm.mod.render.text.TextAlignment;
import com.lx862.jcm.mod.render.text.TextInfo;
import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.DrawCall;
import com.lx862.jcm.mod.render.text.TextRenderingManager;
import com.lx862.jcm.mod.util.TextUtil;
import org.mtr.core.operation.ArrivalResponse;
import org.mtr.core.operation.ArrivalsResponse;
import org.mtr.mapping.holder.*;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mapping.mapper.WorldHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RVPIDSPreset extends PIDSPresetBase {
private static final int HEADER_HEIGHT = 11;
private static final int PIDS_MARGIN = 7;
private static final float ARRIVAL_TEXT_SCALE = 1.35F;
private static final Identifier TEXTURE_PLATFORM_CIRCLE = new Identifier("jsblock:textures/block/pids/plat_circle.png");
Expand All @@ -26,11 +31,11 @@ public RVPIDSPreset() {
}

public void render(PIDSBlockEntity be, GraphicsHolder graphicsHolder, World world, Direction facing, ArrivalsResponse arrivals, boolean[] rowHidden, float tickDelta, int x, int y, int width, int height) {
int startX = PIDS_MARGIN;
int contentWidth = width - (PIDS_MARGIN * 2);
int rowAmount = be.getRowAmount();
boolean hidePlatform = be.getHidePlatformNumber();
int contentHeight = height - HEADER_HEIGHT;

// Draw Textures
// Draw Background
graphicsHolder.createVertexConsumer(RenderLayer.getText(TEXTURE_BACKGROUND));
RenderHelper.drawTexture(graphicsHolder, TEXTURE_BACKGROUND, 0, 0, 0, width, height, facing, ARGB_WHITE, MAX_RENDER_LIGHT);

Expand All @@ -40,104 +45,56 @@ public void render(PIDSBlockEntity be, GraphicsHolder graphicsHolder, World worl
drawAtlasBackground(graphicsHolder, width, height, facing);
}

graphicsHolder.translate(PIDS_MARGIN, 0, -0.5);
// Weather icon
drawWeatherIcon(graphicsHolder, world, facing, 0);
graphicsHolder.translate(startX, 0, -0.5);

List<DrawCall> drawCalls = new ArrayList<>();
drawCalls.add(new ClockDrawCall(getFont(), ARGB_WHITE, contentWidth, 2, contentWidth, 10));
drawCalls.add(new WeatherIconDrawCall(ICON_WEATHER_SUNNY, ICON_WEATHER_RAINY, ICON_WEATHER_THUNDER, 0, 0, 11, 11));

drawArrivals(arrivals, rowHidden, 0, 15, contentWidth, contentHeight, be.getRowAmount(), ARGB_BLACK, be.platformNumberHidden(), drawCalls);

List<DrawCall> textureDrawCall = drawCalls.stream().filter(e -> e instanceof TextureDrawCall).collect(Collectors.toList());
List<DrawCall> textDrawCall = drawCalls.stream().filter(e -> e instanceof TextDrawCall).collect(Collectors.toList());

// Texture
for(DrawCall drawCall : textureDrawCall) {
graphicsHolder.push();
drawCall.render(graphicsHolder, world, facing);
graphicsHolder.pop();
}

// Text
graphicsHolder.translate(0, 0, -0.5);
TextRenderingManager.bind(graphicsHolder);
drawClock(graphicsHolder, facing, world, contentWidth, 2, ARGB_WHITE);

int headerHeight = 11;
drawArrivals(graphicsHolder, arrivals, rowHidden, facing, 0, 15, contentWidth, height - headerHeight, rowAmount, ARGB_BLACK, hidePlatform);
for(DrawCall drawCall : textDrawCall) {
graphicsHolder.push();
drawCall.render(graphicsHolder, world, facing);
graphicsHolder.pop();
}
}

private void drawArrivals(GraphicsHolder graphicsHolder, ArrivalsResponse arrivals, boolean[] rowHidden, Direction facing, int x, int y, int width, int height, int rows, int textColor, boolean hidePlatform) {
graphicsHolder.push();
graphicsHolder.translate(x, y, 0);
graphicsHolder.scale(ARRIVAL_TEXT_SCALE, ARRIVAL_TEXT_SCALE, ARRIVAL_TEXT_SCALE);

private void drawArrivals(ArrivalsResponse arrivals, boolean[] rowHidden, int x, int y, int width, int height, int rows, int textColor, boolean hidePlatform, List<DrawCall> drawCalls) {
int arrivalIndex = 0;
double rowY = y;
for(int i = 0; i < rows; i++) {
if(arrivalIndex >= arrivals.getArrivals().size()) return;

if(!rowHidden[i]) {
ArrivalResponse arrival = arrivals.getArrivals().get(arrivalIndex);
drawArrival(graphicsHolder, facing, (int)(width / ARRIVAL_TEXT_SCALE), arrival, textColor, false, hidePlatform);
arrivalIndex++;
}

graphicsHolder.translate(0, (height / 7.25) * ARRIVAL_TEXT_SCALE, 0);
}
graphicsHolder.pop();
}
float destinationMaxWidth = !hidePlatform ? (65 * ARRIVAL_TEXT_SCALE) : (75 * ARRIVAL_TEXT_SCALE);
drawCalls.add(new DestinationDrawCall(arrival, getFont(), textColor, x, rowY, destinationMaxWidth, 10, ARRIVAL_TEXT_SCALE));

private void drawArrival(GraphicsHolder graphicsHolder, Direction facing, int width, ArrivalResponse arrival, int textColor, boolean showCar, boolean hidePlatform) {
String routeNo = arrival.getRouteNumber().isEmpty() ? "" : arrival.getRouteNumber() + " ";
String leftDestination = routeNo + cycleString(arrival.getDestination());
TextInfo destinationText = new TextInfo(leftDestination).withColor(textColor).withFont(getFont());
if(!hidePlatform) {
drawCalls.add(new PlatformDrawCall(arrival, getFont(), RenderHelper.ARGB_WHITE, 65 * ARRIVAL_TEXT_SCALE, rowY, 9, 9));
drawCalls.add(new PlatformCircleDrawCall(arrival, TEXTURE_PLATFORM_CIRCLE, 65 * ARRIVAL_TEXT_SCALE, rowY, 11, 11));
}

int destinationWidth = TextRenderingManager.getTextWidth(destinationText);
float destinationMaxWidth = !hidePlatform ? (44 * ARRIVAL_TEXT_SCALE) : (54 * ARRIVAL_TEXT_SCALE);

graphicsHolder.push();
if(destinationWidth > destinationMaxWidth) {
RenderHelper.scaleToFit(graphicsHolder, destinationWidth, destinationMaxWidth - 2, false);
// TODO: Make marquee an option in custom PIDS Preset
//drawPIDSScrollingText(graphicsHolder, facing, leftDestination, 0, 0, textColor, (int)destinationMaxWidth - 2);
}

drawPIDSText(graphicsHolder, TextAlignment.LEFT, facing, destinationText, 0, 0, textColor);
graphicsHolder.pop();

// Platform Indicator
if(!hidePlatform) {
graphicsHolder.push();
// Icon
graphicsHolder.createVertexConsumer(RenderLayer.getText(TEXTURE_PLATFORM_CIRCLE));
RenderHelper.drawTexture(graphicsHolder, 44 * ARRIVAL_TEXT_SCALE, 0, 0.3F, 9, 9, facing, arrival.getRouteColor() + ARGB_BLACK, MAX_RENDER_LIGHT);

// Text
graphicsHolder.translate((44 * ARRIVAL_TEXT_SCALE) + 4.5, 1.75, 0);

TextInfo platformText = new TextInfo(arrival.getPlatformName()).withFont(getFont());
int platformTextWidth = TextRenderingManager.getTextWidth(platformText);

TextRenderingManager.bind(graphicsHolder);
graphicsHolder.scale(0.75F, 0.75F, 1);
RenderHelper.scaleToFit(graphicsHolder, platformTextWidth, 8, true, 12);
drawPIDSText(graphicsHolder, TextAlignment.CENTER, facing, platformText, 0, 0, ARGB_WHITE);
graphicsHolder.pop();
}

// Draw ETA
long remTime = arrival.getArrival() - System.currentTimeMillis();
long remSec = remTime / 1000L;

if((int)remSec > 0) {
MutableText etaText = remSec > 60 ? TextUtil.translatable("gui.mtr.arrival_min", remSec / 60) : TextUtil.translatable("gui.mtr.arrival_sec", remSec % 60);
drawPIDSText(graphicsHolder, TextAlignment.RIGHT, facing, new TextInfo(etaText), width, 0, textColor);
}
}

private void drawClock(GraphicsHolder graphicsHolder, Direction facing, World world, int x, int y, int textColor) {
long timeNow = WorldHelper.getTimeOfDay(world) + 6000;
long hours = timeNow / 1000;
long minutes = Math.round((timeNow - (hours * 1000)) / 16.8);
String timeString = String.format("%02d:%02d", hours % 24, minutes % 60);
drawPIDSText(graphicsHolder, TextAlignment.RIGHT, facing, timeString, x, y, textColor);
}
drawCalls.add(new ETADrawCall(arrival, getFont(), textColor, width, rowY, 100, 20, ARRIVAL_TEXT_SCALE));
arrivalIndex++;
}

private void drawWeatherIcon(GraphicsHolder graphicsHolder, World world, Direction facing, float x) {
if(world.isRaining()) {
graphicsHolder.createVertexConsumer(RenderLayer.getText(ICON_WEATHER_RAINY));
} else if(world.isThundering()) {
graphicsHolder.createVertexConsumer(RenderLayer.getText(ICON_WEATHER_THUNDER));
} else {
graphicsHolder.createVertexConsumer(RenderLayer.getText(ICON_WEATHER_SUNNY));
rowY += (height / 5.25) * ARRIVAL_TEXT_SCALE;
}
RenderHelper.drawTexture(graphicsHolder, x, 0, 0, 11, 11, facing, ARGB_WHITE, MAX_RENDER_LIGHT);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lx862.jcm.mod.data.pids.preset.drawcalls;

import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextDrawCall;
import com.lx862.jcm.mod.render.TextOverflowMode;
import com.lx862.jcm.mod.render.text.TextAlignment;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mapping.mapper.WorldHelper;

public class ClockDrawCall extends TextDrawCall {
// TODO: (Maybe next version) Add 12/24h toggle
public ClockDrawCall(String font, int textColor, double x, double y, double width, double height) {
super(font, textColor, x, y, width, height);
}

@Override
public void render(GraphicsHolder graphicsHolder, World world, Direction facing) {
long timeNow = WorldHelper.getTimeOfDay(world) + 6000;
long hours = timeNow / 1000;
long minutes = Math.round((timeNow - (hours * 1000)) / 16.8);
String timeString = String.format("%02d:%02d", hours % 24, minutes % 60);
drawText(graphicsHolder, TextAlignment.RIGHT, facing, timeString, TextOverflowMode.STRETCH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.lx862.jcm.mod.data.pids.preset.drawcalls;

import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextDrawCall;
import com.lx862.jcm.mod.render.RenderHelper;
import com.lx862.jcm.mod.render.TextOverflowMode;
import com.lx862.jcm.mod.render.text.TextAlignment;
import com.lx862.jcm.mod.render.text.TextInfo;
import com.lx862.jcm.mod.render.text.TextRenderingManager;
import org.mtr.core.operation.ArrivalResponse;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;

public class DestinationDrawCall extends TextDrawCall {
private final ArrivalResponse arrival;
public DestinationDrawCall(ArrivalResponse arrival, String font, int textColor, double x, double y, double width, double height, double scale) {
super(font, textColor, x, y, width, height, scale);
this.arrival = arrival;
}

@Override
public void render(GraphicsHolder graphicsHolder, World world, Direction facing) {
String routeNo = arrival.getRouteNumber().isEmpty() ? "" : arrival.getRouteNumber() + " ";
String leftDestination = routeNo + arrival.getDestination();
TextInfo destinationText = new TextInfo(leftDestination).withColor(textColor).withFont(font);

int destinationWidth = TextRenderingManager.getTextWidth(destinationText);

graphicsHolder.push();
if(destinationWidth > width) {
RenderHelper.scaleToFit(graphicsHolder, destinationWidth, width, false);
// TODO: Make marquee an option in custom PIDS Preset
//drawPIDSScrollingText(graphicsHolder, facing, leftDestination, 0, 0, textColor, (int)destinationMaxWidth - 2);
}

drawText(graphicsHolder, TextAlignment.LEFT, facing, destinationText, TextOverflowMode.STRETCH);
graphicsHolder.pop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.lx862.jcm.mod.data.pids.preset.drawcalls;

import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextDrawCall;
import com.lx862.jcm.mod.render.TextOverflowMode;
import com.lx862.jcm.mod.render.text.TextAlignment;
import com.lx862.jcm.mod.render.text.TextInfo;
import com.lx862.jcm.mod.util.TextUtil;
import org.mtr.core.operation.ArrivalResponse;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.MutableText;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;

public class ETADrawCall extends TextDrawCall {
// TODO: (Maybe next version) Add absolute time?
private final ArrivalResponse arrival;
public ETADrawCall(ArrivalResponse arrival, String font, int textColor, double x, double y, double width, double height, double scale) {
super(font, textColor, x, y, width, height, scale);
this.arrival = arrival;
}

@Override
public void render(GraphicsHolder graphicsHolder, World world, Direction facing) {
long remTime = arrival.getArrival() - System.currentTimeMillis();
long remSec = remTime / 1000L;

if((int)remSec > 0) {
MutableText etaText = remSec > 60 ? TextUtil.translatable("gui.mtr.arrival_min", remSec / 60) : TextUtil.translatable("gui.mtr.arrival_sec", remSec % 60);
drawText(graphicsHolder, TextAlignment.RIGHT, facing, new TextInfo(etaText), TextOverflowMode.STRETCH);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lx862.jcm.mod.data.pids.preset.drawcalls;

import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextureDrawCall;
import org.mtr.core.operation.ArrivalResponse;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.Identifier;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;

public class PlatformCircleDrawCall extends TextureDrawCall {
private final ArrivalResponse arrival;
public PlatformCircleDrawCall(ArrivalResponse arrival, Identifier textureId, double x, double y, double width, double height) {
super(textureId, x, y, width, height);
this.arrival = arrival;
}

@Override
public void render(GraphicsHolder graphicsHolder, World world, Direction facing) {
drawTexture(graphicsHolder, facing, texture, 0, 0, width, height, arrival.getRouteColor() + ARGB_BLACK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lx862.jcm.mod.data.pids.preset.drawcalls;

import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextDrawCall;
import com.lx862.jcm.mod.render.RenderHelper;
import com.lx862.jcm.mod.render.TextOverflowMode;
import com.lx862.jcm.mod.render.text.TextAlignment;
import com.lx862.jcm.mod.render.text.TextInfo;
import com.lx862.jcm.mod.render.text.TextRenderingManager;
import org.mtr.core.operation.ArrivalResponse;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;

public class PlatformDrawCall extends TextDrawCall {
private final ArrivalResponse arrival;
public PlatformDrawCall(ArrivalResponse arrival, String font, int textColor, double x, double y, double width, double height) {
super(font, textColor, x, y, width, height, 1);
this.arrival = arrival;
}

@Override
public void render(GraphicsHolder graphicsHolder, World world, Direction facing) {
graphicsHolder.translate(width / 1.6, 2, 0);
drawText(graphicsHolder, TextAlignment.CENTER, facing, arrival.getPlatformName(), TextOverflowMode.SCALE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.lx862.jcm.mod.data.pids.preset.drawcalls;

import com.lx862.jcm.mod.data.pids.preset.drawcalls.base.TextureDrawCall;
import org.mtr.mapping.holder.Direction;
import org.mtr.mapping.holder.Identifier;
import org.mtr.mapping.holder.World;
import org.mtr.mapping.mapper.GraphicsHolder;

public class WeatherIconDrawCall extends TextureDrawCall {
private final Identifier iconSunny;
private final Identifier iconThunder;
private final Identifier iconRainy;
public WeatherIconDrawCall(Identifier iconSunny, Identifier iconRainy, Identifier iconThunder, double x, double y, double width, double height) {
super(null, x, y, width, height);
this.iconSunny = iconSunny;
this.iconRainy = iconRainy;
this.iconThunder = iconThunder;
}

@Override
public void render(GraphicsHolder graphicsHolder, World world, Direction facing) {
Identifier textureId;
if(world.isRaining()) {
textureId = iconRainy;
} else if(world.isThundering()) {
textureId = iconThunder;
} else {
textureId = iconSunny;
}
drawTexture(graphicsHolder, facing, textureId, 0, 0, width, height, ARGB_WHITE);
}
}
Loading

0 comments on commit 7766a5f

Please sign in to comment.