Skip to content

Commit

Permalink
Add ghost item icons
Browse files Browse the repository at this point in the history
Closes #228.
  • Loading branch information
Juuxel committed Nov 26, 2023
1 parent f5d46de commit c821113
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -15,7 +16,11 @@
* @since 2.2.0
*/
public class ItemIcon implements Icon {
// Matches the vanilla RecipeBookGhostSlots class (1.20.2).
private static final int GHOST_OVERLAY_COLOR = 0x30_FFFFFF;

private final ItemStack stack;
private boolean ghost = false;

/**
* Constructs an item icon.
Expand Down Expand Up @@ -47,6 +52,34 @@ public void paint(DrawContext context, int x, int y, int size) {
matrices.translate(x, y, 0);
matrices.scale(scale, scale, 1);
context.drawItemWithoutEntity(stack, 0, 0);

if (isGhost()) {
context.fill(RenderLayer.getGuiGhostRecipeOverlay(), 0, 0, 16, 16, GHOST_OVERLAY_COLOR);
}

matrices.pop();
}

/**
* Checks whether this icon is a ghost item.
* Ghost items are rendered with a pale overlay.
*
* @return {@code true} if this icon is a ghost item, {@code false} otherwise
* @since 9.2.0
*/
public boolean isGhost() {
return ghost;
}

/**
* Marks this icon as a ghost or non-ghost icon.
*
* @param ghost {@code true} if this icon is a ghost item, {@code false} otherwise
* @return this icon
* @since 9.2.0
*/
public ItemIcon setGhost(boolean ghost) {
this.ghost = ghost;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.cottonmc.test.client;

import net.minecraft.item.Items;
import net.minecraft.text.Text;

import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WToggleButton;
import io.github.cottonmc.cotton.gui.widget.icon.ItemIcon;

public class GhostIconTestGui extends LightweightGuiDescription {
public GhostIconTestGui() {
WGridPanel root = (WGridPanel) rootPanel;
root.setGaps(2, 2);

ItemIcon icon = new ItemIcon(Items.CACTUS);
WButton button = new WButton(icon, Text.literal("Hello world"));
WToggleButton ghostToggle = new WToggleButton(Text.literal("Ghost"));
ghostToggle.setOnToggle(icon::setGhost);

root.add(button, 0, 0, 5, 1);
root.add(ghostToggle, 0, 1, 5, 1);
root.validate(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void onInitializeClient() {
.then(literal("texture").executes(openScreen(client -> new TextureTestGui())))
.then(literal("textalignment").executes(openScreen(client -> new TextAlignmentTestGui())))
.then(literal("list").executes(openScreen(client -> new ListTestGui())))
.then(literal("ghosticon").executes(openScreen(client -> new GhostIconTestGui())))
));
}

Expand Down

0 comments on commit c821113

Please sign in to comment.