diff --git a/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java b/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java index b34da81..f3f29f6 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java @@ -3,6 +3,7 @@ package me.towdium.jecalculation.command.commands; import me.towdium.jecalculation.command.ISubCommand; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; +import net.minecraft.entity.Entity; import net.minecraft.util.ChatComponentText; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/src/main/java/me/towdium/jecalculation/data/label/ILabel.java b/src/main/java/me/towdium/jecalculation/data/label/ILabel.java index 0107f80..002c2f1 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/ILabel.java +++ b/src/main/java/me/towdium/jecalculation/data/label/ILabel.java @@ -2,6 +2,7 @@ package me.towdium.jecalculation.data.label; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.data.label.labels.LFluidStack; import me.towdium.jecalculation.data.label.labels.LItemStack; import me.towdium.jecalculation.data.label.labels.LOreDict; @@ -39,7 +40,7 @@ import java.util.stream.Collectors; @ParametersAreNonnullByDefault public interface ILabel { Merger MERGER = new Merger(); - Serializer DESERIALIZER = new Serializer(); + Serializer SERIALIZER = new Serializer(); Converter CONVERTER = new Converter(); RegistryEditor EDITOR = new RegistryEditor(); ILabel EMPTY = new LEmpty(); @@ -62,22 +63,26 @@ public interface ILabel { MERGER.register("itemStack", "itemStack", LItemStack::merge); MERGER.register("oreDict", "oreDict", LOreDict::merge); MERGER.register("itemStack", "oreDict", LOreDict::merge); + MERGER.register("fluidStack", "fluidStack", Impl::merge); + MERGER.register("fluidStack", "fluidStack", Impl::merge); } int getAmount(); ILabel multiply(int i); + @SuppressWarnings("BooleanMethodIsAlwaysInverted") boolean acceptPercent(); + @SuppressWarnings("UnusedReturnValue") ILabel setPercent(boolean p); static void initServer() { - DESERIALIZER.register(LFluidStack.IDENTIFIER, LFluidStack::new); - DESERIALIZER.register(LItemStack.IDENTIFIER, LItemStack::new); - DESERIALIZER.register(LOreDict.IDENTIFIER, LOreDict::new); - DESERIALIZER.register(LPlaceholder.IDENTIFIER, LPlaceholder::new); - DESERIALIZER.register(LEmpty.IDENTIFIER, i -> EMPTY); + SERIALIZER.register(LFluidStack.IDENTIFIER, LFluidStack::new); + SERIALIZER.register(LItemStack.IDENTIFIER, LItemStack::new); + SERIALIZER.register(LOreDict.IDENTIFIER, LOreDict::new); + SERIALIZER.register(LPlaceholder.IDENTIFIER, LPlaceholder::new); + SERIALIZER.register(LEmpty.IDENTIFIER, i -> EMPTY); } String getAmountString(); @@ -175,7 +180,12 @@ public interface ILabel { */ public ILabel deserialize(NBTTagCompound nbt) { String s = nbt.getString(KEY_IDENTIFIER); - return idToData.get(s).apply(nbt.getCompoundTag(KEY_CONTENT)); + Function func = idToData.get(s); + if (func != null) return func.apply(nbt.getCompoundTag(KEY_CONTENT)); + else { + JustEnoughCalculation.logger.warn("Unrecognized type identifier \"" + s + "\", use empty instead."); + return ILabel.EMPTY; + } } public NBTTagCompound serialize(ILabel label) { @@ -396,7 +406,12 @@ public interface ILabel { existing.add(FORMAT_GREY + Utilities.I18n.format("label.common.tooltip.amount", getAmountString())); } - protected static Optional merge(Impl a, Impl b, boolean add) { + public static Optional merge(ILabel a, ILabel b, boolean add) { + if (a instanceof Impl && b instanceof Impl && a.matches(b)) return mergeUnchecked((Impl) a, (Impl) b, add); + else return Optional.empty(); + } + + protected static Optional mergeUnchecked(Impl a, Impl b, boolean add) { Impl ret = a.copy(); boolean p = a.percent || b.percent; int amountA = a.amount; diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LItemStack.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LItemStack.java index cde1aa7..25c32aa 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LItemStack.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LItemStack.java @@ -92,7 +92,7 @@ public class LItemStack extends ILabel.Impl { if (lisA.item == lisB.item && checkedMeta && checkedNbt) { LItemStack is = lisA.copy(); is.meta = resultMeta; - return Impl.merge(is, lisB, add); + return Impl.mergeUnchecked(is, lisB, add); } } return Optional.empty(); diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LOreDict.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LOreDict.java index af7a0a5..4790311 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LOreDict.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LOreDict.java @@ -64,7 +64,7 @@ public class LOreDict extends ILabel.Impl { LOreDict lodA = (LOreDict) a; LOreDict lodB = (LOreDict) b; if (lodA.getName().equals(lodB.getName())) { - return Impl.merge(lodA, lodB, add); + return Impl.mergeUnchecked(lodA, lodB, add); } } else if ((a instanceof LOreDict && b instanceof LItemStack) || a instanceof LItemStack && b instanceof LOreDict) { @@ -159,12 +159,12 @@ public class LOreDict extends ILabel.Impl { }); if(list.isEmpty()) return; long index = System.currentTimeMillis() / 1500; - gui.drawResource(Resource.LBL_FRAME, 0, 0); - GlStateManager.pushMatrix(); - GlStateManager.translate(1, 1, 0); - GlStateManager.scale(14f / 16, 14f / 16, 1); + //GlStateManager.pushMatrix(); + //GlStateManager.translate(1, 1, 0); + //GlStateManager.scale(14f / 16, 14f / 16, 1); gui.drawItemStack(0, 0, list.get((int) (index % list.size())), false); - GlStateManager.popMatrix(); + //GlStateManager.popMatrix(); + gui.drawResource(Resource.LBL_FRAME, 0, 0); } @Override diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LPlaceholder.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LPlaceholder.java index 13898fe..a83b509 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LPlaceholder.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LPlaceholder.java @@ -20,7 +20,7 @@ import java.util.stream.Collectors; @ParametersAreNonnullByDefault public class LPlaceholder extends ILabel.Impl { public static final String KEY_NAME = "name"; - public static final String IDENTIFIER = "string"; + public static final String IDENTIFIER = "placeholder"; static Utilities.Recent recentClient = new Utilities.Recent<>(100); static Utilities.Recent recentServer = new Utilities.Recent<>(100); @@ -51,7 +51,7 @@ public class LPlaceholder extends ILabel.Impl { @Override public String getIdentifier() { - return "string"; + return IDENTIFIER; } public static List getRecent() { diff --git a/src/main/java/me/towdium/jecalculation/data/structure/Recents.java b/src/main/java/me/towdium/jecalculation/data/structure/Recents.java index 9bac747..3c09d2a 100644 --- a/src/main/java/me/towdium/jecalculation/data/structure/Recents.java +++ b/src/main/java/me/towdium/jecalculation/data/structure/Recents.java @@ -17,7 +17,7 @@ public class Recents { public Recents(NBTTagList nbt) { List ls = StreamSupport.stream(NBTHelper.spliterator(nbt), false) .filter(n -> n instanceof NBTTagCompound) - .map(n -> ILabel.DESERIALIZER.deserialize((NBTTagCompound) n)) + .map(n -> ILabel.SERIALIZER.deserialize((NBTTagCompound) n)) .collect(Collectors.toList()); new Utilities.ReversedIterator<>(ls).forEachRemaining(l -> record.push(l)); } @@ -39,7 +39,7 @@ public class Recents { public NBTTagList serialize() { NBTTagList ret = new NBTTagList(); - record.toList().forEach(l -> ret.appendTag(ILabel.DESERIALIZER.serialize(l))); + record.toList().forEach(l -> ret.appendTag(ILabel.SERIALIZER.serialize(l))); return ret; } } diff --git a/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java b/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java index b146b7d..feda68d 100644 --- a/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java +++ b/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java @@ -64,7 +64,7 @@ public class Recipe { static private List readNbtList(NBTTagList list) { return StreamSupport.stream(NBTHelper.spliterator(list), false) .filter(n -> n instanceof NBTTagCompound) - .map(n -> ILabel.DESERIALIZER.deserialize((NBTTagCompound) n)) + .map(n -> ILabel.SERIALIZER.deserialize((NBTTagCompound) n)) .collect(Collectors.toList()); } @@ -96,7 +96,7 @@ public class Recipe { NBTTagCompound ret = new NBTTagCompound(); Function convert = (ls) -> { NBTTagList r = new NBTTagList(); - Arrays.stream(ls).forEach(l -> r.appendTag(ILabel.DESERIALIZER.serialize(l))); + Arrays.stream(ls).forEach(l -> r.appendTag(ILabel.SERIALIZER.serialize(l))); return r; }; ret.setTag(KEY_INPUT, convert.apply(input)); diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/EBEventHandler.java b/src/main/java/me/towdium/jecalculation/event/handlers/EBEventHandler.java index 6e4da1a..62d7c34 100644 --- a/src/main/java/me/towdium/jecalculation/event/handlers/EBEventHandler.java +++ b/src/main/java/me/towdium/jecalculation/event/handlers/EBEventHandler.java @@ -22,4 +22,5 @@ public class EBEventHandler { event.setCanceled(gui.handleMouseEvent()); } } + } diff --git a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java index b17260b..8db55e0 100644 --- a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java +++ b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java @@ -8,11 +8,8 @@ import me.towdium.jecalculation.data.label.ILabel; import me.towdium.jecalculation.gui.drawables.WContainer; import me.towdium.jecalculation.nei.NEIPlugin; import me.towdium.jecalculation.polyfill.mc.client.renderer.GlStateManager; -import me.towdium.jecalculation.utils.IllegalPositionException; -import me.towdium.jecalculation.utils.wrappers.Wrapper; -import me.towdium.jecalculation.utils.wrappers.Triple; +import me.towdium.jecalculation.utils.Utilities; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.RenderHelper; @@ -22,7 +19,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; -import net.minecraft.util.ResourceLocation; import net.minecraftforge.fluids.Fluid; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; @@ -33,7 +29,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.function.Function; /** * Author: towdium @@ -44,6 +39,7 @@ import java.util.function.Function; public class JecaGui extends GuiContainer { public static final int COLOR_GUI_GREY = 0xFFA1A1A1; public static final int COLOR_TEXT_RED = 0xFF0000; + public static final int COLOR_TEXT_GREY = 0x404040; public static final int COLOR_TEXT_WHITE = 0xFFFFFF; public static final boolean ALWAYS_TOOLTIP = false; public ILabel hand = ILabel.EMPTY; @@ -88,6 +84,8 @@ public class JecaGui extends GuiContainer { return ret; } + + private static void displayGuiUnsafe(boolean updateParent, boolean acceptsTransfer, WContainer root) { Minecraft mc = Minecraft.getMinecraft(); JecaGui parent; @@ -121,11 +119,11 @@ public class JecaGui extends GuiContainer { @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { + drawDefaultBackground(); } @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { - drawDefaultBackground(); super.drawScreen(mouseX, mouseY, partialTicks); GlStateManager.pushMatrix(); GlStateManager.translate(guiLeft, guiTop, 0); @@ -169,8 +167,9 @@ public class JecaGui extends GuiContainer { JustEnoughCalculation.logger.info("handle mouse clicked"); int xMouse = Mouse.getEventX() * this.width / this.mc.displayWidth; int yMouse = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; + int button = Mouse.getEventButton(); if (Mouse.getEventButtonState()) { - if (Mouse.getEventButton() == 0) { + if (button == 0) { if (hand == ILabel.EMPTY) { ILabel e = NEIPlugin.getLabelUnderMouse(); if (e != ILabel.EMPTY) { @@ -183,7 +182,7 @@ public class JecaGui extends GuiContainer { return true; } } - } else if (Mouse.getEventButton() == 1) { + } else if (button == 1) { if (hand != ILabel.EMPTY) { hand = ILabel.EMPTY; return true; @@ -193,56 +192,29 @@ public class JecaGui extends GuiContainer { return false; } - public FontRenderer getFontRenderer() { - return fontRendererObj; - } - public void drawResource(Resource r, int xPos, int yPos) { drawResource(r, xPos, yPos, 0xFFFFFF); } public void drawResource(Resource r, int xPos, int yPos, int color) { - drawTexture(r.getResourceLocation(), xPos, yPos, r.getXPos(), r.getYPos(), r.getXSize(), r.getYSize(), color); - } - - public void drawResourceContinuous(Resource r, - int xPos, - int yPos, - int xSize, - int ySize, - int borderTop, - int borderBottom, - int borderLeft, - int borderRight) { - drawTextureContinuous(r.getResourceLocation(), xPos, yPos, xSize, ySize, r.getXPos(), r.getYPos(), r.getXSize(), - r.getYSize(), borderTop, borderBottom, borderLeft, borderRight); - } - - public void drawTexture(ResourceLocation l, - int destXPos, - int destYPos, - int sourceXPos, - int sourceYPos, - int sourceXSize, - int sourceYSize) { - drawTexture(l, destXPos, destYPos, sourceXPos, sourceYPos, sourceXSize, sourceYSize, 0xFFFFFF); - } - - public void drawTexture(ResourceLocation l, - int destXPos, - int destYPos, - int sourceXPos, - int sourceYPos, - int sourceXSize, - int sourceYSize, - int color) { float red = (color >> 16 & 0xFF) / 255.0F; float green = (color >> 8 & 0xFF) / 255.0F; float blue = (color & 0xFF) / 255.0F; float alpha = (~(color >> 24) & 0xFF) / 255.0F; GlStateManager.color(red, green, blue, alpha); - mc.getTextureManager().bindTexture(l); - drawTexturedModalRect(destXPos, destYPos, sourceXPos, sourceYPos, sourceXSize, sourceYSize); + mc.getTextureManager().bindTexture(r.getResourceLocation()); + drawTexturedModalRect(xPos, yPos, r.getXPos(), r.getYPos(), r.getXSize(), r.getYSize()); + } + + public void drawResourceContinuous(Resource r, int xPos, int yPos, int xSize, int ySize, int border) { + drawResourceContinuous(r, xPos, yPos, xSize, ySize, border, border, border, border); + } + + public void drawResourceContinuous( + Resource r, int xPos, int yPos, int xSize, int ySize, + int borderTop, int borderBottom, int borderLeft, int borderRight) { + GuiUtils.drawContinuousTexturedBox(r.getResourceLocation(), xPos, yPos, r.getXPos(), r.getYPos(), xSize, ySize, + r.getXSize(), r.getYSize(), borderTop, borderBottom, borderLeft, borderRight, zLevel); } public void drawFluid(Fluid f, int xPos, int yPos, int xSize, int ySize) { @@ -257,24 +229,6 @@ public class JecaGui extends GuiContainer { drawTexturedModelRectFromIcon(xPos, yPos, fluidStillIcon, xSize, ySize); } - public void drawTextureContinuous(ResourceLocation l, - int destXPos, - int destYPos, - int destXSize, - int destYSize, - int sourceXPos, - int sourceYPos, - int sourceXSize, - int sourceYSize, - int borderTop, - int borderBottom, - int borderLeft, - int borderRight) { - GuiUtils.drawContinuousTexturedBox(l, destXPos, destYPos, sourceXPos, sourceYPos, destXSize, destYSize, - sourceXSize, sourceYSize, borderTop, borderBottom, borderLeft, borderRight, - zLevel); - } - public void drawRectangle(int xPos, int yPos, int xSize, int ySize, int color) { float f3 = (float) (color >> 24 & 255) / 255.0F; float f = (float) (color >> 16 & 255) / 255.0F; @@ -296,75 +250,50 @@ public class JecaGui extends GuiContainer { GlStateManager.enableTexture2D(); } - - public void drawText(float xPos, float yPos, Font f, String... text) { - Function indenter; - switch (f.align) { - case AUTO: - case LEFT: - indenter = s -> 0; - break; - case CENTRE: - indenter = s -> fontRendererObj.getStringWidth(s) / 2; - break; - case RIGHT: - indenter = s -> -fontRendererObj.getStringWidth(s); - break; - default: - throw new IllegalPositionException(); - } - drawText(xPos, yPos, f, indenter, text); + public void drawSplitText(float xPos, float yPos, Font f, String s) { + drawSplitText(xPos, yPos, Integer.MAX_VALUE, f, s); } - private void drawText(float xPos, float yPos, Font f, Function indenter, String... text) { - Wrapper y = new Wrapper<>(0); + public void drawSplitText(float xPos, float yPos, int width, Font f, String s) { + drawSplitText(xPos, yPos, f, Utilities.I18n.wrap(s, width)); + } + + public void drawSplitText(float xPos, float yPos, Font f, List ss) { + drawText(xPos, yPos, f, () -> { + int y = 0; + for (String i : ss) { + fontRendererObj.drawString(i, 0, y, f.color, f.shadow); + y += fontRendererObj.FONT_HEIGHT + 1; + } + }); + } + + public void drawText(float xPos, float yPos, Font f, String s) { + drawText(xPos, yPos, Integer.MAX_VALUE, f, s); + } + + public void drawText(float xPos, float yPos, int width, Font f, String s) { + drawText(xPos, yPos, f, () -> { + String str = s; + int strWidth = f.getTextWidth(str); + int ellipsisWidth = f.getTextWidth("..."); + if (strWidth > width && strWidth > ellipsisWidth) + str = f.trimToWidth(str, width - ellipsisWidth).trim() + "..."; + fontRendererObj.drawString(str, 0, 0, f.color, f.shadow); + }); + } + + private void drawText(float xPos, float yPos, Font f, Runnable r) { boolean unicode = fontRendererObj.getUnicodeFlag(); - if (!f.unicode) - fontRendererObj.setUnicodeFlag(false); + if (f.half) fontRendererObj.setUnicodeFlag(false); GlStateManager.pushMatrix(); GlStateManager.translate(xPos, yPos, 0); - GlStateManager.scale(f.size, f.size, 1); - Arrays.stream(text).forEachOrdered(s -> { - fontRendererObj.drawString(s, indenter.apply(s), y.value, f.color, f.shadow); - y.value += fontRendererObj.FONT_HEIGHT; - }); + if (f.half) GlStateManager.scale(0.5, 0.5, 1); + r.run(); GlStateManager.popMatrix(); fontRendererObj.setUnicodeFlag(unicode); } - public void drawText(float xPos, float yPos, float xSize, Font f, String... text) { - float sizeScaled = xSize / f.size; - int l = fontRendererObj.getStringWidth("..."); - String[] ss = !f.cut ? text : Arrays.stream(text).map(s -> { - int w = fontRendererObj.getStringWidth(s); - if (w <= sizeScaled) - return s; - else if (l >= w) - return "..."; - else - return fontRendererObj.trimStringToWidth(s, (int) (sizeScaled - l)) + "..."; - }).toArray(String[]::new); - switch (f.align) { - case LEFT: - drawText(xPos, yPos, f, s -> 0, ss); - break; - case AUTO: - case CENTRE: - drawText(xPos, yPos, f, s -> ((int) sizeScaled - fontRendererObj.getStringWidth(s)) / 2, ss); - break; - case RIGHT: - drawText(xPos, yPos, f, s -> (int) sizeScaled - fontRendererObj.getStringWidth(s), ss); - break; - default: - throw new IllegalPositionException(); - } - } - - public void drawText(float xPos, float yPos, int xSize, int ySize, Font f, String... text) { - int yOffset = (ySize - (int) (text.length * fontRendererObj.FONT_HEIGHT * f.size)) / 2; - drawText(xPos, yPos + yOffset, xSize, f, text); - } - public void drawItemStack(int xPos, int yPos, ItemStack is, boolean centred) { if (is.getItem() == null) { return; @@ -382,6 +311,7 @@ public class JecaGui extends GuiContainer { @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) { + JustEnoughCalculation.logger.info("mouse clicked"); super.mouseClicked(mouseX, mouseY, mouseButton); boolean inGui = root.onClicked(this, mouseX - guiLeft, mouseY - guiTop, mouseButton); if (!inGui) { @@ -391,49 +321,39 @@ public class JecaGui extends GuiContainer { @Override protected void keyTyped(char typedChar, int keyCode) { - if (!root.onKey(this, typedChar, keyCode)) { - if (keyCode == Keyboard.KEY_ESCAPE) { - if (hand != ILabel.EMPTY) - hand = ILabel.EMPTY; - else if (parent != null) - Minecraft.getMinecraft().displayGuiScreen(parent); - else - super.keyTyped(typedChar, keyCode); - } + if (keyCode == Keyboard.KEY_ESCAPE && hand != ILabel.EMPTY) hand = ILabel.EMPTY; + else if (!root.onKey(this, typedChar, keyCode)) { + if (keyCode == Keyboard.KEY_ESCAPE && parent != null) displayParent(); + else super.keyTyped(typedChar, keyCode); } + } 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); - public static final Font DEFAULT_HALF = new Font(0xFFFFFF, true, true, true, 0.5f, enumAlign.AUTO); + public static final Font SHADOW = new Font(JecaGui.COLOR_TEXT_WHITE, true, false); + public static final Font PLAIN = new Font(JecaGui.COLOR_TEXT_GREY, false, false); + public static final Font HALF = new Font(JecaGui.COLOR_TEXT_WHITE, true, true); public int color; - public boolean shadow, cut, unicode; - public float size; - public enumAlign align; + public boolean shadow, half; - /** - * @param color foreground color - * @param shadow whether to draw shadow - * @param cut whether to cut string when exceed xSize - * @param unicode if false, FORCE NOT UNICODE - * @param size font size, 1 for default font size - */ - public Font(int color, boolean shadow, boolean cut, boolean unicode, float size, enumAlign align) { + public Font(int color, boolean shadow, boolean half) { this.color = color; this.shadow = shadow; - this.cut = cut; - this.size = size; - this.align = align; - this.unicode = unicode; + this.half = half; } - public Font copy() { - return new Font(color, shadow, cut, unicode, size, align); + public int getTextWidth(String s) { + return (int) Math.ceil(getCurrent().fontRendererObj.getStringWidth(s) * (half ? 0.5f : 1)); } - public enum enumAlign {LEFT, CENTRE, RIGHT, AUTO} + public int getTextHeight() { + return (int) Math.ceil(getCurrent().fontRendererObj.FONT_HEIGHT * (half ? 0.5f : 1)); + } + + public String trimToWidth(String s, int i) { + return getCurrent().fontRendererObj.trimStringToWidth(s, i * (half ? 2 : 1)); + } } diff --git a/src/main/java/me/towdium/jecalculation/gui/Resource.java b/src/main/java/me/towdium/jecalculation/gui/Resource.java index ca4c4ab..6196ea7 100644 --- a/src/main/java/me/towdium/jecalculation/gui/Resource.java +++ b/src/main/java/me/towdium/jecalculation/gui/Resource.java @@ -72,10 +72,10 @@ public class Resource { public static final Resource WGT_BUTTON_D = new Resource(80, 232, 20, 20); public static final Resource WGT_SLOT = new Resource(20, 232, 20, 20); public static final Resource WGT_LINE = new Resource(0, 252, 163, 4); - public static final Resource WGT_PANEL = new Resource(0, 232, 20, 20); + public static final Resource WGT_PANEL_F = new Resource(0, 232, 20, 20); public static final Resource WGT_PAGER_FN = new Resource(100, 232, 20, 20); public static final Resource WGT_PAGER_F0 = new Resource(120, 232, 20, 20); - public static final Resource WGT_PAGER_N = new Resource(140, 232, 20, 20); + public static final Resource WGT_PANEL_N = new Resource(140, 232, 20, 20); public static final Resource WGT_ARR_L_F = new Resource(160, 232, 7, 9); public static final Resource WGT_ARR_L_N = new Resource(167, 232, 7, 9); public static final Resource WGT_ARR_L_D = new Resource(174, 232, 7, 9); @@ -83,6 +83,9 @@ public class Resource { public static final Resource WGT_ARR_R_N = new Resource(167, 242, 7, 9); public static final Resource WGT_ARR_R_D = new Resource(174, 242, 7, 9); public static final Resource WGT_SCROLL = new Resource(181, 232, 12, 15); + public static final Resource WGT_HELP_F = new Resource(193, 232, 20, 20); + public static final Resource WGT_HELP_N = new Resource(213, 232, 20, 20); + public static final Resource WGT_HELP_B = new Resource(233, 232, 20, 20); public static final Resource ICN_RECENT_N = getIcon(0, 4); public static final Resource ICN_RECENT_F = getIcon(0, 3); public static final Resource ICN_INPUT_N = getIcon(1, 4); diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WButton.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WButton.java index 9494373..f212030 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WButton.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WButton.java @@ -9,6 +9,9 @@ import me.towdium.jecalculation.utils.Utilities; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; /** * Author: towdium @@ -69,8 +72,14 @@ public abstract class WButton extends WTooltip { return JecaGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse); } + @SuppressWarnings("UnusedReturnValue") public WButton setDisabled(boolean b) { disabled = b; return this; } + + @Override + protected List getSuffix() { + return disabled ? Arrays.asList("disabled", "active") : Collections.singletonList("active"); + } } diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WButtonIcon.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WButtonIcon.java index 274bcce..ab85903 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WButtonIcon.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WButtonIcon.java @@ -7,9 +7,6 @@ import me.towdium.jecalculation.gui.Resource; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; /** * Author: towdium @@ -28,8 +25,14 @@ public class WButtonIcon extends WButton { this(xPos, yPos, xSize, ySize, res.normal, res.focused, res.disabled, null); } - private WButtonIcon(int xPos, int yPos, int xSize, int ySize, Resource normal, Resource focused, - @Nullable Resource disabled, @Nullable String name) { + private 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.rNormal = normal; this.rFocused = focused; @@ -40,11 +43,7 @@ public class WButtonIcon extends WButton { public void onDraw(JecaGui gui, int xMouse, int yMouse) { super.onDraw(gui, xMouse, yMouse); 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"); + 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/gui/drawables/WButtonText.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WButtonText.java index 078d0f2..727fec9 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WButtonText.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WButtonText.java @@ -13,23 +13,21 @@ import javax.annotation.ParametersAreNonnullByDefault; */ @ParametersAreNonnullByDefault @SideOnly(Side.CLIENT) -public class WButtonText extends WButton { - public WButtonText(int xPos, int yPos, int xSize, int ySize, String name) { +public class WButtonText extends WButton { // TODO need rework for text without need for localization + public static final JecaGui.Font focused = new JecaGui.Font(0xFFFFA0, true, false); + public static final JecaGui.Font normal = new JecaGui.Font(0xFFFFFF, true, false); + public String text; + + public WButtonText(int xPos, int yPos, int xSize, int ySize, String name, String text) { super(xPos, yPos, xSize, ySize, name); + this.text = text; } @Override public void onDraw(JecaGui gui, int xMouse, int yMouse) { super.onDraw(gui, xMouse, yMouse); - int textColor = mouseIn(xMouse, yMouse) ? 16777120 : 0; - String text = Utilities.I18n.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() + "..."; - JecaGui.Font f = JecaGui.Font.DEFAULT_SHADOW.copy(); - f.color = textColor; - gui.drawText(xPos, yPos, xSize, ySize, f, str); + JecaGui.Font font = mouseIn(xMouse, yMouse) ? focused : normal; + float x = xPos + Math.max(3, xSize / 2.0f - font.getTextWidth(text) / 2.0f); + gui.drawText(x, yPos + ySize / 2.0f - font.getTextHeight() / 2.0f, xSize - 6, font, text); } } diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WContainer.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WContainer.java index f8431bb..805f79a 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WContainer.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WContainer.java @@ -48,19 +48,22 @@ public class WContainer implements IWidget { @Override public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { - for (IWidget w : widgets) if (w.onClicked(gui, xMouse, yMouse, button)) return true; + Utilities.ReversedIterator i = new Utilities.ReversedIterator<>(widgets); + while (i.hasNext()) if (i.next().onClicked(gui, xMouse, yMouse, button)) return true; return false; } @Override public boolean onKey(JecaGui gui, char ch, int code) { - for (IWidget w : widgets) if (w.onKey(gui, ch, code)) return true; + Utilities.ReversedIterator i = new Utilities.ReversedIterator<>(widgets); + while (i.hasNext()) if (i.next().onKey(gui, ch, code)) return true; return false; } @Override public boolean onScroll(JecaGui gui, int xMouse, int yMouse, int diff) { - for (IWidget w : widgets) if (w.onScroll(gui, xMouse, yMouse, diff)) return true; + Utilities.ReversedIterator i = new Utilities.ReversedIterator<>(widgets); + while (i.hasNext()) if (i.next().onScroll(gui, xMouse, yMouse, diff)) return true; return false; } diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WHelp.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WHelp.java new file mode 100644 index 0000000..baf3dc3 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WHelp.java @@ -0,0 +1,127 @@ +package me.towdium.jecalculation.gui.drawables; + +import me.towdium.jecalculation.gui.IWidget; +import me.towdium.jecalculation.gui.JecaGui; +import me.towdium.jecalculation.gui.Resource; +import me.towdium.jecalculation.utils.Utilities.I18n; +import org.lwjgl.input.Keyboard; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.ArrayList; +import java.util.List; + +/** + * Author: Towdium + * Date: 18-9-23 + */ +@ParametersAreNonnullByDefault +public class WHelp extends WTooltip { + protected String key; + + public WHelp(String content) { + super("common.help"); + key = content; + } + + @Override + public void onDraw(JecaGui gui, int xMouse, int yMouse) { + gui.drawResourceContinuous(Resource.WGT_PANEL_N, -21, 0, 25, 24, 4); + gui.drawResource(Resource.WGT_HELP_N, -19, 2); + } + + @Override + public boolean mouseIn(int xMouse, int yMouse) { + return JecaGui.mouseIn(-17, 0, 20, 20, xMouse, yMouse); + } + + @Override + public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { + boolean ret = mouseIn(xMouse, yMouse); + if (ret) gui.root.add(new Doc()); + return ret; + } + + private class Doc extends WContainer { + public Doc() { + Text tContent = new Text(); + WSwitcher sPage = new WSwitcher(7, 146, 162, tContent.amount()); + sPage.setListener(() -> tContent.setPage(sPage.getIndex())); + WText tTitle = new WText(7, 7, JecaGui.Font.SHADOW, I18n.format("gui." + WHelp.this.key + ".title")); + addAll(new WPanel(), new Icon(), tTitle, tContent, sPage); + } + + public class Text implements IWidget { + List> pages = new ArrayList<>(); + int page; + + public Text() { + List ss = I18n.wrap(I18n.format("gui." + WHelp.this.key + ".help"), 162); + List tmp = new ArrayList<>(); + int count = 0; + for (String s : ss) { + if (s.endsWith("\f")) { + tmp.add(s.substring(0, s.length() - 1)); + pages.add(tmp); + tmp = new ArrayList<>(); + count = 0; + } else if (count == 11) { + tmp.add(s); + pages.add(tmp); + tmp = new ArrayList<>(); + count = 0; + } else { + tmp.add(s); + count++; + } + } + if (!tmp.isEmpty()) pages.add(tmp); + } + + @Override + public void onDraw(JecaGui gui, int xMouse, int yMouse) { + gui.drawSplitText(7, 21, JecaGui.Font.PLAIN, pages.get(page)); + } + + public int amount() { + return pages.size(); + } + + public void setPage(int i) { + page = i; + } + } + + public class Icon extends WTooltip { + public Icon() { + super("common.close"); + } + + @Override + public void onDraw(JecaGui gui, int xMouse, int yMouse) { + gui.drawResourceContinuous(Resource.WGT_HELP_B, -21, 0, 25, 24, 4); + gui.drawResource(Resource.WGT_HELP_F, -19, 2); + } + + @Override + public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { + boolean ret = mouseIn(xMouse, yMouse); + if (ret) gui.root.remove(Doc.this); + return ret; + } + + @Override + public boolean mouseIn(int xMouse, int yMouse) { + return JecaGui.mouseIn(-17, 0, 20, 20, xMouse, yMouse); + } + + @Override + public boolean onKey(JecaGui gui, char ch, int code) { + if (super.onKey(gui, ch, code)) return true; + if (code == Keyboard.KEY_ESCAPE) { + gui.root.remove(Doc.this); + return true; + } else return false; + } + } + } +} diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WLabel.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WLabel.java index 4162b26..5d1b30a 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WLabel.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WLabel.java @@ -14,6 +14,8 @@ import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; import java.util.stream.IntStream; +import static me.towdium.jecalculation.gui.JecaGui.Font.HALF; + /** * Author: towdium * Date: 17-8-17. @@ -21,13 +23,6 @@ import java.util.stream.IntStream; @ParametersAreNonnullByDefault @SideOnly(Side.CLIENT) public class WLabel implements IWidget { - static JecaGui.Font font; - - static { - font = JecaGui.Font.DEFAULT_HALF.copy(); - font.align = JecaGui.Font.enumAlign.RIGHT; - } - public int xPos, yPos, xSize, ySize; public ILabel label; public enumMode mode; @@ -55,27 +50,25 @@ public class WLabel implements IWidget { public void onDraw(JecaGui gui, int xMouse, int yMouse) { gui.drawResourceContinuous(Resource.WGT_SLOT, xPos, yPos, xSize, ySize, 3, 3, 3, 3); label.drawLabel(gui, xPos + xSize / 2, yPos + ySize / 2, true); - if (mode == enumMode.RESULT || mode == enumMode.EDITOR) - gui.drawText(xPos + xSize / 2.0f + 7.5f, - yPos + ySize / 2 + 7 - (int) (font.size * gui.getFontRenderer().FONT_HEIGHT), font, - label.getAmountString()); - if (mouseIn(xMouse, yMouse)) { - gui.drawRectangle(xPos + 1, yPos + 1, xSize - 2, ySize - 2, 0x80FFFFFF); + if (mode == enumMode.RESULT || mode == enumMode.EDITOR) { + String s = label.getAmountString(); + gui.drawText(xPos + xSize / 2.0f + 8 - HALF.getTextWidth(s), + yPos + ySize / 2.0f + 8.5f - HALF.getTextHeight(), HALF, s); } 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); } + if (mouseIn(xMouse, yMouse)) gui.drawRectangle(xPos + 1, yPos + 1, xSize - 2, ySize - 2, 0x80FFFFFF); } @Override public boolean onTooltip(JecaGui gui, int xMouse, int yMouse, List tooltip) { - if (mouseIn(xMouse, yMouse)) { - if (label != ILabel.EMPTY) { - tooltip.add(label.getDisplayName()); - label.getToolTip(tooltip, mode == enumMode.EDITOR || mode == enumMode.RESULT); - } + if (!mouseIn(xMouse, yMouse)) return false; + if (label != ILabel.EMPTY) { + tooltip.add(label.getDisplayName()); + label.getToolTip(tooltip, mode == enumMode.EDITOR || mode == enumMode.RESULT); } return false; } @@ -94,50 +87,33 @@ public class WLabel implements IWidget { @Override public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { - if (mouseIn(xMouse, yMouse)) { - switch (mode) { - case EDITOR: - if (gui.hand != label.EMPTY) { - label = gui.hand; - gui.hand = label.EMPTY; - notifyLsnr(); - return true; - } else if (label != label.EMPTY) { - if (button == 0) { - if (JecaGui.isShiftDown()) gui.root.add(new WAmount()); - else { - label = label.increaseAmount(); - notifyLsnr(); - } - return true; - } else if (button == 1) { - if (JecaGui.isShiftDown()) gui.root.add(new WAmount()); - else { - label = label.decreaseAmount(); - notifyLsnr(); - } - return true; - } - } else - return false; - case RESULT: - return false; - case PICKER: - if (label != label.EMPTY) { - notifyLsnr(); - return true; - } else - return false; - case SELECTOR: + if (!mouseIn(xMouse, yMouse)) return false; + switch (mode) { + case EDITOR: + if (gui.hand != label.EMPTY) { label = gui.hand; gui.hand = label.EMPTY; notifyLsnr(); return true; - default: - throw new IllegalPositionException(); - } - } else - return false; + } else if (label != label.EMPTY) { + gui.root.add(new WAmount()); + return true; + } else return false; + case RESULT: + return false; + case PICKER: + if (label != label.EMPTY) { + notifyLsnr(); + return true; + } else return false; + case SELECTOR: + label = gui.hand; + gui.hand = label.EMPTY; + notifyLsnr(); + return true; + default: + throw new IllegalPositionException(); + } } public WLabel setLsnrUpdate(Runnable lsnr) { @@ -177,36 +153,56 @@ public class WLabel implements IWidget { } class WAmount extends WContainer { + WLabel wl = new WLabel(xPos, yPos, xSize, ySize, enumMode.SELECTOR).setLsnrUpdate(this::update); + WTextField wtf = new WTextField(xPos + xSize + 10, yPos + ySize / 2 - WTextField.HEIGHT / 2, 50); + WButton bPercent = new WButtonText(xPos + xSize + 60, yPos, 20, 20, "general.to_percent", "%") + .setLsnrLeft(() -> setPercent(false)); + WButton bAmount = new WButtonText(xPos + xSize + 60, yPos, 20, 20, "general.to_percent", "#") + .setLsnrLeft(() -> setPercent(true)); + WButton bYes = new WButtonIcon(xPos + xSize + 83, yPos, 20, 20, Resource.BTN_YES).setLsnrLeft(() -> { + setLabel(wl.getLabel().setAmount( + wtf.getText().isEmpty() ? 0 : Integer.parseInt(wtf.getText()))); + JecaGui.getCurrent().root.remove(this); + }); + WButton bNo = new WButtonIcon(xPos + xSize + 102, yPos, 20, 20, Resource.BTN_NO).setLsnrLeft(() -> { + setLabel(ILabel.EMPTY); + JecaGui.getCurrent().root.remove(this); + }); + public WAmount() { - WLabel w = WLabel.this; - int xAlign = w.xPos + w.xSize; - WLabel wl = new WLabel(w.xPos, w.yPos, w.xSize, w.ySize, enumMode.SELECTOR); - wl.setLabel(w.getLabel()); - WTextField wtf = new WTextField(xAlign + 10, w.yPos + w.ySize / 2 - WTextField.HEIGHT / 2, 50); - WButton wby = new WButtonIcon(xAlign + 60, w.yPos, 20, 20, Resource.BTN_YES).setLsnrLeft(() -> { - w.setLabel(wl.getLabel().setAmount( - wtf.getText().isEmpty() ? 0 : Integer.parseInt(wtf.getText()))); - JecaGui.getCurrent().root.remove(this); - }); - WButton wbn = new WButtonIcon(xAlign + 79, w.yPos, 20, 20, Resource.BTN_NO).setLsnrLeft(() -> { - w.setLabel(ILabel.EMPTY); - JecaGui.getCurrent().root.remove(this); - }); - wtf.setText(Integer.toString(w.getLabel().getAmount())); + wl.setLabel(getLabel()); + wtf.setText(Integer.toString(getLabel().getAmount())); wtf.setLsnrText(i -> { try { Integer.parseInt(wtf.getText()); wtf.setColor(JecaGui.COLOR_TEXT_WHITE); - wby.setDisabled(false); + bYes.setDisabled(false); } catch (NumberFormatException e) { boolean acceptable = wtf.getText().isEmpty(); wtf.setColor(acceptable ? JecaGui.COLOR_TEXT_WHITE : JecaGui.COLOR_TEXT_RED); - wby.setDisabled(!acceptable); + bYes.setDisabled(!acceptable); } }); - add(new WPanel(w.xPos - 5, w.yPos - 5, w.xSize + 110, w.ySize + 10)); - add(new WText(xAlign + 3, w.yPos + 5, JecaGui.Font.DEFAULT_NO_SHADOW, "x")); - addAll(wl, wtf, wby, wbn); + add(new WPanel(xPos - 5, yPos - 5, xSize + 133, ySize + 10)); + add(new WText(xPos + xSize + 3, yPos + 5, JecaGui.Font.PLAIN, "x")); + addAll(wl, wtf, bYes, bNo); + update(); + } + + private void update() { + bYes.setDisabled(!wl.getLabel().acceptPercent()); + setPercent(false); + } + + private void setPercent(boolean b) { + if (b) { + remove(bAmount); + add(bPercent); + } else { + remove(bPercent); + add(bAmount); + } + wl.getLabel().setPercent(b); } @Override diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WPage.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WPage.java index 18b63a6..fa03575 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WPage.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WPage.java @@ -35,7 +35,7 @@ public class WPage implements IWidget { public void onDraw(JecaGui gui, int xMouse, int yMouse) { Resource resource = focused ? (index == 0 ? Resource.WGT_PAGER_F0 : Resource.WGT_PAGER_FN) : - Resource.WGT_PAGER_N; + Resource.WGT_PANEL_N; gui.drawResourceContinuous(resource, index * 24, -21, 24, 25, 4, 4, 4, 4); record.representation.drawLabel(gui, index * 24 + 4, -17, false); timer.setState(JecaGui.mouseIn(index * 24, -21, 24, 21, xMouse, yMouse)); diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WPanel.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WPanel.java index c9fa992..ddba8f5 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WPanel.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WPanel.java @@ -33,7 +33,7 @@ public class WPanel implements IWidget { @Override public void onDraw(JecaGui gui, int xMouse, int yMouse) { - gui.drawResourceContinuous(Resource.WGT_PANEL, xPos, yPos, xSize, ySize, 5, 5, 5, 5); + gui.drawResourceContinuous(Resource.WGT_PANEL_F, xPos, yPos, xSize, ySize, 5, 5, 5, 5); } @Override @@ -41,6 +41,11 @@ public class WPanel implements IWidget { return mouseIn(xMouse, yMouse); } + @Override + public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { + return mouseIn(xMouse, yMouse); + } + public boolean mouseIn(int x, int y) { int xx = x - xPos; int yy = y - yPos; diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WSearch.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WSearch.java index 563e5ee..af7209e 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WSearch.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WSearch.java @@ -21,7 +21,6 @@ public class WSearch extends WContainer { public WSearch(Consumer callback, WTextField tf, WLabelScroll... lss) { clbk = callback; - List lst = Arrays.asList(lss); tf.setLsnrText(s -> { boolean b = false; for (WLabelScroll i : lss) if (i.setFilter(s)) b = true; @@ -31,7 +30,7 @@ public class WSearch extends WContainer { addAll(lss); for (WLabelScroll i : lss) i.setLsnrUpdate(j -> { - if (clbk != null) clbk.accept(i.getLabelAt(j).copy()); + if (clbk != null) clbk.accept(i.getLabelAt(j)); }); } } diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WSwitcher.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WSwitcher.java index 4999e02..b7123ab 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WSwitcher.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WSwitcher.java @@ -50,8 +50,7 @@ public class WSwitcher extends WContainer { else setTemp(null); }); wRect = new WRectangle(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, JecaGui.COLOR_GUI_GREY); - wText = new WText(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, JecaGui.Font.DEFAULT_SHADOW, - () -> temp == null ? (keys.isEmpty() ? "" : keys.get(index.current())) : temp); + wText = new WText(xPos + SIZE, yPos + 2, xSize - 2 * SIZE, JecaGui.Font.SHADOW, "", true); index = new Circulator(keys.size()); refresh(); addAll(left, right, wRect, wText); @@ -67,6 +66,7 @@ public class WSwitcher extends WContainer { } else temp = null; notifyLsnr(); + refresh(); } public WSwitcher setListener(Runnable listener) { @@ -103,6 +103,7 @@ public class WSwitcher extends WContainer { boolean b = keys.size() < (temp == null ? 2 : 1); left.setDisabled(b); right.setDisabled(b); + wText.key = temp == null ? (keys.isEmpty() ? "" : keys.get(index.current())) : temp; } public void notifyLsnr() { diff --git a/src/main/java/me/towdium/jecalculation/gui/drawables/WText.java b/src/main/java/me/towdium/jecalculation/gui/drawables/WText.java index acedad4..d38f4be 100644 --- a/src/main/java/me/towdium/jecalculation/gui/drawables/WText.java +++ b/src/main/java/me/towdium/jecalculation/gui/drawables/WText.java @@ -17,45 +17,28 @@ import java.util.function.Supplier; public class WText implements IWidget { public static final int UNDEFINED = Integer.MAX_VALUE; - public int xPos, yPos, xSize, ySize; + public int xPos, yPos, xSize; + public boolean centred; public JecaGui.Font font; - public Supplier key; + public String key; public WText(int xPos, int yPos, JecaGui.Font font, String key) { - this(xPos, yPos, UNDEFINED, UNDEFINED, font, key); + this(xPos, yPos, UNDEFINED, font, key, false); } - public WText(int xPos, int yPos, JecaGui.Font font, Supplier key) { - this(xPos, yPos, UNDEFINED, UNDEFINED, font, key); - } - - public WText(int xPos, int yPos, int xSize, JecaGui.Font font, String key) { - this(xPos, yPos, xSize, UNDEFINED, font, key); - } - - public WText(int xPos, int yPos, int xSize, JecaGui.Font font, Supplier key) { - this(xPos, yPos, xSize, UNDEFINED, font, key); - } - - - public WText(int xPos, int yPos, int xSize, int ySize, JecaGui.Font font, String key) { - this(xPos, yPos, xSize, UNDEFINED, font, () -> key); - } - - public WText(int xPos, int yPos, int xSize, int ySize, JecaGui.Font font, Supplier key) { + public WText(int xPos, int yPos, int xSize, JecaGui.Font font, String key, boolean centred) { this.xPos = xPos; this.yPos = yPos; this.xSize = xSize; - this.ySize = ySize; this.font = font; this.key = key; + this.centred = centred; } @Override public void onDraw(JecaGui gui, int xMouse, int yMouse) { - String[] text = key.get().split("\n"); - if (xSize == UNDEFINED) gui.drawText(xPos, yPos, font, text); - else if (ySize == UNDEFINED) gui.drawText(xPos, yPos, xSize, font, text); - else gui.drawText(xPos, yPos, xSize, ySize, font, text); + int x = xPos + (centred ? xSize / 2 - font.getTextWidth(key) / 2 : 0); + if (xSize == UNDEFINED) gui.drawText(x, yPos, font, key); + else gui.drawText(x, yPos, xSize, font, key); } } diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java index ff0bb1b..2dc4e7a 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java @@ -43,6 +43,7 @@ public class GuiCalculator extends WContainer { }); lgRecent.setLsnrUpdate(l -> JecaGui.getCurrent().hand = lgRecent.getLabelAt(l)); tfAmount.setLsnrText(s -> refreshCalculator()); + add(new WHelp("calculator")); add(new WPanel()); add(new WButtonIcon(7, 7, 20, 20, Resource.BTN_LABEL, "calculator.label") .setLsnrLeft(() -> JecaGui.displayGui(new GuiLabel(l -> { @@ -53,7 +54,7 @@ public class GuiCalculator extends WContainer { .setLsnrLeft(() -> JecaGui.displayGui(true, true, new GuiRecipe()))); add(new WButtonIcon(149, 7, 20, 20, Resource.BTN_SEARCH, "calculator.search") .setLsnrLeft(() -> JecaGui.displayGui(new GuiSearch()))); - add(new WText(53, 13, JecaGui.Font.DEFAULT_NO_SHADOW, "x")); + add(new WText(53, 13, JecaGui.Font.PLAIN, "x")); add(new WLine(55)); add(new WIcon(151, 31, 18, 18, Resource.ICN_RECENT, "calculator.history")); addAll(lgRecent, lLabel, btnInput, btnOutput, btnCatalyst, btnSteps, lsResult, tfAmount); diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/pickers/PickerItemStack.java b/src/main/java/me/towdium/jecalculation/gui/guis/pickers/PickerItemStack.java index 2aa4e19..5c95985 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/pickers/PickerItemStack.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/pickers/PickerItemStack.java @@ -66,7 +66,7 @@ public class PickerItemStack extends IWPicker.Impl { remove(bd); add(be); } - ILabel l = this.label.getLabel(); + ILabel l = label.getLabel(); if (l instanceof LItemStack) c.accept((LItemStack) l); } } diff --git a/src/main/java/me/towdium/jecalculation/utils/Utilities.java b/src/main/java/me/towdium/jecalculation/utils/Utilities.java index ac1dd3d..fae1106 100644 --- a/src/main/java/me/towdium/jecalculation/utils/Utilities.java +++ b/src/main/java/me/towdium/jecalculation/utils/Utilities.java @@ -8,6 +8,8 @@ import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.polyfill.NBTHelper; import me.towdium.jecalculation.utils.wrappers.Pair; import me.towdium.jecalculation.utils.wrappers.Wrapper; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -16,9 +18,11 @@ import org.apache.commons.lang3.StringEscapeUtils; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import java.text.BreakIterator; import java.util.*; import java.util.function.BiFunction; import java.util.function.BiPredicate; +import java.util.function.Function; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -29,14 +33,6 @@ import java.util.stream.StreamSupport; @SuppressWarnings({"unused", "UnusedReturnValue"}) @ParametersAreNonnullByDefault public class Utilities { - final static int[] scaleTable = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE}; - - public static boolean stackEqual(ItemStack a, ItemStack b) { - return a.getItem() == b.getItem() && a.getItemDamage() == b.getItemDamage() && - ((a.getTagCompound() == null && b.getTagCompound() == null) || - (a.getTagCompound() != null && a.getTagCompound().equals(b.getTagCompound()))); - } - // FLOAT FORMATTING public static String cutNumber(float f, int size) { BiFunction form = (fl, len) -> { @@ -111,10 +107,6 @@ public class Utilities { // return Optional.empty(); // } - public static void setStack(ItemStack is) { - - } - public static class Timer { long time = System.currentTimeMillis(); boolean running = false; @@ -244,6 +236,68 @@ public class Utilities { public static String format(String translateKey, Object... parameters) { return search(translateKey, parameters).one; } + + public static List wrap(String s, int width) { + return new TextWrapper().wrap(s, Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage().getLanguageCode(), + i -> TextWrapper.renderer.getCharWidth(i), width); + } + + static class TextWrapper { + static FontRenderer renderer = Minecraft.getMinecraft().fontRenderer; + + String str; + BreakIterator it; + List temp = new ArrayList<>(); + Function func; + int start, end, section, space, cursor, width; + + private void cut() { + char c = str.charAt(cursor); + if (c == '\f') cursor++; + temp.add(str.substring(start, cursor)); + if (c == ' ' || c == ' ' || c == '\n') cursor++; + start = cursor; + end = cursor; + space = width; + section = func.apply(str.charAt(cursor)); + } + + private void move() { + temp.add(str.substring(start, end)); + start = end; + space = width; + } + + private List wrap(String s, String languageCode, Function func, int width) { + Locale l = getLocaleFromString(languageCode); + temp.clear(); + start = 0; + end = 0; + cursor = 0; + space = width; + str = s; + it = BreakIterator.getLineInstance(l); + it.setText(s); + this.width = width; + this.func = func; + for (int i = it.next(); i != BreakIterator.DONE; i = it.next()) { + for (cursor = end; cursor < i; cursor++) { + char ch = str.charAt(cursor); + section += func.apply(str.charAt(cursor)); + if (ch == '\n' || ch == '\f') cut(); + else if (section > space) { + if (start == end) cut(); + else move(); + } + } + space -= section; + section = 0; + end = cursor; + } + move(); + return temp; + } + } } public static class Recent { @@ -276,4 +330,13 @@ public class Utilities { data.clear(); } } + + public static Locale getLocaleFromString(String languageCode) { + String parts[] = languageCode.split("_", -1); + if (parts.length == 1) return new Locale(parts[0]); + else if (parts.length == 2 + || (parts.length == 3 && parts[2].startsWith("#"))) + return new Locale(parts[0], parts[1]); + else return new Locale(parts[0], parts[1], parts[2]); + } } diff --git a/src/main/resources/assets/jecalculation/lang/en_US.lang b/src/main/resources/assets/jecalculation/lang/en_US.lang index c75bfcc..3eaf941 100644 --- a/src/main/resources/assets/jecalculation/lang/en_US.lang +++ b/src/main/resources/assets/jecalculation/lang/en_US.lang @@ -4,6 +4,8 @@ jecharacters.command.help.list=Here is the list of all commands available: jecharacters.command.state.desc=The mod is working on state: %s jecharacters.chat.server_mode=The mod is not working in client mode. Key binding will not work. jecharacters.gui.calculator.recipe.active=New recipe +jecharacters.gui.calculator.help=Welcome to the main interface of the mod. The first thing suggested to do is to use the two button at upper right to input the recipes, which allows you to create and edit recipes. All the calculation requires recipes to be recorded.\fTo calculate the cost of item, simply pick an item from JEI item list then put to the slot at upper left of the interface with all the recipes used recorded. The result will then be displayed in lower slots in the interface. The four button provide options to display input, output, catalyst and crafting steps.\fThe button in upper left allows you to to edit and get a lable. Labels are very important part of the mod. They are basiclly things can be put into a slot, including ItemStack, FluidStack, OreDict entry, or simply a placeholder. Lables are used in the calculation process. Most of time they are quite clever as long as you config them properly. +jecharacters.gui.calculator.title=Crafting Calculator jecharacters.gui.recipe.save.active=Save jecharacters.gui.recipe.copy.active=Save as new recipe jecharacters.gui.recipe.copy.disabled=Cannot save as new recipe\nbecause you are not editing a recipe diff --git a/src/main/resources/assets/jecalculation/textures/gui/resources.png b/src/main/resources/assets/jecalculation/textures/gui/resources.png index b1039fe..f577734 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