Base.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. using Microsoft.Xna.Framework;
  2. using OpenVIII.Encoding.Tags;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. namespace OpenVIII.IGMData
  8. {
  9. public abstract class Base : Menu_Base
  10. {
  11. #region Fields
  12. public BitArray BLANKS;
  13. /// <summary>
  14. /// location of where pointer finger will point.
  15. /// </summary>
  16. public Point[] CURSOR;
  17. public Menu_Base[,] ITEM;
  18. public int PointerZIndex = byte.MaxValue;
  19. /// <summary>
  20. /// Size of the entire area
  21. /// </summary>
  22. public Rectangle[] SIZE;
  23. protected bool DepthFirst = false;
  24. protected bool skipdata = false;
  25. protected bool skipsnd = false;
  26. private byte _count = 0;
  27. private int _cursor_select;
  28. #endregion Fields
  29. #region Constructors
  30. public Base() => CONTAINER = new IGMDataItem.Empty();
  31. #endregion Constructors
  32. #region Properties
  33. public static Vector2 TextScale => Menu.TextScale;
  34. public byte Cols { get; protected set; } = 1;
  35. /// <summary>
  36. /// Total number of items
  37. /// </summary>
  38. public byte Count { get => _count; protected set => _count = checked((byte)(value + ExtraCount)); }
  39. public int CURSOR_SELECT
  40. {
  41. get => GetCursor_select(); set => SetCursor_select(value);
  42. }
  43. /// <summary>
  44. /// How many Peices per Item. Example 1 box could have 9 things to draw in it.
  45. /// </summary>
  46. public byte Depth { get; protected set; } = 0;
  47. public Dictionary<int, FF8String> Descriptions { get; protected set; }
  48. public byte ExtraCount { get; protected set; } = 0;
  49. /// <summary>
  50. /// Container's Height
  51. /// </summary>
  52. public override int Height => CONTAINER != null ? Pos.Height : 0;
  53. public override Rectangle Pos { get => CONTAINER?.Pos ?? Rectangle.Empty; set => CONTAINER.Pos = value; }
  54. public byte Rows { get; protected set; } = 1;
  55. public bool SkipSIZE { get; set; } = false;
  56. public Table_Options Table_Options { get; set; } = Table_Options.Default;
  57. /// <summary>
  58. /// Container's Width
  59. /// </summary>
  60. public override int Width => CONTAINER != null ? Pos.Width : 0;
  61. /// <summary>
  62. /// Container's X Position
  63. /// </summary>
  64. public override int X => CONTAINER != null ? Pos.X : 0;
  65. /// <summary>
  66. /// Container's Y Position
  67. /// </summary>
  68. public override int Y => CONTAINER != null ? Pos.Y : 0;
  69. #endregion Properties
  70. #region Indexers
  71. public Menu_Base this[int pos, int i] { get => ITEM[pos, i]; set => ITEM[pos, i] = value; }
  72. #endregion Indexers
  73. #region Methods
  74. public static T Create<T>(int count = 0, int depth = 0, Menu_Base container = null, int? cols = null, int? rows = null, Damageable damageable = null, sbyte? partypos = null, bool battle = false) where T : Base, new()
  75. {
  76. var r = Create<T>(damageable, partypos);
  77. r.Battle = battle;
  78. r.Init(count, depth, container, cols, rows);
  79. return r;
  80. }
  81. /// <summary>
  82. /// Convert to rectangle based on container.
  83. /// </summary>
  84. /// <param name="v">Input data</param>
  85. public static implicit operator Rectangle(Base v) => v.CONTAINER ?? Rectangle.Empty;
  86. //public object PrevSetting { get; protected set; } = null;
  87. //public object Setting { get; protected set; } = null;
  88. public virtual int CURSOR_NEXT()
  89. {
  90. if ((Cursor_Status & Cursor_Status.Enabled) != 0)
  91. {
  92. var value = GetCursor_select();
  93. var loop = 0;
  94. while (true)
  95. {
  96. if (++value >= CURSOR.Length)
  97. {
  98. value = 0;
  99. if (loop++ > 1) break;
  100. }
  101. if ((CURSOR[value] != Point.Zero && !BLANKS[value])) break;
  102. }
  103. SetCursor_select(value);
  104. }
  105. return GetCursor_select();
  106. }
  107. public virtual int CURSOR_PREV()
  108. {
  109. if ((Cursor_Status & Cursor_Status.Enabled) != 0)
  110. {
  111. var value = GetCursor_select();
  112. var loop = 0;
  113. while (true)
  114. {
  115. if (--value < 0)
  116. {
  117. value = CURSOR.Length - 1;
  118. if (loop++ > 1) break;
  119. }
  120. if ((CURSOR[value] != Point.Zero && !BLANKS[value])) break;
  121. }
  122. SetCursor_select(value);
  123. }
  124. return GetCursor_select();
  125. }
  126. /// <summary>
  127. /// Draw all items
  128. /// </summary>
  129. public override void Draw()
  130. {
  131. if (Enabled)
  132. {
  133. if (CONTAINER != null)
  134. CONTAINER.Draw();
  135. var pointer = false;
  136. Target.Group targetgroup = null;
  137. if (!skipdata && ITEM != null)
  138. if (DepthFirst)
  139. for (var d = 0; d < Depth; d++)
  140. for (var i = 0; i < Count; i++)
  141. {
  142. if (i == PointerZIndex && !pointer)
  143. pointer = DrawPointer();
  144. if (ITEM[i, d] != null && (ITEM[i, d].GetType()).Equals(typeof(Target.Group)))
  145. targetgroup = (Target.Group)(ITEM[i, d]);
  146. else
  147. DrawITEM(i, d);
  148. }
  149. else
  150. for (var i = 0; i < Count; i++)
  151. for (var d = 0; d < Depth; d++)
  152. {
  153. if (i == PointerZIndex && !pointer)
  154. pointer = DrawPointer();
  155. if (ITEM[i, d]!=null && (ITEM[i, d].GetType()).Equals(typeof(Target.Group)))
  156. targetgroup = (Target.Group)(ITEM[i, d]);
  157. else
  158. DrawITEM(i, d);
  159. }
  160. if (!pointer)
  161. {
  162. pointer = DrawPointer();
  163. }
  164. targetgroup?.Draw();
  165. }
  166. }
  167. public void DrawPointer(Point cursor, Vector2? offset = null, bool blink = false) => Menu.DrawPointer(cursor, offset, blink);
  168. public override void HideChildren()
  169. {
  170. if (Enabled)
  171. {
  172. //base.Hide();
  173. //maybe overkill to run hide on items. if group is hidden it won't draw.
  174. if (!skipdata)
  175. {
  176. foreach (var i in ITEM)
  177. {
  178. if (i != null)
  179. {
  180. i.HideChildren();
  181. i.Hide();
  182. }
  183. }
  184. }
  185. }
  186. }
  187. public void InitSize(bool force = false)
  188. {
  189. var cellcount = Rows * Cols;
  190. //SetSize(cellcount);
  191. if (((SIZE != null && SIZE.Length > 0) || force))
  192. {
  193. if (cellcount > 1)
  194. {
  195. InitRowsSize(force);
  196. }
  197. else
  198. {
  199. SIZE[0] = new Rectangle(X, Y, Width, Height);
  200. CURSOR[0] = Point.Zero;
  201. BLANKS[0] = false;
  202. InitShift(0, 0, 0);
  203. InitCursor(0, 0, 0);
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Check inputs
  209. /// </summary>
  210. /// <returns>True = input detected</returns>
  211. public override bool Inputs()
  212. {
  213. var ret = false;
  214. var mouse = false;
  215. if ((Cursor_Status & Cursor_Status.Enabled) != 0)
  216. {
  217. Cursor_Status &= ~Cursor_Status.Blinking;
  218. if ((Cursor_Status & Cursor_Status.Static) == 0)
  219. for (var i = 0; i < SIZE.Length; i++)
  220. {
  221. if (SIZE[i].Contains(MouseLocation) && !SIZE[i].IsEmpty && CURSOR[i] != Point.Zero && !BLANKS[i])
  222. {
  223. CURSOR_SELECT = i;
  224. ret = true;
  225. mouse = true;
  226. }
  227. }
  228. if (!ret && (Cursor_Status & Cursor_Status.Horizontal) != 0 && (Cursor_Status & Cursor_Status.Static) == 0)
  229. {
  230. if (Input2.DelayedButton(FF8TextTagKey.Left))
  231. {
  232. CURSOR_PREV();
  233. ret = true;
  234. }
  235. else if (Input2.DelayedButton(FF8TextTagKey.Right))
  236. {
  237. CURSOR_NEXT();
  238. ret = true;
  239. }
  240. }
  241. if ((!ret && (Cursor_Status & Cursor_Status.Horizontal) == 0 || (Cursor_Status & Cursor_Status.Vertical) != 0) && (Cursor_Status & Cursor_Status.Static) == 0)
  242. {
  243. if (Input2.DelayedButton(FF8TextTagKey.Up))
  244. {
  245. CURSOR_PREV();
  246. ret = true;
  247. }
  248. else if (Input2.DelayedButton(FF8TextTagKey.Down))
  249. {
  250. CURSOR_NEXT();
  251. ret = true;
  252. }
  253. }
  254. if (mouse || !ret)
  255. {
  256. if (Input2.DelayedButton(FF8TextTagKey.Confirm))
  257. {
  258. return Inputs_OKAY();
  259. }
  260. else if (Input2.DelayedButton(FF8TextTagKey.Cancel))
  261. {
  262. return Inputs_CANCEL();
  263. }
  264. else if (Input2.DelayedButton(FF8TextTagKey.RotateRight))
  265. {
  266. Inputs_RotateRight();
  267. return true;
  268. }
  269. else if (Input2.DelayedButton(FF8TextTagKey.Cards))
  270. {
  271. Inputs_Cards();
  272. return true;
  273. }
  274. else if (Input2.DelayedButton(FF8TextTagKey.Menu))
  275. {
  276. Inputs_Menu();
  277. return true;
  278. }
  279. else if ((Cursor_Status & Cursor_Status.Horizontal) == 0 && (Cursor_Status & Cursor_Status.Static) == 0)
  280. {
  281. if (Input2.DelayedButton(FF8TextTagKey.Left))
  282. {
  283. Inputs_Left();
  284. return true;
  285. }
  286. else if (Input2.DelayedButton(FF8TextTagKey.Right))
  287. {
  288. Inputs_Right();
  289. return true;
  290. }
  291. }
  292. }
  293. if (ret && !mouse)
  294. {
  295. if (!skipsnd)
  296. AV.Sound.Play(0);
  297. }
  298. }
  299. skipsnd = false;
  300. return ret;
  301. }
  302. public virtual bool Inputs_CANCEL()
  303. {
  304. if (!skipsnd)
  305. AV.Sound.Play(8);
  306. return false;
  307. }
  308. public virtual void Inputs_Cards()
  309. {
  310. if (!skipsnd)
  311. AV.Sound.Play(0);
  312. }
  313. public virtual void Inputs_Left()
  314. {
  315. if (!skipsnd)
  316. AV.Sound.Play(0);
  317. }
  318. public virtual void Inputs_Menu()
  319. {
  320. if (!skipsnd)
  321. AV.Sound.Play(31);
  322. }
  323. public virtual bool Inputs_RotateRight()
  324. {
  325. if (!skipsnd)
  326. AV.Sound.Play(0);
  327. return false;
  328. }
  329. public virtual bool Inputs_OKAY()
  330. {
  331. if (!skipsnd)
  332. AV.Sound.Play(0);
  333. return false;
  334. }
  335. public virtual void Inputs_Right()
  336. {
  337. if (!skipsnd)
  338. AV.Sound.Play(0);
  339. }
  340. public override void Refresh()
  341. {
  342. var count = Memory.State?.PartyData.Count ?? 0;
  343. if (Memory.State?.PartyData != null &&
  344. Damageable == null &&
  345. PartyPos >= 0 &&
  346. PartyPos < count)
  347. {
  348. Damageable = Memory.State[Memory.State.PartyData[PartyPos]];
  349. }
  350. base.Refresh();
  351. }
  352. public override void Reset()
  353. {
  354. foreach (var i in ITEM)
  355. {
  356. i?.Reset();
  357. }
  358. base.Reset();
  359. }
  360. /// <summary>
  361. /// Things that change on every update.
  362. /// </summary>
  363. /// <returns>True = signifigant change</returns>
  364. public override bool Update()
  365. {
  366. var ret = false;
  367. if (!skipdata && ITEM != null)
  368. foreach (var i in ITEM)
  369. {
  370. if (i != null)
  371. ret = i.Update() || ret;
  372. }
  373. return ret;
  374. }
  375. protected void AutoAdjustContainerWidth(Rectangle DataSize)
  376. {
  377. if (DataSize.Right > Pos.Right)
  378. {
  379. CONTAINER.Width += DataSize.Right - Pos.Right + Math.Abs(DataSize.Left - Pos.Left);
  380. }
  381. }
  382. protected bool CheckBounds(ref Rectangle DataSize, Rectangle input)
  383. {
  384. if (input.Right > Pos.Right && input.Right > DataSize.Right)
  385. {
  386. DataSize = input;
  387. return true;
  388. }
  389. return false;
  390. }
  391. protected bool CheckBounds(ref Rectangle DataSize, int pos) => CheckBounds(ref DataSize, ((IGMDataItem.Text)ITEM[pos, 0]).DataSize);
  392. protected virtual void DrawITEM(int i, int d) => ITEM[i, d]?.Draw();
  393. protected virtual bool DrawPointer()
  394. {
  395. if ((Cursor_Status & (Cursor_Status.Enabled | Cursor_Status.Draw)) != 0 &&
  396. (Cursor_Status & Cursor_Status.Hidden) == 0)
  397. {
  398. if ((Cursor_Status & Cursor_Status.All) != 0)
  399. {
  400. for (var i = 0; i < CURSOR.Length; i++)
  401. if (!BLANKS[i])
  402. DrawPointer(CURSOR[i], blink: true);
  403. }
  404. else
  405. DrawPointer(CURSOR[CURSOR_SELECT], blink: ((Cursor_Status & Cursor_Status.Blinking) != 0));
  406. return true;
  407. }
  408. return false;
  409. }
  410. protected int GetCursor_select() => _cursor_select;
  411. /// <summary>
  412. /// Most objects set all these values. also Init Refresh and Update
  413. /// </summary>
  414. /// <param name="count"></param>
  415. /// <param name="depth"></param>
  416. /// <param name="container"></param>
  417. /// <param name="cols"></param>
  418. /// <param name="rows"></param>
  419. protected void Init(int count, int depth, Menu_Base container = null, int? cols = null, int? rows = null)
  420. {
  421. if (count >= 0)
  422. Count = checked((byte)count);
  423. if (depth >= 0)
  424. Depth = checked((byte)depth);
  425. if (container != null)
  426. CONTAINER = container;
  427. if (cols.HasValue && cols.Value > 0)
  428. Cols = checked((byte)cols.Value);
  429. if (rows.HasValue && rows.Value > 0)
  430. Rows = checked((byte)rows.Value);
  431. Init();
  432. Refresh();
  433. Update();
  434. }
  435. /// <summary>
  436. /// Things that are fixed values at startup.
  437. /// </summary>
  438. protected override void Init()
  439. {
  440. if (Count <= 0 || Depth <= 0)
  441. {
  442. if (CONTAINER.Pos == Rectangle.Empty)
  443. {
  444. Debug.WriteLine($"{this}:: Count {Count} or depth {Depth}, is invalid must be >= 1, or a CONTAINER {CONTAINER} and CONTAINER.Pos { Pos.ToString() } must be set instead, Skipping Init()");
  445. return;
  446. }
  447. }
  448. else
  449. {
  450. //if (SIZE == null)
  451. // SIZE = new Rectangle[Count];
  452. if (ITEM == null)
  453. ITEM = new Menu_Base[Count, Depth];
  454. //if (CURSOR == null)
  455. // CURSOR = new Point[Count];
  456. //if (BLANKS == null)
  457. // BLANKS = new bool[Count];
  458. if (Descriptions == null)
  459. Descriptions = new Dictionary<int, FF8String>(Count);
  460. SetSize(Math.Max(Rows * Cols, Count));
  461. }
  462. if (!SkipSIZE)
  463. InitSize();
  464. SkipSIZE = false;
  465. }
  466. protected virtual void InitCursor(int i, int col, int row, bool zero = false)
  467. {
  468. if (zero) CURSOR[i] = Point.Zero;
  469. CURSOR[i].Y += (int)(SIZE[i].Y + 6 * TextScale.Y);
  470. CURSOR[i].X += SIZE[i].X;
  471. }
  472. protected virtual void InitShift(int i, int col, int row)
  473. {
  474. }
  475. protected bool InputITEM(Menu_Base menuitem, ref bool ret)
  476. {
  477. if (menuitem != null && menuitem.Enabled)
  478. {
  479. Cursor_Status |= (Cursor_Status.Enabled | Cursor_Status.Blinking);
  480. ret = menuitem.Inputs();
  481. return true;
  482. }
  483. return false;
  484. }
  485. protected override void RefreshChild()
  486. {
  487. base.RefreshChild();
  488. if (!skipdata)
  489. {
  490. if (CONTAINER != null)
  491. {
  492. if (ForceNullDamageable)
  493. CONTAINER.ForceNullDamageable = ForceNullDamageable;
  494. CONTAINER.Refresh(Damageable);
  495. }
  496. if (ITEM != null)
  497. for (var i = 0; i < Count; i++)
  498. for (var d = 0; d < Depth; d++)
  499. {
  500. if (ForceNullDamageable && ITEM[i, d] != null)
  501. ITEM[i, d].ForceNullDamageable = ForceNullDamageable;
  502. ITEM[i, d]?.Refresh(Damageable);
  503. }
  504. }
  505. }
  506. protected virtual void SetCursor_select(int value)
  507. {
  508. if (value >= 0 && CURSOR != null && value < CURSOR.Length && CURSOR[value] != Point.Zero)
  509. _cursor_select = value;
  510. }
  511. //protected Base(int Count = 0, int depth = 0, Menu_Base container = null, int? cols = null, int? rows = null, Damageable damageable = null, sbyte? PartyPos = null)
  512. //{
  513. // Init(damageable, PartyPos);
  514. // Init(Count, depth, container, cols, rows);
  515. //}
  516. private static T Create<T>(Damageable damageable = null, sbyte? partypos = null) where T : Base, new()
  517. {
  518. var r = new T();
  519. r.SetDamageable(damageable, partypos);
  520. return r;
  521. }
  522. private void InitRowsSize(bool force)
  523. {
  524. for (var i = 0; i < SIZE.Length; i++)
  525. {
  526. var col = (Table_Options & Table_Options.FillRows) != 0 ? i % Cols : i / Rows;
  527. var row = (Table_Options & Table_Options.FillRows) != 0 ? i / Cols : i % Rows;
  528. if (col < Cols && row < Rows)
  529. {
  530. if (SIZE[i].IsEmpty || force) //allows for override a size value before the loop.
  531. {
  532. SIZE[i] = new Rectangle
  533. {
  534. X = X + (Width * col) / Cols,
  535. Y = Y + (Height * row) / Rows,
  536. Width = Width / Cols,
  537. Height = Height / Rows,
  538. };
  539. }
  540. CURSOR[i] = Point.Zero;
  541. InitShift(i, col, row);
  542. InitCursor(i, col, row);
  543. }
  544. }
  545. }
  546. private void SetSize(int cellcount)
  547. {
  548. cellcount = cellcount > 1 ? cellcount : 1;
  549. if (CURSOR == null || CURSOR.Length == 0 ||
  550. SIZE == null || SIZE.Length == 0 ||
  551. BLANKS == null || BLANKS.Length == 0)
  552. {
  553. CURSOR = new Point[cellcount];
  554. SIZE = new Rectangle[cellcount];
  555. BLANKS = new BitArray(cellcount,false);
  556. }
  557. }
  558. #endregion Methods
  559. }
  560. }