Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Even more mutable cleanups #1480

Merged
merged 9 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions kds/src/commonMain/kotlin/korlibs/datastructure/Ref.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package korlibs.datastructure

import kotlin.reflect.*

interface Ref<T : Any> {
var value: T
}

fun <T : Any> Ref(): Ref<T> = object : Ref<T> {
override lateinit var value: T
}

fun <T : Any> KMutableProperty0<T>.toRef(): Ref<T> = Ref(this)

fun <T : Any> Ref(prop: KMutableProperty0<T>): Ref<T> = object : Ref<T> {
override var value: T
get() = prop.get()
set(value) { prop.set(value) }
}
34 changes: 17 additions & 17 deletions kds/src/commonMain/kotlin/korlibs/datastructure/ds/BVH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class BVH<T>(
var nodes: FastArrayList<Node<T>>? = null
)

data class IntersectResult<T>(val intersect: Double, val obj: Node<T>)
data class IntersectResult<T>(val intersect: Float, val obj: Node<T>)

open class Comparators {
companion object : Comparators()
Expand Down Expand Up @@ -418,7 +418,7 @@ class BVH<T>(
}

var d = 0
var last_difference = 0.0
var last_difference = 0f
//for (i = 0; i < _Dimensions; i++) {
for (i in 0 until this.dimensions) {
val difference =
Expand Down Expand Up @@ -554,17 +554,17 @@ class BVH<T>(
if (ints == null) {
ints = this.root.d // By default, use the scene bounding box
}
val parameters = Array(2) { DoubleArray(this.dimensions) }
val parameters = Array(2) { FloatArray(this.dimensions) }
// inv_direction and sign can be pre-computed per ray
val inv_direction = DoubleArray(this.dimensions)
val inv_direction = FloatArray(this.dimensions)
val sign = IntArray(this.dimensions)

// Initialize values
for (i in 0 until this.dimensions) {
parameters[0][i] = ints.a(i)
parameters[1][i] = ints.a(i) + ints.b(i)

val j = 1.0 / ray.b(i)
val j = 1f / ray.b(i)
inv_direction[i] = j
sign[i] = if (j <= 0) 1 else 0
}
Expand All @@ -591,7 +591,7 @@ class BVH<T>(
return null
}
if (omin < 0 && omax < 0) return null
if (omin < 0) omin = 0.0
if (omin < 0) omin = 0f
val rs = _make_Empty()

for (i in 0 until this.dimensions) {
Expand Down Expand Up @@ -844,30 +844,30 @@ class BVH<T>(
* [x, width, y, height, z, depth]
*/
@Suppress("INLINE_CLASS_DEPRECATED")
inline class BVHIntervals(val data: DoubleArray) {
constructor(dimensions: Int) : this(DoubleArray(dimensions * 2))
inline class BVHIntervals(val data: FloatArray) {
constructor(dimensions: Int) : this(FloatArray(dimensions * 2))

companion object {
operator fun invoke(vararg values: Double): BVHIntervals = BVHIntervals(values)
operator fun invoke(vararg values: Int): BVHIntervals = BVHIntervals(DoubleArray(values.size) { values[it].toDouble() })
operator fun invoke(vararg values: Float): BVHIntervals = BVHIntervals(values)
operator fun invoke(vararg values: Int): BVHIntervals = BVHIntervals(FloatArray(values.size) { values[it].toFloat() })
}

fun checkDimensions(dimensions: Int) {
if (dimensions != length) error("element $length doesn't match dimensions $dimensions")
}

fun setTo(vararg values: Double) {
fun setTo(vararg values: Float) {
values.copyInto(data)
}

fun setTo(a0: Double, b0: Double, a1: Double, b1: Double) {
fun setTo(a0: Float, b0: Float, a1: Float, b1: Float) {
data[0] = a0
data[1] = b0
data[2] = a1
data[3] = b1
}

fun setTo(a0: Double, b0: Double, a1: Double, b1: Double, a2: Double, b2: Double) {
fun setTo(a0: Float, b0: Float, a1: Float, b1: Float, a2: Float, b2: Float) {
data[0] = a0
data[1] = b0
data[2] = a1
Expand All @@ -877,11 +877,11 @@ inline class BVHIntervals(val data: DoubleArray) {
}

val length get() = data.size / 2
fun a(index: Int): Double = data[index * 2 + 0]
fun a(index: Int, value: Double) { data[index * 2 + 0] = value }
fun a(index: Int): Float = data[index * 2 + 0]
fun a(index: Int, value: Float) { data[index * 2 + 0] = value }

fun b(index: Int): Double = data[index * 2 + 1]
fun b(index: Int, value: Double) {
fun b(index: Int): Float = data[index * 2 + 1]
fun b(index: Int, value: Float) {
data[index * 2 + 1] = value
}

Expand Down
23 changes: 12 additions & 11 deletions korge-sandbox/src/commonMain/kotlin/samples/MainBVH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MainBVH : Scene() {
val view = solidRect(width, height, rand[Colors.RED, Colors.BLUE]).xy(x, y)
view.movingDirection = if (rand.nextBoolean()) -1 else +1
rects += view
bvh.insertOrUpdate(view.getBounds(this).mutable, view)
bvh.insertOrUpdate(view.getBounds(this), view)
}
addUpdater {
for (n in rects.size - 100 until rects.size) {
Expand All @@ -42,14 +42,14 @@ class MainBVH : Scene() {
view.movingDirection = -1
}
view.x += view.movingDirection
bvh.insertOrUpdate(view.getBounds(this).mutable, view)
bvh.insertOrUpdate(view.getBounds(this), view)
}
}
val center = MPoint(width / 2, height / 2)
val dir = MPoint(-1, -1)
val ray = MRay(center, dir)
val center = Point(width / 2, height / 2)
var dir = Point(-1, -1)
var ray = Ray(center, dir)
val statusText = text("", font = DefaultTtfFontAsBitmap)
var selectedRectangle = MRectangle(MPoint(100, 100) - MPoint(50, 50), MSize(100, 100))
var selectedRectangle = Rectangle(Point(100, 100) - Point(50, 50), Size(100, 100))
val rayLine = line(center, center + (dir * 1000), Colors.WHITE)
val selectedRect = outline(buildVectorPath(VectorPath()) {
rect(selectedRectangle)
Expand All @@ -60,7 +60,7 @@ class MainBVH : Scene() {
var allObjectsSize = 0
var rayObjectsSize = 0
var rectangleObjectsSize = 0
val allObjects = bvh.search(MRectangle(0.0, 0.0, width, height))
val allObjects = bvh.search(Rectangle(0.0, 0.0, width, height))
val time = measureTime {
val rayObjects = bvh.intersect(ray)
val rectangleObjects = bvh.search(selectedRectangle)
Expand All @@ -78,23 +78,24 @@ class MainBVH : Scene() {
addUpdater {
//println("moved")
val mousePos = localMousePos(views)
val angle = Point.angleFull(center.point, mousePos)
val angle = Point.angleFull(center, mousePos)
//println("center=$center, mousePos=$mousePos, angle = $angle")
dir.setTo(angle.cosineD, angle.sineD)
dir = Vector2(angle.cosineD, angle.sineD)
ray = Ray(center, dir)
rayLine.setPoints(center, center + (dir * 1000))

updateRay()
}

mouse {
onDown {
selectedRectangle = MRectangle(stage!!.mousePos.mutable - MPoint(50, 50), MSize(100, 100))
selectedRectangle = Rectangle(stage!!.mousePos - Point(50, 50), Size(100, 100))
selectedRect.vectorPath = buildVectorPath(VectorPath()) {
rect(selectedRectangle)
}
}
onMouseDrag {
selectedRectangle = MRectangle(stage.mousePos.mutable - MPoint(50, 50), MSize(100, 100))
selectedRectangle = Rectangle(stage.mousePos - Point(50, 50), Size(100, 100))
selectedRect.vectorPath = buildVectorPath(VectorPath()) {
rect(selectedRectangle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import korlibs.image.bitmap.Bitmaps
import korlibs.image.bitmap.slice
import korlibs.image.format.readBitmap
import korlibs.io.file.std.resourcesVfs
import korlibs.math.geom.MRectangle
import korlibs.math.geom.*

class MainColorPicker : Scene() {
override suspend fun SContainer.sceneMain() {
Expand All @@ -22,7 +22,7 @@ class MainColorPicker : Scene() {

mouse {
move {
val bmp = stage!!.unsafeRenderToBitmapSync(views!!.renderContext, MRectangle(views.stage.mousePos.xD - 5.0, views.stage.mousePos.yD - 5.0, 10.0, 10.0), views!!.globalToWindowScaleAvg)
val bmp = stage!!.unsafeRenderToBitmapSync(views!!.renderContext, Rectangle(views.stage.mousePos.xD - 5.0, views.stage.mousePos.yD - 5.0, 10.0, 10.0), views!!.globalToWindowScaleAvg)
magnifier.bitmap = bmp.slice()
invalidateRender()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MainGpuVectorRendering3 : Scene() {
})

debugPath("Rect closed", getPos(0, 1), strokeInfo, buildVectorPath {
rect(MRectangle.fromBounds(0, 0, 100, 100))
rect(Rectangle.fromBounds(0, 0, 100, 100))
})

debugPath("Rect not closed", getPos(1, 1), strokeInfo, buildVectorPath {
Expand Down
8 changes: 4 additions & 4 deletions korge-sandbox/src/commonMain/kotlin/samples/MainImageTrace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class MainImageTrace : Scene() {
override suspend fun SContainer.sceneMain() {
val bmp = Bitmap32Context2d(300, 200) {
fill(Colors.WHITE, winding = Winding.EVEN_ODD) {
rect(MRectangle.fromBounds(2, 2, 18, 18))
rectHole(MRectangle.fromBounds(6, 6, 9, 12))
rectHole(MRectangle.fromBounds(10, 5, 15, 12))
rect(MRectangle.fromBounds(50, 2, 68, 18))
rect(Rectangle.fromBounds(2, 2, 18, 18))
rectHole(Rectangle.fromBounds(6, 6, 9, 12))
rectHole(Rectangle.fromBounds(10, 5, 15, 12))
rect(Rectangle.fromBounds(50, 2, 68, 18))
circle(Point(100, 100), 60f)
circle(Point(100, 100), 30f)
roundRect(200, 50, 50, 50, 5, 5)
Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/commonMain/kotlin/samples/connect4/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class MainConnect4 : ScaledScene(448, 384) {
}
image(skeleton)

fun getPosition(column: Int, row: Int): MPoint {
return MPoint(column * 64.0 + 32.0, row * 64.0 + 32.0)
fun getPosition(column: Int, row: Int): Point {
return Point(column * 64.0 + 32.0, row * 64.0 + 32.0)
}

fun createChip(column: Int, row: Int, chip: Chip): Image {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import korlibs.korge.view.*
import korlibs.math.geom.*
import korlibs.math.interpolation.*

fun <T : View> T.dockedTo(anchor: Anchor, scaleMode: ScaleMode = ScaleMode.NO_SCALE, offset: MPoint = MPoint(), hook: (View) -> Unit = {}): T {
DockingComponent(this, anchor, scaleMode, MPoint().copyFrom(offset), hook)
fun <T : View> T.dockedTo(anchor: Anchor, scaleMode: ScaleMode = ScaleMode.NO_SCALE, offset: Point = Point(), hook: (View) -> Unit = {}): T {
DockingComponent(this, anchor, scaleMode, offset, hook)
return this
}

class DockingComponent(
val view: View,
var anchor: Anchor,
var scaleMode: ScaleMode = ScaleMode.NO_SCALE,
val offset: MPoint = MPoint(),
val offset: Point = Point(),
val hook: (View) -> Unit
) {
val initialViewSize = MSize(view.width, view.height)
private val actualVirtualSize = MSize(0, 0)
private val targetSize = MSize(0, 0)
val initialViewSize = Size(view.width, view.height)
private var actualVirtualSize = Size(0, 0)
private var targetSize = Size(0, 0)

init {
view.onStageResized { width, height ->
Expand All @@ -32,9 +32,10 @@ class DockingComponent(
//view.alignX(views.stage, anchor.sx, true)
//view.alignY(views.stage, anchor.sy, true)
if (scaleMode != ScaleMode.NO_SCALE) {
actualVirtualSize.setTo(views.actualVirtualWidth, views.actualVirtualHeight)
val size = scaleMode.invoke(initialViewSize, actualVirtualSize, targetSize)
view.setSize(size.width, size.height)
actualVirtualSize = Size(views.actualVirtualWidth, views.actualVirtualHeight)
val size = scaleMode.invoke(initialViewSize, actualVirtualSize)
targetSize = size
view.setSize(size)
}
view.invalidate()
view.parent?.invalidate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun TouchEvents.swipeRecognizer(

val distance = MPoint.distance(i.startGlobal, i.global)
if (distance >= thresold) {
val angle = Angle.between(i.startGlobal, i.global)
val angle = Angle.between(i.startGlobal.immutable, i.global.immutable)
completed = true
val direction = when {
angle >= 315.degrees || angle < 45.degrees -> SwipeRecognizerDirection.RIGHT
Expand Down Expand Up @@ -104,8 +104,8 @@ fun TouchEvents.rotationRecognizer(
val i1 = it.infos[1]
info.started = info.completed
info.completed = false
info.start = Angle.between(i0.startGlobal, i1.startGlobal)
info.current = Angle.between(i0.global, i1.global)
info.start = Angle.between(i0.startGlobal.immutable, i1.startGlobal.immutable)
info.current = Angle.between(i0.global.immutable, i1.global.immutable)
if (info.started) {
start(info)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class BatchBuilder2D constructor(
val maxTextures = BB_MAX_TEXTURES

@KorgeInternal
val viewMat: MMatrix3D get() = ctx.viewMat
val viewMat: Matrix4 get() = ctx.viewMat
@KorgeInternal
val viewMat2D: Matrix get() = ctx.viewMat2D.immutable
val viewMat2D: Matrix get() = ctx.viewMat2D

inline fun use(block: (BatchBuilder2D) -> Unit) = ctx.useBatcher(this, block)

Expand Down
Loading