Skip to content

Commit

Permalink
added pane to edit labels in
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrycat committed Oct 24, 2022
1 parent 18445ec commit d4d1679
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.kurrycat.mpkmod.events.*;
import io.github.kurrycat.mpkmod.gui.MPKGuiScreen;
import io.github.kurrycat.mpkmod.gui.components.Component;
import io.github.kurrycat.mpkmod.gui.components.InfoLabel;
import io.github.kurrycat.mpkmod.gui.screens.LandingBlockGuiScreen;
import io.github.kurrycat.mpkmod.gui.screens.main_gui.MainGuiScreen;
import io.github.kurrycat.mpkmod.landingblock.LandingBlock;
Expand Down Expand Up @@ -88,7 +89,13 @@ public static void init(String mcVersion) {
Profiler.startSection("labels");
if (mainGUI != null)
for (Component c : mainGUI.movableComponents) {
c.render(new Vector2D(-1, -1));
try {
c.render(new Vector2D(-1, -1));
} catch (ClassCastException err) {
if(c instanceof InfoLabel) {
((InfoLabel) c).infoString.updateProviders();
}
}
}
Profiler.endSection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.github.kurrycat.mpkmod.util.InfoString;
import io.github.kurrycat.mpkmod.util.Vector2D;

import java.awt.*;

public class InfoLabel extends Label {
public final InfoString infoString;

Expand Down Expand Up @@ -43,15 +45,78 @@ public Vector2D getSize() {

@Override
public PopupMenu getPopupMenu() {
Vector2D windowSize = Renderer2D.getScaledSize();
EditPane editPane = new EditPane(
new Vector2D(windowSize.getX() / 4, windowSize.getY() / 2 - 20),
new Vector2D(windowSize.getX() / 2, 40)
);


PopupMenu menu = new PopupMenu();
menu.addComponent(
new InputField(text, Vector2D.OFFSCREEN, Renderer2D.getScaledSize().getX() / 4)
.setOnContentChange(content -> {
this.text = content.getContent();
infoString.input = this.text;
infoString.updateProviders();
})
new Button("Edit", Vector2D.OFFSCREEN, new Vector2D(30, 11), mouseButton -> {
menu.parent.openPane(editPane);
menu.parent.closePane(menu);
})
);
return menu;
}

private class EditPane extends Pane {
private final Label label;
private final InputField inputField;

private class ColorLabel extends Label {
private final Colors color;
public ColorLabel(Colors color, Vector2D pos) {
super(color.getCode() + color.getName(), pos);
this.color = color;
}

@Override
public void render(Vector2D mouse) {
this.text = contains(mouse) ? color.getName() : color.getCode() + color.getName();
super.render(mouse);
}
}

public EditPane(Vector2D pos, Vector2D size) {
super(pos, size);
this.backgroundColor = new Color(31, 31, 31, 50);
this.label = new Label("", Vector2D.OFFSCREEN);
this.components.add(label);
this.inputField = new InputField(text, Vector2D.OFFSCREEN, getSize().getX())
.setOnContentChange(content -> {
text = content.getContent();
infoString.input = text;
infoString.updateProviders();
});
this.components.add(inputField);

int currY = 3;
double maxWidth = 0;
for(Colors c : Colors.values()) {
ColorLabel l = new ColorLabel(c, new Vector2D(3, currY));
this.components.add(l);
currY += l.getSize().getY() + 1;
maxWidth = Math.max(maxWidth, l.getSize().getX());
}

this.components.add(0, new Rectangle(new Vector2D(2, 2), new Vector2D(maxWidth + 2, currY - 2), new Color(31, 31, 31, 150)));
}

@Override
public void render(Vector2D mousePos) {
this.label.setText(getFormattedText());
this.label.pos = new Vector2D(
getDisplayPos().getX() + getSize().getX() / 2 - this.label.getSize().getX() / 2,
getDisplayPos().getY() + 3
);
this.inputField.pos = new Vector2D(
getDisplayPos().getX() + getSize().getX() / 2 - this.inputField.getSize().getX() / 2,
getDisplayPos().getY() + getSize().getY() - this.inputField.getSize().getY() - 1
);
super.render(mousePos);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.awt.*;
import java.util.Arrays;

//TODO: Multiline InputField
public class InputField extends Component implements KeyInputListener, MouseInputListener {
public boolean numbersOnly;
public String content;
Expand Down Expand Up @@ -101,10 +102,10 @@ public boolean handleKeyInput(char keyCode, String key, boolean pressed) {
if (pressed) {
String character = null;

String regex = numbersOnly ? "[0-9.,\\-!]" : "[0-9a-zA-Z!?,.;\\-{}]";
String regex = numbersOnly ? "[0-9.,\\-!]" : "[0-9a-zA-Z!?,.;\\-{} ]";
if(Character.toString(keyCode).matches(regex)) {
character = Character.toString(keyCode);
if(character.equals(",")) character = ".";
//if(character.equals(",")) character = ".";
}

if(key != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Label extends Component {
public Color color = Color.WHITE;
@JsonProperty
public Color selectedColor = new Color(255, 170, 0, 100);
public Color backgroundColor = null;

public Label(String text, Vector2D pos) {
super(pos);
Expand All @@ -22,6 +23,7 @@ public Label(String text, Vector2D pos) {

public void render(Vector2D mouse) {
drawDefaultSelectedBackground();
if (backgroundColor != null) Renderer2D.drawRect(getDisplayPos(), getSize(), backgroundColor);
FontRenderer.drawString(text, getDisplayPos(), color, true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.kurrycat.mpkmod.gui.components;

import io.github.kurrycat.mpkmod.compatability.MCClasses.Renderer2D;
import io.github.kurrycat.mpkmod.util.Vector2D;

import java.awt.*;

public class Rectangle extends Component {
public Color color;

public Rectangle(Vector2D pos, Vector2D size, Color color) {
super(pos);
this.setSize(size);
this.color = color;
}

@Override
public void render(Vector2D mouse) {
Renderer2D.drawRect(getDisplayPos(), getSize(), color);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.kurrycat.mpkmod.gui.screens;

import io.github.kurrycat.mpkmod.compatability.API;
import io.github.kurrycat.mpkmod.compatability.MCClasses.FontRenderer;
import io.github.kurrycat.mpkmod.compatability.MCClasses.Renderer2D;
import io.github.kurrycat.mpkmod.gui.ComponentScreen;
Expand Down Expand Up @@ -54,7 +53,7 @@ public void onGuiInit() {
)
);

components.add(
/*components.add(
new Button(
"t",
new Vector2D(
Expand All @@ -66,7 +65,7 @@ public void onGuiInit() {
ArrayListUtil.getAllOfType(InfoLabel.class, API.mainGUI.movableComponents).forEach(i -> i.infoString.updateProviders());
}
)
);
);*/
}

public void drawScreen(Vector2D mouse, float partialTicks) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/github/kurrycat/mpkmod/util/InfoString.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public static StringProvider getValueFromString(String fullMatch, String input,
if (finalCurrObj.getObj() == null) return fullMatch;
if (finalCurrObj.getObj() instanceof Double)
return MathUtil.formatDecimals((Double) finalCurrObj.getObj(), decimals, keepZeros);
if (finalCurrObj.getObj() instanceof Float)
return MathUtil.formatDecimals((Float) finalCurrObj.getObj(), decimals, keepZeros);
return finalCurrObj.getObj().toString();
};
}
Expand Down

0 comments on commit d4d1679

Please sign in to comment.