Pose mode improvements
This commit is contained in:
@@ -1,9 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using static SerializableBone;
|
||||
|
||||
public class HandleManager : MonoBehaviour
|
||||
{
|
||||
public GameObject Pfb_HandleDisplay;
|
||||
public UIPopupPanel Pfb_Popup;
|
||||
public Button Pfb_PopupButton;
|
||||
|
||||
public Material LineRendererMaterial;
|
||||
|
||||
public List<BoneTags> EnabledHandles = new List<BoneTags>() { BoneTags.Humanoid };
|
||||
public bool EnabledLines = true;
|
||||
|
||||
public static bool InteractionInProgress;
|
||||
|
||||
private List<UIHandle> AllHandles = new List<UIHandle>();
|
||||
|
||||
public static System.Action<RuntimeGizmoUndoData> RegisterRuntimeGizmoUndoAction;
|
||||
public static System.Action<List<RuntimeGizmoUndoData>> RegisterRuntimeGizmoUndoActions;
|
||||
|
||||
public struct RuntimeGizmoUndoData
|
||||
{
|
||||
public Transform NewPosition;
|
||||
public SerializableTransform OldPosition;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var camera = Camera.main;
|
||||
var poseModeOn = UmaViewerUI.Instance.PoseManager.PoseModeOn;
|
||||
|
||||
foreach(var handle in AllHandles)
|
||||
{
|
||||
handle.ForceDisplayOff(!poseModeOn);
|
||||
handle.UpdateManual(camera, EnabledLines);
|
||||
|
||||
if (handle.Popup.gameObject.activeInHierarchy)
|
||||
{
|
||||
handle.Popup.UpdateManual(camera);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void RegisterHandle(UIHandle handle)
|
||||
{
|
||||
var hm = UmaViewerUI.Instance.HandleManager;
|
||||
|
||||
if (hm.AllHandles.Contains(handle))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
hm.AllHandles.Add(handle);
|
||||
}
|
||||
|
||||
public static void UnregisterHandle(UIHandle handle)
|
||||
{
|
||||
var hm = UmaViewerUI.Instance.HandleManager;
|
||||
|
||||
if (!hm.AllHandles.Contains(handle))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
hm.AllHandles.Remove(handle);
|
||||
}
|
||||
|
||||
public static void CloseAllPopups()
|
||||
{
|
||||
var hm = UmaViewerUI.Instance.HandleManager;
|
||||
|
||||
foreach(var handle in hm.AllHandles)
|
||||
{
|
||||
if (handle.Popup.gameObject.activeInHierarchy)
|
||||
{
|
||||
handle.TogglePopup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Used on UI buttons </summary>
|
||||
public void ToggleBonesVisible(string tag)
|
||||
{
|
||||
var enumTag = (BoneTags)System.Enum.Parse(typeof(BoneTags), tag);
|
||||
if (EnabledHandles.Contains(enumTag))
|
||||
{
|
||||
EnabledHandles.Remove(enumTag);
|
||||
}
|
||||
else
|
||||
{
|
||||
EnabledHandles.Add(enumTag);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Used on UI buttons </summary>
|
||||
public void ToggleLinesVisible(bool value)
|
||||
{
|
||||
EnabledLines = value;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,5 @@
|
||||
using I18N.Common;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using static UnityEngine.Rendering.VirtualTexturing.Debugging;
|
||||
|
||||
public class UIHandle : MonoBehaviour
|
||||
{
|
||||
@@ -12,7 +7,7 @@ public class UIHandle : MonoBehaviour
|
||||
public GameObject Owner;
|
||||
|
||||
/// <summary> Object that the handle refers to. Cast in inheriting class </summary>
|
||||
public object Target;
|
||||
public Transform Target;
|
||||
|
||||
/// <summary> Display object on the UI </summary>
|
||||
public GameObject Handle;
|
||||
@@ -23,11 +18,13 @@ public class UIHandle : MonoBehaviour
|
||||
/// <summary> Object that appears when right-clicking the handle </summary>
|
||||
public UIPopupPanel Popup;
|
||||
|
||||
public LineRenderer LineRenderer;
|
||||
|
||||
protected SerializableTransform _defaultTransform;
|
||||
protected float _baseScale = 4;
|
||||
protected float _baseScale = 1;
|
||||
protected bool _forceDisplayOff = false;
|
||||
|
||||
public virtual UIHandle Init(GameObject owner, object target)
|
||||
public virtual UIHandle Init(GameObject owner, Transform target)
|
||||
{
|
||||
Target = target;
|
||||
return Init(owner);
|
||||
@@ -35,17 +32,19 @@ public class UIHandle : MonoBehaviour
|
||||
|
||||
public virtual UIHandle Init(GameObject owner)
|
||||
{
|
||||
gameObject.layer = LayerMask.NameToLayer("UIHandle");
|
||||
Target = Target == null? owner : Target;
|
||||
Owner = owner;
|
||||
HandleManager.RegisterHandle(this);
|
||||
|
||||
Popup = Instantiate(UmaViewerUI.Instance.HandleManager.Pfb_Popup, UmaViewerUI.Instance.HandlesPanel).Init(this);
|
||||
Popup.gameObject.SetActive(false);
|
||||
gameObject.layer = LayerMask.NameToLayer("UIHandle");
|
||||
Target = Target == null? owner.transform : Target;
|
||||
Owner = owner;
|
||||
|
||||
Handle = Instantiate(UmaViewerUI.Instance.HandleManager.Pfb_HandleDisplay, UmaViewerUI.Instance.HandlesPanel);
|
||||
Handle.transform.SetAsFirstSibling();
|
||||
|
||||
_defaultTransform = new SerializableTransform(transform, Space.Self);
|
||||
Popup = Instantiate(UmaViewerUI.Instance.HandleManager.Pfb_Popup, UmaViewerUI.Instance.HandlesPanel).Init(this);
|
||||
Popup.gameObject.SetActive(false);
|
||||
|
||||
_defaultTransform = new SerializableTransform(Target, Space.Self);
|
||||
|
||||
var collider = gameObject.AddComponent<SphereCollider>();
|
||||
collider.center = Vector3.zero;
|
||||
@@ -55,25 +54,26 @@ public class UIHandle : MonoBehaviour
|
||||
return this;
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
public virtual void UpdateManual(Camera camera, bool linesEnabled)
|
||||
{
|
||||
var cam = Camera.main;
|
||||
if (Handle != null && cam != null && transform != null)
|
||||
//Made it like this to minimize calls to Camera.main and UI.Inst.Manager.LinesEnabled. Done by HandleManager.
|
||||
|
||||
if (Handle != null && camera != null && transform != null)
|
||||
{
|
||||
Handle.transform.localScale = Vector3.one * Mathf.Clamp(_baseScale / Vector3.Distance(cam.transform.position, transform.position), 0.1f, 1);
|
||||
Handle.transform.localScale = Vector3.one * Mathf.Clamp(_baseScale / Vector3.Distance(camera.transform.position, transform.position), 0.1f, 1);
|
||||
|
||||
if (Collider.transform.localScale.x != 0 && transform.lossyScale.x != 0)
|
||||
{
|
||||
Collider.radius = 35 /* magic number */ * (1 / transform.lossyScale.x) * GetRadiusOnScreen(Collider.transform.position, Handle.transform.localScale.x);
|
||||
Collider.radius = 35 /* magic number */ * (1 / transform.lossyScale.x) * GetRadiusOnScreen(camera, Collider.transform.position, Handle.transform.localScale.x);
|
||||
}
|
||||
|
||||
Handle.transform.position = cam.WorldToScreenPoint(Collider.transform.TransformPoint(Collider.center));
|
||||
Handle.transform.position = camera.WorldToScreenPoint(Collider.transform.TransformPoint(Collider.center));
|
||||
|
||||
if (_forceDisplayOff)
|
||||
if (ShouldBeHidden())
|
||||
{
|
||||
if (Handle.activeSelf == true)
|
||||
{
|
||||
Handle.SetActive(false);
|
||||
ToggleActive(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -84,15 +84,33 @@ public class UIHandle : MonoBehaviour
|
||||
|
||||
if (Handle.activeSelf != isOnScreen)
|
||||
{
|
||||
Handle.SetActive(!Handle.activeSelf);
|
||||
ToggleActive(!Handle.activeSelf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(Handle.activeSelf && LineRenderer != null)
|
||||
{
|
||||
if (LineRenderer.enabled != linesEnabled)
|
||||
{
|
||||
LineRenderer.enabled = linesEnabled;
|
||||
}
|
||||
if (linesEnabled)
|
||||
{
|
||||
LineRenderer.SetPositions(new Vector3[] { (Target).position, (Target).parent.position });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void TogglePopup(int offset)
|
||||
protected virtual bool ShouldBeHidden()
|
||||
{
|
||||
//Popup.GetComponent<PopupController>().offset = offset * Popup.GetComponent<RectTransform>().sizeDelta.x;
|
||||
return _forceDisplayOff;
|
||||
}
|
||||
|
||||
/// <summary> For future, `offset` param will allow popups to be spaced out when selecting more than 1 handle at a time. </summary>
|
||||
public void TogglePopup(int offset = 0)
|
||||
{
|
||||
Popup.Offset = offset * Popup.GetComponent<RectTransform>().sizeDelta.x;
|
||||
UmaViewerUI.Instance.ToggleVisible(Popup.gameObject);
|
||||
}
|
||||
|
||||
@@ -120,7 +138,7 @@ public class UIHandle : MonoBehaviour
|
||||
public UIHandle SetName(string name)
|
||||
{
|
||||
Handle.gameObject.name = name;
|
||||
Popup.GetComponentInChildren<Text>().text = name;
|
||||
Popup.NameLabel.text = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -130,21 +148,24 @@ public class UIHandle : MonoBehaviour
|
||||
return this;
|
||||
}
|
||||
|
||||
public float GetRadiusOnScreen(Vector3 position, float screenSize)
|
||||
public float GetRadiusOnScreen(Camera cam, Vector3 position, float screenSize)
|
||||
{
|
||||
Vector3 a = Camera.main.WorldToScreenPoint(position);
|
||||
Vector3 a = cam.WorldToScreenPoint(position);
|
||||
Vector3 b = new Vector3(a.x, a.y + screenSize, a.z);
|
||||
|
||||
Vector3 aa = Camera.main.ScreenToWorldPoint(a);
|
||||
Vector3 bb = Camera.main.ScreenToWorldPoint(b);
|
||||
Vector3 aa = cam.ScreenToWorldPoint(a);
|
||||
Vector3 bb = cam.ScreenToWorldPoint(b);
|
||||
|
||||
return (aa - bb).magnitude;
|
||||
}
|
||||
|
||||
public UIHandle ClearForDeletion()
|
||||
public UIHandle WithLineRenderer()
|
||||
{
|
||||
Popup = null;
|
||||
Handle = null;
|
||||
LineRenderer = Handle.gameObject.AddComponent<LineRenderer>();
|
||||
LineRenderer.positionCount = 2;
|
||||
LineRenderer.startWidth = LineRenderer.endWidth = 0.005f;
|
||||
LineRenderer.material = UmaViewerUI.Instance.HandleManager.LineRendererMaterial;
|
||||
LineRenderer.startColor = LineRenderer.endColor = Handle.GetComponent<Image>().color;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -158,35 +179,71 @@ public class UIHandle : MonoBehaviour
|
||||
{
|
||||
Destroy(Handle);
|
||||
}
|
||||
HandleManager.UnregisterHandle(this);
|
||||
}
|
||||
|
||||
public UIHandle PositionReset()
|
||||
public void TransformReset(PoseLoadOptions options = null)
|
||||
{
|
||||
transform.localPosition = _defaultTransform.Position;
|
||||
return this;
|
||||
if(options == null)
|
||||
{
|
||||
options = PoseLoadOptions.All();
|
||||
}
|
||||
|
||||
_defaultTransform.ApplyTo(Target);
|
||||
}
|
||||
|
||||
public UIHandle RotationReset()
|
||||
public void TransformResetAll()
|
||||
{
|
||||
transform.localEulerAngles = _defaultTransform.Rotation;
|
||||
return this;
|
||||
var undoData = new HandleManager.RuntimeGizmoUndoData();
|
||||
|
||||
undoData.OldPosition = new SerializableTransform(Target, Space.World);
|
||||
TransformReset();
|
||||
undoData.NewPosition = Target;
|
||||
|
||||
HandleManager.RegisterRuntimeGizmoUndoAction.Invoke(undoData);
|
||||
}
|
||||
|
||||
public UIHandle ScaleReset()
|
||||
public void TransformResetPosition()
|
||||
{
|
||||
transform.localScale = _defaultTransform.Scale;
|
||||
return this;
|
||||
var undoData = new HandleManager.RuntimeGizmoUndoData();
|
||||
|
||||
undoData.OldPosition = new SerializableTransform(Target, Space.World);
|
||||
TransformReset(new PoseLoadOptions() { Position = true });
|
||||
undoData.NewPosition = Target;
|
||||
|
||||
HandleManager.RegisterRuntimeGizmoUndoAction.Invoke(undoData);
|
||||
}
|
||||
|
||||
public void TransformResetRotation()
|
||||
{
|
||||
var undoData = new HandleManager.RuntimeGizmoUndoData();
|
||||
|
||||
undoData.OldPosition = new SerializableTransform(Target, Space.World);
|
||||
TransformReset(new PoseLoadOptions() { Rotation = true });
|
||||
undoData.NewPosition = Target;
|
||||
|
||||
HandleManager.RegisterRuntimeGizmoUndoAction.Invoke(undoData);
|
||||
}
|
||||
|
||||
public void TransformResetScale()
|
||||
{
|
||||
var undoData = new HandleManager.RuntimeGizmoUndoData();
|
||||
|
||||
undoData.OldPosition = new SerializableTransform(Target, Space.World);
|
||||
TransformReset(new PoseLoadOptions() { Scale = true });
|
||||
undoData.NewPosition = Target;
|
||||
|
||||
HandleManager.RegisterRuntimeGizmoUndoAction.Invoke(undoData);
|
||||
}
|
||||
|
||||
public void ToggleActive(bool value)
|
||||
{
|
||||
this.enabled = value;
|
||||
Handle.SetActive(value);
|
||||
Collider.enabled = value;
|
||||
Popup.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void ToggleVisible(bool value)
|
||||
public void ForceDisplayOff(bool value)
|
||||
{
|
||||
_forceDisplayOff = value;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using static SerializableBone;
|
||||
|
||||
public class UIHandleBone : UIHandle
|
||||
{
|
||||
public Transform Bone => Target as Transform;
|
||||
public Transform Bone => Target;
|
||||
|
||||
public List<BoneTags> Tags;
|
||||
|
||||
public static UIHandleBone CreateAsChild(Transform owner, List<BoneTags> boneTags)
|
||||
{
|
||||
return CreateAsChild(owner.gameObject, boneTags);
|
||||
}
|
||||
|
||||
public static UIHandleBone CreateAsChild(GameObject owner, List<BoneTags> boneTags)
|
||||
{
|
||||
var handle = new GameObject(owner.name + "_Handle").AddComponent<UIHandleBone>();
|
||||
handle.transform.parent = owner.transform;
|
||||
handle.transform.localPosition = Vector3.zero;
|
||||
handle.transform.localScale = Vector3.one;
|
||||
handle.Init(owner, boneTags).SetScale(0.8f);
|
||||
return handle;
|
||||
}
|
||||
|
||||
public UIHandleBone Init(GameObject owner, List<BoneTags> boneTags)
|
||||
{
|
||||
Target = owner.transform;
|
||||
Tags = boneTags;
|
||||
Init(owner);
|
||||
base.Init(owner);
|
||||
if (Tags.Contains(BoneTags.Left)) SetColor(Color.green);
|
||||
if (Tags.Contains(BoneTags.Right)) SetColor(Color.blue);
|
||||
SetScale(2f);
|
||||
|
||||
Popup.AddButton("Reset All", TransformResetAll);
|
||||
Popup.AddButton("Reset Position", TransformResetPosition);
|
||||
Popup.AddButton("Reset Rotation", TransformResetRotation);
|
||||
Popup.AddButton("Reset Scale", TransformResetScale);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected override bool ShouldBeHidden()
|
||||
{
|
||||
return _forceDisplayOff || !UmaViewerUI.Instance.HandleManager.EnabledHandles.Intersect(Tags).Any();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class UIHandleCharacterRoot : UIHandle
|
||||
{
|
||||
public List<UIHandle> ChildHandles = new List<UIHandle>();
|
||||
|
||||
public static UIHandleCharacterRoot CreateAsChild(UmaContainer owner)
|
||||
{
|
||||
var handle = new GameObject(owner.name + "_Handle").AddComponent<UIHandleCharacterRoot>();
|
||||
handle.transform.parent = owner.Position;
|
||||
handle.transform.localPosition = Vector3.zero;
|
||||
handle.transform.localScale = Vector3.one;
|
||||
handle.Target = owner.Position;
|
||||
handle.Init(owner.Position.gameObject).SetColor(Color.red).SetScale(0.8f);
|
||||
return handle;
|
||||
}
|
||||
|
||||
public override UIHandle Init(GameObject owner)
|
||||
{
|
||||
base.Init(owner);
|
||||
|
||||
Popup.AddButton("Reset All", TransformResetAll);
|
||||
Popup.AddButton("Reset Position", TransformResetPosition);
|
||||
Popup.AddButton("Reset Rotation", TransformResetRotation);
|
||||
Popup.AddButton("Reset Scale", TransformResetScale);
|
||||
Popup.AddButton("Reset to T-Pose", TransformResetAllChildren);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void TransformResetAllChildren()
|
||||
{
|
||||
List<HandleManager.RuntimeGizmoUndoData> datas = new List<HandleManager.RuntimeGizmoUndoData>();
|
||||
|
||||
for(int i = 0; i < ChildHandles.Count; i++)
|
||||
{
|
||||
var handle = ChildHandles[i];
|
||||
var bone = handle.Target;
|
||||
|
||||
var undoData = new HandleManager.RuntimeGizmoUndoData();
|
||||
undoData.OldPosition = new SerializableTransform(bone, Space.World);
|
||||
handle.TransformReset();
|
||||
undoData.NewPosition = bone;
|
||||
|
||||
datas.Add(undoData);
|
||||
}
|
||||
|
||||
HandleManager.RegisterRuntimeGizmoUndoActions.Invoke(datas);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38509b8f63b3b4e4f9fc2cfcbf8cae5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
@@ -18,15 +16,13 @@ public class UIPopupPanel : MonoBehaviour
|
||||
{
|
||||
Owner = owner;
|
||||
transform.SetAsFirstSibling();
|
||||
NameLabel.text = owner.name;
|
||||
NameLabel.text = owner.Target.name;
|
||||
return this;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
public void UpdateManual(Camera camera)
|
||||
{
|
||||
var cam = Camera.current;
|
||||
|
||||
if (cam.WorldToScreenPoint(Owner.transform.position).x < cam.pixelWidth / 2 - _side)
|
||||
if (camera.WorldToScreenPoint(Owner.transform.position).x < camera.pixelWidth / 2 - _side)
|
||||
{
|
||||
_side = -1;
|
||||
}
|
||||
@@ -35,8 +31,8 @@ public class UIPopupPanel : MonoBehaviour
|
||||
_side = 1;
|
||||
}
|
||||
|
||||
float pos = _side * cam.pixelWidth / 20 + Offset;
|
||||
transform.position = cam.WorldToScreenPoint(Owner.transform.position) + pos * Vector3.right;
|
||||
float pos = _side * camera.pixelWidth / 20 + Offset;
|
||||
transform.position = camera.WorldToScreenPoint(Owner.transform.position) + pos * Vector3.right;
|
||||
|
||||
foreach (var kv in ConditionalButtons)
|
||||
{
|
||||
@@ -48,14 +44,14 @@ public class UIPopupPanel : MonoBehaviour
|
||||
{
|
||||
Button b = Instantiate(UmaViewerUI.Instance.HandleManager.Pfb_PopupButton, Content).GetComponent<Button>();
|
||||
b.onClick.AddListener(()=>callback.Invoke());
|
||||
b.GetComponentInChildren<Text>().text = name;
|
||||
b.GetComponentInChildren<TMPro.TMP_Text>().text = name;
|
||||
}
|
||||
|
||||
public void AddConditionalButton(string name, System.Func<bool> condition, System.Action callback)
|
||||
{
|
||||
Button b = Instantiate(UmaViewerUI.Instance.HandleManager.Pfb_PopupButton, Content).GetComponent<Button>();
|
||||
b.onClick.AddListener(() => callback.Invoke());
|
||||
b.GetComponentInChildren<Text>().text = name;
|
||||
b.GetComponentInChildren<TMPro.TMP_Text>().text = name;
|
||||
ConditionalButtons.Add(b.gameObject, condition);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user