Skip to content

Commit

Permalink
- added clickable Color List in main gui
Browse files Browse the repository at this point in the history
- added plus buttons to variables
  • Loading branch information
kurrycat committed Jul 9, 2023
1 parent 53cf868 commit 4a9a414
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 82 deletions.
2 changes: 2 additions & 0 deletions common/src/main/java/io/github/kurrycat/mpkmod/gui/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.awt.*;

public class Theme {
public static Color NONE = new Color(0, 0, 0, 0);

public static Color lightEdge = new Color(255, 255, 255, 95);
public static Color darkEdge = new Color(0, 0, 0, 255);
public static Color darkBackground = new Color(31, 31, 31, 150);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
public class ColorLabel extends Label {
private final Colors color;

public ColorLabel(Colors color) {
super(color.getCode() + color.getName());
this.color = color;
}

public ColorLabel(Colors color, Vector2D pos) {
this(color);
this.setPos(pos);
}

public ColorLabel(Colors color) {
super(color.getCode() + color.getName());
this.color = color;
}

@Override
public void render(Vector2D mouse) {
this.text = contains(mouse) ? color.getName() : color.getCode() + color.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.kurrycat.mpkmod.Main;
import io.github.kurrycat.mpkmod.compatibility.MCClasses.FontRenderer;
import io.github.kurrycat.mpkmod.gui.Theme;
import io.github.kurrycat.mpkmod.gui.TickThread;
import io.github.kurrycat.mpkmod.gui.infovars.InfoString;
import io.github.kurrycat.mpkmod.gui.infovars.InfoVar;
Expand All @@ -16,9 +17,8 @@
import io.github.kurrycat.mpkmod.util.Vector2D;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

public class InfoLabel extends Label implements TickThread.Tickable {
Expand Down Expand Up @@ -86,11 +86,13 @@ public PopupMenu getPopupMenu() {

private static class InfoVarList extends ScrollableList<InfoVarListItem> {
private final List<InfoVarListItem> allItems;
private final InputField inputField;

public InfoVarList(Vector2D pos, Vector2D size) {
public InfoVarList(InputField inputField, Vector2D pos, Vector2D size) {
this.setPos(pos);
this.setSize(size);
this.setTitle("Variables");
this.inputField = inputField;
allItems = Main.infoTree.getEntries().stream()
.map(entry ->
new InfoVarListItem(this, entry.getValue())
Expand All @@ -113,26 +115,23 @@ public void render(Vector2D mouse) {

private static class InfoVarListItem extends ScrollableListItem<InfoVarListItem> {
public static final int HEIGHT = 18;
private final String varName;
private final InfoVarComponent infoVarComponent;

public InfoVarListItem(ScrollableList<InfoVarListItem> parent, InfoVar infoVar) {
public InfoVarListItem(InfoVarList parent, InfoVar infoVar) {
super(parent);
this.setHeight(18);
this.varName = infoVar.getName();
this.infoVarComponent = new InfoVarComponent(infoVar);
this.infoVarComponent = new InfoVarComponent(infoVar, parent.inputField);
infoVarComponent.setSize(new Vector2D(1, -HEIGHT));
passPositionTo(infoVarComponent, PERCENT.SIZE_X);
}

@Override
public int getHeight() {
return infoVarComponent.collapsed ? HEIGHT : infoVarComponent.getHeight();
return infoVarComponent.getHeight();
}

@Override
public void render(int index, Vector2D pos, Vector2D size, Vector2D mouse) {
//renderDefaultBorder(pos, size);
infoVarComponent.render(mouse);
}

Expand All @@ -149,17 +148,20 @@ public boolean containsSearchString(String searchString) {

private static class InfoVarComponent extends Component implements MouseInputListener {
private final InfoVar infoVar;
private final InputField inputField;
private final ArrayList<InfoVarComponent> children = new ArrayList<>();
private final Button collapseButton;
private final Button addButton;
private final boolean showCollapseButton;
public boolean collapsed = true;
private InfoVarComponent parent = null;

public InfoVarComponent(InfoVar infoVar) {
public InfoVarComponent(InfoVar infoVar, InputField inputField) {
this.infoVar = infoVar;
this.inputField = inputField;
double[] i = {InfoVarListItem.HEIGHT};
infoVar.getEntries().stream()
.map(entry -> new InfoVarComponent(entry.getValue()))
.map(entry -> new InfoVarComponent(entry.getValue(), inputField))
.forEach(c -> {
c.parent = this;
c.setSize(new Vector2D(-4, InfoVarListItem.HEIGHT));
Expand All @@ -186,13 +188,22 @@ public InfoVarComponent(InfoVar infoVar) {
buttonHolder.setSize(new Vector2D(1, InfoVarListItem.HEIGHT));
passPositionTo(buttonHolder, PERCENT.SIZE_X);

collapseButton = new Button("v", new Vector2D(1, 0), new Vector2D(11, 11));
collapseButton = new Button("v", new Vector2D(13, 0), new Vector2D(11, 11));
collapseButton.setButtonCallback(mouseButton -> {
if (mouseButton != Mouse.Button.LEFT) return;
setCollapsed(!collapsed);
});
buttonHolder.passPositionTo(collapseButton, PERCENT.NONE, Anchor.CENTER_RIGHT);
components.add(collapseButton);

addButton = new Button("+", new Vector2D(1, 0), new Vector2D(11, 11));
addButton.setButtonCallback(mouseButton -> {
if (mouseButton != Mouse.Button.LEFT) return;
inputField.typeContentAtCursor("{" + infoVar.getFullName() + "}");
inputField.focus();
});
buttonHolder.passPositionTo(addButton, PERCENT.NONE, Anchor.CENTER_RIGHT);
components.add(addButton);
}

private void setCollapsed(boolean collapsed) {
Expand Down Expand Up @@ -250,11 +261,49 @@ public int getHeight() {

@Override
public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) {
return showCollapseButton && collapseButton.handleMouseInput(state, mousePos, button) ||
return addButton.handleMouseInput(state, mousePos, button) ||
showCollapseButton && collapseButton.handleMouseInput(state, mousePos, button) ||
!collapsed && ArrayListUtil.orMap(children, c -> c.handleMouseInput(state, mousePos, button));
}
}

private static class ColorList extends ScrollableList<ColorItem> {
public ColorList(InputField inputField) {
super();
setTitle("Colors");
setPos(new Vector2D(5, 5));
double width = Arrays.stream(Colors.values())
.map(c -> FontRenderer.getStringSize(c.getName()).getX())
.max(Comparator.naturalOrder()).orElse(50D);
setSize(new Vector2D(width + 15, -10));
for (Colors c : Colors.values()) {
addItem(new ColorItem(this, inputField, c));
}
}
}

private static class ColorItem extends ScrollableListItem<ColorItem> {
public ColorItem(ScrollableList<ColorItem> parent, InputField inputField, Colors color) {
super(parent);
setHeight(12);
Button button = new Button(
color.getCode() + color.getName(),
new Vector2D(0, 0), new Vector2D(1, 1),
mouseButton -> {
inputField.typeContentAtCursor("{" + color.getName() + "}");
inputField.focus();
}
);
button.normalColor = Theme.NONE;
addChild(button, PERCENT.SIZE);
}

@Override
public void render(int index, Vector2D pos, Vector2D size, Vector2D mouse) {
renderComponents(mouse);
}
}

public class EditPane extends Pane<MainGuiScreen> {
private final TextRectangle label;

Expand All @@ -274,16 +323,15 @@ public EditPane() {

addChild(inputField, PERCENT.SIZE_X, Anchor.BOTTOM_CENTER);

Div colors = new Div(new Vector2D(5, 5), new Vector2D(0, 0));
addChild(colors);
for (Colors c : Colors.values()) {
colors.addChildBelow(new ColorLabel(c));
}
colors.backgroundColor = new Color(31, 31, 31, 150);
colors.setAbsolute(true);
ColorList cl = new ColorList(inputField);
cl.setAbsolute(true);
addChild(cl);


InfoVarList vl = new InfoVarList(new Vector2D(5, 5), new Vector2D(2 / 9D, -10));
InfoVarList vl = new InfoVarList(
inputField,
new Vector2D(5, 5),
new Vector2D(2 / 9D, -10)
);
vl.setAbsolute(true);
addChild(vl, PERCENT.SIZE_X, Anchor.TOP_RIGHT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,19 @@ public InputField(Vector2D pos, double width) {
this("", pos, width, false);
}

public InputField(String content, Vector2D pos, double width) {
this(content, pos, width, false);
}

public InputField(String content, Vector2D pos, double width, boolean numbersOnly) {
this.setPos(pos);
this.setSize(new Vector2D(width, HEIGHT));
this.content = content;
this.numbersOnly = numbersOnly;
}

private String getFilter() {
if (customFilter != null) {
return customFilter;
}
return numbersOnly ? FILTER_NUMBERS : FILTER_ALL;
public InputField(String content, Vector2D pos, double width) {
this(content, pos, width, false);
}

public InputField setFilter(String filter) {
this.customFilter = filter;
return this;
public void focus() {
isFocused = true;
}

public InputField setName(String name) {
Expand Down Expand Up @@ -116,14 +108,17 @@ public void render(Vector2D mouse) {
);
}

private double getCursorX() {
return 2 + FontRenderer.getStringSize(content.substring(0, cursorPos)).getX();
}

@Override
public boolean handleKeyInput(int keyCode, int scanCode, int modifiers, boolean isCharTyped) {
if (!isFocused) return false;
boolean inputPerformed = true;

if (isCharTyped && String.valueOf((char) keyCode).matches(getFilter())) {
replaceSelectionWithChar(Character.toString((char) keyCode));
cursorPos = highlightStart + 1;
typeContentAtCursor(Character.toString((char) keyCode));
} else {
switch (keyCode) {
case InputConstants.KEY_BACKSPACE:
Expand Down Expand Up @@ -164,17 +159,31 @@ public boolean handleKeyInput(int keyCode, int scanCode, int modifiers, boolean
return true;
}

private String getFilter() {
if (customFilter != null) {
return customFilter;
}
return numbersOnly ? FILTER_NUMBERS : FILTER_ALL;
}

public InputField setFilter(String filter) {
this.customFilter = filter;
return this;
}

public void typeContentAtCursor(String c) {
updateContent(content.substring(0, highlightStart) + c + content.substring(highlightEnd));
cursorPos = highlightStart + c.length();
highlightStart = highlightEnd = cursorPos;
}

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

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

private void updateContent(String content) {
this.content = content;
if (onContentChange != null)
Expand All @@ -189,32 +198,6 @@ public void clear() {
content = "";
}

private double getCursorX() {
return 2 + FontRenderer.getStringSize(content.substring(0, cursorPos)).getX();
}

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

double x = mouse.getX() - rectPos.getX() - 2;
if (x < 0)
return 0;
else if (x > FontRenderer.getStringSize(content).getX())
return content.length();

for (int i = 1; i <= content.length(); i++) {
int charWidth = FontRenderer.getStringSize(content.substring(i - 1, i)).getXI();
if (x < charWidth / 2D)
return i - 1;
else if (x < charWidth)
return i;

x -= charWidth;
}
return content.length();
}

public void setWidth(double width) {
this.size.setX(width);
}
Expand All @@ -232,8 +215,6 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt
return true;
} else {
isFocused = false;
highlightStart = 0;
highlightEnd = 0;
}
case DRAG:
case UP:
Expand All @@ -246,14 +227,35 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt
highlightStart = cursorPos;
highlightEnd = c;
}

return contains(mousePos);
return true;
}
}
}
return false;
}

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

double x = mouse.getX() - rectPos.getX() - 2;
if (x < 0)
return 0;
else if (x > FontRenderer.getStringSize(content).getX())
return content.length();

for (int i = 1; i <= content.length(); i++) {
int charWidth = FontRenderer.getStringSize(content.substring(i - 1, i)).getXI();
if (x < charWidth / 2D)
return i - 1;
else if (x < charWidth)
return i;

x -= charWidth;
}
return content.length();
}

@FunctionalInterface
public interface ContentProvider {
void apply(Content content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ else if (o instanceof Float)
return MathUtil.formatDecimals((Float) o, decimals, keepZeros);
else if (o instanceof FormatDecimals)
return ((FormatDecimals) o).formatDecimals(decimals, keepZeros);
if(o.toString().equals(o.getClass().getName() + "@" + Integer.toHexString(o.hashCode())))
return "[" + o.getClass().getSimpleName() + " object]";
return o.toString();
}
}
Expand Down
Loading

0 comments on commit 4a9a414

Please sign in to comment.