Search in sources :

Example 41 with Rect2i

use of org.terasology.math.geom.Rect2i 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)

Example 42 with Rect2i

use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.

the class UIDropdownScrollable method createNoScrollItems.

/**
 * Located in the onDraw method, this draws the menu items when the scrollbar is unnecessary.
 *
 * @param canvas     {@link Canvas} from the onDraw method.
 * @param itemMargin Margin around every menu item.
 * @param itemHeight Height per menu item.
 */
private void createNoScrollItems(Canvas canvas, Border itemMargin, int itemHeight) {
    for (int i = 0; i < optionListeners.size(); ++i) {
        readItemMouseOver(canvas, i);
        Rect2i itemRegion = Rect2i.createFromMinAndSize(0, canvas.size().y + itemHeight * i, canvas.size().x, itemHeight);
        drawItem(canvas, itemMargin, i, itemRegion);
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i)

Example 43 with Rect2i

use of org.terasology.math.geom.Rect2i 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 44 with Rect2i

use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.

the class UIIconBar method onDraw.

@Override
public void onDraw(Canvas canvas) {
    canvas.setPart(ICON_PART);
    if (icon != null && getMaxValue() > 0) {
        Vector2i iconSize = getIconSize(canvas);
        float ratio = maxIcons * getValue() / getMaxValue();
        int fullIcons = TeraMath.floorToInt(ratio);
        boolean halfIcon = false;
        if (ratio - fullIcons >= 0.5f) {
            fullIcons++;
        } else if (ratio - fullIcons > 0) {
            halfIcon = true;
        }
        Vector2i offset = new Vector2i();
        for (int i = 0; i < maxIcons; ++i) {
            Rect2i iconRegion = Rect2i.createFromMinAndSize(offset, iconSize);
            canvas.drawBackground(iconRegion);
            if (ratio - i >= 0.5f) {
                canvas.drawTexture(icon, iconRegion);
            } else if (ratio - i > 0f) {
                switch(halfIconMode) {
                    case SHRINK:
                        Vector2i halfSize = new Vector2i(iconSize);
                        halfSize.x /= 2;
                        halfSize.y /= 2;
                        canvas.drawTexture(icon, Rect2i.createFromMinAndSize(new Vector2i(offset.x + halfSize.x / 2, offset.y + halfSize.y / 2), halfSize));
                        break;
                    case SPLIT:
                        canvas.drawTextureRaw(icon, Rect2i.createFromMinAndSize(offset, new Vector2i(iconSize.x / 2, iconSize.y)), ScaleMode.STRETCH, 0f, 0f, (float) (iconSize.x / 2) / iconSize.x, 1.0f);
                        break;
                    default:
                        canvas.drawTexture(icon, iconRegion);
                        break;
                }
            }
            offset.x += iconSize.x + spacing;
            if (offset.x + iconSize.x > canvas.size().x) {
                offset.x = 0;
                offset.y += iconSize.y + spacing;
            }
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Vector2i(org.terasology.math.geom.Vector2i)

Example 45 with Rect2i

use of org.terasology.math.geom.Rect2i in project Terasology by MovingBlocks.

the class UIRadialRing method initialise.

private void initialise(Canvas canvas) {
    final double circleToSquare = 0.707106781;
    Rect2i region = canvas.getRegion();
    int sectionWidth = region.width() / 4;
    double offset = sectionWidth * 1.5;
    radius = sectionWidth * 2;
    sectionAngle = (Math.PI * 2) / sections.size();
    int infoSquareSize = (int) (radius * circleToSquare);
    int sectionSquareSize = (int) (sectionWidth * circleToSquare);
    Rect2i infoRegion = Rect2i.createFromMinAndSize(sectionWidth + infoSquareSize / 4, sectionWidth + infoSquareSize / 4, infoSquareSize, infoSquareSize);
    for (int i = 0; i < sections.size(); i++) {
        sections.get(i).setDrawRegion(Rect2i.createFromMinAndSize((int) (Math.cos(i * sectionAngle + sectionAngle / 2) * offset + sectionWidth * 1.5), (int) (Math.sin(i * sectionAngle + sectionAngle / 2) * offset + sectionWidth * 1.5), sectionWidth, sectionWidth));
        sections.get(i).setInnerRegion(Rect2i.createFromMinAndSize((int) (Math.cos(i * sectionAngle + sectionAngle / 2) * offset + sectionWidth * 1.5 + sectionSquareSize / 4), (int) (Math.sin(i * sectionAngle + sectionAngle / 2) * offset + sectionWidth * 1.5 + sectionSquareSize / 4), sectionSquareSize, sectionSquareSize));
        sections.get(i).setInfoRegion(infoRegion);
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i)

Aggregations

Rect2i (org.terasology.math.geom.Rect2i)59 Vector2i (org.terasology.math.geom.Vector2i)13 SubRegion (org.terasology.rendering.nui.SubRegion)8 BaseVector2i (org.terasology.math.geom.BaseVector2i)7 Font (org.terasology.rendering.assets.font.Font)5 LayoutHint (org.terasology.rendering.nui.LayoutHint)5 SurfaceHeightFacet (org.terasology.world.generation.facets.SurfaceHeightFacet)4 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)3 Color (org.terasology.rendering.nui.Color)3 BufferedImage (java.awt.image.BufferedImage)2 Test (org.junit.Test)2 UIWidget (org.terasology.rendering.nui.UIWidget)2 FallbackParagraphRenderStyle (org.terasology.rendering.nui.widgets.browser.ui.style.FallbackParagraphRenderStyle)2 ParagraphRenderStyle (org.terasology.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)2 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 TIntList (gnu.trove.list.TIntList)1 Graphics2D (java.awt.Graphics2D)1 DataBufferInt (java.awt.image.DataBufferInt)1