Skip to content

Commit

Permalink
feat: Ability to group the bulk render output by Item Tab or Block/It…
Browse files Browse the repository at this point in the history
…em split

---

Signed-off-by: AterAnimAvis <AterAnimAvis@gmail.com>
  • Loading branch information
AterAnimAvis committed Dec 26, 2020
1 parent 4d91b13 commit a567171
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"block_renderer.gui.switch.bulk": "Bulk Render",
"block_renderer.gui.switch.bulk.tooltip": "Switch to Bulk Render",

"block_renderer.gui.group": "Group Bulk Renders",
"block_renderer.gui.group.tooltip.0": "No Grouping",
"block_renderer.gui.group.tooltip.1": "Group by Tab",
"block_renderer.gui.group.tooltip.2": "Group by Item / Block",

"block_renderer.gui.rendering":"Rendering %1$s entries from %2$s",
"block_renderer.gui.rendered":"Rendered %1$s entries from %2$s",
"block_renderer.gui.renderCancelled":"Operation cancelled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.unascribed.blockrenderer.fabric.client.varia.StringUtils;
import com.unascribed.blockrenderer.render.request.lambda.ImageHandler;
import com.unascribed.blockrenderer.varia.Files;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.LiteralText;
Expand All @@ -14,8 +16,15 @@

