Skip to content

Commit

Permalink
Add panel renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Feb 26, 2024
1 parent 1a7870e commit cfc36bd
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 12 deletions.
2 changes: 1 addition & 1 deletion MoulConfig.xsd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class GuiTextures {
SLIDER_BUTTON,
COLOUR_SELECTOR_DOT, COLOUR_SELECTOR_BAR, COLOUR_SELECTOR_BAR_ALPHA, COLOUR_SELECTOR_CHROMA,
COLOUR_PICKER_INTERNAL, COLOUR_PICKER_INTERNAL_VALUE, COLOUR_PICKER_INTERNAL_OPACITY,
SEARCH;
SEARCH, VANILLA_PANEL;

private static MyResourceLocation root;

Expand Down Expand Up @@ -71,6 +71,7 @@ public static void setTextureRoot(MyResourceLocation root) {
COLOUR_PICKER_INTERNAL_VALUE = r("internal/colourpicker_value");
COLOUR_PICKER_INTERNAL_OPACITY = r("internal/colourpicker_opacity");
SEARCH = r("search.png");
VANILLA_PANEL = r("vanilla_panel.png");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.notenoughupdates.moulconfig.common

import io.github.notenoughupdates.moulconfig.GuiTextures
import juuxel.libninepatch.NinePatch

object NinePatches {
fun createButton(): NinePatch<MyResourceLocation> {
return NinePatch.builder(GuiTextures.BUTTON)
.cornerSize(10)
.cornerUv(10 / 32f, 10 / 96F)
.mode(NinePatch.Mode.STRETCHING)
.build()
}

fun createVanillaPanel(): NinePatch<MyResourceLocation> {
return NinePatch.builder(GuiTextures.VANILLA_PANEL)
.cornerSize(4)
.cornerUv(4 / 16F)
.mode(NinePatch.Mode.STRETCHING)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.notenoughupdates.moulconfig.common;

import io.github.notenoughupdates.moulconfig.internal.NinePatchRenderer;
import juuxel.libninepatch.NinePatch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -107,6 +109,13 @@ default void drawTexturedRect(float x, float y, float width, float height) {

void drawTexturedRect(float x, float y, float width, float height, float u1, float v1, float u2, float v2);

default void drawNinePatch(NinePatch<MyResourceLocation> patch, float x, float y, int width, int height) {
pushMatrix();
translate(x, y, 0);
patch.draw(NinePatchRenderer.INSTANCE, this, width, height);
popMatrix();
}

default void drawDarkRect(int x, int y, int width, int height) {
drawDarkRect(x, y, width, height, true);
}
Expand All @@ -131,7 +140,12 @@ default void drawDarkRect(int x, int y, int width, int height) {

void disableScissor();

default IMinecraft getMinecraft() {

default @NotNull IMinecraft getMinecraft() {
return IMinecraft.instance;
}

default void bindTexture(@NotNull MyResourceLocation texture) {
getMinecraft().bindTexture(texture);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ButtonComponent(
element: GuiComponent,
insets: Int,
val onClick: Runnable
) : PanelComponent(element, insets) {
) : PanelComponent(element, insets, DefaultBackgroundRenderer.BUTTON) {
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
if (context.isHovered && mouseEvent is Click) {
val (mouseButton, mouseState) = mouseEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package io.github.notenoughupdates.moulconfig.gui.component;

import io.github.notenoughupdates.moulconfig.common.NinePatches;
import io.github.notenoughupdates.moulconfig.common.RenderContext;
import io.github.notenoughupdates.moulconfig.gui.GuiComponent;
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext;
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent;
Expand All @@ -33,20 +35,50 @@
*/
@Getter
public class PanelComponent extends GuiComponent {

public interface BackgroundRenderer {
void render(RenderContext renderContext, int x, int y, int width, int height);
}

public enum DefaultBackgroundRenderer implements BackgroundRenderer {
DARK_RECT {
@Override
public void render(RenderContext renderContext, int x, int y, int width, int height) {
renderContext.drawDarkRect(x, y, width, height);
}
},
BUTTON {
@Override
public void render(RenderContext renderContext, int x, int y, int width, int height) {
renderContext.drawNinePatch(NinePatches.INSTANCE.createButton(), x, y, width, height);
}
},
VANILLA {
@Override
public void render(RenderContext renderContext, int x, int y, int width, int height) {
renderContext.drawNinePatch(NinePatches.INSTANCE.createVanillaPanel(), x, y, width, height);
}
}
}

private final GuiComponent element;
private final int insets;
private final BackgroundRenderer backgroundRenderer;

/**
* @param element the child element to render the panels contents
* @param insets the padding size of this panel
* @param element the child element to render the panels contents
* @param insets the padding size of this panel
* @param backgroundRenderer the renderer to render the background of this panel
* @see DefaultBackgroundRenderer
*/
public PanelComponent(GuiComponent element, int insets) {
public PanelComponent(GuiComponent element, int insets, BackgroundRenderer backgroundRenderer) {
this.element = element;
this.insets = insets;
this.backgroundRenderer = backgroundRenderer;
}

public PanelComponent(GuiComponent element) {
this(element, 2);
this(element, 2, DefaultBackgroundRenderer.DARK_RECT);
}

@Override
Expand All @@ -65,13 +97,13 @@ public int getHeight() {
}

protected GuiImmediateContext getChildContext(GuiImmediateContext context) {
return context.translated(insets, insets, element.getWidth(), element.getHeight());
return context.translated(insets, insets, context.getWidth() - insets * 2, context.getHeight() - insets * 2 - 2);
}

@Override
public void render(GuiImmediateContext context) {
context.getRenderContext().pushMatrix();
context.getRenderContext().drawDarkRect(0, 0, context.getWidth(), context.getHeight() - 2);
backgroundRenderer.render(context.getRenderContext(), 0, 0, context.getWidth(), context.getHeight() - 2);
context.getRenderContext().translate(insets, insets, 0);
element.render(getChildContext(context));
context.getRenderContext().popMatrix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object NinePatchRenderer : ContextualTextureRenderer<MyResourceLocation, RenderC
u2: Float,
v2: Float
) {
context.bindTexture(texture)
context.drawTexturedRect(
x.toFloat(),
y.toFloat(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.notenoughupdates.moulconfig.common.IMinecraft;
import io.github.notenoughupdates.moulconfig.common.MyResourceLocation;
import io.github.notenoughupdates.moulconfig.gui.GuiComponent;
import io.github.notenoughupdates.moulconfig.gui.component.PanelComponent;
import io.github.notenoughupdates.moulconfig.xml.loaders.*;
import lombok.SneakyThrows;
import lombok.var;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static XMLUniverse getDefaultUniverse() {
xmlUniverse.registerMapper(boolean.class, Boolean::valueOf);
xmlUniverse.registerMapper(List.class, str -> Arrays.asList(str.split(";")));
xmlUniverse.registerMapper(MyResourceLocation.class, MyResourceLocation.Companion::parse);
xmlUniverse.registerMapper(PanelComponent.BackgroundRenderer.class, PanelComponent.DefaultBackgroundRenderer::valueOf);
return xmlUniverse;
}

Expand Down
Loading

0 comments on commit cfc36bd

Please sign in to comment.