Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized Sizing.Random, Positioning.Random.random #297

Open
wants to merge 1 commit into
base: 1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions src/main/java/io/wispforest/owo/ui/core/Positioning.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.Locale;
import java.util.Objects;
import java.util.Random;

public class Positioning implements Animatable<Positioning> {

Expand Down Expand Up @@ -94,6 +95,198 @@ public static Positioning layout() {
return LAYOUT_POSITIONING;
}

/**
* A collection of utility methods for generating random positioning instances
*
* @author chyzman
*/
public static class Random {
private static final java.util.Random POSITIONING_RANDOM = new java.util.Random();


/**
* Generate a random absolute positioning
*
* @param minX The minimum x offset
* @param maxX The maximum x offset
* @param minY The minimum y offset
* @param maxY The maximum y offset
*/
public static Positioning absolute(int minX, int maxX, int minY, int maxY) {
return Positioning.absolute(
POSITIONING_RANDOM.nextInt(minX, maxX),
POSITIONING_RANDOM.nextInt(minY, maxY)
);
}

/**
* Generate a random absolute positioning
*
* @param min The minimum offset
* @param max The maximum offset
*/
public static Positioning absolute(int min, int max) {
return Positioning.absolute(
POSITIONING_RANDOM.nextInt(min, max),
POSITIONING_RANDOM.nextInt(min, max)
);
}

/**
* Generate a random absolute positioning
*
* @param max The maximum offset
*/
public static Positioning absolute(int max) {
return Positioning.absolute(
POSITIONING_RANDOM.nextInt(max),
POSITIONING_RANDOM.nextInt(max)
);
}

/**
* Generate a random relative positioning
*
* @param minX The minimum x offset
* @param maxX The maximum x offset
* @param minY The minimum y offset
* @param maxY The maximum y offset
*/
public static Positioning relative(int minX, int maxX, int minY, int maxY) {
return Positioning.relative(
POSITIONING_RANDOM.nextInt(minX, maxX),
POSITIONING_RANDOM.nextInt(minY, maxY)
);
}

/**
* Generate a random relative positioning
*
* @param min The minimum offset
* @param max The maximum offset
*/
public static Positioning relative(int min, int max) {
return Positioning.relative(
POSITIONING_RANDOM.nextInt(min, max),
POSITIONING_RANDOM.nextInt(min, max)
);
}

/**
* Generate a random relative positioning
*
* @param max The maximum offset
*/
public static Positioning relative(int max) {
return Positioning.relative(
POSITIONING_RANDOM.nextInt(max),
POSITIONING_RANDOM.nextInt(max)
);
}

/**
* Generate a random relative positioning
*/
public static Positioning relative() {
return Positioning.relative(
POSITIONING_RANDOM.nextInt(100),
POSITIONING_RANDOM.nextInt(100)
);
}

/**
* Generate a random across positioning
*
* @param minX The minimum x offset
* @param maxX The maximum x offset
* @param minY The minimum y offset
* @param maxY The maximum y offset
*/
public static Positioning across(int minX, int maxX, int minY, int maxY) {
return Positioning.across(
POSITIONING_RANDOM.nextInt(minX, maxX),
POSITIONING_RANDOM.nextInt(minY, maxY)
);
}

/**
* Generate a random across positioning
*
* @param min The minimum offset
* @param max The maximum offset
*/
public static Positioning across(int min, int max) {
return Positioning.across(
POSITIONING_RANDOM.nextInt(min, max),
POSITIONING_RANDOM.nextInt(min, max)
);
}

/**
* Generate a random across positioning
*
* @param max The maximum offset
*/
public static Positioning across(int max) {
return Positioning.across(
POSITIONING_RANDOM.nextInt(max),
POSITIONING_RANDOM.nextInt(max)
);
}

/**
* Generate a random across positioning
*/
public static Positioning across() {
return Positioning.across(
POSITIONING_RANDOM.nextInt(100),
POSITIONING_RANDOM.nextInt(100)
);
}

/**
* Generate a random positioning instance
*
* @param minX The minimum x offset
* @param maxX The maximum x offset
* @param minY The minimum y offset
* @param maxY The maximum y offset
*/
public static Positioning random(int minX, int maxX, int minY, int maxY) {
return switch (POSITIONING_RANDOM.nextInt(2)) {
case 0 -> relative(minX, maxX, minY, maxY);
case 1 -> across(minX, maxX, minY, maxY);
default -> throw new IllegalStateException("Unexpected value: " + POSITIONING_RANDOM.nextInt(2));
};
}

/**
* Generate a random positioning instance
*
* @param min The minimum offset
* @param max The maximum offset
*/
public static Positioning random(int min, int max) {
return random(min, max, min, max);
}

/**
* Generate a random positioning instance
*
* @param max The maximum offset
*/
public static Positioning random(int max) {
return random(0, max);
}

/**
* Generate a random positioning instance
*/
public static Positioning random() {
return random(100);
}
}

public enum Type {
RELATIVE, ACROSS, ABSOLUTE, LAYOUT
}
Expand Down
43 changes: 16 additions & 27 deletions src/main/java/io/wispforest/owo/ui/core/Sizing.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static Sizing fill(int min, int max) {
* @return A random sizing instance
*/
public static Sizing fill(int max) {
return Sizing.fill(SIZING_RANDOM.nextInt(0, max));
return Sizing.fill(SIZING_RANDOM.nextInt(max));
}

/**
Expand All @@ -126,7 +126,7 @@ public static Sizing fill(int max) {
* @return A random sizing instance
*/
public static Sizing fill() {
return Sizing.fill(SIZING_RANDOM.nextInt(0, 100));
return Sizing.fill(SIZING_RANDOM.nextInt(100));
}

/**
Expand All @@ -147,7 +147,7 @@ public static Sizing expand(int min, int max) {
* @return A random sizing instance
*/
public static Sizing expand(int max) {
return Sizing.expand(SIZING_RANDOM.nextInt(0, max));
return Sizing.expand(SIZING_RANDOM.nextInt(max));
}

/**
Expand All @@ -156,7 +156,7 @@ public static Sizing expand(int max) {
* @return A random sizing instance
*/
public static Sizing expand() {
return Sizing.expand(SIZING_RANDOM.nextInt(0, 100));
return Sizing.expand(SIZING_RANDOM.nextInt(100));
}

/**
Expand All @@ -177,16 +177,7 @@ public static Sizing fixed(int min, int max) {
* @return A random sizing instance
*/
public static Sizing fixed(int max) {
return Sizing.fixed(SIZING_RANDOM.nextInt(0, max));
}

/**
* Generate a random fixed sizing instance with a value between 0 and 100
*
* @return A random sizing instance
*/
public static Sizing fixed() {
return Sizing.fixed(SIZING_RANDOM.nextInt(0, 100));
return Sizing.fixed(SIZING_RANDOM.nextInt(max));
}

/**
Expand All @@ -207,7 +198,7 @@ public static Sizing content(int min, int max) {
* @return A random sizing instance
*/
public static Sizing content(int max) {
return Sizing.content(SIZING_RANDOM.nextInt(0, max));
return Sizing.content(SIZING_RANDOM.nextInt(max));
}

/**
Expand All @@ -216,7 +207,7 @@ public static Sizing content(int max) {
* @return A random sizing instance
*/
public static Sizing content() {
return Sizing.content(SIZING_RANDOM.nextInt(0, 100));
return Sizing.content(SIZING_RANDOM.nextInt(100));
}

/**
Expand All @@ -228,12 +219,11 @@ public static Sizing content() {
* @apiNote May crash if put on a component that doesn't support content sizing
*/
public static Sizing random(int min, int max) {
return switch (SIZING_RANDOM.nextInt(0, 4)) {
return switch (SIZING_RANDOM.nextInt(3)) {
case 0 -> fill(min, max);
case 1 -> expand(min, max);
case 2 -> fixed(min, max);
case 3 -> content(min, max);
default -> throw new IllegalStateException("Unexpected value: " + SIZING_RANDOM.nextInt(0, 4));
case 2 -> content(min, max);
default -> throw new IllegalStateException("Unexpected value: " + SIZING_RANDOM.nextInt(3));
};
}

Expand All @@ -245,7 +235,7 @@ public static Sizing random(int min, int max) {
* @apiNote May crash if put on a component that doesn't support content sizing
*/
public static Sizing random(int max) {
return random(0, max);
return random(max);
}

/**
Expand All @@ -255,7 +245,7 @@ public static Sizing random(int max) {
* @apiNote May crash if put on a component that doesn't support content sizing
*/
public static Sizing random() {
return random(0, 100);
return random(100);
}

/**
Expand All @@ -267,11 +257,10 @@ public static Sizing random() {
* @return A random sizing instance
*/
public static Sizing noContent(int min, int max) {
return switch (SIZING_RANDOM.nextInt(0, 3)) {
return switch (SIZING_RANDOM.nextInt(2)) {
case 0 -> fill(min, max);
case 1 -> expand(min, max);
case 2 -> fixed(min, max);
default -> throw new IllegalStateException("Unexpected value: " + SIZING_RANDOM.nextInt(0, 3));
default -> throw new IllegalStateException("Unexpected value: " + SIZING_RANDOM.nextInt(2));
};
}

Expand All @@ -283,7 +272,7 @@ public static Sizing noContent(int min, int max) {
* @return A random sizing instance
*/
public static Sizing noContent(int max) {
return noContent(0, max);
return noContent(max);
}

/**
Expand All @@ -292,7 +281,7 @@ public static Sizing noContent(int max) {
* @return A random sizing instance
*/
public static Sizing noContent() {
return noContent(0, 100);
return noContent(100);
}
}

Expand Down
Loading