Skip to content

Commit

Permalink
feat(input): add interaction to smelter menu (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Jan 23, 2024
1 parent 0c0b978 commit 2016a51
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
6 changes: 4 additions & 2 deletions plugin/src/input/ui/InteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public abstract class InteractableUi
public Menu menu;

public Transform transform;
public float zIndex = 0;
protected ScrollRect scrollRect;
private RectTransform rectTransform;
protected RectTransform rectTransform;
private Canvas canvas;

public InteractableUi(GameObject gameObject)
Expand Down Expand Up @@ -85,14 +86,15 @@ public UiRaycastHit Raycast(Ray ray)
}

var scrollOffset = scrollRect?.m_PrevPosition ?? Vector2.zero;
return new UiRaycastHit(this, distance, point, localPoint + scrollOffset);
return new UiRaycastHit(this, distance + zIndex, point, localPoint + scrollOffset);
}

return null;
}

public Interactable getInteractable(Vector2 point)
{
// Logger.LogDebug($"getInteractable {transform.gameObject.name} {point}");
return interactable.Where(i => i.isHit(point)).FirstOrDefault();
}

Expand Down
25 changes: 16 additions & 9 deletions plugin/src/input/ui/InventoryInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ namespace TechtonicaVR.Input.Ui;
public class InventoryInteractableUI : InteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<InventoryInteractableUI>();
private ResourceInfo draggedResourceInfo;
protected ResourceInfo draggedResourceInfo;
private int draggedResourceCount;
private Transform viewport;

public InventoryInteractableUI(GameObject gameObject) : base(gameObject)
{
init();
}

protected virtual void init()
{
var inv = transform.gameObject.GetComponentInParent<InventoryGridUI>();
interactable = inv.ui.slots.Select(getInteractable).ToList();
Expand Down Expand Up @@ -42,12 +47,12 @@ private Interactable getInteractable(InventoryResourceSlotUI slot, int index)
.build();
}

private void onHoverEnter(InventoryResourceSlotUI slot)
protected void onHoverEnter(InventoryResourceSlotUI slot)
{
slot.mouseEnterCallback.Invoke();
}

private void onHoverExit(InventoryResourceSlotUI slot)
protected void onHoverExit(InventoryResourceSlotUI slot)
{
slot.mouseExitCallback.Invoke();
}
Expand All @@ -57,17 +62,17 @@ private void onClick(InventoryResourceSlotUI slot)
// uiSlot.mouseLeftClickCallback.Invoke();
}

private void onDrag(InventoryResourceSlotUI slot)
protected void onDrag(InventoryResourceSlotUI slot)
{
Logger.LogDebug($"Dragged toolbar slot {slot.resourceType?.name}");
draggedResourceInfo = slot.resourceType;
draggedResourceCount = slot.resourceQuantity;
slot.mouseLeftClickCallback();
}

private void onDrop(InteractableUi ui, Interactable target, InventoryResourceSlotUI sourceSlot)
protected void onDrop(InteractableUi ui, Interactable target, InventoryResourceSlotUI sourceSlot)
{
Logger.LogDebug($"Dropped toolbar slot {sourceSlot.resourceType?.name}");
Logger.LogDebug($"Dropped inventory slot {sourceSlot.resourceType?.name}");
var droppedResourceInfo = draggedResourceInfo;
draggedResourceInfo = null;

Expand All @@ -87,6 +92,7 @@ private void onDrop(InteractableUi ui, Interactable target, InventoryResourceSlo
}

targetSlot.mouseLeftClickCallback.Invoke();
MouseCursorBuffer.instance.TryCancel();
return;
}

Expand All @@ -100,13 +106,13 @@ private void onDrop(InteractableUi ui, Interactable target, InventoryResourceSlo
onCancelDrag(sourceSlot);
}

private void onCancelDrag(InventoryResourceSlotUI slot)
protected void onCancelDrag(InventoryResourceSlotUI slot)
{
slot.mouseLeftClickCallback.Invoke();
draggedResourceInfo = null;
}

private void onAcceptsDrop(AcceptDropEventArgs args)
protected void onAcceptsDrop(AcceptDropEventArgs args)
{
if (args.source.ui is not InventoryInteractableUI)
{
Expand All @@ -122,8 +128,9 @@ private void onAcceptsDrop(AcceptDropEventArgs args)
args.accept |= true;
}

private void onReceiveDrop(object sourceObject, InventoryResourceSlotUI slot)
protected void onReceiveDrop(object sourceObject, InventoryResourceSlotUI slot)
{

var resourceInfo = sourceObject as ResourceInfo;
if (resourceInfo == null)
{
Expand Down
57 changes: 57 additions & 0 deletions plugin/src/input/ui/machine/SmelterInteractableUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using TechtonicaVR.Util;
using UnityEngine;

namespace TechtonicaVR.Input.Ui.Machine;

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

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

var inputResourceSlot = GameObjectFinder.FindChildObjectByName("Input Resource Slot", gameObject)
.GetComponent<InventoryResourceSlotUI>();
var outputResourceSlot = GameObjectFinder.FindChildObjectByName("Output Resource Slot", gameObject)
.GetComponent<InventoryResourceSlotUI>();
var fuelResourceSlot = GameObjectFinder.FindChildObjectByName("Fuel Resource Slot", gameObject)
.GetComponent<InventoryResourceSlotUI>();

interactable = [
getInteractable(inputResourceSlot),
getInteractable(outputResourceSlot),
getInteractable(fuelResourceSlot),
];
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 = this.rectTransform.parent;
rectTransform.parent = this.rectTransform;
var relativeLocalPosition = rectTransform.localPosition;
rectTransform.parent = originalParent;

rect.x += relativeLocalPosition.x;
rect.y += relativeLocalPosition.y;
Logger.LogDebug($"Output 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();
}
}
16 changes: 16 additions & 0 deletions plugin/src/ui/patch/UiMenuPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using TechtonicaVR.Util;
using TechtonicaVR.VRCamera;
using UnityEngine;
using UnityEngine.UI;

namespace TechtonicaVR.UI.Patch;

Expand Down Expand Up @@ -44,6 +45,7 @@ public static void StartPostfix(UIMenu __instance)
}
else
{
canvas.GetComponent<CanvasScaler>().enabled = false;
canvas.transform.localScale = ModConfig.menuScale.Value;
}

Expand All @@ -61,6 +63,14 @@ private static void addInteractableUi(UIMenu instance)
menu = new UIMenuWrapper(instance)
};
}
else if (instance is SmelterUI)
{
var container = GameObjectFinder.FindChildObjectByName("Container", instance.gameObject);
new SmelterInteractableUi(container)
{
menu = new UIMenuWrapper(instance)
};
}
}

private static void disableButtonPrompts(UIMenu __instance)
Expand Down Expand Up @@ -88,6 +98,12 @@ private static void disableButtonPrompts(UIMenu __instance)
{
buttonPrompt.gameObject.SetActive(false);
}

var simpleInventoryButtonPrompts = GameObjectFinder.FindChildObjectsByName("Inventory Button Prompts", __instance.gameObject);
foreach (var buttonPrompt in simpleInventoryButtonPrompts)
{
buttonPrompt.gameObject.SetActive(false);
}
}

private static GameObject findTlc(GameObject gameObject)
Expand Down

0 comments on commit 2016a51

Please sign in to comment.