Skip to content

Commit

Permalink
added input fields to lb gui
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrycat committed Oct 17, 2022
1 parent 4ba5737 commit db22e6f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
public class InputField extends Component implements KeyInputListener, MouseInputListener {
public boolean numbersOnly;
public String content;
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, 150);
public Color highlightColor = new Color(255, 255, 255, 175);

private boolean isFocused = false;
private int cursorPos = 0;
Expand All @@ -37,36 +38,54 @@ public InputField(String content, Vector2D pos, double width, boolean numbersOnl
this.numbersOnly = numbersOnly;
}

public InputField setName(String name) {
this.name = name;
return this;
}

@Override
public void render(Vector2D mouse) {
Renderer2D.drawRectWithEdge(getDisplayPos(), getSize(), 1, normalColor, normalColor);
Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name);
Vector2D rectPos = getDisplayPos().add(nameSize.getX(), 0);
Vector2D rectSize = getSize().sub(nameSize.getX(), 0);

if (name != null) {
FontRenderer.drawCenteredString(
name,
getDisplayPos().add(nameSize.getX() / 2D, getSize().getY() / 2D + 1),
Color.WHITE,
false
);
}

Renderer2D.drawRectWithEdge(rectPos.round(), rectSize.round(), 1, normalColor, normalColor);

FontRenderer.drawString(
content.substring(0, highlightStart),
getDisplayPos().add(2, 2),
rectPos.add(2, 2),
Color.WHITE, false
);
if (highlightStart != highlightEnd)
Renderer2D.drawRect(
getDisplayPos().add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2),
new Vector2D(FontRenderer.getStringSize(content.substring(highlightStart, highlightEnd)).getX(), getSize().getY() - 4),
rectPos.add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2),
new Vector2D(FontRenderer.getStringSize(content.substring(highlightStart, highlightEnd)).getX(), rectSize.getY() - 4),
highlightColor
);
FontRenderer.drawString(
content.substring(highlightStart, highlightEnd),
getDisplayPos().add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2),
rectPos.add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2),
Color.BLACK, false
);
FontRenderer.drawString(
content.substring(highlightEnd),
getDisplayPos().add(2 + FontRenderer.getStringSize(content.substring(0, highlightEnd)).getX(), 2),
rectPos.add(2 + FontRenderer.getStringSize(content.substring(0, highlightEnd)).getX(), 2),
Color.WHITE, false
);

