Created
May 5, 2020 04:21
-
-
Save matshou/1498b1c4440ce0cd78e8a963e9b63db4 to your computer and use it in GitHub Desktop.
Gui helper class to help position elements on the screen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.minecraft.client.MainWindow; | |
public class GuiHelper { | |
public static final int DEFAULT_WINDOW_WIDTH = 427; | |
public static final int DEFAULT_WINDOW_HEIGHT = 240; | |
public static class Coordinates { | |
public final int x; | |
public final int y; | |
public Coordinates(int x, int y) { | |
this.x = x; this.y = y; | |
} | |
} | |
/** | |
* Returns a set of {@code Coordinates} for a {@code GUI} element with the given width | |
* and position on the {@code y} axis in the main window. | |
* | |
* @param width size of the element along the {@code x} axis. | |
* @param y position on the {@code y} axis (from bottom). | |
* @param window instance of Minecraft {@code MainWindow}. | |
*/ | |
public static Coordinates getCenteredPosition(int width, int y, MainWindow window) { | |
int coordX = window.getScaledWidth() / 2 - width / 2; | |
int coordY = window.getScaledHeight() - y; | |
return new Coordinates(coordX, coordY); | |
} | |
/** | |
* Returns a set of {@code Coordinates} for a {@code GUI} element with the given | |
* position in the default window size coordinate grid. This way of resolving | |
* position should work for any set of given coordinates whether you want to | |
* center an element or have a free form position. | |
* | |
* @param x initial position of element along {@code x} axis. | |
* @param y initial position of element along {@code y} axis. | |
* @param window instance of Minecraft {@code MainWindow}. | |
*/ | |
public static Coordinates getScaledPosition(int x, int y, MainWindow window) { | |
int coordX = window.getScaledWidth() / 2 - (DEFAULT_WINDOW_WIDTH - x) + DEFAULT_WINDOW_WIDTH / 2; | |
int coordY = window.getScaledHeight() - (DEFAULT_WINDOW_HEIGHT - y); | |
return new Coordinates(coordX, coordY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment