functional scrolling and functional mod menu
This commit is contained in:
@@ -2,21 +2,18 @@ package org.saturnclient.ui2;
|
||||
|
||||
public class RenderContext {
|
||||
public int mouseX, mouseY;
|
||||
public int elementX, elementY;
|
||||
public int elementWidth, elementHeight;
|
||||
public float elementScale;
|
||||
|
||||
public RenderContext(int mouseX, int mouseY, Element e) {
|
||||
this.mouseX = mouseX;
|
||||
this.mouseY = mouseY;
|
||||
this.elementX = e.x;
|
||||
this.elementY = e.y;
|
||||
this.mouseX = mouseX - e.x;
|
||||
this.mouseY = mouseY - e.y;
|
||||
this.elementWidth = e.width;
|
||||
this.elementHeight = e.height;
|
||||
this.elementScale = e.scale;
|
||||
}
|
||||
|
||||
public boolean isHovering() {
|
||||
return Utils.isHovering(mouseX - elementX, mouseY - elementY, elementWidth, elementHeight, elementScale);
|
||||
return Utils.isHovering(mouseX , mouseY, elementWidth, elementHeight, elementScale);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.saturnclient.saturnclient.mixin.DrawContextAccessor;
|
||||
import org.saturnclient.ui2.anim.Animation;
|
||||
import org.saturnclient.ui2.components.ElementRenderer;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gl.PostEffectProcessor;
|
||||
import net.minecraft.client.gui.CubeMapRenderer;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
@@ -100,6 +101,9 @@ public abstract class SaturnScreen extends Screen {
|
||||
|
||||
@Override
|
||||
public boolean mouseClicked(double mouseX, double mouseY, int button) {
|
||||
mouseX *= 2;
|
||||
mouseY *= 2;
|
||||
|
||||
ElementRenderer.mouseClicked(elements, mouseX, mouseY, button);
|
||||
|
||||
return false;
|
||||
@@ -111,6 +115,9 @@ public abstract class SaturnScreen extends Screen {
|
||||
double mouseY,
|
||||
double horizontalAmount,
|
||||
double verticalAmount) {
|
||||
mouseX *= 2;
|
||||
mouseY *= 2;
|
||||
|
||||
ElementRenderer.mouseScrolled(elements, mouseX, mouseY, horizontalAmount, verticalAmount);
|
||||
|
||||
return super.mouseScrolled(
|
||||
@@ -119,4 +126,10 @@ public abstract class SaturnScreen extends Screen {
|
||||
horizontalAmount,
|
||||
verticalAmount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(MinecraftClient client, int width, int height) {
|
||||
elements.clear();
|
||||
super.resize(client, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,14 +17,10 @@ public abstract class Animation {
|
||||
|
||||
public static void execute(Consumer<Float> tick, int duration) {
|
||||
new Thread(() -> {
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
Function<Double, Double> curveFunction = Curve::easeInOutCubic;
|
||||
float progress = 0.0f;
|
||||
double incremental = (1000 - duration) / 10000.0;
|
||||
|
||||
System.out.println("Starting animation...");
|
||||
|
||||
for (int i = 0; i <= 100; i++) {
|
||||
float newProgress = (float) (double) curveFunction.apply(i / 100.0);
|
||||
|
||||
@@ -46,8 +42,6 @@ public abstract class Animation {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Animation complete. " + (System.currentTimeMillis() - startTime) + "MS");
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,6 @@ public class ElementRenderer {
|
||||
}
|
||||
|
||||
public static void mouseClicked(List<Element> elements, double mouseX, double mouseY, int button) {
|
||||
mouseX *= 2;
|
||||
mouseY *= 2;
|
||||
if (button == 0) {
|
||||
for (Element element : new ArrayList<>(elements)) {
|
||||
element.focused = false;
|
||||
@@ -52,7 +50,7 @@ public class ElementRenderer {
|
||||
|
||||
if (Utils.isHovering(adjustedMouseX, adjustedMouseY, element.width, element.height, element.scale)) {
|
||||
element.focused = true;
|
||||
element.click(adjustedMouseX, adjustedMouseY);
|
||||
element.click((int) (adjustedMouseX * element.scale), (int) (adjustedMouseY * element.scale));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,17 +63,10 @@ public class ElementRenderer {
|
||||
double horizontalAmount,
|
||||
double verticalAmount) {
|
||||
for (Element element : new ArrayList<>(elements)) {
|
||||
double adjustedMouseX = mouseX - element.x;
|
||||
double adjustedMouseY = mouseY - element.y;
|
||||
boolean isMouseInside = adjustedMouseX >= 0 &&
|
||||
adjustedMouseX <= (element.width * element.scale) &&
|
||||
adjustedMouseY >= 0 &&
|
||||
adjustedMouseY <= (element.height * element.scale);
|
||||
|
||||
if (isMouseInside) {
|
||||
if (Utils.isHovering((int) mouseX - element.x, (int) mouseY - element.y, element.width, element.height, element.scale)) {
|
||||
element.scroll(
|
||||
(int) (adjustedMouseX / element.scale),
|
||||
(int) (adjustedMouseY / element.scale),
|
||||
(int) mouseX - element.x,
|
||||
(int) mouseY - element.y,
|
||||
horizontalAmount,
|
||||
verticalAmount);
|
||||
}
|
||||
|
||||
@@ -3,34 +3,59 @@ package org.saturnclient.ui2.elements;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.saturnclient.saturnclient.config.Property;
|
||||
import org.saturnclient.saturnclient.config.ThemeManager;
|
||||
import org.saturnclient.ui2.Element;
|
||||
import org.saturnclient.ui2.RenderContext;
|
||||
import org.saturnclient.ui2.RenderScope;
|
||||
import org.saturnclient.ui2.components.ElementRenderer;
|
||||
|
||||
public class Scroll extends Element {
|
||||
private static ThemeManager theme = new ThemeManager("Scroll");
|
||||
private static Property<Integer> bgColor = theme.property("bg-color", new Property<Integer>(0x90000000));
|
||||
private static Property<Integer> scrollBarColor = theme.property("scrollbar-color", new Property<Integer>(0xFFFF0000));
|
||||
|
||||
private static Property<Integer> scrollBarRadius = theme.property("scrollbar-radius", new Property<Integer>(10));
|
||||
private static Property<Integer> scrollBarWidth = theme.property("scrollbar-radius", new Property<Integer>(10));
|
||||
private static Property<Integer> scrollBarPadding = theme.property("scrollbar-padding", new Property<Integer>(5));
|
||||
private static Property<Integer> cornerRadius = theme.property("corner-radius", new Property<Integer>(10));
|
||||
private static Property<Integer> padding = theme.property("padding", new Property<Integer>(10));
|
||||
|
||||
public static Property<Integer> gap = theme.property("gap", new Property<Integer>(10));
|
||||
|
||||
protected List<Element> children = new ArrayList<>();
|
||||
int scroll = 0;
|
||||
int maxScroll = 0;
|
||||
|
||||
@Override
|
||||
public void render(RenderScope renderScope, RenderContext ctx) {
|
||||
renderScope.drawRoundedRectangle(0, 0, width, height, cornerRadius.value, bgColor.value);
|
||||
|
||||
renderScope.enableScissor(padding.value, padding.value, width - padding.value, height - padding.value);
|
||||
renderScope.matrices.push();
|
||||
renderScope.matrices.translate(0, -scroll, 0);
|
||||
ElementRenderer.render(children, renderScope, ctx.mouseX, ctx.mouseY);
|
||||
renderScope.matrices.translate(padding.value, (-scroll) + padding.value, 0);
|
||||
ElementRenderer.render(children, renderScope, ctx.mouseX - padding.value, ctx.mouseY - padding.value + scroll);
|
||||
renderScope.matrices.pop();
|
||||
// renderScope.drawRoundedRectangle(width - 10, 0, 10, 50, 0, Button.fgColor.value);
|
||||
renderScope.disableScissor();
|
||||
|
||||
renderScope.drawRoundedRectangle(width - scrollBarWidth.value - scrollBarPadding.value, calculateScrollBarY(), scrollBarWidth.value, calculateScrollBarHeight(), scrollBarRadius.value, scrollBarColor.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scroll(int mouseX, int mouseY, double horizontalAmount, double verticalAmount) {
|
||||
if (scroll - verticalAmount <= 0) {
|
||||
scroll -= verticalAmount;
|
||||
if (scroll < 0) {
|
||||
scroll = 0;
|
||||
} else if (scroll - verticalAmount <= maxScroll) {
|
||||
scroll -= verticalAmount;
|
||||
} else if (scroll > maxScroll) {
|
||||
scroll = maxScroll;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click(int mouseX, int mouseY) {
|
||||
ElementRenderer.mouseClicked(children, mouseX - padding.value, mouseY - padding.value + scroll, 0);
|
||||
}
|
||||
|
||||
public void draw(Element element) {
|
||||
children.add(element);
|
||||
|
||||
@@ -42,6 +67,17 @@ public class Scroll extends Element {
|
||||
height = element.height;
|
||||
}
|
||||
|
||||
maxScroll = Math.max(maxScroll, element.y - height);
|
||||
maxScroll = Math.max(maxScroll, element.y - maxScroll);
|
||||
}
|
||||
|
||||
int calculateScrollBarHeight() {
|
||||
if (maxScroll <= 0) return height; // No scrolling needed
|
||||
return Math.max(20, (height * height) / (height + maxScroll)) - (scrollBarPadding.value * 2); // At least 20px tall
|
||||
}
|
||||
|
||||
int calculateScrollBarY() {
|
||||
int scrollBarHeight = calculateScrollBarHeight();
|
||||
if (maxScroll <= 0) return 0;
|
||||
return (scroll * (height - scrollBarHeight)) / maxScroll + scrollBarPadding.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.saturnclient.ui2.screens;
|
||||
|
||||
import org.saturnclient.modules.ModManager;
|
||||
import org.saturnclient.modules.Module;
|
||||
import org.saturnclient.saturnclient.SaturnClient;
|
||||
import org.saturnclient.saturnclient.menus.ConfigEditor;
|
||||
import org.saturnclient.ui2.SaturnScreen;
|
||||
import org.saturnclient.ui2.elements.Button;
|
||||
import org.saturnclient.ui2.elements.Scroll;
|
||||
@@ -14,22 +17,22 @@ public class ModMenu extends SaturnScreen {
|
||||
public void ui() {
|
||||
Scroll scroll = new Scroll();
|
||||
|
||||
scroll.draw(new Button("Hello World! 1", () -> {
|
||||
SaturnClient.LOGGER.info("Clicked Hello World!");
|
||||
}).position(0, 0));
|
||||
int row = 0;
|
||||
int col = 0;
|
||||
|
||||
scroll.draw(new Button("Hello World! 1", () -> {
|
||||
SaturnClient.LOGGER.info("Clicked Hello World!");
|
||||
}).position(0, 50));
|
||||
for (Module mod : ModManager.MODS) {
|
||||
scroll.draw(new Button(mod.getName(), () -> {
|
||||
SaturnClient.client.setScreen(new ConfigEditor(mod.getConfig()));
|
||||
}).dimensions(100, 50).position((100 + Scroll.gap.value) * col, (50 + Scroll.gap.value) * row));
|
||||
|
||||
scroll.draw(new Button("Hello World! 2", () -> {
|
||||
SaturnClient.LOGGER.info("Clicked Hello World!");
|
||||
}).position(0, 100));
|
||||
|
||||
scroll.draw(new Button("Hello World! 3", () -> {
|
||||
SaturnClient.LOGGER.info("Clicked Hello World!");
|
||||
}).position(0, 150));
|
||||
|
||||
draw(scroll.dimensions(width, 100));
|
||||
if (col == 3) {
|
||||
col = 0;
|
||||
row++;
|
||||
} else {
|
||||
col++;
|
||||
}
|
||||
}
|
||||
|
||||
draw(scroll.dimensions(400, 250).center(width, height));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user