Skip to content

Commit 330118a

Browse files
committed
More WIP work on moving away from MalisisCore
1 parent 936e423 commit 330118a

21 files changed

+442
-844
lines changed

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/CwgGuiFactory.java

Lines changed: 158 additions & 93 deletions
Large diffs are not rendered by default.

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/FlatCubicGui.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
*/
2424
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui;
2525

26+
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.label;
2627
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.wrap;
2728
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.MalisisGuiUtils.vanillaText;
2829

2930
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiButton;
31+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiLabel;
3032
import io.github.opencubicchunks.cubicchunks.cubicgen.preset.FlatGeneratorSettings;
3133
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UIBorderLayout;
3234
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UIColoredPanel;
@@ -93,31 +95,29 @@ private UITabbedContainer makeTabContainer() {
9395
final int xSize = UIComponent.INHERITED - HORIZONTAL_PADDING * 2 - HORIZONTAL_INSETS * 2;
9496
final int ySize = VERTICAL_PADDING;
9597
final int xPos = HORIZONTAL_PADDING + HORIZONTAL_INSETS;
96-
CwgGuiButton prev = CwgGuiFactory.makeButton( "previous_page");
97-
CwgGuiButton next = CwgGuiFactory.makeButton( "next_page");
98+
CwgGuiButton prev = CwgGuiFactory.button( "previous_page");
99+
CwgGuiButton next = CwgGuiFactory.button( "next_page");
98100
prev.setWidth(BTN_WIDTH);
99101
next.setWidth(BTN_WIDTH);
100102

101-
CwgGuiButton done = CwgGuiFactory.makeButton( "done", btn -> FlatCubicGui.this.done());
103+
CwgGuiButton done = CwgGuiFactory.button( "done", btn -> FlatCubicGui.this.done());
102104
done.setWidth(BTN_WIDTH);
103105

104-
UIMultilineLabel label = new UIMultilineLabel(this)
105-
.setTextAnchor(Anchor.CENTER)
106-
.setFontOptions(FontOptions.builder().color(0xFFFFFF).shadow().build());
106+
CwgGuiLabel label = label();
107107

108108
UIBorderLayout upperLayout = new UIBorderLayout(this)
109109
.setSize(xSize, ySize)
110110
.setPosition(xPos, 0)
111111
.add(wrap(this, prev), UIBorderLayout.Border.LEFT)
112112
.add(wrap(this, next), UIBorderLayout.Border.RIGHT)
113-
.add(label, UIBorderLayout.Border.CENTER);
113+
.add(wrap(this, label), UIBorderLayout.Border.CENTER);
114114

115115
UIBorderLayout lowerLayout = new UIBorderLayout(this)
116116
.setSize(xSize, ySize)
117117
.setAnchor(Anchor.BOTTOM).setPosition(xPos, 0)
118118
.add(wrap(this, done), UIBorderLayout.Border.CENTER);
119119

120-
UITabbedContainer tabGroup = new UITabbedContainer(this, prev, next, label::setText);
120+
UITabbedContainer tabGroup = new UITabbedContainer(this, prev, next, label::setTranslationKey);
121121
tabGroup.add(upperLayout, lowerLayout);
122122

123123
return tabGroup;

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/MalisisGuiUtils.java

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,11 @@
2424
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui;
2525

2626
import com.google.common.base.Converter;
27-
import com.google.common.eventbus.Subscribe;
2827
import io.github.opencubicchunks.cubicchunks.cubicgen.CustomCubicMod;
29-
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UICheckboxNoAutoSize;
3028
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UIRangeSlider;
31-
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UISliderImproved;
32-
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UISplitLayout;
3329
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.converter.Converters;
34-
import net.malisis.core.client.gui.Anchor;
3530
import net.malisis.core.client.gui.MalisisGui;
36-
import net.malisis.core.client.gui.component.UIComponent;
37-
import net.malisis.core.client.gui.component.container.UIContainer;
38-
import net.malisis.core.client.gui.component.decoration.UILabel;
39-
import net.malisis.core.client.gui.component.interaction.UICheckBox;
4031
import net.malisis.core.client.gui.component.interaction.UISelect;
41-
import net.malisis.core.client.gui.component.interaction.UISlider;
42-
import net.malisis.core.client.gui.component.interaction.UITextField;
43-
import net.malisis.core.client.gui.event.ComponentEvent;
44-
import net.malisis.core.renderer.font.FontOptions;
4532
import net.minecraft.client.resources.I18n;
4633
import net.minecraft.util.math.MathHelper;
4734
import net.minecraft.world.biome.Biome;
@@ -55,9 +42,6 @@
5542
import java.util.function.BiFunction;
5643
import java.util.function.BiPredicate;
5744
import java.util.function.DoubleSupplier;
58-
import java.util.function.Function;
59-
60-
import static java.lang.Math.round;
6145

6246
public class MalisisGuiUtils {
6347

@@ -132,123 +116,6 @@ public static UISelect<BiomeOption> makeBiomeList(MalisisGui gui, int selectedId
132116
return select;
133117
}
134118

135-
public static UIComponent<?> label(MalisisGui gui, String text) {
136-
return wrappedCentered(
137-
gui, new UILabel(gui, text)
138-
.setFontOptions(FontOptions.builder().color(0xFFFFFF).shadow().build())
139-
).setSize(0, 15);
140-
}
141-
142-
// textInput as argument so that it's easy to access it later
143-
// otherwise, to access the value of the text field, it would be necessary to get it out of implementation-specific contaier
144-
public static UIComponent<?> floatInput(ExtraGui gui, String text, UITextField textField, float defaultValue) {
145-
146-
try {
147-
Function<String, String> filter = newStr -> {
148-
try {
149-
Float.parseFloat(newStr);
150-
return newStr;
151-
} catch (NumberFormatException e1) {
152-
// bug in 6.5.1 where the old text is actually the new text
153-
String str = textField.getText();
154-
try {
155-
Float.parseFloat(newStr);
156-
return str; // return old text
157-
} catch (NumberFormatException e2) {
158-
// this is ugly...
159-
160-
// first, is it empty or a single character that doesn't parse?
161-
if (str.length() <= 1) {
162-
return "";
163-
}
164-
// this should cover all the "user is typing" cases
165-
int length = str.length();
166-
// iterate end-to-beginning and check if after removing that character, it becomes a valid number
167-
for (int i = length - 1; i >= 0; i--) {
168-
String sub = str.substring(0, i) + str.substring(i + 1);
169-
try {
170-
Float.parseFloat(sub);
171-
return sub;
172-
} catch (NumberFormatException e3) {
173-
}
174-
}
175-
// uh... we still didn't return?
176-
// I don't know what could trigger this, but this is the way we will try to handle it:
177-
// iterate over the characters and remove everything we don't want.
178-
// * up until the dot, remove everything non-digit
179-
// * except 'e' if it's second or later character, then assume there was no dot, and after that, that we are past 'e'
180-
// * if there was no dot, or we are past the dot, remove everything non-digit except 'e'
181-
// * after an 'e', remove everything non-digit
182-
// If it *still* doesn't parse, give up and return empty string
183-
StringBuilder newsb = new StringBuilder(str.length());
184-
boolean seenFirstDigit = false;
185-
boolean seenDot = false;
186-
boolean seenE = false;
187-
for (char ch : str.toCharArray()) {
188-
if (ch >= '0' && ch <= '9') {
189-
newsb.append(ch);
190-
seenFirstDigit = true;
191-
} else if (ch == 'e' || ch == 'E') {
192-
if (!seenE && seenFirstDigit) {
193-
newsb.append(ch);
194-
seenE = true;
195-
seenDot = true;
196-
}
197-
} else if (ch == '.') {
198-
if (!seenDot) {
199-
newsb.append(ch);
200-
seenDot = true;
201-
}
202-
}
203-
}
204-
str = newsb.toString();
205-
try {
206-
Float.parseFloat(str);
207-
return str;
208-
} catch (NumberFormatException e3) {
209-
return "";
210-
}
211-
}
212-
}
213-
};
214-
textField.setFilter(filter);
215-
// another (imperfect) hack because filter isn't applied when remoing characters
216-
textField.register(new Object() {
217-
@Subscribe
218-
public void onValueChange(ComponentEvent.ValueChange<UITextField, String> change) {
219-
String newText = filter.apply(change.getNewValue());
220-
if (!newText.equals(change.getNewValue())) {
221-
textField.setText(newText);
222-
}
223-
}
224-
});
225-
} catch (Throwable t) {
226-
throw new RuntimeException(t);
227-
}
228-
229-
textField.setEditable(true);
230-
textField.setText(String.format("%.1f", defaultValue));
231-
textField.setFontOptions(FontOptions.builder().color(0xFFFFFF).build());
232-
UIComponent<?> label = wrappedMiddle(gui, new UILabel(gui, text).setFontOptions(FontOptions.builder().color(0xFFFFFF).build()));
233-
UISplitLayout<?> split = new UISplitLayout<>(gui, UISplitLayout.Type.SIDE_BY_SIDE, label, textField);
234-
split.setSizeOf(UISplitLayout.Pos.SECOND, 40);
235-
split.autoFitToContent(true);
236-
return split;
237-
}
238-
public static UIContainer<?> wrappedCentered(MalisisGui gui, UIComponent<?> comp) {
239-
comp.setAnchor(Anchor.MIDDLE | Anchor.CENTER);
240-
UIContainer<?> cont = new UIContainer<>(gui);
241-
cont.add(comp);
242-
return cont;
243-
}
244-
245-
public static UIContainer<?> wrappedMiddle(MalisisGui gui, UIComponent<?> comp) {
246-
comp.setAnchor(Anchor.MIDDLE);
247-
UIContainer<?> cont = new UIContainer<>(gui);
248-
cont.add(comp);
249-
return cont;
250-
}
251-
252119
public static String vanillaText(String name) {
253120
String unloc = CustomCubicMod.MODID + ".gui.cubicgen." + name;
254121
return unloc;

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiBlockStateButton.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public class CwgGuiBlockStateButton extends GuiButton {
5656
private String tooltip;
5757
private BlockStateDesc blockState;
5858
private Consumer<CwgGuiBlockStateButton> onClick;
59-
private Consumer<CwgGuiBlockStateButton> onRightClick;
6059

6160
public CwgGuiBlockStateButton(BlockStateDesc blockState) {
6261
super(0, 0, 0, SIZE, SIZE, "");

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiButton.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ public class CwgGuiButton extends GuiButtonExt {
3535

3636
private Consumer<CwgGuiButton> onClick;
3737

38-
public CwgGuiButton(String formatString, @Nullable Consumer<CwgGuiButton> onClick) {
39-
super(0, 0, 0, I18n.format(formatString));
38+
public static CwgGuiButton create(String formatString, @Nullable Consumer<CwgGuiButton> onClick) {
39+
return new CwgGuiButton(I18n.format(formatString), onClick);
40+
}
41+
42+
public static CwgGuiButton createUnlocalized(String text, @Nullable Consumer<CwgGuiButton> onClick) {
43+
return new CwgGuiButton(text, onClick);
44+
}
45+
46+
private CwgGuiButton(String text, @Nullable Consumer<CwgGuiButton> onClick) {
47+
super(0, 0, 0, text);
4048
this.onClick = onClick;
4149
}
4250

@@ -46,7 +54,9 @@ public void onClick(Consumer<CwgGuiButton> handler) {
4654

4755
@Override public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
4856
if (super.mousePressed(mc, mouseX, mouseY)) {
49-
this.onClick.accept(this);
57+
if (this.onClick != null) {
58+
this.onClick.accept(this);
59+
}
5060
return true;
5161
}
5262
return false;

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiCheckBox.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ public class CwgGuiCheckBox extends GuiCheckBox {
3333

3434
private Consumer<CwgGuiCheckBox> clickHandler = x -> {};
3535

36-
public CwgGuiCheckBox(String formatString, boolean isChecked) {
37-
super(0, 0, 0, I18n.format(formatString), isChecked);
36+
public static CwgGuiCheckBox create(String formatString, boolean isChecked) {
37+
return new CwgGuiCheckBox(I18n.format(formatString), isChecked);
38+
}
39+
40+
public static CwgGuiCheckBox createUnlocalized(String formatString, boolean isChecked) {
41+
return new CwgGuiCheckBox(formatString, isChecked);
42+
}
43+
44+
private CwgGuiCheckBox(String formatString, boolean isChecked) {
45+
super(0, 0, 0, formatString, isChecked);
3846
}
3947

4048
public void onClick(Consumer<CwgGuiCheckBox> action) {

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiLabel.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,41 @@
3030

3131
public class CwgGuiLabel extends GuiLabel {
3232

33-
public CwgGuiLabel(String text, int xIn, int yIn, int widthIn, int heightIn, int colorIn) {
34-
super(Minecraft.getMinecraft().fontRenderer, 0, xIn, yIn, widthIn, heightIn, colorIn);
35-
setText(text);
33+
public static CwgGuiLabel create(String formatString, int x, int y, int width, int height, int color) {
34+
CwgGuiLabel label = new CwgGuiLabel(x, y, width, height, color);
35+
label.setTranslationKey(formatString);
36+
return label;
3637
}
3738

38-
public void setText(String text) {
39+
public static CwgGuiLabel createUnlocalized(String text, int x, int y, int width, int height, int color) {
40+
CwgGuiLabel label = new CwgGuiLabel(x, y, width, height, color);
41+
label.setUnlocalizedText(text);
42+
return label;
43+
}
44+
45+
public static CwgGuiLabel create(int x, int y, int width, int height, int color) {
46+
return new CwgGuiLabel(x, y, width, height, color);
47+
}
48+
49+
private CwgGuiLabel(int x, int y, int width, int height, int color) {
50+
super(Minecraft.getMinecraft().fontRenderer, 0, x, y, width, height, color);
51+
}
52+
53+
public void setTranslationKey(String text) {
3954
((IGuiLabel) this).getLabels().clear();
4055
for (String s : I18n.format(text).split("\n")) {
4156
addLine(s);
4257
}
4358
}
59+
60+
public void setUnlocalizedText(String text) {
61+
setLines(text.split("\n"));
62+
}
63+
64+
public void setLines(String... text) {
65+
((IGuiLabel) this).getLabels().clear();
66+
for (String s : text) {
67+
addLine(s);
68+
}
69+
}
4470
}

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiSlider.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@
3636
// TODO: fix vanilla rendering for large widths
3737
public class CwgGuiSlider extends GuiButtonExt {
3838

39-
private final String textFormat;
4039
private final Function<Double, String> valueToString;
4140
private final Converter<Double, Double> positionToValue;
4241

4342
private double sliderPosition;
4443
private boolean mousePressed;
4544
private Consumer<CwgGuiSlider> updateHandler = x -> {};
4645

47-
public CwgGuiSlider(String textFormat, Function<Double, String> valueToString, Converter<Double, Double> positionToValue, double defaultValue) {
46+
public static CwgGuiSlider create(String fmt, Function<Double, Object[]> val2Params, Converter<Double, Double> pos2val, double defaultValue) {
47+
return new CwgGuiSlider(val -> I18n.format(fmt, val2Params.apply(val)), pos2val, defaultValue);
48+
}
49+
50+
public static CwgGuiSlider create(String fmt, Converter<Double, Double> pos2val, double defaultValue) {
51+
return new CwgGuiSlider(val -> I18n.format(fmt, val), pos2val, defaultValue);
52+
}
53+
54+
private CwgGuiSlider(Function<Double, String> displayString, Converter<Double, Double> positionToValue, double defaultValue) {
4855
super(0, 0, 0, "");
49-
this.textFormat = textFormat;
50-
this.valueToString = valueToString;
56+
this.valueToString = displayString;
5157
this.positionToValue = positionToValue;
5258
this.sliderPosition = positionToValue.reverse().convert(defaultValue);
5359
}
@@ -84,7 +90,7 @@ public void setSliderValue(double sliderValue) {
8490
updateHandler.accept(this);
8591
}
8692

87-
this.displayString = I18n.format(textFormat, valueToString.apply(getSliderValue()));
93+
this.displayString = valueToString.apply(getSliderValue());
8894

8995
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
9096
this.drawTexturedModalRect(this.x + (int)(this.sliderPosition * (this.width - 8)), this.y, 0, 66, 4, 20);

0 commit comments

Comments
 (0)