Skip to content

Commit

Permalink
Feature/dsl.marker (#253)
Browse files Browse the repository at this point in the history
* Annotate View with KorDslMarker from KorMA. Proper put the right receiver on fill/stroke callbacks

* Remove ViewsDslMarker, since all the receivers extends View that already have the KorDslMarker

* Fixed explicit this inside View inheritors.
Shouldn't be an issue on most userland code since people is probably not creating custom views

* Bump korim version too
  • Loading branch information
soywiz authored Jul 8, 2020
1 parent 49601c3 commit 348d762
Show file tree
Hide file tree
Showing 39 changed files with 97 additions and 92 deletions.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ version=1.13.9-SNAPSHOT
klockVersion=1.11.13
korgwVersion=1.12.24
korauVersion=1.11.10
korimVersion=1.12.28
korimVersion=1.12.29
kormaVersion=1.11.20
kryptoVersion=1.11.1
kloggerVersion=1.10.1
korinjectVersion=1.10.2
Expand Down
2 changes: 2 additions & 0 deletions korge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ korlibs {
val klockVersion: String by project
val korauVersion: String by project
val korimVersion: String by project
val kormaVersion: String by project
val korgwVersion: String by project
val kryptoVersion: String by project
val korinjectVersion: String by project
Expand All @@ -25,6 +26,7 @@ val kloggerVersion: String by project
dependencies {
add("commonMainApi", "com.soywiz.korlibs.klock:klock:$klockVersion")
add("commonMainApi", "com.soywiz.korlibs.korau:korau:$korauVersion")
add("commonMainApi", "com.soywiz.korlibs.korma:korma:$kormaVersion")
add("commonMainApi", "com.soywiz.korlibs.korim:korim:$korimVersion")
add("commonMainApi", "com.soywiz.korlibs.korgw:korgw:$korgwVersion")
add("commonMainApi", "com.soywiz.korlibs.krypto:krypto:$kryptoVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ class AnEmptyView(override val library: AnLibrary, override val symbol: AnSymbol
class AnTextField(override val library: AnLibrary, override val symbol: AnTextFieldSymbol) : Container(),
AnElement, IText, IHtml {
private val textField = Text("", 16.0).apply {
textBounds.copyFrom(symbol.bounds)
html = symbol.initialHtml
textBounds.copyFrom(this@AnTextField.symbol.bounds)
html = this@AnTextField.symbol.initialHtml
relayout()
}

Expand Down
23 changes: 13 additions & 10 deletions korge/src/commonMain/kotlin/com/soywiz/korge/input/MouseEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,18 @@ internal inline fun <T : View?> T?.doMouseEvent(prop: KProperty1<MouseEvents, Si
this?.mouse?.let { mouse -> prop.get(mouse).add { launchImmediately(mouse.coroutineContext) { handler(it) } } }
return this
}

inline fun <T : View?> T.onClick(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::click, handler)
inline fun <T : View?> T.onOver(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::over, handler)
inline fun <T : View?> T.onOut(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::out, handler)
inline fun <T : View?> T.onDown(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::down, handler)
inline fun <T : View?> T.onDownFromOutside(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::downFromOutside, handler)
inline fun <T : View?> T.onUp(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::up, handler)
inline fun <T : View?> T.onUpOutside(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::upOutside, handler)
inline fun <T : View?> T.onUpAnywhere(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::upAnywhere, handler)
inline fun <T : View?> T.onMove(noinline handler: suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::move, handler)
@DslMarker
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
annotation class EventsDslMarker

inline fun <T : View?> T.onClick(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::click, handler)
inline fun <T : View?> T.onOver(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::over, handler)
inline fun <T : View?> T.onOut(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::out, handler)
inline fun <T : View?> T.onDown(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::down, handler)
inline fun <T : View?> T.onDownFromOutside(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::downFromOutside, handler)
inline fun <T : View?> T.onUp(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::up, handler)
inline fun <T : View?> T.onUpOutside(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::upOutside, handler)
inline fun <T : View?> T.onUpAnywhere(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::upAnywhere, handler)
inline fun <T : View?> T.onMove(noinline handler: @EventsDslMarker suspend (MouseEvents) -> Unit) = doMouseEvent(MouseEvents::move, handler)

fun ViewsScope.installMouseDebugExtensionOnce() = MouseEvents.installDebugExtensionOnce(views)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.soywiz.korma.geom.*

inline fun Container.particleEmitter(
emitter: ParticleEmitter, emitterPos: IPoint = IPoint(),
callback: @ViewsDslMarker ParticleEmitterView.() -> Unit = {}
callback: ParticleEmitterView.() -> Unit = {}
) = ParticleEmitterView(emitter, emitterPos).addTo(this, callback)

class ParticleEmitterView(val emitter: ParticleEmitter, emitterPos: IPoint = IPoint()) : View() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.soywiz.korim.bitmap.*
import com.soywiz.korim.color.*
import com.soywiz.korma.geom.*

inline fun Container.tiledMapView(tiledMap: TiledMap, showShapes: Boolean = true, callback: @ViewsDslMarker TiledMapView.() -> Unit = {}) =
inline fun Container.tiledMapView(tiledMap: TiledMap, showShapes: Boolean = true, callback: TiledMapView.() -> Unit = {}) =
TiledMapView(tiledMap, showShapes).addTo(this, callback)

class TiledMapView(val tiledMap: TiledMap, showShapes: Boolean = true) : Container() {
Expand All @@ -34,7 +34,7 @@ class TiledMapView(val tiledMap: TiledMap, showShapes: Boolean = true) : Contain
}
is TiledMap.Layer.Objects.Rect -> {
if (gid != null) {
val tileTex = tiledMap.tileset[gid] ?: Bitmaps.transparent
val tileTex = this@TiledMapView.tiledMap.tileset[gid] ?: Bitmaps.transparent
//println("tileTex[gid=$gid]: $tileTex!")
shouldShow = true
image(tileTex)
Expand Down
4 changes: 2 additions & 2 deletions korge/src/commonMain/kotlin/com/soywiz/korge/ui/IconButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ inline fun Container.iconButton(
height: Number,
skin: UISkin = defaultUISkin,
iconSkin: IconSkin = defaultCheckSkin,
block: @ViewsDslMarker UIButton.() -> Unit = {}
block: UIButton.() -> Unit = {}
): IconButton = iconButton(width.toDouble(), height.toDouble(), skin, iconSkin, block)

inline fun Container.iconButton(
width: Double = 128.0,
height: Double = 64.0,
skin: UISkin = defaultUISkin,
iconSkin: IconSkin = defaultCheckSkin,
block: @ViewsDslMarker UIButton.() -> Unit = {}
block: UIButton.() -> Unit = {}
): IconButton = IconButton(width, height, skin, iconSkin).addTo(this).apply(block)

open class IconButton(
Expand Down
4 changes: 2 additions & 2 deletions korge/src/commonMain/kotlin/com/soywiz/korge/ui/TextButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inline fun Container.textButton(
text: String = "Button",
skin: UISkin = defaultUISkin,
textFont: Html.FontFace = defaultUIFont,
block: @ViewsDslMarker TextButton.() -> Unit = {}
block: TextButton.() -> Unit = {}
): TextButton = textButton(width.toDouble(), height.toDouble(), text, skin, textFont, block)

inline fun Container.textButton(
Expand All @@ -21,7 +21,7 @@ inline fun Container.textButton(
text: String = "Button",
skin: UISkin = defaultUISkin,
textFont: Html.FontFace = defaultUIFont,
block: @ViewsDslMarker TextButton.() -> Unit = {}
block: TextButton.() -> Unit = {}
): TextButton = TextButton(width, height, text, skin, textFont).addTo(this).apply(block)

open class TextButton(
Expand Down
4 changes: 2 additions & 2 deletions korge/src/commonMain/kotlin/com/soywiz/korge/ui/UIButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ inline fun Container.uiButton(
width: Number,
height: Number,
skin: UISkin = defaultUISkin,
block: @ViewsDslMarker UIButton.() -> Unit = {}
block: UIButton.() -> Unit = {}
): UIButton = uiButton(width.toDouble(), height.toDouble(), skin, block)

inline fun Container.uiButton(
width: Double = 128.0,
height: Double = 64.0,
skin: UISkin = defaultUISkin,
block: @ViewsDslMarker UIButton.() -> Unit = {}
block: UIButton.() -> Unit = {}
): UIButton = UIButton(width, height, skin).addTo(this).apply(block)

open class UIButton(
Expand Down
4 changes: 2 additions & 2 deletions korge/src/commonMain/kotlin/com/soywiz/korge/ui/UICheckBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inline fun Container.uiCheckBox(
textFont: Html.FontFace = defaultUIFont,
skin: UISkin = defaultUISkin,
checkIcon: IconSkin = defaultCheckSkin,
block: @ViewsDslMarker UICheckBox.() -> Unit = {}
block: UICheckBox.() -> Unit = {}
): UICheckBox = uiCheckBox(width.toDouble(), height.toDouble(), checked, text, textFont, skin, checkIcon, block)

inline fun Container.uiCheckBox(
Expand All @@ -27,7 +27,7 @@ inline fun Container.uiCheckBox(
textFont: Html.FontFace = defaultUIFont,
skin: UISkin = defaultUISkin,
checkIcon: IconSkin = defaultCheckSkin,
block: @ViewsDslMarker UICheckBox.() -> Unit = {}
block: UICheckBox.() -> Unit = {}
): UICheckBox = UICheckBox(width, height, checked, text, textFont, skin, checkIcon).addTo(this).apply(block)

open class UICheckBox(
Expand Down
10 changes: 5 additions & 5 deletions korge/src/commonMain/kotlin/com/soywiz/korge/ui/UIComboBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ inline fun <T> Container.uiComboBox(
items: List<T>,
verticalScroll: Boolean = true,
skin: ComboBoxSkin = defaultComboBoxSkin,
block: @ViewsDslMarker UIComboBox<T>.() -> Unit = {}
block: UIComboBox<T>.() -> Unit = {}
) = uiComboBox(width.toDouble(), height.toDouble(), selectedIndex, items, verticalScroll, skin, block)

inline fun <T> Container.uiComboBox(
Expand All @@ -23,7 +23,7 @@ inline fun <T> Container.uiComboBox(
items: List<T>,
verticalScroll: Boolean = true,
skin: ComboBoxSkin = defaultComboBoxSkin,
block: @ViewsDslMarker UIComboBox<T>.() -> Unit = {}
block: UIComboBox<T>.() -> Unit = {}
) = UIComboBox(width, height, selectedIndex, items, verticalScroll, skin).addTo(this).apply(block)

open class UIComboBox<T>(
Expand Down Expand Up @@ -90,10 +90,10 @@ open class UIComboBox<T>(
itemsView.container.removeChildren()
for ((index, item) in items.withIndex()) {
itemsView.container.textButton(width - 32, itemHeight, item.toString(), skin.itemSkin, skin.textFont) {
position(0, index * itemHeight)
position(0, index * this@UIComboBox.itemHeight)
onClick {
showItems = false
selectedIndex = index
this@UIComboBox.showItems = false
this@UIComboBox.selectedIndex = index
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.soywiz.korge.view.*
inline fun Container.uiProgressBar(
width: Number, height: Number, current: Number, maximum: Number,
skin: UISkin = defaultUISkin,
block: @ViewsDslMarker UIProgressBar.() -> Unit = {}
block: UIProgressBar.() -> Unit = {}
): UIProgressBar = uiProgressBar(width.toDouble(), height.toDouble(), current.toDouble(), maximum.toDouble(), skin, block)

inline fun Container.uiProgressBar(
Expand All @@ -15,7 +15,7 @@ inline fun Container.uiProgressBar(
current: Double = 0.0,
maximum: Double = 1.0,
skin: UISkin = defaultUISkin,
block: @ViewsDslMarker UIProgressBar.() -> Unit = {}
block: UIProgressBar.() -> Unit = {}
): UIProgressBar = UIProgressBar(width, height, current, maximum, skin).addTo(this).apply(block)

open class UIProgressBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ inline fun Container.uiScrollBar(
width: Number, height: Number, current: Number, pageSize: Number, totalSize: Number, buttonSize: Number, stepSize: Number,
direction: Direction = if (width.toDouble() > height.toDouble()) Direction.Horizontal else Direction.Vertical,
skin: ScrollBarSkin = if (direction == Direction.Horizontal) defaultHorScrollBarSkin else defaultVerScrollBarSkin,
block: @ViewsDslMarker UIScrollBar.() -> Unit = {}
block: UIScrollBar.() -> Unit = {}
): UIScrollBar = uiScrollBar(width.toDouble(), height.toDouble(), current.toDouble(), pageSize.toDouble(), totalSize.toDouble(), buttonSize.toDouble(),
stepSize.toDouble(), direction, skin, block)

Expand All @@ -27,7 +27,7 @@ inline fun Container.uiScrollBar(
stepSize: Double = pageSize / 10.0,
direction: Direction = Direction.auto(width, height),
skin: ScrollBarSkin = if (direction == Direction.Horizontal) defaultHorScrollBarSkin else defaultVerScrollBarSkin,
block: @ViewsDslMarker UIScrollBar.() -> Unit = {}
block: UIScrollBar.() -> Unit = {}
): UIScrollBar = UIScrollBar(width, height, current, pageSize, totalSize, buttonSize, stepSize, direction, skin).addTo(this).apply(block)

open class UIScrollBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ inline fun Container.uiScrollableArea(
horizontalScroll: Boolean = true,
horSkin: ScrollBarSkin = defaultHorScrollBarSkin,
verSkin: ScrollBarSkin = defaultVerScrollBarSkin,
config: @ViewsDslMarker UIScrollableArea.() -> Unit = {},
block: @ViewsDslMarker Container.() -> Unit = {}
config: UIScrollableArea.() -> Unit = {},
block: Container.() -> Unit = {}
): UIScrollableArea = uiScrollableArea(
width.toDouble(), height.toDouble(), contentWidth.toDouble(), contentHeight.toDouble(), buttonSize.toDouble(),
verticalScroll, horizontalScroll, horSkin, verSkin, config, block
Expand All @@ -30,8 +30,8 @@ inline fun Container.uiScrollableArea(
horizontalScroll: Boolean = true,
horSkin: ScrollBarSkin = defaultHorScrollBarSkin,
verSkin: ScrollBarSkin = defaultVerScrollBarSkin,
config: @ViewsDslMarker UIScrollableArea.() -> Unit = {},
block: @ViewsDslMarker Container.() -> Unit = {}
config: UIScrollableArea.() -> Unit = {},
block: Container.() -> Unit = {}
): UIScrollableArea = UIScrollableArea(width, height, contentWidth, contentHeight, buttonSize, verticalScroll, horizontalScroll, horSkin, verSkin)
.addTo(this).apply(config).also { block(it.container) }

Expand Down Expand Up @@ -65,8 +65,8 @@ open class UIScrollableArea(
val clipContainer = clipContainer(viewportWidth, viewportHeight)
val container = clipContainer.fixedSizeContainer(contentWidth, contentHeight)

val horScrollBar = uiScrollBar(width, buttonSize, skin = horSkin) { onChange { onMoved() } }
val verScrollBar = uiScrollBar(buttonSize, height, skin = verSkin) { onChange { onMoved() } }
val horScrollBar = uiScrollBar(width, buttonSize, skin = horSkin) { onChange { this@UIScrollableArea.onMoved() } }
val verScrollBar = uiScrollBar(buttonSize, height, skin = verSkin) { onChange { this@UIScrollableArea.onMoved() } }

init {
calculateSizes()
Expand Down
4 changes: 2 additions & 2 deletions korge/src/commonMain/kotlin/com/soywiz/korge/ui/UIText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import com.soywiz.korma.geom.*
inline fun Container.uiText(
text: String, width: Number, height: Number,
skin: TextSkin = defaultTextSkin,
block: @ViewsDslMarker UIText.() -> Unit = {}
block: UIText.() -> Unit = {}
): UIText = uiText(text, width.toDouble(), height.toDouble(), skin, block)

inline fun Container.uiText(
text: String,
width: Double = 128.0,
height: Double = 64.0,
skin: TextSkin = defaultTextSkin,
block: @ViewsDslMarker UIText.() -> Unit = {}
block: UIText.() -> Unit = {}
): UIText = UIText(text, width, height, skin).addTo(this).apply(block)

class UIText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.soywiz.korma.interpolation.*
/**
* Creates a new [Camera] and attaches to [this] [Container]. The [callback] argument is called with the [Camera] injected as this to be able to configure the camera.
*/
inline fun Container.camera(callback: @ViewsDslMarker Camera.() -> Unit = {}): Camera = Camera().addTo(this, callback)
inline fun Container.camera(callback: Camera.() -> Unit = {}): Camera = Camera().addTo(this, callback)

/**
* A [Camera] is a [Container] that allows to center its position using [setTo] and [tweenTo] methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class Circle(
private fun updateGraphics() {
clear()
fill(Colors.WHITE) {
circle(radius, radius, radius)
circle(this@Circle.radius, this@Circle.radius, this@Circle.radius)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.soywiz.korge.view

@Deprecated("Kotlin/Native boxes inline+Number")
inline fun Container.clipContainer(width: Number, height: Number, callback: @ViewsDslMarker ClipContainer.() -> Unit = {}) =
inline fun Container.clipContainer(width: Number, height: Number, callback: ClipContainer.() -> Unit = {}) =
clipContainer(width.toDouble(), height.toDouble(), callback)

inline fun Container.clipContainer(width: Double, height: Double, callback: @ViewsDslMarker ClipContainer.() -> Unit = {}) =
inline fun Container.clipContainer(width: Double, height: Double, callback: ClipContainer.() -> Unit = {}) =
ClipContainer(width, height).addTo(this, callback)

open class ClipContainer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.soywiz.korge.render.*
import com.soywiz.korma.geom.*

/** Creates a new [Container], allowing to configure with [callback], and attaches the newly created container to the receiver this [Container] */
inline fun Container.container(callback: @ViewsDslMarker Container.() -> Unit = {}) =
inline fun Container.container(callback: Container.() -> Unit = {}) =
Container().addTo(this, callback)

// For Flash compatibility
Expand Down Expand Up @@ -232,5 +232,5 @@ operator fun View?.plusAssign(view: View?) {
if (view != null) container?.addChild(view)
}

inline fun <T : View> T.addTo(instance: Container, callback: @ViewsDslMarker T.() -> Unit = {}) =
inline fun <T : View> T.addTo(instance: Container, callback: T.() -> Unit = {}) =
this.addTo(instance).apply(callback)
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ open class Ellipse(
clear()
fill(Colors.WHITE) {
//ellipse(radiusX, radiusY, radiusX, radiusY)
ellipse(0.0, 0.0, radiusX, radiusY)
ellipse(0.0, 0.0, this@Ellipse.radiusX, this@Ellipse.radiusY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import com.soywiz.korge.render.*
import com.soywiz.korio.util.*
import com.soywiz.korma.geom.*

inline fun Container.fixedSizeContainer(width: Double, height: Double, clip: Boolean = false, callback: @ViewsDslMarker FixedSizeContainer.() -> Unit = {}) =
inline fun Container.fixedSizeContainer(width: Double, height: Double, clip: Boolean = false, callback: FixedSizeContainer.() -> Unit = {}) =
FixedSizeContainer(width, height, clip).addTo(this, callback)

inline fun Container.fixedSizeContainer(width: Int, height: Int, clip: Boolean = false, callback: @ViewsDslMarker FixedSizeContainer.() -> Unit = {}) =
inline fun Container.fixedSizeContainer(width: Int, height: Int, clip: Boolean = false, callback: FixedSizeContainer.() -> Unit = {}) =
FixedSizeContainer(width.toDouble(), height.toDouble(), clip).addTo(this, callback)

@Deprecated("Kotlin/Native boxes inline+Number")
inline fun Container.fixedSizeContainer(width: Number, height: Number, clip: Boolean = false, callback: @ViewsDslMarker FixedSizeContainer.() -> Unit = {}) =
inline fun Container.fixedSizeContainer(width: Number, height: Number, clip: Boolean = false, callback: FixedSizeContainer.() -> Unit = {}) =
fixedSizeContainer(width.toDouble(), height.toDouble(), clip, callback)


Expand Down
Loading

0 comments on commit 348d762

Please sign in to comment.