Skip to content

Commit

Permalink
Some more float -> double changes (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Sep 28, 2023
1 parent 482d25f commit 761b305
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 40 deletions.
1 change: 1 addition & 0 deletions korge-foundation/src/common/korlibs/math/geom/Anchor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ data class Anchor2D(val sx: Double, val sy: Double) : Interpolable<Anchor> {

companion object {
inline operator fun invoke(sx: Ratio, sy: Ratio): Anchor2D = Anchor2D(sx.toDouble(), sy.toDouble())
inline operator fun invoke(sx: Number, sy: Number): Anchor2D = Anchor2D(sx.toDouble(), sy.toDouble())

val TOP_LEFT: Anchor = Anchor(0f, 0f)
val TOP_CENTER: Anchor = Anchor(.5f, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Asteroid(
anchor(.5, .5)
scaleAvg = asteroidSize.toDouble() / 3.0
name = "asteroid"
speed = 0.6f
speed = 0.6
addUpdater { time ->
val scale = time / 16.0.milliseconds
val dx = angle.cosine * scale
Expand All @@ -34,11 +34,11 @@ class Asteroid(
if (asteroidSize > 1) {
Asteroid(assets, asteroidSize - 1).xy(x, y).addTo(parent!!).also {
it.angle = this.angle + 45.degrees
it.speed = this.speed * 1.5f
it.speed = this.speed * 1.5
}
Asteroid(assets, asteroidSize - 1).xy(x, y).addTo(parent!!).also {
it.angle = this.angle - 45.degrees
it.speed = this.speed * 1.5f
it.speed = this.speed * 1.5
}
}
removeFromParent()
Expand Down
9 changes: 4 additions & 5 deletions korge/src/common/korlibs/korge/view/Anchorable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ fun <T : Anchorable> T.anchor(anchor: Anchor): T {
return this
}

fun <T : Anchorable> T.anchor(ax: Float, ay: Float): T = anchor(Anchor(ax, ay))
fun <T : Anchorable> T.anchor(ax: Double, ay: Double): T = anchor(Anchor(ax, ay))
fun <T : Anchorable> T.anchor(ax: Int, ay: Int): T = anchor(Anchor(ax, ay))
@Suppress("NOTHING_TO_INLINE")
inline fun <T : Anchorable> T.anchor(ax: Number, ay: Number): T = anchor(Anchor(ax.toDouble(), ay.toDouble()))

fun <T : Anchorable> T.center(): T = anchor(0.5f, 0.5f)
val <T : Anchorable> T.centered: T get() = anchor(0.5f, 0.5f)
fun <T : Anchorable> T.center(): T = anchor(0.5, 0.5)
val <T : Anchorable> T.centered: T get() = center()

interface PixelAnchorable {
@ViewProperty(name = "anchorPixel")
Expand Down
49 changes: 21 additions & 28 deletions korge/src/common/korlibs/korge/view/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ abstract class View internal constructor(
@KorgeInternal
open val anchorDispY: Float get() = 0f

val anchorDispXF: Float get() = anchorDispX.toFloat()
val anchorDispYF: Float get() = anchorDispY.toFloat()

/** Read-only internal children list, or null when not a [Container] */
@KorgeInternal
@PublishedApi
Expand Down Expand Up @@ -205,7 +202,7 @@ abstract class View internal constructor(

/** Ratio speed of this node, affecting all the [View.addUpdater] */
@ViewProperty(min = -1.0, max = 1.0, clampMin = false, clampMax = false)
var speed: Float = 1f
var speed: Double = 1.0

internal var _stage: Stage? = null
set(value) {
Expand Down Expand Up @@ -256,7 +253,7 @@ abstract class View internal constructor(
}

/** Computed [speed] combining all the speeds from ancestors */
val globalSpeed: Float get() = if (parent != null) parent!!.globalSpeed * speed else speed
val globalSpeed: Double get() = if (parent != null) parent!!.globalSpeed * speed else speed

protected var _x: Double = 0.0
protected var _y: Double = 0.0
Expand All @@ -266,7 +263,7 @@ abstract class View internal constructor(
private var _skewY: Angle = Angle.ZERO
private var _rotation: Angle = Angle.ZERO

private fun setXY(x: Double, y: Double) {
internal fun setXY(x: Double, y: Double) {
ensureTransform()
if (this._x != x || this._y != y) {
this._x = x
Expand Down Expand Up @@ -1704,8 +1701,7 @@ fun <T : View> T.size(size: Size): T {
return this
}
fun <T : View> T.size(width: Double, height: Double): T = size(Size(width, height))
fun <T : View> T.size(width: Float, height: Float): T = size(Size(width, height))
fun <T : View> T.size(width: Int, height: Int): T = size(Size(width, height))
inline fun <T : View> T.size(width: Number, height: Number): T = size(width.toDouble(), height.toDouble())

fun <T : View> T.globalPos(p: Point): T {
this.globalPos = p
Expand Down Expand Up @@ -1757,33 +1753,34 @@ fun <T : View> T.xy(p: Point): T {
this.pos = p
return this
}
fun <T : View> T.xy(x: Double, y: Double): T = xy(Point(x, y))
fun <T : View> T.xy(x: Float, y: Float): T = xy(Point(x, y))
fun <T : View> T.xy(x: Int, y: Int): T = xy(Point(x, y))
@Deprecated("")
fun <T : View> T.xy(p: MPoint): T = xy(p.point)
fun <T : View> T.xy(x: Double, y: Double): T {
setXY(x, y)
return this
}
inline fun <T : View> T.xy(x: Number, y: Number): T = xy(x.toDouble(), y.toDouble())

/** Chainable method returning this that sets [View.x] and [View.y] */
fun <T : View> T.position(x: Double, y: Double): T = xy(Point(x, y))
fun <T : View> T.position(x: Float, y: Float): T = xy(Point(x, y))
fun <T : View> T.position(x: Int, y: Int): T = xy(Point(x, y))
fun <T : View> T.position(p: Point): T = xy(p)
fun <T : View> T.position(x: Double, y: Double): T = xy(Point(x, y))
inline fun <T : View> T.position(x: Number, y: Number): T = xy(x.toDouble(), y.toDouble())

fun <T : View> T.bounds(left: Double, top: Double, right: Double, bottom: Double): T = xy(left, top).size(Size(right - left, bottom - top))
fun <T : View> T.bounds(rect: Rectangle): T = bounds(rect.left.toDouble(), rect.top.toDouble(), rect.right.toDouble(), rect.bottom.toDouble())
fun <T : View> T.bounds(left: Double, top: Double, right: Double, bottom: Double): T = xy(left, top).size(right - left, bottom - top)
inline fun <T : View> T.bounds(left: Number, top: Number, right: Number, bottom: Number): T = bounds(left.toDouble(), top.toDouble(), right.toDouble(), bottom.toDouble())
fun <T : View> T.bounds(rect: Rectangle): T = bounds(rect.left, rect.top, rect.right, rect.bottom)

fun <T : View> T.positionX(x: Double): T {
this.x = x
return this
}
fun <T : View> T.positionX(x: Float): T = positionX(x.toDouble())
fun <T : View> T.positionX(x: Int): T = positionX(x.toDouble())
inline fun <T : View> T.positionX(x: Number): T = positionX(x.toDouble())

fun <T : View> T.positionY(y: Double): T {
this.y = y
return this
}
fun <T : View> T.positionY(y: Float): T = positionY(y.toDouble())
fun <T : View> T.positionY(y: Int): T = positionY(y.toDouble())
inline fun <T : View> T.positionY(y: Number): T = positionY(y.toDouble())

fun View.getPositionRelativeTo(view: View): Point {
val mat = this.parent!!.getConcatMatrix(view, inclusive = false)
Expand Down Expand Up @@ -1822,12 +1819,10 @@ fun <T : View> T.skew(sx: Angle, sy: Angle): T {

/** Chainable method returning this that sets [View.scaleXD] and [View.scaleYD] */
fun <T : View> T.scale(sx: Double, sy: Double = sx): T {
this.scaleX = sx
this.scaleY = sy
this.scaleXY = Scale(sx, sy)
return this
}
fun <T : View> T.scale(sx: Float, sy: Float = sx): T = scale(sx.toDouble(), sy.toDouble())
fun <T : View> T.scale(sx: Int, sy: Int = sx): T = scale(sx.toDouble(), sy.toDouble())
inline fun <T : View> T.scale(sx: Number, sy: Number = sx): T = scale(sx.toDouble(), sy.toDouble())

/** Chainable method returning this that sets [View.colorMul] */
fun <T : View> T.colorMul(color: RGBA): T {
Expand All @@ -1840,15 +1835,13 @@ fun <T : View> T.alpha(alpha: Float): T {
this.alphaF = alpha
return this
}
fun <T : View> T.alpha(alpha: Double): T = alpha(alpha.toFloat())
fun <T : View> T.alpha(alpha: Int): T = alpha(alpha.toFloat())
inline fun <T : View> T.alpha(alpha: Number): T = alpha(alpha.toFloat())

fun <T : View> T.zIndex(index: Double): T {
this.zIndex = index
return this
}
fun <T : View> T.zIndex(index: Float): T = zIndex(index.toDouble())
fun <T : View> T.zIndex(index: Int): T = zIndex(index.toDouble())
inline fun <T : View> T.zIndex(index: Number): T = zIndex(index.toDouble())

typealias ViewDslMarker = korlibs.math.annotations.ViewDslMarker
// @TODO: This causes issues having to put some explicit this@ when it shouldn't be required
Expand Down
4 changes: 2 additions & 2 deletions korge/src/common/korlibs/korge/view/vector/GpuShapeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ open class GpuShapeView(
}

override fun getLocalBoundsInternal(): Rectangle = Rectangle(
shapeBounds.x - anchorDispXF,
shapeBounds.y - anchorDispYF,
shapeBounds.x - anchorDispX,
shapeBounds.y - anchorDispY,
shapeBounds.width,
shapeBounds.height,
)
Expand Down
4 changes: 2 additions & 2 deletions korge/src/jvm/korlibs/korge/awt/ViewsDebuggerActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ internal open class ViewsDebuggerActions(val views: Views) {
}

var playing: Boolean
get() = root.speed != 0f
get() = root.speed != 0.0
set(value) {
root.speed = if (value) 1f else 0f
root.speed = if (value) 1.0 else 0.0
}

fun togglePlay() {
Expand Down

0 comments on commit 761b305

Please sign in to comment.