-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPinchSelectingZone.pde
295 lines (279 loc) · 10.5 KB
/
PinchSelectingZone.pde
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
static final class PinchSelectingZone extends TextAreaTouchZone implements Observer {
private static final int MARK_WIDTH = 5;
private int selStartRow, selEndRow;
private final Point selStartTouchOffset = new Point(), selEndTouchOffset = new Point();
private Color myColor = new Color(0x40, 0x96, 0xb2, 240);
private final Map<Long, Boolean> touchRecords = new HashMap<Long, Boolean>();
public PinchSelectingZone(final String name, final TextArea textArea) {
super(name, textArea);
textArea.addObserver(this);
}
@Override public void update(final Observable o, final Object arg) {
if (arg instanceof TextSelectionEvent) {
final TextSelectionEvent event = (TextSelectionEvent) arg;
if (event.isInitial) {
selStartRow = event.startRow;
selEndRow = event.endRow;
}
}
}
@Override public void touchDown(final Touch touch) {
super.touchDown(touch);
resetTouches();
if (textArea.hasSelection()) {
final Touch[] touches = getTouches();
switch (touches.length) {
case 1:
bindTouch(touches[0]);
break;
case 2:
bindTouches(touches[0], touches[1]);
break;
}
}
}
@Override public void touchUp(final Touch touch) {
super.touchUp(touch);
resetTouches();
if (textArea.hasSelection()) {
final Touch[] touches = getTouches();
switch (touches.length) {
case 0:
textArea.setChanged();
textArea.notifyObservers(new TextSelectionEvent(textArea.getSelectionStart(), selStartRow, textArea.getSelectionEnd(), selEndRow, false, false));
break;
case 1:
bindTouch(touches[0]);
break;
case 2:
bindTouches(touches[0], touches[1]);
break;
}
}
}
@Override public void touchMoved(final Touch touch) {
super.touchMoved(touch);
final Boolean isStartMark = touchRecords.get(touch.sessionID);
if (isStartMark == null) {
return;
}
final Touch[] touches = getTouches();
if (touches.length <= 0 || touches.length > 2) {
return;
}
final boolean isStart = isStartMark.booleanValue();
int textOffset, otherTextOffset, row, otherRow;
if (isStart) {
textOffset = textArea.getSelectionStart();
otherTextOffset = textArea.getSelectionEnd();
row = selStartRow;
otherRow = selEndRow;
} else {
textOffset = textArea.getSelectionEnd();
otherTextOffset = textArea.getSelectionStart();
row = selEndRow;
otherRow = selStartRow;
}
final Point touchPoint = textArea.getInnerPointByPoint(touch.x, touch.y);
Point ip = new Point(touchPoint);
if (isStart) {
ip.translate(selStartTouchOffset.x, selStartTouchOffset.y);
} else {
ip.translate(selEndTouchOffset.x, selEndTouchOffset.y);
}
TextPosition tp = textArea.getTextPositionByInnerPoint(ip);
if (textOffset == tp.offset && row == tp.row) {
return;
}
if (touches.length == 1) {
ip = textArea.getInnerPointByTextPosition(tp.offset, tp.row);
if (row != tp.row && otherTextOffset == tp.offset) {
if (textArea.getLineOffset(tp.row) == tp.offset) {
++tp.offset;
} else if (textArea.getLineEnd(tp.row) == tp.offset) {
--tp.offset;
} else {
if (touchPoint.x <= ip.x) {
--tp.offset;
} else {
++tp.offset;
}
}
}
if (otherTextOffset == tp.offset) {
return;
}
textArea.setSelection(tp.offset, otherTextOffset);
if (isStart) {
selStartRow = tp.row;
} else {
selEndRow = tp.row;
}
final boolean newIsStart = tp.offset == textArea.getSelectionStart();
touchRecords.put(touch.sessionID, newIsStart);
if (newIsStart != isStart) {
final int tmpStart = selStartRow;
selStartRow = selEndRow;
selEndRow = tmpStart;
if (newIsStart) {
selStartTouchOffset.setLocation(selEndTouchOffset);
} else {
selEndTouchOffset.setLocation(selStartTouchOffset);
}
}
if (row == tp.row) {
if (newIsStart) {
selStartTouchOffset.y = ip.y - touchPoint.y;
} else {
selEndTouchOffset.y = ip.y - touchPoint.y;
}
}
return;
}
final Touch otherTouch = touches[0].sessionID == touch.sessionID ? touches[1] : touches[0];
final Point otherTouchPoint = textArea.getInnerPointByPoint(otherTouch.x, otherTouch.y);
final Point oip = new Point(otherTouchPoint);
if (isStart) {
oip.translate(selEndTouchOffset.x, selEndTouchOffset.y);
} else {
oip.translate(selStartTouchOffset.x, selStartTouchOffset.y);
}
boolean rebind = false;
if (row <= otherRow && ip.y > oip.y && touchPoint.y <= otherTouchPoint.y || row >= otherRow && ip.y < oip.y && touchPoint.y >= otherTouchPoint.y) {
ip.y = oip.y;
tp = textArea.getTextPositionByInnerPoint(ip);
rebind = true;
}
if (row == otherRow && tp.row == otherRow && (textOffset <= otherTextOffset && ip.x > oip.x && touchPoint.x <= otherTouchPoint.x || textOffset >= otherTextOffset && ip.x < oip.x && touchPoint.x >= otherTouchPoint.x)) {
tp.offset = textOffset <= otherTextOffset ? otherTextOffset - 1 : otherTextOffset + 1;
rebind = true;
}
if (textOffset == tp.offset && row == tp.row) {
if (rebind) {
bindTouches(touch, otherTouch);
}
return;
}
if (row != tp.row && otherTextOffset == tp.offset) {
if (textArea.getLineOffset(tp.row) == tp.offset) {
++tp.offset;
} else if (textArea.getLineEnd(tp.row) == tp.offset) {
--tp.offset;
} else if (touchPoint.x <= otherTouchPoint.x) {
--tp.offset;
} else {
++tp.offset;
}
}
if (otherTextOffset == tp.offset) {
if (rebind) {
bindTouches(touch, otherTouch);
}
return;
}
textArea.setSelection(tp.offset, otherTextOffset);
if (isStart) {
selStartRow = tp.row;
} else {
selEndRow = tp.row;
}
final boolean newIsStart = tp.offset == textArea.getSelectionStart();
touchRecords.put(touch.sessionID, newIsStart);
touchRecords.put(otherTouch.sessionID, !newIsStart);
if (newIsStart != isStart) {
final int tmpStart = selStartRow;
selStartRow = selEndRow;
selEndRow = tmpStart;
swapPoint(selStartTouchOffset, selEndTouchOffset);
}
rebind = rebind || row != tp.row && tp.row == otherRow;
rebind = rebind || tp.row > otherRow && touchPoint.y < otherTouchPoint.y || tp.row < otherRow && touchPoint.y > otherTouchPoint.y;
rebind = rebind || tp.row == otherRow && (tp.offset < otherTextOffset && touchPoint.x > otherTouchPoint.x || tp.offset > otherTextOffset && touchPoint.x < otherTouchPoint.x);
if (rebind) {
bindTouches(touch, otherTouch);
return;
}
if (row == tp.row) {
if (newIsStart) {
selStartTouchOffset.y = ip.y - touchPoint.y;
} else {
selEndTouchOffset.y = ip.y - touchPoint.y;
}
}
}
@Override void draw() {
super.draw();
if (textArea.hasSelection()) {
final Touch[] touches = getTouches();
if (touches.length >= 1) {
final int lineHeight = textArea.getFontHeight();
pushStyle();
if (touches.length == 1) {
final Boolean isStart = touchRecords.get(touches[0].sessionID);
if (isStart != null) {
if (isStart.booleanValue()) {
drawStartMark(lineHeight);
} else {
drawEndMark(lineHeight);
}
}
} else {
drawStartMark(lineHeight);
drawEndMark(lineHeight);
}
popStyle();
}
}
}
private void bindTouch(final Touch touch) {
final Point startOffset = textArea.getInnerPointByTextPosition(textArea.getSelectionStart(), selStartRow);
final Point endOffset = textArea.getInnerPointByTextPosition(textArea.getSelectionEnd(), selEndRow);
final Point innerPoint = textArea.getInnerPointByPoint(touch.x, touch.y);
startOffset.translate(-innerPoint.x, -innerPoint.y);
endOffset.translate(-innerPoint.x, -innerPoint.y);
final boolean isStart = startOffset.x * startOffset.x + startOffset.y * startOffset.y <= endOffset.x * endOffset.x + endOffset.y * endOffset.y;
touchRecords.put(touch.sessionID, isStart);
if (isStart) {
selStartTouchOffset.setLocation(startOffset);
} else {
selEndTouchOffset.setLocation(endOffset);
}
}
private void bindTouches(final Touch t0, final Touch t1) {
final boolean isT0Start = selStartRow == selEndRow ? t0.x <= t1.x : t0.y <= t1.y;
final Point startPoint = textArea.getInnerPointByTextPosition(textArea.getSelectionStart(), selStartRow);
final Point endPoint = textArea.getInnerPointByTextPosition(textArea.getSelectionEnd(), selEndRow);
final Point t0p = textArea.getInnerPointByPoint(t0.x, t0.y);
final Point t1p = textArea.getInnerPointByPoint(t1.x, t1.y);
if (isT0Start) {
touchRecords.put(t0.sessionID, true);
selStartTouchOffset.setLocation(startPoint.x - t0p.x, startPoint.y - t0p.y);
touchRecords.put(t1.sessionID, false);
selEndTouchOffset.setLocation(endPoint.x - t1p.x, endPoint.y - t1p.y);
} else {
touchRecords.put(t0.sessionID, false);
selEndTouchOffset.setLocation(endPoint.x - t0p.x, endPoint.y - t0p.y);
touchRecords.put(t1.sessionID, true);
selStartTouchOffset.setLocation(startPoint.x - t1p.x, startPoint.y - t1p.y);
}
}
private void resetTouches() {
selStartTouchOffset.setLocation(0, 0);
selEndTouchOffset.setLocation(0, 0);
touchRecords.clear();
}
private void drawStartMark(final int lineHeight) {
final Point point = textArea.getInnerPointByTextPosition(textArea.getSelectionStart(), selStartRow);
point.setLocation(point.x - 2, textArea.getLineTop(selStartRow));
noStroke();
fill(myColor.getRed(), myColor.getGreen(), myColor.getBlue(), myColor.getAlpha());
triangle(point.x, point.y, point.x, point.y + lineHeight, point.x - MARK_WIDTH, point.y + Math.round(lineHeight / 2.0));
}
private void drawEndMark(final int lineHeight) {
final Point point = textArea.getInnerPointByTextPosition(textArea.getSelectionEnd(), selEndRow);
point.setLocation(point.x + 2, textArea.getLineTop(selEndRow));
noStroke();
fill(myColor.getRed(), myColor.getGreen(), myColor.getBlue(), myColor.getAlpha());
triangle(point.x, point.y, point.x, point.y + lineHeight, point.x + MARK_WIDTH, point.y + Math.round(lineHeight / 2.0));
}
}