Scoop API

scoop:scoop.ui:0.1.0

src/text/handles/scene.scoop

Selected production source · src/text/handles/scene.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.math.*
5import scoop.surface.InputArea
6import scoop.ui.*
8private 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 }
15internal fun textHandleScene(
16 host: TextSelectionHandles,
17 context: SceneContext,
18 bounds: Rect,
19 selection: EditorSelection,
20 anchor: Rect?,
21 cursor: Rect?,
22 color: Color
23) {
24 if (host.closed || !host.visible || !host.enabled || !host.area.scope.activity.hasFocus)
25 return
26 val visible = context.textHandleBounds(bounds) ?: return
27 val collapsed = selection.anchor.byteOffset == selection.cursor.byteOffset
28 val activeAnchor = host.contact?.anchor ?: false
29 val a = anchor ?: return
30 val c = cursor ?: return
31 val radius = host.options.radius
32 val direction =
33 if (a.x <= c.x)
34 -1.0
35 else
36 1.0
37 val first =
38 textHandleShape(
39 a,
40 visible,
41 radius,
42 host.options.hitRadius,
43 if (collapsed)
44 0.0
45 else
46 direction
47 )
48 val last =
49 textHandleShape(
50 c,
51 visible,
52 radius,
53 host.options.hitRadius,
54 if (collapsed)
55 0.0
56 else
57 -direction
58 )
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 Unit
72 }
75private fun textHandleShape(
76 caret: Rect,
77 bounds: Rect,
78 radius: Double,
79 hitRadius: Double,
80 direction: Double
81): 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 )
108private fun textHandleSeparate(
109 a: TextHandleShape,
110 b: TextHandleShape,
111 bounds: Rect,
112 radius: Double
113) {
114 if (hypot(a.center.x - b.center.x, a.center.y - b.center.y) >= radius * 2.0)
115 return
116 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 }
132private fun textHandleDivide(a: TextHandleShape, b: TextHandleShape) {
133 if (intersectReveal(a.hit, b.hit) == None)
134 return
135 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.0
137 val left =
138 if (a.center.x <= b.center.x)
139 a
140 else
141 b
142 val right =
143 if (a.center.x <= b.center.x)
144 b
145 else
146 a
147 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.height
154 )
155 } else {
156 val middle = (a.center.y + b.center.y) / 2.0
157 val top =
158 if (a.center.y <= b.center.y)
159 a
160 else
161 b
162 val bottom =
163 if (a.center.y <= b.center.y)
164 b
165 else
166 a
167 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 }
178private fun textHandleDraw(
179 context: SceneContext,
180 area: InputArea,
181 shape: TextHandleShape,
182 radius: Double,
183 paint: Paint
184) {
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 }