拖动时,如何将矩形固定到左下角



我正在为一款名为Minecraft(Minecraft编码器包(的游戏制作pvp客户端,但我在使用ClickGUI时遇到了问题,该功能允许您在屏幕中间的窗口中启用其他功能。我有一个问题,当我拖动一个矩形时,它总是移动到左下角,就像这样:

https://streamable.com/qjbl71

我希望矩形在按下鼠标时保持原位,只在移动鼠标时移动。

我试过从categoryPositions.get(draggedCategory)的右边和// checking if any category is being moved, then replace category position的左边减去50这样的值,但这只会使矩形移动到中间而不是左边。

我发现了这个方法,这可能会有所帮助:

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;
}
}

我的drawScreen方法,每次勾选都执行:

@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++;
}
}

我没有从Gui创建矩形,而是使用Category和DraggableComponent方法创建了HashMap。然后我在drawScreen方法内部创建了for循环,以调用其DraggableComponent绘制方法。

// 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;
}

相关内容

  • 没有找到相关文章

最新更新