Remove preferred size and default button size
Replace alignment data in horizontal and vertical layout with double value Add convenient size(GuiElement, ReadableDimension) method to custom layout
This commit is contained in:
@@ -196,7 +196,7 @@ public abstract class AbstractGuiContainer<T extends AbstractGuiContainer<T>>
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
public ReadableDimension calcMinSize() {
|
||||
return layout.calcMinSize(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import de.johni0702.minecraft.gui.container.GuiContainer;
|
||||
import de.johni0702.minecraft.gui.function.Clickable;
|
||||
import lombok.Getter;
|
||||
import net.minecraft.client.audio.PositionedSoundRecord;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
@@ -46,12 +47,10 @@ public abstract class AbstractGuiButton<T extends AbstractGuiButton<T>> extends
|
||||
private String label;
|
||||
|
||||
public AbstractGuiButton() {
|
||||
setSize(new Dimension(200, 20));
|
||||
}
|
||||
|
||||
public AbstractGuiButton(GuiContainer container) {
|
||||
super(container);
|
||||
setSize(new Dimension(200, 20));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,8 +83,9 @@ public abstract class AbstractGuiButton<T extends AbstractGuiButton<T>> extends
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
return getPreferredSize();
|
||||
public ReadableDimension calcMinSize() {
|
||||
FontRenderer fontRenderer = getMinecraft().fontRendererObj;
|
||||
return new Dimension(fontRenderer.getStringWidth(label), 20);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -74,13 +74,18 @@ public abstract class AbstractGuiCheckbox<T extends AbstractGuiCheckbox<T>>
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
public ReadableDimension calcMinSize() {
|
||||
FontRenderer fontRenderer = getMinecraft().fontRendererObj;
|
||||
int height = fontRenderer.FONT_HEIGHT + 2;
|
||||
int width = height + 2 + fontRenderer.getStringWidth(label);
|
||||
return new Dimension(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMaxSize() {
|
||||
return getMinSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick() {
|
||||
getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.create(BUTTON_SOUND, 1.0F));
|
||||
|
||||
@@ -42,7 +42,7 @@ public abstract class AbstractGuiElement<T extends AbstractGuiElement<T>> implem
|
||||
@Getter
|
||||
private boolean enabled = true;
|
||||
|
||||
private Dimension maxSize, preferredSize;
|
||||
protected Dimension minSize, maxSize;
|
||||
|
||||
public AbstractGuiElement() {
|
||||
}
|
||||
@@ -99,20 +99,19 @@ public abstract class AbstractGuiElement<T extends AbstractGuiElement<T>> implem
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T setMinSize(ReadableDimension minSize) {
|
||||
this.minSize = new Dimension(minSize);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T setMaxSize(ReadableDimension maxSize) {
|
||||
this.maxSize = new Dimension(maxSize);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T setPreferredSize(ReadableDimension preferredSize) {
|
||||
this.preferredSize = new Dimension(preferredSize);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T setSize(ReadableDimension size) {
|
||||
Dimension dimension = new Dimension(size);
|
||||
maxSize = preferredSize = dimension;
|
||||
return getThis();
|
||||
setMinSize(size);
|
||||
return setMaxSize(size);
|
||||
}
|
||||
|
||||
public T setSize(int width, int height) {
|
||||
@@ -120,30 +119,30 @@ public abstract class AbstractGuiElement<T extends AbstractGuiElement<T>> implem
|
||||
}
|
||||
|
||||
public T setWidth(int width) {
|
||||
if (minSize == null) {
|
||||
minSize = new Dimension(width, 0);
|
||||
} else {
|
||||
minSize.setWidth(width);
|
||||
}
|
||||
if (maxSize == null) {
|
||||
maxSize = new Dimension(width, 0);
|
||||
maxSize = new Dimension(width, Integer.MAX_VALUE);
|
||||
} else {
|
||||
maxSize.setWidth(width);
|
||||
}
|
||||
if (preferredSize == null) {
|
||||
preferredSize = new Dimension(width, 0);
|
||||
} else {
|
||||
preferredSize.setWidth(width);
|
||||
}
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public T setHeight(int height) {
|
||||
if (minSize == null) {
|
||||
minSize = new Dimension(0, height);
|
||||
} else {
|
||||
minSize.setHeight(height);
|
||||
}
|
||||
if (maxSize == null) {
|
||||
maxSize = new Dimension(0, height);
|
||||
maxSize = new Dimension(Integer.MAX_VALUE, height);
|
||||
} else {
|
||||
maxSize.setHeight(height);
|
||||
}
|
||||
if (preferredSize == null) {
|
||||
preferredSize = new Dimension(0, height);
|
||||
} else {
|
||||
preferredSize.setHeight(height);
|
||||
}
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@@ -152,12 +151,14 @@ public abstract class AbstractGuiElement<T extends AbstractGuiElement<T>> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMaxSize() {
|
||||
return maxSize == null ? getPreferredSize() : maxSize;
|
||||
public ReadableDimension getMinSize() {
|
||||
return minSize == null ? calcMinSize() : minSize;
|
||||
}
|
||||
|
||||
protected abstract ReadableDimension calcMinSize();
|
||||
|
||||
@Override
|
||||
public ReadableDimension getPreferredSize() {
|
||||
return preferredSize == null ? getMinSize() : preferredSize;
|
||||
public ReadableDimension getMaxSize() {
|
||||
return maxSize == null ? new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE) : maxSize;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import de.johni0702.minecraft.gui.RenderInfo;
|
||||
import de.johni0702.minecraft.gui.container.GuiContainer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.client.renderer.texture.DynamicTexture;
|
||||
import org.lwjgl.util.Dimension;
|
||||
import org.lwjgl.util.ReadableDimension;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -60,8 +61,8 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
return getPreferredSize();
|
||||
public ReadableDimension calcMinSize() {
|
||||
return new Dimension(0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -61,11 +61,16 @@ public abstract class AbstractGuiLabel<T extends AbstractGuiLabel<T>> extends Ab
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
public ReadableDimension calcMinSize() {
|
||||
FontRenderer fontRenderer = getMinecraft().fontRendererObj;
|
||||
return new Dimension(fontRenderer.getStringWidth(text), fontRenderer.FONT_HEIGHT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMaxSize() {
|
||||
return getMinSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T setText(String text) {
|
||||
this.text = text;
|
||||
|
||||
@@ -32,8 +32,10 @@ import de.johni0702.minecraft.gui.function.Tickable;
|
||||
import de.johni0702.minecraft.gui.function.Typeable;
|
||||
import lombok.Getter;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.util.Dimension;
|
||||
import org.lwjgl.util.ReadableColor;
|
||||
import org.lwjgl.util.ReadableDimension;
|
||||
import org.lwjgl.util.ReadablePoint;
|
||||
@@ -84,8 +86,9 @@ public abstract class AbstractGuiTextField<T extends AbstractGuiTextField<T>>
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
return getPreferredSize();
|
||||
public ReadableDimension calcMinSize() {
|
||||
FontRenderer fontRenderer = getMinecraft().fontRendererObj;
|
||||
return new Dimension(0, fontRenderer.FONT_HEIGHT);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -70,7 +70,7 @@ public abstract class AbstractGuiTooltip<T extends AbstractGuiTooltip<T>> extend
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
public ReadableDimension calcMinSize() {
|
||||
FontRenderer fontRenderer = getMinecraft().fontRendererObj;
|
||||
int height = 1 + LINE_SPACING + text.length * (fontRenderer.FONT_HEIGHT + LINE_SPACING);
|
||||
int width = 0;
|
||||
@@ -84,6 +84,11 @@ public abstract class AbstractGuiTooltip<T extends AbstractGuiTooltip<T>> extend
|
||||
return new Dimension(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMaxSize() {
|
||||
return getMinSize();
|
||||
}
|
||||
|
||||
public T setText(String[]text) {
|
||||
this.text = text;
|
||||
return getThis();
|
||||
|
||||
@@ -38,10 +38,8 @@ public interface GuiElement<T extends GuiElement<T>> {
|
||||
void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo);
|
||||
|
||||
ReadableDimension getMinSize();
|
||||
ReadableDimension getPreferredSize();
|
||||
ReadableDimension getMaxSize();
|
||||
|
||||
T setPreferredSize(ReadableDimension preferredSize);
|
||||
T setMaxSize(ReadableDimension maxSize);
|
||||
|
||||
boolean isEnabled();
|
||||
|
||||
@@ -29,6 +29,7 @@ import de.johni0702.minecraft.gui.element.AbstractGuiElement;
|
||||
import lombok.Getter;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import org.lwjgl.util.Dimension;
|
||||
import org.lwjgl.util.ReadableColor;
|
||||
import org.lwjgl.util.ReadableDimension;
|
||||
|
||||
@@ -82,7 +83,7 @@ public abstract class AbstractGuiProgressBar<T extends AbstractGuiElement<T>> ex
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableDimension getMinSize() {
|
||||
return getPreferredSize();
|
||||
public ReadableDimension calcMinSize() {
|
||||
return new Dimension(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public abstract class CustomLayout<T extends GuiContainer<T>> implements Layout
|
||||
result.clear();
|
||||
Collection<GuiElement> elements = container.getChildren();
|
||||
for (GuiElement element : elements) {
|
||||
result.put(element, Pair.of(new Point(0, 0), new Dimension(element.getPreferredSize())));
|
||||
result.put(element, Pair.of(new Point(0, 0), new Dimension(element.getMinSize())));
|
||||
}
|
||||
|
||||
layout((T) container, size.getWidth(), size.getHeight());
|
||||
@@ -65,6 +65,10 @@ public abstract class CustomLayout<T extends GuiContainer<T>> implements Layout
|
||||
entry(element).getLeft().setLocation(x, y);
|
||||
}
|
||||
|
||||
protected void size(GuiElement element, ReadableDimension size) {
|
||||
size.getSize(entry(element).getRight());
|
||||
}
|
||||
|
||||
protected void size(GuiElement element, int width, int height) {
|
||||
entry(element).getRight().setSize(width, height);
|
||||
}
|
||||
@@ -105,6 +109,6 @@ public abstract class CustomLayout<T extends GuiContainer<T>> implements Layout
|
||||
|
||||
@Override
|
||||
public ReadableDimension calcMinSize(GuiContainer<?> container) {
|
||||
return container.getPreferredSize();
|
||||
return new Dimension(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,20 +66,12 @@ public class HorizontalLayout implements Layout {
|
||||
|
||||
GuiElement element = entry.getKey();
|
||||
Data data = entry.getValue() instanceof Data ? (Data) entry.getValue() : DEFAULT_DATA;
|
||||
Dimension minSize = new Dimension(element.getMinSize());
|
||||
if (minSize.getHeight() < data.minHeight) {
|
||||
minSize.setHeight(data.minHeight);
|
||||
}
|
||||
int y;
|
||||
if (data.alignment == VerticalLayout.Alignment.BOTTOM) {
|
||||
y = size.getHeight() - minSize.getHeight() - data.yOffset;
|
||||
} else if (data.alignment == VerticalLayout.Alignment.CENTER) {
|
||||
y = (size.getHeight() - minSize.getHeight()) / 2 + data.yOffset;
|
||||
} else {
|
||||
y = data.yOffset;
|
||||
}
|
||||
map.put(element, Pair.<ReadablePoint, ReadableDimension>of(new Point(x, y), minSize));
|
||||
x += minSize.getWidth();
|
||||
Dimension elementSize = new Dimension(element.getMinSize());
|
||||
elementSize.setHeight(Math.min(size.getHeight(), element.getMaxSize().getHeight()));
|
||||
int remainingHeight = size.getHeight() - elementSize.getHeight();
|
||||
int y = (int) (data.alignment * remainingHeight);
|
||||
map.put(element, Pair.<ReadablePoint, ReadableDimension>of(new Point(x, y), elementSize));
|
||||
x += elementSize.getWidth();
|
||||
}
|
||||
if (alignment != Alignment.LEFT) {
|
||||
int remaining = size.getWidth() - x;
|
||||
@@ -103,9 +95,8 @@ public class HorizontalLayout implements Layout {
|
||||
spacing = this.spacing;
|
||||
|
||||
GuiElement element = entry.getKey();
|
||||
Data data = entry.getValue() instanceof Data ? (Data) entry.getValue() : DEFAULT_DATA;
|
||||
ReadableDimension minSize = element.getMinSize();
|
||||
int height = Math.max(minSize.getHeight(), data.getMinHeight()) + data.yOffset;
|
||||
int height = minSize.getHeight();
|
||||
if (height > maxHeight) {
|
||||
maxHeight = height;
|
||||
}
|
||||
@@ -117,15 +108,10 @@ public class HorizontalLayout implements Layout {
|
||||
@lombok.Data
|
||||
@AllArgsConstructor
|
||||
public static class Data implements LayoutData {
|
||||
private int yOffset;
|
||||
private VerticalLayout.Alignment alignment;
|
||||
private int minHeight;
|
||||
private double alignment;
|
||||
|
||||
public Data(int yOffset) {
|
||||
this(yOffset, VerticalLayout.Alignment.TOP, 0);
|
||||
}
|
||||
public Data(int yOffset, VerticalLayout.Alignment alignment) {
|
||||
this(yOffset, alignment, 0);
|
||||
public Data() {
|
||||
this(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,20 +66,12 @@ public class VerticalLayout implements Layout {
|
||||
|
||||
GuiElement element = entry.getKey();
|
||||
Data data = entry.getValue() instanceof Data ? (Data) entry.getValue() : DEFAULT_DATA;
|
||||
Dimension minSize = new Dimension(element.getMinSize());
|
||||
if (minSize.getWidth() < data.minWidth) {
|
||||
minSize.setWidth(data.minWidth);
|
||||
}
|
||||
int x;
|
||||
if (data.alignment == HorizontalLayout.Alignment.RIGHT) {
|
||||
x = size.getWidth() - minSize.getWidth() - data.xOffset;
|
||||
} else if (data.alignment == HorizontalLayout.Alignment.CENTER) {
|
||||
x = (size.getWidth() - minSize.getWidth()) / 2 + data.xOffset;
|
||||
} else {
|
||||
x = data.xOffset;
|
||||
}
|
||||
map.put(element, Pair.<ReadablePoint, ReadableDimension>of(new Point(x, y), minSize));
|
||||
y += minSize.getHeight();
|
||||
Dimension elementSize = new Dimension(element.getMinSize());
|
||||
elementSize.setWidth(Math.min(size.getWidth(), element.getMaxSize().getWidth()));
|
||||
int remainingWidth = size.getWidth() - elementSize.getWidth();
|
||||
int x = (int) (data.alignment * remainingWidth);
|
||||
map.put(element, Pair.<ReadablePoint, ReadableDimension>of(new Point(x, y), elementSize));
|
||||
y += elementSize.getHeight();
|
||||
}
|
||||
if (alignment != Alignment.TOP) {
|
||||
int remaining = size.getHeight() - y;
|
||||
@@ -103,9 +95,8 @@ public class VerticalLayout implements Layout {
|
||||
spacing = this.spacing;
|
||||
|
||||
GuiElement element = entry.getKey();
|
||||
Data data = entry.getValue() instanceof Data ? (Data) entry.getValue() : DEFAULT_DATA;
|
||||
ReadableDimension minSize = element.getMinSize();
|
||||
int width = Math.max(minSize.getWidth(), data.minWidth) + data.xOffset;
|
||||
int width = minSize.getWidth();
|
||||
if (width > maxWidth) {
|
||||
maxWidth = width;
|
||||
}
|
||||
@@ -117,15 +108,10 @@ public class VerticalLayout implements Layout {
|
||||
@lombok.Data
|
||||
@AllArgsConstructor
|
||||
public static class Data implements LayoutData {
|
||||
private int xOffset;
|
||||
private HorizontalLayout.Alignment alignment;
|
||||
private int minWidth;
|
||||
private double alignment;
|
||||
|
||||
public Data(int xOffset) {
|
||||
this(xOffset, HorizontalLayout.Alignment.LEFT, 0);
|
||||
}
|
||||
public Data(int xOffset, HorizontalLayout.Alignment alignment) {
|
||||
this(xOffset, alignment, 0);
|
||||
public Data() {
|
||||
this(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@ public class GuiLoginPrompt extends AbstractGuiScreen<GuiLoginPrompt> {
|
||||
private GuiLabel passwordLabel = new GuiLabel(this).setI18nText("replaymod.gui.password");
|
||||
private GuiLabel noAccountLabel = new GuiLabel(this).setI18nText("replaymod.gui.login.noacc");
|
||||
private GuiLabel statusLabel = new GuiLabel(this);
|
||||
private GuiButton loginButton = new GuiButton(this).setI18nLabel("replaymod.gui.login").setWidth(150).setEnabled(false);
|
||||
private GuiButton cancelButton = new GuiButton(this).setI18nLabel("replaymod.gui.cancel").setWidth(150);
|
||||
private GuiButton registerButton = new GuiButton(this).setI18nLabel("replaymod.gui.register").setWidth(150);
|
||||
private GuiButton loginButton = new GuiButton(this).setI18nLabel("replaymod.gui.login").setSize(150, 20).setEnabled(false);
|
||||
private GuiButton cancelButton = new GuiButton(this).setI18nLabel("replaymod.gui.cancel").setSize(150, 20);
|
||||
private GuiButton registerButton = new GuiButton(this).setI18nLabel("replaymod.gui.register").setSize(150, 20);
|
||||
private GuiTextField username = new GuiTextField(this).setSize(145, 20).setMaxLength(16).setFocused(true);
|
||||
private GuiPasswordField password = new GuiPasswordField(this).setSize(145, 20).setNext(username).setPrevious(username);
|
||||
|
||||
@@ -95,7 +95,7 @@ public class GuiLoginPrompt extends AbstractGuiScreen<GuiLoginPrompt> {
|
||||
pos(statusLabel, width / 2 - statusLabel.getMinSize().getWidth() / 2, 92);
|
||||
|
||||
int labelWidth = noAccountLabel.getMinSize().getWidth();
|
||||
int buttonWidth = registerButton.getPreferredSize().getWidth();
|
||||
int buttonWidth = registerButton.getMaxSize().getWidth();
|
||||
int lineWidth = labelWidth + 5 + buttonWidth;
|
||||
int lineStart = width / 2 - lineWidth / 2;
|
||||
pos(noAccountLabel, lineStart, height - 22);
|
||||
|
||||
@@ -16,14 +16,14 @@ public class GuiRegister extends AbstractGuiScreen<GuiRegister> {
|
||||
private final GuiPanel inputs;
|
||||
private final GuiTextField usernameInput, mailInput;
|
||||
private final GuiPasswordField passwordInput, passwordConfirmation;
|
||||
private final GuiButton registerButton = new GuiButton(this).setI18nLabel("replaymod.gui.register").setWidth(150).setDisabled();
|
||||
private final GuiButton cancelButton = new GuiButton(this).setI18nLabel("replaymod.gui.cancel").setWidth(150);
|
||||
private final GuiButton registerButton = new GuiButton(this).setI18nLabel("replaymod.gui.register").setSize(150, 20).setDisabled();
|
||||
private final GuiButton cancelButton = new GuiButton(this).setI18nLabel("replaymod.gui.cancel").setSize(150, 20);
|
||||
private final GuiLabel statusLabel = new GuiLabel(this).setColor(ReadableColor.RED);
|
||||
|
||||
{
|
||||
inputs = new GuiPanel(this).setLayout(new VerticalLayout().setSpacing(10));
|
||||
HorizontalLayout.Data data = new HorizontalLayout.Data(0, VerticalLayout.Alignment.CENTER);
|
||||
inputs.addElements(new VerticalLayout.Data(0, HorizontalLayout.Alignment.RIGHT),
|
||||
HorizontalLayout.Data data = new HorizontalLayout.Data(0.5);
|
||||
inputs.addElements(new VerticalLayout.Data(1),
|
||||
new GuiPanel().setLayout(new HorizontalLayout(HorizontalLayout.Alignment.RIGHT).setSpacing(10))
|
||||
.addElements(data, new GuiLabel().setI18nText("replaymod.gui.username"))
|
||||
.addElements(data, usernameInput = new GuiTextField().setMaxLength(16).setSize(145, 20)),
|
||||
|
||||
Reference in New Issue
Block a user