Skip to content

Commit

Permalink
Update event
Browse files Browse the repository at this point in the history
  • Loading branch information
hackermdch committed Sep 21, 2024
1 parent 18a3704 commit 0c3c7db
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class FlutterInstance implements AutoCloseable {
public final long handle;
public final Rect rect;
public int width, height;
public boolean pressed;

public FlutterInstance(String assets, Rect rect, int width, int height) {
handle = FlutterNative.createInstance(assets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class FlutterNative {

public static native void destroyInstance(long instance);

public static native void sendPosEvent(long instance, @NativeType("FlutterPointerPhase") int phase, double x, double y, long view);
public static native void sendPointerEvent(long instance, @NativeType("FlutterPointerPhase") int phase, double x, double y, int signalKind, double scrollDX, double scrollDY, long view);

public static native void sendKeyEvent(long instance, @NativeType("GLFWwindow *") long window, int key, int scancode, int action, int mods);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.primogemstudio.advancedfmk.flutter;

import static com.primogemstudio.advancedfmk.flutter.Events.instances;
import static com.primogemstudio.advancedfmk.flutter.PointerPhase.*;
import static com.primogemstudio.advancedfmk.flutter.SignalKind.*;

public class MouseEvent {
public static boolean onMouseButton(int phase, double x, double y) {
for (var i : instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPosEvent(i.handle, phase, x - i.rect.left, y - i.rect.top, 0);
FlutterNative.sendPointerEvent(i.handle, phase, x - i.rect.left, y - i.rect.top, None, 0, 0, 0);
if (phase == PointerPhase.kDown) i.pressed = true;
else if (phase == kUp) i.pressed = false;
return true;
}
}
Expand All @@ -16,7 +20,17 @@ public static boolean onMouseButton(int phase, double x, double y) {
public static boolean onMouseMove(double x, double y) {
for (var i : instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPosEvent(i.handle, PointerPhase.kHover, x - i.rect.left, y - i.rect.top, 0);
FlutterNative.sendPointerEvent(i.handle, i.pressed ? kMove : kHover, x - i.rect.left, y - i.rect.top, None, 0, 0, 0);
return true;
}
}
return false;
}

public static boolean onMouseScroll(double x, double y, double dx, double dy) {
for (var i : instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPointerEvent(i.handle, i.pressed ? kUp : kHover, x - i.rect.left, y - i.rect.top, Scroll, dx * 20, -dy * 20, 0);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.primogemstudio.advancedfmk.flutter;

public class SignalKind {
public static final int None = 0;
public static final int Scroll = 1;
public static final int ScrollInertiaCancel = 2;
public static final int Scale = 3;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ private void onMouseMove(long window, double x, double y, CallbackInfo ci) {
if (window != minecraft.getWindow().getWindow()) return;
if (MouseEvent.onMouseMove(x, y)) ci.cancel();
}

@Inject(method = "onScroll", at = @At("HEAD"), cancellable = true)
private void onMouseScroll(long window, double xOffset, double yOffset, CallbackInfo ci) {
if (window != minecraft.getWindow().getWindow()) return;
double[] x = {0};
double[] y = {0};
glfwGetCursorPos(window, x, y);
if (MouseEvent.onMouseScroll(x[0], y[0], xOffset, yOffset)) ci.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class RenderSystemMixin {
private static void flipFrame(long l, CallbackInfo ci) {
if (instance == null) {
var window = Minecraft.getInstance().getWindow();
instance = new FlutterInstance("f:/c++/glfw-flutter/app", new Rect(0, window.getHeight() - 600, 800, window.getHeight()), 800, 600);
instance = new FlutterInstance("F:/Else Language/Dart/flutter-starrail/build/app", new Rect(0, window.getHeight() - 600, 800, window.getHeight()), 800, 600);
}
instance.pollEvents();
blit(800, 600);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.primogemstudio.advancedfmk.tests

import com.primogemstudio.advancedfmk.flutter.FlutterNative
import com.primogemstudio.advancedfmk.flutter.PointerPhase.*
import com.primogemstudio.advancedfmk.flutter.SignalKind.None
import com.primogemstudio.advancedfmk.flutter.SignalKind.Scroll
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.glfw.GLFW.Functions.*
import org.lwjgl.opengl.GL
Expand Down Expand Up @@ -85,6 +87,8 @@ fun initResources() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
}

var pressed = false

fun main() {
glfwInit()
FlutterNative.init(GetKeyName, GetClipboardString, SetClipboardString, GetProcAddress)
Expand All @@ -99,7 +103,7 @@ fun main() {
GL.createCapabilities()
initResources()
createShader()
flutterInstance = FlutterNative.createInstance("f:/c++/glfw-flutter/app")
flutterInstance = FlutterNative.createInstance("F:/Else Language/Dart/flutter-starrail/build/app")
FlutterNative.sendMetricsEvent(flutterInstance, 800, 600, 0)

glfwSetWindowSizeCallback(window) { _, w, h ->
Expand All @@ -111,24 +115,34 @@ fun main() {
}
FlutterNative.sendKeyEvent(flutterInstance, window, key, scancode, action, mods)
}
glfwSetScrollCallback(window) { _, x1, y1 ->
val x = doubleArrayOf(0.0)
val y = doubleArrayOf(0.0)
glfwGetCursorPos(window, x, y)
FlutterNative.sendPointerEvent(
flutterInstance, if (pressed) kUp else kHover, x[0], y[0], Scroll, x1 * 20, -y1 * 20, 0
)
}
glfwSetCharCallback(window) { _, codepoint ->
FlutterNative.sendCharEvent(flutterInstance, window, codepoint)
}
glfwSetCursorPosCallback(window) { _, x1, y1 ->
FlutterNative.sendPosEvent(flutterInstance, kHover, x1, y1, 0)
FlutterNative.sendPointerEvent(flutterInstance, if (pressed) kMove else kHover, x1, y1, None, 0.0, 0.0, 0)
}
glfwSetMouseButtonCallback(window) { _, button, action, _ ->
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
val x = doubleArrayOf(0.0)
val y = doubleArrayOf(0.0)
glfwGetCursorPos(window, x, y)
FlutterNative.sendPosEvent(flutterInstance, kDown, x[0], y[0], 0)
FlutterNative.sendPointerEvent(flutterInstance, kDown, x[0], y[0], None, 0.0, 0.0, 0)
pressed = true
}
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_RELEASE) {
val x = doubleArrayOf(0.0)
val y = doubleArrayOf(0.0)
glfwGetCursorPos(window, x, y)
FlutterNative.sendPosEvent(flutterInstance, kUp, x[0], y[0], 0)
FlutterNative.sendPointerEvent(flutterInstance, kUp, x[0], y[0], None, 0.0, 0.0, 0)
pressed = false
}
}
while (!glfwWindowShouldClose(window)) {
Expand Down

0 comments on commit 0c3c7db

Please sign in to comment.