Skip to content

Commit 2f674b8

Browse files
committed
Migrate most (all?) buttons, sliders and checkboxes to vanilla components
1 parent 053861f commit 2f674b8

File tree

17 files changed

+1001
-1005
lines changed

17 files changed

+1001
-1005
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* This file is part of Cubic World Generation, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2015-2020 contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui;
25+
26+
import com.google.common.base.Converter;
27+
import io.github.opencubicchunks.cubicchunks.cubicgen.CustomCubicMod;
28+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiButton;
29+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiCheckBox;
30+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiSlider;
31+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.WrappedVanillaButton;
32+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.converter.Converters;
33+
import net.malisis.core.client.gui.MalisisGui;
34+
import net.minecraft.client.gui.GuiButton;
35+
36+
import java.util.function.BiPredicate;
37+
import java.util.function.Consumer;
38+
import java.util.function.DoubleSupplier;
39+
import java.util.function.Function;
40+
41+
import javax.annotation.Nonnull;
42+
43+
public class CwgGuiFactory {
44+
45+
private CwgGuiFactory() {
46+
throw new Error();
47+
}
48+
49+
public static <T extends GuiButton> WrappedVanillaButton<T> wrap(MalisisGui gui, T vanillaComponent) {
50+
return new WrappedVanillaButton<>(gui, vanillaComponent);
51+
}
52+
53+
public static CwgGuiButton makeButton(String formatString) {
54+
return new CwgGuiButton(str(formatString), null);
55+
}
56+
57+
public static CwgGuiButton makeButton(String formatString, Consumer<CwgGuiButton> onClick) {
58+
return new CwgGuiButton(str(formatString), onClick);
59+
}
60+
61+
public static CwgGuiCheckBox makeCheckBox(String formatString, boolean defaultValue) {
62+
return new CwgGuiCheckBox(str(formatString), defaultValue);
63+
}
64+
65+
public static CwgGuiCheckBox makeCheckBoxUnlocalized(String text, boolean defaultValue) {
66+
return new CwgGuiCheckBox(text, defaultValue);
67+
}
68+
69+
public static CwgGuiSlider makeSlider(double min, double max, double defVal, String formatString, Function<Double, String> toString) {
70+
71+
CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1];
72+
BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider);
73+
74+
double defMult = defVal == 0 ? 1 : defVal;
75+
76+
Converter<Double, Double> conv = Converters.builder()
77+
.linearScale(min, max).rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128)
78+
.withRoundingRadiusPredicate(isInRoundRadius)
79+
.build();
80+
81+
CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defVal);
82+
wrappedSlider[0] = slider;
83+
return slider;
84+
}
85+
86+
87+
public static CwgGuiSlider makeSlider(double min, double max, double defVal, String formatString) {
88+
return makeSlider(min, max, defVal, formatString, x -> String.format("%.3f", x));
89+
}
90+
91+
public static CwgGuiSlider makeIntSlider(int min, int max, int defVal, String formatString) {
92+
return makeIntSlider(min, max, defVal, formatString, String::valueOf);
93+
}
94+
95+
public static CwgGuiSlider makeIntSlider(int min, int max, int defVal, String formatString, Function<Integer, String> toString) {
96+
97+
CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1];
98+
BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider);
99+
100+
double defMult = defVal == 0 ? 1 : defVal;
101+
102+
Converter<Double, Double> conv = Converters.builder()
103+
.linearScale(min, max).rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128)
104+
.withRoundingRadiusPredicate(isInRoundRadius)
105+
.build();
106+
107+
CwgGuiSlider slider = new CwgGuiSlider(str(formatString), x -> toString.apply((int) Math.round(x)), conv, defVal);
108+
wrappedSlider[0] = slider;
109+
return slider;
110+
}
111+
112+
public static CwgGuiSlider makePositiveExponentialSlider(double minPos, double maxPos, double defaultVal,
113+
String formatString, Function<Double, String> toString) {
114+
115+
CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1];
116+
BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider);
117+
118+
double defMult = defaultVal == 0 ? 1 : defaultVal;
119+
120+
Converter<Double, Double> conv = Converters.builder()
121+
.exponential().withBaseValue(2).withPositiveExponentRange(minPos, maxPos)
122+
.rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128)
123+
.withRoundingRadiusPredicate(isInRoundRadius)
124+
.withInfinity().positiveAt(Math.pow(2, maxPos)).negativeAt(Double.NaN)
125+
.build();
126+
127+
CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defaultVal);
128+
wrappedSlider[0] = slider;
129+
return slider;
130+
}
131+
132+
public static CwgGuiSlider makePositiveExponentialSlider(double minPos, double maxPos, double defaultVal, String formatString) {
133+
return makePositiveExponentialSlider(minPos, maxPos, defaultVal, formatString, x -> String.format("%.3f", x));
134+
}
135+
136+
public static CwgGuiSlider makeExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal,
137+
String formatString, Function<Double, String> toString) {
138+
139+
CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1];
140+
BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider);
141+
142+
double defMult = defaultVal == 0 ? 1 : defaultVal;
143+
144+
Converter<Double, Double> conv = Converters.builder()
145+
.exponential().withZero().withBaseValue(2).withNegativeExponentRange(minNeg, maxNeg).withPositiveExponentRange(minPos, maxPos)
146+
.rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128)
147+
.withRoundingRadiusPredicate(isInRoundRadius)
148+
.build();
149+
150+
CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defaultVal);
151+
wrappedSlider[0] = slider;
152+
return slider;
153+
}
154+
155+
public static CwgGuiSlider makeExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal, String formatString) {
156+
return makeExponentialSlider(minNeg, maxNeg, minPos, maxPos, defaultVal, formatString, x -> String.format("%.3f", x));
157+
}
158+
159+
public static CwgGuiSlider makeSymmetricExponentialSlider(double min, double max, double defaultVal, String formatString, Function<Double, String> toString) {
160+
return makeExponentialSlider(min, max, min, max, defaultVal, formatString, toString);
161+
}
162+
163+
public static CwgGuiSlider makeSymmetricExponentialSlider(double min, double max, double defaultVal, String formatString) {
164+
return makeExponentialSlider(min, max, min, max, defaultVal, formatString);
165+
}
166+
167+
public static CwgGuiSlider makeInvertedExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal,
168+
String formatString, Function<Double, String> toString) {
169+
170+
CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1];
171+
BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider);
172+
173+
double defMult = defaultVal == 0 ? 1 : defaultVal;
174+
175+
Converter<Double, Double> conv = Converters.builder()
176+
.reverse()
177+
.pow(2)
178+
.exponential().withZero().withBaseValue(2).withNegativeExponentRange(minNeg, maxNeg).withPositiveExponentRange(minPos, maxPos)
179+
.inverse()
180+
.rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128)
181+
.withRoundingRadiusPredicate(isInRoundRadius)
182+
.build();
183+
184+
CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defaultVal);
185+
wrappedSlider[0] = slider;
186+
return slider;
187+
}
188+
189+
public static CwgGuiSlider makeInvertedExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal, String formatString) {
190+
return makeInvertedExponentialSlider(minNeg, maxNeg, minPos, maxPos, defaultVal, formatString, x -> String.format("%.3f", x));
191+
}
192+
193+
public static CwgGuiSlider makeInvertedPositiveExponentialSlider(double min, double max, double defaultVal, String formatString) {
194+
return makeInvertedExponentialSlider(Double.NaN, Double.NaN, min, max, defaultVal, formatString);
195+
}
196+
197+
public static CwgGuiSlider makeInvertedPositiveExponentialSlider(double min, double max, double defaultVal,
198+
String formatString, Function<Double, String> toString) {
199+
return makeInvertedExponentialSlider(Double.NaN, Double.NaN, min, max, defaultVal, formatString, toString);
200+
}
201+
202+
// internal utils
203+
204+
@Nonnull private static BiPredicate<Double, Double> getIsInRoundRadiusPredicate(CwgGuiSlider[] slider) {
205+
return getIsInRoundRadiusPredicate(() -> slider[0] == null ? 1000 : slider[0].width);
206+
}
207+
208+
@Nonnull private static BiPredicate<Double, Double> getIsInRoundRadiusPredicate(DoubleSupplier width) {
209+
return (previousSlide, foundSlide) -> {
210+
double w = width.getAsDouble();
211+
double rangeCenter = Math.round(previousSlide * w) / w;
212+
double minRange = rangeCenter - 0.5 / w;
213+
double maxRange = rangeCenter + 0.5 / w;
214+
215+
return foundSlide >= minRange && foundSlide <= maxRange;
216+
};
217+
}
218+
219+
220+
private static String str(String name) {
221+
String unloc = CustomCubicMod.MODID + ".gui.cubicgen." + name;
222+
return unloc;
223+
}
224+
}

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

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

