diff options
Diffstat (limited to 'src/main')
4 files changed, 153 insertions, 21 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java index 4103bca..668fdb3 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ColorSelector.java @@ -1,6 +1,5 @@ package cc.polyfrost.oneconfig.gui.elements; -import cc.polyfrost.oneconfig.internal.assets.Colors; import cc.polyfrost.oneconfig.config.core.OneColor; import cc.polyfrost.oneconfig.gui.OneConfigGui; import cc.polyfrost.oneconfig.gui.animations.Animation; @@ -9,21 +8,21 @@ import cc.polyfrost.oneconfig.gui.animations.EaseInOutCubic; import cc.polyfrost.oneconfig.gui.animations.EaseInOutQuad; import cc.polyfrost.oneconfig.gui.elements.text.NumberInputField; import cc.polyfrost.oneconfig.gui.elements.text.TextInputField; +import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.internal.assets.Images; +import cc.polyfrost.oneconfig.internal.assets.SVGs; import cc.polyfrost.oneconfig.internal.config.OneConfigConfig; import cc.polyfrost.oneconfig.renderer.RenderManager; import cc.polyfrost.oneconfig.renderer.font.Fonts; -import cc.polyfrost.oneconfig.internal.assets.Images; -import cc.polyfrost.oneconfig.internal.assets.SVGs; import cc.polyfrost.oneconfig.renderer.scissor.Scissor; import cc.polyfrost.oneconfig.renderer.scissor.ScissorManager; +import cc.polyfrost.oneconfig.utils.IOUtils; import cc.polyfrost.oneconfig.utils.InputUtils; import cc.polyfrost.oneconfig.utils.NetworkUtils; import cc.polyfrost.oneconfig.utils.color.ColorPalette; import org.lwjgl.input.Mouse; import java.awt.*; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; import java.util.ArrayList; public class ColorSelector { @@ -354,11 +353,11 @@ public class ColorSelector { private void parseHex() { if (copyBtn.isClicked()) { - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(color.getHex()), null); + IOUtils.copyStringToClipboard(color.getHex()); } if (pasteBtn.isClicked() && mode != 2) { try { - color.setColorFromHex(Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor).toString()); + color.setColorFromHex(IOUtils.getStringFromClipboard()); hexInput.setInput("#" + color.getHex()); } catch (Exception ignored) { } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java index f0f1447..c164eb6 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java @@ -1,24 +1,23 @@ package cc.polyfrost.oneconfig.gui.elements.text; -import cc.polyfrost.oneconfig.internal.assets.Colors; import cc.polyfrost.oneconfig.gui.elements.BasicElement; +import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.internal.assets.SVGs; +import cc.polyfrost.oneconfig.libs.universal.UKeyboard; import cc.polyfrost.oneconfig.renderer.RenderManager; import cc.polyfrost.oneconfig.renderer.font.Fonts; -import cc.polyfrost.oneconfig.internal.assets.SVGs; import cc.polyfrost.oneconfig.renderer.scissor.Scissor; import cc.polyfrost.oneconfig.renderer.scissor.ScissorManager; +import cc.polyfrost.oneconfig.utils.IOUtils; import cc.polyfrost.oneconfig.utils.InputUtils; import cc.polyfrost.oneconfig.utils.MathUtils; import cc.polyfrost.oneconfig.utils.TextUtils; -import cc.polyfrost.oneconfig.libs.universal.UKeyboard; import org.jetbrains.annotations.NotNull; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; -import java.awt.*; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; import java.util.ArrayList; +import java.util.Objects; public class TextInputField extends BasicElement { @@ -262,15 +261,15 @@ public class TextInputField extends BasicElement { if (toggled) { if (UKeyboard.isKeyComboCtrlC(key)) { if (selectedText != null && start != 0f && end != 0f) { - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selectedText), null); + IOUtils.copyStringToClipboard(selectedText); } return; } if (UKeyboard.isKeyComboCtrlV(key) || key == Keyboard.KEY_INSERT) { // TODO: find the UKeyboard equivalent for insert try { - String clip = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor).toString(); + String clip = IOUtils.getStringFromClipboard(); input = input.substring(0, caretPos) + clip + input.substring(caretPos); - caretPos = caretPos + clip.length(); + caretPos = caretPos + Objects.requireNonNull(clip).length(); return; } catch (Exception e) { e.printStackTrace(); @@ -307,7 +306,7 @@ public class TextInputField extends BasicElement { } if (UKeyboard.isKeyComboCtrlX(key)) { if (selectedText != null && start != 0f && end != 0f) { - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selectedText), null); + IOUtils.copyStringToClipboard(selectedText); key = UKeyboard.KEY_BACKSPACE; } else return; } diff --git a/src/main/java/cc/polyfrost/oneconfig/images/Image.java b/src/main/java/cc/polyfrost/oneconfig/images/Image.java index 9cc75a0..caae084 100644 --- a/src/main/java/cc/polyfrost/oneconfig/images/Image.java +++ b/src/main/java/cc/polyfrost/oneconfig/images/Image.java @@ -1,12 +1,20 @@ package cc.polyfrost.oneconfig.images; +import cc.polyfrost.oneconfig.internal.OneConfig; +import cc.polyfrost.oneconfig.utils.IOUtils; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.AffineTransform; -import java.awt.image.*; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; +import java.awt.image.AffineTransformOp; +import java.awt.image.BufferedImage; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.Base64; import java.util.Objects; /** An Image wrapper class that is used by the OneConfig system.*/ @@ -125,6 +133,57 @@ public class Image { ImageIO.write(image, "png", new File(filePath)); } + /** Attempt to upload the image to Imgur, returning the JSON that the server replied with. */ + public JsonObject uploadToImgur() { + try { + // thanks stack overflow for the help with this :_) + URL url = new URL("https://api.imgur.com/3/image"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setDoInput(true); + con.setDoOutput(true); + con.setRequestMethod("POST"); + con.setRequestProperty("Authorization", "Client-ID " + "6cfc432a9954f4d"); + con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + con.connect(); + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + ImageIO.write(image, "png", byteOut); + String encoded = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.getEncoder().encodeToString(byteOut.toByteArray()), "UTF-8"); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream())); + writer.write(encoded); + byteOut.close(); + writer.close(); + if(con.getResponseCode() != 200) { + OneConfig.LOGGER.error("Error uploading image to Imgur: " + con.getResponseCode()); + return null; + } + + BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream())); + JsonObject object = new JsonParser().parse(rd).getAsJsonObject(); + rd.close(); + + return object; + } catch (Exception e) { + e.printStackTrace(); + OneConfig.LOGGER.error("Error uploading image to Imgur."); + return null; + } + } + + /** Attempt to upload the image to Imgur, returning the URL that the image is hosted at. + * @param copy weather or not to copy the URL to the clipboard as well. */ + public String uploadToImgur(boolean copy) { + JsonObject object = uploadToImgur(); + String link = object.get("data").getAsJsonObject().get("link").getAsString(); + if(copy) IOUtils.copyStringToClipboard(link); + return link; + } + + /** Copy the image to the system clipboard and delete the graphics object. */ + public void copyToClipboard() { + IOUtils.copyImageToClipboard(image); + dispose(); + } + diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java index efb0c16..ee7e80e 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java @@ -1,5 +1,9 @@ package cc.polyfrost.oneconfig.utils; +import org.jetbrains.annotations.NotNull; + +import java.awt.*; +import java.awt.datatransfer.*; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -52,4 +56,75 @@ public final class IOUtils { } } + /** + * Copy the specified String to the System Clipboard. + * @param s the string to copy + */ + public static void copyStringToClipboard(String s) { + StringSelection stringSelection = new StringSelection(s); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); + } + + /** + * Return the String on the system clipboard. + * @return the string on the system clipboard, or null if there is no string on the clipboard or another error occurred. + */ + public static String getStringFromClipboard() { + try { + return Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor).toString(); + } catch (Exception e) { + return null; + } + } + + /** + * Copy the given image to the System Clipboard. + * @param image the image to copy + */ + public static void copyImageToClipboard(Image image) { + ImageSelection imageSelection = new ImageSelection(image); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null); + } + + /** + * Return the image on the system clipboard. + * @return the image on the system clipboard, or null if there is no image on the clipboard or another error occurred. + */ + public static Image getImageFromClipboard() { + try { + return (Image) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.imageFlavor); + } catch (Exception e) { + return null; + } + } + + + + + + private static class ImageSelection implements Transferable { + private final Image image; + public ImageSelection(Image image) { + this.image = image; + } + + @Override + public DataFlavor[] getTransferDataFlavors() { + return new DataFlavor[] {DataFlavor.imageFlavor}; + } + + @Override + public boolean isDataFlavorSupported(DataFlavor flavor) { + return DataFlavor.imageFlavor.equals(flavor); + } + + @NotNull + @Override + public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { + if(!DataFlavor.imageFlavor.equals(flavor)) { + throw new UnsupportedFlavorException(flavor); + } + return image; + } + } }
\ No newline at end of file |