Implement GUI Stacking feature. (#6927)

* Implement GUI Stacking feature.
You can now use `mc.pushGuiLayer` and `mc.popGuiLayer` to manage the layers.
mc.displayGuiScreen behaves such that if called with a non-null screen it replaces the entire stack, and if called with null it closes the entire stack.

* Apply review comment fixes from 1.17 counterpart.

* Revert unintended changes.

* Missed these reverts.
This commit is contained in:
gigaherz authored and GitHub committed 2021-11-04 17:39:38 +10:30
1 parent 9ef34a9f1a
commit 0ecc6c6359
7 files changed
+248 -1

No files matched your search

@@ -1,5 +1,14 @@
--- a/net/minecraft/client/Minecraft.java
+++ b/net/minecraft/client/Minecraft.java
@@ -241,7 +_,7 @@
import org.apache.logging.log4j.Logger;
@OnlyIn(Dist.CLIENT)
-public class Minecraft extends RecursiveEventLoop<Runnable> implements ISnooperInfo, IWindowEventListener {
+public class Minecraft extends RecursiveEventLoop<Runnable> implements ISnooperInfo, IWindowEventListener, net.minecraftforge.client.extensions.IForgeMinecraft {
private static Minecraft field_71432_P;
private static final Logger field_147123_G = LogManager.getLogger();
public static final boolean field_142025_a = Util.func_110647_a() == Util.OS.OSX;
@@ -368,6 +_,7 @@
public Minecraft(GameConfiguration p_i45547_1_) {
super("Client");
@@ -131,10 +140,11 @@
if (p_147108_1_ == null && this.field_71441_e == null) {
p_147108_1_ = new MainMenuScreen();
} else if (p_147108_1_ == null && this.field_71439_g.func_233643_dh_()) {
@@ -831,6 +_,14 @@
@@ -831,6 +_,15 @@
}
}
+ net.minecraftforge.client.ForgeHooksClient.clearGuiLayers(this);
+ Screen old = this.field_71462_r;
+ net.minecraftforge.client.event.GuiOpenEvent event = new net.minecraftforge.client.event.GuiOpenEvent(p_147108_1_);
+ if (net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event)) return;
@@ -1,5 +1,14 @@
--- a/net/minecraft/client/gui/screen/Screen.java
+++ b/net/minecraft/client/gui/screen/Screen.java
@@ -99,7 +_,7 @@
}
public void func_231175_as__() {
- this.field_230706_i_.func_147108_a((Screen)null);
+ this.field_230706_i_.popGuiLayer();
}
protected <T extends Widget> T func_230480_a_(T p_230480_1_) {
@@ -113,7 +_,10 @@
}
@@ -26,6 +26,19 @@
}
this.field_78531_r.func_147110_a().func_147610_a(true);
@@ -448,10 +_,10 @@
RenderSystem.clear(256, Minecraft.field_142025_a);
RenderSystem.matrixMode(5889);
RenderSystem.loadIdentity();
- RenderSystem.ortho(0.0D, (double)mainwindow.func_198109_k() / mainwindow.func_198100_s(), (double)mainwindow.func_198091_l() / mainwindow.func_198100_s(), 0.0D, 1000.0D, 3000.0D);
+ RenderSystem.ortho(0.0D, (double)mainwindow.func_198109_k() / mainwindow.func_198100_s(), (double)mainwindow.func_198091_l() / mainwindow.func_198100_s(), 0.0D, 1000.0D, net.minecraftforge.client.ForgeHooksClient.getGuiFarPlane());
RenderSystem.matrixMode(5888);
RenderSystem.loadIdentity();
- RenderSystem.translatef(0.0F, 0.0F, -2000.0F);
+ RenderSystem.translatef(0.0F, 0.0F, 1000.0F - net.minecraftforge.client.ForgeHooksClient.getGuiFarPlane());
RenderHelper.func_227784_d_();
MatrixStack matrixstack = new MatrixStack();
if (p_195458_4_ && this.field_78531_r.field_71441_e != null) {
@@ -486,7 +_,7 @@
}
} else if (this.field_78531_r.field_71462_r != null) {
@@ -31,6 +31,7 @@ import net.minecraft.client.audio.SoundEngine;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.ClientBossInfo;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.chat.NarratorChatListener;
import net.minecraft.client.gui.screen.BiomeGeneratorTypeScreens;
import net.minecraft.client.gui.screen.MainMenuScreen;
import net.minecraft.client.gui.screen.Screen;
@@ -117,6 +118,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Stream;
import static net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType.BOSSINFO;
@@ -131,6 +133,55 @@ public class ForgeHooksClient
//private static final ResourceLocation ITEM_GLINT = new ResourceLocation("textures/misc/enchanted_item_glint.png");
/**
* Contains the *extra* GUI layers.
* The current top layer stays in Minecraft#currentScreen, and the rest serve as a background for it.
*/
private static final Stack<Screen> guiLayers = new Stack<>();
public static void clearGuiLayers(Minecraft minecraft)
{
while(guiLayers.size() > 0)
popGuiLayerInternal(minecraft);
}
private static void popGuiLayerInternal(Minecraft minecraft)
{
if (minecraft.screen != null)
minecraft.screen.removed();
minecraft.screen = guiLayers.pop();
}
public static void pushGuiLayer(Minecraft minecraft, Screen screen)
{
if (minecraft.screen != null)
guiLayers.push(minecraft.screen);
minecraft.screen = Objects.requireNonNull(screen);
screen.init(minecraft, minecraft.getWindow().getGuiScaledWidth(), minecraft.getWindow().getGuiScaledHeight());
NarratorChatListener.INSTANCE.sayNow(screen.getNarrationMessage());
}
public static void popGuiLayer(Minecraft minecraft)
{
if (guiLayers.size() == 0)
{
minecraft.setScreen(null);
return;
}
popGuiLayerInternal(minecraft);
if (minecraft.screen != null)
NarratorChatListener.INSTANCE.sayNow(minecraft.screen.getNarrationMessage());
}
public static float getGuiFarPlane()
{
// 1000 units for the overlay background,
// and 2000 units for each layered Screen,
return 1000.0F + 2000.0F * (1 + guiLayers.size());
}
public static String getArmorTexture(Entity entity, ItemStack armor, String _default, EquipmentSlotType slot, String type)
{
String result = armor.getItem().getArmorTexture(armor, entity, slot, type);
@@ -297,6 +348,18 @@ public class ForgeHooksClient
}
public static void drawScreen(Screen screen, MatrixStack mStack, int mouseX, int mouseY, float partialTicks)
{
mStack.pushPose();
guiLayers.forEach(layer -> {
// Prevent the background layers from thinking the mouse is over their controls and showing them as highlighted.
drawScreenInternal(layer, mStack, Integer.MAX_VALUE, Integer.MAX_VALUE, partialTicks);
mStack.translate(0,0,2000);
});
drawScreenInternal(screen, mStack, mouseX, mouseY, partialTicks);
mStack.popPose();
}
private static void drawScreenInternal(Screen screen, MatrixStack mStack, int mouseX, int mouseY, float partialTicks)
{
if (!MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.DrawScreenEvent.Pre(screen, mStack, mouseX, mouseY, partialTicks)))
screen.render(mStack, mouseX, mouseY, partialTicks);
@@ -0,0 +1,39 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2020.
*
* This library 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 version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.client.extensions;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraftforge.client.ForgeHooksClient;
public interface IForgeMinecraft
{
default Minecraft getSelf() { return (Minecraft)this; }
default void pushGuiLayer(Screen screen)
{
ForgeHooksClient.pushGuiLayer(getSelf(), screen);
}
default void popGuiLayer()
{
ForgeHooksClient.popGuiLayer(getSelf());
}
}
@@ -0,0 +1,111 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2020.
*
* This library 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 version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.debug.client;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import java.util.Random;
@Mod(GuiLayeringTest.MODID)
public class GuiLayeringTest
{
private static final Random RANDOM = new Random();
public static final String MODID="gui_layer_test";
@Mod.EventBusSubscriber(value= Dist.CLIENT, modid=MODID, bus= Mod.EventBusSubscriber.Bus.FORGE)
public static class ClientEvents
{
@SubscribeEvent
public static void guiOpen(GuiScreenEvent.InitGuiEvent event)
{
if (event.getGui() instanceof ContainerScreen)
{
event.addWidget(new Button(2, 2, 150, 20, new StringTextComponent("Test Gui Layering"), btn -> {
Minecraft.getInstance().pushGuiLayer(new TestLayer(new StringTextComponent("LayerScreen")));
}));
event.addWidget(new Button(2, 25, 150, 20, new StringTextComponent("Test Gui Normal"), btn -> {
Minecraft.getInstance().setScreen(new TestLayer(new StringTextComponent("LayerScreen")));
}));
}
}
public static class TestLayer extends Screen
{
protected TestLayer(ITextComponent titleIn)
{
super(titleIn);
}
@Override
public void render(MatrixStack mStack, int mouseX, int mouseY, float partialTicks)
{
this.renderBackground(mStack);
drawString(mStack, this.font, this.title, this.width / 2, 15, 0xFFFFFF);
super.render(mStack, mouseX, mouseY, partialTicks);
}
@Override
protected void init()
{
int buttonWidth = 150;
int buttonHeight = 20;
int buttonGap = 4;
int buttonSpacing = (buttonHeight + buttonGap);
int buttons = 3;
int xoff = (this.width - buttonWidth);
int yoff = (this.height - buttonHeight - buttonSpacing * (buttons - 1));
int cnt = 0;
xoff = RANDOM.nextInt(xoff);
yoff = RANDOM.nextInt(yoff);
this.addButton(new Button(xoff, yoff + buttonSpacing * (cnt++), buttonWidth, buttonHeight, new StringTextComponent("Push New Layer"), this::pushLayerButton));
this.addButton(new Button(xoff, yoff + buttonSpacing * (cnt++), buttonWidth, buttonHeight, new StringTextComponent("Pop Current Layer"), this::popLayerButton));
this.addButton(new Button(xoff, yoff + buttonSpacing * (cnt++), buttonWidth, buttonHeight, new StringTextComponent("Close entire stack"), this::closeStack));
}
private void closeStack(Button button)
{
this.minecraft.setScreen(null);
}
private void popLayerButton(Button button)
{
this.minecraft.popGuiLayer();
}
private void pushLayerButton(Button button)
{
this.minecraft.pushGuiLayer(new TestLayer(new StringTextComponent("LayerScreen")));
}
}
}
}
+2
View File
@@ -140,5 +140,7 @@ license="LGPL v2.1"
modId="player_attack_knockback_test"
[[mods]]
modId="send_datapacks_to_client"
[[mods]]
modId="gui_layer_test"
# ADD ABOVE THIS LINE