Skip to content

Commit

Permalink
feat(input): add interaction to thresher (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Jan 26, 2024
1 parent 0231e33 commit 54fb1ad
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
47 changes: 47 additions & 0 deletions plugin/src/input/ui/machine/ThresherInteractableUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Linq;
using TechtonicaVR.Util;
using UnityEngine;

namespace TechtonicaVR.Input.Ui.Machine;

public class ThresherInteractableUi : InventoryInteractableUI
{
private static PluginLogger Logger = PluginLogger.GetLogger<ThresherInteractableUi>();

public ThresherInteractableUi(GameObject gameObject) : base(gameObject)
{
zIndex = 0.001f;

interactable = gameObject.GetComponentsInChildren<InventoryResourceSlotUI>().Select(getInteractable).ToList();
Logger.LogDebug($"Interactable: {rectTransform.rect}");
}

protected override void init()
{
}

private Interactable getInteractable(InventoryResourceSlotUI slot)
{
var rectTransform = slot.GetComponent<RectTransform>();
var rect = rectTransform.rect;

var originalParent = rectTransform.parent;
rectTransform.parent = this.rectTransform;
var relativeLocalPosition = rectTransform.localPosition;
rectTransform.parent = originalParent;

rect.x += relativeLocalPosition.x;
rect.y += relativeLocalPosition.y;
Logger.LogDebug($"Slot rect: {slot} {rect} {rectTransform.localPosition}");

return new InteractableBuilder(this, rect, rectTransform.gameObject)
.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))
.build();
}
}
8 changes: 8 additions & 0 deletions plugin/src/ui/patch/UiMenuPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ private static void addInteractableUi(UIMenu instance)
menu = new UIMenuWrapper(instance)
};
}
else if (instance is ThresherUI)
{
var container = GameObjectFinder.FindChildObjectByName("Container", instance.gameObject);
new ThresherInteractableUi(container)
{
menu = new UIMenuWrapper(instance)
};
}
}

private static void disableButtonPrompts(UIMenu __instance)
Expand Down

0 comments on commit 54fb1ad

Please sign in to comment.