Add new Gui API

This commit is contained in:
johni0702
2015-07-08 12:54:25 +02:00
parent 4ebf6c5b21
commit db8b5a9be0
38 changed files with 2498 additions and 0 deletions

View File

@@ -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<T extends AbstractGuiContainer<T>>
extends AbstractComposedGuiElement<T> implements GuiContainer<T> {
private final Map<GuiElement, LayoutData> elements = new LinkedHashMap<GuiElement, LayoutData>();
private Map<GuiElement, Pair<ReadablePoint, ReadableDimension>> 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<ReadablePoint, ReadableDimension> 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<GuiElement> getChildren() {
return Collections.unmodifiableCollection(elements.keySet());
}
@Override
public Map<GuiElement, LayoutData> 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<GuiElement, Pair<ReadablePoint, ReadableDimension>> 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);
}
}

View File

@@ -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<T extends AbstractGuiScreen<T>> extends AbstractGuiContainer<T> {
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();
}
}
}

View File

@@ -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<T extends GuiContainer<T>> extends ComposedGuiElement<T> {
T setLayout(Layout layout);
Layout getLayout();
void convertFor(GuiElement element, Point point);
Map<GuiElement, LayoutData> getElements();
T addElements(LayoutData layoutData, GuiElement...elements);
}

View File

@@ -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<GuiPanel> {
public GuiPanel() {
}
public GuiPanel(GuiContainer container) {
super(container);
}
@Builder
GuiPanel(Layout layout, int width , int height, @Singular("with") Map<GuiElement, LayoutData> withElements) {
setLayout(layout);
setSize(width, height);
for (Map.Entry<GuiElement, LayoutData> e : withElements.entrySet()) {
addElements(e.getValue(), e.getKey());
}
}
@Override
protected GuiPanel getThis() {
return this;
}
}

View File

@@ -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<GuiScreen> {
@SuppressWarnings("unchecked")
public static GuiScreen from(net.minecraft.client.gui.GuiScreen minecraft) {
return ((GuiScreen.MinecraftGuiScreen) minecraft).getWrapper();
}
@Override
protected GuiScreen getThis() {
return this;
}
}