scoop:scoop.ui:0.1.0
src/text/handles/scene.scoop
Selected production source · src/text/handles/scene.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.math.*5
import scoop.surface.InputArea6
import scoop.ui.*8
private class TextHandleShape(val caret: Rect, val tip: Point, var center: Point, var hit: Rect) {9
fun moveX(x: Double) {10
hit = Rect(hit.x + x - center.x, hit.y, hit.width, hit.height)11
center = Point(x, center.y)12
}13
}15
internal fun textHandleScene(16
host: TextSelectionHandles,17
context: SceneContext,18
bounds: Rect,19
selection: EditorSelection,20
anchor: Rect?,21
cursor: Rect?,22
color: Color23
) {24
if (host.closed || !host.visible || !host.enabled || !host.area.scope.activity.hasFocus)25
return26
val visible = context.textHandleBounds(bounds) ?: return27
val collapsed = selection.anchor.byteOffset == selection.cursor.byteOffset28
val activeAnchor = host.contact?.anchor ?: false29
val a = anchor ?: return30
val c = cursor ?: return31
val radius = host.options.radius32
val direction =33
if (a.x <= c.x)34
-1.035
else36
1.037
val first =38
textHandleShape(39
a,40
visible,41
radius,42
host.options.hitRadius,43
if (collapsed)44
0.045
else46
direction47
)48
val last =49
textHandleShape(50
c,51
visible,52
radius,53
host.options.hitRadius,54
if (collapsed)55
0.056
else57
-direction58
)59
if (!collapsed) {60
textHandleSeparate(first, last, visible, radius)61
textHandleDivide(first, last)62
}63
val paint = Paint(host.options.color ?: color)64
context.withState {65
scene ->66
scene.clipRect(bounds)67
if (!collapsed || activeAnchor)68
textHandleDraw(scene, host.anchorArea, first, radius, paint)69
if (!collapsed || !activeAnchor)70
textHandleDraw(scene, host.cursorArea, last, radius, paint)71
Unit72
}73
}75
private fun textHandleShape(76
caret: Rect,77
bounds: Rect,78
radius: Double,79
hitRadius: Double,80
direction: Double81
): TextHandleShape {82
val insetX = min(radius, bounds.width / 2.0)83
val insetY = min(radius, bounds.height / 2.0)84
val center =85
Point(86
min(87
bounds.x + bounds.width - insetX,88
max(bounds.x + insetX, caret.x + caret.width / 2.0 + direction * radius)89
),90
min(91
bounds.y + bounds.height - insetY,92
max(bounds.y + insetY, caret.y + caret.height + radius)93
)94
)95
val tip =96
Point(97
min(bounds.x + bounds.width, max(bounds.x, caret.x + caret.width / 2.0)),98
min(bounds.y + bounds.height, max(bounds.y, caret.y + caret.height))99
)100
return TextHandleShape(101
caret,102
tip,103
center,104
Rect(center.x - hitRadius, center.y - hitRadius, hitRadius * 2.0, hitRadius * 2.0)105
)106
}108
private fun textHandleSeparate(109
a: TextHandleShape,110
b: TextHandleShape,111
bounds: Rect,112
radius: Double113
) {114
if (hypot(a.center.x - b.center.x, a.center.y - b.center.y) >= radius * 2.0)115
return116
val inset = min(radius, bounds.width / 2.0)117
val spread = min(radius, (bounds.width - inset * 2.0) / 2.0)118
val middle =119
min(120
bounds.x + bounds.width - inset - spread,121
max(bounds.x + inset + spread, (a.center.x + b.center.x) / 2.0)122
)123
if (a.caret.x <= b.caret.x) {124
a.moveX(middle - spread)125
b.moveX(middle + spread)126
} else {127
a.moveX(middle + spread)128
b.moveX(middle - spread)129
}130
}132
private fun textHandleDivide(a: TextHandleShape, b: TextHandleShape) {133
if (intersectReveal(a.hit, b.hit) == None)134
return135
if (abs(a.center.x - b.center.x) >= abs(a.center.y - b.center.y)) {136
val middle = (a.center.x + b.center.x) / 2.0137
val left =138
if (a.center.x <= b.center.x)139
a140
else141
b142
val right =143
if (a.center.x <= b.center.x)144
b145
else146
a147
left.hit = Rect(left.hit.x, left.hit.y, max(0.0, middle - left.hit.x), left.hit.height)148
right.hit =149
Rect(150
middle,151
right.hit.y,152
max(0.0, right.hit.x + right.hit.width - middle),153
right.hit.height154
)155
} else {156
val middle = (a.center.y + b.center.y) / 2.0157
val top =158
if (a.center.y <= b.center.y)159
a160
else161
b162
val bottom =163
if (a.center.y <= b.center.y)164
b165
else166
a167
top.hit = Rect(top.hit.x, top.hit.y, top.hit.width, max(0.0, middle - top.hit.y))168
bottom.hit =169
Rect(170
bottom.hit.x,171
middle,172
bottom.hit.width,173
max(0.0, bottom.hit.y + bottom.hit.height - middle)174
)175
}176
}178
private fun textHandleDraw(179
context: SceneContext,180
area: InputArea,181
shape: TextHandleShape,182
radius: Double,183
paint: Paint184
) {185
context.place(area, shape.hit)186
context.draw {187
drawing ->188
drawing.canvas.drawRect(shape.caret, paint)189
drawing.canvas.drawLine(shape.tip, shape.center, paint)190
drawing.canvas.drawCircle(shape.center, radius, paint)191
}192
}