-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(input): add interaction to filter inserter (#86)
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
plugin/src/input/ui/machine/FilterInserterInteractableUi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |