Search in sources :

Example 6 with Rectangle2d

use of net.minecraft.client.renderer.Rectangle2d in project minecolonies by Minecolonies.

the class JobBasedRecipeCategory method calculateInfoBlocks.

@NotNull
private List<InfoBlock> calculateInfoBlocks(@NotNull T recipe) {
    final List<ITextComponent> lines = generateInfoBlocks(recipe);
    final Minecraft mc = Minecraft.getInstance();
    final List<InfoBlock> result = new ArrayList<>();
    int y = CITIZEN_Y;
    for (final ITextComponent line : lines) {
        final String text = line.getString();
        final int width = (int) mc.font.getSplitter().stringWidth(text);
        final int height = mc.font.lineHeight;
        final int x = WIDTH - width;
        String tip = null;
        if (line instanceof TranslationTextComponent) {
            final String key = ((TranslationTextComponent) line).getKey() + ".tip";
            if (I18n.exists(key)) {
                tip = (new TranslationTextComponent(key, ((TranslationTextComponent) line).getArgs())).getString();
            }
        }
        result.add(new InfoBlock(text, tip, new Rectangle2d(x, y, width, height)));
        y += height + 2;
    }
    return result;
}
Also used : Rectangle2d(net.minecraft.client.renderer.Rectangle2d) Minecraft(net.minecraft.client.Minecraft) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Rectangle2d

use of net.minecraft.client.renderer.Rectangle2d in project Mekanism by mekanism.

the class GhostIngredientHandler method getTargets.

@Override
public <INGREDIENT> List<Target<INGREDIENT>> getTargets(GUI gui, INGREDIENT ingredient, boolean doStart) {
    boolean hasTargets = false;
    int depth = 0;
    Int2ObjectLinkedOpenHashMap<List<TargetInfo<INGREDIENT>>> depthBasedTargets = new Int2ObjectLinkedOpenHashMap<>();
    Int2ObjectMap<List<Rectangle2d>> layerIntersections = new Int2ObjectOpenHashMap<>();
    List<TargetInfo<INGREDIENT>> ghostTargets = getTargets(gui.children(), ingredient);
    if (!ghostTargets.isEmpty()) {
        // If we found any targets increment the layer count and add them to our depth based target list
        depthBasedTargets.put(depth, ghostTargets);
        hasTargets = true;
    }
    // Now gather the targets for the windows in reverse-order (i.e. back to front)
    for (LRU<GuiWindow>.LRUIterator iter = gui.getWindowsDescendingIterator(); iter.hasNext(); ) {
        GuiWindow window = iter.next();
        depth++;
        if (hasTargets) {
            // If we have at least one layer with targets grab the intersection information for this window's layer
            List<Rectangle2d> areas = new ArrayList<>();
            areas.add(new Rectangle2d(window.x, window.y, window.getWidth(), window.getHeight()));
            areas.addAll(GuiElementHandler.getAreasFor(window.x, window.y, window.getWidth(), window.getHeight(), window.children()));
            layerIntersections.put(depth, areas);
        }
        ghostTargets = getTargets(window.children(), ingredient);
        if (!ghostTargets.isEmpty()) {
            // If we found any targets increment the layer count and add them to our depth based target list
            depthBasedTargets.put(depth, ghostTargets);
            hasTargets = true;
        }
    }
    if (!hasTargets) {
        // If we don't have any layers with elements in them just return
        return Collections.emptyList();
    }
    List<Target<INGREDIENT>> targets = new ArrayList<>();
    List<Rectangle2d> coveredArea = new ArrayList<>();
    // Note: we iterate the target info in reverse so that we are able to more easily build up a list of the area that is covered
    // in front of the level of targets we are currently adding to
    FastSortedEntrySet<List<TargetInfo<INGREDIENT>>> depthEntries = depthBasedTargets.int2ObjectEntrySet();
    for (ObjectBidirectionalIterator<Entry<List<TargetInfo<INGREDIENT>>>> iter = depthEntries.fastIterator(depthEntries.last()); iter.hasPrevious(); ) {
        Entry<List<TargetInfo<INGREDIENT>>> entry = iter.previous();
        int targetDepth = entry.getIntKey();
        for (; depth > targetDepth; depth--) {
            // If we are at a lower depth than the max depth we have things for add all the ones of higher depth
            coveredArea.addAll(layerIntersections.get(depth));
        }
        for (TargetInfo<INGREDIENT> ghostTarget : entry.getValue()) {
            targets.addAll(ghostTarget.convertToTargets(coveredArea));
        }
    }
    return targets;
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) GuiWindow(mekanism.client.gui.element.window.GuiWindow) Rectangle2d(net.minecraft.client.renderer.Rectangle2d) ArrayList(java.util.ArrayList) LRU(mekanism.common.lib.collection.LRU) IJEIGhostTarget(mekanism.client.jei.interfaces.IJEIGhostTarget) Entry(it.unimi.dsi.fastutil.ints.Int2ObjectMap.Entry) ArrayList(java.util.ArrayList) List(java.util.List) Int2ObjectLinkedOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap)

