Example usage for net.minecraftforge.client MinecraftForgeClient reserveStencilBit

List of usage examples for net.minecraftforge.client MinecraftForgeClient reserveStencilBit

Introduction

In this page you can find the example usage for net.minecraftforge.client MinecraftForgeClient reserveStencilBit.

Prototype

public static int reserveStencilBit() 

Source Link

Document

Reserve a stencil bit for use in rendering Note: you must check the Framebuffer you are working with to determine if stencil bits are enabled on it before use.

Usage

From source file:matteroverdrive.gui.element.MOElementTextField.java

License:Open Source License

@Override
public void drawForeground(int mouseX, int mouseY) {

    boolean enableStencil = this.enableStencil;
    int bit = -1;
    l: if (enableStencil) {
        bit = MinecraftForgeClient.reserveStencilBit();
        if (bit == -1) {
            enableStencil = false;//from  ww w .  j a v a2  s.  co  m
            break l;
        }
        GL11.glEnable(GL11.GL_STENCIL_TEST);
        drawStencil(posX + 1, posY + 1, posX + sizeX - 1, posY + sizeY - 1, 1 << bit);
    }

    FontRenderer font = getFontRenderer();
    char[] text = this.text;
    int startX = posX + 1 - (multiline ? renderStartX : 0), endX = sizeX - 1;
    int startY = posY + 1 - renderStartY, endY = startY + font.FONT_HEIGHT;
    int drawY = renderStartY + Math.max(0, (sizeY - 2) / font.FONT_HEIGHT) * font.FONT_HEIGHT;
    if (enableStencil) {
        if (sizeY - (drawY - renderStartY) > 2) {
            drawY += font.FONT_HEIGHT;
        }
    }
    int drawX = endX + (multiline ? renderStartX : 0);
    for (int i = multiline ? 0 : renderStartX, width = 0, height = 0; i <= textLength; ++i) {
        boolean end = i == textLength, draw = height >= renderStartY && width < drawX && height < drawY;
        int charW = 2;
        char c = 0;
        if (!end) {
            c = text[i];
            if (draw) {
                charW = multiline && c == '\n' ? 2 : font.getCharWidth(c);
            }
            int tWidth = width + charW;
            if (multiline) {
                if (!enableStencil) {
                    draw &= width >= renderStartX;
                }
                draw &= tWidth > renderStartX;
            }
            l: if (!enableStencil && tWidth > endX) {
                draw = false;
                if (multiline) {
                    if (c == '\n') {
                        break l;
                    }
                    continue;
                }
                break;
            }
        }

        boolean drawCaret = draw && i == caret && (caretCounter &= 31) < 16 && isFocused();
        if (drawCaret) {
            int caretEnd = width + 2;
            if (caretInsert) {
                caretEnd = width + charW;
            }
            drawModalRect(startX + width, startY - 1 + height, startX + caretEnd, endY + height,
                    (0xFF000000 & defaultCaretColor) | (~defaultCaretColor & 0xFFFFFF));
        }

        if (draw && !end) {
            boolean selected = i >= selectionStart & i < selectionEnd;
            if (selected) {
                drawModalRect(startX + width, startY + height, startX + width + charW, endY + height,
                        selectedLineColor);
            }
            if (c != '\n') {
                font.drawString(String.valueOf(c), startX + width, startY + height,
                        selected ? selectedTextColor : textColor);
            }
        }

        if (drawCaret) {
            int caretEnd = width + 2;
            if (caretInsert) {
                caretEnd = width + charW;
            }

            GL11.glEnable(GL11.GL_BLEND);
            GL11.glBlendFunc(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ZERO);
            gui.drawSizedRect(startX + width, startY - 1 + height, startX + caretEnd, endY + height, -1);
            GL11.glDisable(GL11.GL_BLEND);
        }

        if (c == '\n') {
            height += font.FONT_HEIGHT;
            charW = width = 0;
            if (height > drawY) {
                break;
            }
        }

        width += charW;
        if (!multiline && width > endX) {
            break;
        }
    }

    if (enableStencil) {
        GL11.glDisable(GL11.GL_STENCIL_TEST);
        MinecraftForgeClient.releaseStencilBit(bit);
    }

    if (holoIcon != null) {
        if (color != null)
            RenderUtils.applyColor(color);

        float heightScale = (float) Math.min(holoIcon.getOriginalHeight(), sizeY)
                / (float) holoIcon.getOriginalHeight();
        ClientProxy.holoIcons.renderIcon(holoIcon, posX - holoIcon.getOriginalWidth() - 2, posY,
                (int) (holoIcon.getOriginalWidth() * heightScale),
                (int) (holoIcon.getOriginalHeight() * heightScale));
    }
}