ATB.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. namespace OpenVIII.IGMDataItem.Gradient
  5. {
  6. public class ATB : Texture
  7. {
  8. #region Fields
  9. private static Texture2D common;
  10. private static object locker = new object();
  11. #endregion Fields
  12. #region Constructors
  13. private ATB()
  14. {
  15. }
  16. #endregion Constructors
  17. #region Properties
  18. ///Restriction controls the bounds of the drawing. And Pos is where it will draw.
  19. ///So if one is set and the other is not than you see no bar.
  20. public override Rectangle Pos { get => base.Pos; set => Restriction = base.Pos = value; }
  21. #endregion Properties
  22. #region Methods
  23. public static ATB Create(Rectangle? pos = null)
  24. {
  25. ATB r = new ATB()
  26. {
  27. _pos = pos ?? Rectangle.Empty,
  28. Restriction = pos ?? Rectangle.Empty,
  29. };
  30. r.Data = ThreadUnsafeOperations(r.Width);
  31. r.Width = r.Data.Width;
  32. return r;
  33. }
  34. public static Texture2D ThreadUnsafeOperations(int width)
  35. {
  36. lock (locker)
  37. {
  38. if (common == null)
  39. {
  40. //if (Memory.IsMainThread)
  41. //{
  42. float dark = 0.067f;
  43. float fade = 0.933f;
  44. int total = width;
  45. Color lightline = new Color(118, 118, 118, 255);
  46. Color darkline = new Color(58, 58, 58, 255);
  47. Color[] cfade = new Color[total];
  48. int i;
  49. for (i = 0; i < cfade.Length - (dark * total); i++)
  50. cfade[i] = Color.Lerp(Color.Black, lightline, i / (fade * total));
  51. for (; i < cfade.Length; i++)
  52. cfade[i] = darkline;
  53. common = new Texture2D(Memory.graphics.GraphicsDevice, cfade.Length, 1, false, SurfaceFormat.Color);
  54. common.SetData(cfade);
  55. //}
  56. //else throw new Exception("Must be in main thread!");
  57. }
  58. }
  59. return common;
  60. }
  61. //public override int Width { get => Data.Width; }
  62. public override void Refresh(Damageable damageable)
  63. {
  64. base.Refresh(damageable);
  65. damageable?.Refresh();
  66. }
  67. public override bool Update()
  68. {
  69. if (Enabled)
  70. {
  71. if (Damageable != null)
  72. {
  73. X = Lerp(Restriction.X - Width, Restriction.X, Damageable.ATBPercent);
  74. if (Damageable.IsDead)
  75. {
  76. //Color = Faded_Color = Color.Red * .5f;
  77. X = 0;
  78. }
  79. else if (Damageable.IsPetrify)
  80. {
  81. Color = Faded_Color = Color.Gray * .8f;
  82. }
  83. else if ((Damageable.Statuses1 & Kernel_bin.Battle_Only_Statuses.Stop) != 0)
  84. {
  85. Color = Faded_Color = Color.DarkBlue * .8f;
  86. }
  87. else if ((Damageable.Statuses1 & Kernel_bin.Battle_Only_Statuses.Slow) != 0)
  88. {
  89. Color = Faded_Color = Color.DarkCyan * .8f;
  90. }
  91. else if ((Damageable.Statuses1 & Kernel_bin.Battle_Only_Statuses.Haste) != 0)
  92. {
  93. Color = Faded_Color = Color.Violet * .8f;
  94. }
  95. else Color = Faded_Color = Color.Orange * .8f;
  96. }
  97. return true;
  98. }
  99. return false;
  100. int Lerp(int x, int y, float p) => (int)Math.Round(MathHelper.Lerp(x, y, p));
  101. }
  102. #endregion Methods
  103. }
  104. }