diff --git a/src/main/java/de/johni0702/minecraft/gui/GuiRenderer.java b/src/main/java/de/johni0702/minecraft/gui/GuiRenderer.java new file mode 100644 index 00000000..af77bbc2 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/GuiRenderer.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui; + +import net.minecraft.util.ResourceLocation; +import org.lwjgl.util.ReadableColor; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +public interface GuiRenderer { + + ReadablePoint getOpenGlOffset(); + + ReadableDimension getSize(); + + void bindTexture(ResourceLocation location); + + void drawTexturedRect(int x, int y, int u, int v, int width, int height); + + int drawString(int x, int y, int color, String text); + + int drawString(int x, int y, ReadableColor color, String text); + + int drawCenteredString(int x, int y, int color, String text); + + int drawCenteredString(int x, int y, ReadableColor color, String text); + +} diff --git a/src/main/java/de/johni0702/minecraft/gui/MinecraftGuiRenderer.java b/src/main/java/de/johni0702/minecraft/gui/MinecraftGuiRenderer.java new file mode 100644 index 00000000..73399366 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/MinecraftGuiRenderer.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui; + +import lombok.NonNull; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.Gui; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.util.*; + +public class MinecraftGuiRenderer implements GuiRenderer { + + private final Gui gui = new Gui(); + + @NonNull + private final ReadableDimension size; + + public MinecraftGuiRenderer(ReadableDimension size) { + this.size = size; + } + + @Override + public ReadablePoint getOpenGlOffset() { + return new Point(0, 0); + } + + @Override + public ReadableDimension getSize() { + return size; + } + + @Override + public void bindTexture(ResourceLocation location) { + Minecraft.getMinecraft().getTextureManager().bindTexture(location); + } + + @Override + public void drawTexturedRect(int x, int y, int u, int v, int width, int height) { + gui.drawTexturedModalRect(x, y, u, v, width, height); + } + + @Override + public int drawString(int x, int y, int color, String text) { + FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj; + return fontRenderer.drawStringWithShadow(text, x, y, color); + } + + @Override + public int drawString(int x, int y, ReadableColor color, String text) { + return drawString(x, y, color(color), text); + } + + @Override + public int drawCenteredString(int x, int y, int color, String text) { + FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj; + return fontRenderer.drawStringWithShadow(text, x - fontRenderer.getStringWidth(text) / 2, y, color); + } + + @Override + public int drawCenteredString(int x, int y, ReadableColor color, String text) { + return drawCenteredString(x, y, color(color), text); + } + + private int color(ReadableColor color) { + return color.getAlpha() << 24 + | color.getRed() << 16 + | color.getGreen() << 8 + | color.getBlue(); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/OffsetGuiRenderer.java b/src/main/java/de/johni0702/minecraft/gui/OffsetGuiRenderer.java new file mode 100644 index 00000000..ffda23fe --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/OffsetGuiRenderer.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui; + +import lombok.NonNull; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableColor; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +public class OffsetGuiRenderer implements GuiRenderer { + + @NonNull + private final GuiRenderer renderer; + + @NonNull + private final ReadablePoint position; + + @NonNull + private final ReadableDimension size; + + public OffsetGuiRenderer(GuiRenderer renderer, ReadablePoint position, ReadableDimension size) { + this.renderer = renderer; + this.position = position; + this.size = size; + if (size.getHeight() > renderer.getSize().getHeight() + || size.getWidth() > renderer.getSize().getWidth()) { + throw new IllegalArgumentException("Size must not be larger than size of renderer."); + } + } + + @Override + public ReadablePoint getOpenGlOffset() { + ReadablePoint parentOffset = renderer.getOpenGlOffset(); + return new Point(parentOffset.getX() + position.getX(), parentOffset.getY() + position.getY()); + } + + @Override + public ReadableDimension getSize() { + return size; + } + + @Override + public void bindTexture(ResourceLocation location) { + renderer.bindTexture(location); + } + + @Override + public void drawTexturedRect(int x, int y, int u, int v, int width, int height) { + renderer.drawTexturedRect(x + position.getX(), y + position.getY(), u, v, width, height); + } + + @Override + public int drawString(int x, int y, int color, String text) { + return renderer.drawString(x + position.getX(), y + position.getY(), color, text) - position.getX(); + } + + @Override + public int drawString(int x, int y, ReadableColor color, String text) { + return renderer.drawString(x + position.getX(), y + position.getY(), color, text) - position.getX(); + } + + @Override + public int drawCenteredString(int x, int y, int color, String text) { + return renderer.drawCenteredString(x + position.getX(), y + position.getY(), color, text) - position.getX(); + } + + @Override + public int drawCenteredString(int x, int y, ReadableColor color, String text) { + return renderer.drawCenteredString(x + position.getX(), y + position.getY(), color, text) - position.getX(); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/RenderInfo.java b/src/main/java/de/johni0702/minecraft/gui/RenderInfo.java new file mode 100644 index 00000000..8019685f --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/RenderInfo.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui; + +import lombok.Data; +import net.minecraft.crash.CrashReport; +import net.minecraft.crash.CrashReportCategory; + +@Data +public class RenderInfo { + public final float partialTick; + public final int mouseX; + public final int mouseY; + + public RenderInfo offsetMouse(int minusX, int minusY) { + return new RenderInfo(partialTick, mouseX - minusX, mouseY - minusY); + } + + public void addTo(CrashReport crashReport) { + CrashReportCategory category = crashReport.makeCategory("Render info details"); + category.addCrashSection("Partial Tick", partialTick); + category.addCrashSection("Mouse X", mouseX); + category.addCrashSection("Mouse Y", mouseY); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/container/AbstractGuiContainer.java b/src/main/java/de/johni0702/minecraft/gui/container/AbstractGuiContainer.java new file mode 100644 index 00000000..834d238b --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/container/AbstractGuiContainer.java @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.container; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.OffsetGuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.element.AbstractComposedGuiElement; +import de.johni0702.minecraft.gui.element.GuiElement; +import de.johni0702.minecraft.gui.layout.Layout; +import de.johni0702.minecraft.gui.layout.LayoutData; +import net.minecraft.crash.CrashReport; +import net.minecraft.crash.CrashReportCategory; +import net.minecraft.util.ReportedException; +import org.apache.commons.lang3.tuple.Pair; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +import java.util.*; +import java.util.concurrent.Callable; + +import static com.google.common.base.Preconditions.checkState; + +public abstract class AbstractGuiContainer> + extends AbstractComposedGuiElement implements GuiContainer { + + private final Map elements = new LinkedHashMap(); + + private Map> layedOutElements; + + private Layout layout; + + public AbstractGuiContainer() { + } + + public AbstractGuiContainer(GuiContainer container) { + super(container); + } + + @Override + public T setLayout(Layout layout) { + this.layout = layout; + return getThis(); + } + + @Override + public Layout getLayout() { + return layout; + } + + @Override + public void convertFor(GuiElement element, Point point) { + checkState(layedOutElements != null, "Cannot convert position unless rendered at least once."); + Pair pair = layedOutElements.get(element); + checkState(pair != null, "Element " + element + " not part of " + this); + ReadablePoint pos = pair.getKey(); + point.translate(-pos.getX(), -pos.getY()); + if (getContainer() != null) { + getContainer().convertFor(element, point); + } + } + + @Override + public Collection getChildren() { + return Collections.unmodifiableCollection(elements.keySet()); + } + + @Override + public Map getElements() { + return Collections.unmodifiableMap(elements); + } + + @Override + public T addElements(LayoutData layoutData, GuiElement... elements) { + if (layoutData == null) { + layoutData = LayoutData.NONE; + } + for (GuiElement element : elements) { + this.elements.put(element, layoutData); + element.setContainer(this); + } + return getThis(); + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + try { + layedOutElements = layout.layOut(this, size); + } catch (Exception ex) { + CrashReport crashReport = CrashReport.makeCrashReport(ex, "Gui Layout"); + renderInfo.addTo(crashReport); + CrashReportCategory category = crashReport.makeCategory("Gui container details"); + category.addCrashSectionCallable("Container", new Callable() { + @Override + public Object call() throws Exception { + return this; + } + }); + category.addCrashSectionCallable("Layout", new Callable() { + @Override + public Object call() throws Exception { + return layout; + } + }); + throw new ReportedException(crashReport); + } + for (final Map.Entry> e : layedOutElements.entrySet()) { + final ReadablePoint ePosition = e.getValue().getLeft(); + final ReadableDimension eSize = e.getValue().getRight(); + try { + OffsetGuiRenderer eRenderer = new OffsetGuiRenderer(renderer, ePosition, eSize); + e.getKey().draw(eRenderer, eSize, renderInfo.offsetMouse(ePosition.getX(), ePosition.getY())); + } catch (Exception ex) { + CrashReport crashReport = CrashReport.makeCrashReport(ex, "Rendering Gui"); + renderInfo.addTo(crashReport); + CrashReportCategory category = crashReport.makeCategory("Gui container details"); + category.addCrashSectionCallable("Container", new Callable() { + @Override + public Object call() throws Exception { + return this; + } + }); + category.addCrashSection("Width", size.getWidth()); + category.addCrashSection("Height", size.getHeight()); + category = crashReport.makeCategory("Gui element details"); + category.addCrashSectionCallable("Element", new Callable() { + @Override + public Object call() throws Exception { + return e.getKey(); + } + }); + category.addCrashSectionCallable("Position", new Callable() { + @Override + public Object call() throws Exception { + return ePosition; + } + }); + category.addCrashSectionCallable("Size", new Callable() { + @Override + public Object call() throws Exception { + return eSize; + } + }); + throw new ReportedException(crashReport); + } + } + } + + @Override + public ReadableDimension getMinSize() { + return layout.calcMinSize(this); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/container/AbstractGuiScreen.java b/src/main/java/de/johni0702/minecraft/gui/container/AbstractGuiScreen.java new file mode 100644 index 00000000..e1d3ebe3 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/container/AbstractGuiScreen.java @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.container; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.MinecraftGuiRenderer; +import de.johni0702.minecraft.gui.OffsetGuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.element.GuiLabel; +import de.johni0702.minecraft.gui.function.*; +import eu.crushedpixel.replaymod.utils.MouseUtils; +import lombok.Getter; +import lombok.Setter; +import org.lwjgl.input.Keyboard; +import org.lwjgl.util.Dimension; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableDimension; + +import java.io.IOException; + +public abstract class AbstractGuiScreen> extends AbstractGuiContainer { + + private final MinecraftGuiScreen wrapped = new MinecraftGuiScreen(); + + private Dimension screenSize; + + @Getter + @Setter + private boolean drawBackground = true; + + @Getter + private boolean enabledRepeatedKeyEvents = true; + + @Getter + @Setter + private GuiLabel title; + + public net.minecraft.client.gui.GuiScreen toMinecraft() { + return wrapped; + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + if (drawBackground) { + wrapped.drawDefaultBackground(); + } + if (title != null) { + int x = screenSize.getWidth() / 2 - title.getMinSize().getWidth() / 2; + OffsetGuiRenderer eRenderer = new OffsetGuiRenderer(renderer, new Point(x, 10), new Dimension(0, 0)); + title.draw(eRenderer, null, null); + } + super.draw(renderer, size, renderInfo); + } + + @Override + public ReadableDimension getMinSize() { + return screenSize; + } + + @Override + public ReadableDimension getPreferredSize() { + return screenSize; + } + + @Override + public ReadableDimension getMaxSize() { + return screenSize; + } + + public void setEnabledRepeatedKeyEvents(boolean enableRepeatKeyEvents) { + this.enabledRepeatedKeyEvents = enableRepeatKeyEvents; + if (wrapped.active) { + Keyboard.enableRepeatEvents(enableRepeatKeyEvents); + } + } + + public void display() { + getMinecraft().displayGuiScreen(toMinecraft()); + } + + protected class MinecraftGuiScreen extends net.minecraft.client.gui.GuiScreen { + private MinecraftGuiRenderer renderer; + private boolean active; + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + draw(renderer, screenSize, new RenderInfo(partialTicks, mouseX, mouseY)); + } + + @Override + protected void keyTyped(char typedChar, int keyCode) throws IOException { + forEach(Typeable.class).typeKey(MouseUtils.getMousePos(), keyCode, typedChar, isCtrlKeyDown(), isShiftKeyDown()); + super.keyTyped(typedChar, keyCode); + } + + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { + forEach(Clickable.class).mouseClick(new Point(mouseX, mouseY), mouseButton); + } + + @Override + protected void mouseReleased(int mouseX, int mouseY, int mouseButton) { + forEach(Draggable.class).mouseClick(new Point(mouseX, mouseY), mouseButton); + } + + @Override + protected void mouseClickMove(int mouseX, int mouseY, int mouseButton, long timeSinceLastClick) { + forEach(Draggable.class).mouseDrag(new Point(mouseX, mouseY), mouseButton, timeSinceLastClick); + } + + @Override + public void updateScreen() { + forEach(Tickable.class).tick(); + } + + @Override + public void onGuiClosed() { + forEach(Closeable.class).close(); + active = false; + if (enabledRepeatedKeyEvents) { + Keyboard.enableRepeatEvents(false); + } + } + + @Override + public void initGui() { + active = false; + if (enabledRepeatedKeyEvents) { + Keyboard.enableRepeatEvents(true); + } + screenSize = new Dimension(width, height); + renderer = new MinecraftGuiRenderer(screenSize); + } + + public T getWrapper() { + return AbstractGuiScreen.this.getThis(); + } + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/container/GuiContainer.java b/src/main/java/de/johni0702/minecraft/gui/container/GuiContainer.java new file mode 100644 index 00000000..dca68f8f --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/container/GuiContainer.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.container; + +import de.johni0702.minecraft.gui.element.ComposedGuiElement; +import de.johni0702.minecraft.gui.element.GuiElement; +import de.johni0702.minecraft.gui.layout.Layout; +import de.johni0702.minecraft.gui.layout.LayoutData; +import org.lwjgl.util.Point; + +import java.util.Map; + +public interface GuiContainer> extends ComposedGuiElement { + + T setLayout(Layout layout); + Layout getLayout(); + + void convertFor(GuiElement element, Point point); + + Map getElements(); + T addElements(LayoutData layoutData, GuiElement...elements); + +} diff --git a/src/main/java/de/johni0702/minecraft/gui/container/GuiPanel.java b/src/main/java/de/johni0702/minecraft/gui/container/GuiPanel.java new file mode 100644 index 00000000..e0089872 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/container/GuiPanel.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.container; + +import de.johni0702.minecraft.gui.element.GuiElement; +import de.johni0702.minecraft.gui.layout.Layout; +import de.johni0702.minecraft.gui.layout.LayoutData; +import lombok.Builder; +import lombok.Singular; + +import java.util.Map; + +public class GuiPanel extends AbstractGuiContainer { + + public GuiPanel() { + } + + public GuiPanel(GuiContainer container) { + super(container); + } + + @Builder + GuiPanel(Layout layout, int width , int height, @Singular("with") Map withElements) { + setLayout(layout); + setSize(width, height); + for (Map.Entry e : withElements.entrySet()) { + addElements(e.getValue(), e.getKey()); + } + } + + @Override + protected GuiPanel getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/container/GuiScreen.java b/src/main/java/de/johni0702/minecraft/gui/container/GuiScreen.java new file mode 100644 index 00000000..fc25c8c8 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/container/GuiScreen.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.container; + +public class GuiScreen extends AbstractGuiScreen { + @SuppressWarnings("unchecked") + public static GuiScreen from(net.minecraft.client.gui.GuiScreen minecraft) { + return ((GuiScreen.MinecraftGuiScreen) minecraft).getWrapper(); + } + + @Override + protected GuiScreen getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractComposedGuiElement.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractComposedGuiElement.java new file mode 100644 index 00000000..c12b2ded --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractComposedGuiElement.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; +import net.minecraft.crash.CrashReport; +import net.minecraft.crash.CrashReportCategory; +import net.minecraft.util.ReportedException; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.concurrent.Callable; + +public abstract class AbstractComposedGuiElement> + extends AbstractGuiElement implements ComposedGuiElement { + public AbstractComposedGuiElement() { + } + + public AbstractComposedGuiElement(GuiContainer container) { + super(container); + } + + @Override + @SuppressWarnings("unchecked") + public C forEach(final Class ofType) { + return (C) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ofType}, new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Object handled = null; + for (final GuiElement element : getChildren()) { + try { + if (element instanceof ComposedGuiElement) { + handled = method.invoke(((ComposedGuiElement) element).forEach(ofType), args); + } else if (ofType.isInstance(element)) { + handled = method.invoke(element, args); + } + if (Boolean.TRUE.equals(handled)) { + break; + } + } catch (Exception e) { + CrashReport crash = CrashReport.makeCrashReport(e, "Calling Gui method"); + CrashReportCategory category = crash.makeCategory("Gui"); + category.addCrashSection("Method", method); + category.addCrashSectionCallable("ComposedElement", new Callable() { + @Override + public Object call() throws Exception { + return element; + } + }); + category.addCrashSectionCallable("Element", new Callable() { + @Override + public Object call() throws Exception { + return element; + } + }); + throw new ReportedException(crash); + } + } + return handled; + } + }); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiButton.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiButton.java new file mode 100644 index 00000000..b4098996 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiButton.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +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.renderer.GlStateManager; +import net.minecraft.client.resources.I18n; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.util.Dimension; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableDimension; + +import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA; +import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA; + +public abstract class AbstractGuiButton> extends AbstractGuiClickable implements Clickable, IGuiButton { + protected static final ResourceLocation BUTTON_SOUND = new ResourceLocation("gui.button.press"); + protected static final ResourceLocation WIDGETS_TEXTURE = new ResourceLocation("textures/gui/widgets.png"); + + @Getter + private String label; + + public AbstractGuiButton() { + setSize(new Dimension(200, 20)); + } + + public AbstractGuiButton(GuiContainer container) { + super(container); + setSize(new Dimension(200, 20)); + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + super.draw(renderer, size, renderInfo); + + renderer.bindTexture(WIDGETS_TEXTURE); + GlStateManager.color(1, 1, 1, 1); + + byte texture = 1; + int color = 0xe0e0e0; + if (!isEnabled()) { + texture = 0; + color = 0xa0a0a0; + } else if (isMouseHovering(new Point(renderInfo.mouseX, renderInfo.mouseY))) { + texture = 2; + color = 0xffffa0; + } + + GlStateManager.enableBlend(); + GlStateManager.tryBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1, 0); + GlStateManager.blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + int textureY = 46 + texture * 20; + int halfWidth = size.getWidth() / 2; + + renderer.drawTexturedRect(0, 0, 0, textureY, halfWidth, size.getHeight()); + renderer.drawTexturedRect(halfWidth, 0, 200 - halfWidth, textureY, halfWidth, size.getHeight()); + renderer.drawCenteredString(halfWidth, (size.getHeight() - 8) / 2, color, label); + } + + @Override + public ReadableDimension getMinSize() { + return getPreferredSize(); + } + + @Override + public void onClick() { + getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.create(BUTTON_SOUND, 1.0F)); + super.onClick(); + } + + @Override + public T setLabel(String label) { + this.label = label; + return getThis(); + } + + @Override + public T setI18nLabel(String label, Object... args) { + return setLabel(I18n.format(label, args)); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiClickable.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiClickable.java new file mode 100644 index 00000000..92b8e09d --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiClickable.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.container.GuiContainer; +import de.johni0702.minecraft.gui.function.Clickable; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +public abstract class AbstractGuiClickable> extends AbstractGuiElement implements Clickable, IGuiClickable { + private Runnable onClick; + private ReadableDimension size; + + public AbstractGuiClickable() { + } + + public AbstractGuiClickable(GuiContainer container) { + super(container); + } + + @Override + public void mouseClick(ReadablePoint position, int button) { + Point pos = new Point(position); + if (getContainer() != null) { + getContainer().convertFor(this, pos); + } + + if (isMouseHovering(pos) && isEnabled()) { + onClick(); + } + } + + protected boolean isMouseHovering(ReadablePoint pos) { + return pos.getX() > 0 && pos.getY() > 0 + && pos.getX() < size.getWidth() && pos.getY() < size.getHeight(); + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + this.size = size; + } + + protected void onClick() { + if (onClick != null) { + onClick.run(); + } + } + + @Override + public T onClick(Runnable onClick) { + this.onClick = onClick; + return getThis(); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiElement.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiElement.java new file mode 100644 index 00000000..0861b60c --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiElement.java @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; +import lombok.Getter; +import net.minecraft.client.Minecraft; +import org.lwjgl.util.Dimension; +import org.lwjgl.util.ReadableDimension; + +public abstract class AbstractGuiElement> implements GuiElement { + @Getter + private final Minecraft minecraft = Minecraft.getMinecraft(); + + @Getter + private GuiContainer container; + + @Getter + private boolean enabled = true; + + private Dimension maxSize, preferredSize; + + public AbstractGuiElement() { + } + + public AbstractGuiElement(GuiContainer container) { + container.addElements(null, this); + } + + protected abstract T getThis(); + + @Override + public T setEnabled(boolean enabled) { + this.enabled = enabled; + return getThis(); + } + + @Override + public T setEnabled() { + return setEnabled(true); + } + + @Override + public T setDisabled() { + return setEnabled(false); + } + + @Override + public T setContainer(GuiContainer container) { + this.container = container; + 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(); + } + + public T setSize(int width, int height) { + return setSize(new Dimension(width, height)); + } + + public T setWidth(int width) { + if (maxSize == null) { + maxSize = new Dimension(width, 0); + } else { + maxSize.setWidth(width); + } + if (preferredSize == null) { + preferredSize = new Dimension(width, 0); + } else { + preferredSize.setWidth(width); + } + return getThis(); + } + + public T setHeight(int height) { + if (maxSize == null) { + maxSize = new Dimension(0, height); + } else { + maxSize.setHeight(height); + } + if (preferredSize == null) { + preferredSize = new Dimension(0, height); + } else { + preferredSize.setHeight(height); + } + return getThis(); + } + + @Override + public ReadableDimension getMaxSize() { + return maxSize == null ? getPreferredSize() : maxSize; + } + + @Override + public ReadableDimension getPreferredSize() { + return preferredSize == null ? getMinSize() : preferredSize; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiLabel.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiLabel.java new file mode 100644 index 00000000..45c4d387 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiLabel.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.container.GuiContainer; +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; + +public abstract class AbstractGuiLabel> extends AbstractGuiElement implements IGuiLabel { + @Getter + private String text = ""; + + @Getter + private ReadableColor color = ReadableColor.WHITE, disabledColor = ReadableColor.GREY; + + public AbstractGuiLabel() { + } + + public AbstractGuiLabel(GuiContainer container) { + super(container); + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + renderer.drawString(0, 0, isEnabled() ? color : disabledColor, text); + } + + @Override + public ReadableDimension getMinSize() { + FontRenderer fontRenderer = getMinecraft().fontRendererObj; + return new Dimension(fontRenderer.getStringWidth(text), fontRenderer.FONT_HEIGHT); + } + + @Override + public T setText(String text) { + this.text = text; + return getThis(); + } + + @Override + public T setI18nText(String text, Object... args) { + return setText(I18n.format(text, args)); + } + + @Override + public T setColor(ReadableColor color) { + this.color = color; + return getThis(); + } + + @Override + public T setDisabledColor(ReadableColor disabledColor) { + this.disabledColor = disabledColor; + return getThis(); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiPasswordField.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiPasswordField.java new file mode 100644 index 00000000..510eda5c --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiPasswordField.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.container.GuiContainer; +import org.apache.commons.lang3.StringUtils; +import org.lwjgl.util.ReadableDimension; + +public abstract class AbstractGuiPasswordField> extends AbstractGuiTextField { + public AbstractGuiPasswordField() { + } + + public AbstractGuiPasswordField(GuiContainer container) { + super(container); + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + String text = getText(); + setText(StringUtils.repeat('*', text.length())); + super.draw(renderer, size, renderInfo); + setText(text); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiTextField.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiTextField.java new file mode 100644 index 00000000..42fc05ff --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiTextField.java @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.container.GuiContainer; +import de.johni0702.minecraft.gui.function.Clickable; +import de.johni0702.minecraft.gui.function.Focusable; +import de.johni0702.minecraft.gui.function.Tickable; +import de.johni0702.minecraft.gui.function.Typeable; +import net.minecraft.client.Minecraft; +import org.lwjgl.input.Keyboard; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +public abstract class AbstractGuiTextField> + extends AbstractGuiElement implements Clickable, Tickable, Typeable, IGuiTextField { + private final net.minecraft.client.gui.GuiTextField wrapped; + + private Runnable textChanged; + private Runnable onEnter; + + private Focusable next, previous; + + public AbstractGuiTextField() { + this.wrapped = new net.minecraft.client.gui.GuiTextField(0, Minecraft.getMinecraft().fontRendererObj, 0, 0, 0, 0); + } + + public AbstractGuiTextField(GuiContainer container) { + super(container); + this.wrapped = new net.minecraft.client.gui.GuiTextField(0, Minecraft.getMinecraft().fontRendererObj, 0, 0, 0, 0); + } + + @Override + public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { + ReadablePoint position = renderer.getOpenGlOffset(); + wrapped.xPosition = position.getX(); + wrapped.yPosition = position.getY(); + wrapped.width = size.getWidth(); + wrapped.height = size.getHeight(); + wrapped.drawTextBox(); + } + + @Override + public ReadableDimension getMinSize() { + return getPreferredSize(); + } + + @Override + public void mouseClick(ReadablePoint position, int button) { + wrapped.mouseClicked(position.getX(), position.getY(), button); + } + + @Override + public boolean typeKey(ReadablePoint mousePosition, int keyCode, char keyChar, boolean ctrlDown, boolean shiftDown) { + if (!isFocused()) { + return false; + } + + if (keyCode == Keyboard.KEY_RETURN) { + onEnter(); + return true; + } + + if (keyCode == Keyboard.KEY_TAB) { + Focusable other = shiftDown ? previous : next; + if (other != null) { + setFocused(false); + other.setFocused(true); + } + return true; + } + + String before = wrapped.getText(); + wrapped.textboxKeyTyped(keyChar, keyCode); + String after = wrapped.getText(); + if (!before.equals(after)) { + textChanged.run(); + } + return true; + } + + @Override + public T setText(String text) { + if (text.length() > wrapped.getMaxStringLength()) { + wrapped.text = text.substring(0, wrapped.getMaxStringLength()); + } else { + wrapped.text = text; + } + return getThis(); + } + + @Override + public String getText() { + return wrapped.text; + } + + @Override + public boolean isFocused() { + return wrapped.isFocused(); + } + + @Override + public T setFocused(boolean focused) { + wrapped.setFocused(focused); + return getThis(); + } + + @Override + public Focusable getNext() { + return next; + } + + @Override + public T setNext(Focusable next) { + if (this.next == next) return getThis(); + if (this.next != null) { + this.next.setPrevious(null); + } + this.next = next; + if (this.next != null) { + this.next.setPrevious(this); + } + return getThis(); + } + + @Override + public Focusable getPrevious() { + return previous; + } + + @Override + public T setPrevious(Focusable previous) { + if (this.previous == previous) return getThis(); + if (this.previous != null) { + this.previous.setNext(null); + } + this.previous = previous; + if (this.previous != null) { + this.previous.setNext(this); + } + return getThis(); + } + + @Override + public int getMaxLength() { + return wrapped.getMaxStringLength(); + } + + @Override + public T setMaxLength(int maxLength) { + wrapped.setMaxStringLength(maxLength); + return getThis(); + } + + @Override + public void tick() { + wrapped.updateCursorCounter(); + } + + protected void onEnter() { + if (onEnter != null) { + onEnter.run(); + } + } + + @Override + public T onEnter(Runnable onEnter) { + this.onEnter = onEnter; + return getThis(); + } + + @Override + public T onTextChanged(Runnable textChanged) { + this.textChanged = textChanged; + return getThis(); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/ComposedGuiElement.java b/src/main/java/de/johni0702/minecraft/gui/element/ComposedGuiElement.java new file mode 100644 index 00000000..fddcf9d8 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/ComposedGuiElement.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import java.util.Collection; + +public interface ComposedGuiElement> extends GuiElement { + Collection getChildren(); + + C forEach(Class ofType); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiButton.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiButton.java new file mode 100644 index 00000000..50dc69b4 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiButton.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; + +public class GuiButton extends AbstractGuiButton { + public GuiButton() { + } + + public GuiButton(GuiContainer container) { + super(container); + } + + @Override + protected GuiButton getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiElement.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiElement.java new file mode 100644 index 00000000..a9fdb9a5 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiElement.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.GuiRenderer; +import de.johni0702.minecraft.gui.RenderInfo; +import de.johni0702.minecraft.gui.container.GuiContainer; +import net.minecraft.client.Minecraft; +import org.lwjgl.util.ReadableDimension; + +public interface GuiElement> { + + Minecraft getMinecraft(); + + GuiContainer getContainer(); + T setContainer(GuiContainer container); + + void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo); + + ReadableDimension getMinSize(); + ReadableDimension getPreferredSize(); + ReadableDimension getMaxSize(); + + T setPreferredSize(ReadableDimension preferredSize); + T setMaxSize(ReadableDimension maxSize); + + boolean isEnabled(); + T setEnabled(boolean enabled); + T setEnabled(); + T setDisabled(); + +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiLabel.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiLabel.java new file mode 100644 index 00000000..fde3d45a --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiLabel.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; + +public class GuiLabel extends AbstractGuiLabel { + public GuiLabel() { + } + + public GuiLabel(GuiContainer container) { + super(container); + } + + @Override + protected GuiLabel getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiPasswordField.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiPasswordField.java new file mode 100644 index 00000000..d7a5c890 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiPasswordField.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; + +public class GuiPasswordField extends AbstractGuiPasswordField { + public GuiPasswordField() { + } + + public GuiPasswordField(GuiContainer container) { + super(container); + } + + @Override + protected GuiPasswordField getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiTextField.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiTextField.java new file mode 100644 index 00000000..f1f9c2b3 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiTextField.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.container.GuiContainer; + +public class GuiTextField extends AbstractGuiTextField { + public GuiTextField() { + } + + public GuiTextField(GuiContainer container) { + super(container); + } + + @Override + protected GuiTextField getThis() { + return this; + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/IGuiButton.java b/src/main/java/de/johni0702/minecraft/gui/element/IGuiButton.java new file mode 100644 index 00000000..c225b24a --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/IGuiButton.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +public interface IGuiButton> extends IGuiClickable { + T setLabel(String label); + + T setI18nLabel(String label, Object... args); + + String getLabel(); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/IGuiClickable.java b/src/main/java/de/johni0702/minecraft/gui/element/IGuiClickable.java new file mode 100644 index 00000000..e22586a4 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/IGuiClickable.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +public interface IGuiClickable> extends GuiElement { + T onClick(Runnable onClick); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/IGuiLabel.java b/src/main/java/de/johni0702/minecraft/gui/element/IGuiLabel.java new file mode 100644 index 00000000..fafbc4be --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/IGuiLabel.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import org.lwjgl.util.ReadableColor; + +public interface IGuiLabel> extends GuiElement { + T setText(String text); + + T setI18nText(String text, Object... args); + + T setColor(ReadableColor color); + + T setDisabledColor(ReadableColor disabledColor); + + String getText(); + + ReadableColor getColor(); + + ReadableColor getDisabledColor(); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/element/IGuiTextField.java b/src/main/java/de/johni0702/minecraft/gui/element/IGuiTextField.java new file mode 100644 index 00000000..cbfa6ad1 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/element/IGuiTextField.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.element; + +import de.johni0702.minecraft.gui.function.Focusable; + +public interface IGuiTextField> extends GuiElement, Focusable { + T setText(String text); + String getText(); + + int getMaxLength(); + T setMaxLength(int maxLength); + + T onEnter(Runnable onEnter); + T onTextChanged(Runnable textChanged); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/function/Clickable.java b/src/main/java/de/johni0702/minecraft/gui/function/Clickable.java new file mode 100644 index 00000000..0b8acaf6 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/function/Clickable.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.function; + +import org.lwjgl.util.ReadablePoint; + +public interface Clickable { + void mouseClick(ReadablePoint position, int button); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/function/Closeable.java b/src/main/java/de/johni0702/minecraft/gui/function/Closeable.java new file mode 100644 index 00000000..83cc63da --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/function/Closeable.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.function; + +public interface Closeable { + void close(); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/function/Draggable.java b/src/main/java/de/johni0702/minecraft/gui/function/Draggable.java new file mode 100644 index 00000000..759d7692 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/function/Draggable.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.function; + +import org.lwjgl.util.ReadablePoint; + +public interface Draggable extends Clickable { + void mouseDrag(ReadablePoint position, int button, long timeSinceLastCall); + void mouseRelease(ReadablePoint position, int button); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/function/Focusable.java b/src/main/java/de/johni0702/minecraft/gui/function/Focusable.java new file mode 100644 index 00000000..e4abcd54 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/function/Focusable.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.function; + +public interface Focusable> { + + boolean isFocused(); + T setFocused(boolean focused); + + Focusable getNext(); + T setNext(Focusable next); + + Focusable getPrevious(); + T setPrevious(Focusable previous); + +} diff --git a/src/main/java/de/johni0702/minecraft/gui/function/Tickable.java b/src/main/java/de/johni0702/minecraft/gui/function/Tickable.java new file mode 100644 index 00000000..772cc08c --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/function/Tickable.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.function; + +public interface Tickable { + void tick(); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/function/Typeable.java b/src/main/java/de/johni0702/minecraft/gui/function/Typeable.java new file mode 100644 index 00000000..7d635d92 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/function/Typeable.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.function; + +import org.lwjgl.util.ReadablePoint; + +public interface Typeable { + boolean typeKey(ReadablePoint mousePosition, int keyCode, char keyChar, boolean ctrlDown, boolean shiftDown); +} diff --git a/src/main/java/de/johni0702/minecraft/gui/layout/CustomLayout.java b/src/main/java/de/johni0702/minecraft/gui/layout/CustomLayout.java new file mode 100644 index 00000000..3525dd54 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/layout/CustomLayout.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.layout; + +import com.google.common.collect.Maps; +import de.johni0702.minecraft.gui.container.GuiContainer; +import de.johni0702.minecraft.gui.element.GuiElement; +import org.apache.commons.lang3.tuple.Pair; +import org.lwjgl.util.Dimension; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +import java.util.Collection; +import java.util.Map; + +public abstract class CustomLayout> implements Layout { + private Map> result = Maps.newHashMap(); + + @Override + @SuppressWarnings("unchecked") + public Map> layOut(GuiContainer container, ReadableDimension size) { + result.clear(); + Collection elements = container.getChildren(); + for (GuiElement element : elements) { + result.put(element, Pair.of(new Point(0, 0), new Dimension(element.getPreferredSize()))); + } + + layout((T) container, size.getWidth(), size.getHeight()); + + return (Map) result; + } + + private Pair entry(GuiElement element) { + return result.get(element); + } + + protected void set(GuiElement element, int x, int y, int width, int height) { + Pair entry = entry(element); + entry.getLeft().setLocation(x, y); + entry.getRight().setSize(width, height); + } + + protected void pos(GuiElement element, int x, int y) { + entry(element).getLeft().setLocation(x, y); + } + + protected void size(GuiElement element, int width, int height) { + entry(element).getRight().setSize(width, height); + } + + protected void x(GuiElement element, int x) { + entry(element).getLeft().setX(x); + } + + protected void y(GuiElement element, int y) { + entry(element).getLeft().setY(y); + } + + protected void width(GuiElement element, int width) { + entry(element).getRight().setWidth(width); + } + + protected void height(GuiElement element, int height) { + entry(element).getRight().setHeight(height); + } + + protected int x(GuiElement element) { + return entry(element).getLeft().getX(); + } + + protected int y(GuiElement element) { + return entry(element).getLeft().getY(); + } + + protected int width(GuiElement element) { + return entry(element).getRight().getWidth(); + } + + protected int height(GuiElement element) { + return entry(element).getRight().getHeight(); + } + + protected abstract void layout(T container, int width, int height); + + @Override + public ReadableDimension calcMinSize(GuiContainer container) { + return container.getPreferredSize(); + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/layout/HorizontalLayout.java b/src/main/java/de/johni0702/minecraft/gui/layout/HorizontalLayout.java new file mode 100644 index 00000000..28ae68a9 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/layout/HorizontalLayout.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.layout; + +import com.google.common.collect.Maps; +import de.johni0702.minecraft.gui.container.GuiContainer; +import de.johni0702.minecraft.gui.element.GuiElement; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import lombok.experimental.Accessors; +import org.apache.commons.lang3.tuple.Pair; +import org.lwjgl.util.Dimension; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +import java.util.Map; + +public class HorizontalLayout implements Layout { + private static final Data DEFAULT_DATA = new Data(0); + + private final Alignment alignment; + + @Accessors(chain = true) + @Getter + @Setter + private int spacing; + + public HorizontalLayout() { + this(Alignment.LEFT); + } + + public HorizontalLayout(Alignment alignment) { + this.alignment = alignment; + } + + @Override + public Map> layOut(GuiContainer container, ReadableDimension size) { + int x = 0; + int spacing = 0; + Map> map = Maps.newHashMap(); + for (Map.Entry entry : container.getElements().entrySet()) { + x += spacing; + spacing = this.spacing; + + 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.of(new Point(x, y), minSize)); + x += minSize.getWidth(); + } + if (alignment != Alignment.LEFT) { + int remaining = size.getWidth() - x; + if (alignment == Alignment.CENTER) { + remaining /= 2; + } + for (Pair pair : map.values()) { + ((Point) pair.getLeft()).translate(remaining, 0); + } + } + return map; + } + + @Override + public ReadableDimension calcMinSize(GuiContainer container) { + int maxHeight = 0; + int width = 0; + int spacing = 0; + for (Map.Entry entry : container.getElements().entrySet()) { + width += spacing; + 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; + if (height > maxHeight) { + maxHeight = height; + } + width += minSize.getWidth(); + } + return new Dimension(width, maxHeight); + } + + @lombok.Data + @AllArgsConstructor + public static class Data implements LayoutData { + private int yOffset; + private VerticalLayout.Alignment alignment; + private int minHeight; + + public Data(int yOffset) { + this(yOffset, VerticalLayout.Alignment.TOP, 0); + } + public Data(int yOffset, VerticalLayout.Alignment alignment) { + this(yOffset, alignment, 0); + } + } + + public enum Alignment { + LEFT, RIGHT, CENTER + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/layout/Layout.java b/src/main/java/de/johni0702/minecraft/gui/layout/Layout.java new file mode 100644 index 00000000..199589ba --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/layout/Layout.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.layout; + +import de.johni0702.minecraft.gui.container.GuiContainer; +import de.johni0702.minecraft.gui.element.GuiElement; +import org.apache.commons.lang3.tuple.Pair; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +import java.util.Map; + +public interface Layout { + + Map> layOut(GuiContainer container, ReadableDimension size); + + ReadableDimension calcMinSize(GuiContainer container); + +} diff --git a/src/main/java/de/johni0702/minecraft/gui/layout/LayoutData.java b/src/main/java/de/johni0702/minecraft/gui/layout/LayoutData.java new file mode 100644 index 00000000..de64c7e5 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/layout/LayoutData.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.layout; + +public interface LayoutData { + LayoutData NONE = VoidLayoutData.INSTANCE; +} diff --git a/src/main/java/de/johni0702/minecraft/gui/layout/VerticalLayout.java b/src/main/java/de/johni0702/minecraft/gui/layout/VerticalLayout.java new file mode 100644 index 00000000..cd00c637 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/layout/VerticalLayout.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.layout; + +import com.google.common.collect.Maps; +import de.johni0702.minecraft.gui.container.GuiContainer; +import de.johni0702.minecraft.gui.element.GuiElement; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import lombok.experimental.Accessors; +import org.apache.commons.lang3.tuple.Pair; +import org.lwjgl.util.Dimension; +import org.lwjgl.util.Point; +import org.lwjgl.util.ReadableDimension; +import org.lwjgl.util.ReadablePoint; + +import java.util.Map; + +public class VerticalLayout implements Layout { + private static final Data DEFAULT_DATA = new Data(0); + + private final Alignment alignment; + + @Accessors(chain = true) + @Getter + @Setter + private int spacing; + + public VerticalLayout() { + this(Alignment.TOP); + } + + public VerticalLayout(Alignment alignment) { + this.alignment = alignment; + } + + @Override + public Map> layOut(GuiContainer container, ReadableDimension size) { + int y = 0; + int spacing = 0; + Map> map = Maps.newHashMap(); + for (Map.Entry entry : container.getElements().entrySet()) { + y += spacing; + spacing = this.spacing; + + 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.of(new Point(x, y), minSize)); + y += minSize.getHeight(); + } + if (alignment != Alignment.TOP) { + int remaining = size.getHeight() - y; + if (alignment == Alignment.CENTER) { + remaining /= 2; + } + for (Pair pair : map.values()) { + ((Point) pair.getLeft()).translate(0, remaining); + } + } + return map; + } + + @Override + public ReadableDimension calcMinSize(GuiContainer container) { + int maxWidth = 0; + int height = 0; + int spacing = 0; + for (Map.Entry entry : container.getElements().entrySet()) { + height += spacing; + 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; + if (width > maxWidth) { + maxWidth = width; + } + height += minSize.getHeight(); + } + return new Dimension(maxWidth, height); + } + + @lombok.Data + @AllArgsConstructor + public static class Data implements LayoutData { + private int xOffset; + private HorizontalLayout.Alignment alignment; + private int minWidth; + + public Data(int xOffset) { + this(xOffset, HorizontalLayout.Alignment.LEFT, 0); + } + public Data(int xOffset, HorizontalLayout.Alignment alignment) { + this(xOffset, alignment, 0); + } + } + + public enum Alignment { + TOP, BOTTOM, CENTER + } +} diff --git a/src/main/java/de/johni0702/minecraft/gui/layout/VoidLayoutData.java b/src/main/java/de/johni0702/minecraft/gui/layout/VoidLayoutData.java new file mode 100644 index 00000000..bf87b376 --- /dev/null +++ b/src/main/java/de/johni0702/minecraft/gui/layout/VoidLayoutData.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015 johni0702 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package de.johni0702.minecraft.gui.layout; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class VoidLayoutData implements LayoutData { + public static final VoidLayoutData INSTANCE = new VoidLayoutData(); +}