Skip to content

Commit 326650d

Browse files
X-C-0Wide-Cat
authored andcommitted
Fix interaction with focused widgets (e.g. Textbox) when cursor is outside View
1 parent c16ea36 commit 326650d

7 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/main/java/meteordevelopment/meteorclient/gui/screens/settings/ColorSettingScreen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ public boolean onMouseClicked(Click click, boolean doubled) {
283283

284284
if (mouseOver) {
285285
dragging = true;
286+
setFocused(true);
286287

287288
handleX = lastMouseX - x;
288289
handleY = lastMouseY - y;
@@ -298,6 +299,7 @@ public boolean onMouseClicked(Click click, boolean doubled) {
298299
public boolean onMouseReleased(Click click) {
299300
if (dragging) {
300301
dragging = false;
302+
setFocused(false);
301303
}
302304

303305
return false;
@@ -555,6 +557,7 @@ public boolean onMouseClicked(Click click, boolean doubled) {
555557

556558
if (mouseOver) {
557559
dragging = true;
560+
setFocused(true);
558561

559562
handleX = lastMouseX - x;
560563
calculateHueAngleFromHandleX();
@@ -570,6 +573,7 @@ public boolean onMouseClicked(Click click, boolean doubled) {
570573
public boolean onMouseReleased(Click click) {
571574
if (dragging) {
572575
dragging = false;
576+
setFocused(false);
573577
}
574578

575579
return mouseOver;

src/main/java/meteordevelopment/meteorclient/gui/widgets/WWidget.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public abstract class WWidget implements BaseWidget {
2525
public String tooltip;
2626

2727
public boolean mouseOver;
28+
public boolean focused;
2829
protected boolean instantTooltips;
2930
protected double mouseOverTimer;
3031

@@ -143,11 +144,19 @@ protected WWidget getRoot() {
143144
return parent != null ? parent.getRoot() : (this instanceof WRoot ? this : null);
144145
}
145146

146-
protected WView getView() {
147+
public WView getView() {
147148
return this instanceof WView ? (WView) this : (parent != null ? parent.getView() : null);
148149
}
149150

150151
public boolean isOver(double x, double y) {
151152
return x >= this.x && x <= this.x + width && y >= this.y && y <= this.y + height;
152153
}
154+
155+
public boolean isFocused() {
156+
return focused;
157+
}
158+
159+
public void setFocused(boolean focused) {
160+
if (this.focused != focused) this.focused = focused;
161+
}
153162
}

src/main/java/meteordevelopment/meteorclient/gui/widgets/containers/WContainer.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public void moveCells(double deltaX, double deltaY) {
6262
}
6363
}
6464

65+
@Override
66+
public boolean isFocused() {
67+
for (Cell<?> cell : cells) if (cell.widget().isFocused()) return true;
68+
return false;
69+
}
70+
6571
// Layout
6672

6773
@Override
@@ -107,13 +113,15 @@ public boolean render(GuiRenderer renderer, double mouseX, double mouseY, double
107113
if (super.render(renderer, mouseX, mouseY, delta)) return true;
108114

109115
WView view = getView();
116+
double windowHeight = getWindowHeight();
110117

111118
for (Cell<?> cell : cells) {
112-
double y = cell.widget().y;
113-
if (y > getWindowHeight()) break;
114-
if (view != null && !view.isWidgetInView(cell.widget())) continue;
119+
WWidget widget = cell.widget();
120+
121+
if (widget.y > windowHeight) break;
122+
if (widget.y + widget.height <= 0) continue;
115123

116-
if (y + cell.widget().height > 0) renderWidget(cell.widget(), renderer, mouseX, mouseY, delta);
124+
if (shouldRenderWidget(widget, view)) renderWidget(widget, renderer, mouseX, mouseY, delta);
117125
}
118126

119127
return false;
@@ -123,6 +131,17 @@ protected void renderWidget(WWidget widget, GuiRenderer renderer, double mouseX,
123131
widget.render(renderer, mouseX, mouseY, delta);
124132
}
125133

134+
private boolean shouldRenderWidget(WWidget widget, WView view) {
135+
if (view == null) return true;
136+
if (!view.isWidgetInView(widget)) return false;
137+
138+
if (widget.mouseOver && !view.mouseOver) {
139+
widget.mouseOver = false;
140+
}
141+
142+
return true;
143+
}
144+
126145
// Events
127146

128147
protected boolean propagateEvents(WWidget widget) {

src/main/java/meteordevelopment/meteorclient/gui/widgets/containers/WView.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ else if (targetScroll < scroll) {
164164

165165
@Override
166166
protected boolean propagateEvents(WWidget widget) {
167-
return ((widget.y >= y && widget.y <= y + height) || (widget.y + widget.height >= y && widget.y + widget.height <= y + height)) || ((y >= widget.y && y <= widget.y + widget.height) || (y + height >= widget.y && y + height <= widget.y + widget.height));
168-
}
169-
170-
protected boolean isWidgetInView(WWidget widget) {
171-
return widget.y < y + height && widget.y + widget.height > y;
167+
return (mouseOver && isWidgetInView(widget)) || widget.isFocused();
172168
}
173169

174170
protected double handleWidth() {
@@ -186,4 +182,8 @@ protected double handleX() {
186182
protected double handleY() {
187183
return y + (height - handleHeight()) * (scroll / (actualHeight - height));
188184
}
185+
186+
public boolean isWidgetInView(WWidget widget) {
187+
return widget.y < y + height && widget.y + widget.height > y;
188+
}
189189
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import meteordevelopment.meteorclient.gui.utils.Cell;
1010
import meteordevelopment.meteorclient.gui.widgets.WRoot;
1111
import meteordevelopment.meteorclient.gui.widgets.containers.WVerticalList;
12+
import meteordevelopment.meteorclient.gui.widgets.containers.WView;
1213
import meteordevelopment.meteorclient.gui.widgets.pressable.WPressable;
1314
import net.minecraft.client.gui.Click;
1415
import net.minecraft.client.input.CharInput;
@@ -84,6 +85,8 @@ protected void onCalculateWidgetPositions() {
8485
@Override
8586
protected void onPressed(int button) {
8687
expanded = !expanded;
88+
root.setFocused(expanded);
89+
setFocused(expanded);
8790
}
8891

8992
public T get() {
@@ -108,7 +111,10 @@ public boolean render(GuiRenderer renderer, double mouseX, double mouseY, double
108111
animProgress += (expanded ? 1 : -1) * delta * 14;
109112
animProgress = MathHelper.clamp(animProgress, 0, 1);
110113

111-
if (!render && animProgress > 0) {
114+
WView view = getView();
115+
boolean rootInView = view == null || view.isWidgetInView(root);
116+
117+
if (!render && animProgress > 0 && rootInView) {
112118
renderer.absolutePost(() -> {
113119
renderer.scissorStart(x, y + height, width, root.height * animProgress);
114120
root.render(renderer, mouseX, mouseY, delta);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public boolean onMouseClicked(Click click, boolean doubled) {
5353
if (action != null) action.run();
5454

5555
dragging = true;
56+
setFocused(true);
5657
return true;
5758
}
5859

@@ -112,6 +113,7 @@ public boolean onMouseReleased(Click click) {
112113
}
113114

114115
dragging = false;
116+
setFocused(false);
115117
return true;
116118
}
117119

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public abstract class WTextBox extends WWidget {
3939

4040
protected final Renderer renderer;
4141

42-
protected boolean focused;
4342
protected DoubleList textWidths = new DoubleArrayList();
4443

4544
protected int cursor;
@@ -669,10 +668,7 @@ public void set(String text) {
669668
cursorChanged();
670669
}
671670

672-
public boolean isFocused() {
673-
return focused;
674-
}
675-
671+
@Override
676672
public void setFocused(boolean focused) {
677673
if (this.focused && !focused && actionOnUnfocused != null) actionOnUnfocused.run();
678674

0 commit comments

Comments
 (0)