DrawPaintbrush.cs 3.2 KB

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