Search in sources :

Example 31 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.

the class AbstractGuiTextArea method mouseClick.

@Override
public boolean mouseClick(ReadablePoint position, int button) {
    if (getContainer() != null) {
        getContainer().convertFor(this, (Point) (position = new Point(position)));
    }
    boolean hovering = isMouseHovering(position);
    if (hovering && isFocused() && button == 0) {
        updateCurrentOffset();
        int mouseX = position.getX() - BORDER;
        int mouseY = position.getY() - BORDER;
        TextRenderer fontRenderer = MCVer.getFontRenderer();
        int textY = clamp(mouseY / (fontRenderer.fontHeight + LINE_SPACING) + currentYOffset, 0, text.length - 1);
        if (cursorY != textY) {
            currentXOffset = 0;
        }
        String line = text[textY].substring(currentXOffset);
        int textX = fontRenderer.trimToWidth(line, mouseX).length() + currentXOffset;
        setCursorPosition(textX, textY);
    }
    setFocused(hovering);
    return hovering;
}
Also used : Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) TextRenderer(net.minecraft.client.font.TextRenderer)

Example 32 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.

the class AbstractGuiTextArea method updateCurrentOffset.

private void updateCurrentOffset() {
    currentXOffset = Math.min(currentXOffset, cursorX);
    String line = text[cursorY].substring(currentXOffset, cursorX);
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int currentWidth = fontRenderer.getWidth(line);
    if (currentWidth > size.getWidth() - BORDER * 2) {
        currentXOffset = cursorX - fontRenderer.trimToWidth(line, size.getWidth() - BORDER * 2, true).length();
    }
    currentYOffset = Math.min(currentYOffset, cursorY);
    int lineHeight = MCVer.getFontRenderer().fontHeight + LINE_SPACING;
    int contentHeight = size.getHeight() - BORDER * 2;
    int maxLines = contentHeight / lineHeight;
    if (cursorY - currentYOffset >= maxLines) {
        currentYOffset = cursorY - maxLines + 1;
    }
}
Also used : Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) TextRenderer(net.minecraft.client.font.TextRenderer)

Example 33 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.

the class AbstractGuiLabel method draw.

@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
    super.draw(renderer, size, renderInfo);
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    // #if MC>=11600
    List<String> lines = fontRenderer.getTextHandler().wrapLines(new LiteralText(text), size.getWidth(), Style.EMPTY).stream().map(it -> it.visit(Optional::of)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    // #else
    // $$ @SuppressWarnings("unchecked")
    // $$ List<String> lines = fontRenderer.wrapStringToWidthAsList(text, size.getWidth());
    // #endif
    int y = 0;
    for (String line : lines) {
        renderer.drawString(0, y, isEnabled() ? color : disabledColor, line);
        y += fontRenderer.fontHeight;
    }
}
Also used : LiteralText(net.minecraft.text.LiteralText) ReadableColor(de.johni0702.minecraft.gui.utils.lwjgl.ReadableColor) I18n(net.minecraft.client.resource.language.I18n) GuiContainer(de.johni0702.minecraft.gui.container.GuiContainer) MCVer(de.johni0702.minecraft.gui.versions.MCVer) Dimension(de.johni0702.minecraft.gui.utils.lwjgl.Dimension) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) Collectors(java.util.stream.Collectors) List(java.util.List) Style(net.minecraft.text.Style) RenderInfo(de.johni0702.minecraft.gui.RenderInfo) Optional(java.util.Optional) TextRenderer(net.minecraft.client.font.TextRenderer) GuiRenderer(de.johni0702.minecraft.gui.GuiRenderer) Optional(java.util.Optional) TextRenderer(net.minecraft.client.font.TextRenderer) LiteralText(net.minecraft.text.LiteralText)

Example 34 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.

the class AbstractGuiTextField method updateCurrentOffset.

/**
 * Update current text offset to make sure the cursor is always visible.
 */