Example 8 with Rectangle2d

use of net.minecraft.client.renderer.Rectangle2d in project Mekanism by mekanism.

the class GhostIngredientHandler method getVisibleArea.

private static List<Rectangle2d> getVisibleArea(Rectangle2d area, Rectangle2d coveredArea) {
    // Useful tool for visualizing overlaps: https://silentmatt.com/rectangle-intersection/
    // TODO: Look into further cleaning this up so that it is less "hardcoded" manner for adding the various components
    // started out as more hardcoded to actually figure out the different pieces
    int x = area.getX();
    int x2 = x + area.getWidth();
    int y = area.getY();
    int y2 = y + area.getHeight();
    int cx = coveredArea.getX();
    int cx2 = cx + coveredArea.getWidth();
    int cy = coveredArea.getY();
    int cy2 = cy + coveredArea.getHeight();
    // Given we know it intersects we can use a simplified check for seeing which sides get intersected
    boolean intersectsTop = y >= cy && y <= cy2;
    boolean intersectsLeft = x >= cx && x <= cx2;
    boolean intersectsBottom = y2 >= cy && y2 <= cy2;
    boolean intersectsRight = x2 >= cx && x2 <= cx2;
    List<Rectangle2d> areas = new ArrayList<>();
    if (intersectsTop && intersectsBottom) {
        // Intersects three sides (even if the perpendicular one may only have the top and bottom point intersected), we have one rectangle
        if (intersectsLeft) {
            // Right section
            areas.add(new Rectangle2d(cx2, y, x2 - cx2, area.getHeight()));
        } else if (intersectsRight) {
            // Left section
            areas.add(new Rectangle2d(x, y, cx - x, area.getHeight()));
        } else {
            // Intersects two parallel sides, we have two rectangles
            // Left section
            areas.add(new Rectangle2d(x, y, cx - x, area.getHeight()));
            // Right section
            areas.add(new Rectangle2d(cx2, y, x2 - cx2, area.getHeight()));
        }
    } else if (intersectsLeft && intersectsRight) {
        // Intersects three sides (even if the perpendicular one may only have the top and bottom point intersected), we have one rectangle
        if (intersectsTop) {
            // Bottom section
            areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
        } else if (intersectsBottom) {
            // Top section
            areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
        } else {
            // Intersects two parallel sides, we have two rectangles
            // Top section
            areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
            // Bottom section
            areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
        }
    } else // Intersects two perpendicular sides, we have two rectangles
    if (intersectsTop && intersectsLeft) {
        // Bottom section
        areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
        // Right section
        areas.add(new Rectangle2d(cx2, y, x2 - cx2, cy2 - y));
    } else if (intersectsTop && intersectsRight) {
        // Left section
        areas.add(new Rectangle2d(x, y, cx - x, cy2 - y));
        // Bottom section
        areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
    } else if (intersectsBottom && intersectsLeft) {
        // Top section
        areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
        // Right section
        areas.add(new Rectangle2d(cx2, cy, x2 - cx2, y2 - cy));
    } else if (intersectsBottom && intersectsRight) {
        // Top section
        areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
        // Left section
        areas.add(new Rectangle2d(x, cy, cx - x, y2 - cy));
    } else // Intersects a single side, we have three rectangles
    if (intersectsTop) {
        // Left section
        areas.add(new Rectangle2d(x, y, cx - x, cy2 - y));
        // Bottom section
        areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
        // Right section
        areas.add(new Rectangle2d(cx2, y, x2 - cx2, cy2 - y));
    } else if (intersectsLeft) {
        // Top section
        areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
        // Bottom section
        areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
        // Right section
        areas.add(new Rectangle2d(cx2, cy, x2 - cx2, coveredArea.getHeight()));
    } else if (intersectsBottom) {
        // Top section
        areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
        // Left section
        areas.add(new Rectangle2d(x, cy, cx - x, y2 - cy));
        // Right section
        areas.add(new Rectangle2d(cx2, cy, x2 - cx2, y2 - cy));
    } else if (intersectsRight) {
        // Top section
        areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
        // Left section
        areas.add(new Rectangle2d(x, cy, cx - x, coveredArea.getHeight()));
        // Bottom section
        areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
    } else {
        // The covered area is entirely contained by the main area, we have four rectangles
        // Top section
        areas.add(new Rectangle2d(x, y, area.getWidth(), cy - y));
        // Left section
        areas.add(new Rectangle2d(x, cy, cx - x, coveredArea.getHeight()));
        // Bottom section
        areas.add(new Rectangle2d(x, cy2, area.getWidth(), y2 - cy2));
        // Right section
        areas.add(new Rectangle2d(cx2, cy, x2 - cx2, coveredArea.getHeight()));
    }
    return areas;
}
Also used : Rectangle2d(net.minecraft.client.renderer.Rectangle2d) ArrayList(java.util.ArrayList)

