GF.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. namespace OpenVIII.IGMDataItem.Gradient
  5. {
  6. public class GF : 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 GF()
  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 GF Create(Rectangle? pos = null)
  24. {
  25. GF r = new GF()
  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.00f;
  43. float fade = 1.00f;
  44. int total = width;
  45. Color lightline = new Color(1, 1, 255, 255);
  46. Color darkline = new Color(1, 1, 255, 255);
  47. Color fadeto = new Color(221, 237, 237, 255);
  48. Color[] cfade = new Color[total];
  49. int i;
  50. for (i = 0; i < cfade.Length - (dark * total); i++)
  51. cfade[i] = Color.Lerp(lightline, fadeto, i / (fade * total));
  52. for (; i < cfade.Length; i++)
  53. cfade[i] = darkline;
  54. common = new Texture2D(Memory.graphics.GraphicsDevice, cfade.Length, 1, false, SurfaceFormat.Color);
  55. common.SetData(cfade);
  56. //}
  57. //else throw new Exception("Must be in main thread!");
  58. }
  59. }
  60. return common;
  61. }
  62. //public override int Width { get => Data.Width; }
  63. public override void Refresh(Damageable damageable)
  64. {
  65. base.Refresh(damageable);
  66. damageable?.Refresh();
  67. }
  68. public override bool Update() => Update(null);
  69. public bool Update(float? Percent = null)
  70. {
  71. if (Enabled)
  72. {
  73. if (Damageable != null && (Percent != null || Damageable.SummonedGF!= null))
  74. {
  75. Rectangle r = Restriction;
  76. r.Width = Lerp(Width, 0, Percent ?? Damageable.SummonedGF.ATBPercent);
  77. Restriction = r;
  78. Color = Faded_Color = Color.White * .8f;
  79. if (Damageable.IsDead)
  80. {
  81. //Color = Faded_Color = Color.Red * .5f;
  82. X = 0;
  83. }
  84. }
  85. return true;
  86. }
  87. return false;
  88. int Lerp(int x, int y, float p) => (int)Math.Round(MathHelper.Lerp(x, y, p));
  89. }
  90. #endregion Methods
  91. }
  92. }