DrawPaintbrush.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class DrawPaintbrush : DrawShape
  5. {
  6. private RawImage drawImage_;
  7. private RectTransform rectTransform_;
  8. Vector2 prevPos_;
  9. private void Start()
  10. {
  11. drawImage_ = GetComponent<RawImage>();
  12. rectTransform_ = GetComponent<RectTransform>();
  13. PrepareDrawTexture();
  14. }
  15. public override void StartDraw(Vector2 position)
  16. {
  17. Debug.Log("draw position " + position);
  18. prevPos_ = rectTransform_.InverseTransformPoint(position);
  19. }
  20. public override void StopDraw(Vector2 position)
  21. {
  22. }
  23. public override void UpdateShape(Vector2 newPosition)
  24. {
  25. Vector2 position = rectTransform_.InverseTransformPoint(newPosition);
  26. Texture2D tex = (Texture2D)drawImage_.texture;
  27. DrawLine(tex, (int)position.x, (int)position.y, (int)prevPos_.x, (int)prevPos_.y, Color.black);
  28. prevPos_ = position;
  29. }
  30. public override void Clear()
  31. {
  32. ClearDrawTexture(drawImage_.texture as Texture2D);
  33. }
  34. private void PrepareDrawTexture()
  35. {
  36. int WIDTH = (int)rectTransform_.rect.width;
  37. int HEIGHT = (int)rectTransform_.rect.height;
  38. Texture2D tex = new Texture2D(WIDTH, HEIGHT, TextureFormat.ARGB32, false);
  39. ClearDrawTexture(tex);
  40. drawImage_.texture = tex;
  41. }
  42. private void ClearDrawTexture(Texture2D tex)
  43. {
  44. Color fillColor = Color.clear;
  45. Color[] fillPixels = new Color[tex.width * tex.height];
  46. for (int i = 0; i < fillPixels.Length; i++)
  47. {
  48. fillPixels[i] = fillColor;
  49. }
  50. tex.SetPixels(fillPixels);
  51. tex.Apply();
  52. }
  53. private void DrawLine(Texture2D tex, int x0, int y0, int x1, int y1, Color col)
  54. {
  55. int dy = (int)(y1 - y0);
  56. int dx = (int)(x1 - x0);
  57. int stepx, stepy;
  58. if (dy < 0) { dy = -dy; stepy = -1; }
  59. else { stepy = 1; }
  60. if (dx < 0) { dx = -dx; stepx = -1; }
  61. else { stepx = 1; }
  62. dy <<= 1;
  63. dx <<= 1;
  64. float fraction = 0;
  65. tex.SetPixel(x0, y0, col);
  66. if (dx > dy)
  67. {
  68. fraction = dy - (dx >> 1);
  69. while (Mathf.Abs(x0 - x1) > 1)
  70. {
  71. if (fraction >= 0)
  72. {
  73. y0 += stepy;
  74. fraction -= dx;
  75. }
  76. x0 += stepx;
  77. fraction += dy;
  78. tex.SetPixel(x0, y0, col);
  79. tex.SetPixel(x0 - 1, y0, col);
  80. tex.SetPixel(x0 + 1, y0, col);
  81. tex.SetPixel(x0, y0 - 1, col);
  82. tex.SetPixel(x0, y0 + 1, col);
  83. }
  84. }
  85. else
  86. {
  87. fraction = dx - (dy >> 1);
  88. while (Mathf.Abs(y0 - y1) > 1)
  89. {
  90. if (fraction >= 0)
  91. {
  92. x0 += stepx;
  93. fraction -= dy;
  94. }
  95. y0 += stepy;
  96. fraction += dx;
  97. tex.SetPixel(x0, y0, col);
  98. tex.SetPixel(x0 - 1, y0, col);
  99. tex.SetPixel(x0 + 1, y0, col);
  100. tex.SetPixel(x0, y0 - 1, col);
  101. tex.SetPixel(x0, y0 + 1, col);
  102. }
  103. }
  104. tex.Apply();
  105. }
  106. }