Search in sources :

Example 11 with GuiElement

use of de.johni0702.minecraft.gui.element.GuiElement in project jGui by ReplayMod.

the class VerticalLayout method layOut.

@Override
public Map<GuiElement, Pair<ReadablePoint, ReadableDimension>> layOut(GuiContainer<?> container, ReadableDimension size) {
    int y = 0;
    int spacing = 0;
    Map<GuiElement, Pair<ReadablePoint, ReadableDimension>> map = new LinkedHashMap<>();
    for (Map.Entry<GuiElement, LayoutData> 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 elementSize = new Dimension(element.getMinSize());
        ReadableDimension elementMaxSize = element.getMaxSize();
        elementSize.setHeight(Math.min(size.getHeight() - y, Math.min(elementSize.getHeight(), elementMaxSize.getHeight())));
        elementSize.setWidth(Math.min(size.getWidth(), (data.maximizeWidth ? elementMaxSize : elementSize).getWidth()));
        int remainingWidth = size.getWidth() - elementSize.getWidth();
        int x = (int) (data.alignment * remainingWidth);
        map.put(element, Pair.<ReadablePoint, ReadableDimension>of(new Point(x, y), elementSize));
        y += elementSize.getHeight();
    }
    if (alignment != Alignment.TOP) {
        int remaining = size.getHeight() - y;
        if (alignment == Alignment.CENTER) {
            remaining /= 2;
        }
        for (Pair<ReadablePoint, ReadableDimension> pair : map.values()) {
            ((Point) pair.getLeft()).translate(0, remaining);
        }
    }
    return map;
}
Also used : Dimension(de.johni0702.minecraft.gui.utils.lwjgl.Dimension) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) LinkedHashMap(java.util.LinkedHashMap) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) GuiElement(de.johni0702.minecraft.gui.element.GuiElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) Pair(org.apache.commons.lang3.tuple.Pair)

Example 12 with GuiElement

use of de.johni0702.minecraft.gui.element.GuiElement in project jGui by ReplayMod.

the class GuiFileChooserPopup method setFolder.

public void setFolder(File folder) {
    if (!folder.isDirectory()) {
        throw new IllegalArgumentException("Folder has to be a directory.");
    }
    try {
        this.folder = folder = folder.getCanonicalFile();
    } catch (IOException e) {
        close();
        throw new RuntimeException(e);
    }
    updateButton();
    for (GuiElement element : new ArrayList<>(pathPanel.getElements().keySet())) {
        pathPanel.removeElement(element);
    }
    for (GuiElement element : new ArrayList<>(fileList.getListPanel().getElements().keySet())) {
        fileList.getListPanel().removeElement(element);
    }
    File[] files = folder.listFiles();
    if (files != null) {
        Arrays.sort(files, new Comparator<File>() {

            @Override
            public int compare(File f1, File f2) {
                if (f1.isDirectory() && !f2.isDirectory()) {
                    return -1;
                } else if (!f1.isDirectory() && f2.isDirectory()) {
                    return 1;
                }
                return f1.getName().compareToIgnoreCase(f2.getName());
            }
        });
        for (final File file : files) {
            if (file.isDirectory()) {
                fileList.getListPanel().addElements(new VerticalLayout.Data(0), new GuiButton().onClick(new Runnable() {

                    @Override
                    public void run() {
                        setFolder(file);
                    }
                }).setLabel(file.getName() + File.separator));
            } else {
                if (hasValidExtension(file.getName())) {
                    fileList.getListPanel().addElements(new VerticalLayout.Data(0), new GuiButton().onClick(new Runnable() {

                        @Override
                        public void run() {
                            setFileName(file.getName());
                        }
                    }).setLabel(file.getName()));
                }
            }
        }
    }
    fileList.setOffsetY(0);
    File[] roots = File.listRoots();
    if (roots != null && roots.length > 1) {
        // Windows can have multiple file system roots
        // So we place a dropdown menu (skinned like a button) at the front of the path
        final GuiDropdownMenu<File> dropdown = new GuiDropdownMenu<File>(pathPanel) {

            private final GuiButton skin = new GuiButton();

            @Override
            protected ReadableDimension calcMinSize() {
                ReadableDimension dim = super.calcMinSize();
                return new Dimension(dim.getWidth() - 5 - MCVer.getFontRenderer().fontHeight, dim.getHeight());
            }

            @Override
            public void layout(ReadableDimension size, RenderInfo renderInfo) {
                super.layout(size, renderInfo);
                if (renderInfo.layer == 0) {
                    skin.layout(size, renderInfo);
                }
            }

            @Override
            public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
                super.draw(renderer, size, renderInfo);
                if (renderInfo.layer == 0) {
                    skin.setLabel(getSelectedValue().toString());
                    skin.draw(renderer, size, renderInfo);
                }
            }
        };
        List<File> actualRoots = new ArrayList<>();
        File selected = null;
        for (File root : roots) {
            // Windows apparently also has file system roots that aren't directories, so we'll have to filter those
            if (root.isDirectory()) {
                actualRoots.add(root);
                if (folder.getAbsolutePath().startsWith(root.getAbsolutePath())) {
                    selected = root;
                }
            }
        }
        assert selected != null;
        // First set values and current selection
        dropdown.setValues(actualRoots.toArray(new File[actualRoots.size()])).setSelected(selected);
        // then add selection handler afterwards
        dropdown.onSelection(new Consumer<Integer>() {

            @Override
            public void consume(Integer old) {
                setFolder(dropdown.getSelectedValue());
            }
        });
    }
    LinkedList<File> parents = new LinkedList<>();
    while (folder != null) {
        parents.addFirst(folder);
        folder = folder.getParentFile();
    }
    for (final File parent : parents) {
        pathPanel.addElements(null, new GuiButton().onClick(new Runnable() {

            @Override
            public void run() {
                setFolder(parent);
            }
        }).setLabel(parent.getName() + File.separator));
    }
    pathScrollable.setOffsetX(Integer.MAX_VALUE);
}
Also used : IOException(java.io.IOException) Dimension(de.johni0702.minecraft.gui.utils.lwjgl.Dimension) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) GuiButton(de.johni0702.minecraft.gui.element.GuiButton) GuiRenderer(de.johni0702.minecraft.gui.GuiRenderer) VerticalLayout(de.johni0702.minecraft.gui.layout.VerticalLayout) GuiDropdownMenu(de.johni0702.minecraft.gui.element.advanced.GuiDropdownMenu) GuiElement(de.johni0702.minecraft.gui.element.GuiElement) File(java.io.File) RenderInfo(de.johni0702.minecraft.gui.RenderInfo)

