From 5281694d6a8df8c44946495125686b72ddafd9a3 Mon Sep 17 00:00:00 2001 From: GiantLuigi4 <49770992+GiantLuigi4@users.noreply.github.com> Date: Fri, 22 Sep 2023 16:25:45 -0400 Subject: [PATCH] fix a bunch of stuff that I intentionally broke --- .../montoyo/wd/client/gui/GuiKeyboard.java | 6 +- .../net/montoyo/wd/client/gui/GuiServer.java | 1506 ++++++++--------- .../net/montoyo/wd/client/gui/WDScreen.java | 18 +- .../wd/client/gui/controls/Button.java | 17 +- .../wd/client/gui/controls/CheckBox.java | 31 +- .../wd/client/gui/controls/Control.java | 18 +- .../wd/client/gui/controls/ControlGroup.java | 5 +- .../montoyo/wd/client/gui/controls/Label.java | 8 +- .../montoyo/wd/client/gui/controls/List.java | 10 +- .../wd/client/gui/controls/TextField.java | 32 +- .../wd/client/gui/controls/UpgradeGroup.java | 2 +- .../wd/entity/TileEntityInterfaceBase.java | 2 +- .../montoyo/wd/entity/TileEntityScreen.java | 38 +- .../net/montoyo/wd/utilities/TypeData.java | 9 +- 14 files changed, 864 insertions(+), 838 deletions(-) diff --git a/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java b/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java index 0536769..9bfacd7 100644 --- a/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java +++ b/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java @@ -153,19 +153,19 @@ public class GuiKeyboard extends WDScreen { public boolean keyPressed(int keyCode, int scanCode, int modifiers) { if(quitOnEscape && keyCode == GLFW.GLFW_KEY_ESCAPE) Minecraft.getInstance().setScreen(null); - addKey(new TypeData(TypeData.Action.PRESS, keyCode, modifiers)); + addKey(new TypeData(TypeData.Action.PRESS, keyCode, modifiers, scanCode)); return super.keyPressed(keyCode, scanCode, modifiers); } @Override public boolean charTyped(char codePoint, int modifiers) { - addKey(new TypeData(TypeData.Action.TYPE, codePoint, modifiers)); + addKey(new TypeData(TypeData.Action.TYPE, codePoint, modifiers, 0)); return super.charTyped(codePoint, modifiers); } @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - addKey(new TypeData(TypeData.Action.RELEASE, keyCode, modifiers)); + addKey(new TypeData(TypeData.Action.RELEASE, keyCode, modifiers, scanCode)); return super.keyPressed(keyCode, scanCode, modifiers); } diff --git a/src/main/java/net/montoyo/wd/client/gui/GuiServer.java b/src/main/java/net/montoyo/wd/client/gui/GuiServer.java index a596d5d..6f6c23d 100644 --- a/src/main/java/net/montoyo/wd/client/gui/GuiServer.java +++ b/src/main/java/net/montoyo/wd/client/gui/GuiServer.java @@ -43,766 +43,766 @@ import java.util.function.Supplier; import static net.montoyo.wd.client.gui.GuiMinePad.getChar; public class GuiServer extends WDScreen { - - private static final ResourceLocation BG_IMAGE = new ResourceLocation("webdisplays", "textures/gui/server_bg.png"); - private static final ResourceLocation FG_IMAGE = new ResourceLocation("webdisplays", "textures/gui/server_fg.png"); - private static final HashMap COMMAND_MAP = new HashMap<>(); - private static final int MAX_LINE_LEN = 32; - private static final int MAX_LINES = 12; - - private final Vector3i serverPos; - private final NameUUIDPair owner; - private final ArrayList lines = new ArrayList<>(); - private String prompt = ""; - private String userPrompt; - private int blinkTime; - private String lastCmd; - private boolean promptLocked; - private volatile long queryTime; - private ClientTask currentTask; - private int selectedLine = -1; - - //Access command - private int accessTrials; - private int accessTime; - private int accessState = -1; - private SimpleSoundInstance accessSound; - - //Upload wizard - private boolean uploadWizard; - private File uploadDir; - private final ArrayList uploadFiles = new ArrayList<>(); - private int uploadOffset; - private boolean uploadFirstIsParent; - private String uploadFilter = ""; - private long uploadFilterTime; - - public GuiServer(Vector3i vec, NameUUIDPair owner) { - super(Component.nullToEmpty(null)); - serverPos = vec; - this.owner = owner; - userPrompt = "> "; - - if(COMMAND_MAP.isEmpty()) - buildCommandMap(); - - lines.add("MiniServ 1.0"); - lines.add(tr("info")); - uploadCD(FileSystemView.getFileSystemView().getDefaultDirectory()); - } - - private static String tr(String key, Object ... args) { - return I18n.get("webdisplays.server." + key, args); - } - - @Override - public void render(GuiGraphics poseStack, int mouseX, int mouseY, float ptt) { - super.render(poseStack, mouseX, mouseY, ptt); - - int x = (width - 256) / 2; - int y = (height - 176) / 2; + + private static final ResourceLocation BG_IMAGE = new ResourceLocation("webdisplays", "textures/gui/server_bg.png"); + private static final ResourceLocation FG_IMAGE = new ResourceLocation("webdisplays", "textures/gui/server_fg.png"); + private static final HashMap COMMAND_MAP = new HashMap<>(); + private static final int MAX_LINE_LEN = 32; + private static final int MAX_LINES = 12; + + private final Vector3i serverPos; + private final NameUUIDPair owner; + private final ArrayList lines = new ArrayList<>(); + private String prompt = ""; + private String userPrompt; + private int blinkTime; + private String lastCmd; + private boolean promptLocked; + private volatile long queryTime; + private ClientTask currentTask; + private int selectedLine = -1; + + //Access command + private int accessTrials; + private int accessTime; + private int accessState = -1; + private SimpleSoundInstance accessSound; + + //Upload wizard + private boolean uploadWizard; + private File uploadDir; + private final ArrayList uploadFiles = new ArrayList<>(); + private int uploadOffset; + private boolean uploadFirstIsParent; + private String uploadFilter = ""; + private long uploadFilterTime; + + public GuiServer(Vector3i vec, NameUUIDPair owner) { + super(Component.nullToEmpty(null)); + serverPos = vec; + this.owner = owner; + userPrompt = "> "; + + if (COMMAND_MAP.isEmpty()) + buildCommandMap(); + + lines.add("MiniServ 1.0"); + lines.add(tr("info")); + uploadCD(FileSystemView.getFileSystemView().getDefaultDirectory()); + } + + private static String tr(String key, Object... args) { + return I18n.get("webdisplays.server." + key, args); + } + + @Override + public void render(GuiGraphics poseStack, int mouseX, int mouseY, float ptt) { + super.render(poseStack, mouseX, mouseY, ptt); + + int x = (width - 256) / 2; + int y = (height - 176) / 2; // RenderSystem.enableTexture(); - RenderSystem.setShaderTexture(0, BG_IMAGE); - RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); + RenderSystem.setShaderTexture(0, BG_IMAGE); + RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); // blit(poseStack, x, y, 0, 0, 256, 176); - - x += 18; - y += 18; - - for(int i = 0; i < lines.size(); i++) { -// if(selectedLine == i) { -// drawWhiteQuad(x - 1, y - 2, font.width(lines.get(i)) + 1, 12); -// font.drawShadow(poseStack, lines.get(i), x, y, 0xFF129700, false); -// } else -// font.drawShadow(poseStack, lines.get(i), x, y, 0xFFFFFFFF, false); - - y += 12; - } - - if(!promptLocked) { -// if (queue.isEmpty()) { -// x = font.drawShadow(poseStack, userPrompt, x, y, 0xFFFFFFFF, false); -// x = font.drawShadow(poseStack, prompt, x, y, 0xFFFFFFFF, false); -// } else { -// x = font.drawShadow(poseStack, tr("press_for_more"), x, y, 0xFFFFFFFF, false); -// } - } - - if(!uploadWizard && blinkTime < 5) - drawWhiteQuad(x + 1, y, 6, 8); + + x += 18; + y += 18; + + for (int i = 0; i < lines.size(); i++) { + if (selectedLine == i) { + drawWhiteQuad(x - 1, y - 2, font.width(lines.get(i)) + 1, 12); + poseStack.drawString(Minecraft.getInstance().font, lines.get(i), x, y, 0xFF129700, false); + } else + poseStack.drawString(Minecraft.getInstance().font, lines.get(i), x, y, 0xFFFFFFFF, false); + + y += 12; + } + + if (!promptLocked) { + if (queue.isEmpty()) { + x = poseStack.drawString(Minecraft.getInstance().font, userPrompt, x, y, 0xFFFFFFFF, false); + x = poseStack.drawString(Minecraft.getInstance().font, prompt, x, y, 0xFFFFFFFF, false); + } else { + x = poseStack.drawString(Minecraft.getInstance().font, tr("press_for_more"), x, y, 0xFFFFFFFF, false); + } + } + + if (!uploadWizard && blinkTime < 5) + drawWhiteQuad(x + 1, y, 6, 8); // RenderSystem.enableTexture(); - RenderSystem.enableBlend(); - RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); - RenderSystem.setShaderTexture(0, FG_IMAGE); - RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); + RenderSystem.enableBlend(); + RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); + RenderSystem.setShaderTexture(0, FG_IMAGE); + RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); // blit(poseStack,(width - 256) / 2, (height - 176) / 2, 0, 0, 256, 176); - } - - private void drawWhiteQuad(int x, int y, int w, int h) { - float xd = (float) x; - float xd2 = (float) (x + w); - float yd = (float) y; - float yd2 = (float) (y + h); - float zd = (float) getBlitOffset(); + } + + private void drawWhiteQuad(int x, int y, int w, int h) { + float xd = (float) x; + float xd2 = (float) (x + w); + float yd = (float) y; + float yd2 = (float) (y + h); + float zd = (float) getBlitOffset(); // RenderSystem.disableTexture(); - RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); - Tesselator t = Tesselator.getInstance(); - BufferBuilder bb = t.getBuilder(); - bb.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); - bb.vertex(xd, yd2, zd).endVertex(); - bb.vertex(xd2, yd2, zd).endVertex(); - bb.vertex(xd2, yd, zd).endVertex(); - bb.vertex(xd, yd, zd).endVertex(); - t.end(); + RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); + Tesselator t = Tesselator.getInstance(); + BufferBuilder bb = t.getBuilder(); + bb.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); + bb.vertex(xd, yd2, zd).endVertex(); + bb.vertex(xd2, yd2, zd).endVertex(); + bb.vertex(xd2, yd, zd).endVertex(); + bb.vertex(xd, yd, zd).endVertex(); + t.end(); // RenderSystem.enableTexture(); - } - - private float getBlitOffset() { - return 0; - } - - @Override - public void tick() { - super.tick(); - - if(accessState >= 0) { - if(--accessTime <= 0) { - accessState++; - - if(accessState == 1) { - if(lines.size() > 0) - lines.remove(lines.size() - 1); - - lines.add("access: PERMISSION DENIED....and..."); - accessTime = 20; - } else { - if(accessSound == null) { - accessSound = new SimpleSoundInstance( WebDisplays.INSTANCE.soundServer.getLocation(), SoundSource.MASTER, 1.0f, 1.0f, RandomSource.create(),true, 0, SoundInstance.Attenuation.NONE, 0.0f, 0.0f, 0.0f, false); - minecraft.getSoundManager().play(accessSound); - } - - writeLine("YOU DIDN'T SAY THE MAGIC WORD!"); - accessTime = 2; - } - } - } else { - blinkTime = (blinkTime + 1) % 10; - - if(currentTask != null) { - long queryTime; - synchronized(this) { - queryTime = this.queryTime; - } - - if(System.currentTimeMillis() - queryTime >= 10000) { - writeLine(tr("timeout")); - currentTask.cancel(); - clearTask(); - } - } - - if(!uploadFilter.isEmpty() && System.currentTimeMillis() - uploadFilterTime >= 1000) { - Log.info("Upload filter cleared"); - uploadFilter = ""; - } - } - - final int maxl = uploadWizard ? MAX_LINES : (MAX_LINES - 1); //Cuz prompt is hidden - if (!queue.isEmpty()) { - while (!queue.isEmpty()) { - if (lines.size() >= maxl) - break; - writeLine(queue.remove(0)); - } - } - - while (lines.size() > maxl) - lines.remove(0); - } - - @Override - public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - Supplier predicate = () -> super.keyReleased(keyCode, scanCode, modifiers); - - try { - return handleKeyboardInput(keyCode, false, predicate); - } catch (IOException e) { - e.printStackTrace(); - return false; - } - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (keyCode == GLFW.GLFW_KEY_ESCAPE && !uploadWizard) { - Minecraft.getInstance().setScreen(null); - return true; - } - - getChar(keyCode, scanCode).ifPresent(c -> { - try { - keyTyped(c, keyCode, modifiers); - } catch (IOException e) { - e.printStackTrace(); - } - }); - - try { - return handleKeyboardInput(keyCode, true, () -> true); - } catch (IOException e) { - e.printStackTrace(); - return false; - } - } - - public boolean handleKeyboardInput(int keyCode, boolean keyState, Supplier booleanSupplier) throws IOException { - if (!queue.isEmpty()) - return false; - - if(uploadWizard) { - if(keyState) { - if(keyCode == GLFW.GLFW_KEY_UP) { - if(selectedLine > 3) - selectedLine--; - else if(uploadOffset > 0) { - uploadOffset--; - updateUploadScreen(); - } - } else if(keyCode == GLFW.GLFW_KEY_DOWN) { - if(selectedLine < MAX_LINES - 1) - selectedLine++; - else if(uploadOffset + selectedLine - 2 < uploadFiles.size()) { - uploadOffset++; - updateUploadScreen(); - } - } else if(keyCode == GLFW.GLFW_KEY_PAGE_DOWN) { - selectedLine = 3; - int dst = uploadOffset - (MAX_LINES - 3); - if(dst < 0) - dst = 0; - - selectFile(dst); - } else if(keyCode == GLFW.GLFW_KEY_PAGE_UP) { - selectedLine = 3; - int dst = uploadOffset + (MAX_LINES - 3); - if(dst >= uploadFiles.size()) - dst = uploadFiles.size() - 1; - - selectFile(dst); - } else if(keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) { - File file = uploadFiles.get(uploadOffset + selectedLine - 3); - - if(file.isDirectory()) { - uploadCD(file); - updateUploadScreen(); - } else - startFileUpload(file, true); - } else if(keyCode == GLFW.GLFW_KEY_F5) { - uploadCD(uploadDir); - updateUploadScreen(); - } - } - - if(keyCode == GLFW.GLFW_KEY_ESCAPE) { - quitUploadWizard(); - return true; //Don't let the screen handle this - } - - return booleanSupplier.get(); - } else { - boolean value = booleanSupplier.get(); - - if(keyState) { - boolean ctrl = Screen.hasControlDown(); - - if(keyCode == GLFW.GLFW_KEY_L && ctrl) - lines.clear(); - else if(keyCode == GLFW.GLFW_KEY_V && ctrl) { - prompt += Minecraft.getInstance().keyboardHandler.getClipboard(); - - if(prompt.length() > MAX_LINE_LEN) - prompt = prompt.substring(0, MAX_LINE_LEN); - } else if(keyCode == GLFW.GLFW_KEY_UP) { - if(lastCmd != null) { - String tmp = prompt; - prompt = lastCmd; - lastCmd = tmp; - } - } - } - - return value; - } - } - - @Override - public boolean charTyped(char codePoint, int modifiers) { - return super.charTyped(codePoint, modifiers); - - } - - protected void keyTyped(char typedChar, int keyCode, int modifier) throws IOException { - //this.charTyped(typedChar, modifier); - - if (keyCode == GLFW.GLFW_KEY_DOWN) { - if (!queue.isEmpty()) { - writeLine(queue.remove(0)); - return; - } - } - if (!queue.isEmpty()) - return; - - if (uploadWizard) { - boolean found = false; - uploadFilter += Character.toLowerCase(typedChar); - uploadFilterTime = System.currentTimeMillis(); - - for (int i = uploadFirstIsParent ? 1 : 0; i < uploadFiles.size(); i++) { - if (uploadFiles.get(i).getName().toLowerCase().startsWith(uploadFilter)) { - selectFile(i); - found = true; - break; - } - } - - if (!found && uploadFilter.length() == 1) - uploadFilter = ""; - - return; - } else if (promptLocked) - return; - - if (keyCode == GLFW.GLFW_KEY_SPACE) - typedChar = ' '; - if ( - (typedChar == 'v' || typedChar == 'V') && - (modifier & 2) == 2 - ) return; - - if (keyCode == GLFW.GLFW_KEY_BACKSPACE) { - if (prompt.length() > 0) - prompt = prompt.substring(0, prompt.length() - 1); - } else if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) { - if (prompt.length() > 0) { - writeLine(userPrompt + prompt); - evaluateCommand(prompt); - lastCmd = prompt; - prompt = ""; - } else - writeLine(userPrompt); - } else if (prompt.length() + 1 < MAX_LINE_LEN && typedChar >= 32 && typedChar <= 126) - prompt = prompt + typedChar; - - blinkTime = 0; - } - - private void evaluateCommand(String str) { - String[] args = str.trim().split("\\s+"); - Method handler = COMMAND_MAP.get(args[0].toLowerCase()); - - if(handler == null) { - writeLine(tr("unknowncmd")); - return; - } - - Object[] params; - if(handler.getParameterCount() == 0) - params = new Object[0]; - else { - String[] args2 = new String[args.length - 1]; - System.arraycopy(args, 1, args2, 0, args2.length); - params = new Object[] { args2 }; - } - - try { - handler.invoke(this, params); - } catch(IllegalAccessException | InvocationTargetException e) { - Log.errorEx("Caught exception while running command \"%s\"", e, str); - writeLine(tr("error")); - } - } - - private void writeLine(String line) { - final int maxl = uploadWizard ? MAX_LINES : (MAX_LINES - 1); //Cuz prompt is hidden - while(lines.size() >= maxl) - lines.remove(0); - - lines.add(line); - } - - private static void buildCommandMap() { - COMMAND_MAP.clear(); - - Method[] methods = GuiServer.class.getMethods(); - for(Method m: methods) { - CommandHandler cmd = m.getAnnotation(CommandHandler.class); - - if(cmd != null && Modifier.isPublic(m.getModifiers())) { - if(m.getParameterCount() == 0 || (m.getParameterCount() == 1 && m.getParameterTypes()[0] == String[].class)) - COMMAND_MAP.put(cmd.value().toLowerCase(), m); - } - } - } - - private void quitUploadWizard() { - lines.clear(); - promptLocked = false; - uploadWizard = false; - selectedLine = -1; - } - - @Override - public void onClose() { - super.onClose(); - - if(accessSound != null) - Minecraft.getInstance().getSoundManager().stop(accessSound); - } - - private boolean queueTask(ClientTask task) { - if(Client.getInstance().addTask(task)) { - promptLocked = true; - queryTime = System.currentTimeMillis(); //No task is running so it's okay to have an unsynchronized access here - currentTask = task; - return true; - } else { - writeLine(tr("queryerr")); - return false; - } - } - - private void clearTask() { - promptLocked = false; - currentTask = null; - } - - private static String trimStringL(String str) { - int delta = str.length() - MAX_LINE_LEN; - if(delta <= 0) - return str; - - return "..." + str.substring(delta + 3); - } - - private static String trimStringR(String str) { - return (str.length() <= MAX_LINE_LEN) ? str : (str.substring(0, MAX_LINE_LEN - 3) + "..."); - } - - @CommandHandler("clear") - public void commandClear() { - lines.clear(); - } - - @CommandHandler("help") - public void commandHelp(String[] args) { - queueRead = lines.size(); - - if(args.length > 0) { - String cmd = args[0].toLowerCase(); - - if(COMMAND_MAP.containsKey(cmd)) - queueLine(tr("help." + cmd)); - else - queueLine(tr("unknowncmd")); - } else { - for(String c : COMMAND_MAP.keySet()) - queueLine(c + " - " + tr("help." + c)); - } - } - - @CommandHandler("exit") - public void commandExit() { - minecraft.setScreen(null); - } - - @CommandHandler("access") - public void commandAccess(String[] args) { - boolean handled = false; - - if(args.length >= 1 && args[0].equalsIgnoreCase("security")) { - if(args.length == 1 || (args.length == 2 && args[1].equalsIgnoreCase("grid"))) - handled = true; - } else if(args.length == 3 && args[0].equalsIgnoreCase("main") && args[1].equalsIgnoreCase("security") && args[2].equalsIgnoreCase("grid")) - handled = true; - - if(handled) { - writeLine("access: PERMISSION DENIED."); - - if(++accessTrials >= 3) { - promptLocked = true; - accessState = 0; - accessTime = 20; - } - } else - writeLine(tr("argerror")); - } - - @CommandHandler("owner") - public void commandOwner() { - writeLine(tr("ownername", owner.name)); - writeLine(tr("owneruuid")); - writeLine(owner.uuid.toString()); - } - - @CommandHandler("quota") - public void commandQuota() { - if(!minecraft.player.getGameProfile().getId().equals(owner.uuid)) { - writeLine(tr("errowner")); - return; - } - - ClientTaskGetQuota task = new ClientTaskGetQuota(); - task.setFinishCallback((t) -> { - writeLine(tr("quota", Util.sizeString(t.getQuota()), Util.sizeString(t.getMaxQuota()))); - clearTask(); - }); - - queueTask(task); - } - - @CommandHandler("ls") - public void commandList() { - ClientTaskGetFileList task = new ClientTaskGetFileList(owner.uuid); - task.setFinishCallback((t) -> { - String[] files = t.getFileList(); - if(files != null) - Arrays.stream(files).forEach(this::writeLine); - - clearTask(); - }); - - queueTask(task); - } - - @CommandHandler("url") - public void commandURL(String[] args) { - if(args.length < 1) { - writeLine(tr("fnamearg")); - return; - } - - String fname = Util.join(args, " "); - if(Util.isFileNameInvalid(fname)) { - writeLine(tr("nameerr")); - return; - } - - ClientTaskCheckFile task = new ClientTaskCheckFile(owner.uuid, fname); - task.setFinishCallback((t) -> { - int status = t.getStatus(); - if(status == 0) { - writeLine(tr("urlcopied")); - Minecraft.getInstance().keyboardHandler.setClipboard(t.getURL()); - } else if(status == Constants.GETF_STATUS_NOT_FOUND) - writeLine(tr("notfound")); - else - writeLine(tr("error2", status)); - - clearTask(); - }); - - queueTask(task); - } - - private void uploadCD(File newDir) { - try { - uploadDir = newDir.getCanonicalFile(); - } catch(IOException ex) { - uploadDir = newDir; - } - - uploadFiles.clear(); - File parent = uploadDir.getParentFile(); - - if(parent != null && parent.exists()) { - uploadFiles.add(parent); - uploadFirstIsParent = true; - } else - uploadFirstIsParent = false; - - File[] children = uploadDir.listFiles(); - if(children != null) { - Collator c = Collator.getInstance(); - c.setStrength(Collator.SECONDARY); - c.setDecomposition(Collator.CANONICAL_DECOMPOSITION); - - Arrays.stream(children).filter(f -> !f.isHidden() && (f.isDirectory() || f.isFile())).sorted((a, b) -> c.compare(a.getName(), b.getName())).forEach(uploadFiles::add); - } - - uploadOffset = 0; - uploadFilter = ""; - - if(uploadWizard) - selectedLine = 3; - } - - private void updateUploadScreen() { - lines.clear(); - - lines.add(tr("upload.info")); - lines.add(trimStringL(uploadDir.getPath())); - lines.add(""); - - for(int i = uploadOffset; i < uploadFiles.size() && lines.size() < MAX_LINES; i++) { - if(i == 0 && uploadFirstIsParent) - lines.add(tr("upload.parent")); - else - lines.add(trimStringR(uploadFiles.get(i).getName())); - } - } - - private void selectFile(int i) { - int pos = 3 + i - uploadOffset; - if(pos >= 3 && pos < MAX_LINES) { - selectedLine = pos; - return; - } - - uploadOffset = i; - if(uploadOffset + MAX_LINES - 3 > uploadFiles.size()) - uploadOffset = uploadFiles.size() - MAX_LINES + 3; - - updateUploadScreen(); - selectedLine = 3 + i - uploadOffset; - } - - @CommandHandler("upload") - public void commandUpload(String[] args) { - if(!minecraft.player.getGameProfile().getId().equals(owner.uuid)) { - writeLine(tr("errowner")); - return; - } - - if(args.length > 0) { - File fle = new File(Util.join(args, " ")); - if(!fle.exists()) { - writeLine(tr("notfound")); - return; - } - - if(fle.isDirectory()) - uploadCD(fle); - else if(fle.isFile()) { - startFileUpload(fle, false); - return; - } else { - writeLine(tr("notfound")); - return; - } - } - - uploadWizard = true; - promptLocked = true; - uploadOffset = 0; - selectedLine = 3; - updateUploadScreen(); - } - - @CommandHandler("rm") - public void commandDelete(String[] args) { - if(!minecraft.player.getGameProfile().getId().equals(owner.uuid)) { - writeLine(tr("errowner")); - return; - } - - if(args.length < 1) { - writeLine(tr("fnamearg")); - return; - } - - String fname = Util.join(args, " "); - if(Util.isFileNameInvalid(fname)) { - writeLine(tr("nameerr")); - return; - } - - ClientTaskDeleteFile task = new ClientTaskDeleteFile(fname); - task.setFinishCallback((t) -> { - int status = t.getStatus(); - if(status == 1) - writeLine(tr("notfound")); - else if(status != 0) - writeLine(tr("error")); - - clearTask(); - }); - - queueTask(task); - } - - @CommandHandler("reconnect") - public void commandReconnect() { - Client.getInstance().stop(); - WDNetworkRegistry.INSTANCE.sendToServer(Client.getInstance().beginConnection()); - } - - private void startFileUpload(File f, boolean quit) { - if(quit) - quitUploadWizard(); - - if(Util.isFileNameInvalid(f.getName()) || f.getName().length() >= MAX_LINE_LEN - 3) { - writeLine(tr("nameerr")); - return; - } - - ClientTaskUploadFile task; - try { - task = new ClientTaskUploadFile(f); - } catch(IOException ex) { - writeLine(tr("error")); - ex.printStackTrace(); - return; - } - - task.setProgressCallback((cur, total) -> { - synchronized(GuiServer.this) { - queryTime = System.currentTimeMillis(); - } - }); - - task.setFinishCallback(t -> { - int status = t.getUploadStatus(); - if(status == 0) - writeLine(tr("upload.done")); - else if(status == Constants.FUPA_STATUS_FILE_EXISTS) - writeLine(tr("upload.exists")); - else if(status == Constants.FUPA_STATUS_EXCEEDS_QUOTA) - writeLine(tr("upload.quota")); - else - writeLine(tr("error2", status)); - - clearTask(); - }); - - if(queueTask(task)) - writeLine(tr("upload.uploading")); - } - - @Override - public boolean isForBlock(BlockPos bp, BlockSide side) { - return serverPos.equalsBlockPos(bp); - } - - @Nullable - @Override - public String getWikiPageName() { - return "Server"; - } - - int queueRead = 0; - ArrayList queue = new ArrayList<>(); - - private void queueLine(String line) { - final int maxl = uploadWizard ? MAX_LINES : (MAX_LINES - 1); //Cuz prompt is hidden - if (lines.size() < maxl) - writeLine(line); - else if (queueRead > 1) { - writeLine(line); - queueRead -= 1; - } else queue.add(line); - } + } + + private float getBlitOffset() { + return 0; + } + + @Override + public void tick() { + super.tick(); + + if (accessState >= 0) { + if (--accessTime <= 0) { + accessState++; + + if (accessState == 1) { + if (lines.size() > 0) + lines.remove(lines.size() - 1); + + lines.add("access: PERMISSION DENIED....and..."); + accessTime = 20; + } else { + if (accessSound == null) { + accessSound = new SimpleSoundInstance(WebDisplays.INSTANCE.soundServer.getLocation(), SoundSource.MASTER, 1.0f, 1.0f, RandomSource.create(), true, 0, SoundInstance.Attenuation.NONE, 0.0f, 0.0f, 0.0f, false); + minecraft.getSoundManager().play(accessSound); + } + + writeLine("YOU DIDN'T SAY THE MAGIC WORD!"); + accessTime = 2; + } + } + } else { + blinkTime = (blinkTime + 1) % 10; + + if (currentTask != null) { + long queryTime; + synchronized (this) { + queryTime = this.queryTime; + } + + if (System.currentTimeMillis() - queryTime >= 10000) { + writeLine(tr("timeout")); + currentTask.cancel(); + clearTask(); + } + } + + if (!uploadFilter.isEmpty() && System.currentTimeMillis() - uploadFilterTime >= 1000) { + Log.info("Upload filter cleared"); + uploadFilter = ""; + } + } + + final int maxl = uploadWizard ? MAX_LINES : (MAX_LINES - 1); //Cuz prompt is hidden + if (!queue.isEmpty()) { + while (!queue.isEmpty()) { + if (lines.size() >= maxl) + break; + writeLine(queue.remove(0)); + } + } + + while (lines.size() > maxl) + lines.remove(0); + } + + @Override + public boolean keyReleased(int keyCode, int scanCode, int modifiers) { + Supplier predicate = () -> super.keyReleased(keyCode, scanCode, modifiers); + + try { + return handleKeyboardInput(keyCode, false, predicate); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (keyCode == GLFW.GLFW_KEY_ESCAPE && !uploadWizard) { + Minecraft.getInstance().setScreen(null); + return true; + } + + getChar(keyCode, scanCode).ifPresent(c -> { + try { + keyTyped(c, keyCode, modifiers); + } catch (IOException e) { + e.printStackTrace(); + } + }); + + try { + return handleKeyboardInput(keyCode, true, () -> true); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + + public boolean handleKeyboardInput(int keyCode, boolean keyState, Supplier booleanSupplier) throws IOException { + if (!queue.isEmpty()) + return false; + + if (uploadWizard) { + if (keyState) { + if (keyCode == GLFW.GLFW_KEY_UP) { + if (selectedLine > 3) + selectedLine--; + else if (uploadOffset > 0) { + uploadOffset--; + updateUploadScreen(); + } + } else if (keyCode == GLFW.GLFW_KEY_DOWN) { + if (selectedLine < MAX_LINES - 1) + selectedLine++; + else if (uploadOffset + selectedLine - 2 < uploadFiles.size()) { + uploadOffset++; + updateUploadScreen(); + } + } else if (keyCode == GLFW.GLFW_KEY_PAGE_DOWN) { + selectedLine = 3; + int dst = uploadOffset - (MAX_LINES - 3); + if (dst < 0) + dst = 0; + + selectFile(dst); + } else if (keyCode == GLFW.GLFW_KEY_PAGE_UP) { + selectedLine = 3; + int dst = uploadOffset + (MAX_LINES - 3); + if (dst >= uploadFiles.size()) + dst = uploadFiles.size() - 1; + + selectFile(dst); + } else if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) { + File file = uploadFiles.get(uploadOffset + selectedLine - 3); + + if (file.isDirectory()) { + uploadCD(file); + updateUploadScreen(); + } else + startFileUpload(file, true); + } else if (keyCode == GLFW.GLFW_KEY_F5) { + uploadCD(uploadDir); + updateUploadScreen(); + } + } + + if (keyCode == GLFW.GLFW_KEY_ESCAPE) { + quitUploadWizard(); + return true; //Don't let the screen handle this + } + + return booleanSupplier.get(); + } else { + boolean value = booleanSupplier.get(); + + if (keyState) { + boolean ctrl = Screen.hasControlDown(); + + if (keyCode == GLFW.GLFW_KEY_L && ctrl) + lines.clear(); + else if (keyCode == GLFW.GLFW_KEY_V && ctrl) { + prompt += Minecraft.getInstance().keyboardHandler.getClipboard(); + + if (prompt.length() > MAX_LINE_LEN) + prompt = prompt.substring(0, MAX_LINE_LEN); + } else if (keyCode == GLFW.GLFW_KEY_UP) { + if (lastCmd != null) { + String tmp = prompt; + prompt = lastCmd; + lastCmd = tmp; + } + } + } + + return value; + } + } + + @Override + public boolean charTyped(char codePoint, int modifiers) { + return super.charTyped(codePoint, modifiers); + + } + + protected void keyTyped(char typedChar, int keyCode, int modifier) throws IOException { + //this.charTyped(typedChar, modifier); + + if (keyCode == GLFW.GLFW_KEY_DOWN) { + if (!queue.isEmpty()) { + writeLine(queue.remove(0)); + return; + } + } + if (!queue.isEmpty()) + return; + + if (uploadWizard) { + boolean found = false; + uploadFilter += Character.toLowerCase(typedChar); + uploadFilterTime = System.currentTimeMillis(); + + for (int i = uploadFirstIsParent ? 1 : 0; i < uploadFiles.size(); i++) { + if (uploadFiles.get(i).getName().toLowerCase().startsWith(uploadFilter)) { + selectFile(i); + found = true; + break; + } + } + + if (!found && uploadFilter.length() == 1) + uploadFilter = ""; + + return; + } else if (promptLocked) + return; + + if (keyCode == GLFW.GLFW_KEY_SPACE) + typedChar = ' '; + if ( + (typedChar == 'v' || typedChar == 'V') && + (modifier & 2) == 2 + ) return; + + if (keyCode == GLFW.GLFW_KEY_BACKSPACE) { + if (prompt.length() > 0) + prompt = prompt.substring(0, prompt.length() - 1); + } else if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) { + if (prompt.length() > 0) { + writeLine(userPrompt + prompt); + evaluateCommand(prompt); + lastCmd = prompt; + prompt = ""; + } else + writeLine(userPrompt); + } else if (prompt.length() + 1 < MAX_LINE_LEN && typedChar >= 32 && typedChar <= 126) + prompt = prompt + typedChar; + + blinkTime = 0; + } + + private void evaluateCommand(String str) { + String[] args = str.trim().split("\\s+"); + Method handler = COMMAND_MAP.get(args[0].toLowerCase()); + + if (handler == null) { + writeLine(tr("unknowncmd")); + return; + } + + Object[] params; + if (handler.getParameterCount() == 0) + params = new Object[0]; + else { + String[] args2 = new String[args.length - 1]; + System.arraycopy(args, 1, args2, 0, args2.length); + params = new Object[]{args2}; + } + + try { + handler.invoke(this, params); + } catch (IllegalAccessException | InvocationTargetException e) { + Log.errorEx("Caught exception while running command \"%s\"", e, str); + writeLine(tr("error")); + } + } + + private void writeLine(String line) { + final int maxl = uploadWizard ? MAX_LINES : (MAX_LINES - 1); //Cuz prompt is hidden + while (lines.size() >= maxl) + lines.remove(0); + + lines.add(line); + } + + private static void buildCommandMap() { + COMMAND_MAP.clear(); + + Method[] methods = GuiServer.class.getMethods(); + for (Method m : methods) { + CommandHandler cmd = m.getAnnotation(CommandHandler.class); + + if (cmd != null && Modifier.isPublic(m.getModifiers())) { + if (m.getParameterCount() == 0 || (m.getParameterCount() == 1 && m.getParameterTypes()[0] == String[].class)) + COMMAND_MAP.put(cmd.value().toLowerCase(), m); + } + } + } + + private void quitUploadWizard() { + lines.clear(); + promptLocked = false; + uploadWizard = false; + selectedLine = -1; + } + + @Override + public void onClose() { + super.onClose(); + + if (accessSound != null) + Minecraft.getInstance().getSoundManager().stop(accessSound); + } + + private boolean queueTask(ClientTask task) { + if (Client.getInstance().addTask(task)) { + promptLocked = true; + queryTime = System.currentTimeMillis(); //No task is running so it's okay to have an unsynchronized access here + currentTask = task; + return true; + } else { + writeLine(tr("queryerr")); + return false; + } + } + + private void clearTask() { + promptLocked = false; + currentTask = null; + } + + private static String trimStringL(String str) { + int delta = str.length() - MAX_LINE_LEN; + if (delta <= 0) + return str; + + return "..." + str.substring(delta + 3); + } + + private static String trimStringR(String str) { + return (str.length() <= MAX_LINE_LEN) ? str : (str.substring(0, MAX_LINE_LEN - 3) + "..."); + } + + @CommandHandler("clear") + public void commandClear() { + lines.clear(); + } + + @CommandHandler("help") + public void commandHelp(String[] args) { + queueRead = lines.size(); + + if (args.length > 0) { + String cmd = args[0].toLowerCase(); + + if (COMMAND_MAP.containsKey(cmd)) + queueLine(tr("help." + cmd)); + else + queueLine(tr("unknowncmd")); + } else { + for (String c : COMMAND_MAP.keySet()) + queueLine(c + " - " + tr("help." + c)); + } + } + + @CommandHandler("exit") + public void commandExit() { + minecraft.setScreen(null); + } + + @CommandHandler("access") + public void commandAccess(String[] args) { + boolean handled = false; + + if (args.length >= 1 && args[0].equalsIgnoreCase("security")) { + if (args.length == 1 || (args.length == 2 && args[1].equalsIgnoreCase("grid"))) + handled = true; + } else if (args.length == 3 && args[0].equalsIgnoreCase("main") && args[1].equalsIgnoreCase("security") && args[2].equalsIgnoreCase("grid")) + handled = true; + + if (handled) { + writeLine("access: PERMISSION DENIED."); + + if (++accessTrials >= 3) { + promptLocked = true; + accessState = 0; + accessTime = 20; + } + } else + writeLine(tr("argerror")); + } + + @CommandHandler("owner") + public void commandOwner() { + writeLine(tr("ownername", owner.name)); + writeLine(tr("owneruuid")); + writeLine(owner.uuid.toString()); + } + + @CommandHandler("quota") + public void commandQuota() { + if (!minecraft.player.getGameProfile().getId().equals(owner.uuid)) { + writeLine(tr("errowner")); + return; + } + + ClientTaskGetQuota task = new ClientTaskGetQuota(); + task.setFinishCallback((t) -> { + writeLine(tr("quota", Util.sizeString(t.getQuota()), Util.sizeString(t.getMaxQuota()))); + clearTask(); + }); + + queueTask(task); + } + + @CommandHandler("ls") + public void commandList() { + ClientTaskGetFileList task = new ClientTaskGetFileList(owner.uuid); + task.setFinishCallback((t) -> { + String[] files = t.getFileList(); + if (files != null) + Arrays.stream(files).forEach(this::writeLine); + + clearTask(); + }); + + queueTask(task); + } + + @CommandHandler("url") + public void commandURL(String[] args) { + if (args.length < 1) { + writeLine(tr("fnamearg")); + return; + } + + String fname = Util.join(args, " "); + if (Util.isFileNameInvalid(fname)) { + writeLine(tr("nameerr")); + return; + } + + ClientTaskCheckFile task = new ClientTaskCheckFile(owner.uuid, fname); + task.setFinishCallback((t) -> { + int status = t.getStatus(); + if (status == 0) { + writeLine(tr("urlcopied")); + Minecraft.getInstance().keyboardHandler.setClipboard(t.getURL()); + } else if (status == Constants.GETF_STATUS_NOT_FOUND) + writeLine(tr("notfound")); + else + writeLine(tr("error2", status)); + + clearTask(); + }); + + queueTask(task); + } + + private void uploadCD(File newDir) { + try { + uploadDir = newDir.getCanonicalFile(); + } catch (IOException ex) { + uploadDir = newDir; + } + + uploadFiles.clear(); + File parent = uploadDir.getParentFile(); + + if (parent != null && parent.exists()) { + uploadFiles.add(parent); + uploadFirstIsParent = true; + } else + uploadFirstIsParent = false; + + File[] children = uploadDir.listFiles(); + if (children != null) { + Collator c = Collator.getInstance(); + c.setStrength(Collator.SECONDARY); + c.setDecomposition(Collator.CANONICAL_DECOMPOSITION); + + Arrays.stream(children).filter(f -> !f.isHidden() && (f.isDirectory() || f.isFile())).sorted((a, b) -> c.compare(a.getName(), b.getName())).forEach(uploadFiles::add); + } + + uploadOffset = 0; + uploadFilter = ""; + + if (uploadWizard) + selectedLine = 3; + } + + private void updateUploadScreen() { + lines.clear(); + + lines.add(tr("upload.info")); + lines.add(trimStringL(uploadDir.getPath())); + lines.add(""); + + for (int i = uploadOffset; i < uploadFiles.size() && lines.size() < MAX_LINES; i++) { + if (i == 0 && uploadFirstIsParent) + lines.add(tr("upload.parent")); + else + lines.add(trimStringR(uploadFiles.get(i).getName())); + } + } + + private void selectFile(int i) { + int pos = 3 + i - uploadOffset; + if (pos >= 3 && pos < MAX_LINES) { + selectedLine = pos; + return; + } + + uploadOffset = i; + if (uploadOffset + MAX_LINES - 3 > uploadFiles.size()) + uploadOffset = uploadFiles.size() - MAX_LINES + 3; + + updateUploadScreen(); + selectedLine = 3 + i - uploadOffset; + } + + @CommandHandler("upload") + public void commandUpload(String[] args) { + if (!minecraft.player.getGameProfile().getId().equals(owner.uuid)) { + writeLine(tr("errowner")); + return; + } + + if (args.length > 0) { + File fle = new File(Util.join(args, " ")); + if (!fle.exists()) { + writeLine(tr("notfound")); + return; + } + + if (fle.isDirectory()) + uploadCD(fle); + else if (fle.isFile()) { + startFileUpload(fle, false); + return; + } else { + writeLine(tr("notfound")); + return; + } + } + + uploadWizard = true; + promptLocked = true; + uploadOffset = 0; + selectedLine = 3; + updateUploadScreen(); + } + + @CommandHandler("rm") + public void commandDelete(String[] args) { + if (!minecraft.player.getGameProfile().getId().equals(owner.uuid)) { + writeLine(tr("errowner")); + return; + } + + if (args.length < 1) { + writeLine(tr("fnamearg")); + return; + } + + String fname = Util.join(args, " "); + if (Util.isFileNameInvalid(fname)) { + writeLine(tr("nameerr")); + return; + } + + ClientTaskDeleteFile task = new ClientTaskDeleteFile(fname); + task.setFinishCallback((t) -> { + int status = t.getStatus(); + if (status == 1) + writeLine(tr("notfound")); + else if (status != 0) + writeLine(tr("error")); + + clearTask(); + }); + + queueTask(task); + } + + @CommandHandler("reconnect") + public void commandReconnect() { + Client.getInstance().stop(); + WDNetworkRegistry.INSTANCE.sendToServer(Client.getInstance().beginConnection()); + } + + private void startFileUpload(File f, boolean quit) { + if (quit) + quitUploadWizard(); + + if (Util.isFileNameInvalid(f.getName()) || f.getName().length() >= MAX_LINE_LEN - 3) { + writeLine(tr("nameerr")); + return; + } + + ClientTaskUploadFile task; + try { + task = new ClientTaskUploadFile(f); + } catch (IOException ex) { + writeLine(tr("error")); + ex.printStackTrace(); + return; + } + + task.setProgressCallback((cur, total) -> { + synchronized (GuiServer.this) { + queryTime = System.currentTimeMillis(); + } + }); + + task.setFinishCallback(t -> { + int status = t.getUploadStatus(); + if (status == 0) + writeLine(tr("upload.done")); + else if (status == Constants.FUPA_STATUS_FILE_EXISTS) + writeLine(tr("upload.exists")); + else if (status == Constants.FUPA_STATUS_EXCEEDS_QUOTA) + writeLine(tr("upload.quota")); + else + writeLine(tr("error2", status)); + + clearTask(); + }); + + if (queueTask(task)) + writeLine(tr("upload.uploading")); + } + + @Override + public boolean isForBlock(BlockPos bp, BlockSide side) { + return serverPos.equalsBlockPos(bp); + } + + @Nullable + @Override + public String getWikiPageName() { + return "Server"; + } + + int queueRead = 0; + ArrayList queue = new ArrayList<>(); + + private void queueLine(String line) { + final int maxl = uploadWizard ? MAX_LINES : (MAX_LINES - 1); //Cuz prompt is hidden + if (lines.size() < maxl) + writeLine(line); + else if (queueRead > 1) { + writeLine(line); + queueRead -= 1; + } else queue.add(line); + } } diff --git a/src/main/java/net/montoyo/wd/client/gui/WDScreen.java b/src/main/java/net/montoyo/wd/client/gui/WDScreen.java index 53ede97..7c6842a 100644 --- a/src/main/java/net/montoyo/wd/client/gui/WDScreen.java +++ b/src/main/java/net/montoyo/wd/client/gui/WDScreen.java @@ -116,11 +116,11 @@ public abstract class WDScreen extends Screen { if(defaultBackground) renderBackground(poseStack); -// for(Control ctrl: controls) -// ctrl.draw(poseStack, mouseX, mouseY, ptt); -// -// for(Control ctrl: postDrawList) -// ctrl.postDraw(poseStack, mouseX, mouseY, ptt); + for(Control ctrl: controls) + ctrl.draw(poseStack, mouseX, mouseY, ptt); + + for(Control ctrl: postDrawList) + ctrl.postDraw(poseStack, mouseX, mouseY, ptt); } @Override @@ -363,12 +363,12 @@ public abstract class WDScreen extends Screen { } } - public void drawItemStackTooltip(PoseStack poseStack, ItemStack is, int x, int y) { -// renderTooltip(poseStack, is, x, y); //Since it's protected... + public void drawItemStackTooltip(GuiGraphics poseStack, ItemStack is, int x, int y) { + poseStack.renderTooltip(Minecraft.getInstance().font, is, x, y); //Since it's protected... } - public void drawTooltip(PoseStack poseStack, List lines, int x, int y) { -// renderTooltip(poseStack, lines.stream().map(a -> FormattedCharSequence.forward(a, Style.EMPTY)).collect(Collectors.toList()), x, y, font); //This is also protected... + public void drawTooltip(GuiGraphics poseStack, List lines, int x, int y) { + poseStack.renderTooltip(Minecraft.getInstance().font, lines.stream().map(a -> FormattedCharSequence.forward(a, Style.EMPTY)).collect(Collectors.toList()), x, y); //This is also protected... } public void requirePostDraw(Control ctrl) { diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Button.java b/src/main/java/net/montoyo/wd/client/gui/controls/Button.java index db41777..f0d1ae0 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Button.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Button.java @@ -9,6 +9,7 @@ import net.minecraft.client.gui.GuiGraphics; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.Style; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import org.lwjgl.glfw.GLFW; @@ -83,6 +84,7 @@ public class Button extends Control { @Override public void draw(GuiGraphics poseStack, int mouseX, int mouseY, float ptt) { + btn.setFGColor(16777215); btn.render(poseStack, mouseX, mouseY, ptt); } @@ -114,20 +116,17 @@ public class Button extends Control { @Override public void setPos(int x, int y) { -// btn.x = x; -// btn.y = y; + btn.setPosition(x, y); } @Override public int getX() { -// return btn.x; - return 0; + return btn.getX(); } @Override public int getY() { -// return btn.y; - return 0; + return btn.getY(); } public net.minecraft.client.gui.components.Button getMcButton() { @@ -217,8 +216,10 @@ public class Button extends Control { @Override public void load(JsonOWrapper json) { super.load(json); -// btn.x = json.getInt("x", 0); -// btn.y = json.getInt("y", 0); + btn.setPosition( + json.getInt("x", 0), + json.getInt("y", 0) + ); btn.setWidth(json.getInt("width", 200)); btn.setHeight(json.getInt("height", 20)); btn.setMessage(Component.nullToEmpty(tr(json.getString("label", btn.getMessage().getContents().toString())))); diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java b/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java index c023261..98aefc1 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java @@ -7,6 +7,7 @@ package net.montoyo.wd.client.gui.controls; import com.google.common.collect.Lists; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.resources.sounds.SimpleSoundInstance; import net.minecraft.resources.ResourceLocation; @@ -77,20 +78,20 @@ public class CheckBox extends BasicControl { @Override public void draw(GuiGraphics poseStack, int mouseX, int mouseY, float ptt) { -// if(visible) { -//// GlStateManager.disableAlpha(); -// poseStack.pushPose(); -// RenderSystem.setShaderTexture(2, checked ? texChecked : texUnchecked); -// RenderSystem.bindTexture(2); -// RenderSystem.enableBlend(); -// fillTexturedRect(poseStack, x, y, WIDTH, HEIGHT, 0.0, 0.0, 1.0, 1.0); -// RenderSystem.disableBlend(); -// RenderSystem.bindTexture(-1); -// -// poseStack.popPose(); -// boolean inside = (!disabled && mouseX >= x && mouseX <= x + WIDTH + 2 + labelW && mouseY >= y && mouseY < y + HEIGHT); -//// font.draw(poseStack, label, x + WIDTH + 2, y + 4, inside ? 0xFF0080FF : COLOR_WHITE); -// } + if(visible) { +// GlStateManager.disableAlpha(); + poseStack.pose().pushPose(); + RenderSystem.setShaderTexture(2, checked ? texChecked : texUnchecked); + RenderSystem.bindTexture(2); + RenderSystem.enableBlend(); + fillTexturedRect(poseStack.pose(), x, y, WIDTH, HEIGHT, 0.0, 0.0, 1.0, 1.0); + RenderSystem.disableBlend(); + RenderSystem.bindTexture(-1); + + poseStack.pose().popPose(); + boolean inside = (!disabled && mouseX >= x && mouseX <= x + WIDTH + 2 + labelW && mouseY >= y && mouseY < y + HEIGHT); + poseStack.drawString(Minecraft.getInstance().font, label, x + WIDTH + 2, y + 4, inside ? 0xFF0080FF : COLOR_WHITE, false); + } } public void setLabel(String label) { @@ -137,7 +138,7 @@ public class CheckBox extends BasicControl { @Override public void postDraw(GuiGraphics poseStack, int mouseX, int mouseY, float ptt) { if(tooltip != null && !disabled && mouseX >= x && mouseX <= x + WIDTH + 2 + labelW && mouseY >= y && mouseY < y + HEIGHT) - parent.drawTooltip(poseStack.pose(), tooltip, mouseX, mouseY); + parent.drawTooltip(poseStack, tooltip, mouseX, mouseY); } } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Control.java b/src/main/java/net/montoyo/wd/client/gui/controls/Control.java index e9e66b2..589a415 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Control.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Control.java @@ -164,11 +164,11 @@ public abstract class Control { RenderSystem.setShaderTexture(0, resLoc); } - public void drawBorder(PoseStack poseStack, int x, int y, int w, int h, int color) { + public void drawBorder(GuiGraphics poseStack, int x, int y, int w, int h, int color) { drawBorder(poseStack, x, y, w, h, color, 1.0); } - public void drawBorder(PoseStack poseStack, int x, int y, int w, int h, int color, double sz) { + public void drawBorder(GuiGraphics poseStack, int x, int y, int w, int h, int color, double sz) { double x1 = (double) x; double y1 = (double) y; double x2 = (double) (x + w); @@ -213,13 +213,17 @@ public abstract class Control { // RenderSystem.enableTexture(); } - public PoseStack beginFramebuffer(RenderTarget fbo, float vpW, float vpH) { + public GuiGraphics beginFramebuffer(RenderTarget fbo, float vpW, float vpH) { + GuiGraphics tmpGraphics = new GuiGraphics(Minecraft.getInstance(), Minecraft.getInstance().renderBuffers().bufferSource()); + fbo.bindWrite(true); RenderSystem.backupProjectionMatrix(); RenderSystem.setProjectionMatrix(new Matrix4f().ortho(0.0f, vpW, vpH, 0.0f, -1.0f,1.0f), VertexSorting.ORTHOGRAPHIC_Z); - PoseStack poseStack = RenderSystem.getModelViewStack(); + tmpGraphics.pose().last().pose().set(RenderSystem.getModelViewStack().last().pose()); + tmpGraphics.pose().last().normal().set(RenderSystem.getModelViewStack().last().normal()); + PoseStack poseStack = tmpGraphics.pose(); poseStack.pushPose(); poseStack.setIdentity(); // poseStack.mulPose(Vector3f.XP.rotationDegrees(180.0f)); @@ -228,17 +232,17 @@ public abstract class Control { if(!fbo.useDepth) RenderSystem.disableDepthTest(); - return poseStack; + return tmpGraphics; } - public void endFramebuffer(PoseStack poseStack, RenderTarget fbo) { + public void endFramebuffer(GuiGraphics poseStack, RenderTarget fbo) { if(!fbo.useDepth) RenderSystem.enableDepthTest(); RenderSystem.colorMask(true, true, true, true); RenderSystem.restoreProjectionMatrix(); - poseStack.popPose(); + poseStack.pose().popPose(); RenderSystem.applyModelViewMatrix(); fbo.unbindWrite(); mc.getMainRenderTarget().bindWrite(true); diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java b/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java index fb19b67..e8425f2 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java @@ -9,6 +9,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexFormat; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import net.montoyo.wd.utilities.Bounds; @@ -164,8 +165,8 @@ public class ControlGroup extends Container { // RenderSystem.enableTexture(); poseStack.pose().popPose(); -// if(labelW != 0) -// font.drawShadow(poseStack, label, x + 10 + ((int) bp), y, labelColor, labelShadowed); + if(labelW != 0) + poseStack.drawString(Minecraft.getInstance().font, label, x + 10 + ((int) bp), y, labelColor, labelShadowed); } } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Label.java b/src/main/java/net/montoyo/wd/client/gui/controls/Label.java index a6b24ad..200a382 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Label.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Label.java @@ -5,6 +5,7 @@ package net.montoyo.wd.client.gui.controls; import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.montoyo.wd.client.gui.loading.JsonOWrapper; @@ -74,8 +75,11 @@ public class Label extends BasicControl { @Override public void draw(GuiGraphics poseStack, int mouseX, int mouseY, float ptt) { -// if(visible) -// font.drawShadow(poseStack, label, x, y, color, shadowed); + if(visible) + poseStack.drawString( + Minecraft.getInstance().font, + label, x, y, color, shadowed + ); } @Override diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/List.java b/src/main/java/net/montoyo/wd/client/gui/controls/List.java index 2282afa..b707751 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/List.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/List.java @@ -107,8 +107,8 @@ public class List extends BasicControl { } private void renderToFBO() { - PoseStack poseStack = beginFramebuffer(fbo, width, height); - poseStack.pushPose(); + GuiGraphics poseStack = beginFramebuffer(fbo, width, height); + poseStack.pose().pushPose(); fillRect(0, 0, width, height, COLOR_BLACK); RenderSystem.setShaderColor(1.f, 1.f, 1.f, 1.f); @@ -121,13 +121,13 @@ public class List extends BasicControl { break; int color = (i == selected) ? selColor : COLOR_WHITE; -// font.draw(poseStack, content.get(i).text, 4, i * 12 + offset, color); + poseStack.drawString(font, content.get(i).text, 4, i * 12 + offset, color); } } drawBorder(poseStack, 0, 0, width, height, 0xFF808080); endFramebuffer(poseStack, fbo); - poseStack.popPose(); + poseStack.pose().popPose(); } @Override @@ -321,7 +321,7 @@ public class List extends BasicControl { fbo.bindRead(); //TODO: Make sure is right RenderSystem.setShaderColor(1.f, 1.f, 1.f, 1.f); -// fillTexturedRect(poseStack, x, y, width, height, 0.0, 1.0, 1.0, 0.0); + fillTexturedRect(poseStack.pose(), x, y, width, height, 0.0, 1.0, 1.0, 0.0); fbo.unbindRead(); fillRect(x + width - 5, y + 1 + scrollPos, 4, scrollSize, (scrolling || isInScrollbar(mouseX, mouseY)) ? 0xFF202020 : 0xFF404040); diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java b/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java index 5405196..dc7d79a 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java @@ -166,7 +166,9 @@ public class TextField extends Control { @Override public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { - return field.mouseClicked(mouseX, mouseY, mouseButton); + if (field.mouseClicked(mouseX, mouseY, mouseButton)) + setFocused(true); + return false; } @Override @@ -242,25 +244,25 @@ public class TextField extends Control { @Override public void setPos(int x, int y) { -// field.x = x + 1; -// field.y = y + 1; + field.setPosition( + x + 1, + y + 1 + ); } @Override public int getX() { -// return field.x - 1; - return 0; + return field.getX() - 1; } @Override public int getY() { -// return field.y - 1; - return 0; + return field.getY() - 1; } public void setDisabled(boolean en) { enabled = !en; -// field.setFocus(enabled); + field.setFocused(enabled); } public boolean isDisabled() { @@ -268,12 +270,12 @@ public class TextField extends Control { } public void enable() { -// field.setFocus(true); + field.setFocused(true); enabled = true; } public void disable() { -// field.setFocus(false); + field.setFocused(false); enabled = false; } @@ -294,7 +296,7 @@ public class TextField extends Control { } public void setFocused(boolean val) { -// field.setFocus(val); + field.setFocused(val); } public boolean hasFocus() { @@ -302,7 +304,7 @@ public class TextField extends Control { } public void focus() { -// field.setFocus(true); + field.setFocused(true); } public void setTextColor(int color) { @@ -339,8 +341,10 @@ public class TextField extends Control { @Override public void load(JsonOWrapper json) { super.load(json); -// field.x = json.getInt("x", 0) + 1; -// field.y = json.getInt("y", 0) + 1; + field.setPosition( + json.getInt("x", 0) + 1, + json.getInt("y", 0) + 1 + ); field.setWidth(json.getInt("width", 200) - 2); field.setHeight(json.getInt("height", 22) - 2); field.setValue(tr(json.getString("text", ""))); diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java b/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java index f391d53..8f54a08 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java @@ -45,7 +45,7 @@ public class UpgradeGroup extends BasicControl { @Override public void postDraw(GuiGraphics poseStack, int mouseX, int mouseY, float ptt) { if(overStack != null) - parent.drawItemStackTooltip(poseStack.pose(), overStack, mouseX, mouseY); + parent.drawItemStackTooltip(poseStack, overStack, mouseX, mouseY); } @Override diff --git a/src/main/java/net/montoyo/wd/entity/TileEntityInterfaceBase.java b/src/main/java/net/montoyo/wd/entity/TileEntityInterfaceBase.java index ae07714..6ce26ea 100644 --- a/src/main/java/net/montoyo/wd/entity/TileEntityInterfaceBase.java +++ b/src/main/java/net/montoyo/wd/entity/TileEntityInterfaceBase.java @@ -317,7 +317,7 @@ public abstract class TileEntityInterfaceBase extends TileEntityPeripheralBase { code = ((Double) oCode).intValue(); } - data.add(new TypeData(dataAction, code, 0)); + data.add(new TypeData(dataAction, code, 0, 0)); } return realType(WebDisplays.GSON.toJson(data)); diff --git a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java index 86636e3..368a467 100644 --- a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java +++ b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java @@ -556,20 +556,23 @@ public class TileEntityScreen extends BlockEntity { return; } - if (scr.browser != null) { - if (scr.browser instanceof MCEFBrowser mcefBrowser) { - if (event == ClickControl.ControlType.CLICK) { - mcefBrowser.sendMouseMove(vec.x, vec.y); //Move to target - mcefBrowser.sendMousePress(vec.x, vec.y, button); //Press - mcefBrowser.sendMouseRelease(vec.x, vec.y, button); //Release - } else if (event == ClickControl.ControlType.DOWN) { - mcefBrowser.sendMouseMove(vec.x, vec.y); //Move to target - mcefBrowser.sendMousePress(vec.x, vec.y, button); //Press - } else if (event == ClickControl.ControlType.MOVE) - mcefBrowser.sendMouseMove(vec.x, vec.y); //Move - else if (event == ClickControl.ControlType.UP) - mcefBrowser.sendMouseRelease(scr.lastMousePos.x, scr.lastMousePos.y, button); //Release - } + if (scr.browser instanceof MCEFBrowser mcefBrowser) { + if (button == 1) button = 0; + else if (button == 0) button = 1; + + if (event == ClickControl.ControlType.CLICK) { + mcefBrowser.sendMouseMove(vec.x, vec.y); //Move to target + mcefBrowser.sendMousePress(vec.x, vec.y, button); //Press + mcefBrowser.sendMouseRelease(vec.x, vec.y, button); //Release + } else if (event == ClickControl.ControlType.DOWN) { + mcefBrowser.sendMouseMove(vec.x, vec.y); //Move to target + mcefBrowser.sendMousePress(vec.x, vec.y, button); //Press + } else if (event == ClickControl.ControlType.MOVE) + mcefBrowser.sendMouseMove(vec.x, vec.y); //Move + else if (event == ClickControl.ControlType.UP) + mcefBrowser.sendMouseRelease(scr.lastMousePos.x, scr.lastMousePos.y, button); //Release + + mcefBrowser.setFocus(true); if (vec != null) { scr.lastMousePos.x = vec.x; @@ -889,18 +892,19 @@ public class TileEntityScreen extends BlockEntity { if (ev.getKeyCode() == 257) { ev = new TypeData( ev.getAction(), - 10, ev.getModifier() + 10, ev.getModifier(), + ev.getScanCode() ); } switch (ev.getAction()) { case PRESS -> { - mcefBrowser.sendKeyPress(ev.getKeyCode(), (char) ev.getKeyCode(), ev.getModifier()); + mcefBrowser.sendKeyPress(ev.getKeyCode(), ev.getScanCode(), ev.getModifier()); if (ev.getKeyCode() == 10) mcefBrowser.sendKeyTyped('\r', ev.getModifier()); } case RELEASE -> - mcefBrowser.sendKeyRelease(ev.getKeyCode(), (char) ev.getKeyCode(), ev.getModifier()); + mcefBrowser.sendKeyRelease(ev.getKeyCode(), ev.getScanCode(), ev.getModifier()); case TYPE -> mcefBrowser.sendKeyTyped((char) ev.getKeyCode(), ev.getModifier()); // TODO: check default -> throw new RuntimeException("Invalid type action '" + ev.getAction() + '\''); diff --git a/src/main/java/net/montoyo/wd/utilities/TypeData.java b/src/main/java/net/montoyo/wd/utilities/TypeData.java index 803925f..cdf5f74 100644 --- a/src/main/java/net/montoyo/wd/utilities/TypeData.java +++ b/src/main/java/net/montoyo/wd/utilities/TypeData.java @@ -24,17 +24,20 @@ public class TypeData { private final Action a; private final int k; private final int m; + private final int s; public TypeData() { a = Action.INVALID; k = 0; m = 0; + s = 0; } - public TypeData(Action action, int code, int modifier) { + public TypeData(Action action, int code, int modifier, int scan) { a = action; k = code; m = modifier; + s = scan; } public Action getAction() { @@ -48,4 +51,8 @@ public class TypeData { public int getModifier() { return m; } + + public int getScanCode() { + return s; + } }