Skip to content

Commit

Permalink
feat(input): add interaction to recipe picker (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Jan 26, 2024
1 parent f235fc0 commit 30115bc
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 61 deletions.
1 change: 0 additions & 1 deletion plugin/src/MainGamePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ void Start()
new IaCMenuPatch(),
new SaveNotificationPatch(),
new HudPatch(),
new FilterInserterPatch(),
]).ToArray();

Logger.LogDebug("Hello World!");
Expand Down
25 changes: 22 additions & 3 deletions plugin/src/input/ui/InteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class InteractableUi
public UiEnterEvent OnEnterEvent;
public UiExitEvent OnExitEvent;
public Menu menu;
public Func<List<Interactable>> getInteractables;

public Transform transform;
public float zIndex = 0;
Expand Down Expand Up @@ -99,15 +100,32 @@ public Interactable getInteractable(Vector2 point)
return interactable.Where(i => i.isHit(point) && i.gameObject.activeInHierarchy).FirstOrDefault();
}

public void RecalculateInteractablePositions()
public bool rebuildInteractables()
{
if (getInteractables == null)
{
return false;
}

interactable = getInteractables();
return true;
}

public void recalculateInteractablePositions()
{
interactable.ForEach(i => i.recalculate());
}

public void OnEnter()
{
Logger.LogDebug($"OnEnter {transform.gameObject.name}");
RecalculateInteractablePositions();
AsyncGameObject.Instance.timeoutFrames(() =>
{
if (!rebuildInteractables())
{
recalculateInteractablePositions();
}
}, 1);
OnEnterEvent?.Invoke();
}
public void OnExit()
Expand All @@ -127,12 +145,13 @@ private Plane getUiPlane()

protected Rect getRect(RectTransform rectTransform)
{
var rect = rectTransform.rect;
var rect = new Rect(rectTransform.rect);

var relativeLocalPosition = ObjectPosition.addLocalPositions(rectTransform, this.rectTransform);

rect.x += relativeLocalPosition.x;
rect.y += relativeLocalPosition.y;
Logger.LogDebug($"getRect {rectTransform.gameObject.name} {rect} {relativeLocalPosition}");
return rect;
}
}
Expand Down
17 changes: 5 additions & 12 deletions plugin/src/input/ui/machine/DrillInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public DrillInteractableUi(GameObject gameObject) : base(gameObject)

interactable = [
getInteractable(outputResourceSlot),
getInteractable(fuelResourceSlot, new Vector2(0, 95)),
getInteractable(fuelResourceSlot),
];
Logger.LogDebug($"Interactable: {rectTransform.rect}");
}
Expand All @@ -27,21 +27,14 @@ protected override void init()
{
}

private Interactable getInteractable(InventoryResourceSlotUI slot, Vector2 offset = default)
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 + offset.x;
rect.y += relativeLocalPosition.y + offset.y;
Logger.LogDebug($"Slot rect: {slot} {rect} {relativeLocalPosition} {rectTransform.localPosition}");
var rect = getRect(rectTransform);
Logger.LogDebug($"Slot rect: {slot} {rect} {rectTransform.localPosition}");

