Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Created October 3, 2019 00:34
Show Gist options
  • Save Daomephsta/e48e39a6da8eaab29dcdefe78e60c77f to your computer and use it in GitHub Desktop.
Save Daomephsta/e48e39a6da8eaab29dcdefe78e60c77f to your computer and use it in GitHub Desktop.
public abstract class GuideContainerWidget extends GuideWidget implements LayoutContainer
{
private final Map<GuideWidget, LayoutElementMetrics> elementMetrics = new HashMap<>();
private final LayoutContainerMetrics metrics = new LayoutContainerMetrics();
@Override
public Iterable<? extends GuideWidget> getLayoutChildren()
{
return elementMetrics.keySet();
}
protected void add(GuideWidget element, int x, int y)
{
elementMetrics.put(element, new LayoutElementMetrics(x, y));
}
protected void add(GuideWidget element, int x, int y, Consumer<LayoutElementMetrics> metricsConfig)
{
LayoutElementMetrics metrics = new LayoutElementMetrics(x, y);
metricsConfig.accept(metrics);
elementMetrics.put(element, metrics);
}
@Override
public LayoutElementMetrics getLayoutElementMetrics(LayoutElement element)
{
return elementMetrics.get(element);
}
@Override
public LayoutContainerMetrics getLayoutContainerMetrics()
{
return metrics;
}
@Override
public void setLayoutValues(LayoutElement element, int x, int y, int width, int height)
{
if (element instanceof Widget)
((Widget) element).setOwnLayoutValues(x, y, width, height);
}
@Override
public void render(int mouseX, int mouseY, float lastFrameDuration)
{
for (GuideWidget element : getLayoutChildren())
{
element.render(mouseX, mouseY, lastFrameDuration);
}
}
}
public abstract class GuideWidget extends Widget implements GuideGuiElement
{
}
public class LabelWidget extends GuideWidget
{
private final String text;
public LabelWidget(String text)
{
this.text = text;
}
@Override
public void setOwnLayoutValues(int x, int y, int width, int height)
{
height = Math.max(height, MinecraftClient.getInstance().textRenderer.fontHeight);
System.out.printf("x: %d, y: %d, width: %d, height: %d\n", x, y, width, height);
super.setOwnLayoutValues(x, y, width, height);
}
@Override
public void render(int mouseX, int mouseY, float lastFrameDuration)
{
GlStateManager.disableTexture();
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferBuilder = tessellator.getBufferBuilder();
bufferBuilder.begin(GL11.GL_LINE_LOOP, VertexFormats.POSITION_COLOR);
bufferBuilder.vertex(x, y, 0).color(1.0F, 0.5F, 0.0F, 1.0F).next();
bufferBuilder.vertex(x + width, y, 0).color(1.0F, 0.5F, 0.0F, 1.0F).next();
bufferBuilder.vertex(x + width, y + height, 0).color(1.0F, 0.5F, 0.0F, 1.0F).next();
bufferBuilder.vertex(x, y + height, 0).color(1.0F, 0.5F, 0.0F, 1.0F).next();
tessellator.draw();
GlStateManager.enableTexture();
MinecraftClient.getInstance().textRenderer.draw(text, x, y, 0x000000);
}
}
public class ListWidget extends GuideContainerWidget
{
private final List<GuideWidget> elements = new ArrayList<>();
@Override
public void render(int mouseX, int mouseY, float lastFrameDuration)
{
for (GuideWidget element : elements)
{
element.render(mouseX, mouseY, lastFrameDuration);
}
}
public void add(GuideWidget element)
{
elements.add(element);
add(element, 0, elements.size() - 1);
}
public void add(GuideWidget element, Consumer<LayoutElementMetrics> metricsConfig)
{
elements.add(element);
add(element, 0, elements.size() - 1, metricsConfig);
}
@Override
public Iterable<? extends GuideWidget> getLayoutChildren()
{
return elements;
}
@Override
public void setLayoutValues(LayoutElement element, int x, int y, int width, int height)
{
if (element instanceof Widget)
((Widget) element).setOwnLayoutValues(x, y, width, height);
}
}
public class TableOfContentsEntries extends GuideContainerWidget implements VisibleContent
{
private final ListWidget links;
public TableOfContentsEntries(TableOfContents toc, int x, int y, int width, int height)
{
this.links = new ListWidget();
setOwnLayoutValues(x, y, width, height);
this.getLayoutContainerMetrics().setCellPadding(14);
for (Link link : toc.getLinks())
{
links.add(new LabelWidget(link.name), metrics ->
{
metrics.verticalGrowType = GrowType.PACK;
});
}
add(links, 0, 0);
System.out.printf("x: %d y: %d w: %d h: %d\n", x, y, width, height);
Layout.layout(this, x, y, width, height, false);
}
@Override
public void render(int mouseX, int mouseY, float lastFrameDuration)
{
for (LayoutElement child : getLayoutChildren())
{
if (child instanceof RenderableElement)
((RenderableElement) child).render(mouseX, mouseY, lastFrameDuration);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment