Skip to content

Commit

Permalink
commit so that I can resume working on my desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
BasiqueEvangelist committed Aug 20, 2023
1 parent dfd3195 commit 1d29b1d
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 114 deletions.
8 changes: 6 additions & 2 deletions src/main/java/io/wispforest/owo/mixin/ui/ScreenMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.wispforest.owo.mixin.ui;

import io.wispforest.owo.ui.window.CurrentWindowContext;
import io.wispforest.owo.ui.window.context.CurrentWindowContext;
import io.wispforest.owo.ui.window.context.VanillaWindowContext;
import io.wispforest.owo.ui.window.context.WindowContext;
import net.minecraft.client.gui.screen.Screen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -10,6 +12,8 @@
public class ScreenMixin {
@ModifyArg(method = {"hasShiftDown", "hasControlDown", "hasAltDown"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/InputUtil;isKeyPressed(JI)Z"), index = 0)
private static long replaceOnOtherWindow(long handle) {
return CurrentWindowContext.isMain() ? handle : CurrentWindowContext.handle();
WindowContext ctx = CurrentWindowContext.current();

return ctx != VanillaWindowContext.MAIN ? ctx.handle() : handle;
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/wispforest/owo/ui/core/OwoUIAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.wispforest.owo.Owo;
import io.wispforest.owo.renderdoc.RenderDoc;
import io.wispforest.owo.ui.util.CursorAdapter;
import io.wispforest.owo.ui.window.CurrentWindowContext;
import io.wispforest.owo.ui.window.context.CurrentWindowContext;
import io.wispforest.owo.ui.window.OwoWindow;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -190,7 +190,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic
RenderSystem.enableDepthTest();
GlStateManager._enableScissorTest();

GlStateManager._scissorBox(0, 0, CurrentWindowContext.framebufferWidth(), CurrentWindowContext.framebufferHeight());
GlStateManager._scissorBox(0, 0, CurrentWindowContext.current().framebufferWidth(), CurrentWindowContext.current().framebufferHeight());
this.rootComponent.draw(owoContext, mouseX, mouseY, partialTicks, delta);

GlStateManager._disableScissorTest();
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/io/wispforest/owo/ui/util/ScissorStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.mojang.blaze3d.systems.RenderSystem;
import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.core.PositionedRectangle;
import io.wispforest.owo.ui.window.CurrentWindowContext;
import io.wispforest.owo.ui.window.context.CurrentWindowContext;
import io.wispforest.owo.ui.window.context.WindowContext;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.util.math.MatrixStack;
import org.jetbrains.annotations.Nullable;
Expand All @@ -23,12 +24,12 @@ public final class ScissorStack {
private ScissorStack() {}

public static void pushDirect(int x, int y, int width, int height) {
var window = MinecraftClient.getInstance().getWindow();
var scale = window.getScaleFactor();
var ctx = CurrentWindowContext.current();
var scale = ctx.scaleFactor();

push(
(int) (x / scale),
(int) (CurrentWindowContext.scaledHeight() - (y / scale) - height / scale),
(int) (ctx.scaledHeight() - (y / scale) - height / scale),
(int) (width / scale),
(int) (height / scale),
null
Expand Down Expand Up @@ -59,18 +60,20 @@ public static void pop() {
}

private static void applyState() {
WindowContext ctx = CurrentWindowContext.current();

if (STACK.isEmpty()) {
GL11.glScissor(0, 0, CurrentWindowContext.framebufferWidth(), CurrentWindowContext.framebufferHeight());
GL11.glScissor(0, 0, ctx.framebufferWidth(), ctx.framebufferHeight());
return;
}
if (!GL11.glIsEnabled(GL11.GL_SCISSOR_TEST)) return;

var newFrame = STACK.peek();
var scale = CurrentWindowContext.scaleFactor();
var scale = ctx.scaleFactor();

GL11.glScissor(
(int) (newFrame.x() * scale),
(int) (CurrentWindowContext.framebufferHeight() - (newFrame.y() * scale) - newFrame.height() * scale),
(int) (ctx.framebufferHeight() - (newFrame.y() * scale) - newFrame.height() * scale),
(int) (newFrame.width() * scale),
(int) (newFrame.height() * scale)
);
Expand Down

This file was deleted.

38 changes: 29 additions & 9 deletions src/main/java/io/wispforest/owo/ui/window/FramebufferWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.systems.RenderSystem;
import io.wispforest.owo.ui.event.CharTyped;
import io.wispforest.owo.ui.window.context.WindowContext;
import io.wispforest.owo.util.EventSource;
import io.wispforest.owo.util.EventStream;
import io.wispforest.owo.util.OwoGlfwUtil;
Expand All @@ -19,7 +20,7 @@

import static org.lwjgl.glfw.GLFW.*;

public class FramebufferWindow implements AutoCloseable {
public class FramebufferWindow implements AutoCloseable, WindowContext {
private int width;
private int height;
private final long handle;
Expand All @@ -30,7 +31,7 @@ public class FramebufferWindow implements AutoCloseable {
private final List<NativeResource> disposeList;

private final EventStream<WindowClosed> windowClosedEvents = WindowClosed.newStream();
private final EventStream<WindowResized> windowResizedEvents = WindowResized.newStream();
private final EventStream<WindowFramebufferResized> framebufferResizedEvents = WindowFramebufferResized.newStream();
private final EventStream<WindowMouseMoved> mouseMovedEvents = WindowMouseMoved.newStream();
private final EventStream<WindowMouseButton> mouseButtonEvents = WindowMouseButton.newStream();
private final EventStream<WindowMouseScrolled> mouseScrolledEvents = WindowMouseScrolled.newStream();
Expand Down Expand Up @@ -98,18 +99,36 @@ private <T extends NativeResource> T stowAndReturn(T resource) {
return resource;
}

public int width() {
public Framebuffer framebuffer() {
return framebuffer;
}

@Override
public int framebufferWidth() {
return width;
}

public int height() {
@Override
public int framebufferHeight() {
return height;
}

public Framebuffer framebuffer() {
return framebuffer;
@Override
public int scaledWidth() {
return framebufferWidth();
}

@Override
public int scaledHeight() {
return framebufferHeight();
}

@Override
public double scaleFactor() {
return 1;
}

@Override
public long handle() {
return handle;
}
Expand All @@ -118,8 +137,9 @@ public EventSource<WindowClosed> windowClosed() {
return windowClosedEvents.source();
}

public EventSource<WindowResized> windowResized() {
return windowResizedEvents.source();
@Override
public EventSource<WindowFramebufferResized> framebufferResized() {
return framebufferResizedEvents.source();
}

public EventSource<WindowMouseMoved> mouseMoved() {
Expand Down Expand Up @@ -192,7 +212,7 @@ protected void sizeChanged(long handle, int width, int height) {

initLocalFramebuffer();

windowResizedEvents.sink().onWindowResized(width, height);
framebufferResizedEvents.sink().onFramebufferResized(width, height);
}

public boolean closed() {
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/io/wispforest/owo/ui/window/OwoWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.blaze3d.systems.VertexSorter;
import io.wispforest.owo.ui.core.OwoUIAdapter;
import io.wispforest.owo.ui.core.ParentComponent;
import io.wispforest.owo.ui.window.context.CurrentWindowContext;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.DiffuseLighting;
Expand Down Expand Up @@ -34,7 +35,7 @@ public OwoWindow(int width, int height, String name, long parentContext) {
this.registration = OpenWindows.add(this);

windowClosed().subscribe(this::close);
windowResized().subscribe((newWidth, newHeight) -> {
framebufferResized().subscribe((newWidth, newHeight) -> {
try (var ignored = CurrentWindowContext.setCurrent(this)) {
recalculateScale();
adapter.moveAndResize(0, 0, scaledWidth(), scaledHeight());
Expand Down Expand Up @@ -86,10 +87,10 @@ public void recalculateScale() {

while (
factor != guiScale
&& factor < this.width()
&& factor < this.height()
&& this.width() / (factor + 1) >= 320
&& this.height() / (factor + 1) >= 240
&& factor < this.framebufferWidth()
&& factor < this.framebufferHeight()
&& this.framebufferWidth() / (factor + 1) >= 320
&& this.framebufferHeight() / (factor + 1) >= 240
) {
++factor;
}
Expand All @@ -99,8 +100,8 @@ public void recalculateScale() {
}

this.scaleFactor = factor;
this.scaledWidth = (int) Math.ceil((double) this.width() / scaleFactor);
this.scaledHeight = (int) Math.ceil((double) this.height() / scaleFactor);
this.scaledWidth = (int) Math.ceil((double) this.framebufferWidth() / scaleFactor);
this.scaledHeight = (int) Math.ceil((double) this.framebufferHeight() / scaleFactor);
}

public void render() {
Expand Down Expand Up @@ -143,14 +144,17 @@ public void render() {
present();
}

public int scaleFactor() {
@Override
public double scaleFactor() {
return scaleFactor;
}

@Override
public int scaledWidth() {
return scaledWidth;
}

@Override
public int scaledHeight() {
return scaledHeight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import io.wispforest.owo.util.EventStream;

public interface WindowResized {
void onWindowResized(int newWidth, int newHeight);
public interface WindowFramebufferResized {
void onFramebufferResized(int newWidth, int newHeight);

static EventStream<WindowResized> newStream() {
static EventStream<WindowFramebufferResized> newStream() {
return new EventStream<>(subscribers -> (newWidth, newHeight) -> {
for (var subscriber : subscribers) {
subscriber.onWindowResized(newWidth, newHeight);
subscriber.onFramebufferResized(newWidth, newHeight);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.wispforest.owo.ui.window.context;

import net.minecraft.client.MinecraftClient;

public final class CurrentWindowContext {
private static WindowContext CURRENT = VanillaWindowContext.MAIN;

private CurrentWindowContext() {

}

public static WindowResetter setCurrent(WindowContext window) {
var old = CURRENT;
CURRENT = window;
return new WindowResetter(old);
}

public static WindowContext current() {
return CURRENT;
}

public static class WindowResetter implements AutoCloseable {
private final WindowContext window;

private WindowResetter(WindowContext window) {
this.window = window;
}

@Override
public void close() {
CurrentWindowContext.CURRENT = window;
}
}
}
Loading

0 comments on commit 1d29b1d

Please sign in to comment.