Search in sources :

Example 6 with SubRegion

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

the class CursorAttachment method onDraw.

@Override
public void onDraw(Canvas canvas) {
    UIStyle style = canvas.getCurrentStyle();
    Vector2i attachmentSize = canvas.calculatePreferredSize(attachment);
    attachmentSize.add(style.getMargin().getTotals());
    // TODO get rid of CoreRegistry. e.g. by updatin a mousePos field with an InteractionListener
    MouseDevice mouse = CoreRegistry.get(InputSystem.class).getMouseDevice();
    int top;
    switch(style.getVerticalAlignment()) {
        case TOP:
            top = mouse.getPosition().y - attachmentSize.y;
            break;
        case MIDDLE:
            top = mouse.getPosition().y - attachmentSize.y / 2;
            break;
        default:
            top = mouse.getPosition().y + MOUSE_CURSOR_HEIGHT;
            break;
    }
    top = TeraMath.clamp(top, 0, canvas.size().y - attachmentSize.y);
    int left;
    switch(style.getHorizontalAlignment()) {
        case RIGHT:
            left = mouse.getPosition().x - attachmentSize.x;
            break;
        case CENTER:
            left = mouse.getPosition().x - attachmentSize.x / 2;
            break;
        default:
            left = mouse.getPosition().x;
            break;
    }
    left = TeraMath.clamp(left, 0, canvas.size().x - attachmentSize.x);
    try (SubRegion ignored = canvas.subRegion(Rect2i.createFromMinAndSize(left, top, attachmentSize.x, attachmentSize.y), false)) {
        canvas.drawBackground();
        canvas.drawWidget(attachment, style.getBackgroundBorder().shrink(canvas.getRegion()));
    }
}
Also used : UIStyle(org.terasology.rendering.nui.skin.UIStyle) InputSystem(org.terasology.input.InputSystem) Vector2i(org.terasology.math.geom.Vector2i) SubRegion(org.terasology.rendering.nui.SubRegion) MouseDevice(org.terasology.input.device.MouseDevice)

Example 7 with SubRegion

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

the class UIText method drawAll.

protected void drawAll(Canvas canvas, int multilineWidth) {
    if (text.get() == null) {
        text.set("");
    }
    if (text.get().equals("")) {
        text.set(hintText);
        isShowingHintText = true;
    }
    if (isShowingHintText) {
        setCursorPosition(0);
        if (!text.get().equals(hintText) && text.get().endsWith(hintText)) {
            text.set(text.get().substring(0, text.get().length() - hintText.length()));
            setCursorPosition(text.get().length());
            isShowingHintText = false;
        }
    }
    lastFont = canvas.getCurrentStyle().getFont();
    correctCursor();
    String textToDraw = passwordMode ? buildPasswordString() : text.get();
    int widthForDraw = (multiline) ? multilineWidth : lastFont.getWidth(textToDraw);
    try (SubRegion ignored = canvas.subRegion(canvas.getRegion(), true);
        SubRegion ignored2 = canvas.subRegion(Rect2i.createFromMinAndSize(-offset, 0, widthForDraw + 1, Integer.MAX_VALUE), false)) {
        if (isShowingHintText && !readOnly) {
            canvas.drawTextRaw(textToDraw, lastFont, canvas.getCurrentStyle().getHintTextColor(), canvas.getRegion());
        } else {
            canvas.drawText(textToDraw, canvas.getRegion());
        }
        if (isFocused()) {
            if (hasSelection()) {
                drawSelection(canvas);
            } else {
                drawCursor(canvas);
            }
        }
    }
}
Also used : SubRegion(org.terasology.rendering.nui.SubRegion)

Example 8 with SubRegion

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

the class CanvasImpl method calculateRestrictedSize.

