Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@
import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.TagParser;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import io.netty.buffer.ByteBuf;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.chat.Style;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;

import java.util.ArrayList;
import java.util.List;

public class TextBlockEntity extends GlowcaseBlockEntity {
public static final NodeParser PARSER = TagParser.DEFAULT;

public static final int PLATE_BACKGROUND = 0x44000000;

public List<Component> lines = new ArrayList<>();
public TextAlignment textAlignment = TextAlignment.CENTER;
public HorizontalAlignment horizontalAlignment = HorizontalAlignment.CENTER;
public ZOffset zOffset = ZOffset.CENTER;
public boolean shadow = true;
public float scale = 1F;
Expand All @@ -47,6 +51,7 @@ protected void saveAdditional(ValueOutput view) {
view.putInt("background_color", this.backgroundColor);

view.store("text_alignment", TextAlignment.CODEC, this.textAlignment);
view.store("horizontal_alignment", HorizontalAlignment.CODEC, this.horizontalAlignment);
view.store("z_offset", ZOffset.CODEC, this.zOffset);
view.putBoolean("shadow", this.shadow);
view.putFloat("viewDistance", this.viewDistance);
Expand All @@ -69,6 +74,7 @@ protected void loadAdditional(ValueInput view) {
this.backgroundColor = view.getIntOr("background_color", 0);
this.shadow = view.getBooleanOr("shadow", true);
this.textAlignment = view.read("text_alignment", TextAlignment.CODEC).orElse(TextAlignment.CENTER);
this.horizontalAlignment = view.read("horizontal_alignment", HorizontalAlignment.CODEC).orElse(HorizontalAlignment.CENTER);
this.zOffset = view.read("z_offset", ZOffset.CODEC).orElse(ZOffset.CENTER);
this.viewDistance = view.getFloatOr("viewDistance", -1);
this.lines = new ArrayList<>(view.read("lines", ComponentSerialization.CODEC.listOf()).orElseGet(List::of));
Expand Down Expand Up @@ -111,7 +117,13 @@ public void setRawLine(int i, String string) {
}

public enum TextAlignment implements StringRepresentable {
LEFT, CENTER, CENTER_LEFT, CENTER_RIGHT, RIGHT;
LEFT,
CENTER,
@Deprecated
CENTER_LEFT,
@Deprecated
CENTER_RIGHT,
RIGHT;

public static final Codec<TextAlignment> CODEC = StringRepresentable.fromEnum(TextAlignment::values);

Expand All @@ -131,4 +143,16 @@ public String getSerializedName() {
return name().toLowerCase();
}
}

public enum HorizontalAlignment implements StringRepresentable {
LEFT, CENTER, RIGHT;

public static final Codec<HorizontalAlignment> CODEC = StringRepresentable.fromEnum(HorizontalAlignment::values);
public static final StreamCodec<ByteBuf, HorizontalAlignment> STREAM_CODEC = ByteBufCodecs.BYTE.map(index -> TextBlockEntity.HorizontalAlignment.values()[index], textAlignment -> (byte) textAlignment.ordinal());

@Override
public String getSerializedName() {
return name().toLowerCase();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class GlowcaseClient implements ClientModInitializer {
public void onInitializeClient() {
Glowcase.proxy = new GlowcaseClientProxy();

BlockEntityRenderers.register(Glowcase.TEXT_BLOCK_ENTITY.get(), (ctx) -> new TextBlockEntityRenderer());
BlockEntityRenderers.register(Glowcase.TEXT_BLOCK_ENTITY.get(), (ctx) -> new TextBlockEntityRenderer(ctx));
BlockEntityRenderers.register(Glowcase.HYPERLINK_BLOCK_ENTITY.get(), HyperlinkBlockEntityRenderer::new);
BlockEntityRenderers.register(Glowcase.CONFIG_LINK_BLOCK_ENTITY.get(), ConfigLinkBlockEntityRenderer::new);
BlockEntityRenderers.register(Glowcase.ITEM_DISPLAY_BLOCK_ENTITY.get(), ItemDisplayBlockEntityRenderer::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ protected GlowcaseScreen() {
super(Component.empty());
}

protected GlowcaseScreen(Component title) {
super(title);
}

@Override
public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float deltaTicks) {
this.extractTransparentBackground(graphics);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package dev.hephaestus.glowcase.client.gui.screen.ingame;

import com.google.common.primitives.Floats;
import dev.hephaestus.glowcase.block.entity.TextBlockEntity;
import dev.hephaestus.glowcase.client.gui.widget.ingame.ColorPickerWidget;
import dev.hephaestus.glowcase.client.util.ColorUtil;
import dev.hephaestus.glowcase.packet.C2SEditTextBlock;
import eu.pb4.placeholders.api.parsers.tag.TagRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.AbstractSliderButton;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Checkbox;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.components.events.GuiEventListener;
Expand All @@ -30,22 +29,14 @@ public class TextBlockEditScreen extends TextEditorScreen {
private final TextBlockEntity textBlockEntity;

private List<EditBox> textWidgets;

private List<EditBox> colorListeners;

private TextFieldHelper selectionManager;
private EditBox colorEntryWidget;
private int currentRow;
private long ticksSinceOpened = 0;
private ColorPickerWidget colorPickerWidget;
private Button changeAlignment;
private EditBox colorEntryWidget;
private EditBox backgroundColorEntryWidget;
private Color colorEntryPreColorPicker; //used for color picker cancel button
private Button zOffsetToggle;
private Checkbox shadowToggle;

private EditBox viewDistanceField;
private Button viewDistanceHelpButton;

public TextBlockEditScreen(TextBlockEntity textBlockEntity) {
this.textBlockEntity = textBlockEntity;
Expand All @@ -67,42 +58,24 @@ public void init() {

int middle = width / 2;

Button decreaseSize = Button.builder(Component.literal("-"), action -> {
this.textBlockEntity.scale = Math.max(0, this.textBlockEntity.scale - (minecraft.hasShiftDown() ? 1F : 0.125F));
this.textBlockEntity.renderDirty = true;
}).bounds(middle - 130, 0, 20, 20).build();

Button increaseSize = Button.builder(Component.literal("+"), action -> {
this.textBlockEntity.scale += minecraft.hasShiftDown() ? 1F : 0.125F;
this.textBlockEntity.renderDirty = true;
}).bounds(middle - 110, 0, 20, 20).build();

this.changeAlignment = Button.builder(Component.translatableEscape("gui.glowcase.alignment", this.textBlockEntity.textAlignment), action -> {
switch (textBlockEntity.textAlignment) {
case LEFT -> textBlockEntity.textAlignment = TextBlockEntity.TextAlignment.CENTER;
case CENTER -> textBlockEntity.textAlignment = TextBlockEntity.TextAlignment.CENTER_LEFT;
case CENTER_LEFT -> textBlockEntity.textAlignment = TextBlockEntity.TextAlignment.CENTER_RIGHT;
case CENTER_RIGHT -> textBlockEntity.textAlignment = TextBlockEntity.TextAlignment.RIGHT;
case RIGHT -> textBlockEntity.textAlignment = TextBlockEntity.TextAlignment.LEFT;
}
this.textBlockEntity.renderDirty = true;

this.changeAlignment.setMessage(Component.translatableEscape("gui.glowcase.alignment", this.textBlockEntity.textAlignment));
}).bounds(middle - 90 + innerPadding, 0, 160, 20).build();
var scaleSlider = new TextScaleSliderWidget(textBlockEntity, middle - 203, 0, 113, 20);
this.addRenderableWidget(scaleSlider);

this.shadowToggle = Checkbox.builder(Component.translatable("gui.glowcase.shadow"), this.font)
.selected(this.textBlockEntity.shadow)
.onValueChange((widget, checked) -> {
this.textBlockEntity.shadow = checked;
this.textBlockEntity.renderDirty = true;
})
.pos(middle - 90 + innerPadding, 20 + innerPadding).build();
var moreOptionsButton = Button.builder(
Component.translatable("gui.glowcase.more"),
button -> {
var optionsScreen = new TextBlockOptionsScreen(this, textBlockEntity);
Minecraft.getInstance().setScreen(optionsScreen);
})
.bounds(middle + 124, 0, 80, 20)
.build();
this.addRenderableWidget(moreOptionsButton);

this.colorEntryWidget = new EditBox(this.minecraft.font, middle + 70 + innerPadding * 2, 0, 64, 20, Component.empty());
this.colorEntryWidget = new EditBox(this.minecraft.font, middle + 54, 0, 64, 20, Component.empty());
this.colorEntryWidget.setTooltip(Tooltip.create(Component.translatable("gui.glowcase.color")));
this.colorEntryWidget.setValue(ColorUtil.toAlphaHex(this.textBlockEntity.color));
this.colorEntryWidget.setResponder(string -> {
ColorUtil.parse(this.colorEntryWidget.getValue(), this.textBlockEntity.color).ifSuccess(newColor -> {
ColorUtil.parse(string, this.textBlockEntity.color).ifSuccess(newColor -> {
final int color = (Math.max(newColor >>> 24, 0x1A) << 24) | (newColor & ColorUtil.COLOR_MASK);

this.textBlockEntity.color = color;
Expand All @@ -114,70 +87,21 @@ public void init() {
});
});

this.backgroundColorEntryWidget = new EditBox(this.minecraft.font, middle + 136 + innerPadding * 2, 0, 64, 20, Component.empty());
this.backgroundColorEntryWidget.setTooltip(Tooltip.create(Component.translatable("gui.glowcase.background_color")));
this.backgroundColorEntryWidget.setValue(ColorUtil.toAlphaHex(this.textBlockEntity.backgroundColor));
this.backgroundColorEntryWidget.setResponder(string -> {
ColorUtil.parse(string, this.textBlockEntity.backgroundColor).ifSuccess(newColor -> {
this.textBlockEntity.backgroundColor = newColor;
if (this.colorEntryWidget.isFocused()) {
this.colorPickerWidget.setColor(new Color(newColor));
}
this.textBlockEntity.renderDirty = true;
});
});

this.zOffsetToggle = Button.builder(Component.literal(this.textBlockEntity.zOffset.name()), action -> {
switch (textBlockEntity.zOffset) {
case FRONT -> textBlockEntity.zOffset = TextBlockEntity.ZOffset.CENTER;
case CENTER -> textBlockEntity.zOffset = TextBlockEntity.ZOffset.BACK;
case BACK -> textBlockEntity.zOffset = TextBlockEntity.ZOffset.FRONT;
}
this.textBlockEntity.renderDirty = true;

this.zOffsetToggle.setMessage(Component.literal(this.textBlockEntity.zOffset.name()));
}).bounds(middle + 2, 20 + innerPadding, 72, 20).build();

this.colorPickerWidget = ColorPickerWidget.builder(this, 216, 10).size(182, 104).build();
this.colorPickerWidget.toggle(false); //start deactivated

this.viewDistanceField = new EditBox(this.minecraft.font, middle - 203, 20 + innerPadding, 83 + innerPadding, 20, Component.empty());
this.viewDistanceField.setValue(String.valueOf(this.textBlockEntity.viewDistance));
this.viewDistanceField.setResponder(s -> {
if (Floats.tryParse(s) instanceof Float parsed) {
this.textBlockEntity.viewDistance = parsed;
}
});
this.viewDistanceField.setTooltip(Tooltip.create(Component.translatable("gui.glowcase.screen.text_edit.view_distance")));
this.viewDistanceHelpButton = Button.builder(Component.literal("?"), action -> {
})
.bounds(middle - 115 + innerPadding + 5, 20 + innerPadding, 20, 20).build();
this.viewDistanceHelpButton.setTooltip(Tooltip.create(Component.translatable("gui.glowcase.screen.text_edit.view_distance")));

this.addRenderableWidget(colorPickerWidget);
this.addRenderableWidget(increaseSize);
this.addRenderableWidget(decreaseSize);
this.addRenderableWidget(this.changeAlignment);
this.addRenderableWidget(this.shadowToggle);
this.addRenderableWidget(this.zOffsetToggle);
this.addRenderableWidget(this.colorEntryWidget);
this.addRenderableWidget(this.backgroundColorEntryWidget);

this.addRenderableWidget(this.viewDistanceField);
this.addRenderableWidget(this.viewDistanceHelpButton);

this.textWidgets = List.of(
this.colorEntryWidget,
this.backgroundColorEntryWidget,
this.viewDistanceField
this.colorEntryWidget
);

this.colorListeners = List.of(
this.colorEntryWidget,
this.backgroundColorEntryWidget
this.colorEntryWidget
);

addFormattingButtons(middle + 70, 20, innerPadding, 20, 2);
addFormattingButtons(middle - 90 + 6, 0, 0, 20, 2);
}

@Override
Expand All @@ -204,14 +128,13 @@ public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mo
super.extractRenderState(graphics, mouseX, mouseY, delta);

graphics.pose().pushMatrix();
graphics.pose().translate(0, 40 + 2 * this.width / 100F);
graphics.pose().translate(0, 20 + 2 * this.width / 100F);
for (int i = 0; i < this.textBlockEntity.lines.size(); ++i) {
var text = this.currentRow == i ? Component.literal(this.textBlockEntity.getRawLine(i)) : this.textBlockEntity.lines.get(i);

int lineWidth = this.font.width(text);
switch (this.textBlockEntity.textAlignment) {
case LEFT ->
graphics.text(minecraft.font, text, this.width / 10, i * 12, this.textBlockEntity.color);
case LEFT -> graphics.text(minecraft.font, text, this.width / 10, i * 12, this.textBlockEntity.color);
case CENTER, CENTER_LEFT, CENTER_RIGHT ->
graphics.text(minecraft.font, text, this.width / 2 - lineWidth / 2, i * 12, this.textBlockEntity.color);
case RIGHT ->
Expand Down Expand Up @@ -255,7 +178,7 @@ public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mo
}

graphics.pose().popMatrix();
graphics.text(minecraft.font, Component.translatable("gui.glowcase.scale_value", this.textBlockEntity.scale), width / 2 - 203, 7, 0xFFFFFFFF);

colorPickerWidget.extractRenderState(graphics, mouseX, mouseY, delta);
}

Expand Down Expand Up @@ -509,4 +432,28 @@ public void toggleColorPicker(boolean active) {
TextFieldHelper getSelectionManager() {
return this.selectionManager;
}

public static class TextScaleSliderWidget extends AbstractSliderButton {
private static final float MIN_SCALE = 0.125F;
private static final float MAX_SCALE = 16;

private final TextBlockEntity entity;

public TextScaleSliderWidget(TextBlockEntity entity, int x, int y, int width, int height) {
var initialValue = (entity.scale - MIN_SCALE) / (MAX_SCALE - MIN_SCALE);
super(x, y, width, height, Component.translatable("gui.glowcase.scale_value", entity.scale), initialValue);
this.entity = entity;
}

@Override
protected void updateMessage() {
this.setMessage(Component.translatable("gui.glowcase.scale_value", entity.scale));
}

@Override
protected void applyValue() {
entity.scale = (float) Math.round(Mth.lerp(this.value, MIN_SCALE, MAX_SCALE) * 8F) / 8F;
entity.renderDirty = true;
}
}
}
Loading
Loading