GF.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  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. var r = new GF
  26. {
  27. _pos = pos ?? Rectangle.Empty,
  28. Restriction = pos ?? Rectangle.Empty
  29. };
  30. Memory.MainThreadOnlyActions.Enqueue(
  31. () =>
  32. {
  33. r.Data = ThreadUnsafeOperations(r.Width);
  34. r.Width = r.Data.Width;
  35. }
  36. );
  37. return r;
  38. }
  39. public static Texture2D ThreadUnsafeOperations(int width)
  40. {
  41. lock (locker)
  42. {
  43. if (common == null)
  44. {
  45. //if (Memory.IsMainThread)
  46. //{
  47. var dark = 0.00f;
  48. var fade = 1.00f;
  49. var total = width;
  50. var lightline = new Color(1, 1, 255, 255);
  51. var darkline = new Color(1, 1, 255, 255);
  52. var fadeto = new Color(221, 237, 237, 255);
  53. var cfade = new Color[total];
  54. int i;
  55. for (i = 0; i < cfade.Length - (dark * total); i++)
  56. cfade[i] = Color.Lerp(lightline, fadeto, i / (fade * total));
  57. for (; i < cfade.Length; i++)
  58. cfade[i] = darkline;
  59. common = new Texture2D(Memory.Graphics.GraphicsDevice, cfade.Length, 1, false, SurfaceFormat.Color);
  60. common.SetData(cfade);
  61. //}
  62. //else throw new Exception("Must be in main thread!");
  63. }
  64. }
  65. return common;
  66. }
  67. //public override int Width { get => Data.Width; }
  68. public override void Refresh(Damageable damageable)
  69. {
  70. base.Refresh(damageable);
  71. damageable?.Refresh();
  72. }
  73. public override bool Update() => Update();
  74. public bool Update(float? Percent = null)
  75. {
  76. if (Enabled)
  77. {
  78. if (Damageable != null && (Percent != null || Damageable.SummonedGF != null))
  79. {
  80. var r = Restriction;
  81. r.Width = Lerp(Width, 0, Percent ?? Damageable.SummonedGF.ATBPercent);
  82. Restriction = r;
  83. Color = Faded_Color = Color.White * .8f;
  84. if (Damageable.IsDead)
  85. {
  86. //Color = Faded_Color = Color.Red * .5f;
  87. X = 0;
  88. }
  89. }
  90. return true;
  91. }
  92. return false;
  93. int Lerp(int x, int y, float p) => (int)Math.Round(MathHelper.Lerp(x, y, p));
  94. }
  95. #endregion Methods
  96. }
  97. }