| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.components.Button;
|
| 8 | import net.minecraft.client.gui.components.IMEPreeditOverlay;
|
| 9 | import net.minecraft.client.gui.components.TextCursorUtils;
|
| 10 | import net.minecraft.client.gui.font.TextFieldHelper;
|
| 11 | import net.minecraft.client.gui.screens.Screen;
|
| 12 | import net.minecraft.client.input.CharacterEvent;
|
| 13 | import net.minecraft.client.input.KeyEvent;
|
| 14 | import net.minecraft.client.input.PreeditEvent;
|
| 15 | import net.minecraft.client.multiplayer.ClientPacketListener;
|
| 16 | import net.minecraft.client.renderer.blockentity.AbstractSignRenderer;
|
| 17 | import net.minecraft.network.chat.CommonComponents;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.network.protocol.game.ServerboundSignUpdatePacket;
|
| 20 | import net.minecraft.util.ARGB;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import net.minecraft.world.level.block.SignBlock;
|
| 23 | import net.minecraft.world.level.block.entity.SignBlockEntity;
|
| 24 | import net.minecraft.world.level.block.entity.SignText;
|
| 25 | import net.minecraft.world.level.block.entity.SignTextSlot;
|
| 26 | import net.minecraft.world.level.block.entity.SignText.Mutable;
|
| 27 | import net.minecraft.world.level.block.state.properties.WoodType;
|
| 28 | import org.joml.Vector2f;
|
| 29 | import org.joml.Vector3fc;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | @Environment(EnvType.CLIENT)
|
| 33 | public abstract class AbstractSignEditScreen extends Screen {
|
| 34 | private static final int LINE_COUNT = 4;
|
| 35 | protected final SignBlockEntity sign;
|
| 36 | private final Mutable text = SignText.EMPTY.asMutable();
|
| 37 | private final String[] messages = new String[4];
|
| 38 | private final SignTextSlot slot;
|
| 39 | private final int textColor;
|
| 40 | protected final WoodType woodType;
|
| 41 | private long cursorBlinkStartTime;
|
| 42 | private int line;
|
| 43 | private final TextFieldHelper signField;
|
| 44 | private @Nullable IMEPreeditOverlay preeditOverlay;
|
| 45 | private final Vector2f cursorPosScratch = new Vector2f();
|
| 46 |
|
| 47 | public AbstractSignEditScreen(final SignBlockEntity sign, final SignTextSlot slot, final boolean shouldFilter) {
|
| 48 | this(sign, slot, shouldFilter, Component.translatable("sign.edit"));
|
| 49 | }
|
| 50 |
|
| 51 | public AbstractSignEditScreen(final SignBlockEntity sign, final SignTextSlot slot, final boolean shouldFilter, final Component title) {
|
| 52 | super(title);
|
| 53 | this.sign = sign;
|
| 54 | SignText currentText = sign.getText(slot);
|
| 55 | List<Component> currentLines = currentText.getMessages(shouldFilter);
|
| 56 | this.slot = slot;
|
| 57 | this.woodType = SignBlock.getWoodType(sign.getBlockState().getBlock());
|
| 58 | this.textColor = currentText.hasGlowingText() ? currentText.getColor().getTextColor() : AbstractSignRenderer.getDarkColor(currentText);
|
| 59 | this.text.setColor(currentText.getColor()).setTextGlowing(currentText.hasGlowingText());
|
| 60 |
|
| 61 | for (int i = 0; i < 4; i++) {
|
| 62 | String stringifiedLine = i < currentLines.size() ? currentLines.get(i).getString() : "";
|
| 63 | this.messages[i] = stringifiedLine;
|
| 64 | this.text.setLine(i, Component.literal(stringifiedLine));
|
| 65 | }
|
| 66 |
|
| 67 | this.signField = new TextFieldHelper(
|
| 68 | () -> this.messages[this.line],
|
| 69 | this::setMessage,
|
| 70 | TextFieldHelper.createClipboardGetter(this.minecraft),
|
| 71 | TextFieldHelper.createClipboardSetter(this.minecraft),
|
| 72 | s -> this.minecraft.font.width(s) <= sign.getMaxTextLineWidth()
|
| 73 | );
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected void init() {
|
| 78 | this.minecraft.textInputManager().startTextInput(this);
|
| 79 | this.cursorBlinkStartTime = Util.getMillis();
|
| 80 | this.addRenderableWidget(
|
| 81 | Button.builder(CommonComponents.GUI_DONE, var1 -> this.onDone()).bounds(this.width / 2 - 100, this.height / 4 + 144, 200, 20).build()
|
| 82 | );
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public void tick() {
|
| 87 | if (!this.isValid()) {
|
| 88 | this.onDone();
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private boolean isValid() {
|
| 93 | return this.minecraft.player != null && !this.sign.isRemoved() && !this.sign.playerIsTooFarAwayToEdit(this.minecraft.player.getUUID());
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public boolean keyPressed(final KeyEvent event) {
|
| 98 | if (event.isUp()) {
|
| 99 | this.line = this.line - 1 & 3;
|
| 100 | this.signField.setCursorToEnd();
|
| 101 | return true;
|
| 102 | } else if (event.isDown() || event.isConfirmation()) {
|
| 103 | this.line = this.line + 1 & 3;
|
| 104 | this.signField.setCursorToEnd();
|
| 105 | return true;
|
| 106 | } else {
|
| 107 | return this.signField.keyPressed(event) ? true : super.keyPressed(event);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public boolean isInputCaptured() {
|
| 113 | return true;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public boolean charTyped(final CharacterEvent event) {
|
| 118 | this.signField.charTyped(event);
|
| 119 | return true;
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public boolean preeditUpdated(final @Nullable PreeditEvent event) {
|
| 124 | this.preeditOverlay = event != null ? new IMEPreeditOverlay(event, this.font, this.sign.getTextLineHeight()) : null;
|
| 125 | return true;
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 130 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 131 | graphics.centeredText(this.font, this.title, this.width / 2, 40, -1);
|
| 132 | this.extractSign(graphics);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void onClose() {
|
| 137 | this.onDone();
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public void removed() {
|
| 142 | ClientPacketListener connection = this.minecraft.getConnection();
|
| 143 | if (connection != null) {
|
| 144 | connection.send(new ServerboundSignUpdatePacket(this.sign.getBlockPos(), List.of(this.messages), this.slot));
|
| 145 | }
|
| 146 |
|
| 147 | this.minecraft.textInputManager().stopTextInput(this);
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public boolean isPauseScreen() {
|
| 152 | return false;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public boolean isInGameUi() {
|
| 157 | return true;
|
| 158 | }
|
| 159 |
|
| 160 | protected abstract void extractSignBackground(GuiGraphicsExtractor graphics);
|
| 161 |
|
| 162 | protected abstract Vector3fc getSignTextScale();
|
| 163 |
|
| 164 | protected abstract float getSignYOffset();
|
| 165 |
|
| 166 | private void extractSign(final GuiGraphicsExtractor graphics) {
|
| 167 | graphics.pose().pushMatrix();
|
| 168 | float offsetX = this.width / 2.0F;
|
| 169 | float offsetY = this.getSignYOffset();
|
| 170 | graphics.pose().translate(offsetX, offsetY);
|
| 171 | graphics.pose().pushMatrix();
|
| 172 | this.extractSignBackground(graphics);
|
| 173 | graphics.pose().popMatrix();
|
| 174 | Vector3fc textScale = this.getSignTextScale();
|
| 175 | graphics.pose().scale(textScale.x(), textScale.y());
|
| 176 | this.cursorPosScratch.zero();
|
| 177 | this.extractSignText(graphics, this.cursorPosScratch);
|
| 178 | graphics.pose().popMatrix();
|
| 179 | this.cursorPosScratch.mul(textScale.x(), textScale.y()).add(offsetX, offsetY);
|
| 180 | if (this.preeditOverlay == null) {
|
| 181 | this.minecraft
|
| 182 | .textInputManager()
|
| 183 | .setTextInputArea(
|
| 184 | (int)this.cursorPosScratch.x, (int)this.cursorPosScratch.y, (int)this.cursorPosScratch.x + 1, (int)this.cursorPosScratch.y + this.sign.getTextLineHeight()
|
| 185 | );
|
| 186 | } else {
|
| 187 | this.preeditOverlay.updateInputPosition((int)this.cursorPosScratch.x, (int)this.cursorPosScratch.y);
|
| 188 | graphics.setPreeditOverlay(this.preeditOverlay);
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | private void extractSignText(final GuiGraphicsExtractor graphics, final Vector2f cursorPosOutput) {
|
| 193 | boolean showCursor = TextCursorUtils.isCursorVisible(Util.getMillis() - this.cursorBlinkStartTime);
|
| 194 | boolean needsValidCursorPos = true;
|
| 195 | int cursorPos = this.signField.getCursorPos();
|
| 196 | int selectionPos = this.signField.getSelectionPos();
|
| 197 | int signMidpoint = 4 * this.sign.getTextLineHeight() / 2;
|
| 198 | int cursorY = this.line * this.sign.getTextLineHeight() - signMidpoint;
|
| 199 |
|
| 200 | for (int i = 0; i < this.messages.length; i++) {
|
| 201 | String line = this.messages[i];
|
| 202 | if (this.font.isBidirectional()) {
|
| 203 | line = this.font.bidirectionalShaping(line);
|
| 204 | }
|
| 205 |
|
| 206 | int x1 = -this.font.width(line) / 2;
|
| 207 | graphics.text(this.font, line, x1, i * this.sign.getTextLineHeight() - signMidpoint, this.textColor, false);
|
| 208 | if (i == this.line && cursorPos >= 0) {
|
| 209 | if (!showCursor) {
|
| 210 | }
|
| 211 |
|
| 212 | int cursorPosition = this.font.width(line.substring(0, Math.clamp(cursorPos, 0, line.length())));
|
| 213 | int cursorX = cursorPosition - this.font.width(line) / 2;
|
| 214 | if (cursorPos >= line.length()) {
|
| 215 | if (showCursor) {
|
| 216 | TextCursorUtils.extractAppendCursor(graphics, this.font, cursorX, cursorY, this.textColor, false);
|
| 217 | }
|
| 218 |
|
| 219 | cursorPosOutput.set(cursorX, cursorY);
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | for (int i = 0; i < this.messages.length; i++) {
|
| 225 | String line = this.messages[i];
|
| 226 | if (i == this.line && cursorPos >= 0) {
|
| 227 | int cursorPosition = this.font.width(line.substring(0, Math.clamp(cursorPos, 0, line.length())));
|
| 228 | int cursorX = cursorPosition - this.font.width(line) / 2;
|
| 229 | if (cursorPos < line.length()) {
|
| 230 | if (showCursor) {
|
| 231 | TextCursorUtils.extractInsertCursor(graphics, cursorX, cursorY, ARGB.opaque(this.textColor), this.sign.getTextLineHeight());
|
| 232 | }
|
| 233 |
|
| 234 | cursorPosOutput.set(cursorX, cursorY);
|
| 235 | }
|
| 236 |
|
| 237 | if (selectionPos != cursorPos) {
|
| 238 | int startIndex = Math.min(cursorPos, selectionPos);
|
| 239 | int endIndex = Math.max(cursorPos, selectionPos);
|
| 240 | int startPosX = this.font.width(line.substring(0, startIndex)) - this.font.width(line) / 2;
|
| 241 | int endPosX = this.font.width(line.substring(0, endIndex)) - this.font.width(line) / 2;
|
| 242 | int fromX = Math.min(startPosX, endPosX);
|
| 243 | int toX = Math.max(startPosX, endPosX);
|
| 244 | graphics.textHighlight(fromX, cursorY, toX, cursorY + this.sign.getTextLineHeight(), true);
|
| 245 | }
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | private void setMessage(final String newMessage) {
|
| 251 | String currentMessage = this.messages[this.line];
|
| 252 | if (!currentMessage.equals(newMessage)) {
|
| 253 | this.messages[this.line] = newMessage;
|
| 254 | this.text.setLine(this.line, Component.literal(newMessage));
|
| 255 | this.sign.setText(this.text.asImmutable(), this.slot);
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | private void onDone() {
|
| 260 | this.minecraft.gui.setScreen(null);
|
| 261 | }
|
| 262 | }
|
| 263 |
|