DebugSelectPool.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Microsoft.Xna.Framework;
  2. using OpenVIII.Encoding.Tags;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7. namespace OpenVIII.IGMData
  8. {
  9. public class DebugSelectPool<DataType> : Pool.Base<IEnumerable<DataType>, DataType>
  10. {
  11. #region Fields
  12. private string filter;
  13. private Action<string> FilterAction;
  14. private Func<DataType, bool> OkayFunc;
  15. private bool skipRefresh = true;
  16. #endregion Fields
  17. #region Destructors
  18. ~DebugSelectPool()
  19. { Game1.OnTextEntered -= Game1_onTextEntered; }
  20. #endregion Destructors
  21. #region Properties
  22. private int Col => (CURSOR_SELECT / Rows);
  23. private int Row => (CURSOR_SELECT / Cols);
  24. #endregion Properties
  25. #region Methods
  26. public static DebugSelectPool<inDataType> Create<inDataType>(Rectangle pos, IEnumerable<inDataType> source, Func<inDataType, bool> OkayFunc, Action<string> FilterAction, int Cols = 3, int Rows = 12)
  27. {
  28. var r = Base.Create<DebugSelectPool<inDataType>>(Rows*Cols+1, 1, new IGMDataItem.Box { Pos = pos }, Cols, Rows);
  29. r.Source = source;
  30. r.OkayFunc = OkayFunc;
  31. r.FilterAction = FilterAction;
  32. return r;
  33. }
  34. public override bool Inputs()
  35. {
  36. if (InputKeyboard.State.GetPressedKeys().Any(x => (int)x >= (int)Keys.D0 && (int)x <= (int)Keys.Z))
  37. return false;
  38. else if (!string.IsNullOrWhiteSpace(filter) && Input2.DelayedButton(FF8TextTagKey.Cancel, ButtonTrigger.Press | ButtonTrigger.Force))
  39. {
  40. Inputs_CANCEL();
  41. return true;
  42. }
  43. else
  44. return base.Inputs();
  45. }
  46. public override bool Inputs_CANCEL()
  47. {
  48. if (filter.Length == 0)
  49. {
  50. base.Inputs_CANCEL();
  51. Hide();
  52. return true;
  53. }
  54. if (!string.IsNullOrWhiteSpace(filter))
  55. {
  56. if (filter.Length > 1)
  57. filter = filter.Substring(0, filter.Length - 1).Trim();
  58. else
  59. filter = "";
  60. ((IGMDataItem.Box)ITEM[Count - 3, 0]).Data = filter;
  61. FilterAction?.Invoke(filter);
  62. }
  63. //Debug.WriteLine(filter);
  64. return false;
  65. }
  66. public override void Inputs_Left()
  67. {
  68. if (Input2.Button(MouseButtons.MouseWheelup) || Col == 0)
  69. {
  70. PAGE_PREV();
  71. Refresh();
  72. }
  73. else
  74. CURSOR_SELECT -= Rows;
  75. base.Inputs_Left();
  76. }
  77. public override bool Inputs_OKAY()
  78. {
  79. if (!BLANKS[CURSOR_SELECT] && OkayFunc.Invoke(Contents[CURSOR_SELECT]))
  80. {
  81. base.Inputs_OKAY();
  82. return true;
  83. }
  84. return false;
  85. }
  86. public override void Inputs_Right()
  87. {
  88. if (Input2.Button(MouseButtons.MouseWheeldown) || Col == Cols - 1)
  89. {
  90. PAGE_NEXT();
  91. Refresh();
  92. }
  93. else
  94. CURSOR_SELECT += Rows;
  95. base.Inputs_Right();
  96. }
  97. public override void Refresh()
  98. {
  99. if (Source == null || skipRefresh)
  100. {
  101. skipRefresh = false;
  102. return;
  103. }
  104. var total = Rows * Cols;
  105. DefaultPages = Source.Count() / total;
  106. if (DefaultPages <= Page)
  107. Page = DefaultPages - 1;
  108. var skip = Page * total;
  109. var p = 0;
  110. foreach (var i in Source)
  111. {
  112. if (skip > 0)
  113. {
  114. skip--;
  115. continue;
  116. }
  117. if (p >= total)
  118. break;
  119. Contents[p] = i;
  120. var menu_Base = ITEM[p, 0];
  121. menu_Base.Show();
  122. var text = (IGMDataItem.Text)menu_Base;
  123. text.Data = new FF8String(i.ToString());
  124. BLANKS[p] = false;
  125. p++;
  126. }
  127. for (; p < total; p++)
  128. {
  129. Contents[p] = default;
  130. BLANKS[p] = true;
  131. ITEM[p, 0].Hide();
  132. }
  133. base.Refresh();
  134. }
  135. public void Refresh(IEnumerable<DataType> src)
  136. {
  137. Source = src;
  138. Refresh();
  139. }
  140. protected override void Init()
  141. {
  142. base.Init();
  143. filter = "";
  144. Game1.OnTextEntered += Game1_onTextEntered;
  145. Contents = new DataType[Rows * Cols];
  146. foreach (var i in Enumerable.Range(0, Rows * Cols))
  147. {
  148. ITEM[i, 0] = new IGMDataItem.Text { Pos = SIZE[i] };
  149. }
  150. var rect = new Rectangle(CONTAINER.X + CONTAINER.Width / 4, CONTAINER.Y - 112, CONTAINER.Width / 2, 120);
  151. ITEM[Count - 3, 0] = new IGMDataItem.Box { Pos = rect, Options = Box_Options.Center | Box_Options.Middle, Title = Icons.ID.INFO };
  152. Cursor_Status |= Cursor_Status.Horizontal;
  153. Hide();
  154. }
  155. protected override void InitShift(int i, int col, int row)
  156. {
  157. base.InitShift(i, col, row);
  158. SIZE[i].Inflate(-22, -8);
  159. SIZE[i].Offset(-4 * col, 12 + (-4 * row));
  160. }
  161. private void Game1_onTextEntered(object sender, TextInputEventArgs e)
  162. {
  163. if (filter != null && filter.Length < 10 && Enabled && Memory.Module == Module.MainMenuDebug)
  164. {
  165. filter += e.Character;
  166. filter = filter.Trim().TrimEnd('\b');
  167. if (!string.IsNullOrWhiteSpace(filter))
  168. {
  169. ((IGMDataItem.Box)ITEM[Count - 3, 0]).Data = filter;
  170. FilterAction?.Invoke(filter);
  171. }
  172. //Debug.WriteLine(filter);
  173. }
  174. }
  175. #endregion Methods
  176. }
  177. }