Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
KorGE 4.0.0-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Apr 21, 2023
1 parent 3593e83 commit 49cf6e3
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 32 deletions.
6 changes: 3 additions & 3 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import com.soywiz.korge.gradle.*
import korlibs.korge.gradle.*

plugins {
//alias(libs.plugins.korge)
//id("com.soywiz.korge") version "999.0.0.999"
id("com.soywiz.korge") version "4.0.0-alpha-2"
id("com.soywiz.korge") version "4.0.0-rc"
}

korge {
Expand All @@ -21,7 +21,7 @@ korge {
targetJvm()
targetJs()
targetDesktop()
targetDesktopCross()
//targetDesktopCross()
targetIos()
targetAndroidDirect()
serializationJson()
Expand Down
14 changes: 7 additions & 7 deletions example/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import com.soywiz.korge.*
import com.soywiz.korge.scene.*
import com.soywiz.korge.ui.*
import com.soywiz.korge.view.*
import com.soywiz.korio.util.i18n.*
import korge.i18n.*
import korlibs.korge.*
import korlibs.korge.scene.*
import korlibs.korge.ui.*
import korlibs.korge.view.*
import korlibs.io.util.i18n.*
import korlibs.korge.i18n.*

suspend fun main() = Korge {
sceneContainer().changeTo({ MainMyModuleScene() })
Expand All @@ -20,7 +20,7 @@ class MainMyModuleScene : Scene() {
text("").textProvider {
when (it) {
Language.SPANISH -> "¡Hola Mundo!"
Language.ENGLISH -> "Hello World!"
Language.ENGLISH -> "Hello world!"
else -> "Unknown"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package korge.i18n
package korlibs.korge.i18n

import com.soywiz.kds.*
import com.soywiz.korge.view.*
import com.soywiz.korio.util.i18n.*
import korlibs.datastructure.*
import korlibs.korge.view.*
import korlibs.io.util.i18n.*

private var Views._language: Language? by extraProperty { null }

Expand Down
81 changes: 81 additions & 0 deletions korio-bus/src/commonMain/kotlin/korlibs/korge/bus/Bus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package korlibs.korge.bus

import korlibs.datastructure.iterators.fastForEach
import korlibs.inject.AsyncDestructor
import korlibs.inject.AsyncInjector
import korlibs.io.async.launchUnscoped
import korlibs.io.lang.Closeable
import kotlin.coroutines.CoroutineContext
import kotlin.reflect.KClass

class Bus(
private val globalBus: GlobalBus,
val coroutineContext: CoroutineContext = globalBus.coroutineContext,
) : Closeable, AsyncDestructor {
private val closeables = arrayListOf<Closeable>()

suspend fun send(message: Any) {
globalBus.send(message)
}

fun sendAsync(message: Any, coroutineContext: CoroutineContext = this.coroutineContext) {
globalBus.sendAsync(message, coroutineContext)
}

fun <T : Any> register(clazz: KClass<out T>, handler: suspend (T) -> Unit): Closeable {
val closeable = globalBus.register(clazz, handler)
closeables += closeable
return closeable
}

inline fun <reified T : Any> register(noinline handler: suspend (T) -> Unit): Closeable {
return register(T::class, handler)
}

override fun close() {
closeables.fastForEach { c ->
c.close()
}
}

override suspend fun deinit() {
close()
}
}

class GlobalBus(
val coroutineContext: CoroutineContext
) {
val perClassHandlers = HashMap<KClass<*>, ArrayList<suspend (Any) -> Unit>>()

suspend fun send(message: Any) {
val clazz = message::class
perClassHandlers[clazz]?.fastForEach { handler ->
handler(message)
}
}

fun sendAsync(message: Any, coroutineContext: CoroutineContext = this.coroutineContext) {
coroutineContext.launchUnscoped { send(message) }
}

private fun forClass(clazz: KClass<*>) = perClassHandlers.getOrPut(clazz) { arrayListOf() }

@Suppress("UNCHECKED_CAST")
fun <T : Any> register(clazz: KClass<out T>, handler: suspend (T) -> Unit): Closeable {
val chandler = handler as (suspend (Any) -> Unit)
forClass(clazz).add(chandler)
return Closeable {
forClass(clazz).remove(chandler)
}
}

inline fun <reified T : Any> register(noinline handler: suspend (T) -> Unit): Closeable {
return register(T::class, handler)
}
}

fun AsyncInjector.mapBus() {
mapSingleton { GlobalBus(get()) }
mapPrototype { Bus(get(), get()) }
}
68 changes: 68 additions & 0 deletions korio-bus/src/commonMain/kotlin/korlibs/korge/bus/SyncBus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package korlibs.korge.bus

import korlibs.datastructure.iterators.fastForEach
import korlibs.inject.AsyncDestructor
import korlibs.inject.AsyncInjector
import korlibs.io.lang.Closeable
import kotlin.reflect.KClass

class SyncBus(
private val globalBus: SyncGlobalBus
) : Closeable, AsyncDestructor {
private val closeables = arrayListOf<Closeable>()

fun send(message: Any) {
globalBus.send(message)
}

fun <T : Any> register(clazz: KClass<out T>, handler: (T) -> Unit): Closeable {
val closeable = globalBus.register(clazz, handler)
closeables += closeable
return closeable
}

inline fun <reified T : Any> register(noinline handler: (T) -> Unit): Closeable {
return register(T::class, handler)
}

override fun close() {
closeables.fastForEach { c ->
c.close()
}
}

override suspend fun deinit() {
close()
}
}

class SyncGlobalBus {
val perClassHandlers = HashMap<KClass<*>, ArrayList<(Any) -> Unit>>()

fun send(message: Any) {
val clazz = message::class
perClassHandlers[clazz]?.fastForEach { handler ->
handler(message)
}
}

private fun forClass(clazz: KClass<*>) = perClassHandlers.getOrPut(clazz) { arrayListOf() }

@Suppress("UNCHECKED_CAST")
fun <T : Any> register(clazz: KClass<out T>, handler: (T) -> Unit): Closeable {
val chandler = handler as ((Any) -> Unit)
forClass(clazz).add(chandler)
return Closeable {
forClass(clazz).remove(chandler)
}
}

inline fun <reified T : Any> register(noinline handler: (T) -> Unit): Closeable {
return register(T::class, handler)
}
}

fun AsyncInjector.mapSyncBus() {
mapSingleton { SyncGlobalBus() }
mapPrototype { SyncBus(get()) }
}
33 changes: 33 additions & 0 deletions korio-bus/src/commonTest/kotlin/korlibs/korge/bus/BusTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package korlibs.korge.bus

import korlibs.inject.AsyncInjector
import korlibs.io.async.suspendTest
import kotlin.test.Test
import kotlin.test.assertEquals

class BusTest {
val out = arrayListOf<String>()

inner class Scene1(val bus: Bus) {
init {
bus.register<Int> { out += "HELLO$it" }
}
}

@Test
fun test() = suspendTest {
val injector = AsyncInjector()
injector.mapInstance(coroutineContext) // This should be mapped already in the Korge { } block
injector.mapBus()
injector.mapPrototype { Scene1(get()) }
val injector2 = injector.child()
val scene1 = injector2.get<Scene1>()
val bus = injector.get<Bus>()
bus.send(1)
bus.send(2)
//scene1.sceneDestroyInternal()
injector2.deinit()
bus.send(3)
assertEquals("HELLO1,HELLO2", out.joinToString(","))
}
}
32 changes: 32 additions & 0 deletions korio-bus/src/commonTest/kotlin/korlibs/korge/bus/SyncBusTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package korlibs.korge.bus

import korlibs.inject.AsyncInjector
import korlibs.io.async.suspendTest
import kotlin.test.Test
import kotlin.test.assertEquals

class SyncBusTest {
val out = arrayListOf<String>()

inner class Scene1(val bus: SyncBus) {
init {
bus.register<Int> { out += "HELLO$it" }
}
}

@Test
fun test() = suspendTest {
val injector = AsyncInjector()
injector.mapSyncBus()
injector.mapPrototype { Scene1(get()) }
val injector2 = injector.child()
val scene1 = injector2.get<Scene1>()
val bus = injector.get<SyncBus>()
bus.send(1)
bus.send(2)
//scene1.sceneDestroyInternal()
injector2.deinit()
bus.send(3)
assertEquals("HELLO1,HELLO2", out.joinToString(","))
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.soywiz.korma.algo
package korlibs.math.algo

import com.soywiz.kds.BooleanArray2
import com.soywiz.kds.IntPriorityQueue
import com.soywiz.korma.geom.IPointIntArrayList
import com.soywiz.korma.geom.MPoint
import com.soywiz.korma.geom.PointIntArrayList
import korlibs.datastructure.BooleanArray2
import korlibs.datastructure.IntPriorityQueue
import korlibs.math.geom.Point
import korlibs.math.geom.PointIntArrayList
import korlibs.math.geom.PointIntList

class AStar(val width: Int, val height: Int, val isBlocking: (x: Int, y: Int) -> Boolean) {
companion object {
operator fun invoke(board: BooleanArray2) = AStar(board.width, board.height) { x, y -> board[x, y] }
fun find(
board: BooleanArray2, x0: Int, y0: Int, x1: Int, y1: Int, findClosest: Boolean = false,
diagonals: Boolean = false
): IPointIntArrayList = AStar(board.width, board.height) { x, y -> board[x, y] }.find(x0, y0, x1, y1, findClosest, diagonals)
): PointIntList = AStar(board.width, board.height) { x, y -> board[x, y] }.find(x0, y0, x1, y1, findClosest, diagonals)
}

fun find(x0: Int, y0: Int, x1: Int, y1: Int, findClosest: Boolean = false, diagonals: Boolean = false): IPointIntArrayList {
fun find(x0: Int, y0: Int, x1: Int, y1: Int, findClosest: Boolean = false, diagonals: Boolean = false): PointIntList {
val out = PointIntArrayList()
find(x0, y0, x1, y1, findClosest, diagonals) { x, y -> out.add(x, y) }
out.reverse()
Expand All @@ -31,15 +31,15 @@ class AStar(val width: Int, val height: Int, val isBlocking: (x: Int, y: Int) ->
val first = getNode(x0, y0)
val dest = getNode(x1, y1)
var closest = first
var closestDist = MPoint.distance(x0, y0, x1, y1)
var closestDist = Point.distance(x0, y0, x1, y1)
if (!first.value) {
queue.add(first.index)
first.weight = 0
}

while (queue.isNotEmpty()) {
val last = AStarNode(queue.removeHead())
val dist = MPoint.distance(last.posX, last.posY, dest.posX, dest.posY)
val dist = Point.distance(last.posX, last.posY, dest.posX, dest.posY)
if (dist < closestDist) {
closestDist = dist
closest = last
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.soywiz.korma.algo
package korlibs.math.algo

import com.soywiz.kds.BooleanArray2
import com.soywiz.kds.map2
import com.soywiz.korma.annotations.*
import com.soywiz.korma.geom.*
import korlibs.datastructure.BooleanArray2
import korlibs.datastructure.map2
import korlibs.math.annotations.*
import korlibs.math.geom.*
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -67,8 +67,8 @@ class AStarTest {


data class Result(val map: BooleanArray2, @KormaMutableApi val mstart: MPointInt, @KormaMutableApi val mend: MPointInt) {
@KormaValueApi val start: PointInt get() = mstart.point
@KormaValueApi val end: PointInt get() = mend.point
val start: PointInt get() = mstart.point
val end: PointInt get() = mend.point
}

fun map(str: String): Result {
Expand Down Expand Up @@ -111,7 +111,7 @@ class AStarTest {
val pointsMap = points.toPoints().withIndex().map { it.value to it.index }.toMap()
val res = input2.map.map2 { x, y, c ->
//pointsMap[PointInt(x, y)]?.let { xdigits[it] } ?: (if (c) '#' else '.') // @TODO: Kotlin-native: Regression Crashes BUG in runtime - https://github.com/JetBrains/kotlin-native/issues/1736
(pointsMap[MPointInt(x, y)]?.let { "" + xdigits[it] } ?: (if (c) "#" else ".")).first()
(pointsMap[PointInt(x, y)]?.let { "" + xdigits[it] } ?: (if (c) "#" else ".")).first()
}
val output = res.asString { it }
assertEquals(expected.trimIndent(), output)
Expand Down

0 comments on commit 49cf6e3

Please sign in to comment.