Search in sources :

Example 1 with NVGColor

use of org.lwjgl.nanovg.NVGColor in project legui by SpinyOwl.

the class NvgText method drawTextLineToRect.

public static void drawTextLineToRect(long nvg, Vector4fc rect, boolean hideOverflow, HorizontalAlign horizontalAlign, VerticalAlign verticalAlign, float fontSize, String font, String textToRender, Vector4f fontColor, TextDirection direction) {
    if (textToRender.length() == 0) {
        return;
    }
    nvgFontSize(nvg, fontSize);
    nvgFontFace(nvg, font);
    textAlign(nvg, horizontalAlign, verticalAlign);
    ByteBuffer byteText = null;
    try {
        byteText = memUTF8(textToRender, false);
        long startPointer = memAddress(byteText);
        long endPointer = startPointer + byteText.remaining();
        long rowStart = startPointer;
        long rowEnd = endPointer;
        if (hideOverflow) {
            try (NVGTextRow.Buffer buffer = NVGTextRow.calloc(1)) {
                int rows = nnvgTextBreakLines(nvg, startPointer, endPointer, direction == TextDirection.HORIZONTAL ? rect.z() : rect.w(), memAddress(buffer), 1);
                if (rows != 0) {
                    NVGTextRow row = buffer.get(0);
                    rowStart = row.start();
                    rowEnd = row.end();
                }
            }
        }
        Vector2f textPosition = new Vector2f(rect.x() + rect.z() * horizontalAlign.index / 2f, rect.y() + rect.w() * verticalAlign.index / 2f);
        if (rowStart != 0 || rowEnd != 0) {
            try (NVGColor textColor = NvgColorUtil.create(fontColor)) {
                nvgSave(nvg);
                nvgBeginPath(nvg);
                nvgFillColor(nvg, textColor);
                float x;
                float y;
                if (direction == TextDirection.VERTICAL_TOP_DOWN) {
                    nvgTranslate(nvg, rect.x() + rect.z(), rect.y());
                    nvgRotate(nvg, _90);
                    x = rect.w() * horizontalAlign.index / 2f;
                    y = rect.z() * verticalAlign.index / 2f;
                } else if (direction == TextDirection.VERTICAL_DOWN_TOP) {
                    nvgTranslate(nvg, rect.x(), rect.y() + rect.w());
                    nvgRotate(nvg, _270);
                    x = rect.w() * horizontalAlign.index / 2f;
                    y = rect.z() * verticalAlign.index / 2f;
                } else {
                    nvgTranslate(nvg, rect.x(), rect.y());
                    x = textPosition.x - rect.x();
                    y = (int) textPosition.y - rect.y();
                }
                int xx = (int) x;
                int yy = (int) y;
                nnvgText(nvg, xx, yy, rowStart, rowEnd);
                nvgRestore(nvg);
            }
        }
    } finally {
        if (byteText != null) {
            MemoryUtil.memFree(byteText);
        }
    }
}
Also used : Vector2f(org.joml.Vector2f) NVGTextRow(org.lwjgl.nanovg.NVGTextRow) NVGColor(org.lwjgl.nanovg.NVGColor) ByteBuffer(java.nio.ByteBuffer)

Example 2 with NVGColor

use of org.lwjgl.nanovg.NVGColor in project legui by SpinyOwl.

the class NvgColorUtil method create.

/**
 * Used to allocate and fill instance of {@link NVGColor}. Should be used in try-with-resources to
 * avoid memory leaks.
 *
 * @param r red.
 * @param g green.
 * @param b blue.
 * @param a alpha.
 * @return allocated and filled color.
 */
public static NVGColor create(float r, float g, float b, float a) {
    NVGColor color = NVGColor.calloc();
    color.r(r);
    color.g(g);
    color.b(b);
    color.a(a);
    return color;
}
Also used : NVGColor(org.lwjgl.nanovg.NVGColor)

