Skip to content

Commit

Permalink
Merge pull request Haven-King#12 from Patbox/1.20
Browse files Browse the repository at this point in the history
Quick and dirty support for colored/formatted text
  • Loading branch information
LemmaEOF authored Aug 11, 2023
2 parents 9fb94be + 9d32400 commit 3127c34
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ base {
archivesName = project.archives_base_name
}

repositories {
maven {
url "https://maven.nucleoid.xyz/"
name "Nucleoid"
}
}

loom {
accessWidenerPath = file("src/main/resources/glowcase.accesswidener")
}
Expand All @@ -19,6 +26,8 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation include("eu.pb4:placeholder-api:2.1.2+1.20.1")

}

processResources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import dev.hephaestus.glowcase.Glowcase;
import dev.hephaestus.glowcase.client.render.block.entity.BakedBlockEntityRenderer.BakedBlockEntityRendererManager;

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.TextParserV1;
import net.minecraft.text.Style;
import org.jetbrains.annotations.Nullable;

import net.minecraft.block.BlockState;
Expand All @@ -24,6 +28,7 @@
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;

public class TextBlockEntity extends BlockEntity {
public static final NodeParser PARSER = TextParserV1.DEFAULT;
public List<MutableText> lines = new ArrayList<>();
public TextAlignment textAlignment = TextAlignment.CENTER;
public ZOffset zOffset = ZOffset.CENTER;
Expand Down Expand Up @@ -97,6 +102,41 @@ public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}

public String getRawLine(int i) {
var line = this.lines.get(i);

if (line.getStyle() == null) {
return line.getString();
}

var insert = line.getStyle().getInsertion();

if (insert == null) {
return line.getString();
}
return insert;
}

public void addRawLine(int i, String string) {
var parsed = PARSER.parseText(string, ParserContext.of());

if (parsed.getString().equals(string)) {
this.lines.add(i, Text.literal(string));
} else {
this.lines.add(i, Text.empty().append(parsed).setStyle(Style.EMPTY.withInsertion(string)));
}
}

public void setRawLine(int i, String string) {
var parsed = PARSER.parseText(string, ParserContext.of());

if (parsed.getString().equals(string)) {
this.lines.set(i, Text.literal(string));
} else {
this.lines.set(i, Text.empty().append(parsed).setStyle(Style.EMPTY.withInsertion(string)));
}
}

