Skip to content

Commit 95a539a

Browse files
committed
Implement separator component, use vanilla text fields in flat terrain layer
1 parent 6a877ea commit 95a539a

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiButton;
2929
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiCheckBox;
3030
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiLabel;
31+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiSeparator;
3132
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiSlider;
3233
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.WrappedVanillaComponent;
3334
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.converter.Converters;
3435
import net.malisis.core.client.gui.MalisisGui;
36+
import net.minecraft.client.Minecraft;
3537
import net.minecraft.client.gui.GuiButton;
3638
import net.minecraft.client.gui.GuiLabel;
3739
import net.minecraft.client.gui.GuiTextField;
@@ -61,6 +63,25 @@ public static <T extends GuiTextField> WrappedVanillaComponent<T> wrap(MalisisGu
6163
return WrappedVanillaComponent.of(gui, vanillaComponent);
6264
}
6365

66+
public static CwgGuiSeparator makeSeparator() {
67+
return new CwgGuiSeparator(0, 0);
68+
}
69+
70+
public static GuiTextField makeIntTextField(int defaultValue) {
71+
GuiTextField field = new GuiTextField(0, Minecraft.getMinecraft().fontRenderer, 0, 0, 0, 20);
72+
field.setValidator(str -> {
73+
try {
74+
Integer.parseInt(str);
75+
return true;
76+
} catch (NumberFormatException ignored) {
77+
return false;
78+
}
79+
});
80+
field.setText(String.valueOf(defaultValue));
81+
field.setEnableBackgroundDrawing();
82+
return field;
83+
}
84+
6485
public static CwgGuiLabel makeLabel(String formatString) {
6586
return makeLabel(formatString, 0xFFFFFFFF, 0, 0);
6687
}

src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/McGuiRender.java renamed to src/main/java/io/github/opencubicchunks/cubicchunks/cubicgen/common/gui/component/CwgGuiSeparator.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@
2626
import net.minecraft.client.Minecraft;
2727
import net.minecraft.client.gui.GuiButton;
2828

