Skip to content

Commit df68cb0

Browse files
committed
Update double-clicking in text boxes to let you select by whole words
Additionally, fix a bug with selecting whole words with the arrow keys
1 parent fff3126 commit df68cb0

1 file changed

Lines changed: 79 additions & 23 deletions

File tree

  • src/main/java/meteordevelopment/meteorclient/gui/widgets/input

src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract class WTextBox extends WWidget {
4444
protected int cursor;
4545
protected double textStart;
4646

47-
protected boolean selecting;
47+
protected boolean selecting, doubleClick;
4848
protected int selectionStart, selectionEnd;
4949
private int preSelectionCursor;
5050

@@ -120,16 +120,6 @@ public boolean onMouseClicked(Click click, boolean doubled) {
120120
}
121121
}
122122
else if (click.button() == GLFW_MOUSE_BUTTON_LEFT) {
123-
if (doubled) {
124-
selecting = false;
125-
126-
cursor = text.length();
127-
selectionStart = 0;
128-
selectionEnd = cursor;
129-
130-
return true;
131-
}
132-
133123
selecting = true;
134124

135125
double overflowWidth = getOverflowWidthForRender();
@@ -149,6 +139,16 @@ else if (click.button() == GLFW_MOUSE_BUTTON_LEFT) {
149139
}
150140
}
151141

