Base.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. T 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. int value = GetCursor_select();
  93. int 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. int value = GetCursor_select();
  112. int 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. bool pointer = false;
  136. Target.Group targetgroup = null;
  137. if (!skipdata && ITEM != null)
  138. if (DepthFirst)
  139. for (int d = 0; d < Depth; d++)
  140. for (int 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 (int i = 0; i < Count; i++)
  151. for (int 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 (Menu_Base 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. int 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. bool ret = false;
  214. bool 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 (int 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. init_debugger_Audio.PlaySound(0);
  297. }
  298. }
  299. skipsnd = false;
  300. return ret;
  301. }
  302. public virtual bool Inputs_CANCEL()
  303. {
  304. if (!skipsnd)
  305. init_debugger_Audio.PlaySound(8);
  306. return false;
  307. }
  308. public virtual void Inputs_Cards()
  309. {
  310. if (!skipsnd)
  311. init_debugger_Audio.PlaySound(0);
  312. }
  313. public virtual void Inputs_Left()
  314. {
  315. if (!skipsnd)
  316. init_debugger_Audio.PlaySound(0);
  317. }
  318. public virtual void Inputs_Menu()
  319. {
  320. if (!skipsnd)
  321. init_debugger_Audio.PlaySound(31);
  322. }
  323. public virtual bool Inputs_RotateRight()
  324. {
  325. if (!skipsnd)
  326. init_debugger_Audio.PlaySound(0);
  327. return false;
  328. }
  329. public virtual bool Inputs_OKAY()
  330. {
  331. if (!skipsnd)
  332. init_debugger_Audio.PlaySound(0);
  333. return false;
  334. }
  335. public virtual void Inputs_Right()
  336. {
  337. if (!skipsnd)
  338. init_debugger_Audio.PlaySound(0);
  339. }
  340. public override void Refresh()
  341. {
  342. int count = Memory.State.PartyData.Count;
  343. if (Memory.State?.PartyData != null &&
  344. Damageable == null &&
  345. PartyPos >= 0 &&
  346. PartyPos < count &&
  347. Memory.State.Characters.TryGetValue(Memory.State.PartyData[PartyPos], out Saves.CharacterData c))
  348. Damageable = c;
  349. base.Refresh();
  350. }
  351. public override void Reset()
  352. {
  353. foreach (Menu_Base i in ITEM)
  354. {
  355. i?.Reset();
  356. }
  357. base.Reset();
  358. }
  359. /// <summary>
  360. /// Things that change on every update.
  361. /// </summary>
  362. /// <returns>True = signifigant change</returns>
  363. public override bool Update()
  364. {
  365. bool ret = false;
  366. if (!skipdata && ITEM != null)
  367. foreach (Menu_Base i in ITEM)
  368. {
  369. if (i != null)
  370. ret = i.Update() || ret;
  371. }
  372. return ret;
  373. }
  374. protected void AutoAdjustContainerWidth(Rectangle DataSize)
  375. {
  376. if (DataSize.Right > Pos.Right)
  377. {
  378. CONTAINER.Width += DataSize.Right - Pos.Right + Math.Abs(DataSize.Left - Pos.Left);
  379. }
  380. }
  381. protected bool CheckBounds(ref Rectangle DataSize, Rectangle input)
  382. {
  383. if (input.Right > Pos.Right && input.Right > DataSize.Right)
  384. {
  385. DataSize = input;
  386. return true;
  387. }
  388. return false;
  389. }
  390. protected bool CheckBounds(ref Rectangle DataSize, int pos) => CheckBounds(ref DataSize, ((IGMDataItem.Text)ITEM[pos, 0]).DataSize);
  391. protected virtual void DrawITEM(int i, int d) => ITEM[i, d]?.Draw();
  392. protected virtual bool DrawPointer()
  393. {
  394. if ((Cursor_Status & (Cursor_Status.Enabled | Cursor_Status.Draw)) != 0 &&
  395. (Cursor_Status & Cursor_Status.Hidden) == 0)
  396. {
  397. if ((Cursor_Status & Cursor_Status.All) != 0)
  398. {
  399. for (int i = 0; i < CURSOR.Length; i++)
  400. if (!BLANKS[i])
  401. DrawPointer(CURSOR[i], blink: true);
  402. }
  403. else
  404. DrawPointer(CURSOR[CURSOR_SELECT], blink: ((Cursor_Status & Cursor_Status.Blinking) != 0));
  405. return true;
  406. }
  407. return false;
  408. }
  409. protected int GetCursor_select() => _cursor_select;
  410. /// <summary>
  411. /// Most objects set all these values. also Init Refresh and Update
  412. /// </summary>
  413. /// <param name="count"></param>
  414. /// <param name="depth"></param>
  415. /// <param name="container"></param>
  416. /// <param name="cols"></param>
  417. /// <param name="rows"></param>
  418. protected void Init(int count, int depth, Menu_Base container = null, int? cols = null, int? rows = null)
  419. {
  420. if (count >= 0)
  421. Count = checked((byte)count);
  422. if (depth >= 0)
  423. Depth = checked((byte)depth);
  424. if (container != null)
  425. CONTAINER = container;
  426. if (cols.HasValue && cols.Value > 0)
  427. Cols = checked((byte)cols.Value);
  428. if (rows.HasValue && rows.Value > 0)
  429. Rows = checked((byte)rows.Value);
  430. Init();
  431. Refresh();
  432. Update();
  433. }
  434. /// <summary>
  435. /// Things that are fixed values at startup.
  436. /// </summary>
  437. protected override void Init()
  438. {
  439. if (Count <= 0 || Depth <= 0)
  440. {
  441. if (CONTAINER.Pos == Rectangle.Empty)
  442. {
  443. 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()");
  444. return;
  445. }
  446. }
  447. else
  448. {
  449. //if (SIZE == null)
  450. // SIZE = new Rectangle[Count];
  451. if (ITEM == null)
  452. ITEM = new Menu_Base[Count, Depth];
  453. //if (CURSOR == null)
  454. // CURSOR = new Point[Count];
  455. //if (BLANKS == null)
  456. // BLANKS = new bool[Count];
  457. if (Descriptions == null)
  458. Descriptions = new Dictionary<int, FF8String>(Count);
  459. SetSize(Math.Max(Rows * Cols, Count));
  460. }
  461. if (!SkipSIZE)
  462. InitSize();
  463. SkipSIZE = false;
  464. }
  465. protected virtual void InitCursor(int i, int col, int row, bool zero = false)
  466. {
  467. if (zero) CURSOR[i] = Point.Zero;
  468. CURSOR[i].Y += (int)(SIZE[i].Y + 6 * TextScale.Y);
  469. CURSOR[i].X += SIZE[i].X;
  470. }
  471. protected virtual void InitShift(int i, int col, int row)
  472. {
  473. }
  474. protected bool InputITEM(Menu_Base menuitem, ref bool ret)
  475. {
  476. if (menuitem != null && menuitem.Enabled)
  477. {
  478. Cursor_Status |= (Cursor_Status.Enabled | Cursor_Status.Blinking);
  479. ret = menuitem.Inputs();
  480. return true;
  481. }
  482. return false;
  483. }
  484. protected override void RefreshChild()
  485. {
  486. base.RefreshChild();
  487. if (!skipdata)
  488. {
  489. if (CONTAINER != null)
  490. {
  491. if (ForceNullDamageable)
  492. CONTAINER.ForceNullDamageable = ForceNullDamageable;
  493. CONTAINER.Refresh(Damageable);
  494. }
  495. if (ITEM != null)
  496. for (int i = 0; i < Count; i++)
  497. for (int d = 0; d < Depth; d++)
  498. {
  499. if (ForceNullDamageable && ITEM[i, d] != null)
  500. ITEM[i, d].ForceNullDamageable = ForceNullDamageable;
  501. ITEM[i, d]?.Refresh(Damageable);
  502. }
  503. }
  504. }
  505. protected virtual void SetCursor_select(int value)
  506. {
  507. if (value >= 0 && CURSOR != null && value < CURSOR.Length && CURSOR[value] != Point.Zero)
  508. _cursor_select = value;
  509. }
  510. //protected Base(int count = 0, int depth = 0, Menu_Base container = null, int? cols = null, int? rows = null, Damageable damageable = null, sbyte? partypos = null)
  511. //{
  512. // Init(damageable, partypos);
  513. // Init(count, depth, container, cols, rows);
  514. //}
  515. private static T Create<T>(Damageable damageable = null, sbyte? partypos = null) where T : Base, new()
  516. {
  517. T r = new T();
  518. r.SetDamageable(damageable, partypos);
  519. return r;
  520. }
  521. private void InitRowsSize(bool force)
  522. {
  523. for (int i = 0; i < SIZE.Length; i++)
  524. {
  525. int col = (Table_Options & Table_Options.FillRows) != 0 ? i % Cols : i / Rows;
  526. int row = (Table_Options & Table_Options.FillRows) != 0 ? i / Cols : i % Rows;
  527. if (col < Cols && row < Rows)
  528. {
  529. if (SIZE[i].IsEmpty || force) //allows for override a size value before the loop.
  530. {
  531. SIZE[i] = new Rectangle
  532. {
  533. X = X + (Width * col) / Cols,
  534. Y = Y + (Height * row) / Rows,
  535. Width = Width / Cols,
  536. Height = Height / Rows,
  537. };
  538. }
  539. CURSOR[i] = Point.Zero;
  540. InitShift(i, col, row);
  541. InitCursor(i, col, row);
  542. }
  543. }
  544. }
  545. private void SetSize(int cellcount)
  546. {
  547. cellcount = cellcount > 1 ? cellcount : 1;
  548. if (CURSOR == null || CURSOR.Length == 0 ||
  549. SIZE == null || SIZE.Length == 0 ||
  550. BLANKS == null || BLANKS.Length == 0)
  551. {
  552. CURSOR = new Point[cellcount];
  553. SIZE = new Rectangle[cellcount];
  554. BLANKS = new BitArray(cellcount,false);
  555. }
  556. }
  557. #endregion Methods
  558. }
  559. }