DrawScript.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. public class DrawScript : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
  8. {
  9. public DrawShape ArrowPrefab;
  10. public DrawShape TextPrefab;
  11. DrawPaintbrush PaintbrushPrefab;
  12. public DrawShape TargetPrefab;
  13. private RectTransform rectTransform_;
  14. Vector2 posMin;
  15. Vector2 posMax;
  16. private List<GameObject> drawShapeList_ = new List<GameObject>();
  17. private List<DrawShape> allShapesRedo_ = new List<DrawShape>();
  18. public void Clear()
  19. {
  20. foreach (GameObject obj in drawShapeList_)
  21. {
  22. Destroy(obj);
  23. }
  24. drawShapeList_.Clear();
  25. PaintbrushPrefab.Clear();
  26. }
  27. void Start ()
  28. {
  29. rectTransform_ = GetComponent<RectTransform>();
  30. print("rectTransform_ " + rectTransform_.gameObject.name);
  31. PaintbrushPrefab = gameObject.GetComponentsInChildren<DrawPaintbrush>(true)[0];
  32. posMin = rectTransform_.TransformPoint(new Vector2(0, 0));
  33. posMax = rectTransform_.TransformPoint(new Vector2(rectTransform_.rect.width - 1, rectTransform_.rect.height - 1));
  34. }
  35. void Update ()
  36. {
  37. var mousePosition = Input.mousePosition;
  38. if (mousePosition.x > posMin.x &&
  39. mousePosition.x < posMax.x &&
  40. mousePosition.y > posMin.y &&
  41. mousePosition.y < posMax.y)
  42. {
  43. var canUpdateShape = CurrentShapeToDraw != null && IsDrawingShape;
  44. if (canUpdateShape)
  45. {
  46. UpdateShapeVertex(Input.mousePosition);
  47. }
  48. }
  49. }
  50. private DrawShape CurrentShapeToDraw { get; set; }
  51. private bool IsDrawingShape { get; set; }
  52. public void OnPointerDown(PointerEventData eventData)
  53. {
  54. StartDraw(Input.mousePosition);
  55. }
  56. public void OnPointerUp(PointerEventData eventData)
  57. {
  58. StopDraw(Input.mousePosition);
  59. }
  60. public enum DRAW_MODE
  61. {
  62. NO_DRAW,
  63. PAINTBRUSH,
  64. TEXT,
  65. ARROW,
  66. TARGET
  67. };
  68. public DRAW_MODE DrawMode
  69. {
  70. set { drawMode_ = value; }
  71. }
  72. private void StartDraw(Vector2 position)
  73. {
  74. GameObject go;
  75. switch (drawMode_)
  76. {
  77. case DRAW_MODE.ARROW:
  78. CurrentShapeToDraw = Instantiate(ArrowPrefab);
  79. CurrentShapeToDraw.name = "Shape " + _allShapes.Count;
  80. CurrentShapeToDraw.transform.SetParent(transform);
  81. go = GameObject.Find(CurrentShapeToDraw.name);
  82. drawShapeList_.Add(go);
  83. break;
  84. case DRAW_MODE.TEXT:
  85. CurrentShapeToDraw = Instantiate(TextPrefab);
  86. CurrentShapeToDraw.name = "Shape " + _allShapes.Count;
  87. CurrentShapeToDraw.transform.SetParent(transform);
  88. go = GameObject.Find(CurrentShapeToDraw.name);
  89. drawShapeList_.Add(go);
  90. break;
  91. case DRAW_MODE.TARGET:
  92. CurrentShapeToDraw = Instantiate(TargetPrefab);
  93. CurrentShapeToDraw.name = "Shape " + _allShapes.Count;
  94. CurrentShapeToDraw.transform.SetParent(transform);
  95. go = GameObject.Find(CurrentShapeToDraw.name);
  96. drawShapeList_.Add(go);
  97. break;
  98. case DRAW_MODE.PAINTBRUSH:
  99. CurrentShapeToDraw = PaintbrushPrefab;
  100. break;
  101. case DRAW_MODE.NO_DRAW:
  102. return;
  103. }
  104. CurrentShapeToDraw.StartDraw(position);
  105. IsDrawingShape = true;
  106. _allShapes.Add(CurrentShapeToDraw);
  107. }
  108. public void Undo()
  109. {
  110. var last = _allShapes.Last();
  111. last.gameObject.SetActive(false);
  112. print("_allShapes " + _allShapes.Count);
  113. _allShapes.RemoveRange(_allShapes.Count - 1, 1);
  114. allShapesRedo_.Add(last);
  115. print("_allShapes " + _allShapes.Count);
  116. }
  117. private void StopDraw(Vector2 position)
  118. {
  119. if (CurrentShapeToDraw != null)
  120. {
  121. CurrentShapeToDraw.StopDraw(position);
  122. CurrentShapeToDraw = null;
  123. }
  124. }
  125. private void UpdateShapeVertex(Vector2 position)
  126. {
  127. if (CurrentShapeToDraw == null)
  128. {
  129. return;
  130. }
  131. CurrentShapeToDraw.UpdateShape(position);
  132. }
  133. DRAW_MODE drawMode_;
  134. private List<DrawShape> _allShapes = new List<DrawShape>();
  135. }