Example 3 with NVGColor

use of org.lwjgl.nanovg.NVGColor in project legui by SpinyOwl.

the class NvgShapes method drawRect.

/**
 * Used to draw rectangle.
 *
 * @param nvg       nanovg context
 * @param rectangle rectangle size and position.
 * @param bgColor   rectangle background color.
 */
public static void drawRect(long nvg, Vector4fc rectangle, Vector4fc bgColor) {
    if (bgColor.w() <= MIN_ALPHA) {
        return;
    }
    try (NVGColor fillColor = NvgColorUtil.create(bgColor)) {
        nvgBeginPath(nvg);
        nvgFillColor(nvg, fillColor);
        nvgRect(nvg, rectangle.x(), rectangle.y(), rectangle.z(), rectangle.w());
        nvgFill(nvg);
    }
}
Also used : NVGColor(org.lwjgl.nanovg.NVGColor)

Example 4 with NVGColor

use of org.lwjgl.nanovg.NVGColor in project legui by SpinyOwl.

the class NvgShapes method drawRect.

/**
 * Used to draw rectangle.
 *
 * @param nvg      nanovg context.
 * @param position rectangle position.
 * @param size     rectangle size.
 * @param bgColor  rectangle background color.
 * @param radius   cornder radius
 */
public static void drawRect(long nvg, Vector2fc position, Vector2fc size, Vector4fc bgColor, float radius) {
    if (bgColor.w() <= MIN_ALPHA) {
        return;
    }
    try (NVGColor fillColor = NvgColorUtil.create(bgColor)) {
        nvgBeginPath(nvg);
        nvgFillColor(nvg, fillColor);
        nvgRoundedRect(nvg, position.x(), position.y(), size.x(), size.y(), radius);
        nvgFill(nvg);
    }
}
Also used : NVGColor(org.lwjgl.nanovg.NVGColor)

Example 5 with NVGColor

use of org.lwjgl.nanovg.NVGColor in project legui by SpinyOwl.

the class NvgShapes method drawRect.

/**
 * Used to draw rectangle.
 *
 * @param nvg       nanovg context
 * @param rectangle rectangle size and position.
 * @param bgColor   rectangle background color.
 * @param radius    cornder radius
 */
public static void drawRect(long nvg, Vector4fc rectangle, Vector4fc bgColor, float radius) {
    if (bgColor.w() <= MIN_ALPHA) {
        return;
    }
    try (NVGColor fillColor = NvgColorUtil.create(bgColor)) {
        nvgBeginPath(nvg);
        nvgFillColor(nvg, fillColor);
        nvgRoundedRect(nvg, rectangle.x(), rectangle.y(), rectangle.z(), rectangle.w(), radius);
        nvgFill(nvg);
    }
}
Also used : NVGColor(org.lwjgl.nanovg.NVGColor)

Aggregations

NVGColor (org.lwjgl.nanovg.NVGColor)19 ByteBuffer (java.nio.ByteBuffer)3 Vector4f (org.joml.Vector4f)3 TextState (com.spinyowl.legui.component.optional.TextState)2 HorizontalAlign (com.spinyowl.legui.component.optional.align.HorizontalAlign)2 VerticalAlign (com.spinyowl.legui.component.optional.align.VerticalAlign)2 Style (com.spinyowl.legui.style.Style)2 StyleUtilities.getStyle (com.spinyowl.legui.style.util.StyleUtilities.getStyle)2 Vector2f (org.joml.Vector2f)2 NVGGlyphPosition (org.lwjgl.nanovg.NVGGlyphPosition)2 TextInputWidthChangeEvent (com.spinyowl.legui.component.event.textinput.TextInputWidthChangeEvent)1 Shadow (com.spinyowl.legui.style.shadow.Shadow)1 NVGPaint (org.lwjgl.nanovg.NVGPaint)1 NVGTextRow (org.lwjgl.nanovg.NVGTextRow)1