From 17ae74e22c160a6a633b4293e32d00967ecde1b6 Mon Sep 17 00:00:00 2001 From: Xenira <1288524+Xenira@users.noreply.github.com> Date: Sun, 10 Mar 2024 19:34:17 +0100 Subject: [PATCH] feat(ui): add interaction to storage limit ui (#137) --- ..._shader_patch.cs => OutlineShaderPatch.cs} | 0 .../patch/PlayerFirstPersonControllerPatch.cs | 2 +- ...h.cs => TerrainManipulatorRaycastPatch.cs} | 0 ...ble_patch.cs => BuilderReassemblePatch.cs} | 0 ...ilder_patch.cs => ConveyorBuilderPatch.cs} | 0 ...der_patch.cs => MultiPlaceBuilderPatch.cs} | 0 .../src/input/ui/InventoryInteractableUi.cs | 73 ++++++++++++++++--- .../main_game/ui/StorageInventoryUIPatch.cs | 2 +- 8 files changed, 65 insertions(+), 12 deletions(-) rename plugin/src/camera/patch/{outline_shader_patch.cs => OutlineShaderPatch.cs} (100%) rename plugin/src/camera/patch/{terrain_manipulator_raycast_patch.cs => TerrainManipulatorRaycastPatch.cs} (100%) rename plugin/src/camera/patch/builder/{builder_reassemble_patch.cs => BuilderReassemblePatch.cs} (100%) rename plugin/src/camera/patch/builder/{conveyor_builder_patch.cs => ConveyorBuilderPatch.cs} (100%) rename plugin/src/camera/patch/builder/{multi_place_builder_patch.cs => MultiPlaceBuilderPatch.cs} (100%) diff --git a/plugin/src/camera/patch/outline_shader_patch.cs b/plugin/src/camera/patch/OutlineShaderPatch.cs similarity index 100% rename from plugin/src/camera/patch/outline_shader_patch.cs rename to plugin/src/camera/patch/OutlineShaderPatch.cs diff --git a/plugin/src/camera/patch/PlayerFirstPersonControllerPatch.cs b/plugin/src/camera/patch/PlayerFirstPersonControllerPatch.cs index b9c473b..73b445e 100644 --- a/plugin/src/camera/patch/PlayerFirstPersonControllerPatch.cs +++ b/plugin/src/camera/patch/PlayerFirstPersonControllerPatch.cs @@ -17,7 +17,7 @@ static bool AutoCrouchPatch() [HarmonyPatch(typeof(PlayerFirstPersonController), nameof(PlayerFirstPersonController.UpdateThirdPersonView))] public static bool UpdateThirdPersonView() { - if (Player.instance.networkedPlayer) + if (Player.instance.networkedPlayer && VRCameraManager.mainCamera != null) { Player.instance.networkedPlayer.display.transform.position = Player.instance.transform.position; Player.instance.networkedPlayer.display.transform.rotation = Quaternion.Euler(0f, VRCameraManager.mainCamera.transform.rotation.eulerAngles.y, 0f); diff --git a/plugin/src/camera/patch/terrain_manipulator_raycast_patch.cs b/plugin/src/camera/patch/TerrainManipulatorRaycastPatch.cs similarity index 100% rename from plugin/src/camera/patch/terrain_manipulator_raycast_patch.cs rename to plugin/src/camera/patch/TerrainManipulatorRaycastPatch.cs diff --git a/plugin/src/camera/patch/builder/builder_reassemble_patch.cs b/plugin/src/camera/patch/builder/BuilderReassemblePatch.cs similarity index 100% rename from plugin/src/camera/patch/builder/builder_reassemble_patch.cs rename to plugin/src/camera/patch/builder/BuilderReassemblePatch.cs diff --git a/plugin/src/camera/patch/builder/conveyor_builder_patch.cs b/plugin/src/camera/patch/builder/ConveyorBuilderPatch.cs similarity index 100% rename from plugin/src/camera/patch/builder/conveyor_builder_patch.cs rename to plugin/src/camera/patch/builder/ConveyorBuilderPatch.cs diff --git a/plugin/src/camera/patch/builder/multi_place_builder_patch.cs b/plugin/src/camera/patch/builder/MultiPlaceBuilderPatch.cs similarity index 100% rename from plugin/src/camera/patch/builder/multi_place_builder_patch.cs rename to plugin/src/camera/patch/builder/MultiPlaceBuilderPatch.cs diff --git a/plugin/src/input/ui/InventoryInteractableUi.cs b/plugin/src/input/ui/InventoryInteractableUi.cs index 3deb204..c9f4d2f 100644 --- a/plugin/src/input/ui/InventoryInteractableUi.cs +++ b/plugin/src/input/ui/InventoryInteractableUi.cs @@ -1,6 +1,7 @@ using System.Linq; using PiUtils.Util; using UnityEngine; +using UnityEngine.EventSystems; namespace TechtonicaVR.Input.Ui; @@ -8,17 +9,21 @@ public class InventoryInteractableUi : InteractableUi { private static PluginLogger Logger = PluginLogger.GetLogger(); protected ResourceInfo draggedResourceInfo; + private InventoryNavigator chestUi; private int draggedResourceCount; - public InventoryInteractableUi(GameObject gameObject) : base(gameObject) + public InventoryInteractableUi(GameObject gameObject, InventoryNavigator chestUi = null) : base(gameObject) { + this.chestUi = chestUi; init(); } protected virtual void init() { - var inv = transform.gameObject.GetComponentInParent(); - getInteractables = () => inv.ui.slots.Select(getInteractable).ToList(); + var inv = transform.gameObject.GetComponentInChildren() ?? transform.gameObject.GetComponentInParent(); + getInteractables = () => inv.ui.slots.Select(getInteractable) + .Concat(transform.gameObject.GetComponentsInChildren().Select(getButtonInteractable)) + .ToList(); } protected Interactable getInteractable(InventoryResourceSlotUI slot, int index) @@ -28,13 +33,27 @@ protected Interactable getInteractable(InventoryResourceSlotUI slot, int index) return new InteractableBuilder(this, rect, rectTransform.gameObject) .withRecalculate(() => getRect(rectTransform)) + .withClick((_ui) => onClick(slot), () => isClickable()) .withDrag(() => draggedResourceInfo ?? slot.resourceType, - (ui) => onDrag(slot), - (ui, source, target) => onDrop(ui, target, slot), - (ui) => onCancelDrag(slot)) - .withDrop(onAcceptsDrop, (ui, source) => onReceiveDrop(source, slot)) - .withHoverEnter((ui) => onHoverEnter(slot)) - .withHoverExit((ui) => onHoverExit(slot)) + (_ui) => onDrag(slot), + (ui, _source, target) => onDrop(ui, target, slot), + (_ui) => onCancelDrag(slot)) + .withDrop(onAcceptsDrop, (_ui, source) => onReceiveDrop(source, slot)) + .withHoverEnter((_ui) => onHoverEnter(slot)) + .withHoverExit((_ui) => onHoverExit(slot)) + .build(); + } + + protected Interactable getButtonInteractable(UnityEngine.UI.Button button, int index) + { + var rectTransform = button.GetComponent(); + var rect = getRect(rectTransform); + + return new InteractableBuilder(this, rect, rectTransform.gameObject) + .withRecalculate(() => getRect(rectTransform)) + .withClick((_ui) => onButtonClick(button)) + .withHoverEnter((_ui) => onButtonHoverEnter(button)) + .withHoverExit((_ui) => onButtonHoverExit(button)) .build(); } @@ -43,14 +62,38 @@ protected void onHoverEnter(InventoryResourceSlotUI slot) slot.mouseEnterCallback?.Invoke(); } + protected void onButtonHoverEnter(UnityEngine.UI.Button button) + { + button.OnPointerEnter(new PointerEventData(EventSystem.current)); + } + protected void onHoverExit(InventoryResourceSlotUI slot) { slot.mouseExitCallback?.Invoke(); } + protected void onButtonHoverExit(UnityEngine.UI.Button button) + { + button.OnPointerExit(new PointerEventData(EventSystem.current)); + } + private void onClick(InventoryResourceSlotUI slot) { - // uiSlot.mouseLeftClickCallback.Invoke(); + if (!isSettingLimit()) + { + return; + } + slot.mouseLeftClickCallback?.Invoke(); + } + + private bool isClickable() + { + return isSettingLimit(); + } + + private void onButtonClick(UnityEngine.UI.Button button) + { + button.onClick?.Invoke(); } protected void onDrag(InventoryResourceSlotUI slot) @@ -129,4 +172,14 @@ protected void onReceiveDrop(object sourceObject, InventoryResourceSlotUI slot) return; } } + + private bool isSettingLimit() + { + if (chestUi == null) + { + return false; + } + + return chestUi.settingLimit; + } } diff --git a/plugin/src/patch/main_game/ui/StorageInventoryUIPatch.cs b/plugin/src/patch/main_game/ui/StorageInventoryUIPatch.cs index 877180b..73c75b4 100644 --- a/plugin/src/patch/main_game/ui/StorageInventoryUIPatch.cs +++ b/plugin/src/patch/main_game/ui/StorageInventoryUIPatch.cs @@ -18,7 +18,7 @@ protected override bool Apply(GameObject component) return false; } - new InventoryInteractableUi(storageInventory.gameObject); + new InventoryInteractableUi(storageInventory.gameObject.transform.parent.gameObject, UIManager.instance.inventoryAndStorageMenu); return true; }