Skip to content

Commit

Permalink
feat(ui): add interaction to storage limit ui (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Mar 10, 2024
1 parent 5787234 commit 17ae74e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
73 changes: 63 additions & 10 deletions plugin/src/input/ui/InventoryInteractableUi.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
using System.Linq;
using PiUtils.Util;
using UnityEngine;
using UnityEngine.EventSystems;

namespace TechtonicaVR.Input.Ui;

public class InventoryInteractableUi : InteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<InventoryInteractableUi>();
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<InventoryGridUI>();
getInteractables = () => inv.ui.slots.Select(getInteractable).ToList();
var inv = transform.gameObject.GetComponentInChildren<InventoryGridUI>() ?? transform.gameObject.GetComponentInParent<InventoryGridUI>();
getInteractables = () => inv.ui.slots.Select(getInteractable)
.Concat(transform.gameObject.GetComponentsInChildren<UnityEngine.UI.Button>().Select(getButtonInteractable))
.ToList();
}

protected Interactable getInteractable(InventoryResourceSlotUI slot, int index)
Expand All @@ -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<RectTransform>();
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();
}

Expand All @@ -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)
Expand Down Expand Up @@ -129,4 +172,14 @@ protected void onReceiveDrop(object sourceObject, InventoryResourceSlotUI slot)
return;
}
}

private bool isSettingLimit()
{
if (chestUi == null)
{
return false;
}

return chestUi.settingLimit;
}
}
2 changes: 1 addition & 1 deletion plugin/src/patch/main_game/ui/StorageInventoryUIPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 17ae74e

Please sign in to comment.