Skip to content

Commit

Permalink
changed lb gui input fields to number only and added bounding box upd…
Browse files Browse the repository at this point in the history
…ating
  • Loading branch information
kurrycat committed Oct 17, 2022
1 parent db22e6f commit de0aa28
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
public class InputField extends Component implements KeyInputListener, MouseInputListener {
public boolean numbersOnly;
public String content;
public ContentProvider onContentChange = null;
public String name = null;

public Color normalColor = new Color(31, 31, 31, 150);
public Color cursorColor = new Color(255, 255, 255, 150);
public Color highlightColor = new Color(255, 255, 255, 175);

private boolean isFocused = false;
private int cursorPos = 0;
private int highlightStart = 0;
Expand All @@ -43,6 +42,11 @@ public InputField setName(String name) {
return this;
}

public InputField setOnContentChange(ContentProvider onContentChange) {
this.onContentChange = onContentChange;
return this;
}

@Override
public void render(Vector2D mouse) {
Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name);
Expand Down Expand Up @@ -99,8 +103,11 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
String testKey = key;
if (testKey.startsWith("NUMPAD")) testKey = testKey.substring(6);

if (testKey.length() == 1) character = testKey.toLowerCase();
if (testKey.length() == 1)
if (!numbersOnly || "1234567890".contains(testKey))
character = testKey.toLowerCase();
if (Arrays.asList("COMMA", "PERIOD", "DECIMAL").contains(testKey)) character = ".";
if (testKey.equals("MINUS")) character = "-";

switch (key) {
/*case "LSHIFT":
Expand Down Expand Up @@ -150,13 +157,19 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) {

private void deleteSelection() {
if (highlightStart == highlightEnd)
content = content.substring(0, Math.max(cursorPos - 1, 0)) + (cursorPos >= content.length() ? "" : content.substring(cursorPos));
updateContent(content.substring(0, Math.max(cursorPos - 1, 0)) + (cursorPos >= content.length() ? "" : content.substring(cursorPos)));
else
content = content.substring(0, highlightStart) + content.substring(highlightEnd);
updateContent(content.substring(0, highlightStart) + content.substring(highlightEnd));
}

private void replaceSelectionWithChar(String c) {
content = content.substring(0, highlightStart) + c + content.substring(highlightEnd);
updateContent(content.substring(0, highlightStart) + c + content.substring(highlightEnd));
}

private void updateContent(String content) {
this.content = content;
if (onContentChange != null)
onContentChange.apply(new Content(content));
}

private double getCursorX() {
Expand Down Expand Up @@ -223,4 +236,29 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt
}
return false;
}

@FunctionalInterface
public interface ContentProvider {
void apply(Content content);
}

public static class Content {
public String content;

public Content(String content) {
this.content = content;
}

public String getContent() {
return content;
}

public Double getNumber() {
try {
return Double.parseDouble(content);
} catch (NumberFormatException e) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void onGuiInit() {

Vector2D windowSize = Renderer2D.getScaledSize();
lbList = new LBList(
new Vector2D(windowSize.getX() / 4D, 20),
new Vector2D(windowSize.getX() / 2D, windowSize.getY() - 40)
new Vector2D(windowSize.getX() / 5D, 20).round(),
new Vector2D(windowSize.getX() / 5D * 3D, windowSize.getY() - 40).round()
);

components.add(
Expand Down Expand Up @@ -133,12 +133,24 @@ public LBListItem(ScrollableList<LBListItem> parent, LandingBlock landingBlock)
landingBlock.shouldRender = checked;
});
shouldRender.setChecked(landingBlock.shouldRender);
minX = new InputField("" + landingBlock.boundingBox.getMin().getX(), Vector2D.OFFSCREEN, 25).setName("minX: ");
minY = new InputField("" + landingBlock.boundingBox.getMin().getY(), Vector2D.OFFSCREEN, 25).setName("minY: ");
minZ = new InputField("" + landingBlock.boundingBox.getMin().getZ(), Vector2D.OFFSCREEN, 25).setName("minZ: ");
maxX = new InputField("" + landingBlock.boundingBox.getMax().getX(), Vector2D.OFFSCREEN, 25).setName("maxX: ");
maxY = new InputField("" + landingBlock.boundingBox.getMax().getY(), Vector2D.OFFSCREEN, 25).setName("maxY: ");
maxZ = new InputField("" + landingBlock.boundingBox.getMax().getZ(), Vector2D.OFFSCREEN, 25).setName("maxZ: ");
minX = new InputField("" + landingBlock.boundingBox.getMin().getX(), Vector2D.OFFSCREEN, 25, true)
.setName("minX: ")
.setOnContentChange(c -> landingBlock.boundingBox.setMinX(c.getNumber()));
minY = new InputField("" + landingBlock.boundingBox.getMin().getY(), Vector2D.OFFSCREEN, 25, true)
.setName("minY: ")
.setOnContentChange(c -> landingBlock.boundingBox.setMinY(c.getNumber()));;
minZ = new InputField("" + landingBlock.boundingBox.getMin().getZ(), Vector2D.OFFSCREEN, 25, true)
.setName("minZ: ")
.setOnContentChange(c -> landingBlock.boundingBox.setMinZ(c.getNumber()));;
maxX = new InputField("" + landingBlock.boundingBox.getMax().getX(), Vector2D.OFFSCREEN, 25, true)
.setName("maxX: ")
.setOnContentChange(c -> landingBlock.boundingBox.setMaxX(c.getNumber()));;
maxY = new InputField("" + landingBlock.boundingBox.getMax().getY(), Vector2D.OFFSCREEN, 25, true)
.setName("maxY: ")
.setOnContentChange(c -> landingBlock.boundingBox.setMaxY(c.getNumber()));;
maxZ = new InputField("" + landingBlock.boundingBox.getMax().getZ(), Vector2D.OFFSCREEN, 25, true)
.setName("maxZ: ")
.setOnContentChange(c -> landingBlock.boundingBox.setMaxZ(c.getNumber()));;

fields = new InputField[]{minX, minY, minZ, maxX, maxY, maxZ};
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/github/kurrycat/mpkmod/util/BoundingBox3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ public double midZ() {
return (minZ() + maxZ()) / 2D;
}

public BoundingBox3D setMinX(double minX) {
this.min.setX(minX);
return this;
}

public BoundingBox3D setMinY(double minY) {
this.min.setY(minY);
return this;
}

public BoundingBox3D setMinZ(double minZ) {
this.min.setY(minZ);
return this;
}

public BoundingBox3D setMaxX(double maxX) {
this.max.setX(maxX);
return this;
}

public BoundingBox3D setMaxY(double maxY) {
this.max.setY(maxY);
return this;
}

public BoundingBox3D setMaxZ(double maxZ) {
this.max.setZ(maxZ);
return this;
}

public boolean intersectsOrTouchesXZ(BoundingBox3D other) {
return other.maxX() >= this.minX() &&
other.minX() <= this.maxX() &&
Expand Down

0 comments on commit de0aa28

Please sign in to comment.