Skip to content

Commit

Permalink
feat(input): add interaction to tech tree (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Feb 1, 2024
1 parent a58f9ec commit 519cf9e
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 31 deletions.
18 changes: 16 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ image:https://img.shields.io/github/license/{repo}["License", link="https://gith

This is a mod for the game https://store.steampowered.com/app/1457320/Techtonica/[Techtonica] that adds VR support.

ifdef::env-github[]
____
endif::[]
ifndef::env-github[]
****
I spent way over 100 hours of my free time on this mod.
endif::[]
I spent countless hours of my free time on this mod.
If you enjoy it, please consider supporting me on https://liberapay.com/rip3.141[Liberapay] ❤️
ifndef::env-thunderstore[]

image:https://liberapay.com/assets/widgets/donate.svg["Donate using Liberapay", link="https://liberapay.com/rip3.141"]
image::https://liberapay.com/assets/widgets/donate.svg["Donate using Liberapay", link="https://liberapay.com/rip3.141"]
endif::[]
ifdef::env-github[]
____
endif::[]
ifndef::env-github[]
****
endif::[]

ifdef::env-github[]
toc::[]
Expand Down Expand Up @@ -170,6 +182,8 @@ Menu Spawn Distance:: Distance of the menu from the player. Default: `0.8`
Menu Scale:: Scale of the menu (X/Y/Z). Default: `{"x": 0.001,"y":0.001,"z":0.001}`
Inventory and Crafting Menu Scale Override:: Scale of the inventory and crafting menu (X/Y/Z). This menu has different scaling and needs separate config. Default: `{"x": 0.001,"y":0.0005,"z":0.001}`
Menu Downward Offset:: Offset of the menu in the downward direction. Default: `0.2`
Menu Scroll Speed:: Speed of scrolling through menus by moving the cursor to the edge. Speed increases when nearer to the edge. Default: `0.125`
Menu Scroll Deadzone:: Deadzone for scrolling through menus by moving the cursor to the edge. In percent from the center. Effectively the size of the region not triggering scrolling. Default: `0.35`

[horizontal]
.Debug
Expand Down
36 changes: 36 additions & 0 deletions plugin/src/input/LaserPointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public virtual void OnPointerIn(PointerEventArgs e)
PointerIn(this, e);
}

public virtual void OnMouseDown(PointerEventArgs e)
{
Logger.LogDebug($"OnMouseDown: {e.target.transform.name}");
e.interactable?.mouseDown();
}

public virtual void OnMouseUp(PointerEventArgs e)
{
Logger.LogDebug($"OnMouseUp: {e.target.transform.name}");
e.interactable?.mouseUp();
}

public virtual void OnPointerClick(PointerEventArgs e)
{
Logger.LogDebug($"OnPointerClick: {e.target.transform.name}");
Expand Down Expand Up @@ -260,6 +272,30 @@ private void handleInteraction(UiRaycastHit hit, Interactable interactable)
return;
}

if (interactButton.IsPressed())
{
PointerEventArgs argsDown = new PointerEventArgs
{
distance = hit.distance,
flags = 0,
target = hit.ui,
interactable = interactable
};
OnMouseDown(argsDown);
}

if (interactButton.IsReleased())
{
PointerEventArgs argsUp = new PointerEventArgs
{
distance = hit.distance,
flags = 0,
target = hit.ui,
interactable = interactable
};
OnMouseUp(argsUp);
}

var isDraggable = interactable.isDraggable();

if (isDraggable ? interactButton.IsTimedPressUp(0, ModConfig.clickTime.Value) : interactButton.IsReleased())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace TechtonicaVR.Input.Ui;

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

private RecipePageUI recipePage;
private Transform tabsTransform;

public CraftingInteractableUI(GameObject gameObject) : base(gameObject)
public CraftingInteractableUi(GameObject gameObject) : base(gameObject)
{
tabsTransform = GameObjectFinder.FindChildObjectByName("Tabs", gameObject).transform;
recipePage = gameObject.GetComponent<RecipePageUI>();
Expand Down
40 changes: 39 additions & 1 deletion plugin/src/input/ui/InteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ public class InteractableBuilder
private InteractableReceiveDropEvent onReceiveDropEvent;
private InteractableHoverEnterEvent onHoverEnterEvent;
private InteractableHoverExitEvent onHoverExitEvent;
private Action onMouseDownEvent;
private Action onMouseUpEvent;

private InteractableIsHitCallback isHitCallback;
private InteractableGetObjectCallback getObjectCallback;
Expand All @@ -226,9 +228,18 @@ public Interactable build()
{
recalculateCallback = recalculateCallback,
isClickableCallback = isClickableCallback,
mask = mask
mask = mask,
};

if (onMouseDownEvent != null)
{
interactable.OnMouseDown += onMouseDownEvent;
}
if (onMouseUpEvent != null)
{
interactable.OnMouseUp += onMouseUpEvent;
}

return interactable;
}