public class DefaultPngItemStackHandler extends BaseItemStackHandler implements ImageHandler<ItemStack>, Runnable {

private final int grouped;

public DefaultPngItemStackHandler(File folder, int size, boolean useIdentifier, boolean addSize, boolean addDate) {
this(folder, size, useIdentifier, addSize, addDate, 0);
}

public DefaultPngItemStackHandler(File folder, int size, boolean useIdentifier, boolean addSize, boolean addDate, int grouped) {
super(folder, size, useIdentifier, addSize, addDate);
this.grouped = grouped;
}

@Override
Expand All @@ -37,4 +46,20 @@ public void run() {
StringUtils.addMessage(new LiteralText("> Finished Rendering").setStyle(open));
}

@Override
protected String getFilename(ItemStack value) {
String result = super.getFilename(value);

switch (grouped) {
case 1:
ItemGroup group = value.getItem().getGroup();
if (group == null) return result;
return StringUtils.sanitize(group.getName()) + "/" + result;
case 2:
return (value.getItem() instanceof BlockItem ? "blocks" : "items") + "/" + result;
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static IRequest single(ItemStack stack, MapState data, int size, boolean
);
}

public static IRequest bulk(String spec, int size, boolean useId, boolean addSize) {
public static IRequest bulk(String spec, int size, boolean useId, boolean addSize, int grouped) {
Set<String> namespaces = StringUtils.getNamespaces(spec);
List<ItemStack> renders = MiscUtils.collectStacks(namespaces);
String joined = Joiner.on(", ").join(namespaces);
Expand All @@ -54,7 +54,7 @@ public static IRequest bulk(String spec, int size, boolean useId, boolean addSiz
File folder = new File(Files.DEFAULT_FOLDER, StringUtils.dateTime() + "_" + sizeString + StringUtils.sanitize(joined) + "/");

//TODO: Split out into BulkPngItemStackHandler
DefaultPngItemStackHandler handler = new DefaultPngItemStackHandler(folder, size, useId, false, false);
DefaultPngItemStackHandler handler = new DefaultPngItemStackHandler(folder, size, useId, false, false, grouped);

return new BulkRenderingRequest<>(
new ItemStackRenderer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
package com.unascribed.blockrenderer.fabric.client.screens.item;


import com.unascribed.blockrenderer.fabric.client.render.RenderManager;
import com.unascribed.blockrenderer.fabric.client.render.item.ItemRenderer;
import com.unascribed.blockrenderer.fabric.client.screens.widgets.HoverableTinyButtonWidget;
import com.unascribed.blockrenderer.fabric.client.screens.widgets.ItemButtonMultiWidget;
import com.unascribed.blockrenderer.fabric.client.varia.Registries;
import com.unascribed.blockrenderer.fabric.client.varia.StringUtils;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.TranslatableText;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.function.Supplier;

/*
* Note: Screen's get initialized in init
*/
@SuppressWarnings("NotNullFieldNotInitialized")
public class EnterNamespaceScreen extends BaseItemScreen {

private static final int UN_GROUPED = 0; // Empty Map
private static final int GROUP_BY_TAB = 1; // Banner Pattern
private static final int GROUP_BY_TYPE = 2; // Block

private static final TranslatableText TITLE = new TranslatableText("block_renderer.gui.namespace");

private boolean emptySpec = false;

private final String prefill;

private TextFieldWidget text;
private ItemButtonMultiWidget grouped;

private final @Nullable ItemStack stack;

Expand Down Expand Up @@ -69,6 +79,38 @@ public void init() {
);
}

final Supplier<ItemStack> EMPTY_MAP = Registries.mapLazy(Registries.EMPTY_MAP, Item::getDefaultStack);
final Supplier<ItemStack> PATTERN = Registries.mapLazy(Registries.PATTERN, Item::getDefaultStack);
final Supplier<ItemStack> DISPENSER = Registries.mapLazy(Registries.DISPENSER, Item::getDefaultStack);

grouped = addButton(new ItemButtonMultiWidget(
this,
itemRenderer,
(state) -> {
switch (state) {
case UN_GROUPED:
return EMPTY_MAP.get();
case GROUP_BY_TAB:
return PATTERN.get();
case GROUP_BY_TYPE:
return DISPENSER.get();
default:
throw new RuntimeException("Unsupported Group Type");
}
},
12,
height - 32,
new TranslatableText("block_renderer.gui.group"),
(state) -> {
if (state < 0 || state > 2) throw new RuntimeException("Unsupported Group Type");
return Collections.singletonList(new TranslatableText("block_renderer.gui.group.tooltip." + state));
},
button -> {
grouped.state += 1;
if (grouped.state > GROUP_BY_TYPE) grouped.state = UN_GROUPED;
}
), enabled);

super.init();

renderButton.visible = !emptySpec;
Expand Down Expand Up @@ -120,6 +162,6 @@ public void onRender(ButtonWidget button) {
client.openScreen(old);
if (client.world == null) return;

RenderManager.push(ItemRenderer.bulk(text.getText(), round(size), useId.isChecked(), addSize.isChecked()));
RenderManager.push(ItemRenderer.bulk(text.getText(), round(size), useId.isChecked(), addSize.isChecked(), grouped.state));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.unascribed.blockrenderer.fabric.client.screens.widgets;

import com.unascribed.blockrenderer.fabric.client.varia.rendering.Display;
import com.unascribed.blockrenderer.fabric.client.varia.rendering.GL;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;

public class ItemButtonMultiWidget extends ButtonWidget {

private final Screen owner;
private final Function<Integer, List<Text>> tooltip;
private final ItemRenderer renderer;
private final Function<Integer, ItemStack> stack;
public int state = 0;

public ItemButtonMultiWidget(Screen owner, ItemRenderer renderer, Function<Integer, ItemStack> stack, int x, int y, Text message, Text tooltip, PressAction onPress) {
this(owner, renderer, stack, x, y, message, (state) -> Collections.singletonList(tooltip), onPress);
}

public ItemButtonMultiWidget(Screen owner, ItemRenderer renderer, Function<Integer, ItemStack> stack, int x, int y, Text message, Function<Integer, List<Text>> tooltip, PressAction onPress) {
super(x, y, 20, 20, message, onPress);
this.tooltip = tooltip;
this.owner = owner;
this.renderer = renderer;
this.stack = stack;
}

@Override
public void renderButton(MatrixStack matrix, int mouseX, int mouseY, float partialTicks) {

if (isHovered() && active)
Display.drawRect(matrix, x, y, x + width, y + height, 0x33FFFFFF);

renderItemStack(stack.apply(state), x, y, 1.25f);

if (isHovered())
renderToolTip(matrix, mouseX, mouseY);
}

private void renderItemStack(ItemStack stack, int x, int y, float scale) {
int BASE_Z_LEVEL = 100;

GL.pushMatrix();

GL.translate(x, y, 32.0f);
GL.scaleFixedZLevel(scale, -BASE_Z_LEVEL);

renderer.zOffset = -BASE_Z_LEVEL / 2f;
renderer.renderInGuiWithOverrides(stack, 0, 0);
renderer.zOffset = 0.0F;
GL.popMatrix();

}

@Override
public void renderToolTip(MatrixStack stack, int mouseX, int mouseY) {
Display.renderTooltip(owner, stack, tooltip.apply(state), mouseX, mouseY);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

public interface Registries {

Lazy<Item> MAP = new Lazy<>(() -> lookupItem(new Identifier("minecraft:filled_map")));
Lazy<Item> DISPENSER = new Lazy<>(() -> lookupItem(new Identifier("minecraft:dispenser")));
Lazy<Item> CUTTER = new Lazy<>(() -> lookupItem(new Identifier("minecraft:stonecutter")));
Lazy<Item> DISPENSER = new Lazy<>(() -> lookupItem(new Identifier("minecraft:dispenser")));
Lazy<Item> EMPTY_MAP = new Lazy<>(() -> lookupItem(new Identifier("minecraft:map")));
Lazy<Item> MAP = new Lazy<>(() -> lookupItem(new Identifier("minecraft:filled_map")));
Lazy<Item> PATTERN = new Lazy<>(() -> lookupItem(new Identifier("minecraft:mojang_banner_pattern")));

static Item lookupItem(Identifier identifier) {
return Registry.ITEM.get(identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.unascribed.blockrenderer.forge.client.varia.StringUtils;
import com.unascribed.blockrenderer.render.request.lambda.ImageHandler;
import com.unascribed.blockrenderer.varia.Files;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.Style;
Expand All @@ -14,8 +16,15 @@

public class DefaultPngItemStackHandler extends BaseItemStackHandler implements ImageHandler<ItemStack>, Runnable {

private final int grouped;

public DefaultPngItemStackHandler(File folder, int size, boolean useIdentifier, boolean addSize, boolean addDate) {
this(folder, size, useIdentifier, addSize, addDate, 0);
}

public DefaultPngItemStackHandler(File folder, int size, boolean useIdentifier, boolean addSize, boolean addDate, int grouped) {
super(folder, size, useIdentifier, addSize, addDate);
this.grouped = grouped;
}

@Override
Expand All @@ -37,4 +46,20 @@ public void run() {
StringUtils.addMessage(new StringTextComponent("> Finished Rendering").setStyle(open));
}

@Override
protected String getFilename(ItemStack value) {
String result = super.getFilename(value);

switch (grouped) {
case 1:
ItemGroup group = value.getItem().getGroup();
if (group == null) return result;
return StringUtils.sanitize(group.getPath()) + "/" + result;
case 2:
return (value.getItem() instanceof BlockItem ? "blocks" : "items") + "/" + result;
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static IRequest single(ItemStack stack, MapData data, int size, boolean u
);
}

public static IRequest bulk(String spec, int size, boolean useId, boolean addSize) {
public static IRequest bulk(String spec, int size, boolean useId, boolean addSize, int grouped) {
Set<String> namespaces = StringUtils.getNamespaces(spec);
List<ItemStack> renders = MiscUtils.collectStacks(namespaces);
String joined = Joiner.on(", ").join(namespaces);
Expand All @@ -54,7 +54,7 @@ public static IRequest bulk(String spec, int size, boolean useId, boolean addSiz
File folder = new File(Files.DEFAULT_FOLDER, StringUtils.dateTime() + "_" + sizeString + StringUtils.sanitize(joined) + "/");

//TODO: Split out into BulkPngItemStackHandler
DefaultPngItemStackHandler handler = new DefaultPngItemStackHandler(folder, size, useId, false, false);
DefaultPngItemStackHandler handler = new DefaultPngItemStackHandler(folder, size, useId, false, false, grouped);

return new BulkRenderingRequest<>(
new ItemStackRenderer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@
import com.unascribed.blockrenderer.forge.client.render.RenderManager;
import com.unascribed.blockrenderer.forge.client.render.item.ItemRenderer;
import com.unascribed.blockrenderer.forge.client.screens.widgets.HoverableTinyButtonWidget;
import com.unascribed.blockrenderer.forge.client.screens.widgets.ItemButtonMultiWidget;
import com.unascribed.blockrenderer.forge.client.varia.Registries;
import com.unascribed.blockrenderer.forge.client.varia.StringUtils;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TranslationTextComponent;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.function.Supplier;

/*
* Note: Screen's get initialized in init
*/
@SuppressWarnings("NotNullFieldNotInitialized")
public class EnterNamespaceScreen extends BaseItemScreen {

private static final int UN_GROUPED = 0; // Empty Map
private static final int GROUP_BY_TAB = 1; // Banner Pattern
private static final int GROUP_BY_TYPE = 2; // Block

private static final TranslationTextComponent TITLE = new TranslationTextComponent("block_renderer.gui.namespace");

private boolean emptySpec = false;

private final String prefill;

private TextFieldWidget text;
private ItemButtonMultiWidget grouped;

private final @Nullable ItemStack stack;

Expand Down Expand Up @@ -68,6 +79,38 @@ public void init() {
);
}

final Supplier<ItemStack> EMPTY_MAP = Registries.EMPTY_MAP.lazyMap(Item::getDefaultInstance);
final Supplier<ItemStack> PATTERN = Registries.PATTERN.lazyMap(Item::getDefaultInstance);
final Supplier<ItemStack> DISPENSER = Registries.DISPENSER.lazyMap(Item::getDefaultInstance);

grouped = addButton(new ItemButtonMultiWidget(
this,
itemRenderer,
(state) -> {
switch (state) {
case UN_GROUPED:
return EMPTY_MAP.get();
case GROUP_BY_TAB:
return PATTERN.get();
case GROUP_BY_TYPE:
return DISPENSER.get();
default:
throw new RuntimeException("Unsupported Group Type");
}
},
12,
height - 32,
new TranslationTextComponent("block_renderer.gui.group"),
(state) -> {
if (state < 0 || state > 2) throw new RuntimeException("Unsupported Group Type");
return Collections.singletonList(new TranslationTextComponent("block_renderer.gui.group.tooltip." + state));
},
button -> {
grouped.state += 1;
if (grouped.state > GROUP_BY_TYPE) grouped.state = UN_GROUPED;
}
), enabled);

super.init();

renderButton.visible = !emptySpec;
Expand Down Expand Up @@ -119,6 +162,6 @@ public void onRender(Button button) {
minecraft.displayGuiScreen(old);
if (minecraft.world == null) return;

RenderManager.push(ItemRenderer.bulk(text.getText(), round(size), useId.isChecked(), addSize.isChecked()));
RenderManager.push(ItemRenderer.bulk(text.getText(), round(size), useId.isChecked(), addSize.isChecked(), grouped.state));
}
}
Loading

0 comments on commit a567171

Please sign in to comment.