Skip to content

Commit

Permalink
Add IDrawableBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Jul 2, 2018
1 parent fd7b2fe commit e9be366
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 73 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -4,5 +4,5 @@ mcp_mappings=snapshot_20180421
curse_project_id=238222

version_major=4
version_minor=10
version_minor=11
version_patch=0
41 changes: 34 additions & 7 deletions src/api/java/mezz/jei/api/IGuiHelper.java
Expand Up @@ -3,6 +3,7 @@
import mezz.jei.api.gui.ICraftingGridHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IDrawableAnimated;
import mezz.jei.api.gui.IDrawableBuilder;
import mezz.jei.api.gui.IDrawableStatic;
import mezz.jei.api.gui.ITickTimer;
import net.minecraft.util.ResourceLocation;
Expand All @@ -12,19 +13,20 @@
* Get the instance from {@link IJeiHelpers#getGuiHelper()}.
*/
public interface IGuiHelper {

/**
* Create a drawable from part of a standard 256x256 gui texture.
*/
IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height);
default IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height) {
return drawableBuilder(resourceLocation, u, v, width, height).build();
}

/**
* Create a drawable from part of an arbitrary sized texture.
* @since JEI 4.0.1
* Create a {@link IDrawableBuilder} which gives more control over drawable creation.
*
* @return a new {@link IDrawableBuilder} with the given resource location
* @since JEI 4.11.0
*/
IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int textureWidth, int textureHeight);

IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int paddingTop, int paddingBottom, int paddingLeft, int paddingRight);
IDrawableBuilder drawableBuilder(ResourceLocation resourceLocation, int u, int v, int width, int height);

/**
* Creates an animated texture for a gui, revealing the texture over time.
Expand Down Expand Up @@ -66,4 +68,29 @@ public interface IGuiHelper {
* @param countDown if true, the tick timer will count backwards from maxValue
*/
ITickTimer createTickTimer(int ticksPerCycle, int maxValue, boolean countDown);

/**
* Create a drawable from part of an arbitrary sized texture.
* @since JEI 4.0.1
* @deprecated since JEI 4.11.0. Use {@link #drawableBuilder(ResourceLocation, int, int, int, int)}
*/
@Deprecated
default IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int textureWidth, int textureHeight) {
return drawableBuilder(resourceLocation, u, v, width, height)
.setTextureSize(textureWidth, textureHeight)
.build();
}

/**
* Create a drawable from part of a 256x256 texture.
* Padding arguments create extra space around the texture.
* @deprecated since JEI 4.11.0. Use {@link #drawableBuilder(ResourceLocation, int, int, int, int)}
*/
@Deprecated
default IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int paddingTop, int paddingBottom, int paddingLeft, int paddingRight) {
return drawableBuilder(resourceLocation, u, v, width, height)
.addPadding(paddingTop, paddingBottom, paddingLeft, paddingRight)
.build();
}

}
43 changes: 43 additions & 0 deletions src/api/java/mezz/jei/api/gui/IDrawableBuilder.java
@@ -0,0 +1,43 @@
package mezz.jei.api.gui;

import mezz.jei.api.IGuiHelper;
import net.minecraft.util.ResourceLocation;

/**
* Builder for creating drawables from a resource location.
* Create an instance with {@link IGuiHelper#drawableBuilder(ResourceLocation, int, int, int, int)}
* @since JEI 4.11.0
*/
public interface IDrawableBuilder {
/**
* For textures that are not 256x256, specify the size.
*/
IDrawableBuilder setTextureSize(int width, int height);

/**
* Add extra blank space around the texture by adjusting the padding.
*/
IDrawableBuilder addPadding(int paddingTop, int paddingBottom, int paddingLeft, int paddingRight);

/**
* Creates a normal, non-animated drawable.
*/
IDrawableStatic build();

/**
* Creates an animated texture for a gui, revealing the texture over time.
*
* @param ticksPerCycle the number of ticks for the animation to run before starting over
* @param startDirection the direction that the animation starts drawing the texture
* @param inverted when inverted is true, the texture will start fully drawn and be hidden over time
*/
IDrawableAnimated buildAnimated(int ticksPerCycle, IDrawableAnimated.StartDirection startDirection, boolean inverted);

/**
* Creates an animated texture for a gui, revealing the texture over time.
*
* @param tickTimer a custom tick timer, used for advanced control over the animation
* @param startDirection the direction that the animation starts drawing the texture
*/
IDrawableAnimated buildAnimated(ITickTimer tickTimer, IDrawableAnimated.StartDirection startDirection);
}
64 changes: 14 additions & 50 deletions src/main/java/mezz/jei/gui/GuiHelper.java
Expand Up @@ -4,15 +4,16 @@
import mezz.jei.api.gui.ICraftingGridHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IDrawableAnimated;
import mezz.jei.api.gui.IDrawableBuilder;
import mezz.jei.api.gui.IDrawableStatic;
import mezz.jei.api.gui.ITickTimer;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.config.Constants;
import mezz.jei.gui.elements.DrawableAnimated;
import mezz.jei.gui.elements.DrawableBlank;
import mezz.jei.gui.elements.DrawableBuilder;
import mezz.jei.gui.elements.DrawableIngredient;
import mezz.jei.gui.elements.DrawableResource;
import mezz.jei.util.ErrorUtil;
import net.minecraft.util.ResourceLocation;

