How can I fix rectangle going to left down corner when dragged?

104
February 03, 2022, at 09:30 AM

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:

https://streamable.com/qjbl71

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++;
    }
}
Rent Charter Buses Company
READ ALSO
routing error Cannot Get /api/name student activity

routing error Cannot Get /api/name student activity

I am trying to recreate a Note Taking App using ExpressMy code follows the instructor's example but when I deploy it and try to add a new note I get the error cannot get/api/name

103
Send Json file for Async Validation

Send Json file for Async Validation

I am working with Angular reactive forms and async validation and instead of a normal value I want to send Json file for validation

132
I&#39;m on windows os, I&#39;m trying to export the data from mysql workbench to my local file system. But showing an error message like (no disk space)

I'm on windows os, I'm trying to export the data from mysql workbench to my local file system. But showing an error message like (no disk space)

I've searched many threads online but none of them worked for meI found solutions for linux but there is no solution related to windows operating system

107