Skip to content

Instantly share code, notes, and snippets.

@chaarlottte
Last active August 16, 2023 14:46
Show Gist options
  • Save chaarlottte/a4e455d635455efe3c78ccbbc071a631 to your computer and use it in GitHub Desktop.
Save chaarlottte/a4e455d635455efe3c78ccbbc071a631 to your computer and use it in GitHub Desktop.
Draggable Component made by someone i dont remember
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import org.lwjgl.input.Mouse;
public class DraggableComponent {
private int x;
private int y;
private int width;
private int height;
private int color;
private int lastX;
private int lastY;
private boolean dragging;
public DraggableComponent(int x, int y, int width, int height, int color){
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.color = color;
}
public int getxPosition() {
return x;
}
public int getyPosition() {
return y;
}
public void setxPosition(int x) {
this.x = x;
}
public void setyPosition(int y) {
this.y = y;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public void draw(int mouseX, int mouseY){
draggingFix(mouseX, mouseY);
Gui.drawRect(this.getxPosition(), this.getyPosition(), this.getxPosition()+this.getWidth(), this.getyPosition()+this.getHeight(), this.getColor());
boolean mouseOverX = (mouseX >= this.getxPosition() && mouseX <= this.getxPosition()+this.getWidth());
boolean mouseOverY = (mouseY >= this.getyPosition() && mouseY <= this.getyPosition()+this.getHeight());
if(mouseOverX && mouseOverY){
if(Mouse.isButtonDown(0)){
if (!this.dragging) {
this.lastX = x - mouseX;
this.lastY = y - mouseY;
this.dragging = true;
}
}
}
}
private void draggingFix(int mouseX, int mouseY) {
if (this.dragging) {
this.x = mouseX + this.lastX;
this.y = mouseY + this.lastY;
if(!Mouse.isButtonDown(0)) this.dragging = false;
}
}
}
@ThePenguin-developer
Copy link

thx quick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment