Skip to content

Commit

Permalink
Merge pull request #17 from KasugaLibGroup/gui
Browse files Browse the repository at this point in the history
fix: gui
  • Loading branch information
TimeBather authored Nov 30, 2024
2 parents 9aca1fd + 96ac8c6 commit 8c03c33
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"@types/node": "^22.9.3",
"esbuild": "^0.24.0",
"execa": "^5.5.1",
"execa": "^5.1.1",
"glob": "^11.0.0",
"tsx": "^4.19.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

Expand Down Expand Up @@ -82,9 +83,7 @@ public void run() {
return;
}
ResourceLocation id = getParameter("id", ResourceLocation.class);
RenderSystem.recordRenderCall(()->{
Minecraft.getInstance().setScreen(KasugaLib.STACKS.GUI.get().create(id).createScreen());
});
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()->()->GuiScreenHelper.createAndAttach(id));
}
}).submit(REGISTRY);

Expand Down Expand Up @@ -126,18 +125,16 @@ public void run() {
.addLiteral("gui", false)
.addLiteral("instances", false)
.addLiteral("inspect", false)
.addResourceLocation("id", false)
.addString("id", false)
.onlyIn(Dist.CLIENT)
.setHandler(new CommandHandler(){
@Override
public void run() {
if(KasugaLib.STACKS.GUI.isEmpty()){
return;
}
ResourceLocation id = getParameter("id", ResourceLocation.class);
RenderSystem.recordRenderCall(()->{
Minecraft.getInstance().setScreen(KasugaLib.STACKS.GUI.get().create(id).createScreen());
});
UUID id = UUID.fromString(getParameter("id", String.class));
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()->()->GuiScreenHelper.attach(id));
}
}).submit(REGISTRY);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kasuga.lib.core.client.frontend.commands;

import com.mojang.blaze3d.systems.RenderSystem;
import kasuga.lib.KasugaLib;
import kasuga.lib.core.client.frontend.gui.GuiInstance;
import kasuga.lib.core.client.frontend.gui.GuiScreen;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;

import java.util.UUID;

public class GuiScreenHelper {
public static void attach(UUID instanceId){
GuiInstance instance = KasugaLib.STACKS.GUI.orElseThrow().getInstanceById(instanceId).get();
if(instance == null)
return;
RenderSystem.recordRenderCall(()->{
Minecraft.getInstance().setScreen(new GuiScreen(instance));
});
}

public static void createAndAttach(ResourceLocation location){
RenderSystem.recordRenderCall(()->{
Minecraft.getInstance().setScreen(KasugaLib.STACKS.GUI.get().create(location).createScreen());
});
}
}
33 changes: 26 additions & 7 deletions src/main/java/kasuga/lib/core/client/frontend/gui/GuiContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import kasuga.lib.core.javascript.Tickable;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.locks.ReentrantLock;

@V8Convert()
Expand All @@ -30,7 +32,7 @@ public class GuiContext extends DomContext<GuiDomNode,GuiDomRoot> implements Tic

public GuiContext(GuiInstance guiInstance, DOMPriorityRegistry registry, ResourceLocation location) {
super(registry, location);
layoutEngine = LayoutEngines.YOGA;
layoutEngine = LayoutEngines.YOGA.get();
this.guiInstance = guiInstance;
}

Expand Down Expand Up @@ -77,6 +79,11 @@ public void removeSourceInfo(Object source) {

@Override
public void tick() {
if(!isRendering){
this.renderLock.lock();
beforeRenderTick();
this.renderLock.unlock();
}
super.tick();
this.getRootNode().getLayoutManager().tick();
}
Expand All @@ -93,6 +100,19 @@ public void render(Object source, RenderContext context){
}


Queue<Runnable> afterRenderTickTasks = new ArrayDeque<>(32);

public void beforeRenderTick(){
Runnable task;
while((task = afterRenderTickTasks.poll()) != null){
task.run();
}
}

public void enqueueAfterRenderTask(Runnable runnable) {
this.afterRenderTickTasks.add(runnable);
}

public void queueDuringRender(Runnable task) {
Optional<JavascriptContext> threadContext = this.getRenderer().getContext();
if(threadContext.isEmpty()){
Expand All @@ -102,17 +122,16 @@ public void queueDuringRender(Runnable task) {
JavascriptContext context = threadContext.get();
if(!this.isRendering){
this.renderLock.lock();
context.beforeRenderTick();
beforeRenderTick();
RuntimeException _e = null;
try{
task.run();
}catch (RuntimeException e){
_e = e;
e.printStackTrace();
}
this.renderLock.unlock();
if(_e!=null) throw _e;
}else{
context.enqueueAfterRenderTask(task);
enqueueAfterRenderTask(task);
}
}

Expand All @@ -124,11 +143,11 @@ public void queueDuringRenderUnsafe(Runnable task){
}
JavascriptContext context = threadContext.get();
if(!this.isRendering){
context.beforeRenderTick();
beforeRenderTick();
task.run();
this.renderLock.unlock();
}else{
context.enqueueAfterRenderTask(task);
enqueueAfterRenderTask(task);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kasuga.lib.core.client.frontend.gui.layout;

import kasuga.lib.core.client.frontend.common.layouting.LayoutEngine;
import kasuga.lib.core.client.frontend.gui.layout.yoga.YogaLayoutEngine;
import net.minecraftforge.common.util.Lazy;

public class LayoutEngines {
public static YogaLayoutEngine YOGA = new YogaLayoutEngine();
public static Lazy<LayoutEngine> YOGA = Lazy.of(YogaLayoutEngine::new);
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public boolean removeChild(DomNode<GuiContext> child) {
@Override
public boolean addChild(DomNode<GuiContext> child) {
this.domContext.queueDuringRender(()-> {
super.addChild(child);
addChildInstant(children.size(), child);
});
return true;
}
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/kasuga/lib/core/javascript/JavascriptContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void close(){

public void tick(){
tickables.forEach(Tickable::tick);
this.context.tick();
}

public Callback registerTickable(Tickable tickable){
Expand Down Expand Up @@ -74,19 +75,6 @@ public Callback runTask(Callback task) {
});
}

Queue<Runnable> afterRenderTickTasks = new ArrayDeque<>(32);

public void beforeRenderTick(){
Runnable task;
while((task = afterRenderTickTasks.poll()) != null){
task.run();
}
}

public void enqueueAfterRenderTask(Runnable runnable) {
this.afterRenderTickTasks.add(runnable);
}

public JavascriptEngineContext getRuntimeContext() {
return this.context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface JavascriptEngineContext {
JavascriptEngineModule compileNativeModule(Object target, String moduleName);

JavascriptContext getContext();

void tick();
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@ public JavetJavascriptModule compileNativeModule(Object target, String moduleNam
public JavascriptContext getContext() {
return context;
}

private int gcTicks = 0;
@Override
public void tick() {
if(gcTicks ++ > 20){
runtime.lowMemoryNotification();
}
}
}

0 comments on commit 8c03c33

Please sign in to comment.