feat: render entry amount

This commit is contained in:
Discreater committed 2021-04-28 03:31:06 +08:00
1 parent 408e936815
commit 3881b3eaf8
11 files changed
+310 -105

No files matched your search

@@ -94,13 +94,22 @@ public class JustEnoughCalculation {
public static final String VERSION = "@VERSION@";
}
/**
* CLIENT: running at client side and server not installed
* SERVER: running at server side whether client is installed
* BOTH: running at client side and both installed
* UNDEFINED: default unknown
*/
public enum enumSide {
CLIENT, SERVER, BOTH, UNDEFINED
/**
* Running at client side and server not installed.
*/
CLIENT,
/**
* Running at server side whether client is installed.
*/
SERVER,
/**
* Running at client side and both installed.
*/
BOTH,
/**
* Default unknown.
*/
UNDEFINED
}
}
@@ -1,12 +1,15 @@
package me.towdium.jecalculation.client.gui;
import cpw.mods.fml.client.config.GuiUtils;
import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.client.resource.Resource;
import me.towdium.jecalculation.client.widget.Widget;
import me.towdium.jecalculation.client.widget.widgets.WEntry;
import me.towdium.jecalculation.client.widget.widgets.WEntryGroup;
import me.towdium.jecalculation.core.entry.Entry;
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.helpers.LocalizationHelper;
import me.towdium.jecalculation.utils.wrappers.Single;
import me.towdium.jecalculation.utils.wrappers.Triple;
@@ -21,6 +24,7 @@ import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -91,7 +95,15 @@ public class JecGui extends GuiContainer {
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) {
super.mouseClicked(mouseX, mouseY, mouseButton);
wgtMgr.onClick(mouseX, mouseY, mouseButton);
boolean InGui = wgtMgr.onClick(mouseX, mouseY, mouseButton);
if(!InGui) {
this.handleMouseEvent();
}
}
@Override
public void handleMouseInput() {
super.handleMouseInput();
}
@Override
@@ -108,39 +120,40 @@ public class JecGui extends GuiContainer {
}
}
// /**
// * @return if the event is canceled
// * This function handles click outside the normal region,
// * especially the overlap with JEI overlay. It handles
// * mouse event before JEI.
// */
// public boolean handleMouseEvent() {
// int xMouse = Mouse.getEventX() * this.width / this.mc.displayWidth;
// int yMouse = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1;
// if (Mouse.getEventButtonState()) {
// if (Mouse.getEventButton() == 0) {
// if (hand == Entry.EMPTY) {
// // TODO
// Entry e = JecPlugin.getEntryUnderMouse();
// if (e != Entry.EMPTY) {
// hand = e;
// return true;
// }
// } else {
// if (!mouseIn(guiLeft, guiTop, width, height, xMouse, yMouse)) {
// hand = Entry.EMPTY;
// return true;
// }
// }
// } else if (Mouse.getEventButton() == 1) {
// if (hand != Entry.EMPTY) {
// hand = Entry.EMPTY;
// return true;
// }
// }
// }
// return false;
// }
/**
* @return if the event is canceled
* This function handles click outside the normal region,
* especially the overlap with JEI overlay. It handles
* mouse event before JEI.
*/
public boolean handleMouseEvent() {
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;
if (Mouse.getEventButtonState()) {
if (Mouse.getEventButton() == 0) {
if (hand == Entry.EMPTY) {
// TODO
Entry e = NEIPlugin.getEntryUnderMouse();
if (e != Entry.EMPTY) {
hand = e;
return true;
}
} else {
if (!mouseIn(guiLeft, guiTop, width, height, xMouse, yMouse)) {
hand = Entry.EMPTY;
return true;
}
}
} else if (Mouse.getEventButton() == 1) {
if (hand != Entry.EMPTY) {
hand = Entry.EMPTY;
return true;
}
}
}
return false;
}
public FontRenderer getFontRenderer() {
return fontRendererObj;
@@ -198,11 +211,27 @@ public class JecGui extends GuiContainer {
// TODO Different from origin
}
public void drawText(int xPos, int yPos, Font f, String... text) {
drawText(xPos, yPos, f, (s) -> 0, text);
public void drawText(float xPos, float yPos, Font f, String... text) {
Function<String, Integer> 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 drawText(int xPos, int yPos, Font f, Function<String, Integer> indenter, String... text) {
private void drawText(float xPos, float yPos, Font f, Function<String, Integer> indenter, String... text) {
Single<Integer> y = new Single<>(0);
boolean unicode = fontRendererObj.getUnicodeFlag();
if (!f.unicode)
@@ -218,28 +247,32 @@ public class JecGui extends GuiContainer {
fontRendererObj.setUnicodeFlag(unicode);
}
public void drawText(int xPos, int yPos, int xSize, Font f, String... text) {
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) + "...";
if (w <= sizeScaled) return s;
else if (l >= w) return "...";
else return fontRendererObj.trimStringToWidth(s, (int) (sizeScaled - l)) + "...";
}).toArray(String[]::new);
if (f.centred) {
drawText(xPos, yPos, f, (s) -> (int) ((sizeScaled - fontRendererObj.getStringWidth(s)) / 2), ss);
} else {
int maxLen = Arrays.stream(ss).mapToInt(s -> fontRendererObj.getStringWidth(s)).max().orElse(0);
int xOffset = (int) ((sizeScaled - maxLen) / 2);
drawText(xPos, yPos, f, (s) -> xOffset, ss);
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(int xPos, int yPos, int xSize, int ySize, Font f, String... text) {
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);
}
@@ -284,13 +317,14 @@ public class JecGui extends GuiContainer {
}
public static class Font {
public static final Font DEFAULT_SHADOW = new Font(0xFFFFFF, true, true, false, true, 1);
public static final Font DEFAULT_NO_SHADOW = new Font(0x404040, false, true, false, true, 1);
public static final Font DEFAULT_HALF = new Font(0xFFFFFF, true, true, true, true, 0.5f);
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 int color;
public boolean shadow, cut, unicode, centred;
public boolean shadow, cut, unicode;
public float size;
public enumAlign align;
/**
* @param color foreground color
@@ -299,18 +333,24 @@ public class JecGui extends GuiContainer {
* @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, boolean centred, float size) {
public Font(int color, boolean shadow, boolean cut, boolean unicode, float size, enumAlign align) {
this.color = color;
this.shadow = shadow;
this.cut = cut;
this.size = size;
this.align = align;
this.unicode = unicode;
this.centred = centred;
}
public Font copy() {
return new Font(color, shadow, cut, unicode, centred, size);
return new Font(color, shadow, cut, unicode, size, align);
}
public enum enumAlign {LEFT, CENTRE, RIGHT, AUTO}
}
public static boolean isShiftDown() {
return Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
}
public static class ContainerTransfer extends Container {
@@ -9,6 +9,8 @@ import net.minecraft.client.gui.GuiScreen;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import static me.towdium.jecalculation.client.widget.widgets.WEntry.enumMode.*;
/**
* Author: towdium
* Date: 8/14/17.
@@ -21,9 +23,9 @@ public class GuiCalculator extends JecGui {
wgtMgr.add(new WButtonIcon(130, 7, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "recipe")
.setListenerLeft(() -> Minecraft.getMinecraft().displayGuiScreen(new GuiEditor(this))));
wgtMgr.add(new WButtonIcon(149, 7, 20, 20, Resource.BTN_SEARCH_N, Resource.BTN_SEARCH_F, "search"));
wgtMgr.add(new WEntryGroup(7, 87, 9, 4));
wgtMgr.add(new WEntryGroup(7, 31, 8, 1));
wgtMgr.add(new WEntry(31, 7, 20, 20));
wgtMgr.add(new WEntryGroup(7, 87, 9, 4, RESULT));
wgtMgr.add(new WEntryGroup(7, 31, 8, 1, PICKER));
wgtMgr.add(new WEntry(31, 7, 20, 20, SELECTOR));
wgtMgr.add(new WLine(52));
wgtMgr.add(new WTextField(61, 7, 64));
wgtMgr.add(new WIcon(151, 31, 18, 18, Resource.ICN_RECENT_N, Resource.ICN_RECENT_F, "history"));
@@ -7,6 +7,8 @@ import net.minecraft.client.gui.GuiScreen;
import javax.annotation.Nullable;
import static me.towdium.jecalculation.client.widget.widgets.WEntry.enumMode.EDITOR;
/**
* Author: towdium
* Date: 17-9-8.
@@ -18,9 +20,9 @@ public class GuiEditor extends JecGui {
WButtonIcon buttonLabel = new WButtonIcon(45, 33, 20, 20, Resource.BTN_LABEL_N, Resource.BTN_LABEL_F, "label");
WButtonIcon buttonYes = new WButtonIcon(7, 33, 20, 20, Resource.BTN_YES_N, Resource.BTN_YES_F, "confirm");
WButtonIcon buttonNo = new WButtonIcon(26, 33, 20, 20, Resource.BTN_NO_N, Resource.BTN_NO_F, "abort");
WEntryGroup groupInput = new WEntryGroup(28, 111, 7, 2, 20, 20);
WEntryGroup groupCatalyst = new WEntryGroup(28, 87, 7, 1, 20, 20);
WEntryGroup groupOutput = new WEntryGroup(28, 63, 7, 1, 20, 20);
WEntryGroup groupInput = new WEntryGroup(28, 111, 7, 2, 20, 20, EDITOR);
WEntryGroup groupCatalyst = new WEntryGroup(28, 87, 7, 1, 20, 20, EDITOR);
WEntryGroup groupOutput = new WEntryGroup(28, 63, 7, 1, 20, 20, EDITOR);
WTextField textField = new WTextField(49, 33, 119);
WButtonIcon buttonNew = new WButtonIcon(7, 33, 20, 20, Resource.BTN_NEW_N, Resource.BTN_NEW_F, "new")
.setListenerLeft(() -> setModeNewGroup(true));
@@ -4,6 +4,7 @@ import me.towdium.jecalculation.client.gui.JecGui;
import me.towdium.jecalculation.client.resource.Resource;
import me.towdium.jecalculation.client.widget.Widget;
import me.towdium.jecalculation.core.entry.Entry;
import me.towdium.jecalculation.utils.IllegalPositionException;
import javax.annotation.ParametersAreNonnullByDefault;
@@ -13,15 +14,25 @@ import javax.annotation.ParametersAreNonnullByDefault;
*/
@ParametersAreNonnullByDefault
public class WEntry extends Widget.Advanced {
static JecGui.Font font;
static {
font = JecGui.Font.DEFAULT_HALF.copy();
font.align = JecGui.Font.enumAlign.RIGHT;
}
public int xPos, yPos, xSize, ySize;
public Entry entry;
public enumMode mode;
public WEntry(int xPos, int yPos, int xSize, int ySize) {
public WEntry(int xPos, int yPos, int xSize, int ySize, enumMode mode) {
this.xPos = xPos;
this.yPos = yPos;
this.xSize = xSize;
this.ySize = ySize;
this.entry = Entry.EMPTY;
this.mode = mode;
}
public Entry getEntry() {
@@ -37,22 +48,49 @@ public class WEntry extends Widget.Advanced {
gui.drawResourceContinuous(Resource.WGT_SLOT, xPos + gui.getGuiLeft(), yPos + gui.getGuiTop(), xSize, ySize,
3, 3, 3, 3);
gui.drawItemStack(gl(gui) + xPos + xSize / 2, gt(gui) + yPos + ySize / 2, entry.getRepresentation(), true);
if (mode == enumMode.RESULT || mode == enumMode.EDITOR)
gui.drawText(gl(gui) + xPos + xSize / 2 + 7.5f, gt(gui) + yPos + ySize / 2 + 7 -
(int) (font.size * gui.getFontRenderer().FONT_HEIGHT), font, entry.getAmountString());
if (mouseIn(gui, xMouse, yMouse))
gui.drawRectangle(gui.getGuiLeft() + xPos + 1, gui.getGuiTop() + yPos + 1, xSize - 2, ySize - 2,
0x80FFFFFF);
// TODO draw amount
}
@Override
public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) {
if (mouseIn(gui, xMouse, yMouse)) { // TODO different modes
if (gui.hand != Entry.EMPTY && button == 0) {
entry = gui.hand;
gui.hand = Entry.EMPTY;
return true;
if (mouseIn(gui, xMouse, yMouse)) {
switch (mode) {
case EDITOR:
if (gui.hand != Entry.EMPTY) {
entry = gui.hand;
gui.hand = Entry.EMPTY;
return true;
} else if (entry != Entry.EMPTY) {
if (button == 0) {
if (JecGui.isShiftDown()) entry = entry.increaseAmountLarge();
else entry = entry.increaseAmount();
return true;
} else if (button == 1) {
if (JecGui.isShiftDown()) entry = entry.decreaseAmountLarge();
else entry = entry.decreaseAmount();
return true;
}
} else return false;
case RESULT:
return false;
case PICKER:
if (entry != Entry.EMPTY) {
gui.hand = entry.copy();
return true;
} else return false;
case SELECTOR:
entry = gui.hand;
gui.hand = Entry.EMPTY;
return true;
default:
throw new IllegalPositionException();
}
}
return false;
} else return false;
}
public boolean mouseIn(JecGui gui, int x, int y) {
@@ -60,4 +98,23 @@ public class WEntry extends Widget.Advanced {
int yy = y - gui.getGuiTop() - yPos;
return xx >= 0 && xx < xSize && yy >= 0 && yy < ySize;
}
public enum enumMode {
/**
* Slots in editor gui. Can use to edit amount. Exact amount displayed.
*/
EDITOR,
/**
* Slots to display calculate/search result. Rounded amount displayed.
*/
RESULT,
/**
* Slots that can pick items from. No amount displayed.
*/
PICKER,
/**
* Slots to select items, eg. in calculate and search gui. No amount displayed.
*/
SELECTOR
}
}
@@ -16,13 +16,13 @@ import java.util.stream.IntStream;
public class WEntryGroup extends Widget.Advanced {
ArrayList<WEntry> widgets = new ArrayList<>();
public WEntryGroup(int xPos, int yPos, int column, int row) {
this(xPos, yPos, column, row, 18, 18);
public WEntryGroup(int xPos, int yPos, int column, int row, WEntry.enumMode mode) {
this(xPos, yPos, column, row, 18, 18, mode);
}
public WEntryGroup(int xPos, int yPos, int column, int row, int xSize, int ySize) {
public WEntryGroup(int xPos, int yPos, int column, int row, int xSize, int ySize, WEntry.enumMode mode) {
IntStream.range(0, column).forEach(c -> IntStream.range(0, row).forEach(r ->
widgets.add(new WEntry(xPos + c * xSize, yPos + r * ySize, xSize, ySize))));
widgets.add(new WEntry(xPos + c * xSize, yPos + r * ySize, xSize, ySize, mode))));
}
@Override
@@ -24,14 +24,20 @@ public interface Entry {
EntryRegistry REGISTRY = EntryRegistry.INSTANCE;
Entry EMPTY = new EntryItemStack(Entry.EMPTY_ITEM_STACK, 0);
int getAmount();
Entry increaseAmount();
void setAmount(int amount);
Entry increaseAmountLarge();
Entry decreaseAmount();
Entry decreaseAmountLarge();
ItemStack getRepresentation();
String getAmountString();
Entry copy();
/**
* Since {@link Entry} merging is bidirectional, it is redundant to
* implement on both side. So this class is created for merging
@@ -1,6 +1,7 @@
package me.towdium.jecalculation.core.entry.entries;
import me.towdium.jecalculation.core.entry.Entry;
import me.towdium.jecalculation.utils.Utilities;
import net.minecraft.item.ItemStack;
/**
@@ -22,14 +23,39 @@ public class EntryItemStack implements Entry {
this.amount = amount;
}
@Override
public int getAmount() {
return amount;
private EntryItemStack(EntryItemStack eis) {
amount = eis.amount;
itemStack = eis.itemStack;
}
@Override
public void setAmount(int amount) {
this.amount = amount;
public Entry increaseAmount() {
amount++;
return this;
}
@Override
public Entry increaseAmountLarge() {
amount += 10;
return this;
}
@Override
public Entry decreaseAmount() {
if (amount <= 1) return Entry.EMPTY;
else {
amount--;
return this;
}
}
@Override
public Entry decreaseAmountLarge() {
if (amount <= 10) return Entry.EMPTY;
else {
amount -= 10;
return this;
}
}
@Override
@@ -39,6 +65,11 @@ public class EntryItemStack implements Entry {
@Override
public String getAmountString() {
return Integer.toString(amount); // TODO format
return amount == 0 ? "" : Utilities.cutLong(amount, 5);
}
@Override
public Entry copy() {
return new EntryItemStack(this);
}
}
@@ -26,12 +26,12 @@ public class InputEventHandler {
}
}
// @SubscribeEvent(priority = EventPriority.HIGH)
// public void onMouseClick(GuiScreenEvent.MouseInputEvent.Pre event) {
// if (event.getGui() instanceof JecGui) {
// GuiScreen gui = event.getGui();
// event.setCanceled(((JecGui) gui).handleMouseEvent());
// }
// }
@SubscribeEvent(priority = EventPriority.HIGH)
public void onMouseClick(GuiScreenEvent.ActionPerformedEvent.Pre event) {
if (event.gui instanceof JecGui) {
JecGui gui = (JecGui) event.gui;
event.setCanceled(gui.handleMouseEvent());
}
}
}
@@ -15,9 +15,6 @@ public class JecaTooltipHandler implements IContainerTooltipHandler {
@Override
public List<String> handleItemDisplayName(GuiContainer gui, ItemStack itemstack, List<String> currenttip) {
// if (gui instanceof JecGui) {
// ((JecGui)gui).handleMouseOverNEIItemPanel(itemstack);
// }
return currenttip;
}
@@ -27,9 +24,9 @@ public class JecaTooltipHandler implements IContainerTooltipHandler {
int mousex,
int mousey,
List<String> currenttip) {
// if (gui instanceof JecaGuiContainer) {
// ((JecaGuiContainer)gui).handleMouseOverNEIItemPanel(itemstack);
// }
if (gui instanceof JecGui) {
NEIPlugin.setEntryUnderMouse(itemstack);
}
return currenttip;
}
}
@@ -2,9 +2,70 @@ package me.towdium.jecalculation.nei;
import codechicken.nei.api.API;
import codechicken.nei.guihook.GuiContainerManager;
import me.towdium.jecalculation.core.entry.Entry;
import me.towdium.jecalculation.core.entry.entries.EntryItemStack;
import me.towdium.jecalculation.utils.Utilities;
import net.minecraft.item.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class NEIPlugin {
public static final TransferRegistryItem registryItem = TransferRegistryItem.INSTANCE;
public static void init() {
GuiContainerManager.addTooltipHandler(new JecaTooltipHandler());
}
private static ItemStack currentItemStack;
public static Entry getEntryUnderMouse() {
if (NEIPlugin.currentItemStack == null) return Entry.EMPTY;
else return registryItem.toEntry(NEIPlugin.currentItemStack);
}
public static void setEntryUnderMouse(ItemStack itemStack) {
NEIPlugin.currentItemStack = itemStack;
}
public static List<Entry> transferRawItemStack(List<ItemStack> iss) {
return iss.stream().map(EntryItemStack::new).collect(Collectors.toList());
}
public static class TransferRegistryItem extends TransferHandlerRegistry<ItemStack> {
public static final TransferRegistryItem INSTANCE;
static {
INSTANCE = new TransferRegistryItem();
INSTANCE.register(NEIPlugin::transferRawItemStack);
}
private TransferRegistryItem() {
}
}
public static class TransferHandlerRegistry<T> {
List<Function<List<T>, List<Entry>>> handlers = new ArrayList<>();
private TransferHandlerRegistry() {
}
Entry toEntry(T ingredient) {
return handlers.isEmpty() ? Entry.EMPTY :
handlers.get(0).apply(Collections.singletonList(ingredient)).get(0);
}
List<Entry> toEntry(List<T> ingredients) {
return new Utilities.ReversedIterator<>(handlers).stream().flatMap(h -> h.apply(ingredients).stream())
.collect(Collectors.toList());
}
void register(Function<List<T>, List<Entry>> handler) {
handlers.add(handler);
}
}
}