26-
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.MalisisGuiUtils.malisisText;
26+
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.CwgGuiFactory.wrap;
2727
import static io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.MalisisGuiUtils.vanillaText;
2828

29-
import com.google.common.eventbus.Subscribe;
29+
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiButton;
3030
import io.github.opencubicchunks.cubicchunks.cubicgen.preset.FlatGeneratorSettings;
3131
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UIBorderLayout;
3232
import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.UIColoredPanel;
@@ -37,7 +37,6 @@
3737
import net.malisis.core.client.gui.MalisisGui;
3838
import net.malisis.core.client.gui.component.UIComponent;
3939
import net.malisis.core.client.gui.component.container.UIContainer;
40-
import net.malisis.core.client.gui.component.interaction.UIButton;
4140
import net.malisis.core.renderer.font.FontOptions;
4241
import net.minecraft.client.gui.GuiCreateWorld;
4342

@@ -94,16 +93,13 @@ private UITabbedContainer makeTabContainer() {
9493
final int xSize = UIComponent.INHERITED - HORIZONTAL_PADDING * 2 - HORIZONTAL_INSETS * 2;
9594
final int ySize = VERTICAL_PADDING;
9695
final int xPos = HORIZONTAL_PADDING + HORIZONTAL_INSETS;
97-
UIButton prev = new UIButton(this, malisisText("previous_page")).setSize(BTN_WIDTH, 20);
98-
UIButton next = new UIButton(this, malisisText("next_page")).setSize(BTN_WIDTH, 20);
96+
CwgGuiButton prev = CwgGuiFactory.makeButton( "previous_page");
97+
CwgGuiButton next = CwgGuiFactory.makeButton( "next_page");
98+
prev.setWidth(BTN_WIDTH);
99+
next.setWidth(BTN_WIDTH);
99100

100-
UIButton done = new UIButton(this, malisisText("done")).setSize(BTN_WIDTH, 20);
101-
done.register(new Object() {
102-
@Subscribe
103-
public void onClick(UIButton.ClickEvent evt) {
104-
FlatCubicGui.this.done();
105-
}
106-
});
101+
CwgGuiButton done = CwgGuiFactory.makeButton( "done", btn -> FlatCubicGui.this.done());
102+
done.setWidth(BTN_WIDTH);
107103

108104
UIMultilineLabel label = new UIMultilineLabel(this)
109105
.setTextAnchor(Anchor.CENTER)
@@ -112,14 +108,14 @@ public void onClick(UIButton.ClickEvent evt) {
112108
UIBorderLayout upperLayout = new UIBorderLayout(this)
113109
.setSize(xSize, ySize)
114110
.setPosition(xPos, 0)
115-
.add(prev, UIBorderLayout.Border.LEFT)
116-
.add(next, UIBorderLayout.Border.RIGHT)
111+
.add(wrap(this, prev), UIBorderLayout.Border.LEFT)
112+
.add(wrap(this, next), UIBorderLayout.Border.RIGHT)
117113
.add(label, UIBorderLayout.Border.CENTER);
118114

119115
UIBorderLayout lowerLayout = new UIBorderLayout(this)
120116
.setSize(xSize, ySize)
121117
.setAnchor(Anchor.BOTTOM).setPosition(xPos, 0)
122-
.add(done, UIBorderLayout.Border.CENTER);
118+
.add(wrap(this, done), UIBorderLayout.Border.CENTER);
123119

124120
UITabbedContainer tabGroup = new UITabbedContainer(this, prev, next, label::setText);
125121
tabGroup.add(upperLayout, lowerLayout);

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

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)