| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.gui.Font;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.input.PreeditEvent;
|
| 9 | import net.minecraft.client.renderer.RenderPipelines;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.network.chat.Style;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.util.Util;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class IMEPreeditOverlay implements Renderable {
|
| 17 | private static final Identifier BACKGROUND = Identifier.withDefaultNamespace("widget/preedit");
|
| 18 | private static final Style FOCUSED_STYLE = Style.EMPTY.withUnderlined(true);
|
| 19 | private static final int SEPARATION_FROM_INPUT = 4;
|
| 20 | private static final int BORDER_MARGIN = 4;
|
| 21 | private static final int BORDER_WIDTH = 1;
|
| 22 | private static final int BORDER_OFFSET = 5;
|
| 23 | private static final int TEXT_COLOR = -16777216;
|
| 24 | private final Font font;
|
| 25 | private int inputLeft;
|
| 26 | private int inputTop;
|
| 27 | private final int inputHeight;
|
| 28 | private final long initTimeMs;
|
| 29 | private final Component preEditText;
|
| 30 | private final int preEditTextWidth;
|
| 31 | private final int caretPos;
|
| 32 |
|
| 33 | public IMEPreeditOverlay(final PreeditEvent contents, final Font font, final int inputHeight) {
|
| 34 | this.font = font;
|
| 35 | this.inputHeight = inputHeight;
|
| 36 | this.initTimeMs = Util.getMillis();
|
| 37 | this.preEditText = contents.toFormattedText(FOCUSED_STYLE).withColor(-16777216);
|
| 38 | this.preEditTextWidth = font.width(this.preEditText);
|
| 39 | String textBeforeCaret = contents.fullText().substring(0, contents.caretPosition());
|
| 40 | this.caretPos = font.width(textBeforeCaret);
|
| 41 | }
|
| 42 |
|
| 43 | public void updateInputPosition(final int inputLeft, final int inputTop) {
|
| 44 | this.inputLeft = inputLeft;
|
| 45 | this.inputTop = inputTop;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 50 | int preeditLeft = this.inputLeft;
|
| 51 | int preeditRight = preeditLeft + this.preEditTextWidth;
|
| 52 | if (preeditRight > graphics.guiWidth()) {
|
| 53 | preeditLeft = graphics.guiWidth() - this.preEditTextWidth;
|
| 54 | preeditRight = preeditLeft + this.preEditTextWidth;
|
| 55 | }
|
| 56 |
|
| 57 | int inputBottom = this.inputTop + this.inputHeight;
|
| 58 | int preeditBottom = inputBottom + 4 + 9;
|
| 59 | if (preeditBottom > graphics.guiHeight()) {
|
| 60 | preeditBottom = this.inputTop - 4 - 9;
|
| 61 | }
|
| 62 |
|
| 63 | int preeditTop = preeditBottom - 9;
|
| 64 | Minecraft.getInstance().textInputManager().setTextInputArea(preeditLeft, preeditTop, preeditRight, preeditBottom);
|
| 65 | int backgroundWidth = preeditRight - preeditLeft + 10;
|
| 66 | int backgroundHeight = preeditBottom - preeditTop + 10;
|
| 67 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND, preeditLeft - 5, preeditTop - 5, backgroundWidth, backgroundHeight);
|
| 68 | graphics.text(this.font, this.preEditText, preeditLeft, preeditTop, -16777216, false);
|
| 69 | if (TextCursorUtils.isCursorVisible(Util.getMillis() - this.initTimeMs)) {
|
| 70 | TextCursorUtils.extractInsertCursor(graphics, preeditLeft + this.caretPos, preeditTop, -16777216, 9 + 1);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|