Expand All @@ -250,6 +261,18 @@ public InteractableBuilder withMask(RectTransform mask)
return this;
}

public InteractableBuilder withMouseDown(Action onMouseDown)
{
onMouseDownEvent = onMouseDown;
return this;
}

public InteractableBuilder withMouseUp(Action onMouseUp)
{
onMouseUpEvent = onMouseUp;
return this;
}

public InteractableBuilder withClick(InteractableClickEvent onClick, Func<bool> isClickableCallback = null)
{
onClickEvent = onClick;
Expand Down Expand Up @@ -294,6 +317,8 @@ public class Interactable
public Rect rect;
public RectTransform mask;
public GameObject gameObject;
public event Action OnMouseDown;
public event Action OnMouseUp;
public event InteractableClickEvent OnClick;
public event InteractableDragEvent OnDrag;
public event InteractableDropEvent OnDrop;
Expand Down Expand Up @@ -368,9 +393,22 @@ public void hoverEnter(InteractableUi ui)
public void hoverExit(InteractableUi ui)
{
Logger.LogDebug($"hoverExit {ui?.transform.gameObject.name}");
OnMouseUp?.Invoke();
OnHoverExit?.Invoke(ui);
}

public void mouseDown()
{
Logger.LogDebug($"mouseDown {gameObject.name}");
OnMouseDown?.Invoke();
}

public void mouseUp()
{
Logger.LogDebug($"mouseUp {gameObject.name}");
OnMouseUp?.Invoke();
}

public void click(InteractableUi ui)
{
Logger.LogDebug($"click {ui.transform.gameObject.name}");
Expand Down
12 changes: 6 additions & 6 deletions plugin/src/input/ui/InventoryInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace TechtonicaVR.Input.Ui;

public class InventoryInteractableUI : InteractableUi
public class InventoryInteractableUi : InteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<InventoryInteractableUI>();
private static PluginLogger Logger = PluginLogger.GetLogger<InventoryInteractableUi>();
protected ResourceInfo draggedResourceInfo;
private int draggedResourceCount;

public InventoryInteractableUI(GameObject gameObject) : base(gameObject)
public InventoryInteractableUi(GameObject gameObject) : base(gameObject)
{
init();
}
Expand Down Expand Up @@ -73,7 +73,7 @@ protected void onDrop(InteractableUi ui, Interactable target, InventoryResourceS
return;
}

if (ui is InventoryInteractableUI)
if (ui is InventoryInteractableUi)
{
var targetSlot = target.gameObject.GetComponent<InventoryResourceSlotUI>();
if (targetSlot == null)
Expand All @@ -87,7 +87,7 @@ protected void onDrop(InteractableUi ui, Interactable target, InventoryResourceS
return;
}

if (ui is ToolbarInteractableUI)
if (ui is ToolbarInteractableUi)
{
target.receiveDrop(target.ui, droppedResourceInfo);
sourceSlot.mouseLeftClickCallback.Invoke();
Expand All @@ -105,7 +105,7 @@ protected void onCancelDrag(InventoryResourceSlotUI slot)

protected void onAcceptsDrop(AcceptDropEventArgs args)
{
if (args.source.ui is not InventoryInteractableUI)
if (args.source.ui is not InventoryInteractableUi)
{
return;
}
Expand Down
50 changes: 50 additions & 0 deletions plugin/src/input/ui/TechTreeInteractableUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Linq;
using UnityEngine;

namespace TechtonicaVR.Input.Ui;

public class TechTreeInteractableUi : InteractableUi
{
private TechTreeUI techTreeUi;
public TechTreeInteractableUi(TechTreeUI ui, GameObject gameObject) : base(gameObject)
{
techTreeUi = ui;
getInteractables = () => gameObject.GetComponentsInChildren<TechTreeCategoryButton>()
.Select(getCategoryButtonInteractable)
.Concat(gameObject.GetComponentsInChildren<TechTreeNode>()
.Select(getTechTreeNodeInteractable)
.Where(interactable => interactable != null))
.ToList();
}

private Interactable getTechTreeNodeInteractable(TechTreeNode node)
{
if (node.myUnlock.category != techTreeUi.gridUI.curCategory)
{
return null;
}

var rect = node.GetComponent<RectTransform>();
return new InteractableBuilder(this, getRect(rect), node.gameObject)
.withClick((ui) => node.mouseLeftClickCallback?.Invoke())
.withMouseDown(() => node.mouseDownCallback?.Invoke())
.withMouseUp(() => node.mouseUpCallback?.Invoke())
.withHoverEnter((ui) => node.mouseEnterCallback?.Invoke())
.withHoverExit((ui) => node.mouseExitCallback?.Invoke())
.build();
}

private Interactable getCategoryButtonInteractable(TechTreeCategoryButton button)
{
var rect = button.GetComponent<RectTransform>();
return new InteractableBuilder(this, getRect(rect), button.gameObject)
.withClick((ui) =>
{
button.mouseLeftClickCallback?.Invoke();
ui.refresh();
})
.withHoverEnter((ui) => button.mouseEnterCallback?.Invoke())
.withHoverExit((ui) => button.mouseExitCallback?.Invoke())
.build();
}
}
8 changes: 4 additions & 4 deletions plugin/src/input/ui/ToolbarInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace TechtonicaVR.Input.Ui;

public class ToolbarInteractableUI : InteractableUi
public class ToolbarInteractableUi : InteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<ToolbarInteractableUI>();
private static PluginLogger Logger = PluginLogger.GetLogger<ToolbarInteractableUi>();
private ResourceInfo draggedResourceInfo;

public ToolbarInteractableUI(GameObject gameObject) : base(gameObject)
public ToolbarInteractableUi(GameObject gameObject) : base(gameObject)
{
var uiSlots = gameObject.GetComponentsInChildren<ToolbarSlotUI>();
interactable = uiSlots.Select(getInteractable).ToList();
Expand Down Expand Up @@ -56,7 +56,7 @@ private void onDrop(InteractableUi ui, Interactable target, ToolbarSlotUI source
}

ResourceInfo targetResourceInfo = null;
if (ui is ToolbarInteractableUI)
if (ui is ToolbarInteractableUi)
{
var targetSlot = target.gameObject.GetComponent<ToolbarSlotUI>();
if (targetSlot == null)
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/input/ui/machine/AssemblerInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class AssemblerInteractableUi : InventoryInteractableUI
public class AssemblerInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<AssemblerInteractableUi>();

Expand All @@ -20,7 +20,7 @@ protected override void init()
}
}

public class AssemblerRecipeSelectInteractableUi : InventoryInteractableUI
public class AssemblerRecipeSelectInteractableUi : InventoryInteractableUi
{
public AssemblerRecipeSelectInteractableUi(GameObject gameObject) : base(gameObject)
{
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/input/ui/machine/DrillInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class DrillInteractableUi : InventoryInteractableUI
public class DrillInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<DrillInteractableUi>();

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/input/ui/machine/PlanterInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class PlanterInteractableUi : InventoryInteractableUI
public class PlanterInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<PlanterInteractableUi>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class ProductionTerminalInteractableUi : InventoryInteractableUI
public class ProductionTerminalInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<ProductionTerminalInteractableUi>();

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/input/ui/machine/ResourceGateInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class ResourceGateInteractableUi : InventoryInteractableUI
public class ResourceGateInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<ResourceGateInteractableUi>();

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/input/ui/machine/SmelterInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class SmelterInteractableUi : InventoryInteractableUI
public class SmelterInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<SmelterInteractableUi>();

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/input/ui/machine/ThresherInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class ThresherInteractableUi : InventoryInteractableUI
public class ThresherInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<ThresherInteractableUi>();

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/input/ui/machine/TransitDepotInteractableUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TechtonicaVR.Input.Ui.Machine;

public class TransitDepotInteractableUi : InventoryInteractableUI
public class TransitDepotInteractableUi : InventoryInteractableUi
{
private static PluginLogger Logger = PluginLogger.GetLogger<TransitDepotInteractableUi>();

Expand Down
Loading

0 comments on commit 519cf9e

Please sign in to comment.