Rework title screen button, hopefully should be repositionable by FancyMenu now

This commit is contained in:
Moulberry committed 2025-03-13 21:42:36 +08:00
1 parent 5d26b2611f
commit fb414e4af7
1 file changed
+51 -9
@@ -37,12 +37,13 @@ public class MixinTitleScreen extends Screen {
@Unique @Unique
private AbstractWidget openSelectReplayScreenButton = null; private AbstractWidget openSelectReplayScreenButton = null;
@Unique
private boolean checkedPositionOfReplayButton = false;
@Inject(method = "init", at = @At("RETURN")) @Inject(method = "init", at = @At("RETURN"))
public void init(CallbackInfo ci) { public void init(CallbackInfo ci) {
if (this.openSelectReplayScreenButton != null) { this.createOrPositionOpenSelectReplayScreenButton(true);
this.removeWidget(this.openSelectReplayScreenButton); this.checkedPositionOfReplayButton = false;
this.openSelectReplayScreenButton = null;
}
} }
// We do this on tick to ensure that our button is added after ever other mod's // We do this on tick to ensure that our button is added after ever other mod's
@@ -50,18 +51,52 @@ public class MixinTitleScreen extends Screen {
// might not implement repositioning logic, causing our button to render under theirs // might not implement repositioning logic, causing our button to render under theirs
@Inject(method = "tick", at = @At("RETURN")) @Inject(method = "tick", at = @At("RETURN"))
public void tick(CallbackInfo ci) { public void tick(CallbackInfo ci) {
if (this.openSelectReplayScreenButton == null) { if (!this.checkedPositionOfReplayButton) {
this.createOpenSelectReplayScreenButton(); this.checkedPositionOfReplayButton = true;
this.createOrPositionOpenSelectReplayScreenButton(false);
} }
} }
@Unique @Unique
private void createOpenSelectReplayScreenButton() { private void createOrPositionOpenSelectReplayScreenButton(boolean allowCreating) {
if (this.openSelectReplayScreenButton != null) { // Set button to null if not in renderables list
this.removeWidget(this.openSelectReplayScreenButton); if (this.openSelectReplayScreenButton != null && !this.renderables.contains(this.openSelectReplayScreenButton)) {
this.openSelectReplayScreenButton = null; this.openSelectReplayScreenButton = null;
} }
// Exit early if the button doesn't exist and we aren't allowed to create it
if (!allowCreating && this.openSelectReplayScreenButton == null) {
return;
}
// If the button already exists, check if it overlaps with anything
// If there's no overlap, no need to move it
if (this.openSelectReplayScreenButton != null) {
int x = this.openSelectReplayScreenButton.getX();
int y = this.openSelectReplayScreenButton.getY();
int width = this.openSelectReplayScreenButton.getWidth();
int height = this.openSelectReplayScreenButton.getHeight();
boolean overlapsWithExistingButton = false;
for (Renderable renderable : this.renderables) {
if (renderable == this.openSelectReplayScreenButton) {
continue;
}
if (renderable instanceof AbstractWidget otherWidget) {
if (x < otherWidget.getRight() && x+width > otherWidget.getX() &&
y < otherWidget.getBottom() && y+height > otherWidget.getY()) {
overlapsWithExistingButton = true;
break;
}
}
}
if (!overlapsWithExistingButton) {
return;
}
}
for (int offsetX = 0; offsetX < 5; offsetX++) { for (int offsetX = 0; offsetX < 5; offsetX++) {
for (int i = this.normalMenuWidgets.size()-1; i >= 0; i--) { for (int i = this.normalMenuWidgets.size()-1; i >= 0; i--) {
AbstractWidget widget = this.normalMenuWidgets.get(i); AbstractWidget widget = this.normalMenuWidgets.get(i);
@@ -81,6 +116,9 @@ public class MixinTitleScreen extends Screen {
// Check for overlaps with any existing buttons // Check for overlaps with any existing buttons
for (Renderable renderable : this.renderables) { for (Renderable renderable : this.renderables) {
if (renderable == this.openSelectReplayScreenButton) {
continue;
}
if (renderable instanceof AbstractWidget otherWidget) { if (renderable instanceof AbstractWidget otherWidget) {
if (x < otherWidget.getRight() && x+size > otherWidget.getX() && if (x < otherWidget.getRight() && x+size > otherWidget.getX() &&
y < otherWidget.getBottom() && y+size > otherWidget.getY()) { y < otherWidget.getBottom() && y+size > otherWidget.getY()) {
@@ -91,6 +129,7 @@ public class MixinTitleScreen extends Screen {
} }
if (!overlapsWithExistingButton) { if (!overlapsWithExistingButton) {
if (this.openSelectReplayScreenButton == null) {
this.openSelectReplayScreenButton = new FlashbackButton(x, y, size, size, Component.literal("Open Replays"), button -> { this.openSelectReplayScreenButton = new FlashbackButton(x, y, size, size, Component.literal("Open Replays"), button -> {
List<String> incompatibleMods = Screen.hasShiftDown() ? List.of() : Flashback.getReplayIncompatibleMods(); List<String> incompatibleMods = Screen.hasShiftDown() ? List.of() : Flashback.getReplayIncompatibleMods();
@@ -109,6 +148,9 @@ public class MixinTitleScreen extends Screen {
}); });
this.addRenderableWidget(this.openSelectReplayScreenButton); this.addRenderableWidget(this.openSelectReplayScreenButton);
} else {
this.openSelectReplayScreenButton.setPosition(x, y);
}
return; return;
} }
} }