29-
public class McGuiRender {
29+
public class CwgGuiSeparator extends GuiButton {
3030

31-
public static void drawWidgetString(Minecraft mc, GuiButton btn, int colorOverride, boolean enabled, boolean hovered) {
32-
int color = 0xe0e0e0;
33-
if (colorOverride != 0) {
34-
color = colorOverride;
35-
} else if (!enabled) {
36-
color = 0xa0a0a0;
37-
} else if (hovered) {
38-
color = 0xffffa0;
31+
public CwgGuiSeparator(int x, int y) {
32+
super(0, x, y, "");
33+
height = 5;
34+
}
35+
36+
@Override public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) {
37+
if (visible) {
38+
drawHorizontalLine(x, x + width, y + height / 2, packedFGColour == 0 ? 0xFF767676 : packedFGColour);
3939
}
40-
btn.drawString(mc.fontRenderer, btn.displayString, btn.x, btn.y + (btn.height - 8) / 2, color);
40+
}
41+
42+
@Override public void mouseReleased(int mouseX, int mouseY) {
43+
}
44+
45+
@Override public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
46+
return false;
4147
}
4248
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525

2626
import io.github.opencubicchunks.cubicchunks.cubicgen.preset.wrapper.BlockStateDesc;
2727
import net.malisis.core.client.gui.component.container.UIContainer;
28-
import net.malisis.core.client.gui.component.decoration.UISeparator;
28+
import net.minecraft.client.gui.GuiTextField;
2929
import net.minecraft.init.Blocks;
3030

3131
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.makeButton;
32+
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.makeIntTextField;
3233
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.makeLabel;
34+
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.makeSeparator;
3335
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.wrap;
3436

3537
import io.github.opencubicchunks.cubicchunks.cubicgen.preset.FlatLayer;
@@ -49,9 +51,9 @@ public final class UIFlatTerrainLayer extends UIVerticalTableLayout<UIFlatTerrai
4951
private final CwgGuiLabel blockProperties;
5052
private final CwgGuiLabel from;
5153
private final CwgGuiLabel to;
52-
private final UISeparator separator;
53-
private final UIIntegerInputField fromField;
54-
private final UIIntegerInputField toField;
54+
private final CwgGuiSeparator separator;
55+
private final GuiTextField fromField;
56+
private final GuiTextField toField;
5557

5658
private final FlatCubicGui gui;
5759

@@ -81,10 +83,10 @@ public UIFlatTerrainLayer(FlatCubicGui guiFor, FlatLayersTab flatLayersTabFor, F
8183
from = makeLabel("from");
8284
to = makeLabel("to_exclusively");
8385

84-
fromField = new UIIntegerInputField(gui, layer.fromY);
85-
toField = new UIIntegerInputField(gui, layer.toY);
86+
fromField = makeIntTextField(layer.fromY);
87+
toField = makeIntTextField(layer.toY);
8688

87-
separator = new UISeparator(gui, false).setColor(0x767676);
89+
separator = makeSeparator();
8890

8991
/*
9092
The layout:
@@ -113,14 +115,14 @@ public UIFlatTerrainLayer(FlatCubicGui guiFor, FlatLayersTab flatLayersTabFor, F
113115
.userResizable(false).setSizeOf(UISplitLayout.Pos.SECOND, BTN_WIDTH).autoFitToContent(true);
114116

115117
UISplitLayout<?> fromLayout = new UISplitLayout<>(gui, UISplitLayout.Type.SIDE_BY_SIDE,
116-
wrap(gui, from), fromField).setSizeOf(UISplitLayout.Pos.FIRST, 50).autoFitToContent(true);
118+
wrap(gui, from), wrap(gui, fromField)).setSizeOf(UISplitLayout.Pos.FIRST, 50).autoFitToContent(true);
117119
UISplitLayout<?> toLayout = new UISplitLayout<>(gui, UISplitLayout.Type.SIDE_BY_SIDE,
118-
wrap(gui, to), toField).setSizeOf(UISplitLayout.Pos.FIRST, 90).autoFitToContent(true);
120+
wrap(gui, to), wrap(gui, toField)).setSizeOf(UISplitLayout.Pos.FIRST, 90).autoFitToContent(true);
119121

120122
this.add(blockstateButtonsSplit, new GridLocation(0, 0, 2));
121123
this.add(fromLayout, new GridLocation(0, 1, 1));
122124
this.add(toLayout, new GridLocation(1, 1, 1));
123-
this.add(separator, new GridLocation(0, 2, 2));
125+
this.add(wrap(gui, separator), new GridLocation(0, 2, 2));
124126
this.autoFitToContent(true);
125127

126128
}
@@ -135,12 +137,12 @@ protected void removeLayer() {
135137
}
136138

137139
protected void addLayer() {
138-
int to = this.toField.getValue();
140+
int to = Integer.parseInt(this.toField.getText());
139141
FlatLayer newLayer = new FlatLayer(to, to + 1, new BlockStateDesc(Blocks.SANDSTONE.getDefaultState()));
140142
this.flatLayersTab.add(this, newLayer);
141143
}
142144

143145
public FlatLayer toLayer() {
144-
return new FlatLayer(fromField.getValue(), toField.getValue(), block.getBlockState());
146+
return new FlatLayer(Integer.parseInt(fromField.getText()), Integer.parseInt(toField.getText()), block.getBlockState());
145147
}
146148
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,28 @@ public static <T extends GuiTextField> WrappedVanillaComponent<T> of(MalisisGui
9494
return new WrappedVanillaComponent<>(
9595
gui, fld,
9696
() -> fld.x, () -> fld.y, x -> fld.x = x, y -> fld.y = y,
97-
() -> fld.width, () -> fld.height, width -> fld.width = width, height -> fld.height = height,
97+
() -> fld.width, () -> fld.height, width -> {
98+
fld.width = width;
99+
// changing width affects line wrapping?
100+
fld.setSelectionPos(fld.getSelectionEnd());
101+
}, height -> fld.height = height,
98102
fld::getVisible, fld::setVisible, fld::getVisible,
99-
(mc, mouseX, mouseY, partialTick) -> fld.drawTextBox(),
103+
(mc, mouseX, mouseY, partialTick) -> {
104+
// vanilla renders outline 1 px out of bounds...
105+
if (fld.getEnableBackgroundDrawing()) {
106+
fld.x += 1;
107+
fld.y += 1;
108+
fld.width -= 2;
109+
fld.height -= 2;
110+
}
111+
fld.drawTextBox();
112+
if (fld.getEnableBackgroundDrawing()) {
113+
fld.x -= 1;
114+
fld.y -= 1;
115+
fld.width += 2;
116+
fld.height += 2;
117+
}
118+
},
100119
GuiRender.NULL,
101120
(mc, mouseX, mouseY) -> fld.mouseClicked(mouseX, mouseY, 0),
102121
MouseHandler.NULL, fld::textboxKeyTyped

0 commit comments

Comments
 (0)