scoop:scoop.ui.component:0.1.0
src/overlays/arrow.scoop
Selected production source · src/overlays/arrow.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.min5
import scoop.math.max6
import scoop.ui.*8
public class PopupArrow public constructor(9
public val width: Double = 10.0,10
public val height: Double = 5.0,11
public val paint: Paint? = None12
) {13
init {14
nonnegative(width, "invalid popup arrow width")15
nonnegative(height, "invalid popup arrow height")16
}17
}19
internal class PopupArrowInk(20
val width: Double,21
val height: Double,22
val inset: Double,23
val paint: Paint24
)26
internal fun popupArrowInk(27
arrow: PopupArrow?,28
density: Double,29
background: Paint?30
): PopupArrowInk? {31
val value = arrow ?: return None32
val paint = value.paint ?: background ?: return None33
return Some(PopupArrowInk(value.width * density, value.height * density, 8.0 * density, paint))34
}36
/** Rounded tip on a unit triangle; the same geometry is used on every physical side. */37
internal fun popupArrowPath(): Path =38
path {39
moveTo(Point(-0.5, -0.1))40
lineTo(Point(-0.12, 0.76))41
quadraticTo(Point(0.0, 1.0), Point(0.12, 0.76))42
lineTo(Point(0.5, -0.1))43
closeContour()44
}46
internal fun drawPopupArrow(context: SceneContext, shape: Path, ink: PopupArrowInk) {47
val layout = context.overlayLayout ?: return48
val anchor = layout.anchor ?: return49
val side = layout.side ?: return50
val size = context.size51
val vertical = side == OverlaySide.Top || side == OverlaySide.Bottom52
val edge =53
if (vertical)54
size.width55
else56
size.height57
val width = min(ink.width, max(0.0, edge - ink.inset * 2.0))58
if (width == 0.0 || ink.height == 0.0)59
return60
val wanted =61
if (vertical)62
anchor.x + anchor.width / 2.0 - layout.bounds.x63
else64
anchor.y + anchor.height / 2.0 - layout.bounds.y65
val center = max(width / 2.0 + ink.inset, min(wanted, edge - width / 2.0 - ink.inset))66
context.draw {67
paint -> paint.canvas.withState {68
canvas ->69
when (side) {70
case Top -> canvas.translate(center, size.height)71
case Bottom -> {72
canvas.translate(center, 0.0);73
canvas.scale(1.0, -1.0)74
}75
case Left -> {76
canvas.translate(size.width, center);77
canvas.rotate(-1.5707963267948966)78
}79
case Right -> {80
canvas.translate(0.0, center);81
canvas.rotate(1.5707963267948966)82
}83
else -> return@withState84
}85
canvas.scale(width, ink.height)86
canvas.drawPath(shape, ink.paint)87
}88
}89
}