add config system and window dragging

This commit is contained in:
kawaiizenbo 2023-06-29 18:30:32 -07:00
parent 6e584de0e6
commit a18cb2a137
22 changed files with 354 additions and 104 deletions

View file

@ -5,56 +5,57 @@ import java.util.ArrayList;
import me.kawaiizenbo.moonlight.module.Category;
import me.kawaiizenbo.moonlight.module.ModuleManager;
import me.kawaiizenbo.moonlight.module.Module_;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
public class CategoryPane
{
private MinecraftClient mc = MinecraftClient.getInstance();
private TextRenderer textRenderer = mc.textRenderer;
public Category category;
public int x;
public int y;
private int height;
private int width = 96;
private boolean collapsed = false;
private ArrayList<ModuleButton> moduleButtons;
public int x, y, height, width = 96;
int startX, startY;
boolean dragging = false;
public boolean collapsed = false;
ArrayList<ModuleButton> moduleButtons;
public CategoryPane(Category category, int initialX, int initialY)
public CategoryPane(Category category, int initialX, int initialY, boolean collapsed)
{
this.category = category;
this.x = initialX;
this.y = initialY;
int buttonYOffset = y+16;
this.collapsed = collapsed;
moduleButtons = new ArrayList<ModuleButton>();
for (Module_ m : ModuleManager.INSTANCE.getModulesByCategory(category))
{
moduleButtons.add(new ModuleButton(m, x+2, buttonYOffset));
buttonYOffset += 12;
moduleButtons.add(new ModuleButton(m));
}
if (moduleButtons.size() == 0) collapsed = true;
height = (moduleButtons.size()*12) + 18;
}
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta, TextRenderer textRenderer)
{
if (dragging)
{
x = mouseX - startX;
y = mouseY - startY;
}
drawContext.fill(x, y, x+width, collapsed ? y+16 : y+height, category.color);
drawContext.fill(x+2, y+2, x+(width-2), y+14, hovered(mouseX, mouseY) ? 0xFF333333 : 0xFF222222);
drawContext.drawText(textRenderer, category.name, x+4, y+4, 0xFFFFFFFF, false);
if (!collapsed)
{
int buttonYOffset = y+16;
for (ModuleButton m : moduleButtons)
{
m.render(drawContext, mouseX, mouseY);
m.render(drawContext, mouseX, mouseY, x+2, buttonYOffset, textRenderer);
buttonYOffset += 12;
}
}
}
public boolean hovered(int mouseX, int mouseY)
{
return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
return mouseX >= x+2 && mouseX <= x+(width-2) && mouseY >= y+2 && mouseY <= y+14;
}
public void mouseClicked(int mouseX, int mouseY, int button)
@ -63,6 +64,17 @@ public class CategoryPane
{
if (moduleButton.mouseClicked(mouseX, mouseY, button)) return;
}
if (hovered(mouseX, mouseY)) collapsed = !collapsed;
if (hovered(mouseX, mouseY) && button == 1) collapsed = !collapsed;
else if (hovered(mouseX, mouseY) && button == 0)
{
startX = (int)mouseX-x;
startY = (int)mouseY-y;
dragging = true;
}
}
public void mouseReleased(int mouseX, int mouseY, int button)
{
dragging = false;
}
}

View file

@ -1,8 +1,11 @@
package me.kawaiizenbo.moonlight.ui.clickgui;
import java.util.ArrayList;
import java.util.Map;
import me.kawaiizenbo.moonlight.Moonlight;
import me.kawaiizenbo.moonlight.module.Category;
import me.kawaiizenbo.moonlight.util.MathUtils;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
@ -10,23 +13,19 @@ import net.minecraft.text.Text;
public class ClickGUIScreen extends Screen
{
public static ClickGUIScreen INSTANCE = new ClickGUIScreen();
public static ArrayList<CategoryPane> categoryPanes;
public ArrayList<CategoryPane> categoryPanes;
public ClickGUIScreen()
{
super(Text.literal("ClickGUI"));
int xOffset = 4;
int yOffset = 4;
categoryPanes = new ArrayList<CategoryPane>();
Map<String, Object> panePos = ((Map<String, Object>)Moonlight.INSTANCE.CONFIG.config.get("panes"));
for (Category category : Category.values())
{
if (xOffset > 400)
{
xOffset = 4;
yOffset = 128;
}
categoryPanes.add(new CategoryPane(category, xOffset, yOffset));
xOffset += 100;
int xOffset = MathUtils.d2iSafe(((Map<String, Object>)panePos.get(category.name)).get("x"));
int yOffset = MathUtils.d2iSafe(((Map<String, Object>)panePos.get(category.name)).get("y"));
boolean collapsed = (boolean)((Map<String, Object>)panePos.get(category.name)).get("collapsed");
categoryPanes.add(new CategoryPane(category, xOffset, yOffset, collapsed));
}
}
@ -36,7 +35,7 @@ public class ClickGUIScreen extends Screen
this.renderBackground(drawContext);
for (CategoryPane category : categoryPanes)
{
category.render(drawContext, mouseX, mouseY, delta);
category.render(drawContext, mouseX, mouseY, delta, textRenderer);
}
}
@ -49,6 +48,16 @@ public class ClickGUIScreen extends Screen
}
return super.mouseClicked(mouseX, mouseY, button);
}
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button)
{
for (CategoryPane category : categoryPanes)
{
category.mouseReleased((int) mouseX, (int) mouseY, button);
}
return super.mouseReleased(mouseX, mouseY, button);
}
@Override
public boolean shouldPause()