if (isFocused && highlightStart == highlightEnd)
Renderer2D.drawRect(
new Vector2D(getDisplayPos().getX() + getCursorX(), getDisplayPos().getY() + 1),
new Vector2D(1, getSize().getY() - 2),
new Vector2D(rectPos.getX() + getCursorX(), rectPos.getY() + 1),
new Vector2D(1, rectSize.getY() - 2),
cursorColor
);
}
Expand All @@ -78,10 +97,10 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
if (pressed) {
String character = null;
String testKey = key;
if(testKey.startsWith("NUMPAD")) testKey = testKey.substring(6);
if (testKey.startsWith("NUMPAD")) testKey = testKey.substring(6);

if(testKey.length() == 1) character = testKey.toLowerCase();
if(Arrays.asList("COMMA", "PERIOD", "DECIMAL").contains(testKey)) character = ".";
if (testKey.length() == 1) character = testKey.toLowerCase();
if (Arrays.asList("COMMA", "PERIOD", "DECIMAL").contains(testKey)) character = ".";

switch (key) {
/*case "LSHIFT":
Expand All @@ -94,10 +113,10 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
else cursorPos = highlightStart;
break;
case "DELETE":
if(highlightStart == highlightEnd)
if (highlightStart == highlightEnd)
cursorPos++;
deleteSelection();
if(highlightStart == highlightEnd)
if (highlightStart == highlightEnd)
cursorPos--;
break;
case "LEFT":
Expand All @@ -119,7 +138,7 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
}*/
break;
}
if(Arrays.asList("BACK", "DELETE", "LEFT", "RIGHT").contains(key) || character != null) {
if (Arrays.asList("BACK", "DELETE", "LEFT", "RIGHT").contains(key) || character != null) {
cursorPos = MathUtil.constrain(cursorPos, 0, content.length());
highlightStart = cursorPos;
highlightEnd = cursorPos;
Expand All @@ -145,7 +164,10 @@ private double getCursorX() {
}

private int getCursorPosFromMousePos(Vector2D mouse) {
double x = mouse.getX() - getDisplayPos().getX() - 2;
Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name);
Vector2D rectPos = getDisplayPos().add(nameSize.getX(), 0);

double x = mouse.getX() - rectPos.getX() - 2;
if (x < 0)
return 0;
else if (x > FontRenderer.getStringSize(content).getX())
Expand All @@ -163,6 +185,10 @@ else if (x < charWidth)
return content.length();
}

public void setWidth(double width) {
this.size.setX(width);
}

@Override
public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) {
if (button == Mouse.Button.LEFT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.awt.*;
import java.util.ArrayList;

public class ScrollableList<I extends ScrollableListItem<I>> extends Component implements MouseInputListener, MouseScrollListener {
public class ScrollableList<I extends ScrollableListItem<I>> extends Component implements MouseInputListener, MouseScrollListener, KeyInputListener {
public Color backgroundColor = Color.DARK_GRAY;
public ScrollBar<I> scrollBar;
public ArrayList<I> items = new ArrayList<>();
Expand All @@ -22,6 +22,10 @@ public void addItem(I item) {
}

public ArrayList<I> getItems() {
ArrayList<I> items = new ArrayList<>();
for (int i = 0; i < getItemCount(); i++) {
items.add(getItem(i));
}
return items;
}

Expand All @@ -30,7 +34,7 @@ public int getItemCount() {
}

public I getItem(int index) {
return getItems().get(index);
return items.get(index);
}

public void render(Vector2D mouse) {
Expand Down Expand Up @@ -75,7 +79,6 @@ public Pair<I, Vector2D> getItemAndRelMousePosUnderMouse(Vector2D mouse) {
double currY = mouse.getY() - 1 - getDisplayPos().getY() + scrollBar.scrollAmount;
for (int i = 0; i < getItemCount(); i++) {
I item = getItem(i);
if (item == null) item = getItems().get(i);
if (currY >= 0 && currY <= item.getHeight()) {
return new Pair<>(item, new Vector2D(mouse.getX() - getDisplayPos().getX() - 1, currY));
}
Expand All @@ -97,40 +100,39 @@ private boolean shouldRenderScrollbar() {
}

public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) {
if (shouldRenderScrollbar())
scrollBar.handleMouseInput(state, mousePos, button);
if (shouldRenderScrollbar() && scrollBar.handleMouseInput(state, mousePos, button))
return true;

Pair<I, Vector2D> p = getItemAndRelMousePosUnderMouse(mousePos);
if (p != null) {
I item = p.first;
Vector2D relMousePos = p.second;
return item.handleMouseInput(state, mousePos, button);
}

return contains(mousePos);
return ArrayListUtil.orMapAll(
getItems(),
e -> e.handleMouseInput(state, mousePos, button)
) || contains(mousePos);
}

public boolean handleMouseScroll(Vector2D mousePos, int delta) {
Pair<I, Vector2D> p = getItemAndRelMousePosUnderMouse(mousePos);
if (p != null) {
I item = p.first;
Vector2D relMousePos = p.second;
return item.handleMouseScroll(mousePos, delta);
}
if(ArrayListUtil.orMapAll(
getItems(),
e -> e.handleMouseScroll(mousePos, delta)
)) return true;

if (shouldRenderScrollbar())
scrollBar.scrollBy(-delta);
return contains(mousePos);
}

public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
return ArrayListUtil.orMapAll(
getItems(),
e -> e.handleKeyInput(keyCode, key, pressed)
);
}

public int totalHeight() {
if (getItemCount() == 0) return 0;

int sum = 3;
ArrayList<I> items = getItems();
for (int i = 0; i < getItemCount(); i++) {
if (getItem(i) != null) sum += getItem(i).getHeight() + 1;
else sum += items.get(i).getHeight() + 1;
sum += getItem(i).getHeight() + 1;
}
return sum;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.github.kurrycat.mpkmod.util.Mouse;
import io.github.kurrycat.mpkmod.util.Vector2D;

public abstract class ScrollableListItem<I extends ScrollableListItem<I>> implements MouseInputListener, MouseScrollListener {
public abstract class ScrollableListItem<I extends ScrollableListItem<I>> implements MouseInputListener, KeyInputListener, MouseScrollListener {
protected int height;
private ScrollableList<I> parent;

Expand All @@ -26,4 +26,8 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt
public boolean handleMouseScroll(Vector2D mousePos, int delta) {
return false;
}

public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,62 +123,55 @@ public static class LBListItem extends ScrollableListItem<LBListItem> {
public CheckButton shouldRender;
public LandingBlock landingBlock;

public InputField minX, minY, minZ, maxX, maxY, maxZ;
public InputField[] fields;

public LBListItem(ScrollableList<LBListItem> parent, LandingBlock landingBlock) {
super(parent);
this.landingBlock = landingBlock;
shouldRender = new CheckButton(Vector2D.ZERO, checked -> {
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: ");

fields = new InputField[]{minX, minY, minZ, maxX, maxY, maxZ};
}

public void render(int index, Vector2D pos, Vector2D size, Vector2D mouse) {
shouldRender.pos = pos.add(size.getX() / 5 - 11, size.getY() / 2 - 5.5);
shouldRender.pos = pos.add(size.getX() / 16 - 4, size.getY() / 2 - 5.5);

Renderer2D.drawRectWithEdge(pos, size, 1, lbListColorBg, lbListColorItemEdge);
FontRenderer.drawCenteredString(
"minX: " + landingBlock.boundingBox.getMin().getX(),
pos.add(size.getX() / 5 * 2,
size.getY() / 4),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"minY: " + landingBlock.boundingBox.getMin().getY(),
pos.add(size.getX() / 5 * 2,
size.getY() / 2),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"minZ: " + landingBlock.boundingBox.getMin().getZ(),
pos.add(size.getX() / 5 * 2,
size.getY() / 4 * 3),
Color.WHITE, false
);

FontRenderer.drawCenteredString(
"maxX: " + landingBlock.boundingBox.getMax().getX(),
pos.add(size.getX() / 5 * 4,
size.getY() / 4),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"maxY: " + landingBlock.boundingBox.getMax().getY(),
pos.add(size.getX() / 5 * 4,
size.getY() / 2),
Color.WHITE, false
);
FontRenderer.drawCenteredString(
"maxZ: " + landingBlock.boundingBox.getMax().getZ(),
pos.add(size.getX() / 5 * 4,
size.getY() / 4 * 3),
Color.WHITE, false
);
for (int i = 0; i < fields.length; i++) {
fields[i].pos = pos.add(
size.getX() / 7 * (1 + ((int) (i / 3) * 3)),
size.getY() / 4 * (1 + (i % 3)) - fields[i].getSize().getY() / 2
);
fields[i].setWidth(size.getX() / 3);
fields[i].render(mouse);
}

shouldRender.render(mouse);
}

public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) {
return shouldRender.handleMouseInput(state, mousePos, button);
return ArrayListUtil.orMapAll(
ArrayListUtil.getAllOfType(MouseInputListener.class, minX, minY, minZ, maxX, maxY, maxZ, shouldRender),
ele -> ele.handleMouseInput(state, mousePos, button)
);
}

public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
return ArrayListUtil.orMapAll(
ArrayListUtil.getAllOfType(KeyInputListener.class, minX, minY, minZ, maxX, maxY, maxZ),
ele -> ele.handleKeyInput(keyCode, key, pressed)
);
}
}
}
Loading

0 comments on commit db22e6f

Please sign in to comment.