Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Created September 6, 2024 11:26
Show Gist options
  • Select an option

  • Save Densamisten/0badfcb5a388ac32191e70d180299f7d to your computer and use it in GitHub Desktop.

Select an option

Save Densamisten/0badfcb5a388ac32191e70d180299f7d to your computer and use it in GitHub Desktop.
package exonihility.client.gui;
import exonihility.client.util.cursor.CursorUtil;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.EditBoxWidget;
import net.minecraft.client.option.SimpleOption;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWDropCallback;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CursorConfigScreen extends Screen {
private EditBoxWidget cursorWidth;
private EditBoxWidget cursorHeight;
public CursorConfigScreen(Text title) {
super(title);
}
@Override
protected void init() {
long window = this.client.getWindow().getHandle();
GLFW.glfwSetDropCallback(window, this::onFileDrop);
// Read edit box
cursorWidth = new EditBoxWidget(textRenderer,200, 40, 20, height / 2, Text.literal("200"), Text.literal("200"));
cursorWidth.setText("0"); // Optional: Set default text
cursorHeight = new EditBoxWidget(textRenderer,100, 40, 20, height / 2, Text.literal("200"), Text.literal("200"));
cursorHeight.setText("0"); // Optional: Set default text
addSelectableChild(cursorWidth);
addSelectableChild(cursorHeight);
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
// Draw the screen background and other elements
this.renderBackground(context, mouseX, mouseY, delta);
context.drawCenteredTextWithShadow(textRenderer, "Drag & Drop",this.width / 2, this.height / 2 - 10, Formatting.WHITE.getColorValue());
cursorWidth.render(context, mouseX, mouseY, delta);
cursorHeight.render(context, mouseX, mouseY, delta);
// Draw labels for width and height inputs
context.drawTextWithShadow(this.textRenderer, "Cursor Width:", this.width / 2 - 160, this.height / 2 - 26, Formatting.WHITE.getColorValue());
context.drawTextWithShadow(this.textRenderer, "Cursor Height:", this.width / 2 - 160, this.height / 2 + 14, Formatting.WHITE.getColorValue());
super.render(context, mouseX, mouseY, delta);
}
private void onFileDrop(long window, int count, long names) {
for (int i = 0; i < count; i++) {
String filePath = GLFWDropCallback.getName(names, i);
File file = new File(filePath);
// You can now check if the file is an image and proceed with setting the cursor.
if (file.getName().endsWith(".png")) {
try {
// Get width and height from text boxes
int width = Integer.parseInt(cursorWidth.getText());
int height = Integer.parseInt(cursorHeight.getText());
// Set the cursor with the file and the specified dimensions
setCustomCursorFromFile(file, width, height);
} catch (NumberFormatException e) {
System.err.println("Invalid number format for width or height");
}
} else {
System.err.println("Not a valid image file: " + file.getName());
}
}
}
private void setCustomCursorFromFile(File file, int width, int height) {
try {
BufferedImage image = ImageIO.read(file);
if (image != null) {
// Resize the image if necessary
BufferedImage resizedImage = resizeImage(image, width, height);
CursorUtil.setCursor(resizedImage, 16, 16); // Use default hotspot for now, this can be configurable too
} else {
System.err.println("Failed to load image: " + file.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Optional: Resize image to match the width and height
private BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = outputImage.createGraphics();
graphics.drawImage(resultingImage, 0, 0, null);
graphics.dispose();
return outputImage;
}
@Override
public void removed() {
// Cleanup when the screen is closed, if needed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment