mirror of
https://github.com/nothke/quality-control.git
synced 2025-08-11 08:03:44 +00:00
Added interaction, hand, rigidbody dragging, picking up hammer
This commit is contained in:
176
Assets/Plugins/Interaction/Runtime/UI/InteractionUI.cs
Normal file
176
Assets/Plugins/Interaction/Runtime/UI/InteractionUI.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
#define CENTER_TEXT
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
#if CENTER_TEXT
|
||||
using TMPro;
|
||||
#endif
|
||||
|
||||
using Nothke.Interaction.Items;
|
||||
|
||||
namespace Nothke.Interaction.UI
|
||||
{
|
||||
public interface ICustomReticle
|
||||
{
|
||||
ReticleUI.State GetCustomReticle();
|
||||
}
|
||||
|
||||
public interface IReticleUIOverridable
|
||||
{
|
||||
bool OverrideUI { get; }
|
||||
bool ReleaseInteractionBlocking { get; }
|
||||
}
|
||||
|
||||
public class InteractionUI : MonoBehaviour
|
||||
{
|
||||
public static InteractionUI e;
|
||||
private void Awake()
|
||||
{
|
||||
e = this;
|
||||
|
||||
controller = GetComponent<InteractionController>();
|
||||
if (!controller) controller = FindObjectOfType<InteractionController>();
|
||||
if (!controller)
|
||||
{
|
||||
Debug.LogError("No controller found");
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
#if CENTER_TEXT
|
||||
public TMP_Text centerText;
|
||||
#endif
|
||||
|
||||
InteractionController controller;
|
||||
|
||||
bool mouseRay;
|
||||
|
||||
RigidbodyInteractable riHovered;
|
||||
RigidbodyInteractable ri;
|
||||
|
||||
IReticleUIOverridable overridable;
|
||||
|
||||
bool ovrr;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
controller.OnHover += OnHover;
|
||||
controller.OnDehover += OnDehover;
|
||||
controller.OnRayModeChange += OnRayModeChange;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
controller.OnHover -= OnHover;
|
||||
controller.OnDehover -= OnDehover;
|
||||
controller.OnRayModeChange += OnRayModeChange;
|
||||
}
|
||||
|
||||
#if CENTER_TEXT
|
||||
public void SetText(string description)
|
||||
{
|
||||
centerText.enabled = true;
|
||||
centerText.text = description;
|
||||
}
|
||||
|
||||
public void HideText()
|
||||
{
|
||||
centerText.enabled = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
protected virtual void OnHover(Interactable interactable)
|
||||
{
|
||||
if (ovrr) return;
|
||||
|
||||
#if CENTER_TEXT
|
||||
if (centerText)
|
||||
{
|
||||
SetText(interactable.Label);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Interactable icon choice
|
||||
|
||||
if (interactable is ICustomReticle)
|
||||
{
|
||||
ReticleUI.state = (interactable as ICustomReticle).GetCustomReticle();
|
||||
}
|
||||
else if (interactable is ITakeable)
|
||||
{
|
||||
ReticleUI.state = ReticleUI.State.Take;
|
||||
}
|
||||
else if (interactable is RigidbodyInteractable)
|
||||
{
|
||||
ReticleUI.state = ReticleUI.State.Grab;
|
||||
|
||||
riHovered = interactable as RigidbodyInteractable;
|
||||
}
|
||||
else
|
||||
{
|
||||
ReticleUI.state = ReticleUI.State.Idle;
|
||||
}
|
||||
|
||||
if (interactable is IReticleUIOverridable)
|
||||
{
|
||||
overridable = interactable as IReticleUIOverridable;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (overridable != null)
|
||||
{
|
||||
ovrr = overridable.OverrideUI;
|
||||
if (ovrr == false)
|
||||
{
|
||||
OnDehover();
|
||||
overridable = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (riHovered && riHovered.held && ri == null)
|
||||
{
|
||||
ri = riHovered;
|
||||
ovrr = true;
|
||||
ReticleUI.state = ReticleUI.State.Hold;
|
||||
}
|
||||
|
||||
if (ri && !ri.held)
|
||||
{
|
||||
ovrr = false;
|
||||
OnDehover();
|
||||
|
||||
if (controller.hovered)
|
||||
OnHover(controller.hovered);
|
||||
|
||||
ri = null;
|
||||
}
|
||||
|
||||
if (controller.mouseRay)
|
||||
{
|
||||
ReticleUI.SetReticlePosition(Input.mousePosition);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDehover()
|
||||
{
|
||||
if (ovrr) return;
|
||||
|
||||
#if CENTER_TEXT
|
||||
if (centerText)
|
||||
centerText.enabled = false;
|
||||
#endif
|
||||
|
||||
ReticleUI.state = ReticleUI.State.None;
|
||||
}
|
||||
|
||||
protected virtual void OnRayModeChange(bool enable)
|
||||
{
|
||||
if (!enable)
|
||||
ReticleUI.ResetReticlePosition();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Plugins/Interaction/Runtime/UI/InteractionUI.cs.meta
Normal file
11
Assets/Plugins/Interaction/Runtime/UI/InteractionUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81363621df3548e45ba871e78efd9d68
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
95
Assets/Plugins/Interaction/Runtime/UI/ReticleUI.cs
Normal file
95
Assets/Plugins/Interaction/Runtime/UI/ReticleUI.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Nothke.Interaction.UI
|
||||
{
|
||||
public class ReticleUI : MonoBehaviour
|
||||
{
|
||||
static ReticleUI e;
|
||||
private void Awake() { e = this; }
|
||||
|
||||
public bool showIconForNone;
|
||||
|
||||
public Texture icon_none;
|
||||
public Texture icon_generic;
|
||||
public Texture icon_grab;
|
||||
public Texture icon_hold;
|
||||
public Texture icon_take;
|
||||
public Texture icon_slot;
|
||||
|
||||
public Texture icon_ride;
|
||||
public Texture icon_scroll;
|
||||
public Texture icon_grab_knob;
|
||||
public Texture icon_hold_knob;
|
||||
|
||||
public RawImage image;
|
||||
|
||||
public enum State { None, Idle, Grab, Hold, Take, Slot, Ride, Scroll, GrabKnob, HoldKnob }
|
||||
public static State state;
|
||||
static State lastState;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private void Init()
|
||||
{
|
||||
state = State.None;
|
||||
lastState = State.None;
|
||||
}
|
||||
|
||||
public static void Hide()
|
||||
{
|
||||
state = State.None;
|
||||
e.SetState();
|
||||
}
|
||||
|
||||
public static void ResetReticlePosition()
|
||||
{
|
||||
if (!e) return;
|
||||
RectTransform t = e.image.GetComponent<RectTransform>();
|
||||
t.anchoredPosition = Vector2.zero;
|
||||
}
|
||||
|
||||
public static void SetReticlePosition(Vector2 screenPos)
|
||||
{
|
||||
if (!e) return;
|
||||
RectTransform t = e.image.GetComponent<RectTransform>();
|
||||
t.position = screenPos;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
SetState();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (state != lastState)
|
||||
SetState();
|
||||
|
||||
lastState = state;
|
||||
}
|
||||
|
||||
void SetState()
|
||||
{
|
||||
if (!showIconForNone)
|
||||
image.enabled = state != State.None;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case State.None: image.texture = icon_none; break;
|
||||
case State.Idle: image.texture = icon_generic; break;
|
||||
case State.Grab: image.texture = icon_grab; break;
|
||||
case State.Hold: image.texture = icon_hold; break;
|
||||
case State.Take: image.texture = icon_take; break;
|
||||
case State.Slot: image.texture = icon_slot; break;
|
||||
|
||||
case State.Ride: image.texture = icon_ride; break;
|
||||
case State.Scroll: image.texture = icon_scroll; break;
|
||||
case State.GrabKnob: image.texture = icon_grab_knob; break;
|
||||
case State.HoldKnob: image.texture = icon_hold_knob; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Plugins/Interaction/Runtime/UI/ReticleUI.cs.meta
Normal file
11
Assets/Plugins/Interaction/Runtime/UI/ReticleUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f225472c8049dc418e5a37e0cf6e0e8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user