Подведи собственные итоги года совместно с YOUGAME и забери ценные призы! Перейти

Вопрос Не могу сделать логику для открытия модуля

  • Автор темы Автор темы pupsila
  • Дата начала Дата начала
Начинающий
Начинающий
Статус
Оффлайн
Регистрация
8 Май 2024
Сообщения
32
Реакции
0
Делаю логику что при правой кнопке мыши модуль открывается и показываются компоненти, но все равно при правом клике оно на момент открывается и сразу закрывается, уже перебровывал разные способы, даже чат гпт не помагает
Java:
Expand Collapse Copy
package dev.ui.dropdown.im;

import com.mojang.blaze3d.matrix.MatrixStack;
import dev.modules.api.Module;
import dev.settings.Setting;
import dev.settings.im.BooleanSetting;
import dev.ui.dropdown.components.BooleanComponent;
import dev.utils.font.Fonts;
import dev.utils.render.ColorUtils;
import dev.utils.render.RenderUtils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.util.math.vector.Vector4f;

public class ModuleButton {
    private int x, y, width, height;
    private Module module;
    private boolean expanded = false;
    private boolean wasRightClick;
    private final ObjectArrayList<Component> components = new ObjectArrayList<>();

    public ModuleButton(Module module) {
        this.module = module;
        for (Setting<?> setting : module.settings) {
            if (setting instanceof BooleanSetting bool) {
                components.add(new BooleanComponent(bool));
            }
        }
    }

    public void render(MatrixStack stack, int mouseX, int mouseY, boolean leftClick, boolean rightClick, boolean middleClick) {
        handleMouseClick(mouseX, mouseY, leftClick, rightClick);
        int backgroundColor = module.isEnabled() ? ColorUtils.hudColor2 : ColorUtils.hudColor1;

        RenderUtils.drawRoundedRect(x, y, width, height, new Vector4f(3, 3, 3, 3), backgroundColor);

        String moduleName = module.getName();
        int moduleNameWidth = (int) Fonts.lFBold[16].getWidth(moduleName);
        int moduleNameHeight = (int) Fonts.lFBold[16].getFontHeight();
        Fonts.lFBold[16].drawString(stack, moduleName, x + (width - moduleNameWidth) / 2, y + (height - moduleNameHeight) / 2 + 1.5, -1);

        if (expanded) {
            RenderUtils.drawRoundedRect(x, y + height, width, getComponentsHeight(), new Vector4f(3,3,3,3), ColorUtils.hudColor1);
            int componentY = y + height + 2;
            for (Component component : components) {
                if (component.isVisible()) {
                    component.setX(x);
                    component.setY(componentY);
                    component.setWidth(width);
                    component.render(stack, mouseX, mouseY);
                    componentY += component.getHeight() + 2;
                }
            }
        }
    }

    private int getComponentsHeight() {
        return components.stream()
                .filter(Component::isVisible)
                .mapToInt(component -> (int) component.getHeight())
                .sum() + components.size() * 2;
    }

    public void handleMouseClick(int mouseX, int mouseY, boolean leftClick, boolean rightClick) {
        boolean isHovered = mouseX > x && mouseX < x + width && mouseY > y && mouseY < y + height;

        if (isHovered && rightClick && !wasRightClick) {
            expanded = !expanded;
        }
        if (isHovered && leftClick) {
            module.setEnable(!module.isEnabled());
        }

        wasRightClick = rightClick;
    }

    public void setPosition(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public int getHeight() {
        return expanded ? height + getComponentsHeight() : height;
    }
}
 
Делаю логику что при правой кнопке мыши модуль открывается и показываются компоненти, но все равно при правом клике оно на момент открывается и сразу закрывается, уже перебровывал разные способы, даже чат гпт не помагает
Java:
Expand Collapse Copy
package dev.ui.dropdown.im;

import com.mojang.blaze3d.matrix.MatrixStack;
import dev.modules.api.Module;
import dev.settings.Setting;
import dev.settings.im.BooleanSetting;
import dev.ui.dropdown.components.BooleanComponent;
import dev.utils.font.Fonts;
import dev.utils.render.ColorUtils;
import dev.utils.render.RenderUtils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.util.math.vector.Vector4f;

public class ModuleButton {
    private int x, y, width, height;
    private Module module;
    private boolean expanded = false;
    private boolean wasRightClick;
    private final ObjectArrayList<Component> components = new ObjectArrayList<>();

    public ModuleButton(Module module) {
        this.module = module;
        for (Setting<?> setting : module.settings) {
            if (setting instanceof BooleanSetting bool) {
                components.add(new BooleanComponent(bool));
            }
        }
    }