Example 13 with GuiElement

use of de.johni0702.minecraft.gui.element.GuiElement in project jGui by ReplayMod.

the class AbstractGuiContainer method convertFor.

@Override
public void convertFor(GuiElement element, Point point, int relativeLayer) {
    if (layedOutElements == null || !layedOutElements.containsKey(element)) {
        layout(null, new RenderInfo(0, 0, 0, relativeLayer));
    }
    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();
    if (getContainer() != null) {
        getContainer().convertFor(this, point, relativeLayer + getLayer());
    }
    point.translate(-pos.getX(), -pos.getY());
}
Also used : ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) RenderInfo(de.johni0702.minecraft.gui.RenderInfo) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint)

Example 14 with GuiElement

use of de.johni0702.minecraft.gui.element.GuiElement in project jGui by ReplayMod.

the class AbstractGuiContainer method layout.

@Override
public void layout(ReadableDimension size, RenderInfo renderInfo) {
    super.layout(size, renderInfo);
    if (size == null)
        return;
    try {
        layedOutElements = layout.layOut(this, size);
    } catch (Exception ex) {
        CrashReport crashReport = CrashReport.create(ex, "Gui Layout");
        renderInfo.addTo(crashReport);
        CrashReportSection category = crashReport.addElement("Gui container details");
        MCVer.addDetail(category, "Container", this::toString);
        MCVer.addDetail(category, "Layout", layout::toString);
        throw new CrashException(crashReport);
    }
    for (final Map.Entry<GuiElement, Pair<ReadablePoint, ReadableDimension>> e : layedOutElements.entrySet()) {
        GuiElement element = e.getKey();
        if (element instanceof ComposedGuiElement) {
            if (((ComposedGuiElement) element).getMaxLayer() < renderInfo.layer) {
                continue;
            }
        } else {
            if (element.getLayer() != renderInfo.layer) {
                continue;
            }
        }
        ReadablePoint ePosition = e.getValue().getLeft();
        ReadableDimension eSize = e.getValue().getRight();
        element.layout(eSize, renderInfo.offsetMouse(ePosition.getX(), ePosition.getY()).layer(renderInfo.getLayer() - element.getLayer()));
    }
}
Also used : CrashException(net.minecraft.util.crash.CrashException) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) CrashReport(net.minecraft.util.crash.CrashReport) CrashReportSection(net.minecraft.util.crash.CrashReportSection) AbstractComposedGuiElement(de.johni0702.minecraft.gui.element.AbstractComposedGuiElement) GuiElement(de.johni0702.minecraft.gui.element.GuiElement) ComposedGuiElement(de.johni0702.minecraft.gui.element.ComposedGuiElement) AbstractComposedGuiElement(de.johni0702.minecraft.gui.element.AbstractComposedGuiElement) ComposedGuiElement(de.johni0702.minecraft.gui.element.ComposedGuiElement) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) CrashException(net.minecraft.util.crash.CrashException) Pair(org.apache.commons.lang3.tuple.Pair)

