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;
}
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);
}
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());
}
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()));
}
}
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);
}
}
}
}
Aggregations