Skip to content

Commit

Permalink
feat(input): add interaction to crafting menu (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Jan 28, 2024
1 parent 1a79ec9 commit 02e48d8
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
92 changes: 92 additions & 0 deletions plugin/src/input/ui/CraftingInteractable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Collections.Generic;
using System.Linq;
using TechtonicaVR.Util;
using UnityEngine;

namespace TechtonicaVR.Input.Ui;

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

private RecipePageUI recipePage;
private Transform tabsTransform;

public CraftingInteractableUI(GameObject gameObject) : base(gameObject)
{
tabsTransform = GameObjectFinder.FindChildObjectByName("Tabs", gameObject).transform;
recipePage = gameObject.GetComponent<RecipePageUI>();
getInteractables = () => getTabs().Concat(getRecipes()).ToList();
}

private IEnumerable<Interactable> getTabs()
{
for (var i = 0; i < tabsTransform.childCount; i++)
{
var tab = tabsTransform.GetChild(i).gameObject;
yield return new InteractableBuilder(this, getRect(tab.GetComponent<RectTransform>()), tab)
.withClick((_ui) => onTabClick(tab))
.withHoverEnter((_ui) => onTabHoverEnter(tab))
.withHoverExit((_ui) => onTabHoverExit(tab))
.build();
}
}

protected void onTabHoverEnter(GameObject tab)
{
var mouseInteraction = tab.GetComponent<MouseInteraction>();
if (mouseInteraction == null)
{
return;
}
mouseInteraction.mouseEnterCallback.Invoke();
}

protected void onTabHoverExit(GameObject tab)
{
var mouseInteraction = tab.GetComponent<MouseInteraction>();
if (mouseInteraction == null)
{
return;
}
mouseInteraction.mouseExitCallback.Invoke();
}

private void onTabClick(GameObject tab)
{
var mouseInteraction = tab.GetComponent<MouseInteraction>();
if (mouseInteraction == null)
{
return;
}
mouseInteraction.mouseLeftClickCallback.Invoke();
rebuildInteractables();
}

private IEnumerable<Interactable> getRecipes()
{
foreach (var recipe in recipePage.gameObject.GetComponentsInChildren<RecipeSlotUI>())
{
yield return new InteractableBuilder(this, getRect(recipe.GetComponent<RectTransform>()), recipe.gameObject)
.withClick((_ui) => onRecipeClick(recipe))
.withHoverEnter((_ui) => onRecipeHoverEnter(recipe))
.withHoverExit((_ui) => onRecipeHoverExit(recipe))
.build();
}
}

protected void onRecipeHoverEnter(RecipeSlotUI slot)
{
slot.mouseEnterCallback.Invoke();
}

protected void onRecipeHoverExit(RecipeSlotUI slot)
{
slot.mouseExitCallback.Invoke();
}

private void onRecipeClick(RecipeSlotUI slot)
{
slot.mouseLeftClickCallback.Invoke();
}
}
22 changes: 22 additions & 0 deletions plugin/src/patch/main_game/ui/IaCMenuPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ protected override bool Apply(GameObject gameObject)
return false;
}

var craftingCanvas = GameObjectFinder.FindChildObjectByName("Crafting Canvas", gameObject);
if (craftingCanvas == null)
{
return false;
}

var iac = GameObject.FindObjectOfType<InventoryAndCraftingUI>();
if (iac == null)
{
Expand Down Expand Up @@ -48,6 +54,22 @@ protected override bool Apply(GameObject gameObject)
iac.Refresh();
};

var craftingAnchor = new GameObject("Crafting UI Anchor");
craftingAnchor.transform.parent = iac.transform;
craftingAnchor.transform.localPosition = new Vector3(1065, 1270, 0);
craftingAnchor.transform.localRotation = Quaternion.identity;
craftingAnchor.transform.localScale = new Vector3(1.1f, 2.35f, 1);

var craftingObject = gameObject.GetComponentInChildren<RecipePageUI>().gameObject;
var crafting = new CraftingInteractableUI(craftingObject);
crafting.transform = craftingAnchor.transform;
crafting.menu = new UIMenuWrapper(iac);
crafting.OnEnterEvent += () =>
{
iac.inventoryHasFocus = false;
iac.Refresh();
};

return true;
}
}

0 comments on commit 02e48d8

Please sign in to comment.