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 2e13d4f..74bae97 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/JecGui.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/JecGui.java @@ -39,45 +39,58 @@ import java.util.function.Function; @ParametersAreNonnullByDefault public class JecGui extends GuiContainer { public static final int COLOR_GREY = 0xFFA1A1A1; - protected static JecGui last; + public static final boolean ALWAYS_TOOLTIP = false; + protected JecGui parent; public ILabel hand = ILabel.EMPTY; - public DContainer container = new DContainer(); + public IDrawable root; protected List>> tooltipBuffer = new ArrayList<>(); - public JecGui(@Nullable JecGui parent, IDrawable... drawables) { - this(parent, false, drawables); + public JecGui(@Nullable JecGui parent, IDrawable root) { + this(parent, false, root); } - public JecGui(@Nullable JecGui parent, boolean acceptsTransfer, IDrawable... drawables) { + public JecGui(@Nullable JecGui parent, boolean acceptsTransfer, IDrawable root) { super(acceptsTransfer ? new ContainerTransfer() : new ContainerNonTransfer()); this.parent = parent; - container.addAll(drawables); + this.root = root; } public static boolean mouseIn(int xPos, int yPos, int xSize, int ySize, int xMouse, int yMouse) { return xMouse > xPos && yMouse > yPos && xMouse <= xPos + xSize && yMouse <= yPos + ySize; } - public static void displayGui(IDrawable... components) { - displayGui(true, false, components); + public static void displayGui(IDrawable root) { + displayGui(true, false, root); } - public static void displayGui(boolean updateParent, boolean acceptsTransfer, IDrawable... components) { + public static void displayGui(boolean updateParent, boolean acceptsTransfer, IDrawable root) { // isCallingFromMinecraftThread if (Minecraft.getMinecraft().func_152345_ab()) - displayGuiUnsafe(updateParent, acceptsTransfer, components); + displayGuiUnsafe(updateParent, acceptsTransfer, root); } - public static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, IDrawable... components) { + public static JecGui getCurrent() { + return last; + } + + private static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, IDrawable root) { Minecraft mc = Minecraft.getMinecraft(); JecGui parent; if (mc.currentScreen == null) parent = null; else if (!(mc.currentScreen instanceof JecGui)) parent = last; else if (updateParent) parent = (JecGui) mc.currentScreen; else parent = ((JecGui) mc.currentScreen).parent; - mc.displayGuiScreen(new JecGui(parent, acceptsTransfer, components)); + JecGui toShow = new JecGui(parent, acceptsTransfer, root); + mc.displayGuiScreen(toShow); + last = toShow; + } + + @Override + public void onGuiClosed() { + super.onGuiClosed(); + last = null; } @Override @@ -97,7 +110,7 @@ public class JecGui extends GuiContainer { GlStateManager.translate(guiLeft, guiTop, 0); GlStateManager.disableLighting(); GlStateManager.disableDepth(); - container.onDraw(this, mouseX - guiLeft, mouseY - guiTop); + root.onDraw(this, mouseX - guiLeft, mouseY - guiTop); GlStateManager.popMatrix(); GlStateManager.pushMatrix(); GlStateManager.translate(mouseX - 8, mouseY - 8, 0); @@ -318,10 +331,6 @@ public class JecGui extends GuiContainer { RenderHelper.disableStandardItemLighting(); } - public String localize(String key, Object... os) { - return LocalizationHelper.format(this.getClass(), "gui", key, os); - } - public static class Font { public static final Font DEFAULT_SHADOW = new Font(0xFFFFFF, true, true, false, 1, enumAlign.AUTO); public static final Font DEFAULT_NO_SHADOW = new Font(0x404040, false, true, false, 1, enumAlign.AUTO); @@ -358,7 +367,7 @@ public class JecGui extends GuiContainer { @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { super.mouseClicked(mouseX, mouseY, mouseButton); - boolean inGui = container.onClicked(this, mouseX - guiLeft, mouseY - guiTop, mouseButton); + boolean inGui = root.onClicked(this, mouseX - guiLeft, mouseY - guiTop, mouseButton); if(!inGui) { this.handleMouseEvent(); } @@ -366,7 +375,7 @@ public class JecGui extends GuiContainer { @Override protected void keyTyped(char typedChar, int keyCode) { - if (!container.onKey(this, typedChar, keyCode)) { + if (!root.onKey(this, typedChar, keyCode)) { if (keyCode == Keyboard.KEY_ESCAPE) { if (hand != ILabel.EMPTY) hand = ILabel.EMPTY; 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 4493a29..7406d9d 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/Resource.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/Resource.java @@ -36,8 +36,9 @@ public class Resource { public static final Resource WGT_ARR_L_N = new Resource(0, 242, 9, 9); public static final Resource WGT_ARR_R_F = new Resource(10, 232, 9, 9); public static final Resource WGT_ARR_R_N = new Resource(10, 242, 9, 9); - public static final Resource WGT_PAGER_F = new Resource(120, 232, 20, 20); - public static final Resource WGT_PAGER_B = new Resource(120, 232, 20, 16); + public static final Resource WGT_PAGER_FN = new Resource(120, 232, 20, 20); + public static final Resource WGT_PAGER_F0 = new Resource(140, 232, 20, 20); + public static final Resource WGT_PAGER_N = new Resource(160, 232, 20, 20); public static final Resource ICN_RECENT_N = new Resource(0, 42, 14, 14); public static final Resource ICN_RECENT_F = new Resource(0, 28, 14, 14); public static final Resource ICN_INPUT_N = new Resource(14, 42, 14, 14); @@ -46,6 +47,7 @@ public class Resource { public static final Resource ICN_OUTPUT_F = new Resource(28, 28, 14, 14); public static final Resource ICN_CATALYST_N = new Resource(42, 42, 14, 14); public static final Resource ICN_CATALYST_F = new Resource(42, 28, 14, 14); + public static final Resource LBL_ORE_DICT = new Resource(0, 56, 16, 16); protected int xPos, yPos, xSize, ySize; protected ResourceLocation rl; diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButton.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButton.java index 8ee901e..81f23fb 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButton.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButton.java @@ -14,31 +14,17 @@ import javax.annotation.ParametersAreNonnullByDefault; * Date: 17-8-17. */ @ParametersAreNonnullByDefault -public class DButton implements IDrawable { +public abstract class DButton extends DTooltip { protected int xPos, yPos, xSize, ySize; - protected Resource normal, focused; - protected String tooltip, text; protected Runnable lsnrLeft, lsnrRight; protected Utilities.Timer timer = new Utilities.Timer(); - public DButton(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused) { - this(xPos, yPos, xSize, ySize, "", normal, focused, null); - } - - public DButton(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused, String tooltip) { - this(xPos, yPos, xSize, ySize, "", normal, focused, tooltip); - } - - public DButton(int xPos, int yPos, int xSize, int ySize, String text, - @Nullable Resource normal, @Nullable Resource focused, @Nullable String tooltip) { + public DButton(int xPos, int yPos, int xSize, int ySize, @Nullable String name) { + super(name); this.xPos = xPos; this.yPos = yPos; this.xSize = xSize; this.ySize = ySize; - this.normal = normal; - this.focused = focused; - this.tooltip = tooltip; - this.text = text; } public DButton setListenerLeft(Runnable r) { @@ -56,22 +42,6 @@ public class DButton implements IDrawable { 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); - Resource r = hovered ? focused : normal; - if (r != null) - gui.drawResource(r, xPos + (xSize - r.getXSize()) / 2, yPos + (ySize - r.getYSize()) / 2); - if (tooltip != null) { - timer.setState(hovered); - if (timer.getTime() > 500) gui.drawTooltip(xMouse, yMouse, gui.localize("tooltip." + tooltip)); - } - int textColor = hovered ? 16777120 : 0; - int strWidth = gui.getFontRenderer().getStringWidth(text); - int ellipsisWidth = gui.getFontRenderer().getStringWidth("..."); - String str = text; - if (strWidth > xSize - 6 && strWidth > ellipsisWidth) - str = gui.getFontRenderer().trimStringToWidth(text, xSize - 6 - ellipsisWidth).trim() + "..."; - JecGui.Font f = JecGui.Font.DEFAULT_SHADOW.copy(); - f.color = textColor; - gui.drawText(xPos, yPos, xSize, ySize, f, str); } @Override @@ -89,4 +59,9 @@ public class DButton implements IDrawable { } return false; } + + @Override + boolean mouseIn(int xMouse, int yMouse) { + return JecGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse); + } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButtonIcon.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButtonIcon.java new file mode 100644 index 0000000..c5fbcd8 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButtonIcon.java @@ -0,0 +1,34 @@ +package me.towdium.jecalculation.client.gui.drawables; + +import me.towdium.jecalculation.client.gui.JecGui; +import me.towdium.jecalculation.client.gui.Resource; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +/** + * Author: towdium + * Date: 17-9-16. + */ +@ParametersAreNonnullByDefault +public class DButtonIcon extends DButton { + protected Resource normal, focused; + + public DButtonIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused) { + this(xPos, yPos, xSize, ySize, normal, focused, null); + } + + public DButtonIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused, @Nullable String name) { + super(xPos, yPos, xSize, ySize, name); + this.normal = normal; + this.focused = focused; + } + + @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); + } +} diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButtonText.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButtonText.java new file mode 100644 index 0000000..c7b6e7e --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DButtonText.java @@ -0,0 +1,32 @@ +package me.towdium.jecalculation.client.gui.drawables; + +import me.towdium.jecalculation.client.gui.JecGui; +import me.towdium.jecalculation.utils.helpers.LocalizationHelper; + +import javax.annotation.ParametersAreNonnullByDefault; + +/** + * Author: towdium + * Date: 17-9-16. + */ +@ParametersAreNonnullByDefault +public class DButtonText extends DButton { + public DButtonText(int xPos, int yPos, int xSize, int ySize, String name) { + super(xPos, yPos, xSize, ySize, name); + } + + @Override + public void onDraw(JecGui gui, int xMouse, int yMouse) { + super.onDraw(gui, xMouse, yMouse); + int textColor = mouseIn(xMouse, yMouse) ? 16777120 : 0; + String text = LocalizationHelper.format(String.join(".", "gui", name, "text")); + int strWidth = gui.getFontRenderer().getStringWidth(text); + int ellipsisWidth = gui.getFontRenderer().getStringWidth("..."); + String str = text; + if (strWidth > xSize - 6 && strWidth > ellipsisWidth) + str = gui.getFontRenderer().trimStringToWidth(text, xSize - 6 - ellipsisWidth).trim() + "..."; + JecGui.Font f = JecGui.Font.DEFAULT_SHADOW.copy(); + f.color = textColor; + gui.drawText(xPos, yPos, xSize, ySize, f, str); + } +} diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DContainer.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DContainer.java index b0cef0d..0ead4e1 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DContainer.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DContainer.java @@ -34,6 +34,10 @@ public class DContainer implements IDrawable { remove(aw); } + public void clear() { + widgets.clear(); + } + public void onDraw(JecGui gui, int mouseX, int mouseY) { widgets.forEach(widget -> widget.onDraw(gui, mouseX, mouseY)); } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DIcon.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DIcon.java index 2ccee70..113c436 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DIcon.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DIcon.java @@ -14,37 +14,36 @@ import javax.annotation.ParametersAreNonnullByDefault; * Date: 17-8-18. */ @ParametersAreNonnullByDefault -public class DIcon implements IDrawable { +public class DIcon extends DTooltip { public int xPos, yPos, xSize, ySize; public Resource normal, focused; - Timer timer = new Timer(); - public String tooltip; public DIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused) { this(xPos, yPos, xSize, ySize, normal, focused, null); } public DIcon(int xPos, int yPos, int xSize, int ySize, - Resource normal, Resource focused, @Nullable String tooltip) { + Resource normal, Resource focused, @Nullable String name) { + super(name); this.xPos = xPos; this.yPos = yPos; this.xSize = xSize; this.ySize = ySize; this.normal = normal; this.focused = focused; - this.tooltip = tooltip; } @Override public void onDraw(JecGui gui, int xMouse, int yMouse) { - boolean hovered = JecGui.mouseIn(xPos + (xSize - normal.getXSize()) / 2, - yPos + (ySize - normal.getYSize()) / 2, normal.getXSize(), normal.getYSize(), xMouse, yMouse); + super.onDraw(gui, xMouse, yMouse); gui.drawRectangle(xPos, yPos, xSize, ySize, JecGui.COLOR_GREY); - Resource r = hovered ? focused : normal; + Resource r = mouseIn(xMouse, yMouse) ? focused : normal; gui.drawResource(r, (xSize - r.getXSize()) / 2 + xPos, (ySize - r.getYSize()) / 2 + yPos); - if (tooltip != null) { - timer.setState(hovered); - if (timer.getTime() > 500) gui.drawTooltip(xMouse, yMouse, gui.localize("tooltip." + tooltip)); - } + } + + @Override + 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/DPage.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DPage.java new file mode 100644 index 0000000..3556468 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DPage.java @@ -0,0 +1,54 @@ +package me.towdium.jecalculation.client.gui.drawables; + +import me.towdium.jecalculation.client.gui.IDrawable; +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.Utilities; +import me.towdium.jecalculation.utils.helpers.LocalizationHelper; + +import javax.annotation.ParametersAreNonnullByDefault; + +/** + * Author: towdium + * Date: 17-9-16. + */ +@ParametersAreNonnullByDefault +public class DPage implements IDrawable { + + + protected int index; + protected ILabel.RegistryEditor.Record record; + protected Utilities.Timer timer = new Utilities.Timer(); + protected boolean focused; + protected Runnable listener; + + public DPage(int index, ILabel.RegistryEditor.Record record, boolean focused) { + this.index = index; + this.record = record; + this.focused = focused; + } + + @Override + public void onDraw(JecGui gui, int xMouse, int yMouse) { + Resource resource = focused ? (index == 0 ? Resource.WGT_PAGER_F0 : Resource.WGT_PAGER_FN) + : Resource.WGT_PAGER_N; + gui.drawResourceContinuous(resource, index * 24, -21, 24, 25, 4, 4, 4, 4); + record.representation.drawEntry(gui, index * 24 + 4, -17, false); + timer.setState(JecGui.mouseIn(index * 24, -21, 24, 21, xMouse, yMouse)); + if (timer.getTime() > 1000) + gui.drawTooltip(xMouse, yMouse, LocalizationHelper.format("gui." + record.localizeKey)); + } + + @Override + public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) { + boolean ret = JecGui.mouseIn(index * 24, -21, 24, 21, xMouse, yMouse) && listener != null && !focused; + if (ret) listener.run(); + return ret; + } + + public DPage setListener(Runnable r) { + listener = r; + return this; + } +} diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DPager.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DPager.java deleted file mode 100644 index 92e2a0d..0000000 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DPager.java +++ /dev/null @@ -1,45 +0,0 @@ -package me.towdium.jecalculation.client.gui.drawables; - -import me.towdium.jecalculation.client.gui.JecGui; -import me.towdium.jecalculation.client.gui.Resource; -import me.towdium.jecalculation.client.gui.IDrawable; -import me.towdium.jecalculation.core.labels.ILabel; -import me.towdium.jecalculation.utils.Utilities; - -import javax.annotation.ParametersAreNonnullByDefault; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.Stream; - -/** - * Author: towdium - * Date: 17-8-19. - */ -@ParametersAreNonnullByDefault -public class DPager extends DContainer { - public DPager() { - // TODO - //add(new DPage(0, ILabel.DESERIALIZER.getRecords().stream().filter(r -> r.representation != null).findFirst().get())); - } - - public class DPage implements IDrawable { - protected int index; - protected ILabel.RegistryEditor.Record record; - - public DPage(int index, ILabel.RegistryEditor.Record record) { - this.index = index; - this.record = record; - } - - @Override - public void onDraw(JecGui gui, int xMouse, int yMouse) { - gui.drawResourceContinuous(Resource.WGT_PAGER_B, index * 24, -21, 24, 24, 4, 4, 4, 4); - } - - @Override - public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) { - return false; - } - } -} diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DScroll.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DScroll.java index 5ce6e90..1083b74 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DScroll.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DScroll.java @@ -36,9 +36,9 @@ public class DScroll implements IDrawable { this.xSize = xSize; this.yPos = yPos; this.keys = keys; - left = new DButton(xPos, yPos, SIZE, SIZE, Resource.WGT_ARR_L_N, Resource.WGT_ARR_L_F) + left = new DButtonIcon(xPos, yPos, SIZE, SIZE, Resource.WGT_ARR_L_N, Resource.WGT_ARR_L_F) .setListenerLeft(() -> index.prev()); - right = new DButton(xPos + xSize - SIZE, yPos, SIZE, SIZE, + right = new DButtonIcon(xPos + xSize - SIZE, yPos, SIZE, SIZE, Resource.WGT_ARR_R_N, Resource.WGT_ARR_R_F) .setListenerLeft(() -> index.next()); wRect = new DRectangle(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, JecGui.COLOR_GREY); diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/DTooltip.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DTooltip.java new file mode 100644 index 0000000..6ffda5f --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/DTooltip.java @@ -0,0 +1,38 @@ +package me.towdium.jecalculation.client.gui.drawables; + +import me.towdium.jecalculation.client.gui.IDrawable; +import me.towdium.jecalculation.client.gui.JecGui; +import me.towdium.jecalculation.utils.Utilities; +import me.towdium.jecalculation.utils.helpers.LocalizationHelper; +import me.towdium.jecalculation.utils.wrappers.Pair; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +/** + * Author: towdium + * Date: 17-9-16. + */ +@ParametersAreNonnullByDefault +public abstract class DTooltip implements IDrawable { + @Nullable + public String name; + Utilities.Timer timer = new Utilities.Timer(); + + public DTooltip(@Nullable String name) { + this.name = name; + } + + @Override + public void onDraw(JecGui gui, int xMouse, int yMouse) { + if (name != null) { + timer.setState(mouseIn(xMouse, yMouse)); + if (timer.getTime() > 500) { + Pair str = LocalizationHelper.search(String.join(".", "gui", name, "tooltip")); + if (str.two || JecGui.ALWAYS_TOOLTIP) gui.drawTooltip(xMouse, yMouse, str.one); + } + } + } + + abstract boolean mouseIn(int xMouse, int yMouse); +} diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java index 264ff3a..d90ec7a 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java @@ -17,16 +17,19 @@ public class GuiCalculator extends DContainer { public GuiCalculator() { add(new DPanel()); add(new DTextField(61, 7, 64)); - add(new DButton(7, 7, 20, 20, Resource.BTN_LABEL_N, Resource.BTN_LABEL_F, "label") - .setListenerLeft(() -> JecGui.displayGui(new GuiLabelPicker()))); - add(new DButton(130, 7, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "recipe") - .setListenerLeft(() -> JecGui.displayGui(new GuiEditor()))); - add(new DButton(149, 7, 20, 20, Resource.BTN_SEARCH_N, Resource.BTN_SEARCH_F, "search")); + add(new DButtonIcon(7, 7, 20, 20, Resource.BTN_LABEL_N, Resource.BTN_LABEL_F, "calculator.label") + .setListenerLeft(() -> JecGui.displayGui(new GuiLabel(l -> { + JecGui.displayGui(this); + JecGui.getCurrent().hand = l; + })))); + add(new DButtonIcon(130, 7, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "calculator.recipe") + .setListenerLeft(() -> JecGui.displayGui(new GuiRecipe()))); + add(new DButtonIcon(149, 7, 20, 20, Resource.BTN_SEARCH_N, Resource.BTN_SEARCH_F, "calculator.search")); add(new DLabelGroup(7, 87, 9, 4, RESULT)); add(new DLabelGroup(7, 31, 8, 1, PICKER)); add(new DLabel(31, 7, 20, 20, SELECTOR)); add(new DLine(52)); - add(new DIcon(151, 31, 18, 18, Resource.ICN_RECENT_N, Resource.ICN_RECENT_F, "history")); + add(new DIcon(151, 31, 18, 18, Resource.ICN_RECENT_N, Resource.ICN_RECENT_F, "calculator.history")); add(new DScroll(7, 56, 162, 5)); } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiLabel.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiLabel.java new file mode 100644 index 0000000..695bfdc --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiLabel.java @@ -0,0 +1,53 @@ +package me.towdium.jecalculation.client.gui.guis; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.cache.Weigher; +import me.towdium.jecalculation.client.gui.IDrawable; +import me.towdium.jecalculation.client.gui.drawables.DContainer; +import me.towdium.jecalculation.client.gui.drawables.DPage; +import me.towdium.jecalculation.client.gui.drawables.DPanel; +import me.towdium.jecalculation.core.labels.ILabel; +import me.towdium.jecalculation.utils.wrappers.Single; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.function.Consumer; + +/** + * Author: towdium + * Date: 17-9-14. + */ +@ParametersAreNonnullByDefault +public class GuiLabel extends DContainer { + DContainer container = new DContainer(); + Consumer callback; + LoadingCache cache = CacheBuilder.newBuilder().concurrencyLevel(1) + .maximumWeight(16).weigher((Weigher) (key, value) -> 1) + .build(new CacheLoader() { + @Override + public IDrawable load(Integer i) { + ILabel.RegistryEditor.Record record = ILabel.EDITOR.getRecords().get(i); + return record.editor.setCallback(callback); + } + }); + + public GuiLabel(Consumer callback) { + this.callback = callback; + Single index = new Single<>(0); + ILabel.EDITOR.getRecords().forEach(r -> { + int i = index.value; + add(new DPage(0, r, false).setListener(() -> refresh(i))); + index.value += 1; + }); + add(new DPanel()); + add(container); + refresh(0); + } + + protected void refresh(int index) { + container.clear(); + container.add(cache.getUnchecked(index)); + container.add(new DPage(index, ILabel.EDITOR.getRecords().get(index), true)); + } +} diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiLabelPicker.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiLabelPicker.java deleted file mode 100644 index 8909cbc..0000000 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiLabelPicker.java +++ /dev/null @@ -1,16 +0,0 @@ -package me.towdium.jecalculation.client.gui.guis; - -import me.towdium.jecalculation.client.gui.drawables.DContainer; -import me.towdium.jecalculation.client.gui.drawables.DPager; -import me.towdium.jecalculation.client.gui.drawables.DPanel; - -/** - * Author: towdium - * Date: 17-9-14. - */ -public class GuiLabelPicker extends DContainer { - public GuiLabelPicker() { - add(new DPager()); - add(new DPanel()); - } -} diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiEditor.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiRecipe.java similarity index 58% rename from src/main/java/me/towdium/jecalculation/client/gui/guis/GuiEditor.java rename to src/main/java/me/towdium/jecalculation/client/gui/guis/GuiRecipe.java index f4215ff..0c1c8d1 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiEditor.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiRecipe.java @@ -9,26 +9,26 @@ import static me.towdium.jecalculation.client.gui.drawables.DLabel.enumMode.EDIT * Author: towdium * Date: 17-9-8. */ -public class GuiEditor extends DContainer { - DButton buttonSave = new DButton(26, 33, 20, 20, Resource.BTN_SAVE_N, Resource.BTN_SAVE_F, "save"); - DButton buttonCopy = new DButton(83, 33, 20, 20, Resource.BTN_COPY_N, Resource.BTN_COPY_F, "copy"); - DButton buttonDel = new DButton(64, 33, 20, 20, Resource.BTN_DEL_N, Resource.BTN_DEL_F, "clear"); - DButton buttonLabel = new DButton(45, 33, 20, 20, Resource.BTN_LABEL_N, Resource.BTN_LABEL_F, "label"); - DButton buttonYes = new DButton(7, 33, 20, 20, Resource.BTN_YES_N, Resource.BTN_YES_F, "confirm"); - DButton buttonNo = new DButton(26, 33, 20, 20, Resource.BTN_NO_N, Resource.BTN_NO_F, "abort"); +public class GuiRecipe extends DContainer { + DButton buttonSave = new DButtonIcon(26, 33, 20, 20, Resource.BTN_SAVE_N, Resource.BTN_SAVE_F, "recipe.save"); + DButton buttonCopy = new DButtonIcon(83, 33, 20, 20, Resource.BTN_COPY_N, Resource.BTN_COPY_F, "recipe.copy"); + DButton buttonDel = new DButtonIcon(64, 33, 20, 20, Resource.BTN_DEL_N, Resource.BTN_DEL_F, "recipe.clear"); + DButton buttonLabel = new DButtonIcon(45, 33, 20, 20, Resource.BTN_LABEL_N, Resource.BTN_LABEL_F, "recipe.label"); + DButton buttonYes = new DButtonIcon(7, 33, 20, 20, Resource.BTN_YES_N, Resource.BTN_YES_F, "recipe.confirm"); + DButton buttonNo = new DButtonIcon(26, 33, 20, 20, Resource.BTN_NO_N, Resource.BTN_NO_F, "recipe.abort"); DLabelGroup groupInput = new DLabelGroup(28, 111, 7, 2, 20, 20, EDITOR); DLabelGroup groupCatalyst = new DLabelGroup(28, 87, 7, 1, 20, 20, EDITOR); DLabelGroup groupOutput = new DLabelGroup(28, 63, 7, 1, 20, 20, EDITOR); DTextField textField = new DTextField(49, 33, 119); - DButton buttonNew = new DButton(7, 33, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "new") + DButton buttonNew = new DButtonIcon(7, 33, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "recipe.new") .setListenerLeft(() -> setModeNewGroup(true)); - public GuiEditor() { + public GuiRecipe() { add(new DPanel()); add(new DScroll(7, 7, 162, 2)); - add(new DIcon(7, 63, 21, 20, Resource.ICN_OUTPUT_N, Resource.ICN_OUTPUT_F, "output")); - add(new DIcon(7, 87, 21, 20, Resource.ICN_CATALYST_N, Resource.ICN_CATALYST_F, "catalyst")); - add(new DIcon(7, 111, 21, 40, Resource.ICN_INPUT_N, Resource.ICN_INPUT_F, "input")); + add(new DIcon(7, 63, 21, 20, Resource.ICN_OUTPUT_N, Resource.ICN_OUTPUT_F, "recipe.output")); + add(new DIcon(7, 87, 21, 20, Resource.ICN_CATALYST_N, Resource.ICN_CATALYST_F, "recipe.catalyst")); + add(new DIcon(7, 111, 21, 40, Resource.ICN_INPUT_N, Resource.ICN_INPUT_F, "recipe.input")); add(new DLine(57)); addAll(groupInput, groupCatalyst, groupOutput); setModeNewGroup(false); 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 3d31f57..8c1678e 100644 --- a/src/main/java/me/towdium/jecalculation/core/labels/ILabel.java +++ b/src/main/java/me/towdium/jecalculation/core/labels/ILabel.java @@ -28,7 +28,10 @@ public interface ILabel { RegistryDeserializer DESERIALIZER = RegistryDeserializer.INSTANCE; RegistryConverterItem CONVERTER_ITEM = RegistryConverterItem.INSTANCE; RegistryConverterFluid CONVERTER_FLUID = RegistryConverterFluid.INSTANCE; + RegistryEditor EDITOR = RegistryEditor.INSTANCE; ILabel EMPTY = new LabelItemStack(ItemStackHelper.EMPTY_ITEM_STACK, 0); + String LOCALIZE_PREFIX = "label"; + String LOCALIZE_SUFFIX = "name"; ILabel increaseAmount(); @@ -215,7 +218,7 @@ public interface ILabel { static { INSTANCE = new RegistryEditor(); - INSTANCE.register(LabelOreDict.getEditor(), "ore_dict", new LabelOreDict("ingotIron")); + INSTANCE.register(LabelOreDict.getEditor(), "common.label.ore_dict", new LabelOreDict("ingotIron")); } private ArrayList records = new ArrayList<>(); @@ -227,22 +230,22 @@ public interface ILabel { records.add(new Record(editor, unlocalizedName, representation)); } - public Collection getRecords() { + public List getRecords() { return records; } public interface IEditor extends IDrawable { - void setCallback(Consumer callback); + IEditor setCallback(Consumer callback); } public static class Record { public IEditor editor; - public String unlocalizedName; + public String localizeKey; public ILabel representation; - public Record(IEditor editor, String unlocalizedName, ILabel representation) { + public Record(IEditor editor, String localizeKey, ILabel representation) { this.editor = editor; - this.unlocalizedName = unlocalizedName; + this.localizeKey = localizeKey; this.representation = representation; } } 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 4c12012..ac3210f 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 @@ -1,6 +1,7 @@ package me.towdium.jecalculation.core.labels.labels; import me.towdium.jecalculation.client.gui.JecGui; +import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.client.gui.drawables.DContainer; import me.towdium.jecalculation.client.gui.drawables.DText; import me.towdium.jecalculation.core.labels.ILabel; @@ -11,12 +12,14 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.oredict.OreDictionary; +import javax.annotation.ParametersAreNonnullByDefault; import java.util.function.Consumer; /** * Author: towdium * Date: 17-9-10. */ +@ParametersAreNonnullByDefault public class LabelOreDict extends LabelSimpleAmount { public static final String IDENTIFIER = "oreDict"; public static final String KEY_NAME = "name"; @@ -65,8 +68,9 @@ public class LabelOreDict extends LabelSimpleAmount { is.getItem().getSubItems(is.getItem(), CreativeTabs.tabAllSearch, list); } else list.add(is); }); + if(list.isEmpty()) return; long index = System.currentTimeMillis() / 1500; - + gui.drawResource(Resource.LBL_ORE_DICT, 0, 0); GlStateManager.pushMatrix(); GlStateManager.translate(1, 1, 0); GlStateManager.scale(14f / 16, 14f / 16, 1); @@ -83,15 +87,22 @@ public class LabelOreDict extends LabelSimpleAmount { } public static class Editor extends DContainer implements RegistryEditor.IEditor { - Consumer label; + Consumer callback; public Editor() { add(new DText(5, 5, JecGui.Font.DEFAULT_NO_SHADOW, "hello")); } @Override - public void setCallback(Consumer callback) { - label = callback; + public RegistryEditor.IEditor setCallback(Consumer callback) { + this.callback = callback; + return this; + } + + @Override + public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) { + callback.accept(new LabelOreDict("plankWood")); + return super.onClicked(gui, xMouse, yMouse, button); } } } diff --git a/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java b/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java index 439078b..0d0f1d0 100644 --- a/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java +++ b/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java @@ -4,8 +4,7 @@ import codechicken.nei.PositionedStack; import codechicken.nei.api.IOverlayHandler; import codechicken.nei.recipe.IRecipeHandler; import me.towdium.jecalculation.client.gui.JecGui; -import me.towdium.jecalculation.client.gui.guis.GuiCalculator; -import me.towdium.jecalculation.client.gui.guis.GuiEditor; +import me.towdium.jecalculation.client.gui.guis.GuiRecipe; import me.towdium.jecalculation.utils.ItemStackHelper; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiContainer; @@ -33,7 +32,7 @@ public class JecaOverlayHandler implements IOverlayHandler { ItemStack outputStack = recipe.getResultStack(recipeIndex).items[0]; Minecraft mc = Minecraft.getMinecraft(); - JecGui.displayGui(new GuiEditor()); + JecGui.displayGui(new GuiRecipe()); GuiContainer myGuiContainer = (GuiContainer) mc.currentScreen; if (myGuiContainer != null) { myGuiContainer.inventorySlots.getSlot(0).putStack(outputStack); diff --git a/src/main/java/me/towdium/jecalculation/utils/helpers/LocalizationHelper.java b/src/main/java/me/towdium/jecalculation/utils/helpers/LocalizationHelper.java index c557fb0..d8b37b0 100644 --- a/src/main/java/me/towdium/jecalculation/utils/helpers/LocalizationHelper.java +++ b/src/main/java/me/towdium/jecalculation/utils/helpers/LocalizationHelper.java @@ -23,35 +23,7 @@ public class LocalizationHelper { return ret; } - /** - * @param c the (super) class name will be used after prefix translation key - * @param prefix the prefix of translation key - * @param translateKey the body of translation key - * @param parameters format parameters - * @return the localized value - * The localized key is "prefix.className(superClassName).translateKey". - * It will search all the super classes. - */ - public static Pair search(Class c, String prefix, String translateKey, Object... parameters) { - String clazz = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, c.getSimpleName()); - String key = String.join(".", prefix, clazz, translateKey); - Pair ret = LocalizationHelper.search(key, parameters); - if (ret.two || c.equals(Object.class)) - return ret; - else { - ret = search(c.getSuperclass(), prefix, translateKey, parameters); - if (ret.two) - return ret; - else - return ret.setOne(key); - } - } - public static String format(String translateKey, Object... parameters) { return search(translateKey, parameters).one; } - - public static String format(Class c, String prefix, String translateKey, Object... parameters) { - return search(c, prefix, translateKey, parameters).one; - } } diff --git a/src/main/resources/assets/jecalculation/lang/en_US.lang b/src/main/resources/assets/jecalculation/lang/en_US.lang index 47990dd..a3da9ad 100644 --- a/src/main/resources/assets/jecalculation/lang/en_US.lang +++ b/src/main/resources/assets/jecalculation/lang/en_US.lang @@ -10,6 +10,6 @@ command.state.s_client=client only command.state.s_both=client with server command.state.s_undefined=error state -gui.gui_calculator.tooltip.new_recipe=New recipe +gui.gui_calculator.name.new_recipe=New recipe config.force_client=Force Client diff --git a/src/main/resources/assets/jecalculation/textures/gui/resources.png b/src/main/resources/assets/jecalculation/textures/gui/resources.png index 626acce..86315cb 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