Skip to content

Commit

Permalink
rewrited rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
MeiNanziiii committed Jan 2, 2025
1 parent 5eb7f20 commit c31796b
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 175 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ archives_base_name=mgui
fabric_version=0.110.5+1.21.4
pfu_version=0.2.3+1.21.4
sgui_version=1.8.1+1.21.4
polymer_version=0.11.0+1.21.4-rc1
polymer_version=0.11.3+1.21.4
43 changes: 0 additions & 43 deletions src/main/java/ua/mei/mgui/api/gui/GuiDrawContext.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/java/ua/mei/mgui/api/gui/MGuiHelpers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ua.mei.mgui.api.gui;

import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandlerType;

import java.util.List;

public class MGuiHelpers {
private static final List<ScreenHandlerType<GenericContainerScreenHandler>> genericScreenHandlers = List.of(
ScreenHandlerType.GENERIC_9X1,
ScreenHandlerType.GENERIC_9X2,
ScreenHandlerType.GENERIC_9X3,
ScreenHandlerType.GENERIC_9X4,
ScreenHandlerType.GENERIC_9X5,
ScreenHandlerType.GENERIC_9X6
);

public static boolean isGeneric(ScreenHandlerType<?> screenHandlerType) {
return genericScreenHandlers.contains(screenHandlerType);
}
}
43 changes: 29 additions & 14 deletions src/main/java/ua/mei/mgui/api/gui/TexturedGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,54 @@
import eu.pb4.sgui.api.gui.SimpleGui;
import eu.pb4.sgui.virtual.SguiScreenHandlerFactory;
import eu.pb4.sgui.virtual.inventory.VirtualScreenHandler;
import net.minecraft.network.packet.s2c.play.OpenScreenS2CPacket;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import ua.mei.mgui.impl.packet.MGuiOpenScreenPacket;
import ua.mei.mgui.mixin.SimpleGuiAccessor;
import org.jetbrains.annotations.Nullable;
import ua.mei.mgui.impl.MGuiDrawContext;

import java.util.OptionalInt;

public abstract class TexturedGui extends SimpleGui {
public Text customText = Text.empty();
public @Nullable Text customTitle;
private @Nullable Text mguiTitle;

public TexturedGui(ScreenHandlerType<GenericContainerScreenHandler> type, ServerPlayerEntity player, boolean manipulatePlayerSlots) {
super(type, player, manipulatePlayerSlots);
}

public abstract void drawGui(GuiDrawContext context);
public abstract void draw(MGuiDrawContext context);

@Override
public void setTitle(Text title) {
((SimpleGuiAccessor) this).titleSet(title);
this.mguiTitle = title;

if (this.isOpen()) {
this.player.networkHandler.connection.send(new MGuiOpenScreenPacket(this.syncId, this.type, title));
this.player.networkHandler.sendPacket(new OpenScreenS2CPacket(this.syncId, this.type, title) {
@Override
public Text getName() {
return title;
}
});
this.screenHandler.syncState();
}
}

@Override
public void onTick() {
GuiDrawContext context = new GuiDrawContext();
MGuiDrawContext context = new MGuiDrawContext();

drawGui(context);
draw(context);

Text text = context.render(this);
Text title = context.render(this);

if (this.isOpen() && !text.equals(this.getTitle())) {
setTitle(text);
if (this.isOpen() && !title.equals(this.mguiTitle)) {
setTitle(title);
}
}

Expand All @@ -67,11 +74,19 @@ protected boolean sendGui() {
this.player.sendMessage(Text.translatable("container.spectatorCantOpen").formatted(Formatting.RED), true);
}
} else {
GuiDrawContext context = new GuiDrawContext();
MGuiDrawContext context = new MGuiDrawContext();

draw(context);

drawGui(context);
Text title = context.render(this);
this.mguiTitle = title;

this.player.networkHandler.connection.send(new MGuiOpenScreenPacket(screenHandler.syncId, screenHandler.getType(), context.render(this)));
this.player.networkHandler.sendPacket(new OpenScreenS2CPacket(screenHandler.syncId, screenHandler.getType(), title) {
@Override
public Text getName() {
return title;
}
});
this.player.onScreenHandlerOpened(screenHandler);
this.player.currentScreenHandler = screenHandler;

Expand Down
9 changes: 0 additions & 9 deletions src/main/java/ua/mei/mgui/api/gui/element/Elements.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/ua/mei/mgui/api/gui/element/GlyphElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ua.mei.mgui.api.gui.element;

import net.minecraft.text.Text;
import ua.mei.pfu.api.BitmapGlyph;
import ua.mei.pfu.api.FontResource;

public class GlyphElement extends MGuiElement {
public final BitmapGlyph glyph;

public GlyphElement(BitmapGlyph glyph) {
this.glyph = glyph;
}

public GlyphElement(String path, int height, int y, FontResource resource) {
this(resource.requestGlyph(path, height, y + 13));
}

@Override
public Text render() {
return this.glyph.value;
}

@Override
public int getWidth() {
return this.glyph.glyphWidth;
}
}
17 changes: 0 additions & 17 deletions src/main/java/ua/mei/mgui/api/gui/element/GuiTexture.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import net.minecraft.text.Text;

public abstract class GuiElement {
public int x = 0;

public abstract class MGuiElement {
public abstract Text render();

public abstract int getWidth();
}
33 changes: 15 additions & 18 deletions src/main/java/ua/mei/mgui/api/gui/element/VanillaElements.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
package ua.mei.mgui.api.gui.element;

import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import ua.mei.mgui.impl.MGuiImpl;
import ua.mei.pfu.api.FontResource;

@ApiStatus.Internal
public class VanillaElements {
private static final FontResource resource = MGuiImpl.manager.requestFont("gui/vanilla");

public static final GuiElement GENERIC_9X1 = Elements.texture("gui/container/generic_9x1.png", 256, 0, resource);
public static final GuiElement GENERIC_9X2 = Elements.texture("gui/container/generic_9x2.png", 256, 0, resource);
public static final GuiElement GENERIC_9X3 = Elements.texture("gui/container/generic_9x3.png", 256, 0, resource);
public static final GuiElement GENERIC_9X4 = Elements.texture("gui/container/generic_9x4.png", 256, 0, resource);
public static final GuiElement GENERIC_9X5 = Elements.texture("gui/container/generic_9x5.png", 256, 0, resource);
public static final GuiElement GENERIC_9X6 = Elements.texture("gui/container/generic_9x6.png", 256, 0, resource);
public static final MGuiElement GENERIC_9X1 = new GlyphElement("gui/container/generic_9x1.png", 256, 0, resource);
public static final MGuiElement GENERIC_9X2 = new GlyphElement("gui/container/generic_9x2.png", 256, 0, resource);
public static final MGuiElement GENERIC_9X3 = new GlyphElement("gui/container/generic_9x3.png", 256, 0, resource);
public static final MGuiElement GENERIC_9X4 = new GlyphElement("gui/container/generic_9x4.png", 256, 0, resource);
public static final MGuiElement GENERIC_9X5 = new GlyphElement("gui/container/generic_9x5.png", 256, 0, resource);
public static final MGuiElement GENERIC_9X6 = new GlyphElement("gui/container/generic_9x6.png", 256, 0, resource);

public static void load() {

}

public static Text fromScreenHandler(ScreenHandlerType<?> handler) {
public static @Nullable MGuiElement fromScreenHandler(ScreenHandlerType<?> handler) {
if (handler == ScreenHandlerType.GENERIC_9X1) {
return GENERIC_9X1.render();
return GENERIC_9X1;
} else if (handler == ScreenHandlerType.GENERIC_9X2) {
return GENERIC_9X2.render();
return GENERIC_9X2;
} else if (handler == ScreenHandlerType.GENERIC_9X3) {
return GENERIC_9X3.render();
return GENERIC_9X3;
} else if (handler == ScreenHandlerType.GENERIC_9X4) {
return GENERIC_9X4.render();
return GENERIC_9X4;
} else if (handler == ScreenHandlerType.GENERIC_9X5) {
return GENERIC_9X5.render();
return GENERIC_9X5;
} else if (handler == ScreenHandlerType.GENERIC_9X6) {
return GENERIC_9X6.render();
return GENERIC_9X6;
} else {
return Text.empty();
return null;
}
}
}
58 changes: 58 additions & 0 deletions src/main/java/ua/mei/mgui/impl/MGuiDrawContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ua.mei.mgui.impl;

import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.ApiStatus;
import ua.mei.mgui.api.gui.TexturedGui;
import ua.mei.mgui.api.gui.element.MGuiElement;
import ua.mei.pfu.api.util.TextBuilder;

import java.util.ArrayList;
import java.util.List;

@ApiStatus.Internal
public class MGuiDrawContext {
private final List<Object> elements = new ArrayList<>();
private int x = 0;

public MGuiDrawContext() {
}

public void x(int x) {
this.x = x;
}

public void draw(MGuiElement element) {
if (!this.elements.isEmpty() && this.elements.getLast() instanceof Integer space) {
this.elements.set(this.elements.size() - 1, space + this.x);
} else {
this.elements.add(this.x - 8);
}
this.elements.add(element);
this.elements.add(-element.getWidth() - this.x - 1);
}

public Text render(TexturedGui gui) {
TextBuilder builder = new TextBuilder();

if (gui.customTitle == null && !this.elements.isEmpty()) {
this.elements.removeLast();
}

this.elements.forEach(obj -> {
if (obj instanceof Integer space && space != 0) {
builder.space(space);
} else if (obj instanceof MGuiElement element) {
builder.text(((MutableText) element.render()).formatted(Formatting.WHITE));
}
});

if (gui.customTitle != null) {
builder.space(8);
builder.text(gui.customTitle);
}

return builder.build();
}
}
12 changes: 0 additions & 12 deletions src/main/java/ua/mei/mgui/impl/MGuiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@

import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.api.ModInitializer;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import org.jetbrains.annotations.ApiStatus;
import ua.mei.mgui.api.gui.element.VanillaElements;
import ua.mei.pfu.api.FontResourceManager;
import ua.mei.pfu.api.util.FontSpaceUtils;

import java.util.List;

@ApiStatus.Internal
public class MGuiImpl implements ModInitializer {
public static final String MOD_ID = "mgui";
public static final FontResourceManager manager = FontResourceManager.create(MOD_ID);
public static final List<ScreenHandlerType<GenericContainerScreenHandler>> genericScreenHandlers = List.of(
ScreenHandlerType.GENERIC_9X1,
ScreenHandlerType.GENERIC_9X2,
ScreenHandlerType.GENERIC_9X3,
ScreenHandlerType.GENERIC_9X4,
ScreenHandlerType.GENERIC_9X5,
ScreenHandlerType.GENERIC_9X6
);

@Override
public void onInitialize() {
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/ua/mei/mgui/impl/packet/MGuiOpenScreenPacket.java

This file was deleted.

Loading

0 comments on commit c31796b

Please sign in to comment.