Example 15 with GuiElement

use of de.johni0702.minecraft.gui.element.GuiElement in project jGui by ReplayMod.

the class AbstractGuiOverlay method draw.

@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
    super.draw(renderer, size, renderInfo);
    if (mouseVisible && renderInfo.layer == getMaxLayer()) {
        final GuiElement tooltip = forEach(GuiElement.class, e -> e.getTooltip(renderInfo));
        if (tooltip != null) {
            final ReadableDimension tooltipSize = tooltip.getMinSize();
            int x, y;
            if (renderInfo.mouseX + 8 + tooltipSize.getWidth() < screenSize.getWidth()) {
                x = renderInfo.mouseX + 8;
            } else {
                x = screenSize.getWidth() - tooltipSize.getWidth() - 1;
            }
            if (renderInfo.mouseY + 8 + tooltipSize.getHeight() < screenSize.getHeight()) {
                y = renderInfo.mouseY + 8;
            } else {
                y = screenSize.getHeight() - tooltipSize.getHeight() - 1;
            }
            final ReadablePoint position = new Point(x, y);
            try {
                OffsetGuiRenderer eRenderer = new OffsetGuiRenderer(renderer, position, tooltipSize);
                tooltip.draw(eRenderer, tooltipSize, renderInfo);
            } catch (Exception ex) {
                CrashReport crashReport = CrashReport.create(ex, "Rendering Gui Tooltip");
                renderInfo.addTo(crashReport);
                CrashReportSection category = crashReport.addElement("Gui container details");
                MCVer.addDetail(category, "Container", this::toString);
                MCVer.addDetail(category, "Width", () -> "" + size.getWidth());
                MCVer.addDetail(category, "Height", () -> "" + size.getHeight());
                category = crashReport.addElement("Tooltip details");
                MCVer.addDetail(category, "Element", tooltip::toString);
                MCVer.addDetail(category, "Position", position::toString);
                MCVer.addDetail(category, "Size", tooltipSize::toString);
                throw new CrashException(crashReport);
            }
        }
    }
}
Also used : CrashException(net.minecraft.util.crash.CrashException) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) CrashReport(net.minecraft.util.crash.CrashReport) OffsetGuiRenderer(de.johni0702.minecraft.gui.OffsetGuiRenderer) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) CrashReportSection(net.minecraft.util.crash.CrashReportSection) GuiElement(de.johni0702.minecraft.gui.element.GuiElement) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) CrashException(net.minecraft.util.crash.CrashException)

Aggregations

GuiElement (de.johni0702.minecraft.gui.element.GuiElement)15 ReadableDimension (de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension)14 ReadablePoint (de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint)13 Point (de.johni0702.minecraft.gui.utils.lwjgl.Point)10 Dimension (de.johni0702.minecraft.gui.utils.lwjgl.Dimension)9 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)8 Pair (org.apache.commons.lang3.tuple.Pair)6 CrashException (net.minecraft.util.crash.CrashException)4 CrashReport (net.minecraft.util.crash.CrashReport)4 CrashReportSection (net.minecraft.util.crash.CrashReportSection)4 OffsetGuiRenderer (de.johni0702.minecraft.gui.OffsetGuiRenderer)3 AbstractComposedGuiElement (de.johni0702.minecraft.gui.element.AbstractComposedGuiElement)3 ComposedGuiElement (de.johni0702.minecraft.gui.element.ComposedGuiElement)3 RenderInfo (de.johni0702.minecraft.gui.RenderInfo)2 VerticalLayout (de.johni0702.minecraft.gui.layout.VerticalLayout)2 GuiRenderer (de.johni0702.minecraft.gui.GuiRenderer)1 GuiButton (de.johni0702.minecraft.gui.element.GuiButton)1 GuiDropdownMenu (de.johni0702.minecraft.gui.element.advanced.GuiDropdownMenu)1 File (java.io.File)1