Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(input): add interaction to assembler (#89)
- Loading branch information
Showing
4 changed files
with
203 additions
and
4 deletions.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using System.Linq; | ||
| using TechtonicaVR.Util; | ||
| using UnityEngine; | ||
|
|
||
| namespace TechtonicaVR.Input.Ui.Machine; | ||
|
|
||
| public class AssemblerInteractableUi : InventoryInteractableUI | ||
| { | ||
| private static PluginLogger Logger = PluginLogger.GetLogger<AssemblerInteractableUi>(); | ||
|
|
||
| public AssemblerInteractableUi(GameObject gameObject) : base(gameObject) | ||
| { | ||
| zIndex = 0.001f; | ||
|
|
||
| interactable = GameObjectFinder.FindChildObjectByName("Container", 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 = 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), | ||
| (ui) => onCancelDrag(slot)) | ||
| .withDrop(onAcceptsDrop, (ui, source) => onReceiveDrop(source, slot)) | ||
| .withHoverEnter((ui) => onHoverEnter(slot)) | ||
| .withHoverExit((ui) => onHoverExit(slot)) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| public class AssemblerRecipeSelectInteractableUi : InventoryInteractableUI | ||
| { | ||
| public AssemblerRecipeSelectInteractableUi(GameObject gameObject) : base(gameObject) | ||
| { | ||
| var recipeSelector = gameObject.GetComponentInChildren<InventoryResourceSlotUI>(); | ||
| interactable = [ | ||
| new InteractableBuilder(this, getRect(recipeSelector.GetComponent<RectTransform>()), recipeSelector.gameObject) | ||
| .withClick((ui) => onClick(recipeSelector)) | ||
| .withHoverEnter((ui) => onHoverEnter(recipeSelector)) | ||
| .withHoverExit((ui) => onHoverExit(recipeSelector)) | ||
| .build() | ||
| ]; | ||
| } | ||
|
|
||
| protected override void init() | ||
| { | ||
| } | ||
|
|
||
| private void onClick(InventoryResourceSlotUI recipeSelector) | ||
| { | ||
| recipeSelector.mouseLeftClickCallback.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
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,78 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using UnityEngine; | ||
|
|
||
| namespace TechtonicaVR.Util; | ||
|
|
||
| public class Async | ||
| { | ||
| public static IEnumerator Timeout(Action callback, float seconds) | ||
| { | ||
| yield return new WaitForSeconds(seconds); | ||
| callback(); | ||
| } | ||
|
|
||
| public static IEnumerator TimeoutFrames(Action callback, int frames) | ||
| { | ||
| for (int i = 0; i < frames; i++) | ||
| { | ||
| yield return new WaitForEndOfFrame(); | ||
| } | ||
| callback(); | ||
| } | ||
|
|
||
| public static IEnumerator Interval(Action callback, float seconds, float startInSeconds) | ||
| { | ||
| yield return new WaitForSeconds(startInSeconds < 0 ? seconds : startInSeconds); | ||
| while (true) | ||
| { | ||
| callback(); | ||
| yield return new WaitForSeconds(seconds); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class AsyncGameObject : MonoBehaviour | ||
| { | ||
| private static PluginLogger Logger = PluginLogger.GetLogger<AsyncGameObject>(); | ||
| private static AsyncGameObject instance; | ||
|
|
||
| public static AsyncGameObject Instance | ||
| { | ||
| get | ||
| { | ||
| if (instance == null) | ||
| { | ||
| instance = new GameObject(nameof(AsyncGameObject)).AddComponent<AsyncGameObject>(); | ||
| } | ||
| return instance; | ||
| } | ||
| } | ||
|
|
||
| void Awake() | ||
| { | ||
| if (instance != null) | ||
| { | ||
| Logger.LogError("AsyncGameObject already exists, destroying this one"); | ||
| Destroy(this); | ||
| return; | ||
| } | ||
| instance = this; | ||
| DontDestroyOnLoad(gameObject); | ||
| } | ||
|
|
||
| public void timeout(Action callback, float seconds) | ||
| { | ||
| StartCoroutine(Async.Timeout(callback, seconds)); | ||
| } | ||
|
|
||
| public void timeoutFrames(Action callback, int frames) | ||
| { | ||
| StartCoroutine(Async.TimeoutFrames(callback, frames)); | ||
| } | ||
|
|
||
| public void interval(Action callback, float seconds, float startInSeconds) | ||
| { | ||
| StartCoroutine(Async.Interval(callback, seconds, startInSeconds)); | ||
| } | ||
| } |