Skip to content

Commit

Permalink
Make panel expansion optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Dec 26, 2023
1 parent 7d6da4e commit a91572c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.github.moulberry.moulconfig.gui.HorizontalAlign;
import io.github.moulberry.moulconfig.processor.ProcessedCategory;
import io.github.moulberry.moulconfig.processor.ProcessedOption;
import net.minecraft.util.EnumChatFormatting;

import java.util.ArrayList;
Expand Down Expand Up @@ -56,6 +57,10 @@ public String getTitle() {
public void saveNow() {
}

public DescriptionRendereringBehaviour getDescriptionBehaviour(ProcessedOption option) {
return DescriptionRendereringBehaviour.SCALE_TEXT;
}

public boolean shouldAutoFocusSearchbar() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.moulberry.moulconfig;

public enum DescriptionRendereringBehaviour {
SCALE_TEXT,
EXPAND_PANEL,
// TODO: HOVER_ONLY,
// TODO: TRUNCATE,
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package io.github.moulberry.moulconfig.gui.editors;

import io.github.moulberry.moulconfig.DescriptionRendereringBehaviour;
import io.github.moulberry.moulconfig.common.IMinecraft;
import io.github.moulberry.moulconfig.gui.GuiComponent;
import io.github.moulberry.moulconfig.gui.GuiImmediateContext;
import io.github.moulberry.moulconfig.gui.GuiOptionEditor;
import io.github.moulberry.moulconfig.gui.MouseEvent;
import io.github.moulberry.moulconfig.gui.component.CenterComponent;
import io.github.moulberry.moulconfig.gui.component.PanelComponent;
import io.github.moulberry.moulconfig.internal.ForgeRenderContext;
import io.github.moulberry.moulconfig.processor.ProcessedOption;
import lombok.var;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.input.Mouse;

import java.util.List;

public abstract class ComponentEditor extends GuiOptionEditor {
protected ComponentEditor(ProcessedOption option) {
super(option);
Expand All @@ -38,6 +43,87 @@ public GuiImmediateContext getImmContext(
);
}

public class EditorComponentWrapper extends PanelComponent {
public EditorComponentWrapper(GuiComponent component) {
super(component);
}

@Override
public int getWidth() {
return super.getWidth() + 150;
}

@Override
public int getHeight() {
if (option.config.getDescriptionBehaviour(option) != DescriptionRendereringBehaviour.EXPAND_PANEL)
return super.getHeight();
var fr = IMinecraft.instance.getDefaultFontRenderer();
return Math.max(45, fr.splitText(option.desc, 250 * 2 / 3 - 10).size() * (fr.getHeight() + 1) + 10);
}

@Override
protected GuiImmediateContext getChildContext(GuiImmediateContext context) {
return context.translated(5, 13, context.getWidth() / 3 - 10, context.getHeight() - 13);
}

@Override
public void render(@NotNull GuiImmediateContext context) {
context.getRenderContext().drawDarkRect(0, 0, context.getWidth(), context.getHeight() - 2);

renderTitle(context);

renderDescription(context);

renderElement(context);
}

protected void renderElement(@NotNull GuiImmediateContext context) {
context.getRenderContext().pushMatrix();
context.getRenderContext().translate(5, 13, 0);
this.getElement().render(getChildContext(context));
context.getRenderContext().popMatrix();
}

protected void renderTitle(@NotNull GuiImmediateContext context) {
int width = context.getWidth();
var minecraft = context.getRenderContext().getMinecraft();
var fr = minecraft.getDefaultFontRenderer();
context.getRenderContext().drawStringCenteredScaledMaxWidth(
option.name, fr, width / 6, 13, true, width / 3 - 10, 0xc0c0c0
);
}

protected void renderDescription(@NotNull GuiImmediateContext context) {
int width = context.getWidth();
var minecraft = context.getRenderContext().getMinecraft();
var fr = minecraft.getDefaultFontRenderer();
float scale = 1;
List<String> lines;
while (true) {
lines = fr.splitText(option.desc, (int) (width * 2 / 3 / scale - 10));
if (lines.size() * scale * (fr.getHeight() + 1) + 10 < context.getHeight())
break;
scale -= 1 / 8f;
if (scale < 1 / 16f) break;
}
context.getRenderContext().pushMatrix();
context.getRenderContext().translate(5 + width / 3, 5, 0);
context.getRenderContext().scale(scale, scale, 1);
context.getRenderContext().translate(0, ((context.getHeight() - 10) - (fr.getHeight() + 1) * (lines.size() - 1) * scale) / 2F, 0);
for (String line : lines) {
context.getRenderContext().drawString(fr, line, 0, 0, 0xc0c0c0, false);
context.getRenderContext().translate(0, fr.getHeight() + 1, 0);
}
context.getRenderContext().popMatrix();
}
}

protected GuiComponent wrapComponent(GuiComponent component) {
return new EditorComponentWrapper(
new CenterComponent(component)
);
}

@Override
public int getHeight() {
return Math.max(getDelegate().getHeight(), super.getHeight());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,85 +50,6 @@ public GuiOptionEditorBoolean(
bool = wrapComponent(new CenterComponent(new SwitchComponent(prop, 200)));
}

private class EditorComponentWrapper extends PanelComponent {
public EditorComponentWrapper(GuiComponent component) {
super(component);
}

@Override
public int getWidth() {
return super.getWidth() + 150;
}

@Override
public int getHeight() {
var fr = IMinecraft.instance.getDefaultFontRenderer();
return Math.max(45, fr.splitText(option.desc, 250 * 2 / 3 - 10).size() * (fr.getHeight() + 1) + 10);
}

@Override
protected GuiImmediateContext getChildContext(GuiImmediateContext context) {
return context.translated(5, 13, context.getWidth() / 3 - 10, context.getHeight() - 13);
}

@Override
public void render(@NotNull GuiImmediateContext context) {
context.getRenderContext().drawDarkRect(0, 0, context.getWidth(), context.getHeight() - 2);

renderTitle(context);

renderDescription(context);

renderElement(context);
}

private void renderElement(@NotNull GuiImmediateContext context) {
context.getRenderContext().pushMatrix();
context.getRenderContext().translate(5, 13, 0);
this.getElement().render(getChildContext(context));
context.getRenderContext().popMatrix();
}

private void renderTitle(@NotNull GuiImmediateContext context) {
int width = context.getWidth();
var minecraft = context.getRenderContext().getMinecraft();
var fr = minecraft.getDefaultFontRenderer();
context.getRenderContext().drawStringCenteredScaledMaxWidth(
option.name, fr, width / 6, 13, true, width / 3 - 10, 0xc0c0c0
);
}

private void renderDescription(@NotNull GuiImmediateContext context) {
int width = context.getWidth();
var minecraft = context.getRenderContext().getMinecraft();
var fr = minecraft.getDefaultFontRenderer();
float scale = 1;
List<String> lines;
while (true) {
lines = fr.splitText(option.desc, (int) (width * 2 / 3 / scale - 10));
if (lines.size() * scale * (fr.getHeight() + 1) + 10 < context.getHeight())
break;
scale -= 1 / 8f;
if (scale < 1 / 16f) break;
}
context.getRenderContext().pushMatrix();
context.getRenderContext().translate(5 + width / 3, 5, 0);
context.getRenderContext().scale(scale, scale, 1);
context.getRenderContext().translate(0, ((context.getHeight() - 10) - (fr.getHeight() + 1) * (lines.size() - 1) * scale) / 2F, 0);
for (String line : lines) {
context.getRenderContext().drawString(fr, line, 0, 0, 0xc0c0c0, false);
context.getRenderContext().translate(0, fr.getHeight() + 1, 0);
}
context.getRenderContext().popMatrix();
}
}

protected GuiComponent wrapComponent(GuiComponent component) {
return new EditorComponentWrapper(
new CenterComponent(component)
);
}

@Override
public @NotNull GuiComponent getDelegate() {
return bool;
Expand Down

0 comments on commit a91572c

Please sign in to comment.