142+
if (doubled && cursor == preSelectionCursor) {
143+
doubleClick = true;
144+
resetSelection();
145+
146+
selectionStart = (cursor - countToNextSpace(true));
147+
selectionEnd = cursor = (cursor + countToNextSpace(false));
148+
149+
return true;
150+
}
151+
152152
preSelectionCursor = cursor;
153153
resetSelection();
154154
cursorChanged();
@@ -173,22 +173,53 @@ public void onMouseMoved(double mouseX, double mouseY, double lastMouseX, double
173173

174174
double smallestDifference = Double.MAX_VALUE;
175175

176+
int best = 0;
176177
for (int i = 0; i < textWidths.size(); i++) {
177178
double difference = Math.abs(textWidths.getDouble(i) + pad - relativeMouseX);
178179

179180
if (difference < smallestDifference) {
181+
best = i;
180182
smallestDifference = difference;
181-
if (i < preSelectionCursor) {
182-
selectionStart = i;
183-
cursor = i;
183+
if (!doubleClick) {
184+
if (i < preSelectionCursor) {
185+
selectionStart = i;
186+
cursor = i;
187+
} else if (i > preSelectionCursor) {
188+
selectionEnd = i;
189+
cursor = i;
190+
} else {
191+
cursor = preSelectionCursor;
192+
resetSelection();
193+
}
184194
}
185-
else if (i > preSelectionCursor) {
186-
selectionEnd = i;
187-
cursor = i;
195+
}
196+
}
197+
198+
// double click selection will select by whole words
199+
if (doubleClick) {
200+
if (best < selectionStart) {
201+
selectionStart = best - countToNextSpace(true, best);
202+
cursor = selectionStart;
203+
}
204+
else if (best > selectionEnd) {
205+
selectionEnd = best + countToNextSpace(false, best);
206+
cursor = selectionEnd;
207+
}
208+
else {
209+
if (cursor == selectionStart) {
210+
int nextRight = countToNextSpace(false);
211+
if (best > cursor + nextRight) {
212+
selectionStart = cursor = cursor + nextRight + 1;
213+
if (selectionStart <= preSelectionCursor && selectionStart + countToNextSpace(false) >= preSelectionCursor) {
214+
cursor = selectionEnd;
215+
}
216+
}
188217
}
189-
else {
190-
cursor = preSelectionCursor;
191-
resetSelection();
218+
else if (cursor == selectionEnd) {
219+
int nextLeft = countToNextSpace(true);
220+
if (best < cursor - nextLeft) {
221+
selectionEnd = cursor = cursor - nextLeft - 1;
222+
}
192223
}
193224
}
194225
}
@@ -197,6 +228,7 @@ else if (i > preSelectionCursor) {
197228
@Override
198229
public boolean onMouseReleased(Click click) {
199230
selecting = false;
231+
doubleClick = false;
200232

201233
if (selectionStart < preSelectionCursor && preSelectionCursor == selectionEnd) {
202234
cursor = selectionStart;
@@ -351,24 +383,32 @@ else if (input.key() == GLFW_KEY_DELETE) {
351383
}
352384
else if (input.key() == GLFW_KEY_LEFT) {
353385
if (cursor > 0) {
386+
// sets the cursor to just after the next leftmost space
354387
if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_CONTROL : GLFW_MOD_ALT)) {
355388
cursor -= countToNextSpace(true);
356389
resetSelection();
357390
}
391+
// sets the cursor to the beginning of the text box
358392
else if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_ALT : MacWindowUtil.IS_MAC ? GLFW_MOD_SUPER : GLFW_MOD_CONTROL)) {
359393
cursor = 0;
360394
resetSelection();
361395
}
396+
// sets the selection to just after the next leftmost space
362397
else if (altShift) {
363398
if (cursor == selectionEnd && cursor != selectionStart) {
364399
cursor -= countToNextSpace(true);
365-
selectionEnd = cursor;
400+
if (cursor >= selectionStart) selectionEnd = cursor;
401+
else {
402+
selectionEnd = selectionStart;
403+
selectionStart = cursor;
404+
}
366405
}
367406
else {
368407
cursor -= countToNextSpace(true);
369408
selectionStart = cursor;
370409
}
371410
}
411+
// sets the selection to the beginning of the text box
372412
else if (controlShift) {
373413
if (cursor == selectionEnd && cursor != selectionStart) {
374414
selectionEnd = selectionStart;
@@ -377,6 +417,7 @@ else if (controlShift) {
377417

378418
cursor = 0;
379419
}
420+
// moves the selection one character to the left
380421
else if (shift) {
381422
if (cursor == selectionEnd && cursor != selectionStart) {
382423
selectionEnd = cursor - 1;
@@ -387,6 +428,7 @@ else if (shift) {
387428

388429
cursor--;
389430
}
431+
// moves the cursor one character to the left
390432
else {
391433
if (cursor == selectionEnd && cursor != selectionStart) {
392434
cursor = selectionStart;
@@ -410,31 +452,40 @@ else if (selectionStart != selectionEnd && selectionStart == 0 && input.modifier
410452
}
411453
else if (input.key() == GLFW_KEY_RIGHT) {
412454
if (cursor < text.length()) {
455+
// sets the cursor to just before the next rightmost space
413456
if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_CONTROL : GLFW_MOD_ALT)) {
414457
cursor += countToNextSpace(false);
415458
resetSelection();
416459
}
460+
// sets the cursor to the end of the text box
417461
else if (input.modifiers() == (SystemUtils.IS_OS_WINDOWS ? GLFW_MOD_ALT : MacWindowUtil.IS_MAC ? GLFW_MOD_SUPER : GLFW_MOD_CONTROL)) {
418462
cursor = text.length();
419463
resetSelection();
420464
}
465+
// sets the selection to just before the next rightmost space
421466
else if (altShift) {
422467
if (cursor == selectionStart && cursor != selectionEnd) {
423468
cursor += countToNextSpace(false);
424-
selectionStart = cursor;
469+
if (cursor <= selectionEnd) selectionStart = cursor;
470+
else {
471+
selectionStart = selectionEnd;
472+
selectionEnd = cursor;
473+
}
425474
}
426475
else {
427476
cursor += countToNextSpace(false);
428477
selectionEnd = cursor;
429478
}
430479
}
480+
// sets the selection to the end of the text box
431481
else if (controlShift) {
432482
if (cursor == selectionStart && cursor != selectionEnd) {
433483
selectionStart = selectionEnd;
434484
}
435485
cursor = text.length();
436486
selectionEnd = cursor;
437487
}
488+
// moves the selection one character to the right
438489
else if (shift) {
439490
if (cursor == selectionStart && cursor != selectionEnd) {
440491
selectionStart = cursor + 1;
@@ -445,6 +496,7 @@ else if (shift) {
445496

446497
cursor++;
447498
}
499+
// moves the cursor one character to the right
448500
else {
449501
if (cursor == selectionStart && cursor != selectionEnd) {
450502
cursor = selectionEnd;
@@ -566,10 +618,14 @@ private void resetSelection() {
566618
}
567619

568620
private int countToNextSpace(boolean toLeft) {
621+
return countToNextSpace(toLeft, cursor);
622+
}
623+
624+
private int countToNextSpace(boolean toLeft, int startPos) {
569625
int count = 0;
570626
boolean hadNonSpace = false;
571627

572-
for (int i = cursor; toLeft ? i >= 0 : i < text.length(); i += toLeft ? -1 : 1) {
628+
for (int i = startPos; toLeft ? i >= 0 : i < text.length(); i += toLeft ? -1 : 1) {
573629
int j = i;
574630
if (toLeft) j--;
575631

0 commit comments

Comments
 (0)