Skip to content

Commit

Permalink
Add KCR Station Name Sign Rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Mar 19, 2024
1 parent 46381c7 commit acb90fd
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 24 deletions.
6 changes: 0 additions & 6 deletions fabric/src/main/java/com/lx862/jcm/mod/Constants.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package com.lx862.jcm.mod;

import org.mtr.mapping.holder.Identifier;

public class Constants {
public static final String MOD_NAME = "Joban Client Mod";
public static final String MOD_ID = "jsblock";
public static final String LOGGING_PREFIX = "[JCM] ";
public static final String MOD_VERSION = "2.0.0";
public static final int MC_TICK_PER_SECOND = 20;

public static Identifier id(String path) {
return new Identifier(MOD_ID, path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class JsonPIDSPreset extends PIDSPresetBase {
private static final int PIDS_MARGIN = 7;
private static final float ARRIVAL_TEXT_SCALE = 1.35F;
private static final int HEADER_HEIGHT = 9;
private static final Identifier ICON_WEATHER_SUNNY = Constants.id("textures/block/pids/weather_sunny.png");
private static final Identifier ICON_WEATHER_RAINY = Constants.id("textures/block/pids/weather_rainy.png");
private static final Identifier ICON_WEATHER_THUNDER = Constants.id("textures/block/pids/weather_thunder.png");
private static final Identifier TEXTURE_PLATFORM_CIRCLE = Constants.id("textures/block/pids/plat_circle.png");
private static final Identifier ICON_WEATHER_SUNNY = new Identifier(Constants.MOD_ID, "textures/block/pids/weather_sunny.png");
private static final Identifier ICON_WEATHER_RAINY = new Identifier(Constants.MOD_ID, "textures/block/pids/weather_rainy.png");
private static final Identifier ICON_WEATHER_THUNDER = new Identifier(Constants.MOD_ID, "textures/block/pids/weather_thunder.png");
private static final Identifier TEXTURE_PLATFORM_CIRCLE = new Identifier(Constants.MOD_ID, "textures/block/pids/plat_circle.png");
private final Identifier background;
private final String fontId;
private final TextOverflowMode textOverflowMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public String getFont() {

@Override
public Identifier getBackground() {
return Constants.id("textures/block/pids/black.png");
return new Identifier(Constants.MOD_ID, "textures/block/pids/black.png");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public class RVPIDSPreset extends PIDSPresetBase {
private static final int PIDS_MARGIN = 7;
private static final float ARRIVAL_TEXT_SCALE = 1.35F;
private static final int TEXT_COLOR = ARGB_BLACK;
private static final Identifier TEXTURE_PLATFORM_CIRCLE = Constants.id("textures/block/pids/plat_circle.png");
private static final Identifier TEXTURE_BACKGROUND = Constants.id("textures/block/pids/rv_default.png");
private static final Identifier ICON_WEATHER_SUNNY = Constants.id("textures/block/pids/weather_sunny.png");
private static final Identifier ICON_WEATHER_RAINY = Constants.id("textures/block/pids/weather_rainy.png");
private static final Identifier ICON_WEATHER_THUNDER = Constants.id("textures/block/pids/weather_thunder.png");
private static final Identifier TEXTURE_PLATFORM_CIRCLE = new Identifier(Constants.MOD_ID, "textures/block/pids/plat_circle.png");
private static final Identifier TEXTURE_BACKGROUND = new Identifier(Constants.MOD_ID, "textures/block/pids/rv_default.png");
private static final Identifier ICON_WEATHER_SUNNY = new Identifier(Constants.MOD_ID, "textures/block/pids/weather_sunny.png");
private static final Identifier ICON_WEATHER_RAINY = new Identifier(Constants.MOD_ID, "textures/block/pids/weather_rainy.png");
private static final Identifier ICON_WEATHER_THUNDER = new Identifier(Constants.MOD_ID, "textures/block/pids/weather_thunder.png");
public RVPIDSPreset() {
super("rv_pids", "Hong Kong Railway Vision PIDS", true);
}
Expand Down
109 changes: 109 additions & 0 deletions fabric/src/main/java/com/lx862/jcm/mod/render/IDrawingJoban.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.lx862.jcm.mod.render;

import com.lx862.jcm.mod.config.ConfigEntry;
import org.mtr.libraries.it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import org.mtr.libraries.it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.mtr.mapping.holder.*;
import org.mtr.mapping.mapper.*;
import org.mtr.mod.data.IGui;

import javax.annotation.Nullable;

/**
* Copied from MTR's IDrawing, but just for rendering with a specific font other than the hardcoded mtr one
*/
public interface IDrawingJoban {
static void drawStringWithFont(GraphicsHolder graphicsHolder, String text, Identifier font, float x, float y, int light) {
drawStringWithFont(graphicsHolder, text, font, IGui.HorizontalAlignment.CENTER, IGui.VerticalAlignment.CENTER, x, y, -1, -1, 1, IGui.ARGB_WHITE, true, light, null);
}

static void drawStringWithFont(GraphicsHolder graphicsHolder, String text, Identifier font, IGui.HorizontalAlignment horizontalAlignment, IGui.VerticalAlignment verticalAlignment, float x, float y, float maxWidth, float maxHeight, float scale, int textColor, boolean shadow, int light, @Nullable DrawingCallback drawingCallback) {
drawStringWithFont(graphicsHolder, text, font, horizontalAlignment, verticalAlignment, horizontalAlignment, x, y, maxWidth, maxHeight, scale, textColor, shadow, light, drawingCallback);
}

static void drawStringWithFont(GraphicsHolder graphicsHolder, String text, Identifier font, IGui.HorizontalAlignment horizontalAlignment, IGui.VerticalAlignment verticalAlignment, IGui.HorizontalAlignment xAlignment, float x, float y, float maxWidth, float maxHeight, float scale, int textColor, boolean shadow, int light, @Nullable DrawingCallback drawingCallback) {
drawStringWithFont(graphicsHolder, text, font, horizontalAlignment, verticalAlignment, xAlignment, x, y, maxWidth, maxHeight, scale, textColor, textColor, 2, shadow, light, drawingCallback);
}

static void drawStringWithFont(GraphicsHolder graphicsHolder, String text, Identifier font, IGui.HorizontalAlignment horizontalAlignment, IGui.VerticalAlignment verticalAlignment, IGui.HorizontalAlignment xAlignment, float x, float y, float maxWidth, float maxHeight, float scale, int textColorCjk, int textColor, float fontSizeRatio, boolean shadow, int light, @Nullable DrawingCallback drawingCallback) {
final Style style = ConfigEntry.USE_CUSTOM_FONT.getBool() ? Style.getEmptyMapped().withFont(font) : Style.getEmptyMapped();

while (text.contains("||")) {
text = text.replace("||", "|");
}
final String[] stringSplit = text.split("\\|");

final BooleanArrayList isCJKList = new BooleanArrayList();
final ObjectArrayList<OrderedText> orderedTexts = new ObjectArrayList<>();
int totalHeight = 0, totalWidth = 0;
for (final String stringSplitPart : stringSplit) {
final boolean isCJK = IGui.isCjk(stringSplitPart);
isCJKList.add(isCJK);

final OrderedText orderedText = TextHelper.mutableTextToOrderedText(TextHelper.setStyle(TextHelper.literal(stringSplitPart), style));
orderedTexts.add(orderedText);

totalHeight += Math.round(IGui.LINE_HEIGHT * (isCJK ? fontSizeRatio : 1));
final int width = (int) Math.ceil(GraphicsHolder.getTextWidth(orderedText) * (isCJK ? fontSizeRatio : 1));
if (width > totalWidth) {
totalWidth = width;
}
}

if (maxHeight >= 0 && totalHeight / scale > maxHeight) {
scale = totalHeight / maxHeight;
}

graphicsHolder.push();

final float totalWidthScaled;
final float scaleX;
if (maxWidth >= 0 && totalWidth > maxWidth * scale) {
totalWidthScaled = maxWidth * scale;
scaleX = totalWidth / maxWidth;
} else {
totalWidthScaled = totalWidth;
scaleX = scale;
}
graphicsHolder.scale(1 / scaleX, 1 / scale, 1 / scale);

float offset = verticalAlignment.getOffset(y * scale, totalHeight);
for (int i = 0; i < orderedTexts.size(); i++) {
final boolean isCJK = isCJKList.getBoolean(i);
final float extraScale = isCJK ? fontSizeRatio : 1;
if (isCJK) {
graphicsHolder.push();
graphicsHolder.scale(extraScale, extraScale, 1);
}

final float xOffset = horizontalAlignment.getOffset(xAlignment.getOffset(x * scaleX, totalWidth), GraphicsHolder.getTextWidth(orderedTexts.get(i)) * extraScale - totalWidth);

final float shade = light == GraphicsHolder.getDefaultLight() ? 1 : Math.min(LightmapTextureManager.getBlockLightCoordinates(light) / 16F * 0.1F + 0.7F, 1);
final int a = ((isCJK ? textColorCjk : textColor) >> 24) & 0xFF;
final int r = (int) ((((isCJK ? textColorCjk : textColor) >> 16) & 0xFF) * shade);
final int g = (int) ((((isCJK ? textColorCjk : textColor) >> 8) & 0xFF) * shade);
final int b = (int) (((isCJK ? textColorCjk : textColor) & 0xFF) * shade);

graphicsHolder.drawText(orderedTexts.get(i), Math.round(xOffset / extraScale), Math.round(offset / extraScale), (a << 24) + (r << 16) + (g << 8) + b, shadow, light);

if (isCJK) {
graphicsHolder.pop();
}

offset += IGui.LINE_HEIGHT * extraScale;
}

graphicsHolder.pop();

if (drawingCallback != null) {
final float x1 = xAlignment.getOffset(x, totalWidthScaled / scale);
final float y1 = verticalAlignment.getOffset(y, totalHeight / scale);
drawingCallback.drawingCallback(x1, y1, x1 + totalWidthScaled / scale, y1 + totalHeight / scale);
}
}

@FunctionalInterface
interface DrawingCallback {
void drawingCallback(float x1, float y1, float x2, float y2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.List;

public class ButterflyLightRenderer extends JCMBlockEntityRenderer<ButterflyLightBlockEntity> {
private static final Identifier BUTTERFLY_LIGHT_TEXTURE = Constants.id("textures/block/butterfly_light_dotmatrix.png");
private static final Identifier BUTTERFLY_LIGHT_TEXTURE = new Identifier(Constants.MOD_ID, "textures/block/butterfly_light_dotmatrix.png");
public ButterflyLightRenderer(Argument dispatcher) {
super(dispatcher);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.lx862.jcm.mod.render.block;

import com.lx862.jcm.mod.block.entity.KCRStationNameSignBlockEntity;
import org.mtr.mapping.holder.BlockPos;
import org.mtr.mapping.holder.BlockState;
import org.mtr.mapping.holder.World;
import com.lx862.jcm.mod.data.BlockProperties;
import com.lx862.jcm.mod.render.IDrawingJoban;
import com.lx862.jcm.mod.util.BlockUtil;
import com.lx862.jcm.mod.util.TextUtil;
import org.mtr.core.data.ClientData;
import org.mtr.core.data.Station;
import org.mtr.mapping.holder.*;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mod.Init;
import org.mtr.mod.InitClient;
import org.mtr.mod.data.IGui;

public class KCRStationNameSignRenderer extends JCMBlockEntityRenderer<KCRStationNameSignBlockEntity> {
public KCRStationNameSignRenderer(Argument dispatcher) {
Expand All @@ -13,11 +20,24 @@ public KCRStationNameSignRenderer(Argument dispatcher) {

@Override
public void renderCurated(KCRStationNameSignBlockEntity blockEntity, GraphicsHolder graphicsHolder, World world, BlockState state, BlockPos pos, float tickDelta, int light, int i1) {
graphicsHolder.push();
graphicsHolder.translate(0.5, 0.5, 0.5);
graphicsHolder.scale(0.018F, 0.018F, 0.018F);
rotateToBlockDirection(graphicsHolder, blockEntity);
// TODO: Render with IDrawing
graphicsHolder.pop();
graphicsHolder.rotateZDegrees(180);
graphicsHolder.rotateYDegrees(180);

final Station station = InitClient.findStation(blockEntity.getPos2());
final String stationName = station == null ? TextUtil.translatable("gui.mtr.untitled").getString() : station.getName();
double offsetForExitDirection = BlockUtil.getProperty(state, BlockProperties.EXIT_ON_LEFT) ? -0.225 : 0.225;

// Draw both sides
for(int i = 0; i < 2; i++) {
graphicsHolder.push();
graphicsHolder.translate(offsetForExitDirection, 0, 0);
if(i == 1) graphicsHolder.rotateYDegrees(180); // Other side
graphicsHolder.translate(0, -0.05, -0.175);
graphicsHolder.scale(0.021F, 0.021F, 0.021F);
IDrawingJoban.drawStringWithFont(graphicsHolder, stationName, new Identifier("jsblock:kcr_sign"), IGui.HorizontalAlignment.CENTER, IGui.VerticalAlignment.CENTER, 0, 0, 60, 32, 1, 0xEEEEEE, false, GraphicsHolder.getDefaultLight(), null);
graphicsHolder.pop();
}
}
}

0 comments on commit acb90fd

Please sign in to comment.