Skip to content

Commit

Permalink
Rewrite to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack253-png committed Sep 20, 2024
1 parent faeb736 commit 2ff94bc
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 106 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.primogemstudio.advancedfmk.flutter

class FlutterAppRect {
var left: Int = 0
var top: Int = 0
var right: Int = 0
var bottom: Int = 0

constructor(left: Int, top: Int, right: Int, bottom: Int) {
this.left = left
this.top = top
this.right = right
this.bottom = bottom
}

constructor()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.primogemstudio.advancedfmk.flutter

object FlutterEvents {
val instances: HashSet<FlutterInstance> = java.util.HashSet()

fun register(instance: FlutterInstance) {
instances.add(instance)
}

fun unregister(instance: FlutterInstance) {
instances.remove(instance)
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
package com.primogemstudio.advancedfmk.flutter;
package com.primogemstudio.advancedfmk.flutter

public class FlutterInstance implements AutoCloseable {
public final long handle;
public final Rect rect;
public int width, height;
import com.primogemstudio.advancedfmk.flutter.FlutterEvents.register
import com.primogemstudio.advancedfmk.flutter.FlutterEvents.unregister

public FlutterInstance(String assets, Rect rect, int width, int height) {
handle = FlutterNative.createInstance(assets);
this.rect = rect;
this.width = width;
this.height = height;
FlutterNative.sendMetricsEvent(handle, rect.right - rect.left, rect.bottom - rect.top, 0);
Events.register(this);
}
class FlutterInstance(assets: String?, val rect: FlutterAppRect, var width: Int, var height: Int) : AutoCloseable {
val handle: Long = FlutterNative.createInstance(assets)

public boolean hitTest(double x, double y) {
return x > rect.left && x < rect.right && y > rect.top && y < rect.bottom;
init {
FlutterNative.sendMetricsEvent(handle, rect.right - rect.left, rect.bottom - rect.top, 0)
register(this)
}

public void pollEvents() {
FlutterNative.pollEvents(handle);
fun hitTest(x: Double, y: Double): Boolean {
return x > rect.left && x < rect.right && y > rect.top && y < rect.bottom
}

public int getTexture() {
return FlutterNative.getTexture(handle);
fun pollEvents() {
FlutterNative.pollEvents(handle)
}

@Override
public void close() {
Events.unregister(this);
FlutterNative.destroyInstance(handle);
val texture: Int
get() = FlutterNative.getTexture(handle)

override fun close() {
unregister(this)
FlutterNative.destroyInstance(handle)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public final class FlutterNative {
static {
System.load("D:/engine/src/out/host_release/flutter_minecraft.dll");
System.load("/home/coder2/flutter/engine_build/test/src/out/host_release/libflutter_minecraft.so");
}

public static native void init(long f1, long f2, long f3, long f4);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package com.primogemstudio.advancedfmk.flutter;
package com.primogemstudio.advancedfmk.flutter

import static com.primogemstudio.advancedfmk.flutter.Events.instances;
import com.primogemstudio.advancedfmk.flutter.FlutterEvents.instances
import java.util.function.Consumer

public class KeyEvent {
public static void onKey(long window, int key, int scanCode, int action, int modifiers) {
instances.forEach(i -> FlutterNative.sendKeyEvent(i.handle, window, key, scanCode, action, modifiers));
object KeyEvent {
fun onKey(window: Long, key: Int, scanCode: Int, action: Int, modifiers: Int) {
instances.forEach { i: FlutterInstance ->
FlutterNative.sendKeyEvent(
i.handle,
window,
key,
scanCode,
action,
modifiers
)
}
}

public static void onChar(long window, int code) {
instances.forEach(i -> FlutterNative.sendCharEvent(i.handle, window, code));
fun onChar(window: Long, code: Int) {
instances.forEach { i: FlutterInstance ->
FlutterNative.sendCharEvent(
i.handle,
window,
code
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
package com.primogemstudio.advancedfmk.flutter;
package com.primogemstudio.advancedfmk.flutter

import static com.primogemstudio.advancedfmk.flutter.Events.instances;
import com.primogemstudio.advancedfmk.flutter.FlutterEvents.instances

public class MouseEvent {
public static boolean onMouseButton(int phase, double x, double y) {
for (var i : instances) {
object MouseEvent {
fun onMouseButton(phase: Int, x: Double, y: Double): Boolean {
for (i in instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPosEvent(i.handle, phase, x - i.rect.left, y - i.rect.top, 0);
return true;
FlutterNative.sendPosEvent(i.handle, phase, x - i.rect.left, y - i.rect.top, 0)
return true
}
}
return false;
return false
}

public static boolean onMouseMove(double x, double y) {
for (var i : instances) {
fun onMouseMove(x: Double, y: Double): Boolean {
for (i in instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPosEvent(i.handle, PointerPhase.kHover, x - i.rect.left, y - i.rect.top, 0);
return true;
FlutterNative.sendPosEvent(i.handle, PointerPhase.kMove, x - i.rect.left, y - i.rect.top, 0)
FlutterNative.sendPosEvent(i.handle, PointerPhase.kHover, x - i.rect.left, y - i.rect.top, 0)
return true
}
}
return false;
return false
}

fun onMouseScroll(x: Double, y: Double, amount: Double): Boolean {
for (i in instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPosEvent(i.handle, PointerPhase.kRemove, x - i.rect.left, y - i.rect.top, 0)
FlutterNative.sendPosEvent(i.handle, PointerPhase.kAdd, x - i.rect.left, y - i.rect.top, 0)
FlutterNative.sendPosEvent(i.handle, PointerPhase.kCancel, x - i.rect.left, y - i.rect.top, 0)
FlutterNative.sendPosEvent(i.handle, PointerPhase.kPanZoomStart, x - i.rect.left, y - i.rect.top, 0)

FlutterNative.sendPosEvent(i.handle, PointerPhase.kPanZoomUpdate, x - i.rect.left, y - i.rect.top, 0)
FlutterNative.sendPosEvent(i.handle, PointerPhase.kPanZoomEnd, x - i.rect.left, y - i.rect.top, 0)
return true
}
}
return false
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.primogemstudio.advancedfmk.flutter;
package com.primogemstudio.advancedfmk.flutter

public final class PointerPhase {
public static final int kCancel = 0;
public static final int kUp = 1;
public static final int kDown = 2;
public static final int kMove = 3;
public static final int kAdd = 4;
public static final int kRemove = 5;
public static final int kHover = 6;
public static final int kPanZoomStart = 7;
public static final int kPanZoomUpdate = 8;
public static final int kPanZoomEnd = 9;
}
object PointerPhase {
const val kCancel: Int = 0
const val kUp: Int = 1
const val kDown: Int = 2
const val kMove: Int = 3
const val kAdd: Int = 4
const val kRemove: Int = 5
const val kHover: Int = 6
const val kPanZoomStart: Int = 7
const val kPanZoomUpdate: Int = 8
const val kPanZoomEnd: Int = 9
}

This file was deleted.

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

import static com.primogemstudio.advancedfmk.flutter.Events.instances;

public class ViewEvent {
public static void resize(int width, int height) {
instances.forEach(i -> {
i.rect.top = height - i.height;
i.rect.bottom = height;
});
object ViewEvent {
fun resize(width: Int, height: Int) {
FlutterEvents.instances.forEach { i: FlutterInstance ->
i.rect.top = height - i.height
i.rect.bottom = height
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class FlutterWindowMixin {
@Inject(method = "<init>", at = @At("RETURN"))
private void init(WindowEventHandler eventHandler, ScreenManager screenManager, DisplayData displayData, String preferredFullscreenVideoMode, String title, CallbackInfo ci) {
FlutterNative.init(GetKeyName, GetClipboardString, SetClipboardString, GetProcAddress);
GLFW.glfwSetCharCallback(window, KeyEvent::onChar);
GLFW.glfwSetCharCallback(window, KeyEvent.INSTANCE::onChar);
}

@Inject(method = "onResize", at = @At("RETURN"))
private void onResize(long window, int width, int height, CallbackInfo ci) {
ViewEvent.resize(width, height);
ViewEvent.INSTANCE.resize(width, height);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class KeyboardHandlerMixin {
@Inject(method = "keyPress", at = @At("HEAD"))
private void keyPress(long window, int key, int scanCode, int action, int modifiers, CallbackInfo ci) {
if (window != minecraft.getWindow().getWindow()) return;
KeyEvent.onKey(window, key, scanCode, action, modifiers);
KeyEvent.INSTANCE.onKey(window, key, scanCode, action, modifiers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static org.lwjgl.glfw.GLFW.*;

@Mixin(MouseHandler.class)
public class MouseHandlerMixin {
public abstract class MouseHandlerMixin {
@Shadow
@Final
private Minecraft minecraft;
Expand All @@ -26,16 +26,26 @@ private void onMouseButton(long window, int button, int action, int modifiers, C
double[] y = {0};
glfwGetCursorPos(window, x, y);
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
if (MouseEvent.onMouseButton(PointerPhase.kDown, x[0], y[0])) ci.cancel();
if (MouseEvent.INSTANCE.onMouseButton(PointerPhase.kDown, x[0], y[0])) ci.cancel();
}
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_RELEASE) {
if (MouseEvent.onMouseButton(PointerPhase.kUp, x[0], y[0])) ci.cancel();
if (MouseEvent.INSTANCE.onMouseButton(PointerPhase.kUp, x[0], y[0])) ci.cancel();
}
}

@Inject(method = "onMove", at = @At("HEAD"), cancellable = true)
private void onMouseMove(long window, double x, double y, CallbackInfo ci) {
if (window != minecraft.getWindow().getWindow()) return;
if (MouseEvent.onMouseMove(x, y)) ci.cancel();
if (MouseEvent.INSTANCE.onMouseMove(x, y)) ci.cancel();
}

@Inject(method = "onScroll", at = @At("HEAD"), cancellable = true)
private void onMouseScroll(long window, double xOffset, double yOffset, CallbackInfo ci) {
double[] x = {0};
double[] y = {0};
glfwGetCursorPos(window, x, y);
System.out.println(xOffset + " " + yOffset + " " + x[0] + " " + y[0]);
if (MouseEvent.INSTANCE.onMouseScroll(x[0], y[0], yOffset)) ci.cancel();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.primogemstudio.advancedfmk.flutter.FlutterInstance;
import com.primogemstudio.advancedfmk.flutter.Rect;
import com.primogemstudio.advancedfmk.flutter.FlutterAppRect;
import com.primogemstudio.advancedfmk.flutter.Shaders;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -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("/home/coder2/flutter/flutter_demo/build/linux/x64/release/bundle/data/flutter_assets", new FlutterAppRect(0, window.getHeight() - 600, 800, window.getHeight()), 800, 600);
}
instance.pollEvents();
blit(800, 600);
Expand All @@ -37,6 +37,7 @@ private static void blit(int width, int height) {
GlStateManager._disableDepthTest();
GlStateManager._depthMask(false);
GlStateManager._viewport(0, 0, width, height);
GlStateManager._enableBlend();
var shader = Shaders.BLIT_NO_FLIP;
shader.setSampler("DiffuseSampler", instance.getTexture());
shader.apply();
Expand Down

0 comments on commit 2ff94bc

Please sign in to comment.