From 775bdf264c1127e24d6594ec16b18d670992160d Mon Sep 17 00:00:00 2001 From: The Panda Oliver Date: Sun, 22 Dec 2024 16:49:49 +0100 Subject: [PATCH] Remove deprecated UI component classes. Deleted obsolete and unused UI component classes, including `TextUIComponent`, `VanillaUIComponent`, `StackLayout`, and others, to streamline the codebase. These classes were no longer necessary as part of a planned refactor or redesign. --- .../main/java/me/pandamods/test/TestMod.java | 21 --- .../test/client/screen/TestScreen.java | 49 ------ .../java/me/pandamods/pandalib/PandaLib.java | 2 + .../pandalib/client/screen/BasePLScreen.java | 89 ----------- .../client/screen/BaseParentUIComponent.java | 67 -------- .../client/screen/BaseUIComponent.java | 84 ---------- .../screen/components/TextUIComponent.java | 80 ---------- .../screen/components/VanillaUIComponent.java | 145 ------------------ .../client/screen/containers/StackLayout.java | 109 ------------- .../client/screen/containers/UIContainer.java | 40 ----- .../client/screen/core/ParentUIComponent.java | 83 ---------- .../client/screen/core/UIComponent.java | 101 ------------ .../client/screen/utils/FocusHandler.java | 41 ----- .../client/screen/utils/RenderContext.java | 39 ----- .../pandalib/utils/NetworkHelper.java | 26 ---- .../pandalib/utils/animation/Easing.java | 113 ++++++++++++++ 16 files changed, 115 insertions(+), 974 deletions(-) delete mode 100644 common-testmod/src/main/java/me/pandamods/test/client/screen/TestScreen.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/BasePLScreen.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/BaseParentUIComponent.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/BaseUIComponent.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/components/TextUIComponent.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/components/VanillaUIComponent.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/containers/StackLayout.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/containers/UIContainer.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/core/ParentUIComponent.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/core/UIComponent.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/utils/FocusHandler.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/client/screen/utils/RenderContext.java delete mode 100644 common/src/main/java/me/pandamods/pandalib/utils/NetworkHelper.java create mode 100644 common/src/main/java/me/pandamods/pandalib/utils/animation/Easing.java diff --git a/common-testmod/src/main/java/me/pandamods/test/TestMod.java b/common-testmod/src/main/java/me/pandamods/test/TestMod.java index 15dc0968..d534a3ef 100644 --- a/common-testmod/src/main/java/me/pandamods/test/TestMod.java +++ b/common-testmod/src/main/java/me/pandamods/test/TestMod.java @@ -12,17 +12,11 @@ package me.pandamods.test; -import com.mojang.blaze3d.platform.InputConstants; -import dev.architectury.event.EventResult; -import dev.architectury.event.events.client.ClientScreenInputEvent; -import dev.architectury.registry.client.keymappings.KeyMappingRegistry; import me.pandamods.pandalib.config.PandaLibConfig; import me.pandamods.pandalib.config.holders.ClientConfigHolder; import me.pandamods.pandalib.config.holders.CommonConfigHolder; -import me.pandamods.test.client.screen.TestScreen; import me.pandamods.test.config.ClientTestConfig; import me.pandamods.test.config.CommonTestConfig; -import net.minecraft.client.KeyMapping; import net.minecraft.resources.ResourceLocation; public class TestMod { @@ -32,23 +26,8 @@ public class TestMod { private static final CommonConfigHolder COMMON_TEST_CONFIG = PandaLibConfig.registerCommon(CommonTestConfig.class); private static final ClientConfigHolder CLIENT_TEST_CONFIG = PandaLibConfig.registerClient(ClientTestConfig.class); - public static final KeyMapping TEST_SCREEN_KEYMAPPING = new KeyMapping( - "key.testmod.test_screen", - InputConstants.Type.KEYSYM, - InputConstants.KEY_M, - "key.categories.testmod" - ); - public TestMod() { instance = this; - - KeyMappingRegistry.register(TEST_SCREEN_KEYMAPPING); - ClientScreenInputEvent.KEY_PRESSED_POST.register((minecraft, screen, i, i1, i2) -> { - if (TEST_SCREEN_KEYMAPPING.matches(i, i1)) { - minecraft.setScreen(new TestScreen()); - } - return EventResult.pass(); - }); } public static ResourceLocation resourceLocation(String path) { diff --git a/common-testmod/src/main/java/me/pandamods/test/client/screen/TestScreen.java b/common-testmod/src/main/java/me/pandamods/test/client/screen/TestScreen.java deleted file mode 100644 index 9df715c0..00000000 --- a/common-testmod/src/main/java/me/pandamods/test/client/screen/TestScreen.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.test.client.screen; - -import me.pandamods.pandalib.client.screen.BasePLScreen; -import me.pandamods.pandalib.client.screen.components.TextUIComponent; -import me.pandamods.pandalib.client.screen.components.VanillaUIComponent; -import me.pandamods.pandalib.client.screen.containers.StackLayout; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.components.Button; -import net.minecraft.client.gui.components.EditBox; -import net.minecraft.network.chat.Component; - -import java.util.function.Supplier; - -public class TestScreen extends BasePLScreen { - @Override - protected void build(StackLayout rootComponent) { - VanillaUIComponent vanillaTestButton = VanillaUIComponent.of(Button.builder(Component.literal("Test Vanilla Button"), - button -> System.out.println("Test click")) - .width(200) - .build()); - vanillaTestButton.mount(rootComponent); - - EditBox editBox = new EditBox(Minecraft.getInstance().font, 0, 0, 200, 20, Component.literal("Test TextField")); - VanillaUIComponent vanillaTestTextField = VanillaUIComponent.of(editBox); - vanillaTestTextField.mount(rootComponent); - - TextUIComponent textUIComponent = new TextUIComponent(Minecraft.getInstance().font, Component.literal("Test Text")); - textUIComponent.mount(rootComponent); - } - - @Override - protected StackLayout createRootComponent() { - StackLayout stack = StackLayout.createVerticalLayout(); - stack.setGapSize(4); - return stack; - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/PandaLib.java b/common/src/main/java/me/pandamods/pandalib/PandaLib.java index 8d6f4de9..316df5be 100644 --- a/common/src/main/java/me/pandamods/pandalib/PandaLib.java +++ b/common/src/main/java/me/pandamods/pandalib/PandaLib.java @@ -22,6 +22,8 @@ import me.pandamods.pandalib.platform.Services; import net.minecraft.resources.ResourceLocation; +import java.awt.*; + public class PandaLib { public static final String MOD_ID = "pandalib"; private static PandaLib instance; diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/BasePLScreen.java b/common/src/main/java/me/pandamods/pandalib/client/screen/BasePLScreen.java deleted file mode 100644 index 33fac106..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/BasePLScreen.java +++ /dev/null @@ -1,89 +0,0 @@ -package me.pandamods.pandalib.client.screen; - -import me.pandamods.pandalib.client.screen.core.ParentUIComponent; -import me.pandamods.pandalib.client.screen.utils.FocusHandler; -import me.pandamods.pandalib.client.screen.utils.RenderContext; -import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.network.chat.Component; - -import java.util.function.Supplier; - -public abstract class BasePLScreen extends Screen { - protected T rootComponent; - - protected BasePLScreen(Component title) { - super(title); - } - - protected BasePLScreen() { - this(Component.empty()); - } - - protected abstract void build(T rootComponent); - - protected abstract T createRootComponent(); - - @Override - protected void init() { - if (rootComponent == null) { - this.rootComponent = createRootComponent(); - build(this.rootComponent); - } - - this.rootComponent.mount(null); - this.rootComponent.position(0, 0); - this.rootComponent.size(this.width, this.height); - } - - @Override - public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) { - super.render(guiGraphics, mouseX, mouseY, partialTick); - this.rootComponent.render(new RenderContext(guiGraphics), mouseX, mouseY, partialTick); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (this.rootComponent.mousePressed(mouseX, mouseY, button)) return true; - return super.mouseClicked(mouseX, mouseY, button); - } - - @Override - public boolean mouseReleased(double mouseX, double mouseY, int button) { - if (this.rootComponent.mouseReleased(mouseX, mouseY, button)) return true; - return super.mouseReleased(mouseX, mouseY, button); - } - - @Override - public boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) { - if (this.rootComponent.mouseScrolled(mouseX, mouseY, scrollX, scrollY)) return true; - return super.mouseScrolled(mouseX, mouseY, scrollX, scrollY); - } - - @Override - public void mouseMoved(double mouseX, double mouseY) { - this.rootComponent.mouseMoved(mouseX, mouseY); - super.mouseMoved(mouseX, mouseY); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - FocusHandler focusHandler = this.rootComponent.getFocusHandler(); - if (focusHandler.getFocused() != null && focusHandler.getFocused().keyPressed(keyCode, scanCode, modifiers)) return true; - return super.keyPressed(keyCode, scanCode, modifiers); - } - - @Override - public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - FocusHandler focusHandler = this.rootComponent.getFocusHandler(); - if (focusHandler.getFocused() != null && focusHandler.getFocused().keyReleased(keyCode, scanCode, modifiers)) return true; - return super.keyReleased(keyCode, scanCode, modifiers); - } - - @Override - public boolean charTyped(char codePoint, int modifiers) { - FocusHandler focusHandler = this.rootComponent.getFocusHandler(); - if (focusHandler.getFocused() != null && focusHandler.getFocused().charTyped(codePoint, modifiers)) return true; - return super.charTyped(codePoint, modifiers); - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/BaseParentUIComponent.java b/common/src/main/java/me/pandamods/pandalib/client/screen/BaseParentUIComponent.java deleted file mode 100644 index bb48b0d6..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/BaseParentUIComponent.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen; - -import me.pandamods.pandalib.client.screen.core.ParentUIComponent; -import me.pandamods.pandalib.client.screen.core.UIComponent; -import me.pandamods.pandalib.client.screen.utils.FocusHandler; -import me.pandamods.pandalib.client.screen.utils.RenderContext; - -public abstract class BaseParentUIComponent extends BaseUIComponent implements ParentUIComponent { - protected FocusHandler focusHandler; - - @Override - public void render(RenderContext context, int mouseX, int mouseY, float partialTicks) { - Runnable renderChildren = () -> renderChildren(context, mouseX, mouseY, partialTicks); - if (this.isOverflowAllowed()) { - renderChildren.run(); - }else { - context.scissor(this.getX(), this.getY(), this.getX() + this.getWidth(), this.getY() + this.getHeight(), renderChildren); - } - } - - protected void renderChildren(RenderContext context, int mouseX, int mouseY, float partialTicks) { - for (UIComponent child : this.getChildren()) { - renderChild(child, context, mouseX, mouseY, partialTicks); - } - } - - public void renderChild(UIComponent child, RenderContext context, int mouseX, int mouseY, float partialTicks) { - child.render(context, mouseX, mouseY, partialTicks); - } - - @Override - public void updateChildState(UIComponent uiComponent) { - if (uiComponent.getParent() == this) { - if (!getChildren().contains(uiComponent)) { - addChild(uiComponent); - } - } else { - removeChild(uiComponent); - } - } - - protected abstract void addChild(UIComponent UIComponent); - protected abstract void removeChild(UIComponent UIComponent); - - @Override - public void mount(ParentUIComponent parent) { - super.mount(parent); - this.focusHandler = parent != null ? parent.getFocusHandler() : new FocusHandler(); - } - - @Override - public FocusHandler getFocusHandler() { - return this.focusHandler; - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/BaseUIComponent.java b/common/src/main/java/me/pandamods/pandalib/client/screen/BaseUIComponent.java deleted file mode 100644 index 47109a70..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/BaseUIComponent.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen; - -import me.pandamods.pandalib.client.screen.core.ParentUIComponent; -import me.pandamods.pandalib.client.screen.core.UIComponent; -import org.jetbrains.annotations.Nullable; - -public abstract class BaseUIComponent implements UIComponent { - protected ParentUIComponent parent = null; - - protected int x, y; - protected int width, height; - - @Override - public ParentUIComponent getParent() { - return parent; - } - - @Override - public void mount(ParentUIComponent parent) { - ParentUIComponent oldParent = this.parent; - this.parent = parent; - if (parent != null) - parent.updateChildState(this); - if (oldParent != null) - oldParent.updateChildState(this); - } - - @Override - public void dismount() { - mount(null); - } - - @Override - public void setX(int x) { - this.x = x; - } - - @Override - public void setY(int y) { - this.y = y; - } - - @Override - public void setWidth(int width) { - this.width = width; - } - - @Override - public void setHeight(int height) { - this.height = height; - } - - @Override - public int getX() { - return this.x; - } - - @Override - public int getY() { - return this.y; - } - - @Override - public int getWidth() { - return this.width; - } - - @Override - public int getHeight() { - return this.height; - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/components/TextUIComponent.java b/common/src/main/java/me/pandamods/pandalib/client/screen/components/TextUIComponent.java deleted file mode 100644 index cb58c5a1..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/components/TextUIComponent.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.components; - -import me.pandamods.pandalib.client.screen.BaseUIComponent; -import me.pandamods.pandalib.client.screen.utils.RenderContext; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.Font; -import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.network.chat.Component; -import org.jetbrains.annotations.NotNull; - -import java.awt.*; - -public class TextUIComponent extends BaseUIComponent { - private Font font; - private Component text; - - private Color color = Color.WHITE; - - public TextUIComponent(Font font, Component text) { - this.font = font; - this.text = text; - - this.setHeight(font.lineHeight); - this.setWidth(font.width(text)); - } - - public TextUIComponent(Font font) { - this(font, Component.empty()); - } - - public TextUIComponent() { - this(Minecraft.getInstance().font, Component.empty()); - } - - @Override - public void render(RenderContext context, int mouseX, int mouseY, float partialTicks) { - GuiGraphics graphics = context.guiGraphics; - graphics.drawString(this.font, this.text, this.x, this.y, color.getRGB()); - } - - public void setText(@NotNull String text) { - this.text = Component.literal(text); - } - - public void setText(@NotNull Component text) { - this.text = text; - } - - public Component getText() { - return text; - } - - public void setFont(Font font) { - this.font = font; - } - - public Font getFont() { - return font; - } - - public void setColor(Color color) { - this.color = color; - } - - public Color getColor() { - return color; - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/components/VanillaUIComponent.java b/common/src/main/java/me/pandamods/pandalib/client/screen/components/VanillaUIComponent.java deleted file mode 100644 index c39189f6..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/components/VanillaUIComponent.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.components; - -import me.pandamods.pandalib.client.screen.BaseUIComponent; -import me.pandamods.pandalib.client.screen.core.ParentUIComponent; -import me.pandamods.pandalib.client.screen.utils.RenderContext; -import me.pandamods.pandalib.mixin.accessor.AbstractWidgetAccessor; -import net.minecraft.client.gui.components.AbstractWidget; - -public class VanillaUIComponent extends BaseUIComponent { - private final AbstractWidget widget; - - private boolean dragging = false; - private int draggingButton = 0; - private double lastMouseX, lastMouseY; - - protected VanillaUIComponent(AbstractWidget widget) { - this.widget = widget; - this.x = widget.getX(); - this.y = widget.getY(); - this.width = widget.getWidth(); - this.height = widget.getHeight(); - } - - public static VanillaUIComponent of(AbstractWidget widget) { - return new VanillaUIComponent(widget); - } - - @Override - public void mount(ParentUIComponent parent) { - super.mount(parent); - this.applyToWidget(); - } - - @Override - public void dismount() { - super.dismount(); - this.applyToWidget(); - } - - @Override - public void setX(int x) { - super.setX(x); - applyToWidget(); - } - - @Override - public void setY(int y) { - super.setY(y); - applyToWidget(); - } - - @Override - public void setWidth(int width) { - super.setWidth(width); - applyToWidget(); - } - - @Override - public void setHeight(int height) { - super.setHeight(height); - applyToWidget(); - } - - public void applyToWidget() { - AbstractWidgetAccessor accessor = (AbstractWidgetAccessor) this.widget; - accessor.pandaLib$setX(this.x); - accessor.pandaLib$setY(this.y); - accessor.pandaLib$setWidth(this.width); - accessor.pandaLib$setHeight(this.height); - } - - @Override - public void render(RenderContext context, int mouseX, int mouseY, float partialTicks) { - widget.render(context.guiGraphics, mouseX, mouseY, partialTicks); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - return this.widget.keyPressed(keyCode, scanCode, modifiers); - } - - @Override - public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - return this.widget.keyReleased(keyCode, scanCode, modifiers); - } - - @Override - public boolean charTyped(char codePoint, int modifiers) { - return this.widget.charTyped(codePoint, modifiers); - } - - @Override - public boolean mousePressed(double mouseX, double mouseY, int button) { - if (!dragging) { - dragging = true; - draggingButton = button; - } - return this.widget.mouseClicked(mouseX, mouseY, button); - } - - @Override - public boolean mouseReleased(double mouseX, double mouseY, int button) { - dragging = false; - return this.widget.mouseReleased(mouseX, mouseY, button); - } - - @Override - public boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) { - return this.widget.mouseScrolled(mouseX, mouseY, scrollX, scrollY); - } - - @Override - public void mouseMoved(double mouseX, double mouseY) { - this.widget.mouseMoved(mouseX, mouseY); - if (dragging) { - double dragX = mouseX - lastMouseX; - double dragY = mouseY - lastMouseY; - this.widget.mouseDragged(mouseX, mouseY, draggingButton, dragX, dragY); - } - this.lastMouseX = mouseX; - this.lastMouseY = mouseY; - } - - @Override - public void onFocusGained() { - this.widget.setFocused(true); - } - - @Override - public void onFocusLost() { - this.widget.setFocused(false); - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/containers/StackLayout.java b/common/src/main/java/me/pandamods/pandalib/client/screen/containers/StackLayout.java deleted file mode 100644 index 25ee215e..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/containers/StackLayout.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.containers; - -import me.pandamods.pandalib.client.screen.core.UIComponent; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.function.Consumer; -import java.util.function.Supplier; - -public class StackLayout extends UIContainer { - protected Direction direction; - protected int gapSize = 0; - - private int contentWidth = 0; - private int contentHeight = 0; - - public static StackLayout createHorizontalLayout() { - return new StackLayout(Direction.HORIZONTAL); - } - - public static StackLayout createVerticalLayout() { - return new StackLayout(Direction.VERTICAL); - } - - public StackLayout(Direction direction) { - this.direction = direction; - } - - @Override - public void updateChildState(UIComponent uiComponent) { - super.updateChildState(uiComponent); - align(); - } - - public void setDirection(Direction direction) { - this.direction = direction; - this.align(); - } - - public Direction getDirection() { - return direction; - } - - public void setGapSize(int gapSize) { - this.gapSize = gapSize; - } - - public int getGapSize() { - return gapSize; - } - - public void align() { - int contentLength = 0; - int contentSize = 0; - - Iterator iterator = this.children.iterator(); - while (iterator.hasNext()) { - UIComponent child = iterator.next(); - Consumer setPos = this.direction == Direction.HORIZONTAL ? child::setX : child::setY; - Supplier getLength = this.direction == Direction.HORIZONTAL ? child::getWidth : child::getHeight; - Supplier getSize = this.direction == Direction.HORIZONTAL ? child::getHeight : child::getWidth; - - setPos.accept(contentLength); - contentLength += getLength.get(); - if (iterator.hasNext()) { - contentLength += gapSize; - } - contentSize = Math.max(contentSize, getSize.get()); - } - - switch (this.direction) { - case HORIZONTAL -> { - contentWidth = contentLength; - contentHeight = contentSize; - } - case VERTICAL -> { - contentHeight = contentLength; - contentWidth = contentSize; - } - } - } - - public int getContentWidth() { - return contentWidth; - } - - public int getContentHeight() { - return contentHeight; - } - - public enum Direction { - HORIZONTAL, - VERTICAL - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/containers/UIContainer.java b/common/src/main/java/me/pandamods/pandalib/client/screen/containers/UIContainer.java deleted file mode 100644 index e555b33a..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/containers/UIContainer.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.containers; - -import me.pandamods.pandalib.client.screen.BaseParentUIComponent; -import me.pandamods.pandalib.client.screen.core.UIComponent; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class UIContainer extends BaseParentUIComponent { - protected final List children = new ArrayList<>(); - protected final List viewChildren = Collections.unmodifiableList(children); - - @Override - public List getChildren() { - return viewChildren; - } - - @Override - protected void addChild(UIComponent UIComponent) { - children.add(UIComponent); - } - - @Override - protected void removeChild(UIComponent UIComponent) { - children.remove(UIComponent); - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/core/ParentUIComponent.java b/common/src/main/java/me/pandamods/pandalib/client/screen/core/ParentUIComponent.java deleted file mode 100644 index 670526a3..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/core/ParentUIComponent.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.core; - -import me.pandamods.pandalib.client.screen.utils.FocusHandler; - -import java.util.List; - -public interface ParentUIComponent extends UIComponent { - List getChildren(); - void updateChildState(UIComponent UIComponent); - - default boolean isOverflowAllowed() { - return false; - } - - default UIComponent getChildAt(int x, int y) { - for (UIComponent child : getChildren()) { - if (child.isInBoundingBox(x, y)) { - if (child instanceof ParentUIComponent parent) { - return parent.getChildAt(x, y); - } else { - return this; - } - } - } - - return isInBoundingBox(x, y) ? this : null; - } - - @Override - default boolean mousePressed(double mouseX, double mouseY, int button) { - for (UIComponent child : getChildren()) { - if (!child.isInBoundingBox(getX() + mouseX, getY() + mouseY)) continue; - if (child.mousePressed(mouseX, mouseY, button)) { - FocusHandler focusHandler = getFocusHandler(); - if (focusHandler != null) - focusHandler.focus(child); - return true; - } - } - - return UIComponent.super.mousePressed(mouseX, mouseY, button); - } - - @Override - default boolean mouseReleased(double mouseX, double mouseY, int button) { - for (UIComponent child : getChildren()) { - if (!child.isInBoundingBox(getX() + mouseX, getY() + mouseY)) continue; - if (child.mouseReleased(mouseX, mouseY, button)) return true; - } - - return UIComponent.super.mouseReleased(mouseX, mouseY, button); - } - - @Override - default boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) { - for (UIComponent child : getChildren()) { - if (!child.isInBoundingBox(getX() + mouseX, getY() + mouseY)) continue; - if (child.mouseScrolled(mouseX, mouseY, scrollX, scrollY)) return true; - } - - return UIComponent.super.mouseScrolled(mouseX, mouseY, scrollX, scrollY); - } - - @Override - default void mouseMoved(double mouseX, double mouseY) { - for (UIComponent child : getChildren()) { - child.mouseMoved(mouseX, mouseY); - } - UIComponent.super.mouseMoved(mouseX, mouseY); - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/core/UIComponent.java b/common/src/main/java/me/pandamods/pandalib/client/screen/core/UIComponent.java deleted file mode 100644 index 1b857f1f..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/core/UIComponent.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.core; - -import me.pandamods.pandalib.client.screen.utils.FocusHandler; -import me.pandamods.pandalib.client.screen.utils.RenderContext; - -public interface UIComponent { - void render(RenderContext context, int mouseX, int mouseY, float partialTicks); - - ParentUIComponent getParent(); - - default boolean hasParent() { - return this.getParent() != null; - } - - void mount(ParentUIComponent parent); - void dismount(); - - default boolean isMounted() { - return this.hasParent(); - } - - default ParentUIComponent root() { - ParentUIComponent root = this.getParent(); - while (root.hasParent()) root = root.getParent(); - return root; - } - - default FocusHandler getFocusHandler() { - return this.hasParent() ? this.getParent().getFocusHandler() : null; - } - - void setX(int x); - void setY(int y); - - void setWidth(int width); - void setHeight(int height); - - int getX(); - int getY(); - - int getWidth(); - int getHeight(); - - default boolean isInBoundingBox(double x, double y) { - return x >= getX() && y >= getY() && x < getX() + getWidth() && y < getY() + getHeight(); - } - - default void position(int x, int y) { - setX(x); - setY(y); - } - - default void size(int width, int height) { - setWidth(width); - setHeight(height); - } - default void size(int size) { - size(size, size); - } - - default boolean keyPressed(int keyCode, int scanCode, int modifiers) { - return false; - } - - default boolean keyReleased(int keyCode, int scanCode, int modifiers) { - return false; - } - - default boolean charTyped(char codePoint, int modifiers) { - return false; - } - - default boolean mousePressed(double mouseX, double mouseY, int button) { - return false; - } - - default boolean mouseReleased(double mouseX, double mouseY, int button) { - return false; - } - - default boolean mouseScrolled(double mouseX, double mouseY, double scrollX, double scrollY) { - return false; - } - - default void mouseMoved(double mouseX, double mouseY) {} - - default void onFocusGained() {} - default void onFocusLost() {} -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/utils/FocusHandler.java b/common/src/main/java/me/pandamods/pandalib/client/screen/utils/FocusHandler.java deleted file mode 100644 index 25b93e7e..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/utils/FocusHandler.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.utils; - -import me.pandamods.pandalib.client.screen.core.UIComponent; -import org.jetbrains.annotations.Nullable; - -public class FocusHandler { - @Nullable - protected UIComponent focused; - - public void focus(UIComponent focused) { - if (this.focused == focused) return; - if (this.focused != null) this.focused.onFocusLost(); - this.focused = focused; - if (this.focused != null) this.focused.onFocusGained(); - } - - public void clearFocus() { - focus(null); - } - - @Nullable - public UIComponent getFocused() { - return focused; - } - - public boolean isFocused(UIComponent component) { - return focused == component; - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/client/screen/utils/RenderContext.java b/common/src/main/java/me/pandamods/pandalib/client/screen/utils/RenderContext.java deleted file mode 100644 index a17efbd8..00000000 --- a/common/src/main/java/me/pandamods/pandalib/client/screen/utils/RenderContext.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package me.pandamods.pandalib.client.screen.utils; - -import com.mojang.blaze3d.vertex.PoseStack; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiGraphics; - -public class RenderContext { - public final GuiGraphics guiGraphics; - - public RenderContext(GuiGraphics guiGraphics) { - this.guiGraphics = guiGraphics; - } - - public void scissor(int minX, int minY, int maxX, int maxY, Runnable render) { - guiGraphics.enableScissor(minX, minY, maxX, maxY); - render.run(); - guiGraphics.disableScissor(); - } - - public PoseStack getPoseStack() { - return guiGraphics.pose(); - } - - public double getDeltaSeconds() { - return 1f / Minecraft.getInstance().getFps(); - } -} diff --git a/common/src/main/java/me/pandamods/pandalib/utils/NetworkHelper.java b/common/src/main/java/me/pandamods/pandalib/utils/NetworkHelper.java deleted file mode 100644 index 2ab68ef9..00000000 --- a/common/src/main/java/me/pandamods/pandalib/utils/NetworkHelper.java +++ /dev/null @@ -1,26 +0,0 @@ -package me.pandamods.pandalib.utils; - -import dev.architectury.networking.NetworkManager; -import dev.architectury.platform.Platform; -import dev.architectury.utils.Env; -import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.StreamCodec; -import net.minecraft.network.protocol.common.custom.CustomPacketPayload; - -public class NetworkHelper { - public static void registerS2C(CustomPacketPayload.Type packetType, - StreamCodec codec, - NetworkManager.NetworkReceiver receiver) { - if (Platform.getEnvironment().equals(Env.SERVER)) { - NetworkManager.registerS2CPayloadType(packetType, codec); - } else { - NetworkManager.registerReceiver(NetworkManager.s2c(), packetType, codec, receiver); - } - } - - public static void registerC2S(CustomPacketPayload.Type packetType, - StreamCodec codec, - NetworkManager.NetworkReceiver receiver) { - NetworkManager.registerReceiver(NetworkManager.c2s(), packetType, codec, receiver); - } -} \ No newline at end of file diff --git a/common/src/main/java/me/pandamods/pandalib/utils/animation/Easing.java b/common/src/main/java/me/pandamods/pandalib/utils/animation/Easing.java new file mode 100644 index 00000000..5bc541aa --- /dev/null +++ b/common/src/main/java/me/pandamods/pandalib/utils/animation/Easing.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2024 Oliver Froberg (The Panda Oliver) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package me.pandamods.pandalib.utils.animation; + +public interface Easing { + double ease(double t); + + Easing easeInSine = t -> 1 - Math.cos((t * Math.PI) / 2); + Easing easeOutSine = t -> Math.sin((t * Math.PI) / 2); + Easing easeInOutSine = t -> -(Math.cos(Math.PI * t) - 1) / 2; + + Easing easeInQuad = t -> t * t; + Easing easeOutQuad = t -> 1 - (1 - t) * (1 - t); + Easing easeInOutQuad = t -> t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) * (-2 * t + 2) / 2; + + Easing easeInCubic = t -> t * t * t; + Easing easeOutCubic = t -> 1 - Math.pow(1 - t, 3); + Easing easeInOutCubic = t -> t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; + + Easing easeInQuart = t -> t * t * t * t; + Easing easeOutQuart = t -> 1 - Math.pow(1 - t, 4); + Easing easeInOutQuart = t -> t < 0.5 ? 8 * t * t * t * t : 1 - Math.pow(-2 * t + 2, 4) / 2; + + Easing easeInQuint = t -> t * t * t * t * t; + Easing easeOutQuint = t -> 1 - Math.pow(1 - t, 5); + Easing easeInOutQuint = t -> t < 0.5 ? 16 * t * t * t * t * t : 1 - Math.pow(-2 * t + 2, 5) / 2; + + Easing easeInExpo = t -> t == 0 ? 0 : Math.pow(2, 10 * (t - 1)); + Easing easeOutExpo = t -> t == 1 ? 1 : 1 - Math.pow(2, -10 * t); + Easing easeInOutExpo = t -> t == 0 ? 0 : t == 1 ? 1 : t < 0.5 ? Math.pow(2, 20 * t - 10) / 2 : (2 - Math.pow(2, -20 * t + 10)) / 2; + + Easing easeInCirc = t -> 1 - Math.sqrt(1 - t * t); + Easing easeOutCirc = t -> Math.sqrt(1 - Math.pow(t - 1, 2)); + Easing easeInOutCirc = t -> t < 0.5 ? (1 - Math.sqrt(1 - 4 * t * t)) / 2 : (Math.sqrt(1 - -4 * t * t) + 1) / 2; + + Easing easeInBack = t -> { + double c1 = 1.70158; + double c3 = c1 + 1; + + return c3 * t * t * t - c1 * t * t; + }; + Easing easeOutBack = t -> { + double c1 = 1.70158; + double c3 = c1 + 1; + + return 1 + c3 * (t - 1) * (t - 1) * (t - 1) - c1 * (t - 1) * (t - 1); + }; + Easing easeInOutBack = t -> { + double c1 = 1.70158; + double c2 = c1 * 1.525; + + return t < 0.5 + ? (Math.pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2 + : (Math.pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2; + }; + + Easing easeInElastic = t -> { + double c4 = (2 * Math.PI) / 3; + + return t == 0 + ? 0 + : t == 1 + ? 1 + : -Math.pow(2, 10 * t - 10) * Math.sin((t * 10 - 10.75) * c4); + }; + Easing easeOutElastic = t -> { + double c4 = (2 * Math.PI) / 3; + + return t == 0 + ? 0 + : t == 1 + ? 1 + : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * c4) + 1; + }; + Easing easeInOutElastic = t -> { + double c5 = (2 * Math.PI) / 4.5; + + return t == 0 + ? 0 + : t == 1 + ? 1 + : t < 0.5 + ? -(Math.pow(2, 20 * t - 10) * Math.sin((20 * t - 11.125) * c5)) / 2 + : (Math.pow(2, -20 * t + 10) * Math.sin((20 * t - 11.125) * c5)) / 2 + 1; + }; + + Easing easeOutBounce = t -> { + double n1 = 7.5625; + double d1 = 2.75; + + if (t < 1 / d1) { + return n1 * t * t; + } else if (t < 2 / d1) { + return n1 * (t -= 1.5 / d1) * t + 0.75; + } else if (t < 2.5 / d1) { + return n1 * (t -= 2.25 / d1) * t + 0.9375; + } else { + return n1 * (t -= 2.625 / d1) * t + 0.984375; + } + }; + Easing easeInBounce = t -> 1 - easeOutBounce.ease(1 - t); + Easing easeInOutBounce = t -> t < 0.5 ? (1 - easeOutBounce.ease(1 - 2 * t)) / 2 : (1 + easeOutBounce.ease(2 * t - 1)) / 2; +}