@Override
public Vector2i calculateRestrictedSize(UIWidget widget, Vector2i sizeRestrictions) {
    if (widget == null) {
        return sizeRestrictions;
    }
    String family = (widget.getFamily() != null) ? widget.getFamily() : state.family;
    UISkin skin = (widget.getSkin() != null) ? widget.getSkin() : state.skin;
    UIStyle elementStyle = skin.getStyleFor(family, widget.getClass(), UIWidget.BASE_PART, widget.getMode());
    Rect2i region = applyStyleToSize(Rect2i.createFromMinAndSize(Vector2i.zero(), sizeRestrictions), elementStyle);
    try (SubRegion ignored = subRegionForWidget(widget, region, false)) {
        Vector2i preferredSize = widget.getPreferredContentSize(this, elementStyle.getMargin().shrink(sizeRestrictions));
        preferredSize = elementStyle.getMargin().grow(preferredSize);
        return applyStyleToSize(preferredSize, elementStyle);
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) UISkin(org.terasology.rendering.nui.skin.UISkin) UIStyle(org.terasology.rendering.nui.skin.UIStyle) Vector2i(org.terasology.math.geom.Vector2i) BaseVector2i(org.terasology.math.geom.BaseVector2i) SubRegion(org.terasology.rendering.nui.SubRegion)

Example 9 with SubRegion

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

the class ScrollableArea method layoutWithJustVertical.

private void layoutWithJustVertical(Canvas canvas, Vector2i contentSize, int fullWidth, int availableHeight, int scrollbarWidth) {
    int availableWidth = fullWidth - scrollbarWidth;
    boolean atBottom = verticalBar.getRange() == verticalBar.getValue();
    Rect2i contentRegion = Rect2i.createFromMinAndSize(0, 0, availableWidth, availableHeight);
    verticalBar.setRange(contentSize.y - contentRegion.height());
    if ((stickToBottom && atBottom) || moveToBottomPending) {
        verticalBar.setValue(verticalBar.getRange());
        moveToBottomPending = false;
    }
    if (moveToTopPending) {
        verticalBar.setValue(0);
        moveToTopPending = false;
    }
    canvas.addInteractionRegion(scrollListener);
    canvas.drawWidget(verticalBar, Rect2i.createFromMinAndSize(availableWidth, 0, scrollbarWidth, availableHeight));
    try (SubRegion ignored = canvas.subRegion(contentRegion, true)) {
        canvas.drawWidget(content, Rect2i.createFromMinAndSize(0, -verticalBar.getValue(), availableWidth, contentSize.y));
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) SubRegion(org.terasology.rendering.nui.SubRegion) LayoutHint(org.terasology.rendering.nui.LayoutHint)

Example 10 with SubRegion

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

the class UIDoubleSlider method drawTicker.

private void drawTicker(Canvas canvas, String part, Binding<Float> value, InteractionListener tickerListener, boolean rightTicker) {
    canvas.setPart(part);
    String display = String.format("%." + precision + "f", value.get());
    int tickerWidth = canvas.getCurrentStyle().getFont().getWidth(formatString);
    tickerWidth += canvas.getCurrentStyle().getMargin().getTotalWidth();
    sliderWidth = canvas.size().x - tickerWidth * 2;
    int drawLocation = pixelOffsetFor(value.get(), sliderWidth);
    if (rightTicker) {
        drawLocation += tickerWidth;
    }
    Rect2i tickerRegion = Rect2i.createFromMinAndSize(drawLocation, 0, tickerWidth, canvas.size().y);
    try (SubRegion ignored = canvas.subRegion(tickerRegion, false)) {
        canvas.drawBackground();
        canvas.drawText(display);
        if (isEnabled()) {
            canvas.addInteractionRegion(tickerListener);
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) SubRegion(org.terasology.rendering.nui.SubRegion)

Aggregations

SubRegion (org.terasology.rendering.nui.SubRegion)13 Rect2i (org.terasology.math.geom.Rect2i)8 Vector2i (org.terasology.math.geom.Vector2i)4 UIStyle (org.terasology.rendering.nui.skin.UIStyle)4 LayoutHint (org.terasology.rendering.nui.LayoutHint)3 BaseVector2i (org.terasology.math.geom.BaseVector2i)2 UISkin (org.terasology.rendering.nui.skin.UISkin)2 InputSystem (org.terasology.input.InputSystem)1 MouseDevice (org.terasology.input.device.MouseDevice)1 Vector2f (org.terasology.math.geom.Vector2f)1 UIWidget (org.terasology.rendering.nui.UIWidget)1