Skip to content

Commit 4fe6536

Browse files
committed
MathHelper -> MathUtils
1 parent ad2bfe4 commit 4fe6536

File tree

10 files changed

+86
-54
lines changed

10 files changed

+86
-54
lines changed

src/main/java/com/cleanroommc/modularui/screen/ClientScreenHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
2121
import com.cleanroommc.modularui.utils.Color;
2222
import com.cleanroommc.modularui.utils.FpsCounter;
23+
import com.cleanroommc.modularui.utils.MathUtils;
2324
import com.cleanroommc.modularui.utils.Platform;
2425
import com.cleanroommc.modularui.widget.sizer.Area;
2526
import com.cleanroommc.modularui.widgets.RichTextWidget;
@@ -41,7 +42,6 @@
4142
import net.minecraft.entity.player.InventoryPlayer;
4243
import net.minecraft.inventory.Slot;
4344
import net.minecraft.item.ItemStack;
44-
import net.minecraft.util.math.MathHelper;
4545
import net.minecraft.util.text.TextFormatting;
4646
import net.minecraftforge.client.event.GuiContainerEvent;
4747
import net.minecraftforge.client.event.GuiOpenEvent;
@@ -456,7 +456,7 @@ public static void drawContainer(ModularScreen muiScreen, GuiContainer mcScreen,
456456

457457
if (!acc.getDraggedStack().isEmpty() && acc.getIsRightMouseClick()) {
458458
itemstack = itemstack.copy();
459-
itemstack.setCount(MathHelper.ceil((float) itemstack.getCount() / 2.0F));
459+
itemstack.setCount(MathUtils.ceil((float) itemstack.getCount() / 2.0F));
460460
} else if (acc.getDragSplitting() && acc.getDragSplittingSlots().size() > 1) {
461461
itemstack = itemstack.copy();
462462
itemstack.setCount(acc.getDragSplittingRemnant());

src/main/java/com/cleanroommc/modularui/screen/RichTooltip.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import net.minecraft.client.renderer.RenderHelper;
2323
import net.minecraft.inventory.Slot;
2424
import net.minecraft.item.ItemStack;
25-
import net.minecraft.util.math.MathHelper;
2625
import net.minecraftforge.common.MinecraftForge;
2726

2827
import mezz.jei.input.IClickedIngredient;
@@ -197,7 +196,7 @@ public Rectangle determineTooltipArea(RichText text, GuiContext context, TextRen
197196
width = (int) renderer.getLastTrimmedWidth();
198197
height = (int) renderer.getLastTrimmedHeight();
199198
}
200-
y = MathHelper.clamp(y, padding, screenHeight - padding - height);
199+
y = MathUtils.clamp(y, padding, screenHeight - padding - height);
201200
return new Rectangle(x, y, width, height);
202201
}
203202

src/main/java/com/cleanroommc/modularui/utils/Color.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.cleanroommc.modularui.api.drawable.IInterpolation;
44

55
import net.minecraft.client.renderer.GlStateManager;
6-
import net.minecraft.util.math.MathHelper;
76
import net.minecraftforge.fml.relauncher.Side;
87
import net.minecraftforge.fml.relauncher.SideOnly;
98

@@ -74,9 +73,9 @@ public static int ofHSV(float hue, float saturation, float value) {
7473
public static int ofHSV(float hue, float saturation, float value, float alpha) {
7574
hue %= 360;
7675
if (hue < 0) hue += 360;
77-
saturation = MathHelper.clamp(saturation, 0f, 1f);
78-
value = MathHelper.clamp(value, 0f, 1f);
79-
alpha = MathHelper.clamp(alpha, 0f, 1f);
76+
saturation = MathUtils.clamp(saturation, 0f, 1f);
77+
value = MathUtils.clamp(value, 0f, 1f);
78+
alpha = MathUtils.clamp(alpha, 0f, 1f);
8079
float c = value * saturation;
8180
float x = c * (1 - Math.abs(hue / 60f % 2 - 1));
8281
float m = value - c;
@@ -107,9 +106,9 @@ public static int ofHSL(float hue, float saturation, float lightness) {
107106
public static int ofHSL(float hue, float saturation, float lightness, float alpha) {
108107
hue %= 360;
109108
if (hue < 0) hue += 360;
110-
saturation = MathHelper.clamp(saturation, 0f, 1f);
111-
lightness = MathHelper.clamp(lightness, 0f, 1f);
112-
alpha = MathHelper.clamp(alpha, 0f, 1f);
109+
saturation = MathUtils.clamp(saturation, 0f, 1f);
110+
lightness = MathUtils.clamp(lightness, 0f, 1f);
111+
alpha = MathUtils.clamp(alpha, 0f, 1f);
113112
float c = (1 - Math.abs(2 * lightness - 1)) * saturation;
114113
float x = c * (1 - Math.abs(hue / 60f % 2 - 1));
115114
float m = lightness - c / 2;
@@ -699,7 +698,7 @@ public static int interpolate(int color1, int color2, float value) {
699698
* @return interpolated ARGB color
700699
*/
701700
public static int interpolate(IInterpolation curve, int color1, int color2, float value) {
702-
value = MathHelper.clamp(value, 0, 1);
701+
value = MathUtils.clamp(value, 0, 1);
703702
int r = (int) curve.interpolate(Color.getRed(color1), Color.getRed(color2), value);
704703
int g = (int) curve.interpolate(Color.getGreen(color1), Color.getGreen(color2), value);
705704
int b = (int) curve.interpolate(Color.getBlue(color1), Color.getBlue(color2), value);
@@ -769,14 +768,14 @@ public static int ofJson(JsonElement jsonElement) {
769768
int alpha;
770769
if (alphaS.contains(".") || alphaS.endsWith("f") || alphaS.endsWith("F") || alphaS.endsWith("d") || alphaS.endsWith("D")) {
771770
try {
772-
alphaF = MathHelper.clamp(Float.parseFloat(alphaS), 0f, 1f);
771+
alphaF = MathUtils.clamp(Float.parseFloat(alphaS), 0f, 1f);
773772
alpha = (int) (alphaF * 255);
774773
} catch (NumberFormatException e) {
775774
throw new JsonParseException("Failed to parse alpha value", e);
776775
}
777776
} else {
778777
try {
779-
alpha = MathHelper.clamp(Integer.parseInt(alphaS), 0, 255);
778+
alpha = MathUtils.clamp(Integer.parseInt(alphaS), 0, 255);
780779
alphaF = alpha / 255f;
781780
} catch (NumberFormatException e) {
782781
throw new JsonParseException("Failed to parse alpha value", e);

src/main/java/com/cleanroommc/modularui/utils/Interpolation.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.cleanroommc.modularui.api.drawable.IInterpolation;
44

55
import net.minecraft.util.IStringSerializable;
6-
import net.minecraft.util.math.MathHelper;
76

87
import org.jetbrains.annotations.NotNull;
98

@@ -274,7 +273,7 @@ public float interpolate(float a, float b, float x) {
274273
CIRCLE_IN("circle_in") {
275274
@Override
276275
public float interpolate(float a, float b, float x) {
277-
x = MathHelper.clamp(x, 0, 1);
276+
x = MathUtils.clamp(x, 0, 1);
278277

279278
float factor = 1 - (float) Math.sqrt(1 - Math.pow(x, 2));
280279

@@ -284,7 +283,7 @@ public float interpolate(float a, float b, float x) {
284283
CIRCLE_OUT("circle_out") {
285284
@Override
286285
public float interpolate(float a, float b, float x) {
287-
x = MathHelper.clamp(x, 0, 1);
286+
x = MathUtils.clamp(x, 0, 1);
288287

289288
float factor = (float) Math.sqrt(1 - Math.pow(x - 1, 2));
290289

@@ -294,7 +293,7 @@ public float interpolate(float a, float b, float x) {
294293
CIRCLE_INOUT("circle_inout") {
295294
@Override
296295
public float interpolate(float a, float b, float x) {
297-
x = MathHelper.clamp(x, 0, 1);
296+
x = MathUtils.clamp(x, 0, 1);
298297

299298
float factor = x < 0.5
300299
? (float) (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2
@@ -323,4 +322,4 @@ public static Interpolation getForName(String name) {
323322
}
324323
return null;
325324
}
326-
}
325+
}

src/main/java/com/cleanroommc/modularui/utils/Interpolations.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.cleanroommc.modularui.utils;
22

3-
import net.minecraft.util.math.MathHelper;
4-
53
/**
64
* Interpolation methods
75
* <p>
@@ -32,8 +30,8 @@ public static int lerp(int a, int b, float position) {
3230
* This interpolation method fixes this problem.
3331
*/
3432
public static float lerpYaw(float a, float b, float position) {
35-
a = MathHelper.wrapDegrees(a);
36-
b = MathHelper.wrapDegrees(b);
33+
a = MathUtils.wrapDegrees(a);
34+
b = MathUtils.wrapDegrees(b);
3735

3836
return lerp(a, normalizeYaw(a, b), position);
3937
}
@@ -60,10 +58,10 @@ public static double cubicHermite(double y0, double y1, double y2, double y3, do
6058
* Yaw normalization for cubic interpolation
6159
*/
6260
public static double cubicHermiteYaw(float y0, float y1, float y2, float y3, float position) {
63-
y0 = MathHelper.wrapDegrees(y0);
64-
y1 = MathHelper.wrapDegrees(y1);
65-
y2 = MathHelper.wrapDegrees(y2);
66-
y3 = MathHelper.wrapDegrees(y3);
61+
y0 = MathUtils.wrapDegrees(y0);
62+
y1 = MathUtils.wrapDegrees(y1);
63+
y2 = MathUtils.wrapDegrees(y2);
64+
y3 = MathUtils.wrapDegrees(y3);
6765

6866
y1 = normalizeYaw(y0, y1);
6967
y2 = normalizeYaw(y1, y2);
@@ -93,10 +91,10 @@ public static float cubic(float y0, float y1, float y2, float y3, float x) {
9391
* Yaw normalization for cubic interpolation
9492
*/
9593
public static float cubicYaw(float y0, float y1, float y2, float y3, float position) {
96-
y0 = MathHelper.wrapDegrees(y0);
97-
y1 = MathHelper.wrapDegrees(y1);
98-
y2 = MathHelper.wrapDegrees(y2);
99-
y3 = MathHelper.wrapDegrees(y3);
94+
y0 = MathUtils.wrapDegrees(y0);
95+
y1 = MathUtils.wrapDegrees(y1);
96+
y2 = MathUtils.wrapDegrees(y2);
97+
y3 = MathUtils.wrapDegrees(y3);
10098

10199
y1 = normalizeYaw(y0, y1);
102100
y2 = normalizeYaw(y1, y2);
@@ -215,8 +213,8 @@ public static double lerp(double a, double b, double position) {
215213
* This interpolation method fixes this problem.
216214
*/
217215
public static double lerpYaw(double a, double b, double position) {
218-
a = MathHelper.wrapDegrees(a);
219-
b = MathHelper.wrapDegrees(b);
216+
a = MathUtils.wrapDegrees(a);
217+
b = MathUtils.wrapDegrees(b);
220218

221219
return lerp(a, normalizeYaw(a, b), position);
222220
}
@@ -242,10 +240,10 @@ public static double cubic(double y0, double y1, double y2, double y3, double x)
242240
* Yaw normalization for cubic interpolation
243241
*/
244242
public static double cubicYaw(double y0, double y1, double y2, double y3, double position) {
245-
y0 = MathHelper.wrapDegrees(y0);
246-
y1 = MathHelper.wrapDegrees(y1);
247-
y2 = MathHelper.wrapDegrees(y2);
248-
y3 = MathHelper.wrapDegrees(y3);
243+
y0 = MathUtils.wrapDegrees(y0);
244+
y1 = MathUtils.wrapDegrees(y1);
245+
y2 = MathUtils.wrapDegrees(y2);
246+
y3 = MathUtils.wrapDegrees(y3);
249247

250248
y1 = normalizeYaw(y0, y1);
251249
y2 = normalizeYaw(y1, y2);
@@ -346,4 +344,4 @@ public static double envelope(double x, double lowIn, double lowOut, double high
346344

347345
return 1;
348346
}
349-
}
347+
}

src/main/java/com/cleanroommc/modularui/utils/MathUtils.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,44 @@ public static int max(int... values) {
114114
}
115115
return max;
116116
}
117-
}
117+
118+
public static int ceil(float value) {
119+
int i = (int) value;
120+
return value > (float) i ? i + 1 : i;
121+
}
122+
123+
public static int ceil(double value) {
124+
int i = (int) value;
125+
return value > (double) i ? i + 1 : i;
126+
}
127+
128+
/**
129+
* the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
130+
*/
131+
public static float wrapDegrees(float value) {
132+
value = value % 360.0F;
133+
if (value >= 180.0F) value -= 360.0F;
134+
if (value < -180.0F) value += 360.0F;
135+
return value;
136+
}
137+
138+
/**
139+
* the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
140+
*/
141+
public static double wrapDegrees(double value) {
142+
value = value % 360.0D;
143+
if (value >= 180.0D) value -= 360.0D;
144+
if (value < -180.0D) value += 360.0D;
145+
return value;
146+
}
147+
148+
/**
149+
* Adjust the angle so that his value is in range [-180;180[
150+
*/
151+
public static int wrapDegrees(int angle) {
152+
angle = angle % 360;
153+
if (angle >= 180) angle -= 360;
154+
if (angle < -180) angle += 360;
155+
return angle;
156+
}
157+
}

src/main/java/com/cleanroommc/modularui/widget/scroll/ScrollData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.cleanroommc.modularui.api.GuiAxis;
55
import com.cleanroommc.modularui.drawable.GuiDraw;
66
import com.cleanroommc.modularui.utils.Interpolation;
7+
import com.cleanroommc.modularui.utils.MathUtils;
78

8-
import net.minecraft.util.math.MathHelper;
99
import net.minecraftforge.fml.relauncher.Side;
1010
import net.minecraftforge.fml.relauncher.SideOnly;
1111

@@ -17,7 +17,7 @@ public abstract class ScrollData {
1717
* Creates scroll data which handles scrolling and scroll bar. Scrollbar is 4 pixel thick
1818
* and will be at the end of the cross axis (bottom/right).
1919
*
20-
* @param axis axis on which to scroll
20+
* @param axis axis on which to scroll
2121
* @return new scroll data
2222
*/
2323
public static ScrollData of(GuiAxis axis) {
@@ -183,7 +183,7 @@ public boolean clamp(ScrollArea area) {
183183
if (this.scrollSize <= size) {
184184
this.scroll = 0;
185185
} else {
186-
this.scroll = MathHelper.clamp(this.scroll, 0, this.scrollSize - size);
186+
this.scroll = MathUtils.clamp(this.scroll, 0, this.scrollSize - size);
187187
}
188188
return old != this.scroll; // returns true if the area was clamped
189189
}

src/main/java/com/cleanroommc/modularui/widgets/ProgressWidget.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
66
import com.cleanroommc.modularui.theme.WidgetTheme;
77
import com.cleanroommc.modularui.utils.Color;
8+
import com.cleanroommc.modularui.utils.MathUtils;
89
import com.cleanroommc.modularui.value.DoubleValue;
910
import com.cleanroommc.modularui.value.sync.SyncHandler;
1011
import com.cleanroommc.modularui.widget.Widget;
1112

12-
import net.minecraft.util.math.MathHelper;
13-
1413
import java.util.function.DoubleSupplier;
1514

1615
public class ProgressWidget extends Widget<ProgressWidget> {
@@ -106,10 +105,10 @@ public float getProgressUV(float uv) {
106105

107106
private void drawCircular(float progress, WidgetTheme widgetTheme) {
108107
float[] subAreas = {
109-
getProgressUV(MathHelper.clamp(progress / 0.25f, 0, 1)),
110-
getProgressUV(MathHelper.clamp((progress - 0.25f) / 0.25f, 0, 1)),
111-
getProgressUV(MathHelper.clamp((progress - 0.5f) / 0.25f, 0, 1)),
112-
getProgressUV(MathHelper.clamp((progress - 0.75f) / 0.25f, 0, 1))
108+
getProgressUV(MathUtils.clamp(progress / 0.25f, 0, 1)),
109+
getProgressUV(MathUtils.clamp((progress - 0.25f) / 0.25f, 0, 1)),
110+
getProgressUV(MathUtils.clamp((progress - 0.5f) / 0.25f, 0, 1)),
111+
getProgressUV(MathUtils.clamp((progress - 0.75f) / 0.25f, 0, 1))
113112
};
114113
float halfWidth = getArea().width / 2f;
115114
float halfHeight = getArea().height / 2f;

src/main/java/com/cleanroommc/modularui/widgets/SchemaWidget.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import com.cleanroommc.modularui.api.widget.Interactable;
66
import com.cleanroommc.modularui.drawable.GuiTextures;
77
import com.cleanroommc.modularui.screen.ModularScreen;
8+
import com.cleanroommc.modularui.utils.MathUtils;
89
import com.cleanroommc.modularui.utils.VectorUtil;
910
import com.cleanroommc.modularui.utils.fakeworld.ISchema;
1011
import com.cleanroommc.modularui.utils.fakeworld.SchemaRenderer;
1112
import com.cleanroommc.modularui.widget.Widget;
1213

13-
import net.minecraft.util.math.MathHelper;
14-
1514
import org.jetbrains.annotations.NotNull;
1615
import org.jetbrains.annotations.Nullable;
1716
import org.lwjgl.util.vector.Vector3f;
@@ -69,7 +68,7 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
6968
if (mouseButton == 0 && this.enableRotation) {
7069
float moveScale = 0.025f;
7170
yaw = (yaw + dx * moveScale + PI2) % PI2;
72-
pitch = MathHelper.clamp(pitch + dy * moveScale, -PI2 / 4 + 0.001f, PI2 / 4 - 0.001f);
71+
pitch = MathUtils.clamp(pitch + dy * moveScale, -PI2 / 4 + 0.001f, PI2 / 4 - 0.001f);
7372
} else if (mouseButton == 2 && this.enableTranslation) {
7473
// the idea is to construct a vector which points upwards from the camerae pov (y-axis on screen)
7574
// this vector determines the amount of z offset from mouse movement in y

src/main/java/com/cleanroommc/modularui/widgets/SliderWidget.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
1111
import com.cleanroommc.modularui.theme.WidgetTheme;
1212
import com.cleanroommc.modularui.utils.Color;
13+
import com.cleanroommc.modularui.utils.MathUtils;
1314
import com.cleanroommc.modularui.value.DoubleValue;
1415
import com.cleanroommc.modularui.value.sync.SyncHandler;
1516
import com.cleanroommc.modularui.widget.Widget;
1617
import com.cleanroommc.modularui.widget.sizer.Area;
1718
import com.cleanroommc.modularui.widget.sizer.Unit;
1819

19-
import net.minecraft.util.math.MathHelper;
20-
2120
import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
2221
import it.unimi.dsi.fastutil.doubles.DoubleList;
2322
import org.jetbrains.annotations.NotNull;
@@ -170,7 +169,7 @@ public void setValue(double value, boolean setSource) {
170169
value = this.stopper.get(this.stopper.size() - 1);
171170
}
172171
}
173-
value = MathHelper.clamp(value, this.min, this.max);
172+
value = MathUtils.clamp(value, this.min, this.max);
174173
this.cache = value;
175174
this.sliderArea.setPoint(this.axis, valueToPos(value));
176175
if (setSource) {

0 commit comments

Comments
 (0)