Example 9 with Rectangle2d

use of net.minecraft.client.renderer.Rectangle2d in project Chisel by Chisel-Team.

the class GuiChisel method init.

@Override
public void init() {
    super.init();
    int id = 0;
    Rectangle2d area = getModeButtonArea();
    int buttonsPerRow = area.getWidth() / 20;
    int padding = (area.getWidth() - (buttonsPerRow * 20)) / buttonsPerRow;
    IChiselMode currentMode = NBTUtil.getChiselMode(this.getContainer().getChisel());
    for (IChiselMode mode : CarvingUtils.getModeRegistry().getAllModes()) {
        if (((IChiselItem) this.getContainer().getChisel().getItem()).supportsMode(player, this.getContainer().getChisel(), mode)) {
            int x = area.getX() + (padding / 2) + ((id % buttonsPerRow) * (20 + padding));
            int y = area.getY() + ((id / buttonsPerRow) * (20 + padding));
            ButtonChiselMode button = new ButtonChiselMode(x, y, mode, b -> {
                b.active = false;
                IChiselMode m = ((ButtonChiselMode) b).getMode();
                NBTUtil.setChiselMode(this.getContainer().getChisel(), m);
                Chisel.network.sendToServer(new PacketChiselMode(this.getContainer().getChiselSlot(), m));
                for (Widget other : buttons) {
                    if (other != b && other instanceof ButtonChiselMode) {
                        // TODO see if Button.enabled == Button.active
                        other.active = true;
                    }
                }
            });
            if (mode == currentMode) {
                // TODO see if Button.enabled == Button.active
                button.active = false;
            }
            addButton(button);
            id++;
        }
    }
}
Also used : IChiselItem(team.chisel.api.IChiselItem) IChiselMode(team.chisel.api.carving.IChiselMode) PacketChiselMode(team.chisel.common.item.PacketChiselMode) Rectangle2d(net.minecraft.client.renderer.Rectangle2d) Widget(net.minecraft.client.gui.widget.Widget)

Aggregations

Rectangle2d (net.minecraft.client.renderer.Rectangle2d)9 ArrayList (java.util.ArrayList)5 NotNull (org.jetbrains.annotations.NotNull)4 ItemStack (net.minecraft.item.ItemStack)3 List (java.util.List)2 Minecraft (net.minecraft.client.Minecraft)2 Slot (net.minecraft.inventory.container.Slot)2 Int2ObjectLinkedOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap)1 Entry (it.unimi.dsi.fastutil.ints.Int2ObjectMap.Entry)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 GuiWindow (mekanism.client.gui.element.window.GuiWindow)1 IJEIGhostTarget (mekanism.client.jei.interfaces.IJEIGhostTarget)1 LRU (mekanism.common.lib.collection.LRU)1 IModPlugin (mezz.jei.api.IModPlugin)1 JeiPlugin (mezz.jei.api.JeiPlugin)1 VanillaRecipeCategoryUid (mezz.jei.api.constants.VanillaRecipeCategoryUid)1 IGhostIngredientHandler (mezz.jei.api.gui.handlers.IGhostIngredientHandler)1 IGuiContainerHandler (mezz.jei.api.gui.handlers.IGuiContainerHandler)1 IStackHelper (mezz.jei.api.helpers.IStackHelper)1 ISubtypeInterpreter (mezz.jei.api.ingredients.subtypes.ISubtypeInterpreter)1