Skip to content

Commit 576bf39

Browse files
committed
Implement scrolling
poorly documented, but who reads the documentation anyways?
1 parent 792c774 commit 576bf39

7 files changed

Lines changed: 97 additions & 27 deletions

File tree

src/main/java/de/geolykt/starloader/api/NullUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public final class NullUtils {
2222
* @param object The object to wrap
2323
* @return The newly created optional
2424
*/
25-
public static @NotNull <T> Optional<@NotNull T> asOptional(@Nullable T object) {
25+
public static @NotNull <T> Optional<T> asOptional(@Nullable T object) {
2626
if (object == null) {
2727
return emptyOptional();
2828
}

src/main/java/de/geolykt/starloader/api/gui/Drawing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static float drawText(@NotNull String message, float x, float y, @NotNull
161161
* @param y The Y-position to draw on; it is not known which corner it corresponds to. Caution is advised
162162
* @param width The width of the rectangle.
163163
* @param height The height of the rectangle.
164-
* @param camera The camera to use. It transforms x/y-positions as well as width and height of the drawn rectangle.
164+
* @param camera The camera to use. It transforms x/y-positions of the drawn rectangle. <b>Width and height are unaffected</b>
165165
* @param fillColor The GDX color to fill it with.
166166
* @since 1.5.0
167167
*/

src/main/java/de/geolykt/starloader/api/gui/DrawingImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public default float drawText(@NotNull String message, float x, float y, @NotNul
123123
* @param y The Y-position to draw on; it is not known which corner it corresponds to. Caution is advised
124124
* @param width The width of the rectangle.
125125
* @param height The height of the rectangle.
126-
* @param camera The camera to use. It transforms x/y-positions as well as width and height of the drawn rectangle.
126+
* @param camera The camera to use. It transforms x/y-positions of the drawn rectangle. <b>Width and height are unaffected</b>
127127
* @param fillColor The GDX color to fill it with.
128128
* @since 1.5.0
129129
*/

src/main/java/de/geolykt/starloader/api/gui/screen/ReactiveComponent.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,56 @@
1010
*/
1111
public interface ReactiveComponent extends ScreenComponent {
1212

13+
// Formerly, the screenY was inverted to the rendering y position. For some arbitrary reason this is no longer the case.
14+
15+
/**
16+
* Called when the user clicks on the component.
17+
* The documentation of screenX, screenY, componentX and componentY is done as intended. The future me should not come to the idea
18+
* that the documentation was wrong. However the variable names might be misleading...
19+
*
20+
* @param screenX The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getWidth()} and {@code 0}
21+
* @param screenY The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getHeight()} and {@code 0}
22+
* @param componentX The X-position of the component relative to the screen. Use for drawing operations only.
23+
* @param componentY The Y-position of the component relative to the screen. Use for drawing operations only.
24+
* @param camera The camera supplied for drawing operations.
25+
*/
26+
public void onClick(int screenX, int screenY, int componentX, int componentY, @NotNull Camera camera);
27+
1328
/**
1429
* Called when the mouse moves over the component.
1530
*
16-
* @param screenX The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getWidth()} and {@code 0}
17-
* @param screenY The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getHeight()} and {@code 0}
31+
* @param screenX The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getWidth()} and {@code 0}
32+
* @param screenY The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getHeight()} and {@code 0}
1833
* @param componentX The X-position of the component relative to the screen. Use for drawing operations only.
1934
* @param componentY The Y-position of the component relative to the screen. Use for drawing operations only.
20-
* @param camera The camera supplied for drawing operations.
35+
* @param camera The camera supplied for drawing operations.
2136
* @deprecated Not yet implemented
2237
*/
2338
@Deprecated(forRemoval = false, since = "1.5.0")
2439
public void onHover(int screenX, int screenY, int componentX, int componentY, @NotNull Camera camera);
2540

2641
/**
27-
* Called when the user clicks on the component.
28-
* The documentation of screenX, screenY, componentX and componentY is done as intended. The future me should not come to the idea
29-
* that the documentation was wrong. However the variable names might be misleading ...
30-
*
31-
* <p>screenY will be inverted compared to the rendering y's. You might want to do component.getHeight() - screenY to obtain a more usable height.
42+
* Unknown use but could be helpful to have. Mirrors the behaviour of {@link GestureListener#longPress(float, float)}.
3243
*
33-
* @param screenX The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getWidth()} and {@code 0}
34-
* @param screenY The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getHeight()} and {@code 0}
44+
* @param screenX The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getWidth()} and {@code 0}
45+
* @param screenY The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getHeight()} and {@code 0}
3546
* @param componentX The X-position of the component relative to the screen. Use for drawing operations only.
3647
* @param componentY The Y-position of the component relative to the screen. Use for drawing operations only.
37-
* @param camera The camera supplied for drawing operations.
48+
* @param camera The camera supplied for drawing operations.
3849
*/
39-
public void onClick(int screenX, int screenY, int componentX, int componentY, @NotNull Camera camera);
50+
public void onLongClick(int screenX, int screenY, int componentX, int componentY, @NotNull Camera camera);
4051

4152
/**
42-
* Unknown use but could be helpful to have. Mirrors the behaviour of {@link GestureListener#longPress(float, float)}.
53+
* Called when the user scrolls while being on this component.
54+
* The documentation of screenX, screenY, componentX and componentY is done as intended. The future me should not come to the idea
55+
* that the documentation was wrong. However the variable names might be misleading...
4356
*
44-
* @param screenX The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getWidth()} and {@code 0}
45-
* @param screenY The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getHeight()} and {@code 0}
57+
* @param screenX The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getWidth()} and {@code 0}
58+
* @param screenY The Y-position of the mouse relative to this component. Bound between {@link ScreenComponent#getHeight()} and {@code 0}
4659
* @param componentX The X-position of the component relative to the screen. Use for drawing operations only.
4760
* @param componentY The Y-position of the component relative to the screen. Use for drawing operations only.
48-
* @param camera The camera supplied for drawing operations.
61+
* @param camera The camera supplied for drawing operations.
62+
* @param amount The amount that was scrolled. Either {@code -1} or {@code 1}.
4963
*/
50-
public void onLongClick(int screenX, int screenY, int componentX, int componentY, @NotNull Camera camera);
64+
public void onScroll(int screenX, int screenY, int componentX, int componentY, @NotNull Camera camera, int amount);
5165
}

src/main/java/de/geolykt/starloader/impl/gui/SLAbstractWidget.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.jetbrains.annotations.NotNull;
77
import org.jetbrains.annotations.Nullable;
88

9+
import com.badlogic.gdx.InputProcessor;
910
import com.badlogic.gdx.graphics.Camera;
1011
import com.badlogic.gdx.input.GestureDetector.GestureListener;
1112

@@ -41,6 +42,12 @@ public void a(double x, double y) {
4142
tap(x, getHeight() - y, false);
4243
}
4344

45+
@Override
46+
public void a(float unknown, float amount, float x, float y) {
47+
super.a(unknown, amount, x, y);
48+
scroll((int) x, (int) y, (int) amount / -40);
49+
}
50+
4451
@Override
4552
public void b(double x, double y) {
4653
super.b(x, y);
@@ -152,6 +159,21 @@ protected final void renderChildren() {
152159
this.z();
153160
}
154161

162+
/**
163+
* Listener method for when the user chooses to scroll while having the mouse on this widget.
164+
* Corresponds to {@link InputProcessor#scrolled(int)}.
165+
*
166+
* @param x The X-position of the mouse at this time
167+
* @param y The Y-position of the mouse at this time
168+
* @param amount The amount that should be scrolled. Either {@code -1} or {@code 1}.
169+
* @return True when the game should not be zoomed. False when the zoom of the main board should not be blocked.
170+
* @since 1.5.0
171+
*/
172+
protected boolean scroll(int x, int y, int amount) {
173+
// Stub
174+
return true;
175+
}
176+
155177
/**
156178
* Sets the title that should be used for the {@link #drawHeader()} operation.
157179
* The title itself will be rendered in white color.

src/main/java/de/geolykt/starloader/impl/gui/SLScreenWidget.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.badlogic.gdx.math.Vector2;
1616

1717
import de.geolykt.starloader.api.NullUtils;
18+
import de.geolykt.starloader.api.gui.Drawing;
1819
import de.geolykt.starloader.api.gui.screen.ComponentSupplier;
1920
import de.geolykt.starloader.api.gui.screen.LineWrappingInfo;
2021
import de.geolykt.starloader.api.gui.screen.ReactiveComponent;
@@ -264,9 +265,43 @@ protected void renderSLChildComponents() {
264265
Map.Entry<Vector2, ScreenComponent> componentEntry = populator.next();
265266
Vector2 pos = componentEntry.getKey();
266267
ScreenComponent component = componentEntry.getValue();
267-
int width = component.renderAt((int) pos.x, (int) pos.y, c); // TODO originally the render operation had offsets, but not anymore. Explore why this may have been dumb to remove. (#getInnerWidth does not make any sense anymore dummy.)
268-
componentPositioningMeta.add(new ScreenComponentPositioningMeta(pos, width, component.getHeight(), component));
268+
try {
269+
int width = component.renderAt((int) pos.x, (int) pos.y, c); // TODO originally the render operation had offsets, but not anymore. Explore why this may have been dumb to remove. (#getInnerWidth does not make any sense anymore dummy.)
270+
componentPositioningMeta.add(new ScreenComponentPositioningMeta(pos, width, component.getHeight(), component));
271+
} catch (Exception e) {
272+
// Throwing an exception here would cause serious UI issues
273+
e.printStackTrace();
274+
Drawing.toast("Unable to draw a screen component. Review the log for details!");
275+
}
276+
}
277+
}
278+
279+
@Override
280+
protected boolean scroll(int x, int y, int amount) {
281+
Camera c = NullUtils.requireNotNull(getCamera());
282+
double actualY = lastRenderHeight - y - 25.0D;
283+
284+
for (ScreenComponentPositioningMeta posMeta : this.componentPositioningMeta) {
285+
if (!(posMeta.component instanceof ReactiveComponent)) {
286+
continue;
287+
}
288+
ReactiveComponent component = (ReactiveComponent) posMeta.component;
289+
Vector2 pos = posMeta.pos;
290+
System.out.println("x >= pos.x && x <= pos.x + posMeta.width = " + (x >= pos.x && x <= pos.x + posMeta.width));
291+
System.out.println("pos.y <= actualY = " + (pos.y <= actualY));
292+
System.out.println("(pos.y + posMeta.height) >= actualY = " + ((pos.y + posMeta.height) >= actualY));
293+
if ((x >= pos.x && x <= pos.x + posMeta.width)
294+
&& (pos.y <= actualY && (pos.y + posMeta.height) >= actualY)) {
295+
component.onScroll((int) (x - pos.x), (int) (posMeta.height - (actualY - pos.y)), (int) pos.x, (int) pos.y, c, amount);
296+
}
269297
}
298+
299+
return true;
300+
}
301+
302+
@Override
303+
public void setCamera(@NotNull Camera camera) {
304+
throw new UnsupportedOperationException("This method call is not permitted.");
270305
}
271306

272307
@Override
@@ -289,9 +324,4 @@ protected void tap(double x, double y, boolean isLongTap) {
289324
}
290325
}
291326
}
292-
293-
@Override
294-
public void setCamera(@NotNull Camera camera) {
295-
throw new UnsupportedOperationException("This method call is not permitted.");
296-
}
297327
}

src/main/java/de/geolykt/starloader/impl/gui/SimpleScreenChildList.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
/**
1414
* A collection class that sets the parent screen of all the components. Note: in order for this to work the component
1515
* must implement {@link SLScreenComponent}.
16+
*
17+
* @deprecated The only functionality of this class depends on {@link SLScreenComponent}, which has been deprecated
18+
* for removal
1619
*/
20+
@Deprecated(forRemoval = true, since = "1.5.0")
1721
public class SimpleScreenChildList extends LinkedList<@NotNull ScreenComponent> {
1822

1923
/**

0 commit comments

Comments
 (0)