Margin.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace TheraBytes.BetterUi
  7. {
  8. [Serializable]
  9. public class Margin
  10. {
  11. public float Horizontal { get { return left + right; } }
  12. public float Vertical { get { return top + bottom; } }
  13. public int Left { get { return left; } set { left = value; } }
  14. public int Right { get { return right; } set { right = value; } }
  15. public int Top { get { return top; } set { top = value; } }
  16. public int Bottom { get { return bottom; } set { bottom = value; } }
  17. [SerializeField]
  18. int left, right, top, bottom;
  19. public int this[int idx]
  20. {
  21. get
  22. {
  23. switch (idx)
  24. {
  25. case 0: return left;
  26. case 1: return right;
  27. case 2: return top;
  28. default: return bottom;
  29. }
  30. }
  31. set
  32. {
  33. switch (idx)
  34. {
  35. case 0: left = value; break;
  36. case 1: right = value; break;
  37. case 2: top = value; break;
  38. default: bottom = value; break;
  39. }
  40. }
  41. }
  42. public Margin()
  43. : this(0, 0, 0, 0)
  44. { }
  45. public Margin(Vector4 source)
  46. : this((int)source.x, (int)source.z, (int)source.y, (int)source.w)
  47. {
  48. }
  49. public Margin(RectOffset source)
  50. : this(source.left, source.right, source.top, source.bottom)
  51. {
  52. }
  53. public Margin(int left, int right, int top, int bottom)
  54. {
  55. this.left = left;
  56. this.right = right;
  57. this.top = top;
  58. this.bottom = bottom;
  59. }
  60. public Margin Clone()
  61. {
  62. return new Margin(this.left, this.right, this.top, this.bottom);
  63. }
  64. public void CopyValuesTo(RectOffset target)
  65. {
  66. target.left = this.left;
  67. target.right = this.right;
  68. target.top = this.top;
  69. target.bottom = this.bottom;
  70. }
  71. public Vector4 ToVector4()
  72. {
  73. return new Vector4(left, top, right, bottom);
  74. }
  75. public override string ToString()
  76. {
  77. return string.Format("(left: {0}, right: {1}, top: {2}, bottom: {3})", left, right, top, bottom);
  78. }
  79. }
  80. }