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
inline fun Panel.square(init: SquareBuilder.() -> Unit): Square { | |
return SquareBuilder().apply { init() }.build().also { this.addShape(it) } | |
} |
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
@ShapeDsl | |
class SquareBuilder: ShapeBuilder() { | |
override fun build() = Square(lines, char) | |
} |
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
inline fun panel(init: (@ShapeDsl Panel).() -> Unit): Panel { | |
return Panel().apply { init() } | |
} | |
inline fun Panel.square(init: (@ShapeDsl SquareBuilder).() -> Unit): Square { | |
return SquareBuilder().apply { init() }.build().also { this.addShape(it) } | |
} |
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
@DslMarker | |
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE) | |
annotation class ShapeDsl |
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
panel { | |
square { | |
lines = 8 | |
char = 'd' | |
square { | |
lines = 5 | |
char = 'o' | |
triangle { |
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
inline fun panel(init: Panel.() -> Unit): Panel { | |
return Panel().apply { init() } | |
} | |
inline fun Panel.square(init: SquareBuilder.() -> Unit): Square { | |
return SquareBuilder().apply { init() }.build().also { this.addShape(it) } | |
} | |
inline fun Panel.triangle(init: TriangleBuilder.() -> Unit): Triangle { | |
return TriangleBuilder().apply { init() }.build().also { this.addShape(it) } |
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
operator fun Shape.plus(shape: Shape): ComposedShape = this union shape | |
operator fun Shape.minus(shape: Shape): ComposedShape = this intersection shape |
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
panel { | |
val square = square { | |
lines = 8 | |
char = 'd' | |
} | |
space() | |
val triangle = triangle { | |
lines = 7 | |
char = 's' | |
} |
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
panel { | |
... | |
space() | |
addShape(square union rhombus) | |
... | |
space() | |
composed { ComposedShape(rhombus, triangle, ComposedShape.Operation.INTERSECTION) } | |
... | |
}.print() |
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
fun Panel.composed(init: Panel.() -> ComposedShape): ComposedShape { | |
return init().also { this.addShape(it) } | |
} |