Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes world generation display name input entry rendering problem #489

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/minicraft/screen/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ public void render(Screen screen) {
ListEntry entry = entries.get(idx);

if(!(entry instanceof BlankEntry)) {
Point pos = entryPos.positionRect(new Dimension(entry.getWidth(), ListEntry.getHeight()), new Rectangle(entryBounds.getLeft(), y, entryBounds.getWidth(), ListEntry.getHeight(), Rectangle.CORNER_DIMS));
Point displacement = entry.getRenderingDisplacement();
Point pos = entryPos.positionRect(new Dimension(entry.getWidth(), ListEntry.getHeight()),
(displacement != null) ? new Rectangle(entryBounds.getLeft() + displacement.x, y + displacement.y, entryBounds.getWidth(), ListEntry.getHeight(), Rectangle.CORNER_DIMS) :
new Rectangle(entryBounds.getLeft(), y, entryBounds.getWidth(), ListEntry.getHeight(), Rectangle.CORNER_DIMS));
boolean selected = idx == selection;
if (searcherBarActive && useSearcherBar) {
entry.render(screen, pos.x, pos.y, selected, typingSearcher, Color.YELLOW);
Expand Down
39 changes: 32 additions & 7 deletions src/main/java/minicraft/screen/WorldGenDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import minicraft.core.io.Settings;
import minicraft.gfx.Color;
import minicraft.gfx.Font;
import minicraft.gfx.Point;
import minicraft.gfx.Screen;
import minicraft.screen.entry.InputEntry;
import minicraft.screen.entry.SelectEntry;
import minicraft.util.Logging;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
Expand All @@ -29,6 +32,7 @@ public class WorldGenDisplay extends Display {
if (FileHandler.OS.contains("windows")) {
// Reference: https://stackoverflow.com/a/6804755
worldNameRegex = "[^<>:\"/\\\\|?*\\x00-\\x1F]+";
//noinspection RegExpRepeatedSpace
detailedFilenamePattern = Pattern.compile(
"# Match a valid Windows filename (unspecified file system). \n" +
"^ # Anchor to start of string. \n" +
Expand Down Expand Up @@ -78,8 +82,8 @@ public static OptionalLong getSeed() {
return OptionalLong.of(seed);
}

public static InputEntry makeWorldNameInput(String prompt, List<String> takenNames, String initValue, boolean isGen) {
return new InputEntry(prompt, worldNameRegex, 36, initValue) {
public static InputEntry makeWorldNameInput(String prompt, List<String> takenNames, String initValue) {
return new InputEntry(prompt, worldNameRegex, 255, initValue) {
private String lastName;

@Override
Expand Down Expand Up @@ -127,19 +131,40 @@ public String getUserInput() {
return super.getUserInput().toLowerCase(Localization.getSelectedLocale());
}

private int maxXDisplacement = 0; // Relative maximum rendering displacement.
private int renderingX = 0; // Relative rendering displacement.

{
setChangeListener(i -> renderingX = maxXDisplacement = getUserInput().length() > 9 ? -(getUserInput().length()-9) * 8 : 0);
}
Makkkkus marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void render(Screen screen, int x, int y, boolean isSelected) {
super.render(screen, isGen?
(getUserInput().length() > 11? x - (getUserInput().length()-11) * 8: x):
x, y, isSelected);
public @NotNull Point getRenderingDisplacement() {
return new Point(renderingX, 0);
}

@Override
public void tick(InputHandler input) {
super.tick(input);
if (input.getKey("cursor-right").clicked) {
if (renderingX > maxXDisplacement) { // Enough space on the left.
renderingX -= 8;
if (renderingX < maxXDisplacement) renderingX = maxXDisplacement;
}
} else if (input.getKey("cursor-left").clicked) {
if (renderingX < 0) {
renderingX += 8;
if (renderingX > 0) renderingX = 0;
}
}
}
};
}

public WorldGenDisplay() {
super(true);

InputEntry nameField = makeWorldNameInput("minicraft.displays.world_gen.enter_world", WorldSelectDisplay.getWorldNames(), "", true);
InputEntry nameField = makeWorldNameInput("minicraft.displays.world_gen.enter_world", WorldSelectDisplay.getWorldNames(), "");

SelectEntry nameHelp = new SelectEntry("minicraft.displays.world_gen.troublesome_input", () -> Game.setDisplay(new PopupDisplay(null, "minicraft.displays.world_gen.troublesome_input.msg"))) {
@Override
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/minicraft/screen/WorldSelectDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class WorldSelectDisplay extends Display {

Expand Down Expand Up @@ -87,7 +84,7 @@ public void tick(InputHandler input) {
ArrayList<ListEntry> entries = new ArrayList<>();
ArrayList<String> names = WorldSelectDisplay.getWorldNames();
entries.add(new StringEntry("minicraft.displays.world_select.popups.display.change", Color.BLUE));
entries.add(WorldGenDisplay.makeWorldNameInput("", names, worldName, false));
entries.add(WorldGenDisplay.makeWorldNameInput("", names, worldName));
entries.addAll(Arrays.asList(StringEntry.useLines(Color.WHITE, "",
Localization.getLocalized("minicraft.displays.world_select.popups.display.confirm", Game.input.getMapping("select")),
Localization.getLocalized("minicraft.displays.world_select.popups.display.cancel", Game.input.getMapping("exit"))
Expand Down Expand Up @@ -135,7 +132,7 @@ public void tick(InputHandler input) {
ArrayList<String> names = WorldSelectDisplay.getWorldNames();
names.remove(worldName);
entries.add(new StringEntry("minicraft.displays.world_select.popups.display.change", Color.GREEN));
entries.add(WorldGenDisplay.makeWorldNameInput("", names, worldName, false));
entries.add(WorldGenDisplay.makeWorldNameInput("", names, worldName));
entries.addAll(Arrays.asList(StringEntry.useLines(Color.WHITE, "",
Localization.getLocalized("minicraft.displays.world_select.popups.display.confirm", Game.input.getMapping("select")),
Localization.getLocalized("minicraft.displays.world_select.popups.display.cancel", Game.input.getMapping("exit"))
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/minicraft/screen/entry/ListEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import minicraft.core.io.InputHandler;
import minicraft.gfx.Color;
import minicraft.gfx.Font;
import minicraft.gfx.Point;
import minicraft.gfx.Screen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;

Expand Down Expand Up @@ -77,6 +80,14 @@ public static int getHeight() {
return Font.textHeight();
}

/**
* Calculates the rendering displacement of the entry in the menu.
* @return a 2D point denoting the x and y displacement respectively.
*/
public @Nullable Point getRenderingDisplacement() {
return null;
}

/**
* Determines if this entry can be selected.
* @return true if it is visible and can be selected, false otherwise.
Expand Down