Skip to content

Commit

Permalink
Add custom render elements
Browse files Browse the repository at this point in the history
  • Loading branch information
LLytho committed Jul 31, 2024
1 parent 45269bc commit ed8e3f9
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [1.4.0]
- Add custom render elements

## [1.3.1]
- Fix crash with new kubejs version

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.daemon=false
# Mod data
mod_id=ponderjs
mod_name=PonderJS
mod_version=1.3.1
mod_version=1.4.0
mod_base_package=com.almostreliable
mod_authors=kotakotik22, AlmostReliable
mod_description=Allows creating ponder scenes and tags with KubeJS.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.almostreliable.ponderjs.api;

import com.simibubi.create.foundation.ponder.PonderScene;
import com.simibubi.create.foundation.ponder.element.AnimatedOverlayElement;
import com.simibubi.create.foundation.ponder.ui.PonderUI;
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.client.gui.GuiGraphics;

public class CustomPonderOverlayElement extends AnimatedOverlayElement {
protected OnRenderOverlay onRender = (ctx) -> {};
protected OnElementAction onWhileSkipping = (ctx) -> {};
protected OnElementAction onTick = (ctx) -> {};
protected OnElementAction onReset = (ctx) -> {};
private int currentTick = 0;

public int getCurrentTick() {
return currentTick;
}

public CustomPonderOverlayElement onSkipping(OnElementAction onWhileSkipping) {
this.onWhileSkipping = onWhileSkipping;
return this;
}

public CustomPonderOverlayElement onTick(OnElementAction onTick) {
this.onTick = onTick;
return this;
}

public CustomPonderOverlayElement onReset(OnElementAction onReset) {
this.onReset = onReset;
return this;
}

public CustomPonderOverlayElement onRender(OnRenderOverlay onRender) {
this.onRender = onRender;
return this;
}

@Override
public void whileSkipping(PonderScene scene) {
super.whileSkipping(scene);
onWhileSkipping.accept(new OnElementAction.Context(this, scene));
}

@Override
public void tick(PonderScene scene) {
super.tick(scene);
currentTick++;
onTick.accept(new OnElementAction.Context(this, scene));
}

@Override
public void reset(PonderScene scene) {
super.reset(scene);
currentTick = 0;
onReset.accept(new OnElementAction.Context(this, scene));
}

@HideFromJS
@Override
protected void render(PonderScene scene, PonderUI screen, GuiGraphics graphics, float partialTicks, float fade) {
var ctx = new OnRenderOverlay.RenderContext(this, scene, screen, graphics, partialTicks, fade);
onRender.render(ctx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.almostreliable.ponderjs.api;

import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.foundation.ponder.PonderScene;
import com.simibubi.create.foundation.ponder.PonderWorld;
import com.simibubi.create.foundation.ponder.element.AnimatedSceneElement;
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;

public class CustomPonderSceneElement extends AnimatedSceneElement {
protected OnRenderWorld onRenderFirst = (ctx) -> {};
protected OnRenderWorld.Layer onRenderWorldLayer = (ctx) -> {};
protected OnRenderWorld onRenderLast = (ctx) -> {};
protected OnElementAction onWhileSkipping = (ctx) -> {};
protected OnElementAction onTick = (ctx) -> {};
protected OnElementAction onReset = (ctx) -> {};
private int currentTick = 0;

public int getCurrentTick() {
return currentTick;
}

public CustomPonderSceneElement onSkipping(OnElementAction onWhileSkipping) {
this.onWhileSkipping = onWhileSkipping;
return this;
}

public CustomPonderSceneElement onTick(OnElementAction onTick) {
this.onTick = onTick;
return this;
}

public CustomPonderSceneElement onReset(OnElementAction onReset) {
this.onReset = onReset;
return this;
}

public CustomPonderSceneElement onRenderFirst(OnRenderWorld onRenderWorld) {
this.onRenderFirst = onRenderWorld;
return this;
}

public CustomPonderSceneElement onRender(OnRenderWorld.Layer onRenderWorldLayer) {
this.onRenderWorldLayer = onRenderWorldLayer;
return this;
}

public CustomPonderSceneElement onRenderLast(OnRenderWorld onRenderWorld) {
this.onRenderLast = onRenderWorld;
return this;
}

@Override
public void whileSkipping(PonderScene scene) {
super.whileSkipping(scene);
onWhileSkipping.accept(new OnElementAction.Context(this, scene));
}

@Override
public void tick(PonderScene scene) {
super.tick(scene);
currentTick++;
onTick.accept(new OnElementAction.Context(this, scene));
}

@Override
public void reset(PonderScene scene) {
super.reset(scene);
currentTick = 0;
onReset.accept(new OnElementAction.Context(this, scene));
}

@HideFromJS
@Override
public void renderFirst(PonderWorld world, MultiBufferSource buffer, PoseStack ms, float fade, float pt) {
super.renderFirst(world, buffer, ms, fade, pt);
var ctx = new OnRenderWorld.RenderContext(this, world, buffer, ms, pt, fade);
onRenderFirst.renderWorld(ctx);
}

@HideFromJS
@Override
public void renderLayer(PonderWorld world, MultiBufferSource buffer, RenderType type, PoseStack ms, float fade, float pt) {
super.renderLayer(world, buffer, type, ms, fade, pt);
var ctx = new OnRenderWorld.Layer.RenderContext(this, world, buffer, type, ms, pt, fade);
onRenderWorldLayer.renderLayer(ctx);
}

@HideFromJS
@Override
public void renderLast(PonderWorld world, MultiBufferSource buffer, PoseStack ms, float fade, float pt) {
super.renderLast(world, buffer, ms, fade, pt);
var ctx = new OnRenderWorld.RenderContext(this, world, buffer, ms, pt, fade);
onRenderLast.renderWorld(ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.simibubi.create.foundation.ponder.element.EntityElement;
import com.simibubi.create.foundation.ponder.element.InputWindowElement;
import com.simibubi.create.foundation.ponder.element.TextWindowElement;
import com.simibubi.create.foundation.ponder.instruction.FadeInOutInstruction;
import com.simibubi.create.foundation.ponder.instruction.ShowInputInstruction;
import com.simibubi.create.foundation.ponder.instruction.TickingInstruction;
import com.simibubi.create.foundation.utility.Pointing;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.rhino.util.HideFromJS;
Expand Down Expand Up @@ -42,6 +44,7 @@ public ExtendedSceneBuilder(PonderScene ponderScene) {
this.ponderScene = ponderScene;
((SceneBuilderAccessor) this).ponderjs$setWorldInstructions(new ExtendedWorldInstructions());
((SceneBuilderAccessor) this).ponderjs$setSpecialInstructions(new ExtendedSpecialInstructions());
((SceneBuilderAccessor) this).ponderjs$setOverlayInstructions(new ExtendedOverlayInstructions());
this.particles = new ParticleInstructions(this);
}

Expand Down Expand Up @@ -141,7 +144,67 @@ public InputWindowElement showControls(int duration, Vec3 pos, Pointing pointing
return element;
}

public class ExtendedOverlayInstructions extends OverlayInstructions {

public CustomPonderOverlayElement addElement(int ticks) {
var element = new CustomPonderOverlayElement();
addInstruction(new TickingInstruction(false, ticks) {
@Override
protected void firstTick(PonderScene scene) {
super.firstTick(scene);
scene.addElement(element);
}
});

return element;
}

public CustomPonderOverlayElement addElement() {
var element = new CustomPonderOverlayElement();
addInstruction(ponderScene -> {
ponderScene.addElement(element);
});

return element;
}
}

public class ExtendedWorldInstructions extends WorldInstructions {

public CustomPonderSceneElement addElement(int ticks) {
var element = new CustomPonderSceneElement();
element.setVisible(false);
addInstruction(new FadeInOutInstruction(ticks) {
@Override
protected void show(PonderScene scene) {
scene.addElement(element);
element.setVisible(true);
}

@Override
protected void hide(PonderScene scene) {
element.setVisible(false);
}

@Override
protected void applyFade(PonderScene scene, float fade) {
element.setFade(fade);
}
});

return element;
}

public CustomPonderSceneElement addElement() {
var element = new CustomPonderSceneElement();
addInstruction(ponderScene -> {
ponderScene.addElement(element);
element.setVisible(true);
});

return element;
}

/**
* Create a new entity with some default behavior. The entity will be rotated to face north.
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/almostreliable/ponderjs/api/OnElementAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.almostreliable.ponderjs.api;

import com.simibubi.create.foundation.ponder.PonderScene;
import com.simibubi.create.foundation.ponder.element.PonderElement;

@FunctionalInterface
public interface OnElementAction {

void accept(Context context);

record Context(PonderElement getElement, PonderScene getScene) {}
}
15 changes: 15 additions & 0 deletions src/main/java/com/almostreliable/ponderjs/api/OnRenderOverlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.almostreliable.ponderjs.api;

import com.simibubi.create.foundation.ponder.PonderScene;
import com.simibubi.create.foundation.ponder.element.PonderOverlayElement;
import com.simibubi.create.foundation.ponder.ui.PonderUI;
import net.minecraft.client.gui.GuiGraphics;

@FunctionalInterface
public interface OnRenderOverlay {

void render(RenderContext context);

record RenderContext(PonderOverlayElement getElement, PonderScene getScene, PonderUI getScreen,
GuiGraphics getGraphics, float getPartialTicks, float getFade) {}
}
25 changes: 25 additions & 0 deletions src/main/java/com/almostreliable/ponderjs/api/OnRenderWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.almostreliable.ponderjs.api;

import com.mojang.blaze3d.vertex.PoseStack;
import com.simibubi.create.foundation.ponder.PonderWorld;
import com.simibubi.create.foundation.ponder.element.PonderElement;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;

@FunctionalInterface
public interface OnRenderWorld {

void renderWorld(RenderContext context);

@FunctionalInterface
interface Layer {
void renderLayer(RenderContext context);

record RenderContext(PonderElement getElement, PonderWorld getWorld, MultiBufferSource getBuffer,
RenderType getType, PoseStack getPoseStack, float getPartialTicks, float getFade) {}
}


record RenderContext(PonderElement getElement, PonderWorld getWorld, MultiBufferSource getBuffer,
PoseStack getPoseStack, float getPartialTicks, float getFade) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public interface SceneBuilderAccessor {
@Accessor(value = "special", remap = false)
@Mutable
void ponderjs$setSpecialInstructions(SceneBuilder.SpecialInstructions specialInstructions);

@Accessor(value = "overlay", remap = false)
@Mutable
void ponderjs$setOverlayInstructions(SceneBuilder.OverlayInstructions overlayInstructions);
}

0 comments on commit ed8e3f9

Please sign in to comment.