Expand All @@ -22,7 +23,6 @@ public class GuiHelper implements IGuiHelper {
private final IDrawableStatic tabSelected;
private final IDrawableStatic tabUnselected;
private final IDrawableStatic shapelessIcon;
private final IDrawableStatic infoIcon;
private final IDrawableStatic arrowPrevious;
private final IDrawableStatic arrowNext;
private final IDrawableStatic recipeTransfer;
Expand All @@ -35,60 +35,28 @@ public GuiHelper(IIngredientRegistry ingredientRegistry) {
tabUnselected = createDrawable(Constants.RECIPE_BACKGROUND, 220, 15, 24, 22);

shapelessIcon = createDrawable(Constants.RECIPE_BACKGROUND, 196, 0, 16, 15);
infoIcon = createDrawable(Constants.RECIPE_BACKGROUND, 212, 39, 16, 16);

arrowPrevious = createDrawable(Constants.RECIPE_BACKGROUND, 196, 55, 5, 8, 1, 0, 0, 0);
arrowNext = createDrawable(Constants.RECIPE_BACKGROUND, 204, 55, 5, 8, 1, 0, 1, 0);
recipeTransfer = createDrawable(Constants.RECIPE_BACKGROUND, 212, 55, 6, 6, 1, 0, 1, 0);
arrowPrevious = drawableBuilder(Constants.RECIPE_BACKGROUND, 196, 55, 5, 8)
.addPadding(1, 0, 0, 0)
.build();
arrowNext = drawableBuilder(Constants.RECIPE_BACKGROUND, 204, 55, 5, 8)
.addPadding(1, 0, 1, 0)
.build();
recipeTransfer = drawableBuilder(Constants.RECIPE_BACKGROUND, 212, 55, 6, 6)
.addPadding(1, 0, 1, 0)
.build();
}

@Override
public IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height) {
ErrorUtil.checkNotNull(resourceLocation, "resourceLocation");

return new DrawableResource(resourceLocation, u, v, width, height, 0, 0, 0, 0, 256, 256);
}

@Override
public IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int textureWidth, int textureHeight) {
ErrorUtil.checkNotNull(resourceLocation, "resourceLocation");

return new DrawableResource(resourceLocation, u, v, width, height, 0, 0, 0, 0, textureWidth, textureHeight);
}

@Override
public IDrawableStatic createDrawable(ResourceLocation resourceLocation, int u, int v, int width, int height, int paddingTop, int paddingBottom, int paddingLeft, int paddingRight) {
ErrorUtil.checkNotNull(resourceLocation, "resourceLocation");

return new DrawableResource(resourceLocation, u, v, width, height, paddingTop, paddingBottom, paddingLeft, paddingRight, 256, 256);
public IDrawableBuilder drawableBuilder(ResourceLocation resourceLocation, int u, int v, int width, int height) {
return new DrawableBuilder(resourceLocation, u, v, width, height);
}

@Override
public IDrawableAnimated createAnimatedDrawable(IDrawableStatic drawable, int ticksPerCycle, IDrawableAnimated.StartDirection startDirection, boolean inverted) {
ErrorUtil.checkNotNull(drawable, "drawable");
ErrorUtil.checkNotNull(startDirection, "startDirection");

IDrawableAnimated.StartDirection animationStartDirection = startDirection;
if (inverted) {
if (startDirection == IDrawableAnimated.StartDirection.TOP) {
animationStartDirection = IDrawableAnimated.StartDirection.BOTTOM;
} else if (startDirection == IDrawableAnimated.StartDirection.BOTTOM) {
animationStartDirection = IDrawableAnimated.StartDirection.TOP;
} else if (startDirection == IDrawableAnimated.StartDirection.LEFT) {
animationStartDirection = IDrawableAnimated.StartDirection.RIGHT;
} else {
animationStartDirection = IDrawableAnimated.StartDirection.LEFT;
}
}

int tickTimerMaxValue;
if (animationStartDirection == IDrawableAnimated.StartDirection.TOP || animationStartDirection == IDrawableAnimated.StartDirection.BOTTOM) {
tickTimerMaxValue = drawable.getHeight();
} else {
tickTimerMaxValue = drawable.getWidth();
}
ITickTimer tickTimer = createTickTimer(ticksPerCycle, tickTimerMaxValue, !inverted);
return new DrawableAnimated(drawable, tickTimer, animationStartDirection);
return new DrawableAnimated(drawable, ticksPerCycle, startDirection, inverted);
}

@Override
Expand Down Expand Up @@ -129,10 +97,6 @@ public IDrawableStatic getShapelessIcon() {
return shapelessIcon;
}

public IDrawableStatic getInfoIcon() {
return infoIcon;
}

public IDrawableStatic getArrowPrevious() {
return arrowPrevious;
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/mezz/jei/gui/elements/DrawableAnimated.java
Expand Up @@ -3,13 +3,39 @@
import mezz.jei.api.gui.IDrawableAnimated;
import mezz.jei.api.gui.IDrawableStatic;
import mezz.jei.api.gui.ITickTimer;
import mezz.jei.gui.TickTimer;
import net.minecraft.client.Minecraft;

public class DrawableAnimated implements IDrawableAnimated {
private final IDrawableStatic drawable;
private final ITickTimer tickTimer;
private final StartDirection startDirection;

public DrawableAnimated(IDrawableStatic drawable, int ticksPerCycle, IDrawableAnimated.StartDirection startDirection, boolean inverted) {
IDrawableAnimated.StartDirection animationStartDirection = startDirection;
if (inverted) {
if (startDirection == IDrawableAnimated.StartDirection.TOP) {
animationStartDirection = IDrawableAnimated.StartDirection.BOTTOM;
} else if (startDirection == IDrawableAnimated.StartDirection.BOTTOM) {
animationStartDirection = IDrawableAnimated.StartDirection.TOP;
} else if (startDirection == IDrawableAnimated.StartDirection.LEFT) {
animationStartDirection = IDrawableAnimated.StartDirection.RIGHT;
} else {
animationStartDirection = IDrawableAnimated.StartDirection.LEFT;
}
}

int tickTimerMaxValue;
if (animationStartDirection == IDrawableAnimated.StartDirection.TOP || animationStartDirection == IDrawableAnimated.StartDirection.BOTTOM) {
tickTimerMaxValue = drawable.getHeight();
} else {
tickTimerMaxValue = drawable.getWidth();
}
this.drawable = drawable;
this.tickTimer = new TickTimer(ticksPerCycle, tickTimerMaxValue, !inverted);
this.startDirection = animationStartDirection;
}

public DrawableAnimated(IDrawableStatic drawable, ITickTimer tickTimer, StartDirection startDirection) {
this.drawable = drawable;
this.tickTimer = tickTimer;
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/mezz/jei/gui/elements/DrawableBuilder.java
@@ -0,0 +1,65 @@
package mezz.jei.gui.elements;

import mezz.jei.api.gui.IDrawableAnimated;
import mezz.jei.api.gui.IDrawableBuilder;
import mezz.jei.api.gui.IDrawableStatic;
import mezz.jei.api.gui.ITickTimer;
import mezz.jei.util.ErrorUtil;
import net.minecraft.util.ResourceLocation;

public class DrawableBuilder implements IDrawableBuilder {
private final ResourceLocation resourceLocation;
private final int u;
private final int v;
private final int width;
private final int height;
private int textureWidth = 256;
private int textureHeight = 256;
private int paddingTop = 0;
private int paddingBottom = 0;
private int paddingLeft = 0;
private int paddingRight = 0;

public DrawableBuilder(ResourceLocation resourceLocation, int u, int v, int width, int height) {
ErrorUtil.checkNotNull(resourceLocation, "resourceLocation");
this.u = u;
this.v = v;
this.width = width;
this.height = height;
this.resourceLocation = resourceLocation;
}

@Override
public IDrawableBuilder setTextureSize(int width, int height) {
this.textureWidth = width;
this.textureHeight = height;
return this;
}

@Override
public IDrawableBuilder addPadding(int paddingTop, int paddingBottom, int paddingLeft, int paddingRight) {
this.paddingTop = paddingTop;
this.paddingBottom = paddingBottom;
this.paddingLeft = paddingLeft;
this.paddingRight = paddingRight;
return this;
}

@Override
public IDrawableStatic build() {
return new DrawableResource(resourceLocation, u, v, width, height, paddingTop, paddingBottom, paddingLeft, paddingRight, textureWidth, textureHeight);
}

@Override
public IDrawableAnimated buildAnimated(int ticksPerCycle, IDrawableAnimated.StartDirection startDirection, boolean inverted) {
ErrorUtil.checkNotNull(startDirection, "startDirection");
IDrawableStatic drawable = build();
return new DrawableAnimated(drawable, ticksPerCycle, startDirection, inverted);
}

@Override
public IDrawableAnimated buildAnimated(ITickTimer tickTimer, IDrawableAnimated.StartDirection startDirection) {
IDrawableStatic drawable = build();
return new DrawableAnimated(drawable, tickTimer, startDirection);
}
}
Expand Up @@ -38,8 +38,12 @@ public DebugRecipeCategory(IGuiHelper guiHelper) {
this.localizedName = "debug";

ResourceLocation backgroundTexture = Constants.RECIPE_BACKGROUND;
this.tankBackground = guiHelper.createDrawable(backgroundTexture, 220, 196, 18, 60, -1, -1, -1, -1);
this.tankOverlay = guiHelper.createDrawable(backgroundTexture, 238, 196, 18, 60, -1, -1, -1, -1);
this.tankBackground = guiHelper.drawableBuilder(backgroundTexture, 220, 196, 18, 60)
.addPadding(-1, -1, -1, -1)
.build();
this.tankOverlay = guiHelper.drawableBuilder(backgroundTexture, 238, 196, 18, 60)
.addPadding(-1, -1, -1, -1)
.build();
}

@Override
Expand Down
Expand Up @@ -17,7 +17,9 @@ public class AnvilRecipeCategory implements IRecipeCategory<AnvilRecipeWrapper>
private final IDrawable icon;

public AnvilRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.createDrawable(Constants.RECIPE_GUI_VANILLA, 0, 168, 125, 18, 0, 20, 0, 0);
background = guiHelper.drawableBuilder(Constants.RECIPE_GUI_VANILLA, 0, 168, 125, 18)
.addPadding(0, 20, 0, 0)
.build();
icon = guiHelper.createDrawableIngredient(new ItemStack(Blocks.ANVIL));
}

Expand Down
Expand Up @@ -11,7 +11,6 @@
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.config.Constants;
import mezz.jei.gui.elements.DrawableAnimated;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Items;
Expand All @@ -36,16 +35,18 @@ public class BrewingRecipeCategory implements IRecipeCategory<BrewingRecipeWrapp

public BrewingRecipeCategory(IGuiHelper guiHelper) {
ResourceLocation location = Constants.RECIPE_GUI_VANILLA;
background = guiHelper.createDrawable(location, 0, 0, 64, 60, 1, 0, 0, 50);
background = guiHelper.drawableBuilder(location, 0, 0, 64, 60)
.addPadding(1, 0, 0, 50)
.build();
icon = guiHelper.createDrawableIngredient(new ItemStack(Items.BREWING_STAND));
localizedName = Translator.translateToLocal("gui.jei.category.brewing");

IDrawableStatic brewArrowDrawable = guiHelper.createDrawable(location, 64, 0, 9, 28);
arrow = guiHelper.createAnimatedDrawable(brewArrowDrawable, 400, IDrawableAnimated.StartDirection.TOP, false);
arrow = guiHelper.drawableBuilder(location, 64, 0, 9, 28)
.buildAnimated(400, IDrawableAnimated.StartDirection.TOP, false);

IDrawableStatic brewBubblesDrawable = guiHelper.createDrawable(location, 73, 0, 12, 29);
ITickTimer bubblesTickTimer = new BrewingBubblesTickTimer(guiHelper);
bubbles = new DrawableAnimated(brewBubblesDrawable, bubblesTickTimer, IDrawableAnimated.StartDirection.BOTTOM);
bubbles = guiHelper.drawableBuilder(location, 73, 0, 12, 29)
.buildAnimated(bubblesTickTimer, IDrawableAnimated.StartDirection.BOTTOM);

blazeHeat = guiHelper.createDrawable(location, 64, 29, 18, 4);

Expand Down

0 comments on commit e9be366

Please sign in to comment.