Skip to content

Commit

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

Logger.LogDebug("Hello World!");
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/input/ui/InteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public UiRaycastHit Raycast(Ray ray)

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

Expand Down
62 changes: 62 additions & 0 deletions plugin/src/input/ui/machine/FilterInserterInteractableUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using TechtonicaVR.Util;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;

namespace TechtonicaVR.Input.Ui.Machine;

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

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

private Interactable getInteractable(ResourceSlotUI slot, int index)
{
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}");

return new InteractableBuilder(this, rect, rectTransform.gameObject)
.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();
}
}
13 changes: 13 additions & 0 deletions plugin/src/patch/main_game/ui/FilterInserterPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using TechtonicaVR.Input.Ui.Machine;
using TechtonicaVR.Util;

namespace TechtonicaVR.Patches.MainGame.UI;

public class FilterInserterPatch : GameComponentPatch<FilterInserterUI>
{
protected override bool Apply(FilterInserterUI component)
{
new FilterInserterInteractableUi(GameObjectFinder.FindChildObjectByName("Filter Options Container", component.gameObject));
return true;
}
}
37 changes: 37 additions & 0 deletions plugin/src/util/ObjectPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using UnityEngine;

namespace TechtonicaVR.Util;

public class ObjectPosition
{
private static PluginLogger Logger = PluginLogger.GetLogger<ObjectPosition>();

public static Vector3 addLocalPositions(Transform transform, Transform stopAt = null)
{
var position = transform.localPosition;
while (transform.parent != null && transform.parent != stopAt)
{
transform = transform.parent;
Logger.LogDebug($"Adding {transform.localPosition} from {transform.gameObject.name} to {position}");
position += transform.localPosition;
}
return position;
}

public static Vector3 addLocalPositions(Transform transform, int stopAt)
{
var position = transform.localPosition;
while (transform.transform.parent != null && stopAt > 0)
{
transform = transform.parent;
Logger.LogDebug($"Adding {transform.localPosition} from {transform.gameObject.name} to {position}");
position += transform.localPosition;
stopAt--;
}
if (stopAt > 0)
{
Logger.LogWarning($"Ran out of parents to add to {transform.gameObject.name} ({stopAt} remaining)");
}
return position;
}
}

0 comments on commit 0231e33

Please sign in to comment.