return new InteractableBuilder(this, rect, rectTransform.gameObject)
.withRecalculate(() => getRect(rectTransform))
.withDrag(() => draggedResourceInfo ?? slot.resourceType,
(ui) => onDrag(slot),
(ui, source, target) => onDrop(ui, target, slot),
Expand Down
26 changes: 4 additions & 22 deletions plugin/src/input/ui/machine/FilterInserterInteractableUi.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using TechtonicaVR.Util;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;

namespace TechtonicaVR.Input.Ui.Machine;

Expand All @@ -11,32 +10,15 @@ public class FilterInserterInteractableUi : InteractableUi

public FilterInserterInteractableUi(GameObject gameObject) : base(gameObject)
{
interactable = gameObject.GetComponentsInChildren<ResourceSlotUI>().Select(getInteractable).ToList();
getInteractables = () => gameObject.GetComponentsInChildren<ResourceSlotUI>().Select(getInteractable).ToList();
}

private Interactable getInteractable(ResourceSlotUI slot, int index)
{
Logger.LogDebug($"Slot: {slot} {slot.gameObject.name} {slot.gameObject.transform.localPosition}");
var rectTransform = slot.GetComponent<RectTransform>();
var rect = rectTransform.rect;

var scrollView = slot.GetComponentInParent<ScrollRect>();
if (scrollView != null)
{
// Get the scroll position
var scrollPosition = new Vector2(scrollView.horizontalNormalizedPosition, scrollView.verticalNormalizedPosition);
var scrollOffset = new Vector2(scrollPosition.x * scrollView.content.rect.width, scrollPosition.y * scrollView.content.rect.height);
var scrollTransformation = rectTransform.InverseTransformPoint(scrollView.content.TransformPoint(scrollOffset));

// Adjust the local position by the scroll position
rect.x += scrollTransformation.x + rectTransform.localPosition.x;
rect.y += scrollTransformation.y + rectTransform.localPosition.y;
}

var relativeLocalPosition = ObjectPosition.addLocalPositions(slot.transform, 4);

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

return new InteractableBuilder(this, rect, rectTransform.gameObject)
.withClick((ui) => onClick(slot))
Expand Down
46 changes: 46 additions & 0 deletions plugin/src/input/ui/machine/RecipePickerInteractableUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using TechtonicaVR.Util;
using UnityEngine;
using System.Linq;

namespace TechtonicaVR.Input.Ui.Machine;

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

public RecipePickerInteractableUi(GameObject gameObject) : base(gameObject)
{
zIndex = -0.001f;
interactable = gameObject.GetComponentsInChildren<ResourceSlotUI>().Select(getInteractable).ToList();
}

private Interactable getInteractable(ResourceSlotUI slot, int index)
{
Logger.LogDebug($"Slot: {slot} {slot.gameObject.name} {slot.gameObject.transform.localPosition}");
var rectTransform = slot.GetComponent<RectTransform>();
var rect = getRect(rectTransform);
Logger.LogDebug($"Slot rect: {slot} {rect} {rectTransform.localPosition}");

return new InteractableBuilder(this, rect, rectTransform.gameObject)
.withRecalculate(() => getRect(rectTransform))
.withClick((ui) => onClick(slot))
.withHoverEnter((ui) => onHoverEnter(slot))
.withHoverExit((ui) => onHoverExit(slot))
.build();
}

private void onClick(ResourceSlotUI slot)
{
slot.mouseLeftClickCallback.Invoke();
}

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

private void onHoverExit(ResourceSlotUI slot)
{
slot.mouseExitCallback.Invoke();
}
}
11 changes: 2 additions & 9 deletions plugin/src/input/ui/machine/SmelterInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,11 @@ 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;
var rect = getRect(rectTransform);
Logger.LogDebug($"Slot rect: {slot} {rect} {rectTransform.localPosition}");

return new InteractableBuilder(this, rect, rectTransform.gameObject)
.withRecalculate(() => getRect(rectTransform))
.withDrag(() => draggedResourceInfo ?? slot.resourceType,
(ui) => onDrag(slot),
(ui, source, target) => onDrop(ui, target, slot),
Expand Down
13 changes: 0 additions & 13 deletions plugin/src/patch/main_game/ui/FilterInserterPatch.cs

This file was deleted.

17 changes: 17 additions & 0 deletions plugin/src/ui/RecipePickerMenuWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace TechtonicaVR.UI;

public class RecipePickerMenuWrapper : Menu
{

private RecipePickerUI menu;

public RecipePickerMenuWrapper(RecipePickerUI menu)
{
this.menu = menu;
}

public bool isOpen()
{
return menu.myCanvas.enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public UIMenuWrapper(UIMenu menu)

public bool isOpen()
{
return menu.isOpen;
return menu.isOpen && !UIManager.instance.recipePickerUI.myCanvas.enabled;
}
}
17 changes: 17 additions & 0 deletions plugin/src/ui/patch/UiMenuPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ private static void addInteractableUi(UIMenu instance)
menu = new UIMenuWrapper(instance)
};
}
else if (instance is FilterInserterUI)
{
var container = GameObjectFinder.FindChildObjectByName("Filter Options Container", instance.gameObject);
new FilterInserterInteractableUi(container)
{
menu = new UIMenuWrapper(instance)
};
}
}

private static void disableButtonPrompts(UIMenu __instance)
Expand Down Expand Up @@ -215,6 +223,15 @@ public static void RecipePickerUIStartPostfix(RecipePickerUI __instance)
canvas.transform.SetParent(getWorldAnchor().transform, true);
canvas.transform.localPosition = Vector3.zero;
canvas.transform.localScale = ModConfig.menuScale.Value;

AsyncGameObject.Instance.timeoutFrames(() =>
{
var container = GameObjectFinder.FindChildObjectByName("Container", __instance.gameObject);
new RecipePickerInteractableUi(container)
{
menu = new RecipePickerMenuWrapper(__instance)
};
}, 10);
}

private static void destroyBlur(GameObject blur)
Expand Down

0 comments on commit 30115bc

Please sign in to comment.