forked from CleanroomMC/ModularUI
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detach Mui from GuiScreenWrapper (CleanroomMC#64)
* detach GuiScreenWrapper * fix mouse input * slightly scuffed gui overlays * fix text widget for unusual scales * lots of small stuff * setup tests & own matrix and vector impl * dont use deprecated field * move overlay test to own class * test button overlapping * allow creating custom gui wrappers from UIFactory * helpers & javadoc for ui factories
- Loading branch information
Showing
65 changed files
with
2,941 additions
and
557 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/com/cleanroommc/modularui/api/IMuiScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.cleanroommc.modularui.api; | ||
|
||
import com.cleanroommc.modularui.core.mixin.GuiContainerAccessor; | ||
import com.cleanroommc.modularui.screen.ClientScreenHandler; | ||
import com.cleanroommc.modularui.screen.ModularScreen; | ||
|
||
import net.minecraft.client.gui.GuiScreen; | ||
import net.minecraft.client.gui.inventory.GuiContainer; | ||
import net.minecraft.inventory.Slot; | ||
|
||
import net.minecraftforge.fml.relauncher.Side; | ||
import net.minecraftforge.fml.relauncher.SideOnly; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.awt.*; | ||
import java.util.function.IntConsumer; | ||
|
||
/** | ||
* Implement this interface on a {@link GuiScreen} to be able to use it as a custom wrapper. | ||
* The GuiScreen should have final {@link ModularScreen} field, which is set from the constructor. | ||
* Additionally, the GuiScreen MUST call {@link ModularScreen#construct(IMuiScreen)} in its constructor. | ||
* See {@link com.cleanroommc.modularui.screen.GuiScreenWrapper GuiScreenWrapper} and {@link com.cleanroommc.modularui.screen.GuiContainerWrapper GuiContainerWrapper} | ||
* for default implementations. | ||
*/ | ||
@SideOnly(Side.CLIENT) | ||
public interface IMuiScreen { | ||
|
||
/** | ||
* Returns the {@link ModularScreen} that is being wrapped. This should return a final instance field. | ||
* | ||
* @return the wrapped modular screen | ||
*/ | ||
@NotNull | ||
ModularScreen getScreen(); | ||
|
||
/** | ||
* {@link GuiScreen GuiScreens} need to be focused when a text field is focused, to prevent key input from | ||
* behaving unexpectedly. | ||
* | ||
* @param focused if the screen should be focused | ||
*/ | ||
default void setFocused(boolean focused) { | ||
getGuiScreen().setFocused(focused); | ||
} | ||
|
||
/** | ||
* This method decides how the gui background is drawn. | ||
* The intended usage is to override {@link GuiScreen#drawWorldBackground(int)} and call this method | ||
* with the super method reference as the second parameter. | ||
* | ||
* @param tint background color tint | ||
* @param drawFunction a method reference to draw the world background normally with the tint as the parameter | ||
*/ | ||
@ApiStatus.NonExtendable | ||
default void handleDrawBackground(int tint, IntConsumer drawFunction) { | ||
if (ClientScreenHandler.shouldDrawWorldBackground()) { | ||
drawFunction.accept(tint); | ||
} | ||
ClientScreenHandler.drawDarkBackground(getGuiScreen(), tint); | ||
} | ||
|
||
/** | ||
* This method is called every time the {@link ModularScreen} resizes. | ||
* This usually only affects {@link GuiContainer GuiContainers}. | ||
* | ||
* @param area area of the main panel | ||
*/ | ||
default void updateGuiArea(Rectangle area) { | ||
if (getGuiScreen() instanceof GuiContainer container) { | ||
ClientScreenHandler.updateGuiArea(container, area); | ||
} | ||
} | ||
|
||
/** | ||
* @return if this wrapper is a {@link GuiContainer} | ||
*/ | ||
@ApiStatus.NonExtendable | ||
default boolean isGuiContainer() { | ||
return getGuiScreen() instanceof GuiContainer; | ||
} | ||
|
||
/** | ||
* Hovering widget is handled by {@link com.cleanroommc.modularui.screen.viewport.GuiContext}. | ||
* If it detects a slot, this method is called. Only affects {@link GuiContainer GuiContainers}. | ||
* | ||
* @param slot hovered slot | ||
*/ | ||
@ApiStatus.NonExtendable | ||
default void setHoveredSlot(Slot slot) { | ||
if (getGuiScreen() instanceof GuiContainerAccessor acc) { | ||
acc.setHoveredSlot(slot); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the {@link GuiScreen} that wraps the {@link ModularScreen}. | ||
* In most cases this does not need to be overridden as this interfaces should be implemented on {@link GuiScreen GuiScreens}. | ||
* | ||
* @return the wrapping gui screen | ||
*/ | ||
default GuiScreen getGuiScreen() { | ||
return (GuiScreen) this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.cleanroommc.modularui.api; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.entity.EntityPlayerSP; | ||
import net.minecraft.client.gui.GuiScreen; | ||
|
||
public class MCHelper { | ||
|
||
public static boolean hasMc() { | ||
return getMc() != null; | ||
} | ||
|
||
public static Minecraft getMc() { | ||
return Minecraft.getMinecraft(); | ||
} | ||
|
||
public static EntityPlayerSP getPlayer() { | ||
if (hasMc()) { | ||
return getMc().player; | ||
} | ||
return null; | ||
} | ||
|
||
public static boolean closeScreen() { | ||
if (!hasMc()) return false; | ||
EntityPlayerSP player = Minecraft.getMinecraft().player; | ||
if (player != null) { | ||
player.closeScreen(); | ||
return true; | ||
} | ||
Minecraft.getMinecraft().displayGuiScreen(null); | ||
return false; | ||
} | ||
|
||
public static boolean displayScreen(GuiScreen screen) { | ||
Minecraft mc = getMc(); | ||
if (mc != null) { | ||
mc.displayGuiScreen(screen); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static GuiScreen getCurrentScreen() { | ||
Minecraft mc = getMc(); | ||
return mc != null ? mc.currentScreen : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/cleanroommc/modularui/api/layout/IViewportStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/cleanroommc/modularui/core/mixin/GuiAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.cleanroommc.modularui.core.mixin; | ||
|
||
import net.minecraft.client.gui.Gui; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
@Mixin(Gui.class) | ||
public interface GuiAccessor { | ||
|
||
@Accessor | ||
float getZLevel(); | ||
|
||
@Accessor | ||
void setZLevel(float z); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/cleanroommc/modularui/core/mixin/GuiButtonMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.cleanroommc.modularui.core.mixin; | ||
|
||
import com.cleanroommc.modularui.overlay.OverlayStack; | ||
|
||
import net.minecraft.client.gui.GuiButton; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
/** | ||
* This mixin fixes some visual bugs that can happen with overlays. | ||
*/ | ||
@Mixin(GuiButton.class) | ||
public abstract class GuiButtonMixin { | ||
|
||
@Shadow protected boolean hovered; | ||
|
||
@Shadow | ||
protected abstract int getHoverState(boolean mouseOver); | ||
|
||
@Redirect(method = "drawButton", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiButton;getHoverState(Z)I")) | ||
public int draw(GuiButton instance, boolean mouseOver) { | ||
// fixes buttons being hovered when an overlay element is already hovered | ||
if (this.hovered) this.hovered = !OverlayStack.isHoveringOverlay(); | ||
return getHoverState(this.hovered); | ||
} | ||
} |
Oops, something went wrong.