diff --git a/src/main/java/me/towdium/jecalculation/client/gui/JecGui.java b/src/main/java/me/towdium/jecalculation/client/gui/JecGui.java index 08123f6..1c12d60 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/JecGui.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/JecGui.java @@ -128,7 +128,7 @@ public class JecGui extends GuiContainer { root.onDraw(this, mouseX - guiLeft, mouseY - guiTop); GlStateManager.popMatrix(); GlStateManager.pushMatrix(); - GlStateManager.translate(mouseX - 8, mouseY - 8, 0); + GlStateManager.translate(mouseX - 8, mouseY - 8, 80); hand.drawLabel(this); GlStateManager.popMatrix(); drawBufferedTooltip(); @@ -153,7 +153,7 @@ public class JecGui extends GuiContainer { /** * @return if the event is canceled - * This function handles click outside the normal region, + * This function handles click outside the rNormal region, * especially the overlap with JEI overlay. It handles * mouse event before JEI. */ @@ -277,12 +277,14 @@ public class JecGui extends GuiContainer { Tessellator tessellator = Tessellator.instance; GlStateManager.disableTexture2D(); GlStateManager.color(f, f1, f2, f3); + GlStateManager.disableAlpha(); tessellator.startDrawingQuads(); tessellator.addVertex((double) xPos, (double) bottom, 0.0D); tessellator.addVertex((double) right, (double) bottom, 0.0D); tessellator.addVertex((double) right, (double) yPos, 0.0D); tessellator.addVertex((double) xPos, (double) yPos, 0.0D); tessellator.draw(); + GlStateManager.enableAlpha(); GlStateManager.enableTexture2D(); } @@ -380,9 +382,11 @@ public class JecGui extends GuiContainer { xPos -= 8; yPos -= 8; } + GlStateManager.enableDepth(); RenderHelper.enableGUIStandardItemLighting(); itemRender.renderItemIntoGUI(fontRendererObj, mc.getTextureManager(), is, xPos, yPos); RenderHelper.disableStandardItemLighting(); + GlStateManager.disableDepth(); } public static class Font { diff --git a/src/main/java/me/towdium/jecalculation/client/gui/Resource.java b/src/main/java/me/towdium/jecalculation/client/gui/Resource.java index 6b73927..9da9a12 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/Resource.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/Resource.java @@ -26,6 +26,10 @@ public class Resource { public static final Resource BTN_YES_F = new Resource(84, 0, 14, 14); public static final Resource BTN_NO_N = new Resource(98, 14, 14, 14); public static final Resource BTN_NO_F = new Resource(98, 0, 14, 14); + public static final Resource BTN_DISAMB_D = new Resource(242, 0, 14, 14); + public static final Resource BTN_COPY_D = new Resource(242, 14, 14, 14); + public static final Resource BTN_DISAMB_N = new Resource(112, 14, 14, 14); + public static final Resource BTN_DISAMB_F = new Resource(112, 0, 14, 14); public static final Resource WGT_BUTTON_N = new Resource(40, 232, 20, 20); public static final Resource WGT_BUTTON_F = new Resource(60, 232, 20, 20); public static final Resource WGT_BUTTON_D = new Resource(80, 232, 20, 20); diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButton.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButton.java index 944403e..a90f7c9 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButton.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButton.java @@ -17,6 +17,7 @@ public abstract class WButton extends WTooltip { protected int xPos, yPos, xSize, ySize; protected Runnable lsnrLeft, lsnrRight; protected Utilities.Timer timer = new Utilities.Timer(); + protected boolean disabled; public WButton(int xPos, int yPos, int xSize, int ySize, @Nullable String name) { super(name); @@ -38,9 +39,10 @@ public abstract class WButton extends WTooltip { @Override public void onDraw(JecGui gui, int xMouse, int yMouse) { + super.onDraw(gui, xMouse, yMouse); boolean hovered = JecGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse); - gui.drawResourceContinuous(hovered ? Resource.WGT_BUTTON_F : Resource.WGT_BUTTON_N, xPos, yPos, - xSize, ySize, 3, 3, 3, 3); + gui.drawResourceContinuous(disabled ? Resource.WGT_BUTTON_D : + (hovered ? Resource.WGT_BUTTON_F : Resource.WGT_BUTTON_N), xPos, yPos, xSize, ySize, 3, 3, 3, 3); } @Override @@ -60,7 +62,12 @@ public abstract class WButton extends WTooltip { } @Override - boolean mouseIn(int xMouse, int yMouse) { + public boolean mouseIn(int xMouse, int yMouse) { return JecGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse); } + + public WButton setDisabled(boolean b) { + disabled = b; + return this; + } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButtonIcon.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButtonIcon.java index e8f2734..6cd72e1 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButtonIcon.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WButtonIcon.java @@ -5,6 +5,9 @@ import me.towdium.jecalculation.client.gui.Resource; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; /** * Author: towdium @@ -12,23 +15,33 @@ import javax.annotation.ParametersAreNonnullByDefault; */ @ParametersAreNonnullByDefault public class WButtonIcon extends WButton { - protected Resource normal, focused; + protected Resource rNormal, rFocused, rDisabled; public WButtonIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused) { - this(xPos, yPos, xSize, ySize, normal, focused, null); + this(xPos, yPos, xSize, ySize, normal, focused, null, null); } - public WButtonIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused, @Nullable String name) { + public WButtonIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused, String name) { + this(xPos, yPos, xSize, ySize, normal, focused, null, name); + } + + public WButtonIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused, + @Nullable Resource disabled, @Nullable String name) { super(xPos, yPos, xSize, ySize, name); - this.normal = normal; - this.focused = focused; + this.rNormal = normal; + this.rFocused = focused; + this.rDisabled = disabled; } @Override public void onDraw(JecGui gui, int xMouse, int yMouse) { super.onDraw(gui, xMouse, yMouse); - Resource r = mouseIn(xMouse, yMouse) ? focused : normal; - if (r != null) - gui.drawResource(r, xPos + (xSize - r.getXSize()) / 2, yPos + (ySize - r.getYSize()) / 2); + Resource r = disabled ? rDisabled : (mouseIn(xMouse, yMouse) ? rFocused : rNormal); + if (r != null) gui.drawResource(r, xPos + (xSize - r.getXSize()) / 2, yPos + (ySize - r.getYSize()) / 2); + } + + @Override + protected List getSuffix() { + return disabled ? Arrays.asList("disabled", "active") : Collections.singletonList("active"); } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WIcon.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WIcon.java index 1272254..6799642 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WIcon.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WIcon.java @@ -40,7 +40,7 @@ public class WIcon extends WTooltip { } @Override - boolean mouseIn(int xMouse, int yMouse) { + public boolean mouseIn(int xMouse, int yMouse) { return JecGui.mouseIn(xPos + (xSize - normal.getXSize()) / 2, yPos + (ySize - normal.getYSize()) / 2, normal.getXSize(), normal.getYSize(), xMouse, yMouse); } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java index c9d7c9b..54da7cf 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java @@ -5,9 +5,11 @@ import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.core.labels.ILabel; import me.towdium.jecalculation.utils.IllegalPositionException; +import me.towdium.jecalculation.utils.Utilities; import javax.annotation.ParametersAreNonnullByDefault; import java.util.ArrayList; +import java.util.stream.IntStream; /** * Author: towdium @@ -25,6 +27,7 @@ public class WLabel implements IWidget { public int xPos, yPos, xSize, ySize; public ILabel label; public enumMode mode; + protected Utilities.Timer timer = new Utilities.Timer(); public WLabel(int xPos, int yPos, int xSize, int ySize, enumMode mode) { this.xPos = xPos; @@ -59,8 +62,25 @@ public class WLabel implements IWidget { label.getToolTip(buf, mode == enumMode.EDITOR || mode == enumMode.RESULT)); } } + if (mode == enumMode.EDITOR || mode == enumMode.SELECTOR) { + timer.setState(gui.hand != ILabel.EMPTY); + int color = 0xFFFFFF + (int) ((-Math.cos(timer.getTime() * Math.PI / 1500) + 1) * 0x40) * 0x1000000; + gui.drawRectangle(xPos + 1, yPos + 1, xSize - 2, ySize - 2, color); + } } + @Override + public boolean onScroll(JecGui gui, int xMouse, int yMouse, int diff) { + if (mouseIn(xMouse, yMouse) && mode == enumMode.EDITOR && label != ILabel.EMPTY) { + IntStream.range(0, Math.abs(diff)).forEach(i -> { + if (JecGui.isShiftDown()) label = diff > 0 ? label.increaseAmountLarge() : label.decreaseAmountLarge(); + else label = diff > 0 ? label.increaseAmount() : label.decreaseAmount(); + }); + return true; + } else return false; + } + + @Override public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) { if (mouseIn(xMouse, yMouse)) { diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabelGroup.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabelGroup.java index c855f2f..7a0d0d6 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabelGroup.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabelGroup.java @@ -16,34 +16,33 @@ import java.util.stream.IntStream; * Date: 17-8-17. */ @ParametersAreNonnullByDefault -public class WLabelGroup implements IWidget { - ArrayList widgets = new ArrayList<>(); +public class WLabelGroup extends WContainer { + ArrayList labels = new ArrayList<>(); public WLabelGroup(int xPos, int yPos, int column, int row, WLabel.enumMode mode) { this(xPos, yPos, column, row, 18, 18, mode); } public WLabelGroup(int xPos, int yPos, int column, int row, int xSize, int ySize, WLabel.enumMode mode) { - IntStream.range(0, row).forEach(r -> IntStream.range(0, column).forEach( - c -> widgets.add(new WLabel(xPos + c * xSize, yPos + r * ySize, xSize, ySize, mode)))); + IntStream.range(0, row).forEach(r -> IntStream.range(0, column).forEach(c -> { + WLabel l = new WLabel(xPos + c * xSize, yPos + r * ySize, xSize, ySize, mode); + labels.add(l); + add(l); + })); } @Override - public void onDraw(JecGui gui, int xMouse, int yMouse) { - widgets.forEach(w -> w.onDraw(gui, xMouse, yMouse)); - } - - @Override - public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) { - return widgets.stream().anyMatch(w -> w.onClicked(gui, xMouse, yMouse, button)); + public void remove(IWidget w) { + super.remove(w); + if (w instanceof WLabel) labels.remove(w); } public Optional getLabelAt(int xMouse, int yMouse) { - return widgets.stream().filter(w -> w.mouseIn(xMouse, yMouse)).findFirst(); + return labels.stream().filter(w -> w.mouseIn(xMouse, yMouse)).findFirst(); } public void setLabel(List labels, int start) { Single i = new Single<>(start); - widgets.forEach(l -> l.setLabel(i.value < labels.size() ? labels.get(i.value++) : ILabel.EMPTY)); + this.labels.forEach(l -> l.setLabel(i.value < labels.size() ? labels.get(i.value++) : ILabel.EMPTY)); } -} +} \ No newline at end of file diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java index a822e22..b705318 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java @@ -7,6 +7,7 @@ import me.towdium.jecalculation.utils.Utilities.Circulator; import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; +import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -25,6 +26,7 @@ public class WSwitcher implements IWidget { protected WText wText; protected List keys; protected Circulator index; + public Consumer listener; public WSwitcher(int xPos, int yPos, int xSize, int total) { this(xPos, yPos, xSize, IntStream.rangeClosed(1, total) @@ -37,15 +39,22 @@ public class WSwitcher implements IWidget { this.yPos = yPos; this.keys = keys; left = new WButtonIcon(xPos, yPos, SIZE, SIZE, Resource.WGT_ARR_L_N, Resource.WGT_ARR_L_F) - .setListenerLeft(() -> index.prev()); + .setListenerLeft(() -> move(true)); right = new WButtonIcon(xPos + xSize - SIZE, yPos, SIZE, SIZE, Resource.WGT_ARR_R_N, Resource.WGT_ARR_R_F) - .setListenerLeft(() -> index.next()); + .setListenerLeft(() -> move(false)); wRect = new WRectangle(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, JecGui.COLOR_GUI_GREY); wText = new WText(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, JecGui.Font.DEFAULT_SHADOW, () -> keys.get(index.index())); index = new Circulator(keys.size()); } + protected void move(boolean forward) { + if (forward) index.next(); + else index.prev(); + + if (listener != null) listener.accept(index.index()); + } + @Override public void onDraw(JecGui gui, int xMouse, int yMouse) { Stream.of(left, right, wRect, wText).forEach(w -> w.onDraw(gui, xMouse, yMouse)); @@ -60,4 +69,9 @@ public class WSwitcher implements IWidget { public boolean onKey(JecGui gui, char ch, int code) { return Stream.of(left, right).anyMatch(w -> w.onKey(gui, ch, code)); } + + public WSwitcher setListener(Consumer listener) { + this.listener = listener; + return this; + } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WTooltip.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WTooltip.java index 473d31e..3546455 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WTooltip.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WTooltip.java @@ -7,6 +7,8 @@ import me.towdium.jecalculation.utils.wrappers.Pair; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.Collections; +import java.util.List; /** * Author: towdium @@ -16,7 +18,7 @@ import javax.annotation.ParametersAreNonnullByDefault; public abstract class WTooltip implements IWidget { @Nullable public String name; - Utilities.Timer timer = new Utilities.Timer(); + protected Utilities.Timer timer = new Utilities.Timer(); public WTooltip(@Nullable String name) { this.name = name; @@ -27,11 +29,17 @@ public abstract class WTooltip implements IWidget { if (name != null) { timer.setState(mouseIn(xMouse, yMouse)); if (timer.getTime() > 500) { - Pair str = Utilities.L18n.search(String.join(".", "gui", name, "tooltip")); - if (str.two || JecGui.ALWAYS_TOOLTIP) gui.drawTooltip(xMouse, yMouse, str.one); + String str = getSuffix().stream().map(s -> Utilities.L18n.search(String.join(".", "gui", name, s))) + .filter(p -> p.two).findFirst().map(p -> p.one) + .orElse(JecGui.ALWAYS_TOOLTIP ? String.join(".", "gui", name, getSuffix().get(0)) : null); + if (str != null) gui.drawTooltip(xMouse, yMouse, str); } } } - abstract boolean mouseIn(int xMouse, int yMouse); + protected List getSuffix() { + return Collections.singletonList("tooltip"); + } + + public abstract boolean mouseIn(int xMouse, int yMouse); } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java index 6580e21..a693c80 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java @@ -1,5 +1,6 @@ package me.towdium.jecalculation.client.gui.guis; +import me.towdium.jecalculation.client.gui.IWPicker; import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.client.gui.drawables.*; import me.towdium.jecalculation.core.labels.ILabel; @@ -8,26 +9,37 @@ import net.minecraft.item.ItemStack; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.IntStream; /** * Author: towdium * Date: 17-9-16. */ -public class GuiDisambiguation extends WContainer { - public GuiDisambiguation(List> iss) { - ArrayList labels = new ArrayList<>(); - IntStream.range(0, 50).forEach(i -> labels.add(new LabelOreDict(i % 2 == 0 ? "plankWood" : "ingotIron", i))); - WLabelScroll lsUp = new WLabelScroll(25, 48, 7, 3, WLabel.enumMode.RESULT, true).setLabels(labels); - WLabelScroll lsDown = new WLabelScroll(25, 105, 7, 3, WLabel.enumMode.RESULT, true).setLabels(labels); +public class GuiDisambiguation extends IWPicker.Simple { + protected WLabelScroll lsUp; + protected WLabelScroll lsDown; + protected List> record; + + public GuiDisambiguation(List> record) { + lsUp = new WLabelScroll(25, 48, 7, 3, WLabel.enumMode.PICKER, true); + lsDown = new WLabelScroll(25, 105, 7, 3, WLabel.enumMode.PICKER, true); + this.record = new ArrayList<>(record.stream().filter(iss -> !ILabel.CONVERTER_ITEM.toLabel(iss).isEmpty()) + .collect(Collectors.toSet())); WTextField tf = new WTextField(25, 24, 90); add(new WPanel()); - add(new WSwitcher(7, 7, 162, 5)); + add(new WSwitcher(7, 7, 162, this.record.size()).setListener(this::setPage)); add(new WIcon(149, 24, 20, 20, Resource.ICN_HELP_N, Resource.ICN_HELP_F, "disambiguation.help")); add(new WIcon(7, 48, 18, 54, Resource.ICN_LIST_N, Resource.ICN_LIST_F, "disambiguation.list")); add(new WIcon(7, 105, 18, 54, Resource.ICN_LABEL_N, Resource.ICN_LABEL_F, "disambiguation.label")); - add(new WSearch(i -> { - }, tf, lsUp, lsDown)); + add(new WSearch(i -> callback.ifPresent(c -> c.accept(i)), tf, lsUp, lsDown)); + + setPage(0); + } + + protected void setPage(int n) { + lsUp.setLabels(record.get(n).stream().map(ILabel.CONVERTER_ITEM::toLabel).collect(Collectors.toList())); + lsDown.setLabels(ILabel.CONVERTER_ITEM.toLabel(record.get(n))); } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiRecipe.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiRecipe.java index d2dd626..08b514a 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiRecipe.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiRecipe.java @@ -1,14 +1,16 @@ package me.towdium.jecalculation.client.gui.guis; import codechicken.nei.recipe.IRecipeHandler; +import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.client.gui.drawables.*; import me.towdium.jecalculation.core.labels.ILabel; import net.minecraft.item.ItemStack; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import java.util.function.BiConsumer; import java.util.stream.Collectors; +import java.util.stream.IntStream; import static me.towdium.jecalculation.client.gui.drawables.WLabel.enumMode.EDITOR; @@ -17,10 +19,19 @@ import static me.towdium.jecalculation.client.gui.drawables.WLabel.enumMode.EDIT * Date: 17-9-8. */ public class GuiRecipe extends WContainer { + List> disambiguation; WButton buttonSave = new WButtonIcon(26, 33, 20, 20, Resource.BTN_SAVE_N, Resource.BTN_SAVE_F, "recipe.save"); - WButton buttonCopy = new WButtonIcon(83, 33, 20, 20, Resource.BTN_COPY_N, Resource.BTN_COPY_F, "recipe.copy"); + WButton buttonCopy = new WButtonIcon(83, 33, 20, 20, Resource.BTN_COPY_N, Resource.BTN_COPY_F, Resource.BTN_COPY_D, + "recipe.copy").setDisabled(true); WButton buttonDel = new WButtonIcon(64, 33, 20, 20, Resource.BTN_DEL_N, Resource.BTN_DEL_F, "recipe.clear"); WButton buttonLabel = new WButtonIcon(45, 33, 20, 20, Resource.BTN_LABEL_N, Resource.BTN_LABEL_F, "recipe.label"); + WButton buttonDisamb = new WButtonIcon(102, 33, 20, 20, Resource.BTN_DISAMB_N, + Resource.BTN_DISAMB_F, Resource.BTN_DISAMB_D, "recipe.disamb").setDisabled(true).setListenerLeft(() -> { + if (disambiguation != null) JecGui.displayGui(new GuiDisambiguation(disambiguation).setCallback(l -> { + JecGui.displayParent(); + JecGui.getCurrent().hand = l; + })); + }); WButton buttonYes = new WButtonIcon(7, 33, 20, 20, Resource.BTN_YES_N, Resource.BTN_YES_F, "recipe.confirm"); WButton buttonNo = new WButtonIcon(26, 33, 20, 20, Resource.BTN_NO_N, Resource.BTN_NO_F, "recipe.abort"); WLabelGroup groupInput = new WLabelGroup(28, 111, 7, 2, 20, 20, EDITOR); @@ -43,18 +54,29 @@ public class GuiRecipe extends WContainer { public void setModeNewGroup(boolean b) { if (b) { - removeAll(buttonNew, buttonLabel, buttonDel, buttonCopy, buttonSave); + removeAll(buttonNew, buttonLabel, buttonDel, buttonCopy, buttonSave, buttonDisamb); addAll(buttonYes, buttonNo, textField); } else { - addAll(buttonNew, buttonLabel, buttonDel, buttonSave); // TODO buttonCopy + addAll(buttonNew, buttonLabel, buttonDel, buttonCopy, buttonSave, buttonDisamb); removeAll(buttonYes, buttonNo, textField); } } public void transfer(IRecipeHandler recipe, int recipeIndex) { - List> buf = new ArrayList<>(); - List input = new ArrayList<>(); - List output = new ArrayList<>(); + ArrayList> buf = new ArrayList<>(); + ArrayList input = new ArrayList<>(); + ArrayList output = new ArrayList<>(); + + BiConsumer, ILabel> merge = (list, label) -> IntStream.range(0, list.size()).filter(i -> { + Optional l = ILabel.MERGER.merge(list.get(i), label, true); + boolean ret = l.isPresent(); + if (ret) list.set(i, l.get()); + return ret; + }).findAny().orElseGet(() -> { + list.add(label); + return -1; + }); + List raw = new ArrayList<>(); List itemStacks = recipe.getIngredientStacks(recipeIndex).stream() @@ -64,13 +86,17 @@ public class GuiRecipe extends WContainer { raw.add(ILabel.CONVERTER_ITEM.toLabel(itemStack)); }); List guessed = ILabel.CONVERTER_ITEM.toLabel(itemStacks); - input.add(guessed.isEmpty() ? ILabel.CONVERTER_ITEM.toLabel(itemStacks.get(0)) : guessed.get(0)); - buf.add(raw); + merge.accept(input, guessed.isEmpty() ? ILabel.CONVERTER_ITEM.toLabel(itemStacks.get(0)) : guessed.get(0)); + + buf.add(itemStacks); ItemStack outputStack = recipe.getResultStack(recipeIndex).item; output.add(ILabel.CONVERTER_ITEM.toLabel(outputStack)); + buf.add(Collections.singletonList(outputStack)); groupInput.setLabel(input, 0); groupOutput.setLabel(output, 0); + disambiguation = buf; + buttonDisamb.setDisabled(buf.isEmpty()); } } diff --git a/src/main/java/me/towdium/jecalculation/core/labels/ILabel.java b/src/main/java/me/towdium/jecalculation/core/labels/ILabel.java index 4c89d96..765f2e2 100644 --- a/src/main/java/me/towdium/jecalculation/core/labels/ILabel.java +++ b/src/main/java/me/towdium/jecalculation/core/labels/ILabel.java @@ -91,8 +91,11 @@ public interface ILabel { static { INSTANCE = new RegistryMerger(); // registerGuess functions here + INSTANCE.register("itemStack", "itemStack", RegistryMerger::mergeItemStackNItemStack); + INSTANCE.register("oreDict", "oreDict", RegistryMerger::mergeOreDictNOreDict); } + private Relation functions = new Relation<>(); @@ -103,20 +106,43 @@ public interface ILabel { functions.add(a, b, func); } - public Pair merge(ILabel a, ILabel b, boolean add) { - return functions.get(ILabel.getIdentifier(a), ILabel.getIdentifier(b)) - .orElse((x, y, f) -> new Pair<>(x, y)).merge(a, b, add); + static Optional mergeItemStackNItemStack(ILabel a, ILabel b, boolean add) { + if (a instanceof LabelItemStack && b instanceof LabelItemStack) { + LabelItemStack lisA = (LabelItemStack) a; + LabelItemStack lisB = (LabelItemStack) b; + ItemStack isA = lisA.getItemStack(); + ItemStack isB = lisB.getItemStack(); + if (isA.getItem() == isB.getItem() && isA.getItemDamage() == isB.getItemDamage() && + (isA.getTagCompound() == null ? !isB.hasTagCompound() : + isA.getTagCompound().equals(isB.getTagCompound()))) { + LabelItemStack ret = new LabelItemStack(lisA.getItemStack(), + add ? lisA.getAmount() + lisB.getAmount() : lisA.getAmount() - lisB.getAmount()); + return Optional.of(ret); + } + } + return Optional.empty(); } - public boolean test(ILabel a, ILabel b) { - Optional f = functions.get(ILabel.getIdentifier(a), ILabel.getIdentifier(b)); - if (!f.isPresent()) return false; - else { - Pair p = f.get().merge(a, b, true); - return (!(p.one == a && p.two == b) && !(p.two == a && p.one == a)); + static Optional mergeOreDictNOreDict(ILabel a, ILabel b, boolean add) { + if (a instanceof LabelOreDict && b instanceof LabelOreDict) { + LabelOreDict lodA = (LabelOreDict) a; + LabelOreDict lodB = (LabelOreDict) b; + if (lodA.getName().equals(lodB.getName())) + return Optional.of(new LabelOreDict(lodA.getName(), + add ? lodA.getAmount() + lodB.getAmount() : lodA.getAmount() - lodB.getAmount())); } + return Optional.empty(); } + public Optional merge(ILabel a, ILabel b, boolean add) { + Optional ret = functions.get(ILabel.getIdentifier(a), ILabel.getIdentifier(b)) + .orElse((x, y, f) -> Optional.empty()).merge(a, b, add); + if (ret.isPresent() && (ret.get() == a || ret.get() == b)) + throw new RuntimeException("Merger should not modify the given labels."); + return ret; + } + + @FunctionalInterface public interface MergerFunction { /** @@ -125,7 +151,7 @@ public interface ILabel { * @param add add together or cancel each other * @return merged {@link ILabel label(s)} if not changed (no matter order), they cannot merge. */ - Pair merge(ILabel a, ILabel b, boolean add); + Optional merge(ILabel a, ILabel b, boolean add); } } diff --git a/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelItemStack.java b/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelItemStack.java index 5fdf682..ad711e0 100644 --- a/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelItemStack.java +++ b/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelItemStack.java @@ -75,4 +75,8 @@ public class LabelItemStack extends LabelSimpleAmount { public boolean equals(Object obj) { return obj instanceof LabelItemStack && itemStack.equals(((LabelItemStack) obj).itemStack); } + + public ItemStack getItemStack() { + return itemStack; + } } diff --git a/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelOreDict.java b/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelOreDict.java index da1a3c3..8a355a4 100644 --- a/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelOreDict.java +++ b/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelOreDict.java @@ -101,4 +101,8 @@ public class LabelOreDict extends LabelSimpleAmount { existing.add(FORMAT_BLUE + FORMAT_ITALIC + JustEnoughCalculation.Reference.MODNAME); return existing; } + + public String getName() { + return name; + } } diff --git a/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelSimpleAmount.java b/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelSimpleAmount.java index 93a9202..2cdac9c 100644 --- a/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelSimpleAmount.java +++ b/src/main/java/me/towdium/jecalculation/core/labels/labels/LabelSimpleAmount.java @@ -77,4 +77,8 @@ public abstract class LabelSimpleAmount implements ILabel { nbt.setInteger(KEY_AMOUNT, amount); return nbt; } + + public int getAmount() { + return amount; + } } diff --git a/src/main/java/me/towdium/jecalculation/utils/Utilities.java b/src/main/java/me/towdium/jecalculation/utils/Utilities.java index 8c61458..f126d2f 100644 --- a/src/main/java/me/towdium/jecalculation/utils/Utilities.java +++ b/src/main/java/me/towdium/jecalculation/utils/Utilities.java @@ -98,6 +98,7 @@ public class Utilities { } + @SuppressWarnings("UnusedReturnValue") public static class Circulator { int total, current; diff --git a/src/main/java/me/towdium/jecalculation/utils/wrappers/Pair.java b/src/main/java/me/towdium/jecalculation/utils/wrappers/Pair.java index b3fe9f7..10794ab 100644 --- a/src/main/java/me/towdium/jecalculation/utils/wrappers/Pair.java +++ b/src/main/java/me/towdium/jecalculation/utils/wrappers/Pair.java @@ -15,9 +15,17 @@ public class Pair { @Override public int hashCode() { - return one.hashCode() ^ two.hashCode(); + return one.hashCode() + two.hashCode(); } + @Override + public boolean equals(Object obj) { + if (obj instanceof Pair) { + Pair p = (Pair) obj; + return one.equals(p.one) && two.equals(p.two); + } + return false; + } public boolean swap(Class ke, Class ve) { if (ke.isInstance(one) && ve.isInstance(two)) return true; else if (!(ke.isInstance(two) && ve.isInstance(one))) return false; diff --git a/src/main/resources/assets/jecalculation/textures/gui/resources.png b/src/main/resources/assets/jecalculation/textures/gui/resources.png index 6f55f27..7269ece 100644 Binary files a/src/main/resources/assets/jecalculation/textures/gui/resources.png and b/src/main/resources/assets/jecalculation/textures/gui/resources.png differ