Menu_Base.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Linq;
  4. namespace OpenVIII
  5. {
  6. /// <summary>
  7. /// Root class all menu objects can grow from.
  8. /// </summary>
  9. public abstract class Menu_Base
  10. {
  11. #region Fields
  12. protected Rectangle _pos;
  13. #endregion Fields
  14. #region Properties
  15. /// <summary>
  16. /// Focus scales and centers the menu.
  17. /// </summary>
  18. public static Matrix Focus { get; protected set; }
  19. /// <summary>
  20. /// Adjusted mouse location used to determine if mouse is highlighting a button.
  21. /// </summary>
  22. public static Point MouseLocation => InputMouse.Location.Transform(Focus);
  23. public static Point ScreenBottomLeft => new Point(0, Memory.Graphics.GraphicsDevice.Viewport.Height).Transform(Focus);
  24. public static Point ScreenBottomRight => new Point(Memory.Graphics.GraphicsDevice.Viewport.Width, Memory.Graphics.GraphicsDevice.Viewport.Height).Transform(Focus);
  25. public static Point ScreenTopLeft => new Point(0, 0).Transform(Focus);
  26. public static Point ScreenTopRight => new Point(Memory.Graphics.GraphicsDevice.Viewport.Width, 0).Transform(Focus);
  27. public Menu_Base CONTAINER { get; set; }
  28. public Cursor_Status Cursor_Status { get; set; } = Cursor_Status.Disabled;
  29. /// <summary>
  30. /// Characters/Enemies/GF
  31. /// </summary>
  32. public virtual Damageable Damageable { get; protected set; }
  33. /// <summary>
  34. /// If enabled the menu is Visible and all functionality works. Else everything is hidden and
  35. /// nothing functions.
  36. /// </summary>
  37. public bool Enabled { get; private set; } = true;
  38. public virtual int Height { get => _pos.Height; set => _pos.Height = value; }
  39. public OffsetAnchor OffsetAnchor { get; set; }
  40. /// <summary>
  41. /// Position of party member 0,1,2. If -1 at the time of setting the character wasn't in the party.
  42. /// </summary>
  43. public sbyte PartyPos { get; protected set; } = sbyte.MaxValue;
  44. /// <summary>
  45. /// Where to draw this item.
  46. /// </summary>
  47. public virtual Rectangle Pos { get => _pos; set => _pos = value; }
  48. public virtual int Width { get => _pos.Width; set => _pos.Width = value; }
  49. public virtual int X { get => _pos.X; set => _pos.X = value; }
  50. public virtual int Y { get => _pos.Y; set => _pos.Y = value; }
  51. #endregion Properties
  52. #region Methods
  53. public static implicit operator Rectangle(Menu_Base v) => v.Pos;
  54. public abstract void Draw();
  55. /// <summary>
  56. /// Hide object prevents drawing, update, inputs.
  57. /// </summary>
  58. public virtual void Hide() => Enabled = false;
  59. public virtual void HideChildren()
  60. {
  61. }
  62. public abstract bool Inputs();
  63. public virtual void ModeChangeEvent(object sender, Enum e)
  64. {
  65. }
  66. /// <summary>
  67. /// Things that change rarely. Like a party member changes or Laguna dream happens.
  68. /// </summary>
  69. public virtual void Refresh() => RefreshChild();
  70. /// <summary>
  71. /// by default null damageable won't propogate to children. resets to false after refresh.
  72. /// </summary>
  73. public bool ForceNullDamageable { get; set; } = false;
  74. /// <summary>
  75. /// Update set characters and then refresh.
  76. /// </summary>
  77. /// <param name="character"></param>
  78. /// <param name="Visiblecharacter"></param>
  79. public virtual void Refresh(Damageable damageable)
  80. {
  81. SetDamageable(damageable, null);
  82. Refresh();
  83. }
  84. public bool Battle { get; protected set; } = false;
  85. public void SetDamageable(Damageable damageable, sbyte? partypos = null, bool forcenull = false)
  86. {
  87. if ((Damageable != damageable) || (partypos.HasValue && partypos.Value != PartyPos))
  88. {
  89. if (partypos != null)
  90. {
  91. Damageable = damageable;
  92. PartyPos = partypos.Value;
  93. if (Damageable == null)
  94. {
  95. if (PartyPos >= 0 && Memory.State?.PartyData != null && PartyPos < Memory.State.PartyData.Count)
  96. Damageable = Memory.State[Memory.State.PartyData[PartyPos]];
  97. else
  98. {
  99. var enemypos = (0 - PartyPos) - 1;
  100. if (PartyPos < 0 && Enemy.Party != null && enemypos < Enemy.Party.Count)
  101. {
  102. Damageable = Enemy.Party[enemypos];
  103. }
  104. }
  105. }
  106. }
  107. else if (damageable != null)
  108. {
  109. Damageable = damageable;
  110. if (Damageable.GetCharacterData(out var c))
  111. {
  112. PartyPos = (sbyte)(Memory.State?.Party?.Where(x => !x.Equals(Characters.Blank)).ToList().FindIndex(x => x.Equals(c.ID)) ?? -1);
  113. }
  114. else if (Damageable is Enemy)
  115. {
  116. PartyPos = checked((sbyte)(0 - Enemy.Party.FindIndex(x => x.Equals(Damageable)) - 1));
  117. }
  118. else PartyPos = sbyte.MaxValue;
  119. }
  120. else if(ForceNullDamageable || forcenull)
  121. {
  122. ForceNullDamageable = true;
  123. Damageable = null;
  124. PartyPos = sbyte.MaxValue;
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// Plan is to use this to reset values to a default state if done.
  130. /// </summary>
  131. public virtual void Reset() { }
  132. /// <summary>
  133. /// Show object enables drawing, update, inputs.
  134. /// </summary>
  135. public virtual void Show() => Enabled = true;
  136. public abstract bool Update();
  137. protected abstract void Init();
  138. /// <summary>
  139. /// For child items.
  140. /// </summary>
  141. protected virtual void RefreshChild()
  142. {
  143. }
  144. #endregion Methods
  145. }
  146. public class OffsetAnchor
  147. {
  148. #region Fields
  149. private Vector2 v = Vector2.Zero;
  150. #endregion Fields
  151. #region Constructors
  152. public OffsetAnchor(Vector2 pos) => this.v = pos;
  153. public OffsetAnchor(Point pos) => this.v = pos.ToVector2();
  154. public OffsetAnchor()
  155. {
  156. }
  157. #endregion Constructors
  158. #region Properties
  159. public Point pPos { get => v.ToPoint(); set => v = value.ToVector2(); }
  160. public Vector2 vPos { get => v; set => v = value; }
  161. public float X { get => v.X; set => v.X = value; }
  162. public float Y { get => v.Y; set => v.Y = value; }
  163. #endregion Properties
  164. #region Methods
  165. public static explicit operator Point(OffsetAnchor a) => a.v.ToPoint();
  166. public static implicit operator OffsetAnchor(Vector2 a) => new OffsetAnchor(a);
  167. public static implicit operator OffsetAnchor(Point a) => new OffsetAnchor(a);
  168. public static implicit operator Vector2(OffsetAnchor a) => a.v;
  169. public OffsetAnchor Offset(float x, float y)
  170. {
  171. v.X += x;
  172. v.Y += y;
  173. return this;
  174. }
  175. public OffsetAnchor Offset(Vector2 _v)
  176. {
  177. v += _v;
  178. return this;
  179. }
  180. internal void Set(Vector2 vector2) => v = vector2;
  181. #endregion Methods
  182. }
  183. }