mouseClicked refactor + documentation

This commit is contained in:
bigguy345 committed 2025-12-18 23:16:08 +02:00
1 parent 7ccd4a57b7
commit 21a7922a95
3 files changed
+59 -19

No files matched your search

@@ -1162,22 +1162,29 @@ public class GuiScriptTextArea extends GuiNpcTextField {
// ==================== MOUSE HANDLING ====================
public void mouseClicked(int xMouse, int yMouse, int mouseButton) {
// Determine whether click occurred inside the text area bounds
this.active = xMouse >= this.x && xMouse < this.x + this.width && yMouse >= this.y && yMouse < this.y + this.height;
if (this.active) {
// Compute logical click position in text and reset selection/caret
int clickPos = this.getSelectionPos(xMouse, yMouse);
selection.reset(clickPos);
selection.markActivity();
// Prepare click state (left button starts most interactions)
this.clicked = mouseButton == 0;
this.doubleClicked = false;
this.tripleClicked = false;
long time = System.currentTimeMillis();
// Prefer delegating scrollbar-start logic to ScrollState. If the click
// is on the scrollbar area and the scrollbar can be dragged, we start
// click-scrolling mode and cancel the normal text click-drag behavior.
if (this.clicked && this.container.linesCount * this.container.lineHeight > this.height && xMouse > this.x + this.width - 8) {
// We consumed the mouse-down as a scrollbar drag start
this.clicked = false;
scroll.setClickScrolling(true);
double linesCountD = Math.max(1, (double) this.container.linesCount);
int thumbTop = (int) (this.y + 1f * scroll.getScrollPos() / linesCountD * (this.height - 4)) + 1;
scroll.setScrollbarDragOffset(yMouse - thumbTop);
scroll.startScrollbarDrag(yMouse,this.y,this.height,container.linesCount);
} else {
// Handle double/triple click selection counting
if (time - this.lastClicked < 300L) {
this.clickCount++;
} else {
@@ -1185,24 +1192,13 @@ public class GuiScriptTextArea extends GuiNpcTextField {
}
if (this.clickCount == 2) {
// Double-click: select word under cursor (restore prior behavior)
// Double-click: select the word under the caret using the container's word regex
this.doubleClicked = true;
Matcher m = this.container.regexWord.matcher(this.text);
while (m.find()) {
if (selection.getCursorPosition() >= m.start() && selection.getCursorPosition() <= m.end()) {
selection.setSelection(m.start(), m.end());
break;
}
}
selection.selectWordAtCursor(this.text, this.container.regexWord);
} else if (this.clickCount >= 3) {
// Triple-click: select entire line
// Triple-click: select the entire logical line that contains the caret
this.tripleClicked = true;
for (LineData line : container.lines) {
if (selection.getCursorPosition() >= line.start && selection.getCursorPosition() <= line.end) {
selection.setSelection(line.start, line.end);
break;
}
}
selection.selectLineAtCursor(container.lines);
this.clickCount = 0;
}
}
@@ -193,4 +193,17 @@ public class ScrollState {
setScrollbarDragOffset(0);
}
}
/**
* Attempt to start a scrollbar drag if the mouse down occurred on the scrollbar thumb area.
* Returns true if drag was initiated (caller should cancel normal text-click drag behavior).
*/
public void startScrollbarDrag(int yMouse, int areaY, int areaHeight, int linesCount) {
// Thumb width in GuiScriptTextArea is 6px (posX..posX+5), so check clicks near the right edge
// Start click-scrolling mode and set initial drag offset relative to thumb top.
setClickScrolling(true);
double linesCountD = Math.max(1, (double) linesCount);
int thumbTop = (int) (areaY + 1f * getScrollPos() / linesCountD * (areaHeight - 4)) + 1;
setScrollbarDragOffset(yMouse - thumbTop);
}
}
@@ -2,6 +2,7 @@ package noppes.npcs.client.gui.util.script;
import noppes.npcs.client.gui.util.script.JavaTextContainer.LineData;
import java.util.List;
import java.util.regex.Pattern;
/**
* Manages cursor position, text selection, and related operations.
@@ -189,4 +190,34 @@ public class SelectionState {
endSelection = startSelection = cursorPosition = newPosition;
markActivity();
}
/**
* Select the word surrounding the current cursor position using the provided regex.
* The regex should match word tokens; the implementation finds the token which
* contains the cursor and selects its start..end.
*/
public void selectWordAtCursor(String text, Pattern wordPattern) {
if (text == null || wordPattern == null) return;
java.util.regex.Matcher m = wordPattern.matcher(text);
while (m.find()) {
if (cursorPosition >= m.start() && cursorPosition <= m.end()) {
setSelection(m.start(), m.end());
return;
}
}
}
/**
* Select the entire line containing the cursor. `lines` is the parsed
* line data from `JavaTextContainer` where each LineData has start/end indices.
*/
public void selectLineAtCursor(List<LineData> lines) {
if (lines == null) return;
for (LineData line : lines) {
if (isCursorOnLine(lines.indexOf(line), line, lines.size())) {
setSelection(line.start, line.end);
return;
}
}
}
}