Search in sources :

Example 11 with SubRegion

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

the class UIDropdownScrollable method createScrollbarItems.

/**
 * Located in the onDraw method, this draws the menu items with a scrollbar.
 *
 * @param canvas     {@link Canvas} from the onDraw method.
 * @param frame      Menu frame.
 * @param font       {@link Font} used in the menu.
 * @param itemMargin Margin around every menu item.
 * @param height     Total menu height.
 * @param itemHeight Height per menu item.
 */
private void createScrollbarItems(Canvas canvas, Rect2i frame, Font font, Border itemMargin, int height, int itemHeight) {
    // Scrollable Area
    Rect2i scrollableArea = Rect2i.createFromMinAndSize(0, canvas.size().y, canvas.size().x, height - itemMargin.getBottom());
    // Scrollbar Measurement
    int scrollbarWidth = canvas.calculateRestrictedSize(verticalBar, new Vector2i(canvas.size().x, canvas.size().y)).x;
    int scrollbarHeight = frame.size().y - itemMargin.getTop();
    int availableWidth = frame.size().x - scrollbarWidth;
    int scrollbarXPos = availableWidth - itemMargin.getRight();
    int scrollbarYPos = itemMargin.getTotalHeight() * 2 + font.getLineHeight();
    // Draw Scrollbar
    Rect2i scrollbarRegion = Rect2i.createFromMinAndSize(scrollbarXPos, scrollbarYPos, scrollbarWidth, scrollbarHeight);
    canvas.drawWidget(verticalBar, scrollbarRegion);
    // Set the range of Scrollbar
    float maxVertBarDesired = itemHeight * (optionListeners.size() - visibleOptionsNum - 0.5f) + itemMargin.getBottom();
    verticalBar.setRange((int) maxVertBarDesired);
    for (int i = 0; i < optionListeners.size(); ++i) {
        readItemMouseOver(canvas, i);
        Rect2i itemRegion = Rect2i.createFromMinAndSize(0, itemHeight * i - verticalBar.getValue(), availableWidth, itemHeight);
        // If outside location, then hide
        try (SubRegion ignored = canvas.subRegion(scrollableArea, true)) {
            drawItem(canvas, itemMargin, i, itemRegion);
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Vector2i(org.terasology.math.geom.Vector2i) SubRegion(org.terasology.rendering.nui.SubRegion)

Example 12 with SubRegion

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

the class UISlider method onDraw.

@Override
public void onDraw(Canvas canvas) {
    canvas.setPart(SLIDER);
    canvas.drawBackground();
    canvas.setPart(TICKER);
    String display = getDisplayText();
    int tickerWidth = canvas.getCurrentStyle().getFont().getWidth(display);
    tickerWidth += canvas.getCurrentStyle().getMargin().getTotalWidth();
    sliderWidth = canvas.size().x - tickerWidth;
    int drawLocation = pixelOffsetFor(getValue(), sliderWidth);
    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)

Example 13 with SubRegion

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

the class BehaviorEditor method onDraw.

@Override
public void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    canvas.addInteractionRegion(mouseInteractionListener);
    try (SubRegion subRegion = canvas.subRegion(canvas.getRegion(), false)) {
        canvas.setDrawOnTop(true);
        for (UIWidget widget : getWidgets()) {
            if (!widget.isVisible()) {
                continue;
            }
            if (widget instanceof RenderableNode) {
                RenderableNode renderableNode = (RenderableNode) widget;
                for (Port port : renderableNode.getPorts()) {
                    Port targetPort = port.getTargetPort();
                    if (port.isInput() || targetPort == null || !targetPort.node.isVisible()) {
                        continue;
                    }
                    drawConnection(canvas, port, targetPort, port == activeConnectionStart ? Color.BLACK : Color.GREY);
                }
            }
        }
        if (activeConnectionStart != null) {
            drawConnection(canvas, activeConnectionStart, mouseWorldPosition, Color.WHITE);
        }
        if (selectedNode != null) {
            Vector2f size = selectedNode.getSize();
            Vector2f topLeft = selectedNode.getPosition();
            Vector2f topRight = new Vector2f(topLeft);
            topRight.add(new Vector2f(size.x + .1f, 0));
            Vector2f bottomLeft = new Vector2f(topLeft);
            bottomLeft.add(new Vector2f(0, size.y + .1f));
            Vector2f bottomRight = new Vector2f(topLeft);
            bottomRight.add(new Vector2f(size.x + 0.1f, size.y + 0.1f));
            drawConnection(canvas, topLeft, topRight, Color.GREEN);
            drawConnection(canvas, topRight, bottomRight, Color.GREEN);
            drawConnection(canvas, bottomRight, bottomLeft, Color.GREEN);
            drawConnection(canvas, bottomLeft, topLeft, Color.GREEN);
        }
        if (newNode != null) {
            newNode.visit(node -> drawWidget(canvas, node));
        }
        canvas.setDrawOnTop(false);
    }
}
Also used : Vector2f(org.terasology.math.geom.Vector2f) SubRegion(org.terasology.rendering.nui.SubRegion) UIWidget(org.terasology.rendering.nui.UIWidget)

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