Search in sources :

Example 6 with TextRenderer

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

the class AbstractGuiTextArea method draw.

@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
    this.size = size;
    updateCurrentOffset();
    super.draw(renderer, size, renderInfo);
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int width = size.getWidth();
    int height = size.getHeight();
    // Draw black rect once pixel smaller than gray rect
    renderer.drawRect(0, 0, width, height, BACKGROUND_COLOR);
    renderer.drawRect(1, 1, width - 2, height - 2, ReadableColor.BLACK);
    ReadableColor textColor = isEnabled() ? textColorEnabled : textColorDisabled;
    int lineHeight = fontRenderer.fontHeight + LINE_SPACING;
    int contentHeight = height - BORDER * 2;
    int maxLines = contentHeight / lineHeight;
    int contentWidth = width - BORDER * 2;
    // Draw hint if applicable
    if (hint != null && !isFocused() && Arrays.stream(text).allMatch(String::isEmpty)) {
        for (int i = 0; i < maxLines && i < hint.length; i++) {
            String line = fontRenderer.trimToWidth(hint[i], contentWidth);
            int posY = BORDER + i * lineHeight;
            renderer.drawString(BORDER, posY, textColorDisabled, line, true);
        }
        return;
    }
    // Draw lines
    for (int i = 0; i < maxLines && i + currentYOffset < text.length; i++) {
        int lineY = i + currentYOffset;
        String line = text[lineY];
        int leftTrimmed = 0;
        if (lineY == cursorY) {
            line = line.substring(currentXOffset);
            leftTrimmed = currentXOffset;
        }
        line = fontRenderer.trimToWidth(line, contentWidth);
        // Draw line
        int posY = BORDER + i * lineHeight;
        int lineEnd = renderer.drawString(BORDER, posY, textColor, line, true);
        // Draw selection
        int fromX = getSelectionFromX();
        int fromY = getSelectionFromY();
        int toX = getSelectionToX();
        int toY = getSelectionToY();
        if (lineY > fromY && lineY < toY) {
            // Whole line selected
            renderer.invertColors(lineEnd, posY - 1 + lineHeight, BORDER, posY - 1);
        } else if (lineY == fromY && lineY == toY) {
            // Part of line selected
            String leftStr = line.substring(0, clamp(fromX - leftTrimmed, 0, line.length()));
            String rightStr = line.substring(clamp(toX - leftTrimmed, 0, line.length()));
            int left = BORDER + fontRenderer.getWidth(leftStr);
            int right = lineEnd - fontRenderer.getWidth(rightStr) - 1;
            renderer.invertColors(right, posY - 1 + lineHeight, left, posY - 1);
        } else if (lineY == fromY) {
            // End of line selected
            String rightStr = line.substring(clamp(fromX - leftTrimmed, 0, line.length()));
            renderer.invertColors(lineEnd, posY - 1 + lineHeight, lineEnd - fontRenderer.getWidth(rightStr), posY - 1);
        } else if (lineY == toY) {
            // Beginning of line selected
            String leftStr = line.substring(0, clamp(toX - leftTrimmed, 0, line.length()));
            int right = BORDER + fontRenderer.getWidth(leftStr);
            renderer.invertColors(right, posY - 1 + lineHeight, BORDER, posY - 1);
        }
        // Draw cursor
        if (lineY == cursorY && blinkCursorTick / 6 % 2 == 0 && focused) {
            String beforeCursor = line.substring(0, cursorX - leftTrimmed);
            int posX = BORDER + fontRenderer.getWidth(beforeCursor);
            if (cursorX == text[lineY].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)

Example 7 with TextRenderer

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

the class AbstractGuiTextField 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;
        TextRenderer fontRenderer = MCVer.getFontRenderer();
        String text = this.text.substring(currentOffset);
        int textX = fontRenderer.trimToWidth(text, mouseX).length() + currentOffset;
        setCursorPosition(textX);
        return true;
    }
    setFocused(hovering);
    // Do not yet return true to allow focusables later in the event chain to be notified of the focus change
    return false;
}
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 8 with TextRenderer

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

the class AbstractGuiTooltip method draw.

@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
    super.draw(renderer, size, renderInfo);
    int width = size.getWidth();
    int height = size.getHeight();
    // Draw background
    // Top to bottom
    renderer.drawRect(1, 0, width - 2, height, BACKGROUND_COLOR);
    // Left pixel row
    renderer.drawRect(0, 1, 1, height - 2, BACKGROUND_COLOR);
    // Right pixel row
    renderer.drawRect(width - 1, 1, 1, height - 2, BACKGROUND_COLOR);
    // Draw the border, it gets darker from top to bottom
    // Top border
    renderer.drawRect(1, 1, width - 2, 1, BORDER_LIGHT);
    // Bottom border
    renderer.drawRect(1, height - 2, width - 2, 1, BORDER_DARK);
    // Left border
    renderer.drawRect(1, 2, 1, height - 4, BORDER_LIGHT, BORDER_LIGHT, BORDER_DARK, BORDER_DARK);
    // Right border
    renderer.drawRect(width - 2, 2, 1, height - 4, BORDER_LIGHT, BORDER_LIGHT, BORDER_DARK, BORDER_DARK);
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int y = LINE_SPACING + 1;
    for (String line : text) {
        renderer.drawString(LINE_SPACING + 1, y, color, line, true);
        y += fontRenderer.fontHeight + LINE_SPACING;
    }
}
Also used : TextRenderer(net.minecraft.client.font.TextRenderer)

Example 9 with TextRenderer

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

the class AbstractGuiTooltip method calcMinSize.

@Override
public ReadableDimension calcMinSize() {
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int height = 1 + LINE_SPACING + text.length * (fontRenderer.fontHeight + LINE_SPACING);
    int width = 0;
    for (String line : text) {
        int w = fontRenderer.getWidth(line);
        if (w > width) {
            width = w;
        }
    }
    width += 4 * 2;
    return new Dimension(width, height);
}
Also used : Dimension(de.johni0702.minecraft.gui.utils.lwjgl.Dimension) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) TextRenderer(net.minecraft.client.font.TextRenderer)

Example 10 with TextRenderer

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

the class AbstractGuiCheckbox method calcMinSize.

@Override
public ReadableDimension calcMinSize() {
    TextRenderer fontRenderer = MCVer.getFontRenderer();
    int height = fontRenderer.fontHeight + 2;
    int width = height + 2 + fontRenderer.getWidth(label);
    return new Dimension(width, height);
}
Also used : Dimension(de.johni0702.minecraft.gui.utils.lwjgl.Dimension) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) 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