public enum TextAlignment {
LEFT, CENTER, RIGHT
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public void init() {
// }

this.selectionManager = new SelectionManager(
() -> this.textBlockEntity.lines.get(this.currentRow).getString(),
() -> this.textBlockEntity.getRawLine(this.currentRow),
(string) -> {
textBlockEntity.lines.set(this.currentRow, Text.literal(string));
textBlockEntity.setRawLine(this.currentRow, string);
this.textBlockEntity.renderDirty = true;
},
SelectionManager.makeClipboardGetter(this.client),
Expand Down Expand Up @@ -138,19 +138,21 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
context.getMatrices().push();
context.getMatrices().translate(0, 40 + 2 * this.width / 100F, 0);
for (int i = 0; i < this.textBlockEntity.lines.size(); ++i) {
int lineWidth = this.textRenderer.getWidth(this.textBlockEntity.lines.get(i));
var text = this.currentRow == i ? Text.literal(this.textBlockEntity.getRawLine(i)) : this.textBlockEntity.lines.get(i);

int lineWidth = this.textRenderer.getWidth(text);
switch (this.textBlockEntity.textAlignment) {
case LEFT -> context.drawTextWithShadow(client.textRenderer, this.textBlockEntity.lines.get(i), this.width / 10, i * 12, this.textBlockEntity.color);
case CENTER -> context.drawTextWithShadow(client.textRenderer, this.textBlockEntity.lines.get(i), this.width / 2 - lineWidth / 2, i * 12, this.textBlockEntity.color);
case RIGHT -> context.drawTextWithShadow(client.textRenderer, this.textBlockEntity.lines.get(i), this.width - this.width / 10 - lineWidth, i * 12, this.textBlockEntity.color);
case LEFT -> context.drawTextWithShadow(client.textRenderer, text, this.width / 10, i * 12, this.textBlockEntity.color);
case CENTER -> context.drawTextWithShadow(client.textRenderer, text, this.width / 2 - lineWidth / 2, i * 12, this.textBlockEntity.color);
case RIGHT -> context.drawTextWithShadow(client.textRenderer, text, this.width - this.width / 10 - lineWidth, i * 12, this.textBlockEntity.color);
}
}

int caretStart = this.selectionManager.getSelectionStart();
int caretEnd = this.selectionManager.getSelectionEnd();

if (caretStart >= 0) {
String line = this.textBlockEntity.lines.get(this.currentRow).getString();
String line = this.textBlockEntity.getRawLine(this.currentRow);
int selectionStart = MathHelper.clamp(Math.min(caretStart, caretEnd), 0, line.length());
int selectionEnd = MathHelper.clamp(Math.max(caretStart, caretEnd), 0, line.length());

Expand Down Expand Up @@ -224,12 +226,12 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
} else {
this.focusOn(null);
if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) {
this.textBlockEntity.lines.add(this.currentRow + 1, Text.literal(
this.textBlockEntity.lines.get(this.currentRow).getString().substring(
MathHelper.clamp(this.selectionManager.getSelectionStart(), 0, this.textBlockEntity.lines.get(this.currentRow).getString().length()))
this.textBlockEntity.addRawLine(this.currentRow + 1,
this.textBlockEntity.getRawLine(this.currentRow).substring(
MathHelper.clamp(this.selectionManager.getSelectionStart(), 0, this.textBlockEntity.getRawLine(this.currentRow).length())
));
this.textBlockEntity.lines.set(this.currentRow, Text.literal(
this.textBlockEntity.lines.get(this.currentRow).getString().substring(0, MathHelper.clamp(this.selectionManager.getSelectionStart(), 0, this.textBlockEntity.lines.get(this.currentRow).getString().length()))
this.textBlockEntity.setRawLine(this.currentRow,
this.textBlockEntity.getRawLine(this.currentRow).substring(0, MathHelper.clamp(this.selectionManager.getSelectionStart(), 0, this.textBlockEntity.getRawLine(this.currentRow).length())
));
this.textBlockEntity.renderDirty = true;
++this.currentRow;
Expand All @@ -248,7 +250,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
this.selectionManager.moveCursorToEnd(false);
deleteLine();
return true;
} else if (keyCode == GLFW.GLFW_KEY_DELETE && this.currentRow < this.textBlockEntity.lines.size() - 1 && this.selectionManager.getSelectionEnd() == this.textBlockEntity.lines.get(this.currentRow).getString().length()) {
} else if (keyCode == GLFW.GLFW_KEY_DELETE && this.currentRow < this.textBlockEntity.lines.size() - 1 && this.selectionManager.getSelectionEnd() == this.textBlockEntity.getRawLine(this.currentRow).length()) {
deleteLine();
return true;
} else {
Expand All @@ -264,8 +266,8 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
}

private void deleteLine() {
this.textBlockEntity.lines.set(this.currentRow,
this.textBlockEntity.lines.get(this.currentRow).append(this.textBlockEntity.lines.get(this.currentRow + 1))
this.textBlockEntity.setRawLine(this.currentRow,
this.textBlockEntity.getRawLine(this.currentRow) + this.textBlockEntity.getRawLine(this.currentRow + 1)
);

this.textBlockEntity.lines.remove(this.currentRow + 1);
Expand All @@ -281,7 +283,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (mouseY > topOffset) {
this.currentRow = MathHelper.clamp((int) (mouseY - topOffset) / 12, 0, this.textBlockEntity.lines.size() - 1);
this.setFocused(null);
String baseContents = this.textBlockEntity.lines.get(currentRow).getString();
String baseContents = this.textBlockEntity.getRawLine(currentRow);
int baseContentsWidth = this.textRenderer.getWidth(baseContents);
int contentsStart;
int contentsEnd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,7 @@ public void renderBaked(TextBlockEntity blockEntity, MatrixStack matrices, Verte
matrices.translate(0, 0, 0.025D);
}

if (blockEntity.shadowType == TextBlockEntity.ShadowType.DROP) {
// Don't use the vanilla shadow rendering - it breaks when you try to use it in 3D
int shadowColor = 0x88000000;
matrices.translate(0, 0, -0.025D);
textRenderer.draw(blockEntity.lines.get(i), 1, (i * 12) + 1, shadowColor, false, matrices.peek().getPositionMatrix(), vertexConsumers, TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
matrices.translate(0, 0, 0.025D);
}

textRenderer.draw(blockEntity.lines.get(i), 0, i * 12, blockEntity.color, false, matrices.peek().getPositionMatrix(), vertexConsumers, TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
textRenderer.draw(blockEntity.lines.get(i), 0, i * 12, blockEntity.color, blockEntity.shadowType == TextBlockEntity.ShadowType.DROP, matrices.peek().getPositionMatrix(), vertexConsumers, TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);

matrices.pop();
}
Expand Down

0 comments on commit 3127c34

Please sign in to comment.