private void updateCurrentOffset() {
    currentOffset = Math.min(currentOffset, cursorPos);
    String line = text.substring(currentOffset, cursorPos);
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int currentWidth = fontRenderer.getWidth(line);
    if (currentWidth > size.getWidth() - 2 * BORDER) {
        currentOffset = cursorPos - fontRenderer.trimToWidth(line, size.getWidth() - 2 * BORDER, true).length();
    }
}
Also used : Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) TextRenderer(net.minecraft.client.font.TextRenderer)

Example 35 with TextRenderer

use of net.minecraft.client.font.TextRenderer in project jGui by ReplayMod.

the class AbstractGuiTextField method draw.

@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
    this.size = size;
    updateCurrentOffset();
    super.draw(renderer, size, renderInfo);
    int width = size.getWidth(), height = size.getHeight();
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int posY = height / 2 - fontRenderer.fontHeight / 2;
    // Draw black rect once pixel smaller than gray rect
    renderer.drawRect(0, 0, width, height, isFocused() ? ReadableColor.WHITE : BORDER_COLOR);
    renderer.drawRect(1, 1, width - 2, height - 2, ReadableColor.BLACK);
    if (text.isEmpty() && !isFocused() && !Strings.isNullOrEmpty(hint)) {
        // Draw hint
        String text = fontRenderer.trimToWidth(hint, width - 2 * BORDER);
        renderer.drawString(BORDER, posY, textColorDisabled, text);
    } else {
        // Draw text
        String renderText = text.substring(currentOffset);
        renderText = fontRenderer.trimToWidth(renderText, width - 2 * BORDER);
        ReadableColor color = isEnabled() ? textColorEnabled : textColorDisabled;
        int lineEnd = renderer.drawString(BORDER, height / 2 - fontRenderer.fontHeight / 2, color, renderText);
        // Draw selection
        int from = getSelectionFrom();
        int to = getSelectionTo();
        String leftStr = renderText.substring(0, clamp(from - currentOffset, 0, renderText.length()));
        String rightStr = renderText.substring(clamp(to - currentOffset, 0, renderText.length()));
        int left = BORDER + fontRenderer.getWidth(leftStr);
        int right = lineEnd - fontRenderer.getWidth(rightStr) - 1;
        renderer.invertColors(right, height - 2, left, 2);
        // Draw cursor
        if (blinkCursorTick / 6 % 2 == 0 && focused) {
            String beforeCursor = renderText.substring(0, cursorPos - currentOffset);
            int posX = BORDER + fontRenderer.getWidth(beforeCursor);
            if (cursorPos == text.length()) {
                renderer.drawString(posX, posY, CURSOR_COLOR, "_", true);
            } else {
                renderer.drawRect(posX, posY - 1, 1, 1 + fontRenderer.fontHeight, CURSOR_COLOR);
            }
        }
    }
}
Also used : ReadableColor(de.johni0702.minecraft.gui.utils.lwjgl.ReadableColor) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) TextRenderer(net.minecraft.client.font.TextRenderer)

Aggregations

TextRenderer (net.minecraft.client.font.TextRenderer)45 Point (de.johni0702.minecraft.gui.utils.lwjgl.Point)9 ReadablePoint (de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint)9 MinecraftClient (net.minecraft.client.MinecraftClient)7 LiteralText (net.minecraft.text.LiteralText)7 Dimension (de.johni0702.minecraft.gui.utils.lwjgl.Dimension)6 ReadableDimension (de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension)6 TranslatableText (net.minecraft.text.TranslatableText)5 ReadableColor (de.johni0702.minecraft.gui.utils.lwjgl.ReadableColor)3 GameRenderer (net.minecraft.client.render.GameRenderer)3 MatrixStack (net.minecraft.client.util.math.MatrixStack)3 Text (net.minecraft.text.Text)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Environment (net.fabricmc.api.Environment)2 InGameHud (net.minecraft.client.gui.hud.InGameHud)2 Screen (net.minecraft.client.gui.screen.Screen)2 BufferBuilder (net.minecraft.client.render.BufferBuilder)2 I18n (net.minecraft.client.resource.language.I18n)2 Sprite (net.minecraft.client.texture.Sprite)2