Skip to content

Commit

Permalink
cursed garbage to support moving window during drag
Browse files Browse the repository at this point in the history
  • Loading branch information
BasiqueEvangelist committed Jul 4, 2024
1 parent 20fbdf9 commit 6d2d7df
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/main/java/io/wispforest/owo/ui/window/OwoWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import static org.lwjgl.glfw.GLFW.glfwSetCharModsCallback;

public abstract class OwoWindow<R extends ParentComponent> extends SupportsFeaturesImpl<WindowContext> implements WindowContext {
private static final boolean USE_GLOBAL_POS = glfwGetPlatform() != GLFW_PLATFORM_WAYLAND;

private String title = "owo-ui window";
private int screenWidth = 854;
private int screenHeight = 480;
Expand All @@ -57,10 +59,15 @@ public abstract class OwoWindow<R extends ParentComponent> extends SupportsFeatu

private int mouseX = -1;
private int mouseY = -1;
private int globalMouseX = -1;
private int globalMouseY = -1;
private int deltaX = 0;
private int deltaY = 0;
private int activeButton = -1;

private final int[] globalX = new int[1];
private final int[] globalY = new int[1];

private final EventStream<WindowFramebufferResized> framebufferResizedEvents = WindowFramebufferResized.newStream();

public OwoWindow() {
Expand Down Expand Up @@ -186,11 +193,25 @@ public void open() {
int newX = (int) (xpos / scaleFactor);
int newY = (int) (ypos / scaleFactor);

deltaX += newX - mouseX;
deltaY += newY - mouseY;
if (!USE_GLOBAL_POS) {
this.deltaX += newX - mouseX;
this.deltaY += newY - mouseY;
}

this.mouseY = newY;
this.mouseX = newX;

mouseY = newY;
mouseX = newX;
if (USE_GLOBAL_POS) {
glfwGetWindowPos(handle, this.globalX, this.globalY);
int newGlobalX = (int) ((this.globalX[0] + xpos) / scaleFactor);
int newGlobalY = (int) ((this.globalY[0] + ypos) / scaleFactor);

this.deltaX += newGlobalX - this.globalMouseX;
this.deltaY += newGlobalY - this.globalMouseY;

this.globalMouseX = newGlobalX;
this.globalMouseY = newGlobalY;
}
})));

glfwSetMouseButtonCallback(handle, stowAndReturn(GLFWMouseButtonCallback.create((window, button, action, mods) -> {
Expand Down
52 changes: 52 additions & 0 deletions src/testmod/java/io/wispforest/uwu/client/MoveButtonComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.wispforest.uwu.client;

import io.wispforest.owo.ui.component.ButtonComponent;
import io.wispforest.owo.ui.window.OwoWindow;
import io.wispforest.owo.ui.window.context.CurrentWindowContext;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;

import java.util.function.Consumer;

public class MoveButtonComponent extends ButtonComponent {
private boolean buttoning = false;

public MoveButtonComponent(Text message, Consumer<ButtonComponent> onPress) {
super(message, onPress);
}

@Override
public boolean onMouseDown(double mouseX, double mouseY, int button) {
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT) this.buttoning = true;

return super.onMouseDown(mouseX, mouseY, button);
}

@Override
public boolean onMouseUp(double mouseX, double mouseY, int button) {
buttoning = false;

return super.onMouseUp(mouseX, mouseY, button);
}

@Override
public boolean onMouseDrag(double mouseX, double mouseY, double deltaX, double deltaY, int button) {
if (buttoning) {
var window = (OwoWindow<?>) CurrentWindowContext.current();
int[] windowWidth = new int[1];
int[] windowHeight = new int[1];

GLFW.glfwGetWindowSize(window.handle(), windowWidth, windowHeight);

double factor = window.scaleFactor() * windowWidth[0] / window.framebufferWidth();

int[] windowX = new int[1];
int[] windowY = new int[1];

GLFW.glfwGetWindowPos(window.handle(), windowX, windowY);
GLFW.glfwSetWindowPos(window.handle(), windowX[0] + (int)(deltaX * factor), windowY[0] + (int)(deltaY * factor));
}

return super.onMouseDrag(mouseX, mouseY, deltaX, deltaY, button);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected void build(FlowLayout rootComponent) {
var inner = Containers.verticalFlow(Sizing.content(), Sizing.content());
rootComponent.child(Containers.verticalScroll(Sizing.fill(100), Sizing.fill(100), inner));

inner.child(new MoveButtonComponent(Text.literal("hehe move"), unused -> {}));
inner.child(Components.label(Text.of("Are you an owl?")));

var textbox = Components.textBox(Sizing.fixed(60));
Expand Down

0 comments on commit 6d2d7df

Please sign in to comment.