Skip to content

Commit

Permalink
重写至 Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack253-png committed Sep 21, 2024
1 parent a1752eb commit 1dcbb24
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.HashSet;

public class Events {
public class FlutterEvents {
static final HashSet<FlutterInstance> instances = new HashSet<>();

public static void register(FlutterInstance instance) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,81 @@
package com.primogemstudio.advancedfmk.flutter;
package com.primogemstudio.advancedfmk.flutter

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Unique;
import com.mojang.blaze3d.platform.GlStateManager
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.BufferUploader
import com.mojang.blaze3d.vertex.DefaultVertexFormat
import com.mojang.blaze3d.vertex.VertexFormat
import net.minecraft.client.Minecraft
import java.lang.ref.Cleaner

import java.lang.ref.Cleaner;
class FlutterInstance(assets: String?, val rect: FlutterRect, var width: Int, var height: Int) :
AutoCloseable {
private val cleaner: Cleaner.Cleanable
val handle: Long = FlutterNative.createInstance(assets)
var pressed: Boolean = false

public class FlutterInstance implements AutoCloseable {
private final Cleaner.Cleanable cleaner;
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);
this.rect = rect;
this.width = width;
this.height = height;
sendSizeEvent();
Events.register(this);
var _handle = handle;
cleaner = FlutterNative.cleaner.register(this, () -> FlutterNative.destroyInstance(_handle));
init {
sendSizeEvent()
FlutterEvents.register(this)
val _handle = handle
cleaner = FlutterNative.cleaner.register(this) {
FlutterNative.destroyInstance(
_handle
)
}
}

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

void sendSizeEvent() {
FlutterNative.sendMetricsEvent(handle, rect.right - rect.left, rect.bottom - rect.top, 0);
fun sendSizeEvent() {
FlutterNative.sendMetricsEvent(handle, rect.right - rect.left, rect.bottom - rect.top, 0)
}

public void resize(int width, int height) {
this.width = width;
this.height = height;
ViewEvent.resize(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight());
fun resize(width: Int, height: Int) {
this.width = width
this.height = height
FlutterViewEvent.resize(Minecraft.getInstance().window.width, Minecraft.getInstance().window.height)
}

public void pollEvents() {
FlutterNative.pollEvents(handle);
fun pollEvents() {
FlutterNative.pollEvents(handle)
}

public int getTexture() {
return FlutterNative.getTexture(handle);
}
val texture: Int
get() = FlutterNative.getTexture(handle)

public void renderToScreen() {
var x = rect.left;
var y = Minecraft.getInstance().getWindow().getHeight() - rect.bottom;
var width = rect.right - rect.left;
var height = rect.bottom - rect.top;
var w = Minecraft.getInstance().getWindow().getWidth();
var h = Minecraft.getInstance().getWindow().getHeight();
GlStateManager._colorMask(true, true, true, false);
GlStateManager._disableDepthTest();
GlStateManager._depthMask(false);
GlStateManager._viewport(x, y, width, height);
GlStateManager._enableBlend();
var shader = Shaders.BLIT_NO_FLIP;
shader.getUniform("PositionOffset").set((float) (x / w), (float) (y / h));
shader.setSampler("DiffuseSampler", getTexture());
shader.apply();
BufferBuilder buff = RenderSystem.renderThreadTesselator().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLIT_SCREEN);
buff.addVertex(0.0F, 0.0F, 0.0F);
buff.addVertex(1.0F, 0.0F, 0.0F);
buff.addVertex(1.0F, 1.0F, 0.0F);
buff.addVertex(0.0F, 1.0F, 0.0F);
BufferUploader.draw(buff.buildOrThrow());
shader.clear();
GlStateManager._disableBlend();
GlStateManager._depthMask(true);
GlStateManager._colorMask(true, true, true, true);
fun renderToScreen() {
val x = rect.left
val y = Minecraft.getInstance().window.height - rect.bottom
val width = rect.right - rect.left
val height = rect.bottom - rect.top
val w = Minecraft.getInstance().window.width
val h = Minecraft.getInstance().window.height
GlStateManager._colorMask(true, true, true, false)
GlStateManager._disableDepthTest()
GlStateManager._depthMask(false)
GlStateManager._viewport(x, y, width, height)
GlStateManager._enableBlend()
val shader = Shaders.BLIT_NO_FLIP
shader.getUniform("PositionOffset")!![(x / w).toFloat()] = (y / h).toFloat()
shader.setSampler("DiffuseSampler", texture)
shader.apply()
val buff = RenderSystem.renderThreadTesselator().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLIT_SCREEN)
buff.addVertex(0.0f, 0.0f, 0.0f)
buff.addVertex(1.0f, 0.0f, 0.0f)
buff.addVertex(1.0f, 1.0f, 0.0f)
buff.addVertex(0.0f, 1.0f, 0.0f)
BufferUploader.draw(buff.buildOrThrow())
shader.clear()
GlStateManager._disableBlend()
GlStateManager._depthMask(true)
GlStateManager._colorMask(true, true, true, true)
}

@Override
public void close() {
Events.unregister(this);
cleaner.clean();
override fun close() {
FlutterEvents.unregister(this)
cleaner.clean()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.primogemstudio.advancedfmk.flutter

object FlutterKeyEvent {
fun onKey(window: Long, key: Int, scanCode: Int, action: Int, modifiers: Int) {
FlutterEvents.instances.forEach { i: FlutterInstance ->
FlutterNative.sendKeyEvent(
i.handle,
window,
key,
scanCode,
action,
modifiers
)
}
}

fun onChar(window: Long, code: Int) {
FlutterEvents.instances.forEach { i: FlutterInstance ->
FlutterNative.sendCharEvent(
i.handle,
window,
code
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.primogemstudio.advancedfmk.flutter

object FlutterMouseEvent {
fun onMouseButton(phase: Int, x: Double, y: Double): Boolean {
for (i in FlutterEvents.instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPointerEvent(
i.handle,
phase,
x - i.rect.left,
y - i.rect.top,
FlutterSignalKind.None,
0.0,
0.0,
0
)
if (phase == FlutterPointerPhase.kDown) i.pressed = true
else if (phase == FlutterPointerPhase.kUp) i.pressed = false
return true
}
}
return false
}

fun onMouseMove(x: Double, y: Double): Boolean {
for (i in FlutterEvents.instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPointerEvent(
i.handle,
if (i.pressed) FlutterPointerPhase.kMove else FlutterPointerPhase.kHover,
x - i.rect.left,
y - i.rect.top,
FlutterSignalKind.None,
0.0,
0.0,
0
)
return true
}
}
return false
}

fun onMouseScroll(x: Double, y: Double, dx: Double, dy: Double): Boolean {
for (i in FlutterEvents.instances) {
if (i.hitTest(x, y)) {
FlutterNative.sendPointerEvent(
i.handle,
if (i.pressed) FlutterPointerPhase.kUp else FlutterPointerPhase.kHover,
x - i.rect.left,
y - i.rect.top,
FlutterSignalKind.Scroll,
dx * 20,
-dy * 20,
0
)
return true
}
}
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.primogemstudio.advancedfmk.flutter

object FlutterPointerPhase {
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.primogemstudio.advancedfmk.flutter

class FlutterRect {
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()

override fun toString(): String {
return "$left $top $right $bottom"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.primogemstudio.advancedfmk.flutter

object FlutterSignalKind {
const val None: Int = 0
const val Scroll: Int = 1
const val ScrollInertiaCancel: Int = 2
const val Scale: Int = 3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.primogemstudio.advancedfmk.flutter

import java.util.function.Consumer

object FlutterViewEvent {
fun resize(width: Int, height: Int) {
FlutterEvents.instances.forEach(Consumer { i: FlutterInstance ->
i.rect.left = (width - i.width) / 2
i.rect.right = (width + i.width) / 2
i.rect.top = (height - i.height) / 2
i.rect.bottom = (height + i.height) / 2
i.sendSizeEvent()
})
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1dcbb24

Please sign in to comment.