-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1752eb
commit 1dcbb24
Showing
19 changed files
with
228 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 64 additions & 70 deletions
134
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/FlutterInstance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/FlutterKeyEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/FlutterMouseEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/FlutterPointerPhase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
21 changes: 21 additions & 0 deletions
21
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/FlutterRect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/FlutterSignalKind.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
15 changes: 15 additions & 0 deletions
15
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/FlutterViewEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/KeyEvent.java
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/MouseEvent.java
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
flutter-ui/src/main/java/com/primogemstudio/advancedfmk/flutter/PointerPhase.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.