    public void render(MatrixStack stack, int mouseX, int mouseY, boolean leftClick, boolean rightClick, boolean middleClick) {
        handleMouseClick(mouseX, mouseY, leftClick, rightClick);
        int backgroundColor = module.isEnabled() ? ColorUtils.hudColor2 : ColorUtils.hudColor1;

        RenderUtils.drawRoundedRect(x, y, width, height, new Vector4f(3, 3, 3, 3), backgroundColor);

        String moduleName = module.getName();
        int moduleNameWidth = (int) Fonts.lFBold[16].getWidth(moduleName);
        int moduleNameHeight = (int) Fonts.lFBold[16].getFontHeight();
        Fonts.lFBold[16].drawString(stack, moduleName, x + (width - moduleNameWidth) / 2, y + (height - moduleNameHeight) / 2 + 1.5, -1);

        if (expanded) {
            RenderUtils.drawRoundedRect(x, y + height, width, getComponentsHeight(), new Vector4f(3,3,3,3), ColorUtils.hudColor1);
            int componentY = y + height + 2;
            for (Component component : components) {
                if (component.isVisible()) {
                    component.setX(x);
                    component.setY(componentY);
                    component.setWidth(width);
                    component.render(stack, mouseX, mouseY);
                    componentY += component.getHeight() + 2;
                }
            }
        }
    }

    private int getComponentsHeight() {
        return components.stream()
                .filter(Component::isVisible)
                .mapToInt(component -> (int) component.getHeight())
                .sum() + components.size() * 2;
    }

    public void handleMouseClick(int mouseX, int mouseY, boolean leftClick, boolean rightClick) {
        boolean isHovered = mouseX > x && mouseX < x + width && mouseY > y && mouseY < y + height;

        if (isHovered && rightClick && !wasRightClick) {
            expanded = !expanded;
        }
        if (isHovered && leftClick) {
            module.setEnable(!module.isEnabled());
        }

        wasRightClick = rightClick;
    }

    public void setPosition(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public int getHeight() {
        return expanded ? height + getComponentsHeight() : height;
    }
}
на, пробуй это:

Код:
Expand Collapse Copy
public void handleMouseClick(int mouseX, int mouseY, boolean leftClick, boolean rightClick) {
    boolean isHovered = mouseX > x && mouseX < x + width && mouseY > y && mouseY < y + height;

    if (isHovered && rightClick && !wasRightClick) {
        expanded = !expanded;
        wasRightClick = true;
    } else if (!rightClick) {
        wasRightClick = false;
    }

    if (isHovered && leftClick) {
        module.setEnable(!module.isEnabled());
    }
}
 
на, пробуй это:

Код:
Expand Collapse Copy
public void handleMouseClick(int mouseX, int mouseY, boolean leftClick, boolean rightClick) {
    boolean isHovered = mouseX > x && mouseX < x + width && mouseY > y && mouseY < y + height;

    if (isHovered && rightClick && !wasRightClick) {
        expanded = !expanded;
        wasRightClick = true;
    } else if (!rightClick) {
        wasRightClick = false;
    }

    if (isHovered && leftClick) {
        module.setEnable(!module.isEnabled());
    }
}
не помагает :(
 
на, пробуй это:

Код:
Expand Collapse Copy
public void handleMouseClick(int mouseX, int mouseY, boolean leftClick, boolean rightClick) {
    boolean isHovered = mouseX > x && mouseX < x + width && mouseY > y && mouseY < y + height;

    if (isHovered && rightClick && !wasRightClick) {
        expanded = !expanded;
        wasRightClick = true;
    } else if (!rightClick) {
        wasRightClick = false;
    }

    if (isHovered && leftClick) {
        module.setEnable(!module.isEnabled());
    }
}
1733841637109.png
вот в дебаге, когда кликнул правой кнопкой мыши тут стопает, но все равно компонентов не видно, оно открывается и сразу закрывается
 

Вложения

  • 1733841568116.png
    1733841568116.png
    191.3 KB · Просмотры: 7
может кто то знает решение? до сих пор не смог решить
У тебя скорее всего expanded изменяется несколько раз, if(expanded) sout("test") если у тебя пишет постоянно то хуй знает если написало пару раз и перестало после открытия модуля, то смотри где ты изменяешь expanded ( ctrl зажми и левый клик мыши по названию) и меняй, скорее всего у тебя оно срабатывает несколько раз
 
У тебя скорее всего expanded изменяется несколько раз, if(expanded) sout("test") если у тебя пишет постоянно то хуй знает если написало пару раз и перестало после открытия модуля, то смотри где ты изменяешь expanded ( ctrl зажми и левый клик мыши по названию) и меняй, скорее всего у тебя оно срабатывает несколько раз
не в этом дело
А у тя handleMouseCLick() хукнут куда надо?
не уверен, подскажешь где и как?
 
Назад
Сверху Снизу