-
Notifications
You must be signed in to change notification settings - Fork 120
/
RoundRect.kt
70 lines (61 loc) · 2.28 KB
/
RoundRect.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.soywiz.korge.view
import com.soywiz.korge.ui.*
import com.soywiz.korim.color.*
import com.soywiz.korim.paint.Paint
import com.soywiz.korma.geom.vector.*
/** Creates a new [RoundRect] of size [width]x[height] and [color]
* and allows you to configure it via [callback].
* Once created, it is added to this receiver [Container].
**/
inline fun Container.roundRect(
width: Int,
height: Int,
rx: Int,
ry: Int = rx,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 0.0,
autoScaling: Boolean = true,
callback: @ViewDslMarker RoundRect.() -> Unit = {}
) = roundRect(width.toDouble(), height.toDouble(), rx.toDouble(), ry.toDouble(), fill, stroke, strokeThickness, autoScaling, callback)
inline fun Container.roundRect(
width: Double,
height: Double,
rx: Double,
ry: Double = rx,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 0.0,
autoScaling: Boolean = true,
callback: @ViewDslMarker RoundRect.() -> Unit = {}
) = RoundRect(width, height, rx, ry, fill, stroke, strokeThickness, autoScaling).addTo(this, callback)
/**
* A Rect [View] with rounded corners of size [width] and [height] with the initial [color].
*/
class RoundRect(
width: Double,
height: Double,
rx: Double,
ry: Double = rx,
fill: Paint = Colors.WHITE,
stroke: Paint = Colors.WHITE,
strokeThickness: Double = 0.0,
autoScaling: Boolean = true
) : ShapeView(shape = VectorPath(), fill = fill, stroke = stroke, strokeThickness = strokeThickness, autoScaling = autoScaling) {
override var width: Double by uiObservable(width) { updateGraphics() }
override var height: Double by uiObservable(height) { updateGraphics() }
var rx: Double by uiObservable(rx) { updateGraphics() }
var ry: Double by uiObservable(ry) { updateGraphics() }
/** The [color] of this [RoundRect]. Alias of [colorMul]. */
var color: RGBA by ::colorMul
init {
updateGraphics()
}
private fun updateGraphics() {
updatePath {
clear()
roundRect(0.0, 0.0, this@RoundRect.width, this@RoundRect.height, this@RoundRect.rx, this@RoundRect.ry)
assumeConvex = true // Optimization to avoid computing convexity
}
}
}