Skip to content

Commit

Permalink
Add basic dragging and expandablity in ClickGUI
Browse files Browse the repository at this point in the history
  • Loading branch information
TangyKiwi committed Jan 19, 2025
1 parent 94819be commit b61c86c
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@
import com.tangykiwi.kiwiclient.util.font.FontRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundEvents;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

import static com.tangykiwi.kiwiclient.KiwiClient.mc;

public class CategoryWindow {
public List<Module> moduleList = new ArrayList<Module>();

private int x, y;
private int width, height;
public int x, y;
public int width, height;
private Category category;
private String title;
private ItemStack icon;
Expand All @@ -32,6 +36,19 @@ public class CategoryWindow {
private FontRenderer fontRenderer;
private float fontHeight;

protected boolean dragging;
protected int dragOffX;
protected int dragOffY;

public int mouseX;
public int mouseY;

public int keyDown = -1;
public boolean lmDown = false;
public boolean rmDown = false;
public boolean lmHeld = false;
public int mwScroll = 0;

public CategoryWindow(int x, int y, int width, Category category, ItemStack icon) {
this.x = x;
this.y = y;
Expand All @@ -48,6 +65,11 @@ public CategoryWindow(int x, int y, int width, Category category, ItemStack icon
}

public void render(DrawContext context, int mouseX, int mouseY) {
if (dragging) {
x = Math.max(0, mouseX - dragOffX);
y = Math.max(0, mouseY - dragOffY);
}

int trueLen = (int) (expanded ? y + fontHeight + 1 /*+ getHeight()*/ : y + fontHeight + 1);

MatrixStack matrixStack = context.getMatrices();
Expand All @@ -63,7 +85,7 @@ public void render(DrawContext context, int mouseX, int mouseY) {
/* base title */
RenderUtils.drawRoundedQuadWH(matrixStack, new Color(0xffec8625), x + 1, y + 1, width - 2, fontHeight + 1, 5, 90);

fontRenderer.drawStringWithShadow(matrixStack, expanded ? "-" : "+", x + width - 10, y + (expanded ? 2 : 4), -1);
fontRenderer.drawStringWithShadow(matrixStack, expanded ? "-" : "+", x + width - 10, y + 2, -1);

boolean blockItem = icon != null && icon.getItem() instanceof BlockItem;

Expand All @@ -82,5 +104,40 @@ public void render(DrawContext context, int mouseX, int mouseY) {

/* window title */
fontRenderer.drawStringWithShadow(matrixStack, title, x + (icon == null || icon.getItem() == Items.AIR ? 4 : (blockItem ? 15 : 14)), y + 3, -1);

if (rmDown && mouseOver(x, y, x + width, y + 13)) {
mc.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
expanded = !expanded;
}
}

public void mouseClicked(double mouseX, double mouseY, int button) {
if (mouseX >= x && mouseX <= x + width - 2 && mouseY >= y && mouseY <= y + height + 11) {
dragging = true;
dragOffX = (int) mouseX - x;
dragOffY = (int) mouseY - y;
}
}

public void mouseReleased(double mouseX, double mouseY, int button) {
dragging = false;
}

public void keyPressed(int keyCode, int scanCode, int modifiers) {

}

public boolean mouseOver(int minX, int minY, int maxX, int maxY) {
return mouseX >= minX && mouseX <= maxX && mouseY >= minY && mouseY < maxY;
}

public void updateKeys(int mouseX, int mouseY, int keyDown, boolean lmDown, boolean rmDown, boolean lmHeld, int mwScroll) {
this.mouseX = mouseX;
this.mouseY = mouseY;
this.keyDown = keyDown;
this.lmDown = lmDown;
this.rmDown = rmDown;
this.lmHeld = lmHeld;
this.mwScroll = mwScroll;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public class ClickGUIScreen extends Base {

List<CategoryWindow> windows = new ArrayList<CategoryWindow>();

protected int keyDown = -1;
protected boolean lmDown = false;
protected boolean rmDown = false;
protected boolean lmHeld = false;
protected int mwhScroll = 0;
protected int mwvScroll = 0;

public ClickGUIScreen() {
super(Text.literal("ClickGUI"));
}
Expand Down Expand Up @@ -42,10 +49,64 @@ public boolean shouldPause() {

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
for (CategoryWindow window : windows) {
window.updateKeys(mouseX, mouseY, keyDown, lmDown, rmDown, lmHeld, mwvScroll);
}

super.render(context, mouseX, mouseY, delta);

for (CategoryWindow window : windows) {
window.render(context, mouseX, mouseY);
}

lmDown = false;
rmDown = false;
keyDown = -1;
mwhScroll = 0;
mwvScroll = 0;
}

public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0) {
lmDown = true;
lmHeld = true;
} else if (button == 1) {
rmDown = true;
}

for (CategoryWindow window : windows) {
if (mouseX > window.x && mouseX < window.x + window.width && mouseY > window.y && mouseY < window.y + window.height) {
window.mouseClicked(mouseX, mouseY, button);
break;
}
}

return super.mouseClicked(mouseX, mouseY, button);
}

public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (button == 0) lmHeld = false;

for (CategoryWindow window : windows) {
window.mouseReleased(mouseX, mouseY, button);
}

return super.mouseReleased(mouseX, mouseY, button);
}

public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
keyDown = keyCode;

for (CategoryWindow window : windows) {
window.keyPressed(keyCode, scanCode, modifiers);
}

return super.keyPressed(keyCode, scanCode, modifiers);
}

public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
mwhScroll = (int) horizontalAmount;
mwvScroll = (int) verticalAmount;
return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
}
}

0 comments on commit b61c86c

Please sign in to comment.