From 8575693fe5a0b8536d81097d048cdf5c5e27aa53 Mon Sep 17 00:00:00 2001 From: pietru Date: Sun, 29 Sep 2024 23:34:50 +0200 Subject: [PATCH] add component registering --- .../pietru/omni_power/ui/CustomScreen.java | 123 ++++++++++-------- 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/src/main/java/net/pietru/omni_power/ui/CustomScreen.java b/src/main/java/net/pietru/omni_power/ui/CustomScreen.java index 7b0ce9c..391091f 100644 --- a/src/main/java/net/pietru/omni_power/ui/CustomScreen.java +++ b/src/main/java/net/pietru/omni_power/ui/CustomScreen.java @@ -10,16 +10,20 @@ import finalforeach.cosmicreach.gamestates.InGame; import finalforeach.cosmicreach.items.ItemSlot; import finalforeach.cosmicreach.items.containers.SlotContainer; import finalforeach.cosmicreach.items.screens.BaseItemScreen; +import finalforeach.cosmicreach.savelib.utils.TriConsumer; import finalforeach.cosmicreach.ui.UI; import finalforeach.cosmicreach.ui.widgets.ItemSlotWidget; import net.pietru.omni_power.OmniPower; import net.pietru.omni_power.blocks.CustomUIBlockEntity; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import static net.pietru.omni_power.OmniPower.MOD_ID; public class CustomScreen extends BaseItemScreen { + public static final Map> components = new HashMap<>(); public static Skin skin = new Skin(GameAssetLoader.loadAsset(MOD_ID+":gdx-skins/default/skin/uiskin.json")); //vis/skin/x1/uiskin.json @@ -71,63 +75,10 @@ public class CustomScreen extends BaseItemScreen { String type = data.getString("type",""); if (type.isEmpty()) continue; - Button button; - switch (type.toLowerCase()) { - case "row": - table.row(); - break; - case "space": - table.add() - .height(data.getFloat("width",10F)) - .height(data.getFloat("height",10F)); - break; - case "label": - Label lbl = new Label(data.getString("text",""),skin); - lbl.setFontScale(data.getFloat("font_scale",1F)); - table.add(lbl); - break; - case "change_ui_text_button": - button = new TextButton(data.getString("text",""),skin); - String ui_id = data.getString("gui_id",""); - button.addListener(event -> { - if (!button.isPressed()) - return false; - UI.openContainers.removeValue(screen.container, true); - InGame.IN_GAME.removeBaseItemScreen(screen); - addOpenContainer(screen.blockEntity,screen.blockEntity.slotContainer,ui_id); - return true; - }); - table.add(button); - break; - case "trigger_block_event_text_button": - button = new TextButton(data.getString("text", ""), skin); - String trigger_id = data.getString("trigger_id",""); - button.addListener(event -> { - if (!button.isPressed()) - return false; - UI.openContainers.removeValue(screen.container, true); - InGame.IN_GAME.removeBaseItemScreen(screen); - //INSERT CODE TO RUN BE TRIGGER - return true; - }); - table.add(button); - break; - case "item_slot": - add_ui_slot(table,screen,data.getInt("slot",0),data.getBoolean("output",false)); - break; - case "table": - JsonValue ui_layout = data.get("layout"); - Table new_table = new Table(); - build_ui(ui_layout,new_table,screen); - - float default_width = Math.max(new_table.getColumns(),1) * 32; - new_table.setWidth(ui_layout.getFloat("width",default_width)); - float default_height = Math.max(new_table.getRows(),1) * 32; - new_table.setHeight(ui_layout.getFloat("height",default_height)); - - table.add(new_table); - break; - } + if (!components.containsKey(type.toLowerCase())) + continue; + TriConsumer component = components.get(type.toLowerCase()); + component.accept(table,screen,data); } } @@ -176,4 +127,62 @@ public class CustomScreen extends BaseItemScreen { screen.getActor().addAction(perFrameAct); } } + + static { + //Table table, CustomScreen screen + components.put("row",(table,screen,data)->{ + table.row(); + }); + components.put("space",(table,screen,data)->{ + table.add() + .height(data.getFloat("width",10F)) + .height(data.getFloat("height",10F)); + }); + components.put("label",(table,screen,data)->{ + Label lbl = new Label(data.getString("text",""),skin); + lbl.setFontScale(data.getFloat("font_scale",1F)); + table.add(lbl); + }); + components.put("change_ui_text_button",(table,screen,data)->{ + Button button = new TextButton(data.getString("text",""),skin); + String ui_id = data.getString("gui_id",""); + button.addListener(event -> { + if (!button.isPressed()) + return false; + UI.openContainers.removeValue(screen.container, true); + InGame.IN_GAME.removeBaseItemScreen(screen); + addOpenContainer(screen.blockEntity,screen.blockEntity.slotContainer,ui_id); + return true; + }); + table.add(button); + }); + components.put("trigger_block_event_text_button",(table,screen,data)->{ + Button button = new TextButton(data.getString("text", ""), skin); + String trigger_id = data.getString("trigger_id",""); + button.addListener(event -> { + if (!button.isPressed()) + return false; + UI.openContainers.removeValue(screen.container, true); + InGame.IN_GAME.removeBaseItemScreen(screen); + //INSERT CODE TO RUN BE TRIGGER + return true; + }); + table.add(button); + }); + components.put("item_slot",(table,screen,data)->{ + add_ui_slot(table,screen,data.getInt("slot",0),data.getBoolean("output",false)); + }); + components.put("table",(table,screen,data)->{ + JsonValue ui_layout = data.get("layout"); + Table new_table = new Table(); + build_ui(ui_layout,new_table,screen); + + float default_width = Math.max(new_table.getColumns(),1) * 32; + new_table.setWidth(ui_layout.getFloat("width",default_width)); + float default_height = Math.max(new_table.getRows(),1) * 32; + new_table.setHeight(ui_layout.getFloat("height",default_height)); + + table.add(new_table); + }); + } } \ No newline at end of file