Click to pin HoverState option

This commit is contained in:
bigguy345 committed 2025-12-26 17:28:28 +02:00
1 parent 9d5c577cf4
commit 741608a353
2 files changed
+86 -3

No files matched your search

@@ -86,6 +86,8 @@ public class GuiScriptTextArea extends GuiNpcTextField {
private final ScrollState scroll = new ScrollState();
private final SelectionState selection = new SelectionState();
private final HoverState hoverState = new HoverState();
/** When true, clicking a token will pin its hover tooltip until dismissed. */
public boolean clickToPinEnabled = false;
// ==================== UI COMPONENTS ====================
private int cursorCounter;
@@ -135,6 +137,8 @@ public class GuiScriptTextArea extends GuiNpcTextField {
KEYS_OVERLAY.openOnClick = true;
initGui();
initializeKeyBindings();
// Propagate click-to-pin option into hover state
hoverState.setClickToPinEnabled(clickToPinEnabled);
}
public void initGui() {
int endX = x + width, endY = y + height;
@@ -993,7 +997,8 @@ public class GuiScriptTextArea extends GuiNpcTextField {
int accumulatedWidth = 0;
for (int i = 0; i < lineText.length(); i++) {
int charWidth = ClientProxy.Font.width(String.valueOf(lineText.charAt(i)));
if (relativeX < accumulatedWidth + charWidth) {
// Check if mouse is in first half or second half of character
if (relativeX < accumulatedWidth + charWidth / 2) {
charPos = i;
break;
}
@@ -2195,6 +2200,29 @@ public class GuiScriptTextArea extends GuiNpcTextField {
this.lastClicked = time;
activeTextfield = this;
// Click-to-pin handling: if enabled, clicking a token will pin/unpin its tooltip
if (mouseButton == 0 && hoverState.isClickToPinEnabled()) {
Object[] tokenInfo = getTokenAtScreenPosition(xMouse, yMouse);
if (tokenInfo != null) {
Token clickedToken = (Token) tokenInfo[0];
int tokenScreenX = (Integer) tokenInfo[1];
int tokenScreenY = (Integer) tokenInfo[2];
int tokenWidth = (Integer) tokenInfo[3];
// If already pinned on same token, unpin; otherwise pin this token
if (hoverState.isPinned() && hoverState.getHoveredToken() == clickedToken) {
hoverState.unpin();
} else {
hoverState.pinToken(clickedToken, tokenScreenX, tokenScreenY, tokenWidth);
}
} else {
// Clicked outside any token -> unpin any pinned tooltip
if (hoverState.isPinned()) {
hoverState.unpin();
}
}
}
}
}
@@ -43,6 +43,13 @@ public class HoverState {
private int tokenScreenX;
private int tokenScreenY;
private int tokenWidth;
/** Pinned token (set when user clicks a token to keep tooltip open) */
private Token pinnedToken;
private TokenHoverInfo pinnedHoverInfo;
/** Whether click-to-pin behaviour is enabled for this hover state. */
private boolean clickToPinEnabled = true;
// ==================== UPDATE ====================
@@ -144,6 +151,13 @@ public class HoverState {
public void update(int mouseX, int mouseY, Token token, int tokenX, int tokenY, int tokenW) {
lastMouseX = mouseX;
lastMouseY = mouseY;
// If a token has been pinned by click, ignore mouse movement updates
if (pinnedToken != null) {
// keep pinned tooltip visible
tooltipVisible = true;
hoverInfo = pinnedHoverInfo;
return;
}
if (token == null) {
clearHover();
@@ -179,8 +193,11 @@ public class HoverState {
if (hoveredToken != null) {
hoveredToken = null;
hoverStartTime = 0;
tooltipVisible = false;
hoverInfo = null;
// Do not clear tooltipInfo here if pinned; if not pinned, hide tooltip.
if (pinnedToken == null) {
tooltipVisible = false;
hoverInfo = null;
}
}
}
@@ -191,6 +208,44 @@ public class HoverState {
tooltipVisible = false;
}
/**
* Enable or disable click-to-pin behaviour.
*/
public void setClickToPinEnabled(boolean enabled) {
this.clickToPinEnabled = enabled;
}
public boolean isClickToPinEnabled() {
return clickToPinEnabled;
}
/**
* Pin a token so its tooltip stays visible until explicitly unpinned.
*/
public void pinToken(Token token, int tokenX, int tokenY, int tokenW) {
if (token == null) return;
this.pinnedToken = token;
this.pinnedHoverInfo = TokenHoverInfo.fromToken(token);
this.tooltipVisible = pinnedHoverInfo != null && pinnedHoverInfo.hasContent();
this.tokenScreenX = tokenX;
this.tokenScreenY = tokenY;
this.tokenWidth = tokenW;
// ensure hoveredToken reflects pinned token
this.hoveredToken = token;
}
/**
* Unpin any pinned token and hide tooltip.
*/
public void unpin() {
this.pinnedToken = null;
this.pinnedHoverInfo = null;
this.tooltipVisible = false;
this.hoverInfo = null;
}
public boolean isPinned() { return pinnedToken != null; }
// ==================== POSITION CALCULATION ====================
/**