Skip to content

Commit

Permalink
- Redo the texture management (may break existing APIs).
Browse files Browse the repository at this point in the history
- Fix the issue that `MMWorldEventListener#isAreaChanged` does not return the correct value properly.
  • Loading branch information
KasumiNova committed Jul 30, 2024
1 parent 93360ff commit 4a04cd0
Show file tree
Hide file tree
Showing 17 changed files with 409 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import appeng.client.gui.widgets.GuiCustomSlot;
import appeng.fluids.client.gui.widgets.GuiFluidTank;
import github.kasuminova.mmce.client.gui.slot.GuiFullCapFluidTank;
import github.kasuminova.mmce.client.gui.util.TextureProperties;
import github.kasuminova.mmce.client.gui.widget.Button;
import github.kasuminova.mmce.client.gui.widget.Button4State;
import github.kasuminova.mmce.client.gui.widget.ButtonElements;
Expand Down Expand Up @@ -84,9 +85,9 @@ public GuiMEPatternProvider(final MEPatternProvider owner, final EntityPlayer pl
// Init ReturnItems...
Button4State returnItems = new Button4State();
returnItems
.setMouseDownTextureXY(176 + 18 + 18, 214)
.setHoveredTextureXY(176 + 18, 214)
.setTextureXY(176, 214)
.setMouseDownTexture(176 + 18 + 18, 214)
.setHoveredTexture(176 + 18, 214)
.setTexture(176, 214)
.setTextureLocation(GuiMEPatternProvider.GUI_TEXTURE)
.setTooltipFunction((btn) -> {
List<String> tooltips = new ArrayList<>();
Expand All @@ -100,17 +101,17 @@ public GuiMEPatternProvider(final MEPatternProvider owner, final EntityPlayer pl
// Init WorkModeSetting...
workModeSetting
// ButtonTexture 1
.addElement(MEPatternProvider.WorkModeSetting.DEFAULT, 140, 196, GuiMEPatternProvider.GUI_TEXTURE)
.addElement(MEPatternProvider.WorkModeSetting.DEFAULT, TextureProperties.of(140, 196, 16, 16))
// ButtonTexture 2
.addElement(MEPatternProvider.WorkModeSetting.BLOCKING_MODE, 140 + 18, 196, GuiMEPatternProvider.GUI_TEXTURE)
.addElement(MEPatternProvider.WorkModeSetting.BLOCKING_MODE, TextureProperties.of(140 + 18, 196, 16, 16))
// ButtonTexture 3
.addElement(MEPatternProvider.WorkModeSetting.CRAFTING_LOCK_MODE, 140 + 18 + 18, 196, GuiMEPatternProvider.GUI_TEXTURE)
.addElement(MEPatternProvider.WorkModeSetting.CRAFTING_LOCK_MODE, TextureProperties.of(140 + 18 + 18, 196, 16, 16))
// ButtonTexture 5
.setMouseDownTextureXY(140 + 18 + 18 + 18 + 18 + 18, 196)
.setMouseDownTexture(140 + 18 + 18 + 18 + 18 + 18, 196)
// ButtonTexture 5
.setHoveredTextureXY(140 + 18 + 18 + 18 + 18, 196)
.setHoveredTexture(140 + 18 + 18 + 18 + 18, 196)
// ButtonTexture 4
.setTextureXY(140 + 18 + 18 + 18, 196)
.setTexture(140 + 18 + 18 + 18, 196)
.setTextureLocation(GuiMEPatternProvider.GUI_TEXTURE)
.setTooltipFunction((btn) -> {
MEPatternProvider.WorkModeSetting current = workModeSetting.getCurrentSelection();
Expand Down Expand Up @@ -141,8 +142,8 @@ public GuiMEPatternProvider(final MEPatternProvider owner, final EntityPlayer pl
Button singleInvTip = new Button();
singleInvTip
.setTextureLocation(GuiMEPatternProvider.GUI_TEXTURE)
.setTextureXY(230, 214)
.setHoveredTextureXY(230 + 11, 214)
.setTexture(230, 214)
.setHoveredTexture(230 + 11, 214)
.setTooltipFunction((btn) -> {
List<String> tooltips = new ArrayList<>();
tooltips.add(I18n.format("gui.mepatternprovider.single_inv.desc"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package github.kasuminova.mmce.client.gui.util;

import com.github.bsideup.jabel.Desugar;
import com.google.common.base.Preconditions;
import github.kasuminova.mmce.client.gui.widget.base.WidgetGui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Optional;
import java.util.function.Consumer;

@Desugar
@ParametersAreNonnullByDefault
@SuppressWarnings("unused")
public record TextureProperties(@Nullable ResourceLocation texRes, int texX, int texY, int width, int height) {

public static final TextureProperties EMPTY = new TextureProperties(null, 0, 0, 0, 0);

public static TextureProperties of(@Nullable final ResourceLocation texRes, final int texX, final int texY, final int width, final int height) {
return new TextureProperties(texRes, texX, texY, width, height);
}

public static TextureProperties of(final int texX, final int texY, final int width, final int height) {
return of(null, texX, texY, width, height);
}

public static TextureProperties of(final int texX, final int texY, final int widthHeight) {
return of(null, texX, texY, widthHeight, widthHeight);
}

public static TextureProperties of(@Nullable final ResourceLocation texRes, final int texX, final int texY) {
return of(texRes, texX, texY, 0, 0);
}

public static TextureProperties of(final int texX, final int texY) {
return of(null, texX, texY);
}

public void bind(final TextureManager textureManager) {
if (texRes != null) {
bind(textureManager, texRes);
}
}

private static void bind(final TextureManager textureManager, final ResourceLocation texRes) {
textureManager.bindTexture(texRes);
}

// Default render

public void render(final RenderPos renderPos, final WidgetGui gui) {
render(renderPos, gui.getGui());
}

public void render(final RenderPos renderPos, final GuiScreen gui) {
bind(gui.mc.getTextureManager());
gui.drawTexturedModalRect(renderPos.posX(), renderPos.posY(), texX, texY, width, height);
}

public void renderIfPresent(final RenderPos renderPos, final WidgetGui gui) {
ifPresent(t -> t.render(renderPos, gui));
}

public void renderIfPresent(final RenderPos renderPos, final GuiScreen gui) {
ifPresent(t -> t.render(renderPos, gui));
}

public void renderIfPresent(final RenderPos renderPos, final WidgetGui gui, final Consumer<TextureProperties> before) {
ifPresent(t -> before.andThen(t1 -> t.render(renderPos, gui)).accept(t));
}

public void renderIfPresent(final RenderPos renderPos, final GuiScreen gui, final Consumer<TextureProperties> before) {
ifPresent(t -> before.andThen(t1 -> t.render(renderPos, gui)).accept(t));
}

public void renderIfPresent(final RenderPos renderPos, final WidgetGui gui, @Nullable final Consumer<TextureProperties> before, @Nullable final Consumer<TextureProperties> after) {
ifPresent(t -> {
Optional.ofNullable(before).ifPresent(bc -> bc.accept(t));
t.render(renderPos, gui);
Optional.ofNullable(after).ifPresent(ac -> ac.accept(t));
});
}

public void renderIfPresent(final RenderPos renderPos, final GuiScreen gui, @Nullable final Consumer<TextureProperties> before, @Nullable final Consumer<TextureProperties> after) {
ifPresent(t -> {
Optional.ofNullable(before).ifPresent(bc -> bc.accept(t));
t.render(renderPos, gui);
Optional.ofNullable(after).ifPresent(ac -> ac.accept(t));
});
}

public void ifPresent(final Consumer<TextureProperties> runnable) {
if (texRes != null && width > 0 && height > 0) {
runnable.accept(this);
}
}

// Custom texture render

public void render(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final WidgetGui gui) {
render(customTexRes, renderPos, gui.getGui());
}

public void render(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final GuiScreen gui) {
TextureManager textureManager = gui.mc.getTextureManager();
if (texRes != null) {
bind(textureManager);
} else {
bind(textureManager, Preconditions.checkNotNull(customTexRes, "texRes is null, but customTexRes is null too!"));
}
gui.drawTexturedModalRect(renderPos.posX(), renderPos.posY(), texX, texY, width, height);
}

public void renderIfPresent(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final WidgetGui gui) {
ifSizePresent(t -> t.render(customTexRes, renderPos, gui));
}

public void renderIfPresent(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final GuiScreen gui) {
ifSizePresent(t -> t.render(customTexRes, renderPos, gui));
}

public void renderIfPresent(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final WidgetGui gui, final Consumer<TextureProperties> before) {
ifSizePresent(t -> before.andThen(t1 -> t.render(customTexRes, renderPos, gui)).accept(t));
}

public void renderIfPresent(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final GuiScreen gui, final Consumer<TextureProperties> before) {
ifSizePresent(t -> before.andThen(t1 -> t.render(customTexRes, renderPos, gui)).accept(t));
}

public void renderIfPresent(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final WidgetGui gui, @Nullable final Consumer<TextureProperties> before, @Nullable final Consumer<TextureProperties> after) {
ifPresent(t -> {
Optional.ofNullable(before).ifPresent(bc -> bc.accept(t));
t.render(customTexRes, renderPos, gui);
Optional.ofNullable(after).ifPresent(ac -> ac.accept(t));
});
}

public void renderIfPresent(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final GuiScreen gui, @Nullable final Consumer<TextureProperties> before, @Nullable final Consumer<TextureProperties> after) {
ifPresent(t -> {
Optional.ofNullable(before).ifPresent(bc -> bc.accept(t));
t.render(customTexRes, renderPos, gui);
Optional.ofNullable(after).ifPresent(ac -> ac.accept(t));
});
}

public void ifSizePresent(final Consumer<TextureProperties> runnable) {
if (width > 0 && height > 0) {
runnable.accept(this);
}
}

// Custom texture and size render

public void render(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final RenderSize renderSize, final WidgetGui gui) {
render(customTexRes, renderPos, renderSize, gui.getGui());
}

public void render(@Nullable final ResourceLocation customTexRes, final RenderPos renderPos, final RenderSize renderSize, final GuiScreen gui) {
TextureManager textureManager = gui.mc.getTextureManager();
if (texRes != null) {
bind(textureManager);
} else {
bind(textureManager, Preconditions.checkNotNull(customTexRes, "texRes is null, but customTexRes is null too!"));
}
gui.drawTexturedModalRect(renderPos.posX(), renderPos.posY(), texX, texY, renderSize.width(), renderSize.height());
}

// Custom size render

public void render(final RenderPos renderPos, final RenderSize renderSize, final WidgetGui gui) {
render(renderPos, renderSize, gui.getGui());
}

public void render(final RenderPos renderPos, final RenderSize renderSize, final GuiScreen gui) {
bind(gui.mc.getTextureManager());
gui.drawTexturedModalRect(renderPos.posX(), renderPos.posY(), texX, texY, renderSize.width(), renderSize.height());
}

public void renderIfPresent(final RenderPos renderPos, final RenderSize renderSize, final WidgetGui gui) {
ifTexPresent(t -> t.render(renderPos, renderSize, gui));
}

public void renderIfPresent(final RenderPos renderPos, final RenderSize renderSize, final GuiScreen gui) {
ifTexPresent(t -> t.render(renderPos, renderSize, gui));
}

public void renderIfPresent(final RenderPos renderPos, final RenderSize renderSize, final WidgetGui gui, final Consumer<TextureProperties> before) {
ifTexPresent(t -> before.andThen(t1 -> t.render(renderPos, renderSize, gui)).accept(t));
}

public void renderIfPresent(final RenderPos renderPos, final RenderSize renderSize, final GuiScreen gui, final Consumer<TextureProperties> before) {
ifTexPresent(t -> before.andThen(t1 -> t.render(renderPos, renderSize, gui)).accept(t));
}

public void renderIfPresent(final RenderPos renderPos, final RenderSize renderSize, final WidgetGui gui, @Nullable final Consumer<TextureProperties> before, @Nullable final Consumer<TextureProperties> after) {
ifPresent(t -> {
Optional.ofNullable(before).ifPresent(bc -> bc.accept(t));
t.render(renderPos, renderSize, gui);
Optional.ofNullable(after).ifPresent(ac -> ac.accept(t));
});
}

public void renderIfPresent(final RenderPos renderPos, final RenderSize renderSize, final GuiScreen gui, @Nullable final Consumer<TextureProperties> before, @Nullable final Consumer<TextureProperties> after) {
ifPresent(t -> {
Optional.ofNullable(before).ifPresent(bc -> bc.accept(t));
t.render(renderPos, renderSize, gui);
Optional.ofNullable(after).ifPresent(ac -> ac.accept(t));
});
}

public void ifTexPresent(final Consumer<TextureProperties> runnable) {
if (texRes != null) {
runnable.accept(this);
}
}

}
Loading

0 comments on commit 4a04cd0

Please sign in to comment.