diff --git a/src/main/java/net/pietru/omni_power/OmniPower.java b/src/main/java/net/pietru/omni_power/OmniPower.java index a2aaed5..0a36534 100644 --- a/src/main/java/net/pietru/omni_power/OmniPower.java +++ b/src/main/java/net/pietru/omni_power/OmniPower.java @@ -7,6 +7,7 @@ import finalforeach.cosmicreach.GameAssetLoader; import finalforeach.cosmicreach.blockevents.BlockEvents; import finalforeach.cosmicreach.blocks.Block; import finalforeach.cosmicreach.items.ItemThing; +import net.pietru.omni_power.blockevents.OpenCustomUI; import net.pietru.omni_power.blockevents.Power; import dev.crmodders.cosmicquilt.api.entrypoint.ModInitializer; import net.pietru.omni_power.blocks.CustomUIBlockEntity; @@ -31,7 +32,8 @@ public class OmniPower implements ModInitializer { "door_cube", "cable_merger", "cable_adapter", - "custom_crate" + "custom_crate", + "custom_boombox" }; public static Map uis = new HashMap<>(); @@ -69,11 +71,13 @@ public class OmniPower implements ModInitializer { load_custom_ui("omni_power:gui/example.json"); load_custom_ui("omni_power:gui/example2.json"); + load_custom_ui("omni_power:gui/example-boombox.json"); } public static void load_power_event(){ try { BlockEvents.registerBlockEventAction(Power.class); + BlockEvents.registerBlockEventAction(OpenCustomUI.class); } catch (Exception e){ System.out.println(e); System.out.println("If msg above says something Duplicate block event action key it might be intended behaviour..."); diff --git a/src/main/java/net/pietru/omni_power/blockevents/OpenCustomUI.java b/src/main/java/net/pietru/omni_power/blockevents/OpenCustomUI.java new file mode 100644 index 0000000..d7bbcd2 --- /dev/null +++ b/src/main/java/net/pietru/omni_power/blockevents/OpenCustomUI.java @@ -0,0 +1,42 @@ +package net.pietru.omni_power.blockevents; + +import com.badlogic.gdx.utils.Array; +import finalforeach.cosmicreach.blockentities.BlockEntity; +import finalforeach.cosmicreach.blockevents.BlockEventArgs; +import finalforeach.cosmicreach.blockevents.BlockEventTrigger; +import finalforeach.cosmicreach.blockevents.actions.ActionId; +import finalforeach.cosmicreach.blockevents.actions.IBlockAction; +import finalforeach.cosmicreach.blocks.BlockPosition; +import finalforeach.cosmicreach.blocks.BlockState; +import finalforeach.cosmicreach.items.containers.SlotContainer; +import finalforeach.cosmicreach.world.Zone; +import net.pietru.omni_power.OmniPower; +import net.pietru.omni_power.api.IPowerClient; +import net.pietru.omni_power.api.IPowerProducer; +import net.pietru.omni_power.ui.CustomScreen; +import net.pietru.omni_power.ui.DummyObject; + +@ActionId(id = OmniPower.MOD_ID+":open_ui") +public class OpenCustomUI implements IBlockAction { + public static boolean block_entities = false; + + public String ui_id; + public int slots; + + public OpenCustomUI(){ + block_entities=OmniPower.block_entities; + } + + @Override + public void act(BlockEventArgs args) { + DummyObject tmp = new DummyObject(); + tmp.srcBlockState=args.srcBlockState; + tmp.blockEventTrigger=args.blockEventTrigger; + tmp.zone=args.zone; + tmp.blockPos=args.blockPos; + tmp.srcIdentity=args.srcIdentity; + tmp.ui_id=ui_id; + tmp.container=new SlotContainer(slots); + CustomScreen.addOpenContainer(tmp,tmp.container); + } +} 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 4a51548..252993d 100644 --- a/src/main/java/net/pietru/omni_power/ui/CustomScreen.java +++ b/src/main/java/net/pietru/omni_power/ui/CustomScreen.java @@ -6,6 +6,10 @@ import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable; import com.badlogic.gdx.utils.JsonValue; import finalforeach.cosmicreach.GameAssetLoader; +import finalforeach.cosmicreach.blockevents.BlockEventArgs; +import finalforeach.cosmicreach.blockevents.BlockEventTrigger; +import finalforeach.cosmicreach.blocks.BlockPosition; +import finalforeach.cosmicreach.blocks.BlockState; import finalforeach.cosmicreach.gamestates.InGame; import finalforeach.cosmicreach.items.ISlotContainerParent; import finalforeach.cosmicreach.items.ItemSlot; @@ -14,6 +18,7 @@ import finalforeach.cosmicreach.items.screens.BaseItemScreen; import finalforeach.cosmicreach.savelib.utils.TriConsumer; import finalforeach.cosmicreach.ui.UI; import finalforeach.cosmicreach.ui.widgets.ItemSlotWidget; +import finalforeach.cosmicreach.world.Zone; import net.pietru.omni_power.OmniPower; import net.pietru.omni_power.blocks.CustomUIBlockEntity; @@ -25,11 +30,11 @@ import static net.pietru.omni_power.OmniPower.MOD_ID; public class CustomScreen extends BaseItemScreen { public static final Map> components = new HashMap<>(); + public static final Map> button_events = new HashMap<>(); SlotContainer container; - CustomUIBlockEntity blockEntity; - public CustomScreen(ISlotContainerParent parent,final CustomUIBlockEntity be, final String ui_id) { + public CustomScreen(ISlotContainerParent parent, final String ui_id) { super(parent); if (!OmniPower.uis.containsKey(ui_id)) { @@ -40,8 +45,11 @@ public class CustomScreen extends BaseItemScreen { JsonValue dataJson = OmniPower.uis.get(ui_id); JsonValue ui_layout = dataJson.get("layout"); - container = be.slotContainer; - blockEntity = be; + + if (parent instanceof CustomUIBlockEntity) + container=((CustomUIBlockEntity) parent).slotContainer; + else if (parent instanceof DummyObject) + container=((DummyObject) parent).container; Stack stack = new Stack(); Actor background = new Image(UI.container9Patch); @@ -98,14 +106,19 @@ public class CustomScreen extends BaseItemScreen { - public static void addOpenContainer(final CustomUIBlockEntity be, final SlotContainer container){ - addOpenContainer(be,container,be.ui_id); + public static void addOpenContainer(final ISlotContainerParent be, final SlotContainer container){ + String ui_id = ""; + if (be instanceof CustomUIBlockEntity) + ui_id=((CustomUIBlockEntity) be).ui_id; + else if (be instanceof DummyObject) + ui_id= ((DummyObject) be).ui_id; + addOpenContainer(be,container,ui_id); } - public static void addOpenContainer(final CustomUIBlockEntity be, final SlotContainer container, final String ui_id) { + public static void addOpenContainer(final ISlotContainerParent be, final SlotContainer container, final String ui_id) { if (!UI.openContainers.contains(container, true)) { UI.itemCatalog.show(); UI.openContainers.add(container); - final CustomScreen screen = new CustomScreen(be, be, ui_id); + final CustomScreen screen = new CustomScreen(be, ui_id); InGame.IN_GAME.openedMainItemScreen = screen; InGame.IN_GAME.addBaseItemScreen(screen); Action perFrameAct = new Action() { @@ -126,6 +139,66 @@ public class CustomScreen extends BaseItemScreen { } } + public static void load_base_button_events(){ + button_events.put("change_ui",(button,screen,data)->{ + 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.parent,screen.container,ui_id); + return true; + }); + }); + button_events.put("run_trigger",(button,screen,data)->{ + String trigger_id = data.getString("trigger_id",""); + button.addListener(event -> { + if (!button.isPressed()) + return false; + if (screen.parent instanceof DummyObject) + runTrigger(trigger_id,((DummyObject) screen.parent).blockPos, (BlockEventArgs) screen.parent); + return true; + }); + }); + button_events.put("close_ui",(button,screen,data)->{ + 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); + return true; + }); + }); + } + + private static void runTrigger(String triggerId, BlockPosition triggerPos, BlockEventArgs args) { + + Zone zone = args.zone; + if (triggerPos != null) { + BlockState targetBlockState = triggerPos.getBlockState(); + if (targetBlockState == null) { + return; + } + + BlockEventTrigger[] customTrigger = targetBlockState.getTrigger(triggerId); + if (customTrigger == null) { + return; + } + + BlockEventArgs newArgs = new BlockEventArgs(); + newArgs.blockPos = triggerPos; + newArgs.zone = zone; + newArgs.srcBlockState = targetBlockState; + newArgs.srcIdentity = args.srcIdentity; + + for (BlockEventTrigger t : customTrigger) { + t.act(newArgs); + } + } + } + public static void load_base_ui_components() { //Table table, CustomScreen screen components.put("row",(table,screen,data)->{ @@ -144,32 +217,19 @@ public class CustomScreen extends BaseItemScreen { ); table.add(lbl); }); - components.put("change_ui_text_button",(table,screen,data)->{ + components.put("text_button",(table,screen,data)->{ //Button button = new TextButton(data.getString("text",""),skin); TextButtonWidget button = new TextButtonWidget(data.getString("text","")); - 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); - TextButtonWidget button = new TextButtonWidget(data.getString("text","")); - 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; - }); + JsonValue events = data.get("events"); + for (JsonValue event : events) { + String type = event.getString("type", ""); + if (type.isEmpty()) + continue; + if (!button_events.containsKey(type.toLowerCase())) + continue; + TriConsumer component = button_events.get(type.toLowerCase()); + component.accept(button, screen, event); + } table.add(button); }); components.put("item_slot",(table,screen,data)->{ @@ -190,6 +250,7 @@ public class CustomScreen extends BaseItemScreen { } static { + load_base_button_events(); load_base_ui_components(); } } \ No newline at end of file diff --git a/src/main/java/net/pietru/omni_power/ui/DummyObject.java b/src/main/java/net/pietru/omni_power/ui/DummyObject.java new file mode 100644 index 0000000..b3f7208 --- /dev/null +++ b/src/main/java/net/pietru/omni_power/ui/DummyObject.java @@ -0,0 +1,11 @@ +package net.pietru.omni_power.ui; + +import finalforeach.cosmicreach.blockevents.BlockEventArgs; +import finalforeach.cosmicreach.blocks.BlockPosition; +import finalforeach.cosmicreach.items.ISlotContainerParent; +import finalforeach.cosmicreach.items.containers.SlotContainer; + +public class DummyObject extends BlockEventArgs implements ISlotContainerParent { + public String ui_id; + public SlotContainer container; +} diff --git a/src/main/java/net/pietru/omni_power/ui/TextButtonWidget.java b/src/main/java/net/pietru/omni_power/ui/TextButtonWidget.java index 8ad5a6d..02b367e 100644 --- a/src/main/java/net/pietru/omni_power/ui/TextButtonWidget.java +++ b/src/main/java/net/pietru/omni_power/ui/TextButtonWidget.java @@ -90,6 +90,7 @@ public class TextButtonWidget extends Table implements Disableable { protected boolean onInputEvent(InputEvent inputEvent) { if (Gdx.input.isCursorCatched()) { + this.pressed = false; return false; } else { InputEvent.Type inputType = inputEvent.getType(); @@ -111,6 +112,7 @@ public class TextButtonWidget extends Table implements Disableable { if (inputType == InputEvent.Type.exit) { this.canSpreadItemTo = true; this.hoveredOver = false; + this.pressed = false; } return false; diff --git a/src/main/resources/omni_power/assets.txt b/src/main/resources/omni_power/assets.txt index 43de21f..1dd30dc 100644 --- a/src/main/resources/omni_power/assets.txt +++ b/src/main/resources/omni_power/assets.txt @@ -2,9 +2,11 @@ omni_power:assets.txt omni_power:blocks\cable.json omni_power:blocks\cable_adapter.json omni_power:blocks\cable_merger.json +omni_power:blocks\custom_boombox.json omni_power:blocks\custom_crate.json omni_power:blocks\door_cube.json omni_power:blocks\lever.json +omni_power:block_events\block_events_boombox.json omni_power:block_events\block_events_cable_merger_off.json omni_power:block_events\block_events_cable_merger_on.json omni_power:block_events\block_events_cable_merger_slim_off.json @@ -15,6 +17,7 @@ omni_power:block_events\block_events_lever_blue_off.json omni_power:block_events\block_events_lever_blue_on.json omni_power:block_events\block_events_lever_red_off.json omni_power:block_events\block_events_lever_red_on.json +omni_power:gui\example-boombox.json omni_power:gui\example.json omni_power:gui\example2.json omni_power:block_events\adapter\cable_adapter_blue_off.json diff --git a/src/main/resources/omni_power/block_events/block_events_boombox.json b/src/main/resources/omni_power/block_events/block_events_boombox.json new file mode 100644 index 0000000..92707a5 --- /dev/null +++ b/src/main/resources/omni_power/block_events/block_events_boombox.json @@ -0,0 +1,44 @@ +{ + "parent":"base:block_events_default", + "stringId": "omni_power:block_events_boombox", + "triggers": + { + "onInteract": + [ + { + "actionId": "omni_power:open_ui", + "parameters": + { + "ui_id": "example_boombox", + "slots": 0 + } + } + ], + "sound1": + [ + { + "actionId": "base:play_sound_3d", + "parameters": + { + "sound": "base:sounds/blocks/e-piano_A.ogg", + "volume": 1, + "pitch": 1, + "position": [0, 0, 0] + } + } + ], + "sound2": + [ + { + "actionId": "base:play_sound_3d", + "parameters": + { + "sound": "base:sounds/blocks/e-piano_A.ogg", + "volume": 1, + "pitch": 2, + "position": [0, 0, 0] + } + } + ] + } +} \ No newline at end of file diff --git a/src/main/resources/omni_power/blocks/custom_boombox.json b/src/main/resources/omni_power/blocks/custom_boombox.json new file mode 100644 index 0000000..d99ffc1 --- /dev/null +++ b/src/main/resources/omni_power/blocks/custom_boombox.json @@ -0,0 +1,14 @@ +{ + "stringId": "omni_power:boombox", + "blockStates": + { + "pitch=A4": + { + "modelName": "base:models/blocks/model_boombox.json", + "blockEventsId": "omni_power:block_events_boombox", + "tags": [ + "tool_pickaxe_effective" + ] + } + } +} \ No newline at end of file diff --git a/src/main/resources/omni_power/gui/example-boombox.json b/src/main/resources/omni_power/gui/example-boombox.json new file mode 100644 index 0000000..6c2924e --- /dev/null +++ b/src/main/resources/omni_power/gui/example-boombox.json @@ -0,0 +1,43 @@ +{ + "id": "example_boombox", + "width": 250, + "height": 200, + "layout": [ + { + "type": "label", + "text": "BoomBox", + "x_off": 0, + "y_off": -2 + }, + { + "type": "row" + }, + { + "type": "space", + "height": 10 + }, + { + "type": "row" + }, + { + "type": "text_button", + "text": "Sound1", + "events": [ + { + "type": "run_trigger", + "trigger_id": "sound1" + } + ] + }, + { + "type": "text_button", + "text": "Sound2", + "events": [ + { + "type": "run_trigger", + "trigger_id": "sound2" + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/resources/omni_power/gui/example.json b/src/main/resources/omni_power/gui/example.json index ef3afb6..2e966f7 100644 --- a/src/main/resources/omni_power/gui/example.json +++ b/src/main/resources/omni_power/gui/example.json @@ -41,9 +41,14 @@ "output": true }, { - "type": "change_ui_text_button", + "type": "text_button", "text": "Second UI", - "gui_id": "example_ui_2" + "events": [ + { + "type": "change_ui", + "gui_id": "example_ui_2" + } + ] } ] } \ No newline at end of file