TranslateControl.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Receives translations from separate TranslateArrow objects, which should be child objects,
  6. /// and applies them to the ZED via CameraAnchor. Also moves the arrow holder object accordingly.
  7. /// When those children are dragged, the distance from this transform affects how quickly the ZED moves.
  8. /// See parent class TransformControl to see how visual elements work.
  9. /// </summary>
  10. public class TranslateControl : TransformControl
  11. {
  12. /// <summary>
  13. /// Max distance that you can drag the arrows on any axis.
  14. /// </summary>
  15. public float maxUIMoveDistance = 0.1f;
  16. private Vector3 startPosLocal = Vector3.zero;
  17. protected override void Awake ()
  18. {
  19. base.Awake();
  20. if (!anchor) anchor = FindObjectOfType<CameraAnchor>(); //For now.
  21. CameraAnchor.OnCameraAnchorCreated += SetNewAnchor;
  22. startPosLocal = visualsParent.localPosition;
  23. }
  24. /// <summary>
  25. /// Takes how far a given arrow has been moved, and turns it to a call to CameraAnchor
  26. /// to move the ZED gradually.
  27. /// </summary>
  28. public void Translate(Vector3 localdistmoved)
  29. {
  30. Vector3 oldvector = visualsParent.localPosition;
  31. //Reflect translation in UI element (this thing).
  32. Vector3 clampdist;
  33. clampdist.x = Mathf.Clamp(localdistmoved.x, -maxUIMoveDistance, maxUIMoveDistance);
  34. clampdist.y = Mathf.Clamp(localdistmoved.y, -maxUIMoveDistance, maxUIMoveDistance);
  35. clampdist.z = Mathf.Clamp(localdistmoved.z, -maxUIMoveDistance, maxUIMoveDistance);
  36. //transform.localPosition = startPosLocal + transform.rotation * clampdist;
  37. visualsParent.localPosition = startPosLocal + clampdist;
  38. //Get clamped value and send to anchor to move the actual ZED.
  39. if (anchor)
  40. {
  41. Vector3 finaldist = clampdist / maxUIMoveDistance;
  42. anchor.TranslateZEDIncrementally(finaldist);
  43. }
  44. PlayTapSoundIfNeeded(oldvector / maxUIMoveDistance, visualsParent.localPosition / maxUIMoveDistance);
  45. }
  46. /// <summary>
  47. /// Changes the anchor object.
  48. /// </summary>
  49. private void SetNewAnchor(CameraAnchor newanchor)
  50. {
  51. anchor = newanchor;
  52. //TODO: Reposition arrows accordingly.
  53. }
  54. /// <summary>
  55. /// Displays the instructions for calibration in manual mode.
  56. /// <para>his could have been put in any class used for manual transform mode exclusively - this was chosen arbitrarily.1</para>T
  57. /// </summary>
  58. private void OnEnable()
  59. {
  60. MessageDisplay.DisplayMessageAll("MANUAL MODE\r\nGrab the colored arrows/circles to move the ZED.\r\n" +
  61. "Position the 3D model of the ZED like it is in real-life.");
  62. }
  63. }