NamesHPATB.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using OpenVIII.IGMDataItem;
  4. using OpenVIII.IGMDataItem.Gradient;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using Texture = OpenVIII.IGMDataItem.Texture;
  9. namespace OpenVIII.IGMData
  10. {
  11. public class NamesHPATB : Base
  12. {
  13. #region Fields
  14. private const float ATBAlpha = .8f;
  15. private const int ATBWidth = 150;
  16. private static Texture2D _dot;
  17. private Damageable.BattleMode _battleMode = Damageable.BattleMode.EndTurn;
  18. private bool _eventAdded;
  19. #endregion Fields
  20. #region Destructors
  21. ~NamesHPATB()
  22. {
  23. if (_eventAdded)
  24. Damageable.BattleModeChangeEventHandler -= ModeChangeEvent;
  25. }
  26. #endregion Destructors
  27. #region Enums
  28. private enum DepthID : byte
  29. {
  30. /// <summary>
  31. /// Name
  32. /// </summary>
  33. Name,
  34. /// <summary>
  35. /// HP
  36. /// </summary>
  37. HP,
  38. /// <summary>
  39. /// Box with GF HP
  40. /// </summary>
  41. GFHPBox,
  42. /// <summary>
  43. /// Box with GF name
  44. /// </summary>
  45. GFNameBox,
  46. /// <summary>
  47. /// ATB charging orange red or dark blue (haste/slowed)
  48. /// </summary>
  49. ATBCharging,
  50. /// <summary>
  51. /// ATB charged blink yellow
  52. /// </summary>
  53. ATBCharged,
  54. /// <summary>
  55. /// blue white gradient that decreases as the gf is charging.
  56. /// </summary>
  57. GFCharging,
  58. /// <summary>
  59. /// border around ATB bar
  60. /// </summary>
  61. ATBBorder,
  62. /// <summary>
  63. /// Max?
  64. /// </summary>
  65. Max
  66. }
  67. #endregion Enums
  68. #region Methods
  69. public static NamesHPATB Create(Rectangle pos, Damageable damageable) => Create<NamesHPATB>(1, (int)DepthID.Max, new Empty { Pos = pos }, 1, 1, damageable);
  70. public override void ModeChangeEvent(object sender, Enum e)
  71. {
  72. if (!e.Equals(_battleMode))
  73. {
  74. base.ModeChangeEvent(sender, e);
  75. _battleMode = (Damageable.BattleMode)e;
  76. if (!e.Equals(Damageable.BattleMode.EndTurn)) //because endturn triggers BattleMenu refresh.
  77. Refresh();
  78. }
  79. }
  80. public override void Refresh(Damageable damageable)
  81. {
  82. if (_eventAdded && damageable != Damageable)
  83. {
  84. _eventAdded = false;
  85. if (Damageable != null)
  86. Damageable.BattleModeChangeEventHandler -= ModeChangeEvent;
  87. }
  88. base.Refresh(damageable);
  89. }
  90. public override void Refresh()
  91. {
  92. if (Damageable == null) return;
  93. if (Memory.State?.Characters != null && Damageable.GetCharacterData(out _))
  94. {
  95. var party = GetParty();
  96. if (GetCharPos(party) == 0xFF) return;
  97. }
  98. var pos = PartyPos;
  99. var rectangle = SIZE[0];
  100. rectangle.Offset(0f, SIZE[0].Height * pos);
  101. var atbBarPos = new Rectangle(rectangle.X + 230, rectangle.Y + 12, ATBWidth, 15);
  102. ((ATB)ITEM[0, (int)DepthID.ATBCharging]).Pos = atbBarPos;
  103. ((Texture)ITEM[0, (byte)DepthID.ATBCharged]).Pos = atbBarPos;
  104. ((Icon)ITEM[0, (byte)DepthID.ATBBorder]).Pos = atbBarPos;
  105. ((GF)ITEM[0, (byte)DepthID.GFCharging]).Pos = atbBarPos;
  106. ((Text)ITEM[0, (byte)DepthID.Name]).Data = Damageable.Name;
  107. ((Text)ITEM[0, (byte)DepthID.Name]).Pos = new Rectangle(rectangle.X - 60, rectangle.Y, 0, 0);
  108. ((Integer)ITEM[0, (byte)DepthID.HP]).Pos = new Rectangle(rectangle.X + 128, rectangle.Y, 0, 0);
  109. ((Text)ITEM[0, (byte)DepthID.Name]).Draw(true);
  110. ((Integer)ITEM[0, (byte)DepthID.HP]).Draw(true);
  111. ((Box)ITEM[0, (byte)DepthID.GFHPBox]).Pos = Rectangle.Union(
  112. ((Text)ITEM[0, (byte)DepthID.Name]).DataSize,
  113. ((Integer)ITEM[0, (byte)DepthID.HP]).DataSize);
  114. ((Box)ITEM[0, (byte)DepthID.GFHPBox]).Y -= 4;
  115. ((Box)ITEM[0, (byte)DepthID.GFNameBox]).Y = ((Box)ITEM[0, (byte)DepthID.GFHPBox]).Y;
  116. ((Box)ITEM[0, (byte)DepthID.GFNameBox]).Height = ((Box)ITEM[0, (byte)DepthID.GFHPBox]).Height = rectangle.Height;
  117. ((Box)ITEM[0, (byte)DepthID.GFNameBox]).X =
  118. ((Box)ITEM[0, (byte)DepthID.GFHPBox]).X -
  119. (((Box)ITEM[0, (byte)DepthID.GFHPBox]).Width * 3) / 8;
  120. ((Box)ITEM[0, (byte)DepthID.GFNameBox]).Width = (((Box)ITEM[0, (byte)DepthID.GFHPBox]).Width * 7) / 8;
  121. if (Damageable.SummonedGF != null)
  122. {
  123. ((Box)ITEM[0, (byte)DepthID.GFNameBox]).Data = Damageable.SummonedGF.Name;
  124. ((Box)ITEM[0, (byte)DepthID.GFHPBox]).Data = $"{Damageable.SummonedGF.CurrentHP()}";
  125. }
  126. if (_eventAdded == false)
  127. {
  128. _eventAdded = true;
  129. Damageable.BattleModeChangeEventHandler += ModeChangeEvent;
  130. }
  131. var blink = false;
  132. var charging = false;
  133. _battleMode = (Damageable.BattleMode)Damageable.GetBattleMode();
  134. ITEM[0, (byte)DepthID.HP].Show();
  135. ITEM[0, (byte)DepthID.Name].Show();
  136. ITEM[0, (byte)DepthID.GFNameBox].Hide();
  137. ITEM[0, (byte)DepthID.GFHPBox].Hide();
  138. ITEM[0, (int)DepthID.ATBCharged].Hide();
  139. ITEM[0, (int)DepthID.GFCharging].Hide();
  140. ITEM[0, (int)DepthID.ATBCharging].Hide();
  141. switch (_battleMode)
  142. {
  143. case Damageable.BattleMode.YourTurn:
  144. ((Texture)ITEM[0, (int)DepthID.ATBCharged]).Color = Color.LightYellow * ATBAlpha;
  145. blink = true;
  146. break;
  147. case Damageable.BattleMode.ATB_Charged:
  148. ((Texture)ITEM[0, (int)DepthID.ATBCharged]).Color = Color.Yellow * ATBAlpha;
  149. break;
  150. case Damageable.BattleMode.ATB_Charging:
  151. charging = true;
  152. ((ATB)ITEM[0, (int)DepthID.ATBCharging]).Refresh(Damageable);
  153. ITEM[0, (int)DepthID.ATBCharging].Show();
  154. break;
  155. case Damageable.BattleMode.GF_Charging:
  156. charging = true;
  157. ITEM[0, (byte)DepthID.HP].Hide();
  158. ITEM[0, (byte)DepthID.Name].Hide();
  159. ITEM[0, (byte)DepthID.GFNameBox].Show();
  160. ITEM[0, (byte)DepthID.GFHPBox].Show();
  161. ITEM[0, (int)DepthID.GFCharging].Show();
  162. ITEM[0, (int)DepthID.GFCharging].Refresh(Damageable.SummonedGF);
  163. break;
  164. case Damageable.BattleMode.EndTurn:
  165. break;
  166. default:
  167. throw new ArgumentOutOfRangeException();
  168. }
  169. if (!charging)
  170. {
  171. ITEM[0, (int)DepthID.ATBCharged].Show();
  172. }
  173. ((Texture)ITEM[0, (int)DepthID.ATBCharged]).Blink = blink;
  174. ((Text)ITEM[0, (byte)DepthID.Name]).Blink = blink;
  175. ((Integer)ITEM[0, (byte)DepthID.HP]).Blink = blink;
  176. base.Refresh();
  177. }
  178. public void ThreadUnsafeOperations()
  179. {
  180. if (_dot == null)
  181. {
  182. if (Memory.IsMainThread)
  183. {
  184. var texture2D = new Texture2D(Memory.Graphics.GraphicsDevice, 4, 4);
  185. var tmp = new Color[texture2D.Height * texture2D.Width];
  186. for (var i = 0; i < tmp.Length; i++)
  187. tmp[i] = Color.White;
  188. texture2D.SetData(tmp);
  189. _dot = texture2D;
  190. ATB.ThreadUnsafeOperations(ATBWidth);
  191. }
  192. else throw new Exception("Must be in main thread!");
  193. }
  194. if (_dot != null)
  195. ((Texture)(ITEM[0, (byte)DepthID.ATBCharged])).
  196. Data = _dot;
  197. }
  198. public override bool Update()
  199. {
  200. if (ITEM[0, 2] is ATB)
  201. {
  202. // unsure if need
  203. }
  204. if (Damageable == null) return base.Update();
  205. int hp = Damageable.CurrentHP();
  206. int criticalHP = Damageable.CriticalHP();
  207. var colorID = Font.ColorID.White;
  208. if (hp < criticalHP)
  209. {
  210. colorID = Font.ColorID.Yellow;
  211. }
  212. if (hp <= 0)
  213. {
  214. colorID = Font.ColorID.Red;
  215. }
  216. ((Text)ITEM[0, (byte)DepthID.Name]).FontColor = colorID;
  217. ((Integer)ITEM[0, (byte)DepthID.HP]).Data = hp;
  218. ((Integer)ITEM[0, (byte)DepthID.HP]).FontColor = colorID;
  219. return base.Update();
  220. }
  221. protected override void Init()
  222. {
  223. base.Init();
  224. Memory.MainThreadOnlyActions.Enqueue(ThreadUnsafeOperations);
  225. // TODO: make a font render that can draw right to left from a point. For Right aligning the names.
  226. var atbBarPos = new Rectangle(SIZE[0].X + 230, SIZE[0].Y + 12, ATBWidth, 15);
  227. ITEM[0, (byte)DepthID.Name] = new Text();
  228. ITEM[0, (byte)DepthID.HP] = new Integer { Spaces = 4, NumType = Icons.NumType.Num8X16A };
  229. ITEM[0, (byte)DepthID.GFHPBox] = new Box { Options = Box_Options.Right | Box_Options.Middle };
  230. ITEM[0, (byte)DepthID.GFHPBox].Hide();
  231. ITEM[0, (byte)DepthID.GFNameBox] = new Box { Options = Box_Options.Center | Box_Options.Middle };
  232. ITEM[0, (byte)DepthID.GFNameBox].Hide();
  233. ITEM[0, (byte)DepthID.ATBBorder] = new Icon { Data = Icons.ID.Size_08x64_Bar, Palette = 0 };
  234. ITEM[0, (byte)DepthID.ATBCharged] = new Texture { Color = Color.LightYellow * ATBAlpha, Faded_Color = new Color(125, 125, 0, 255) * ATBAlpha };
  235. ITEM[0, (byte)DepthID.ATBCharged].Hide();
  236. ITEM[0, (int)DepthID.ATBCharging] = ATB.Create(atbBarPos);
  237. ITEM[0, (int)DepthID.GFCharging] = GF.Create(atbBarPos);
  238. ((ATB)ITEM[0, (byte)DepthID.ATBCharging]).Color = Color.Orange * ATBAlpha;
  239. ((ATB)ITEM[0, (byte)DepthID.ATBCharging]).Faded_Color = Color.Orange * ATBAlpha;
  240. ((ATB)ITEM[0, (byte)DepthID.ATBCharging]).Refresh(Damageable);
  241. }
  242. private static List<KeyValuePair<int, Characters>> GetParty()
  243. {
  244. if (Memory.State != null && Memory.State.Characters)
  245. return Memory.State.Party.Select((element, index) => new { element, index }).ToDictionary(m => m.index, m => m.element).Where(m => !m.Value.Equals(Characters.Blank)).ToList();
  246. return null;
  247. }
  248. private byte GetCharPos(List<KeyValuePair<int, Characters>> party)
  249. {
  250. int i;
  251. if (party != null && (i = party.FindIndex(x => Damageable.GetCharacterData(out var c) && x.Value == c.ID)) > -1)
  252. return checked((byte)i);
  253. return 0xFF;
  254. }
  255. #endregion Methods
  256. }
  257. }