Skip to content

Commit

Permalink
mmm icons
Browse files Browse the repository at this point in the history
  • Loading branch information
BasiqueEvangelist committed Dec 17, 2023
1 parent fcd8c09 commit c75b98e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
67 changes: 67 additions & 0 deletions src/main/java/io/wispforest/owo/ui/window/FramebufferWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
import net.minecraft.client.gl.GlDebug;
import net.minecraft.client.gl.SimpleFramebuffer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.resource.InputSupplier;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.GL32;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.NativeResource;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -26,9 +35,12 @@
public class FramebufferWindow extends SupportsFeaturesImpl<WindowContext> implements AutoCloseable, WindowContext {
private int width;
private int height;

private final long handle;

private Framebuffer framebuffer;
private int localFramebuffer = 0;

protected final MinecraftClient client = MinecraftClient.getInstance();

private final List<NativeResource> disposeList;
Expand Down Expand Up @@ -234,6 +246,61 @@ protected void sizeChanged(long handle, int width, int height) {
framebufferResizedEvents.sink().onFramebufferResized(width, height);
}

public static boolean supportsIcons() {
int platform = GLFW.glfwGetPlatform();
return platform == GLFW_PLATFORM_WIN32 || platform == GLFW_PLATFORM_X11;
}

public void setIconTextures(ResourceManager manager, List<Identifier> iconIds) {
if (!supportsIcons()) return;

List<NativeImage> iconImages = new ArrayList<>(iconIds.size());

try {
for (Identifier iconId : iconIds) {
var icon = manager.getResource(iconId).orElse(null);

if (icon == null) continue;

try {
iconImages.add(NativeImage.read(icon.getInputStream()));
} catch (IOException e) {
throw new RuntimeException("Couldn't open icon " + iconId, e);
}
}

setIcon(iconImages);
} finally {
iconImages.forEach(NativeImage::close);
}
}

public void setIcon(List<NativeImage> icons) {
if (!supportsIcons()) return;

List<ByteBuffer> freeList = new ArrayList<>(icons.size());
try (MemoryStack memoryStack = MemoryStack.stackPush()) {
GLFWImage.Buffer buffer = GLFWImage.malloc(icons.size(), memoryStack);

for (int i = 0; i < icons.size(); i++) {
NativeImage icon = icons.get(i);
ByteBuffer imgBuffer = MemoryUtil.memAlloc(icon.getWidth() * icon.getHeight() * 4);
freeList.add(imgBuffer);
imgBuffer.asIntBuffer().put(icon.copyPixelsRgba());

buffer
.position(i)
.width(icon.getWidth())
.height(icon.getHeight())
.pixels(imgBuffer);
}

GLFW.glfwSetWindowIcon(this.handle, buffer.position(0));
} finally {
freeList.forEach(MemoryUtil::memFree);
}
}

public boolean closed() {
return this.disposeList.isEmpty();
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/wispforest/owo/ui/window/OwoWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ public OwoWindow(int width, int height, String name, long parentContext) {

adapter.keyPressed(keyCode, scanCode, modifiers);
});
charTyped().subscribe((chr, modifiers) -> {
return adapter.charTyped(chr, modifiers);
});
charTyped().subscribe(adapter::charTyped);
}

protected abstract OwoUIAdapter<R> createAdapter();
Expand Down
13 changes: 6 additions & 7 deletions src/testmod/java/io/wispforest/uwu/client/UwuTestWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

import java.util.List;

public class UwuTestWindow extends OwoWindow<FlowLayout> {
public UwuTestWindow() {
super(640, 480, "uωu test window!", MinecraftClient.getInstance().getWindow().getHandle());

setIconTextures(MinecraftClient.getInstance().getResourceManager(), List.of(new Identifier("owo", "icon.png")));
}

@Override
Expand All @@ -29,7 +34,7 @@ protected void build(FlowLayout rootComponent) {
rootComponent.padding(Insets.of(10));

var inner = Containers.verticalFlow(Sizing.content(), Sizing.content());
rootComponent.child(Containers.verticalScroll(Sizing.content(), Sizing.fill(100), inner));
rootComponent.child(Containers.verticalScroll(Sizing.fill(100), Sizing.fill(100), inner));

inner.child(Components.label(Text.of("Are you an owl?")));

Expand All @@ -46,12 +51,6 @@ protected void build(FlowLayout rootComponent) {
}
});

inner.child(Containers.renderEffect(Components.label(Text.literal("breh!")))
.<RenderEffectWrapper<LabelComponent>>configure(component -> {
component.effect(RenderEffectWrapper.RenderEffect.rotate(45));
component.effect(RenderEffectWrapper.RenderEffect.color(Color.BLUE));
}));

for (int i = 0; i < 100; i++) {
inner.child(Components.label(Text.of("breh!")));
}
Expand Down

0 comments on commit c75b98e

Please sign in to comment.