View file

@ -10,20 +10,18 @@ public class ModuleButton
{
public Module_ module;
public int x, y, width, height = 0;
private MinecraftClient mc = MinecraftClient.getInstance();
private TextRenderer textRenderer = mc.textRenderer;
public ModuleButton(Module_ module, int initialX, int initialY)
public ModuleButton(Module_ module)
{
this.module = module;
this.x = initialX;
this.y = initialY;
this.width = 92;
this.height = 12;
}
public void render(DrawContext drawContext, int mouseX, int mouseY)
public void render(DrawContext drawContext, int mouseX, int mouseY, int x, int y, TextRenderer textRenderer)
{
this.x = x;
this.y = y;
drawContext.fill(x, y, x + width, y + height, hovered(mouseX, mouseY) ? 0xFF333333 : 0xFF222222);
drawContext.drawText(textRenderer, module.name, x+2, y+2, module.enabled ? Moonlight.uiColor : 0xFFFFFF, false);
}

View file

@ -13,7 +13,10 @@ public class SettingsScreen extends Screen
private Module_ module;
private TextButton backButton;
protected SettingsScreen(Module_ module)
boolean dragging = false;
int startX, startY, x = 4, y = 4, windowWidth = 224, windowHeight = 192;
public SettingsScreen(Module_ module)
{
super(Text.literal("Settings"));
this.module = module;
@ -23,18 +26,32 @@ public class SettingsScreen extends Screen
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
{
this.renderBackground(drawContext);
drawContext.fill((width/2)-112, (height/2)-96, (width/2)+112, (height/2)+96, 0xFF222222);
drawContext.drawCenteredTextWithShadow(textRenderer, module.name, width/2, (height/2)-88, 0xFFFFFF);
drawContext.drawTextWithShadow(textRenderer, Text.literal(module.description), (width/2)-104, (height/2)-72, 0xFFFFFF);
backButton = new TextButton(ColorUtils.underline + "< Back", (width/2)-104, (height/2)-88, 0xFFFFFF);
// move window if dragging
if (dragging)
{
x = mouseX - startX;
y = mouseY - startY;
}
drawContext.fill(x, y, x+windowWidth, y+windowHeight, 0xFF222222);
drawContext.fill(x, y, x+windowWidth, y+16, module.category.color);
drawContext.fill(x+2, y+2, x+(windowWidth-2), y+14, 0xFF222222);
drawContext.drawCenteredTextWithShadow(textRenderer, "Module Settings: "+module.name, x+(windowWidth/2), y+4, 0xFFFFFF);
drawContext.drawText(textRenderer, module.description, x+8, y+24, 0xFFFFFF, true);
backButton = new TextButton(ColorUtils.underline + "< Back", x+4, y+4, 0xFFFFFF);
backButton.render(drawContext, textRenderer, mouseX, mouseY);
int yOffset = (height/2)-56;
int yOffset = y+40;
for (Setting setting : module.settings)
{
setting.render(drawContext, (width/2)-96, yOffset, mouseX, mouseY);
setting.render(drawContext, x+16, yOffset, mouseX, mouseY, textRenderer);
yOffset += setting.height + 1;
}
}
public boolean barHovered(int mouseX, int mouseY)
{
return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + 16;
}
@Override
public boolean shouldPause()
@ -45,6 +62,12 @@ public class SettingsScreen extends Screen
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button)
{
if (barHovered((int)mouseX, (int)mouseY))
{
startX = (int)mouseX-x;
startY = (int)mouseY-y;
dragging = true;
}
backButton.mouseClicked((int)mouseX, (int)mouseY);
for (Setting setting : module.settings)
{
@ -56,6 +79,7 @@ public class SettingsScreen extends Screen
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button)
{
dragging = false;
for (Setting setting : module.settings)
{
setting.mouseReleased(mouseX, mouseY, button);