Search in sources :

Example 1 with VerticalAlign

use of org.terasology.rendering.nui.VerticalAlign in project Terasology by MovingBlocks.

the class RelativeLayout method getRegion.

private Rect2i getRegion(WidgetInfo element, Canvas canvas) {
    Rect2i cachedRegion = cachedRegions.get(element);
    if (cachedRegion != null) {
        return cachedRegion;
    }
    int left = 0;
    int right = canvas.size().x;
    int center = canvas.size().x / 2;
    if (element.layoutHint.getPositionCenterHorizontal() != null) {
        HorizontalInfo info = element.layoutHint.getPositionCenterHorizontal();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        HorizontalAlign align = (info.getTarget() != null) ? info.getTarget() : HorizontalAlign.CENTER;
        center = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionLeft() != null) {
        HorizontalInfo info = element.layoutHint.getPositionLeft();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        HorizontalAlign align = (info.getTarget() != null) ? info.getTarget() : HorizontalAlign.LEFT;
        left = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionRight() != null) {
        HorizontalInfo info = element.layoutHint.getPositionRight();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        HorizontalAlign align = (info.getTarget() != null) ? info.getTarget() : HorizontalAlign.RIGHT;
        right = align.getStart(targetRegion) - info.getOffset();
    }
    int top = 0;
    int bottom = canvas.size().y;
    int vcenter = canvas.size().y / 2;
    if (element.layoutHint.getPositionCenterVertical() != null) {
        VerticalInfo info = element.layoutHint.getPositionCenterVertical();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        VerticalAlign align = (info.getTarget() != null) ? info.getTarget() : VerticalAlign.MIDDLE;
        vcenter = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionTop() != null) {
        VerticalInfo info = element.layoutHint.getPositionTop();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        VerticalAlign align = (info.getTarget() != null) ? info.getTarget() : VerticalAlign.TOP;
        top = align.getStart(targetRegion) + info.getOffset();
    }
    if (element.layoutHint.getPositionBottom() != null) {
        VerticalInfo info = element.layoutHint.getPositionBottom();
        Rect2i targetRegion = getTargetRegion(info.getWidget(), canvas);
        VerticalAlign align = (info.getTarget() != null) ? info.getTarget() : VerticalAlign.BOTTOM;
        bottom = align.getStart(targetRegion) - info.getOffset();
    }
    int width = element.layoutHint.getWidth();
    if (width == 0 && element.layoutHint.isUsingContentWidth()) {
        width = canvas.calculateRestrictedSize(element.widget, new Vector2i(right - left, bottom - top)).x;
    }
    if (width == 0) {
        width = right - left;
    } else {
        if (element.layoutHint.getPositionCenterHorizontal() != null) {
            left = center - width / 2;
        } else if (element.layoutHint.getPositionRight() != null) {
            if (element.layoutHint.getPositionLeft() != null) {
                center = left + (right - left) / 2;
                left = center - width / 2;
            } else {
                left = right - width;
            }
        }
    }
    int height = element.layoutHint.getHeight();
    if (height == 0 && element.layoutHint.isUsingContentHeight()) {
        height = canvas.calculateRestrictedSize(element.widget, new Vector2i(width, bottom - top)).y;
    }
    if (height == 0) {
        height = bottom - top;
    } else {
        if (element.layoutHint.getPositionCenterVertical() != null) {
            top = vcenter - height / 2;
        } else if (element.layoutHint.getPositionBottom() != null) {
            if (element.layoutHint.getPositionTop() != null) {
                vcenter = top + (bottom - top) / 2;
                top = vcenter - height / 2;
            } else {
                top = bottom - height;
            }
        }
    }
    Rect2i region = Rect2i.createFromMinAndSize(left, top, width, height);
    cachedRegions.put(element, region);
    return region;
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) VerticalAlign(org.terasology.rendering.nui.VerticalAlign) HorizontalAlign(org.terasology.rendering.nui.HorizontalAlign) Vector2i(org.terasology.math.geom.Vector2i)

Example 2 with VerticalAlign

use of org.terasology.rendering.nui.VerticalAlign in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawText.

