'How can I fix rectangle going to left down corner when dragged?
I'm making a pvp client for a game called Minecraft (minecraft coder pack) and I'm having trouble with ClickGUI, a feature that allows you to enable other features in a window that appears in the middle of the screen. I have a problem that when I drag a rectangle, it always moves to its bottom left corner, like this:
I want the rectangle to stay in place when the mouse is pressed, only to move when the mouse is moved.
I have tried subtracting values like 50 from categoryPositions.get(draggedCategory) from the right and left from // checking if any category is being moved, then replace category position, but it only makes that rectangle is moving to the middle instead of left.
I found this method, that may help:
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;
}
}
My drawScreen method that is executed every tick:
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
// module category that is being moved
Category draggedCategory = null;
// checking if any category is being moved
for (Map.Entry<Category, Boolean> categoryBooleanEntry : categoryMoved.entrySet()) {
if (categoryBooleanEntry.getValue()) {
draggedCategory = categoryBooleanEntry.getKey();
break;
}
}
// creating ScaledResolution to get scaled with and height
ScaledResolution sr = new ScaledResolution(mc);
// adding category positions one time
if (!completed) {
for (int i = 0; i < Category.values().length; i++) {
int categoryXCoord = (sr.getScaledWidth() / Category.values().length) * i + (sr.getScaledWidth() / Category.values().length) / 2 - xOffset;
// RectPosition values without offset
categoryPositions.put(Category.values()[i], new RectPosition(categoryXCoord, 40, categoryXCoord, 40 + fontRendererObj.FONT_HEIGHT));
}
completed = true;
}
// checking if any category is being moved, then replace category position
if (draggedCategory != null) {
categoryPositions.get(draggedCategory).left = mouseX;
categoryPositions.get(draggedCategory).top = mouseY - offset;
categoryPositions.get(draggedCategory).right = mouseX;
categoryPositions.get(draggedCategory).bottom = mouseY + offset;
}
// drawing background
Gui.drawRect(0, sr.getScaledHeight(), sr.getScaledWidth(), 0, 0x90000000);
// copying values from enum category to arraylist
categories = new ArrayList<>(Arrays.asList(Category.values()));
// getting max width for all categories
for (Category c : categories) {
if (fontRendererObj.getStringWidth(c.name()) > maxWidth) {
maxWidth = fontRendererObj.getStringWidth(c.name() + offset);
}
}
for (Module m : ModuleManager.modules) {
if (fontRendererObj.getStringWidth(m.getName()) > maxWidth) {
maxWidth = fontRendererObj.getStringWidth(m.getName()) + offset;
}
}
int categoryCount = 0;
for (Category c : categories) {
int moduleCount = 0;
int finalCategoryCount = categoryCount;
int stringY = categoryPositions.get(c).top;
int stringX = (categoryPositions.get(c).left - offset + categoryPositions.get(c).left - offset + maxWidth) / 2 - fontRendererObj.getStringWidth(c.name()) / 2;
// drawing rectangle behind category name
Gui.drawRect(categoryPositions.get(c).left - offset, categoryPositions.get(c).top - offset, categoryPositions.get(c).right + maxWidth, categoryPositions.get(c).bottom + offset, new Color(24, 97, 255).getRGB());
// drawing category name
mc.fontRendererObj.drawString(c.name(), stringX, stringY, Color.cyan.getRGB());
// getting modules belonging to current category
modulesMatchingCategory = (ArrayList<Module>) ModuleManager.modules.stream().filter(m -> m.getCategory() == categories.get(finalCategoryCount)).collect(Collectors.toList());
if (categoryExpanded.get(c)) {
for (Module m : modulesMatchingCategory) {
int x = categoryPositions.get(c).left;
int y = ((mc.fontRendererObj.FONT_HEIGHT + yOffset) * moduleCount) + (categoryPositions.get(c).bottom + yOffset);
// boolean to check if the mouse is in rectangle
boolean hovered = mouseX >= x - offset && mouseY >= y - offset && mouseX < x + maxWidth && mouseY < y + mc.fontRendererObj.FONT_HEIGHT + offset;
// drawing rectangle behind module name
Gui.drawRect(x - offset, y - offset, x + maxWidth, y + fontRendererObj.FONT_HEIGHT + offset, m.isToggled() ? new Color(12, 200, 242).getRGB() : hovered ? new Color(12, 74, 242).getRGB() : new Color(0, 119, 230).getRGB());
// drawing string with module name
mc.fontRendererObj.drawString(m.getName(), x, y, -1);
moduleCount++;
}
}
categoryCount++;
}
}
Solution 1:[1]
Instead of creating rectangles from Gui I created HashMap with Category and DraggableComponent method. Then I created for loop inside drawScreen method to call its DraggableComponent draw method.
// adding category positions one time
if (!completed) {
for (int i = 0; i < Category.values().length; i++) {
int categoryXCoord = (sr.getScaledWidth() / Category.values().length) * i + (sr.getScaledWidth() / Category.values().length) / 2 - xOffset;
draggableComponents.put(Category.values()[i], new DraggableComponent(categoryXCoord - offset, 40, maxWidth , fontRendererObj.FONT_HEIGHT - offset, new Color(24, 97, 255).getRGB()));
}
completed = true;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter |
