diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-06-26 04:09:06 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-06-28 03:21:12 +0800 |
| commit | 091c92b90421c13bcde0f80909170fde0e317d39 (patch) | |
| tree | 22116d5c5fa733dbd0c2a2c03e5f2559dc8e5d3a | |
| parent | ae947d3c2f9935ad4f5d1de0d06d19609940818d (diff) | |
| download | RoughlyEnoughItems-091c92b90421c13bcde0f80909170fde0e317d39.tar.gz RoughlyEnoughItems-091c92b90421c13bcde0f80909170fde0e317d39.tar.bz2 RoughlyEnoughItems-091c92b90421c13bcde0f80909170fde0e317d39.zip | |
Adapt scissors to transform
7 files changed, 85 insertions, 50 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/CloseableScissors.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/CloseableScissors.java new file mode 100644 index 000000000..226c33ffc --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/CloseableScissors.java @@ -0,0 +1,12 @@ +package me.shedaniel.rei.api.client.gui.widgets; + +import org.jetbrains.annotations.ApiStatus; + +import java.io.Closeable; + +@ApiStatus.NonExtendable +@ApiStatus.Experimental +public interface CloseableScissors extends Closeable { + @Override + void close(); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widget.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widget.java index 3c753e747..1c70c616d 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widget.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widget.java @@ -26,6 +26,7 @@ package me.shedaniel.rei.api.client.gui.widgets; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix4f; import com.mojang.math.Vector4f; +import me.shedaniel.clothconfig2.api.ScissorsHandler; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.math.impl.PointHelper; @@ -35,7 +36,11 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; +import java.io.Closeable; +import java.io.IOException; import java.util.Stack; /** @@ -115,4 +120,23 @@ public abstract class Widget extends AbstractContainerEventHandler implements ne public void render(PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { render(matrices, mouseX, mouseY, delta); } + + @ApiStatus.Experimental + public static CloseableScissors scissor(PoseStack matrices, Rectangle bounds) { + return scissor(matrices.last().pose(), bounds); + } + + @ApiStatus.Experimental + public static CloseableScissors scissor(Matrix4f matrix, Rectangle bounds) { + Vector4f vec1 = new Vector4f((float) bounds.x, (float) bounds.y, 0, 1); + vec1.transform(matrix); + Vector4f vec2 = new Vector4f((float) bounds.getMaxX(), (float) bounds.getMaxY(), 0, 1); + vec2.transform(matrix); + int x1 = Math.round(vec1.x()); + int x2 = Math.round(vec2.x()); + int y1 = Math.round(vec1.y()); + int y2 = Math.round(vec2.y()); + ScissorsHandler.INSTANCE.scissor(new Rectangle(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1))); + return ScissorsHandler.INSTANCE::removeLastScissor; + } } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultInformationCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultInformationCategory.java index 50adee384..615269b80 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultInformationCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultInformationCategory.java @@ -36,6 +36,7 @@ import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.gui.AbstractRenderer; import me.shedaniel.rei.api.client.gui.DisplayRenderer; import me.shedaniel.rei.api.client.gui.Renderer; +import me.shedaniel.rei.api.client.gui.widgets.CloseableScissors; import me.shedaniel.rei.api.client.gui.widgets.Widget; import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds; import me.shedaniel.rei.api.client.gui.widgets.Widgets; @@ -192,18 +193,18 @@ public class DefaultInformationCategory implements DisplayCategory<DefaultInform public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { scrolling.updatePosition(delta); Rectangle innerBounds = scrolling.getScissorBounds(); - ScissorsHandler.INSTANCE.scissor(innerBounds); - int currentY = -scrolling.scrollAmountInt() + innerBounds.y; - for (FormattedCharSequence text : texts) { - if (text != null && currentY + font.lineHeight >= innerBounds.y && currentY <= innerBounds.getMaxY()) { - font.draw(matrices, text, innerBounds.x + 2, currentY + 2, REIRuntime.getInstance().isDarkThemeEnabled() ? 0xFFBBBBBB : 0xFF090909); + try (CloseableScissors scissors = scissor(matrices, innerBounds)) { + int currentY = -scrolling.scrollAmountInt() + innerBounds.y; + for (FormattedCharSequence text : texts) { + if (text != null && currentY + font.lineHeight >= innerBounds.y && currentY <= innerBounds.getMaxY()) { + font.draw(matrices, text, innerBounds.x + 2, currentY + 2, REIRuntime.getInstance().isDarkThemeEnabled() ? 0xFFBBBBBB : 0xFF090909); + } + currentY += text == null ? 4 : font.lineHeight; } - currentY += text == null ? 4 : font.lineHeight; } - ScissorsHandler.INSTANCE.removeLastScissor(); - ScissorsHandler.INSTANCE.scissor(scrolling.getBounds()); - scrolling.renderScrollBar(0xff000000, 1, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); - ScissorsHandler.INSTANCE.removeLastScissor(); + try (CloseableScissors scissors = scissor(matrices, scrolling.getBounds())) { + scrolling.renderScrollBar(0xff000000, 1, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); + } } @Override diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconBaseCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconBaseCategory.java index 049756b68..9ea7bc9aa 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconBaseCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconBaseCategory.java @@ -33,10 +33,7 @@ import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.gui.DisplayRenderer; import me.shedaniel.rei.api.client.gui.Renderer; -import me.shedaniel.rei.api.client.gui.widgets.Slot; -import me.shedaniel.rei.api.client.gui.widgets.Widget; -import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds; -import me.shedaniel.rei.api.client.gui.widgets.Widgets; +import me.shedaniel.rei.api.client.gui.widgets.*; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.util.CollectionUtils; @@ -157,21 +154,21 @@ public class DefaultBeaconBaseCategory implements DisplayCategory<DefaultBeaconB public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { scrolling.updatePosition(delta); Rectangle innerBounds = scrolling.getScissorBounds(); - ScissorsHandler.INSTANCE.scissor(innerBounds); - for (int y = 0; y < Mth.ceil(widgets.size() / 8f); y++) { - for (int x = 0; x < 8; x++) { - int index = y * 8 + x; - if (widgets.size() <= index) - break; - Slot widget = widgets.get(index); - widget.getBounds().setLocation(bounds.x + 1 + x * 18, bounds.y + 1 + y * 18 - scrolling.scrollAmountInt()); - widget.render(matrices, mouseX, mouseY, delta); + try (CloseableScissors scissors = scissor(matrices, innerBounds)) { + for (int y = 0; y < Mth.ceil(widgets.size() / 8f); y++) { + for (int x = 0; x < 8; x++) { + int index = y * 8 + x; + if (widgets.size() <= index) + break; + Slot widget = widgets.get(index); + widget.getBounds().setLocation(bounds.x + 1 + x * 18, bounds.y + 1 + y * 18 - scrolling.scrollAmountInt()); + widget.render(matrices, mouseX, mouseY, delta); + } } } - ScissorsHandler.INSTANCE.removeLastScissor(); - ScissorsHandler.INSTANCE.scissor(scrolling.getBounds()); - scrolling.renderScrollBar(0xff000000, 1, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); - ScissorsHandler.INSTANCE.removeLastScissor(); + try (CloseableScissors scissors = scissor(matrices, scrolling.getBounds())) { + scrolling.renderScrollBar(0xff000000, 1, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); + } } @Override diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconPaymentCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconPaymentCategory.java index 853df98b5..478408619 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconPaymentCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/beacon/DefaultBeaconPaymentCategory.java @@ -33,10 +33,7 @@ import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.gui.DisplayRenderer; import me.shedaniel.rei.api.client.gui.Renderer; -import me.shedaniel.rei.api.client.gui.widgets.Slot; -import me.shedaniel.rei.api.client.gui.widgets.Widget; -import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds; -import me.shedaniel.rei.api.client.gui.widgets.Widgets; +import me.shedaniel.rei.api.client.gui.widgets.*; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.util.CollectionUtils; @@ -157,21 +154,21 @@ public class DefaultBeaconPaymentCategory implements DisplayCategory<DefaultBeac public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { scrolling.updatePosition(delta); Rectangle innerBounds = scrolling.getScissorBounds(); - ScissorsHandler.INSTANCE.scissor(innerBounds); - for (int y = 0; y < Mth.ceil(widgets.size() / 8f); y++) { - for (int x = 0; x < 8; x++) { - int index = y * 8 + x; - if (widgets.size() <= index) - break; - Slot widget = widgets.get(index); - widget.getBounds().setLocation(bounds.x + 1 + x * 18, bounds.y + 1 + y * 18 - scrolling.scrollAmountInt()); - widget.render(matrices, mouseX, mouseY, delta); + try (CloseableScissors scissors = scissor(matrices, innerBounds)) { + for (int y = 0; y < Mth.ceil(widgets.size() / 8f); y++) { + for (int x = 0; x < 8; x++) { + int index = y * 8 + x; + if (widgets.size() <= index) + break; + Slot widget = widgets.get(index); + widget.getBounds().setLocation(bounds.x + 1 + x * 18, bounds.y + 1 + y * 18 - scrolling.scrollAmountInt()); + widget.render(matrices, mouseX, mouseY, delta); + } } } - ScissorsHandler.INSTANCE.removeLastScissor(); - ScissorsHandler.INSTANCE.scissor(scrolling.getBounds()); - scrolling.renderScrollBar(0xff000000, 1, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); - ScissorsHandler.INSTANCE.removeLastScissor(); + try (CloseableScissors scissors = scissor(matrices, scrolling.getBounds())) { + scrolling.renderScrollBar(0xff000000, 1, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); + } } @Override diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/tag/DefaultTagCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/tag/DefaultTagCategory.java index c79cc9eb3..07d5fe985 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/tag/DefaultTagCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/tag/DefaultTagCategory.java @@ -86,9 +86,9 @@ public class DefaultTagCategory implements DisplayCategory<DefaultTagDisplay<?, new GuiComponent() { { fillGradient(matrices, 0, 0, 1000, 1000, 0xff3489eb, 0xffc41868); - for (int x = 0; x < 20; x++) { - for (int y = 0; y < 20; y++) { - Widgets.createSlot(new Point(500 - 9 * 20 + x * 18, 500 - 9 * 20 + y * 18)) + for (int x = 0; x < 10; x++) { + for (int y = 0; y < 10; y++) { + Widgets.createSlot(new Point(500 - 9 * 10 + x * 18, 500 - 9 * 10 + y * 18)) .entry(EntryStacks.of(Registry.ITEM.byId(x + y * 10 + 1))) .disableBackground() .render(matrices, mouseX, mouseY, delta); 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 12cdfcfc9..89fca306e 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 @@ -32,7 +32,11 @@ import me.shedaniel.clothconfig2.api.scroll.ScrollingContainer; import me.shedaniel.math.FloatingPoint; 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 { @@ -84,9 +88,9 @@ public class OverflowWidget extends DelegateWidgetWithTranslate { ScrollingContainer.handleBounceBack(this.velocity.target().y, 0, delta, .0001) ), 20); - ScissorsHandler.INSTANCE.scissor(this.bounds); - super.render(poseStack, mouseX, mouseY, delta); - ScissorsHandler.INSTANCE.removeLastScissor(); + try (CloseableScissors scissors = scissor(poseStack, this.bounds)) { + super.render(poseStack, mouseX, mouseY, delta); + } } @Override |