@Override
public void drawText(String text, Font font, HorizontalAlign hAlign, VerticalAlign vAlign, Rect2i absoluteRegion, Color color, Color shadowColor, float alpha, boolean underlined) {
    TextCacheKey key = new TextCacheKey(text, font, absoluteRegion.width(), hAlign, color, shadowColor, underlined);
    usedText.add(key);
    Map<Material, Mesh> fontMesh = cachedText.get(key);
    List<String> lines = TextLineBuilder.getLines(font, text, absoluteRegion.width());
    if (fontMesh != null) {
        for (Mesh mesh : fontMesh.values()) {
            if (mesh.isDisposed()) {
                fontMesh = null;
                break;
            }
        }
    }
    if (fontMesh == null) {
        fontMesh = fontMeshBuilder.createTextMesh(font, lines, absoluteRegion.width(), hAlign, color, shadowColor, underlined);
        cachedText.put(key, fontMesh);
    }
    Vector2i offset = new Vector2i(absoluteRegion.minX(), absoluteRegion.minY());
    offset.y += vAlign.getOffset(lines.size() * font.getLineHeight(), absoluteRegion.height());
    fontMesh.entrySet().stream().filter(entry -> entry.getKey().isRenderable()).forEach(entry -> {
        entry.getKey().bindTextures();
        entry.getKey().setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX(), requestedCropRegion.maxX(), requestedCropRegion.minY(), requestedCropRegion.maxY());
        entry.getKey().setFloat2("offset", offset.x, offset.y);
        entry.getKey().setFloat("alpha", alpha);
        entry.getValue().render();
    });
}
Also used : FloatBuffer(java.nio.FloatBuffer) AssetManager(org.terasology.assets.management.AssetManager) Vector3f(org.terasology.math.geom.Vector3f) FrameBufferObject(org.terasology.rendering.opengl.FrameBufferObject) GL11.glDisable(org.lwjgl.opengl.GL11.glDisable) FontMeshBuilder(org.terasology.rendering.assets.font.FontMeshBuilder) GL11.glOrtho(org.lwjgl.opengl.GL11.glOrtho) GL11.glTranslatef(org.lwjgl.opengl.GL11.glTranslatef) Material(org.terasology.rendering.assets.material.Material) Map(java.util.Map) GL11(org.lwjgl.opengl.GL11) TeraMath(org.terasology.math.TeraMath) TextLineBuilder(org.terasology.rendering.nui.TextLineBuilder) GL11.glLoadMatrix(org.lwjgl.opengl.GL11.glLoadMatrix) Display(org.lwjgl.opengl.Display) Context(org.terasology.context.Context) GL_PROJECTION(org.lwjgl.opengl.GL11.GL_PROJECTION) GL_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_SRC_ALPHA) GL_DEPTH_TEST(org.lwjgl.opengl.GL11.GL_DEPTH_TEST) Set(java.util.Set) MeshBuilder(org.terasology.rendering.assets.mesh.MeshBuilder) GL11.glLoadIdentity(org.lwjgl.opengl.GL11.glLoadIdentity) Sets(com.google.common.collect.Sets) GL11.glPopMatrix(org.lwjgl.opengl.GL11.glPopMatrix) BufferUtils(org.lwjgl.BufferUtils) Objects(java.util.Objects) List(java.util.List) AABB(org.terasology.math.AABB) Font(org.terasology.rendering.assets.font.Font) TextureRegion(org.terasology.rendering.assets.texture.TextureRegion) GL11.glPushMatrix(org.lwjgl.opengl.GL11.glPushMatrix) ResourceUrn(org.terasology.assets.ResourceUrn) GL_MODELVIEW(org.lwjgl.opengl.GL11.GL_MODELVIEW) HorizontalAlign(org.terasology.rendering.nui.HorizontalAlign) VerticalAlign(org.terasology.rendering.nui.VerticalAlign) MatrixUtils(org.terasology.math.MatrixUtils) Rect2f(org.terasology.math.geom.Rect2f) ScaleMode(org.terasology.rendering.nui.ScaleMode) Rect2i(org.terasology.math.geom.Rect2i) Matrix4f(org.terasology.math.geom.Matrix4f) Mesh(org.terasology.rendering.assets.mesh.Mesh) BaseQuat4f(org.terasology.math.geom.BaseQuat4f) GL11.glClear(org.lwjgl.opengl.GL11.glClear) Assets(org.terasology.utilities.Assets) GL11.glMatrixMode(org.lwjgl.opengl.GL11.glMatrixMode) GL11.glEnable(org.lwjgl.opengl.GL11.glEnable) Iterator(java.util.Iterator) Vector2i(org.terasology.math.geom.Vector2i) Vector2f(org.terasology.math.geom.Vector2f) Maps(com.google.common.collect.Maps) ShaderProgramFeature(org.terasology.rendering.assets.shader.ShaderProgramFeature) LwjglFrameBufferObject(org.terasology.rendering.opengl.LwjglFrameBufferObject) GL11.glBlendFunc(org.lwjgl.opengl.GL11.glBlendFunc) GL11.glScalef(org.lwjgl.opengl.GL11.glScalef) GL_ONE_MINUS_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA) Border(org.terasology.math.Border) Quat4f(org.terasology.math.geom.Quat4f) BaseVector2i(org.terasology.math.geom.BaseVector2i) Color(org.terasology.rendering.nui.Color) GL_BLEND(org.lwjgl.opengl.GL11.GL_BLEND) Mesh(org.terasology.rendering.assets.mesh.Mesh) Material(org.terasology.rendering.assets.material.Material) Vector2i(org.terasology.math.geom.Vector2i) BaseVector2i(org.terasology.math.geom.BaseVector2i)

Aggregations

Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 FloatBuffer (java.nio.FloatBuffer)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 BufferUtils (org.lwjgl.BufferUtils)1 Display (org.lwjgl.opengl.Display)1 GL11 (org.lwjgl.opengl.GL11)1 GL_BLEND (org.lwjgl.opengl.GL11.GL_BLEND)1 GL_DEPTH_TEST (org.lwjgl.opengl.GL11.GL_DEPTH_TEST)1 GL_MODELVIEW (org.lwjgl.opengl.GL11.GL_MODELVIEW)1 GL_ONE_MINUS_SRC_ALPHA (org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA)1 GL_PROJECTION (org.lwjgl.opengl.GL11.GL_PROJECTION)1 GL_SRC_ALPHA (org.lwjgl.opengl.GL11.GL_SRC_ALPHA)1 GL11.glBlendFunc (org.lwjgl.opengl.GL11.glBlendFunc)1 GL11.glClear (org.lwjgl.opengl.GL11.glClear)1 GL11.glDisable (org.lwjgl.opengl.GL11.glDisable)1