Pen.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // /******************************************************************************
  2. // * File: Pen.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using System;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.InputSystem;
  11. namespace QCHT.Samples.Drawing
  12. {
  13. /// <summary>
  14. /// Abstract class.
  15. /// Defines drawing tool (Pen)
  16. /// </summary>
  17. public abstract class Pen : MonoBehaviour
  18. {
  19. private const float GLOBAL_WIDTH_REFERENCE = 0.0015f;
  20. [Flags, Serializable]
  21. public enum DrawingMode
  22. {
  23. None = 0x0,
  24. LeftHand = 0x1,
  25. RightHand = 0x2,
  26. };
  27. /// <summary>
  28. /// The current drawing mode.
  29. /// </summary>
  30. [Header("Hands")]
  31. [SerializeField]
  32. protected DrawingMode _drawingMode = DrawingMode.RightHand;
  33. [SerializeField] protected float startDrawingTime = 1f;
  34. [Header("Pointer")]
  35. [SerializeField] protected InputAction isLeftTracked;
  36. [SerializeField] protected PencilPointer pencilLeftPointer;
  37. [SerializeField] protected InputAction isRightTracked;
  38. [SerializeField] protected PencilPointer pencilRightPointer;
  39. [Header("Brush")]
  40. [SerializeField] protected BrushDescriptor defaultBrush;
  41. [SerializeField] protected float defaultWidth = 1f;
  42. [Header("Audio")]
  43. [SerializeField] protected AudioSource audioSource;
  44. /// <summary>
  45. /// The current used brush.
  46. /// </summary>
  47. protected BrushDescriptor _brush;
  48. /// <summary>
  49. /// The current brush width.
  50. /// </summary>
  51. protected float _width;
  52. /// <summary>
  53. /// Is currently drawing ?
  54. /// </summary>
  55. private bool _isLeftDrawing;
  56. private bool _isRightDrawing;
  57. /// <summary>
  58. /// Stores each pencil lines made by the pen tool.
  59. /// </summary>
  60. protected readonly Stack<GameObject> _pencilLinesHistory = new Stack<GameObject>();
  61. /// <summary>
  62. /// Stores each pencil removed historical pencil lines.
  63. /// </summary>
  64. protected readonly Stack<GameObject> _pencilLinesRedo = new Stack<GameObject>();
  65. /// <summary>
  66. /// Validation time between pinch and start drawing.
  67. /// </summary>
  68. private float _downTime;
  69. #region MonoBehaviour Functions
  70. public void Start()
  71. {
  72. SetBrush(defaultBrush);
  73. SetWidth(defaultWidth);
  74. }
  75. public void OnEnable()
  76. {
  77. isLeftTracked.Enable();
  78. isRightTracked.Enable();
  79. }
  80. public void OnDisable()
  81. {
  82. isLeftTracked.Disable();
  83. isRightTracked.Disable();
  84. }
  85. public void Update()
  86. {
  87. UpdatePencil(true, pencilLeftPointer, ref _isLeftDrawing);
  88. UpdatePencil(false, pencilRightPointer, ref _isRightDrawing);
  89. }
  90. #endregion
  91. #region Public Functions
  92. /// <summary>
  93. /// Sets the brush descriptor for this pen.
  94. /// </summary>
  95. /// <param name="brush"> The selected brush descriptor. </param>
  96. public void SetBrush(BrushDescriptor brush)
  97. {
  98. _brush = brush;
  99. var color = _brush.Type == BrushDescriptor.ColorType.Gradient
  100. ? _brush.Gradient.Evaluate(1f)
  101. : _brush.Color;
  102. pencilLeftPointer.SetColor(color);
  103. pencilRightPointer.SetColor(color);
  104. if (_brush.LineParticles)
  105. {
  106. pencilLeftPointer.SetLineParticles(Instantiate(_brush.LineParticles).GetComponent<ParticleSystem>());
  107. pencilRightPointer.SetLineParticles(Instantiate(_brush.LineParticles).GetComponent<ParticleSystem>());
  108. }
  109. else
  110. {
  111. pencilLeftPointer.DestroyLineParticles();
  112. pencilRightPointer.DestroyLineParticles();
  113. }
  114. }
  115. /// <summary>
  116. /// Sets the width of the pencil.
  117. /// It is multiplied by the GLOBAL_WIDTH_REFERENCE in order to friendly scale it.
  118. /// </summary>
  119. /// <param name="width"> The new desired width. </param>
  120. public void SetWidth(float width)
  121. {
  122. _width = width * GLOBAL_WIDTH_REFERENCE;
  123. pencilLeftPointer.SetScale(_width);
  124. pencilRightPointer.SetScale(_width);
  125. }
  126. /// <summary>
  127. /// Sets the drawing mode.
  128. /// </summary>
  129. public void SetDrawingMode(DrawingMode drawingMode)
  130. {
  131. _drawingMode = drawingMode;
  132. }
  133. /// <summary>
  134. /// Undo the last line.
  135. /// </summary>
  136. public void Undo()
  137. {
  138. if (_pencilLinesHistory.Count == 0)
  139. return;
  140. var line = _pencilLinesHistory.Peek();
  141. if (!line)
  142. return;
  143. line = _pencilLinesHistory.Pop();
  144. line.SetActive(false);
  145. _pencilLinesRedo.Push(line);
  146. }
  147. /// <summary>
  148. /// Redo the last removed/hidden line.
  149. /// </summary>
  150. public void Redo()
  151. {
  152. if (_pencilLinesRedo.Count == 0)
  153. return;
  154. var line = _pencilLinesRedo.Pop();
  155. line.SetActive(true);
  156. _pencilLinesHistory.Push(line);
  157. }
  158. /// <summary>
  159. /// Clear all lines and clear the history.
  160. /// </summary>
  161. public void Clear()
  162. {
  163. foreach (var line in _pencilLinesHistory)
  164. Destroy(line);
  165. foreach (var line in _pencilLinesRedo)
  166. Destroy(line);
  167. OnPenClear();
  168. }
  169. #endregion
  170. /// <summary>
  171. /// Performs drawing logic for a given hand.
  172. /// </summary>
  173. /// <param name="isLeft">Is left hand?</param>
  174. /// <param name="pencilPointer">The pencil pointer to update.</param>
  175. /// <param name="isDrawing">Is hand currently drawing?</param>
  176. private void UpdatePencil(bool isLeft, PencilPointer pencilPointer, ref bool isDrawing)
  177. {
  178. var checkActive = isLeft
  179. ? _drawingMode.HasFlag(DrawingMode.LeftHand)
  180. : _drawingMode.HasFlag(DrawingMode.RightHand);
  181. var isTracked = isLeft ? isLeftTracked.IsPressed() : isRightTracked.IsPressed();
  182. if (!checkActive || !isTracked)
  183. {
  184. pencilPointer.Hide();
  185. if (!isDrawing) return;
  186. isDrawing = false;
  187. OnPenUp(isLeft);
  188. pencilPointer.StopLineParticles();
  189. return;
  190. }
  191. pencilPointer.Show();
  192. UpdatePointer(isLeft, pencilPointer);
  193. // Pen down wait before starting to draw
  194. if (!isDrawing && pencilPointer.PenDown.IsPressed())
  195. _downTime += Time.deltaTime;
  196. // Start drawing
  197. if (_downTime >= startDrawingTime)
  198. {
  199. _downTime = 0f;
  200. isDrawing = true;
  201. OnPenDown(isLeft);
  202. pencilPointer.StartLineParticles();
  203. if (!audioSource && !_brush.StartDrawing)
  204. audioSource.PlayOneShot(_brush.StartDrawing);
  205. }
  206. // Pen up
  207. if (isDrawing && !pencilPointer.PenDown.IsPressed())
  208. {
  209. isDrawing = false;
  210. OnPenUp(isLeft);
  211. pencilPointer.StopLineParticles();
  212. }
  213. // Drawing
  214. if (isDrawing)
  215. {
  216. OnPenDrawingUpdate(isLeft);
  217. }
  218. else
  219. {
  220. pencilPointer.Show();
  221. }
  222. }
  223. /// <summary>
  224. /// Updates the pointer position and scale.
  225. /// </summary>
  226. private static void UpdatePointer(bool isLeft, PencilPointer pencilPointer)
  227. {
  228. pencilPointer.UpdateScale();
  229. }
  230. #region Abstract Functions
  231. /// <summary>
  232. /// Called when the pen started drawing.
  233. /// </summary>
  234. protected abstract void OnPenDown(bool isLeft);
  235. /// <summary>
  236. /// Called when the pen stopped drawing.
  237. /// </summary>
  238. protected abstract void OnPenUp(bool isLeft);
  239. /// <summary>
  240. /// Called each frame when the pen currently drawing.
  241. /// </summary>
  242. protected abstract void OnPenDrawingUpdate(bool isLeft);
  243. /// <summary>
  244. /// Called when pen was cleared.
  245. /// </summary>
  246. protected abstract void OnPenClear();
  247. #endregion
  248. }
  249. }