From 30557360a886875b5e953d32ac577706ede3c8e2 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 26 Jun 2022 21:45:36 +0800 Subject: Primitive tags category implementation --- .../gui/screen/AbstractDisplayViewingScreen.java | 5 ++ .../gui/widget/DelegateWidgetWithTranslate.java | 17 +++++-- .../impl/client/gui/widget/InternalWidgets.java | 15 ++++-- .../rei/impl/client/gui/widget/NoOpWidget.java | 10 +++- .../rei/impl/client/gui/widget/OverflowWidget.java | 30 ++++++----- .../impl/client/gui/widget/PaddedCenterWidget.java | 58 ++++++++++++++++++++++ .../rei/impl/client/gui/widget/PaddedWidget.java | 51 +++++++++++++++++++ 7 files changed, 162 insertions(+), 24 deletions(-) create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedCenterWidget.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedWidget.java (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java index 4a5e6fd93..7d9cffa63 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java @@ -283,6 +283,11 @@ public abstract class AbstractDisplayViewingScreen extends Screen implements Dis return super.mouseScrolled(mouseX, mouseY, amount) || (getOverlay().mouseScrolled(mouseX, mouseY, amount) && handleFocuses()); } + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + return super.keyPressed(keyCode, scanCode, modifiers) || (getOverlay().keyPressed(keyCode, scanCode, modifiers) && handleFocuses()); + } + @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { return super.keyReleased(keyCode, scanCode, modifiers) || (getOverlay().keyReleased(keyCode, scanCode, modifiers) && handleFocuses()); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/DelegateWidgetWithTranslate.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/DelegateWidgetWithTranslate.java index c3e2090cd..582a01830 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/DelegateWidgetWithTranslate.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/DelegateWidgetWithTranslate.java @@ -25,6 +25,8 @@ package me.shedaniel.rei.impl.client.gui.widget; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix4f; +import com.mojang.math.Transformation; +import com.mojang.math.Vector3f; import com.mojang.math.Vector4f; import me.shedaniel.rei.api.client.gui.widgets.DelegateWidget; import me.shedaniel.rei.api.client.gui.widgets.Widget; @@ -44,6 +46,13 @@ public class DelegateWidgetWithTranslate extends DelegateWidget { return translate.get(); } + protected final Matrix4f inverseTranslate() { + Transformation transformation = new Transformation(translate()); + Transformation inverse = transformation.inverse(); + if (inverse != null) inverse.getScale(); // This has a side effect + return inverse == null ? Transformation.identity().getMatrix() : inverse.getMatrix(); + } + @Override public void render(PoseStack poseStack, int mouseX, int mouseY, float delta) { poseStack.pushPose(); @@ -55,7 +64,7 @@ public class DelegateWidgetWithTranslate extends DelegateWidget { private Vector4f transformMouse(double mouseX, double mouseY) { Vector4f mouse = new Vector4f((float) mouseX, (float) mouseY, 0, 1); - mouse.transform(translate()); + mouse.transform(inverseTranslate()); return mouse; } @@ -92,7 +101,7 @@ public class DelegateWidgetWithTranslate extends DelegateWidget { @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { try { - Widget.translateMouse(translate()); + Widget.translateMouse(inverseTranslate()); return super.keyPressed(keyCode, scanCode, modifiers); } finally { Widget.popMouse(); @@ -102,7 +111,7 @@ public class DelegateWidgetWithTranslate extends DelegateWidget { @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { try { - Widget.translateMouse(translate()); + Widget.translateMouse(inverseTranslate()); return super.keyReleased(keyCode, scanCode, modifiers); } finally { Widget.popMouse(); @@ -112,7 +121,7 @@ public class DelegateWidgetWithTranslate extends DelegateWidget { @Override public boolean charTyped(char character, int modifiers) { try { - Widget.translateMouse(translate()); + Widget.translateMouse(inverseTranslate()); return super.charTyped(character, modifiers); } finally { Widget.popMouse(); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java index 92004f529..afab2362e 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java @@ -78,7 +78,7 @@ public final class InternalWidgets { if (result.hasApplicable) { autoCraftingButton.setText(text); } else { - autoCraftingButton.setText(new TextComponent("?")); + autoCraftingButton.setText(new TextComponent("!")); } if (result.hasApplicable && (containsMouse(mouseX, mouseY) || autoCraftingButton.isFocused()) && result.renderer != null) { @@ -270,13 +270,18 @@ public final class InternalWidgets { } @Override - public Widget noOp() { + public WidgetWithBounds noOp() { return NoOpWidget.INSTANCE; } - + + @Override + public WidgetWithBounds wrapOverflow(Rectangle bounds, WidgetWithBounds widget) { + return new OverflowWidget(bounds, new PaddedCenterWidget(bounds, widget)); + } + @Override - public Widget wrapOverflow(Rectangle bounds, WidgetWithBounds widget) { - return new OverflowWidget(bounds, widget); + public WidgetWithBounds wrapPadded(int padLeft, int padRight, int padTop, int padBottom, WidgetWithBounds widget) { + return new PaddedWidget(padLeft, padRight, padTop, padBottom, widget); } } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/NoOpWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/NoOpWidget.java index a9cfd79ce..b31c61532 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/NoOpWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/NoOpWidget.java @@ -24,13 +24,14 @@ package me.shedaniel.rei.impl.client.gui.widget; import com.mojang.blaze3d.vertex.PoseStack; -import me.shedaniel.rei.api.client.gui.widgets.Widget; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds; import net.minecraft.client.gui.components.events.GuiEventListener; import java.util.Collections; import java.util.List; -public class NoOpWidget extends Widget { +public class NoOpWidget extends WidgetWithBounds { public static final NoOpWidget INSTANCE = new NoOpWidget(); private NoOpWidget() { @@ -44,4 +45,9 @@ public class NoOpWidget extends Widget { public List children() { return Collections.emptyList(); } + + @Override + public Rectangle getBounds() { + return new Rectangle(); + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/OverflowWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/OverflowWidget.java index 89fca306e..b46640493 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/OverflowWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/OverflowWidget.java @@ -25,7 +25,6 @@ package me.shedaniel.rei.impl.client.gui.widget; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix4f; -import me.shedaniel.clothconfig2.api.ScissorsHandler; import me.shedaniel.clothconfig2.api.animator.NumberAnimator; import me.shedaniel.clothconfig2.api.animator.ValueAnimator; import me.shedaniel.clothconfig2.api.scroll.ScrollingContainer; @@ -34,9 +33,6 @@ import me.shedaniel.math.Rectangle; import me.shedaniel.rei.RoughlyEnoughItemsCoreClient; import me.shedaniel.rei.api.client.gui.widgets.CloseableScissors; import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds; -import me.shedaniel.rei.impl.common.util.RectangleUtils; - -import java.io.Closeable; @SuppressWarnings("UnstableApiUsage") public class OverflowWidget extends DelegateWidgetWithTranslate { @@ -70,15 +66,17 @@ public class OverflowWidget extends DelegateWidgetWithTranslate { public void render(PoseStack poseStack, int mouseX, int mouseY, float delta) { Rectangle widgetBounds = ((WidgetWithBounds) widget).getBounds(); this.scale.update(delta); - this.scale.setTarget(ScrollingContainer.handleBounceBack(this.scale.target() - 1, - Math.min(widgetBounds.width * 1.0F / getBounds().width, widgetBounds.height * 1.0F / getBounds().height) - 1, delta, .001) + 1); + this.scale.setTarget(ScrollingContainer.handleBounceBack(this.scale.target() - 0.78, + Math.min(widgetBounds.width * 1.0F / getBounds().width, widgetBounds.height * 1.0F / getBounds().height) - 0.78, delta, .001) + 0.78); this.translate.update(delta); - this.translate.setAs(new FloatingPoint( - ScrollingContainer.handleBounceBack(this.translate.target().x + widgetBounds.width - getBounds().width / 2 * scale.value(), - widgetBounds.width - getBounds().width * scale.value(), delta, .001) - (widgetBounds.width - getBounds().width / 2 * scale.value()), - ScrollingContainer.handleBounceBack(this.translate.target().y + widgetBounds.height - getBounds().height / 2 * scale.value(), - widgetBounds.height - getBounds().height * scale.value(), delta, .001) - (widgetBounds.height - getBounds().height / 2 * scale.value()) - )); + for (int i = 0; i < 3; i++) { + this.translate.setAs(new FloatingPoint( + ScrollingContainer.handleBounceBack(this.translate.target().x + widgetBounds.width - getBounds().width / 2 * scale.value(), + widgetBounds.width - getBounds().width * scale.value(), delta, .0001) - (widgetBounds.width - getBounds().width / 2 * scale.value()), + ScrollingContainer.handleBounceBack(this.translate.target().y + widgetBounds.height - getBounds().height / 2 * scale.value(), + widgetBounds.height - getBounds().height * scale.value(), delta, .0001) - (widgetBounds.height - getBounds().height / 2 * scale.value()) + )); + } if (!RoughlyEnoughItemsCoreClient.isLeftMousePressed) { this.translate.setAs(new FloatingPoint(this.translate.value().x + this.velocity.value().x, this.translate.value().y + this.velocity.value().y)); } @@ -89,7 +87,13 @@ public class OverflowWidget extends DelegateWidgetWithTranslate { ), 20); try (CloseableScissors scissors = scissor(poseStack, this.bounds)) { - super.render(poseStack, mouseX, mouseY, delta); + boolean containsMouse = this.bounds.contains(mouseX, mouseY); + + if (containsMouse) { + super.render(poseStack, mouseX, mouseY, delta); + } else { + super.render(poseStack, Integer.MAX_VALUE, Integer.MAX_VALUE, delta); + } } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedCenterWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedCenterWidget.java new file mode 100644 index 000000000..7848fa7f0 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedCenterWidget.java @@ -0,0 +1,58 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.client.gui.widget; + +import com.mojang.math.Matrix4f; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds; + +public class PaddedCenterWidget extends DelegateWidgetWithTranslate { + private final Rectangle bounds; + + public PaddedCenterWidget(Rectangle bounds, WidgetWithBounds widget) { + super(widget, Matrix4f::new); + this.bounds = bounds; + } + + @Override + protected Matrix4f translate() { + Rectangle widgetBounds = ((WidgetWithBounds) widget).getBounds(); + float xTranslate = 0, yTranslate = 0; + if (widgetBounds.width < bounds.width) { + xTranslate = (bounds.width - widgetBounds.width) / 2f; + } + if (widgetBounds.height < bounds.height) { + yTranslate = (bounds.height - widgetBounds.height) / 2f; + } + return Matrix4f.createTranslateMatrix(xTranslate, yTranslate, 0); + } + + @Override + public Rectangle getBounds() { + Rectangle widgetBounds = ((WidgetWithBounds) widget).getBounds(); + int newWidth = Math.max(widgetBounds.width, bounds.width); + int newHeight = Math.max(widgetBounds.height, bounds.height); + return new Rectangle(bounds.getCenterX() - newWidth / 2f, bounds.getCenterY() - newHeight / 2f, newWidth, newHeight); + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedWidget.java new file mode 100644 index 000000000..4151ffb45 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/PaddedWidget.java @@ -0,0 +1,51 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.client.gui.widget; + +import com.mojang.math.Matrix4f; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds; + +public class PaddedWidget extends DelegateWidgetWithTranslate { + private final int padLeft, padRight, padTop, padBottom; + + public PaddedWidget(int padLeft, int padRight, int padTop, int padBottom, WidgetWithBounds widget) { + super(widget, Matrix4f::new); + this.padLeft = padLeft; + this.padRight = padRight; + this.padTop = padTop; + this.padBottom = padBottom; + } + + @Override + protected Matrix4f translate() { + return Matrix4f.createTranslateMatrix(padLeft, padRight, 0); + } + + @Override + public Rectangle getBounds() { + Rectangle widgetBounds = ((WidgetWithBounds) widget).getBounds(); + return new Rectangle(widgetBounds.x - padLeft, widgetBounds.y - padTop, widgetBounds.width + padLeft + padRight, widgetBounds.height + padTop + padBottom); + } +} -- cgit