MirAmountBox.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using Client.MirGraphics;
  2. using Client.MirSounds;
  3. namespace Client.MirControls
  4. {
  5. public sealed class MirAmountBox : MirImageControl
  6. {
  7. public MirLabel TitleLabel, TextLabel;
  8. public MirButton OKButton, CancelButton, CloseButton;
  9. public MirTextBox InputTextBox;
  10. public MirControl ItemImage;
  11. public int ImageIndex;
  12. public uint Amount, MinAmount, MaxAmount;
  13. public MirAmountBox(string title, int image, uint max, uint min = 0, uint defaultAmount = 0)
  14. {
  15. ImageIndex = image;
  16. MaxAmount = max;
  17. MinAmount = min;
  18. Amount = max;
  19. Modal = true;
  20. Movable = false;
  21. Index = 238;
  22. Library = Libraries.Prguse;
  23. Location = new Point((Settings.ScreenWidth - Size.Width) / 2, (Settings.ScreenHeight - Size.Height) / 2);
  24. TitleLabel = new MirLabel
  25. {
  26. AutoSize = true,
  27. Location = new Point(19, 8),
  28. Parent = this,
  29. NotControl = true,
  30. Text = title
  31. };
  32. CloseButton = new MirButton
  33. {
  34. HoverIndex = 361,
  35. Index = 360,
  36. Location = new Point(180, 3),
  37. Library = Libraries.Prguse2,
  38. Parent = this,
  39. PressedIndex = 362,
  40. Sound = SoundList.ButtonA,
  41. };
  42. CloseButton.Click += (o, e) => Dispose();
  43. ItemImage = new MirControl
  44. {
  45. Location = new Point(15, 34),
  46. Size = new Size(38, 34),
  47. Parent = this,
  48. };
  49. ItemImage.AfterDraw += (o, e) => DrawItem();
  50. OKButton = new MirButton
  51. {
  52. HoverIndex = 201,
  53. Index = 200,
  54. Library = Libraries.Title,
  55. Location = new Point(23, 76),
  56. Parent = this,
  57. PressedIndex = 202,
  58. };
  59. OKButton.Click += (o, e) => Dispose();
  60. CancelButton = new MirButton
  61. {
  62. HoverIndex = 204,
  63. Index = 203,
  64. Library = Libraries.Title,
  65. Location = new Point(110, 76),
  66. Parent = this,
  67. PressedIndex = 205,
  68. };
  69. CancelButton.Click += (o, e) => Dispose();
  70. InputTextBox = new MirTextBox
  71. {
  72. Parent = this,
  73. Border = true,
  74. BorderColour = Color.Lime,
  75. Location = new Point(58, 43),
  76. Size = new Size(132, 19),
  77. };
  78. InputTextBox.SetFocus();
  79. InputTextBox.TextBox.KeyPress += MirInputBox_KeyPress;
  80. InputTextBox.TextBox.TextChanged += TextBox_TextChanged;
  81. InputTextBox.Text = (defaultAmount > 0 && defaultAmount <= MaxAmount) ? defaultAmount.ToString() : MaxAmount.ToString();
  82. InputTextBox.TextBox.SelectionStart = 0;
  83. InputTextBox.TextBox.SelectionLength = InputTextBox.Text.Length;
  84. }
  85. public MirAmountBox(string title, int image, string message)
  86. {
  87. ImageIndex = image;
  88. Modal = true;
  89. Movable = false;
  90. Index = 238;
  91. Library = Libraries.Prguse;
  92. Location = new Point((800 - Size.Width) / 2, (600 - Size.Height) / 2);
  93. TitleLabel = new MirLabel
  94. {
  95. AutoSize = true,
  96. Location = new Point(19, 8),
  97. Parent = this,
  98. NotControl = true,
  99. Text = title
  100. };
  101. TextLabel = new MirLabel
  102. {
  103. AutoSize = true,
  104. Location = new Point(60, 43),
  105. ForeColour = Color.Yellow,
  106. Parent = this,
  107. NotControl = true,
  108. Text = message
  109. };
  110. CloseButton = new MirButton
  111. {
  112. HoverIndex = 361,
  113. Index = 360,
  114. Location = new Point(180, 3),
  115. Library = Libraries.Prguse2,
  116. Parent = this,
  117. PressedIndex = 362,
  118. Sound = SoundList.ButtonA,
  119. };
  120. CloseButton.Click += (o, e) => Dispose();
  121. ItemImage = new MirControl
  122. {
  123. Location = new Point(15, 34),
  124. Size = new Size(38, 34),
  125. Parent = this,
  126. };
  127. ItemImage.AfterDraw += (o, e) => DrawItem();
  128. OKButton = new MirButton
  129. {
  130. HoverIndex = 201,
  131. Index = 200,
  132. Library = Libraries.Title,
  133. Location = new Point(23, 76),
  134. Parent = this,
  135. PressedIndex = 202,
  136. };
  137. OKButton.Click += (o, e) => Dispose();
  138. CancelButton = new MirButton
  139. {
  140. HoverIndex = 204,
  141. Index = 203,
  142. Library = Libraries.Title,
  143. Location = new Point(110, 76),
  144. Parent = this,
  145. PressedIndex = 205,
  146. };
  147. CancelButton.Click += (o, e) => Dispose();
  148. }
  149. void TextBox_TextChanged(object sender, EventArgs e)
  150. {
  151. if (uint.TryParse(InputTextBox.Text, out Amount) && Amount >= MinAmount)
  152. {
  153. InputTextBox.BorderColour = Color.Lime;
  154. OKButton.Visible = true;
  155. if (Amount > MaxAmount)
  156. {
  157. Amount = MaxAmount;
  158. InputTextBox.Text = MaxAmount.ToString();
  159. InputTextBox.TextBox.SelectionStart = InputTextBox.Text.Length;
  160. }
  161. if (Amount == MaxAmount)
  162. InputTextBox.BorderColour = Color.Orange;
  163. }
  164. else
  165. {
  166. InputTextBox.BorderColour = Color.Red;
  167. OKButton.Visible = false;
  168. }
  169. }
  170. void MirInputBox_KeyPress(object sender, KeyPressEventArgs e)
  171. {
  172. if (!char.IsControl(e.KeyChar)
  173. && !char.IsDigit(e.KeyChar))
  174. {
  175. e.Handled = true;
  176. }
  177. if (e.KeyChar == (char)Keys.Enter)
  178. {
  179. if (OKButton != null && !OKButton.IsDisposed)
  180. OKButton.InvokeMouseClick(EventArgs.Empty);
  181. e.Handled = true;
  182. }
  183. else if (e.KeyChar == (char)Keys.Escape)
  184. {
  185. if (CancelButton != null && !CancelButton.IsDisposed)
  186. CancelButton.InvokeMouseClick(EventArgs.Empty);
  187. e.Handled = true;
  188. }
  189. }
  190. void DrawItem()
  191. {
  192. int x = ItemImage.DisplayLocation.X, y = ItemImage.DisplayLocation.Y;
  193. Size s = Libraries.Items.GetTrueSize(ImageIndex);
  194. x += (ItemImage.Size.Width - s.Width) / 2;
  195. y += (ItemImage.Size.Height - s.Height) / 2;
  196. Libraries.Items.Draw(ImageIndex, x, y);
  197. }
  198. public override void Show()
  199. {
  200. if (Parent != null) return;
  201. Parent = MirScene.ActiveScene;
  202. Highlight();
  203. for (int i = 0; i < Program.Form.Controls.Count; i++)
  204. {
  205. TextBox T = Program.Form.Controls[i] as TextBox;
  206. if (T != null && T.Tag != null && T.Tag != null)
  207. ((MirTextBox)T.Tag).DialogChanged();
  208. }
  209. /*
  210. CMain.Shift = false;
  211. CMain.Ctrl = false;
  212. CMain.Alt = false;
  213. Parent = MirScene.ActiveScene;
  214. Activate();
  215. Highlight();
  216. for (int i = 0; i < Main.This.Controls.Count; i++)
  217. {
  218. TextBox T = (TextBox)Main.This.Controls[i];
  219. if (T != null && T.Tag != null && T.Tag != null)
  220. ((MirTextBox)T.Tag).DialogChanged();
  221. }*/
  222. }
  223. public override void OnKeyDown(KeyEventArgs e)
  224. {
  225. base.OnKeyDown(e);
  226. e.Handled = true;
  227. }
  228. public override void OnKeyUp(KeyEventArgs e)
  229. {
  230. base.OnKeyUp(e);
  231. e.Handled = true;
  232. }
  233. public override void OnKeyPress(KeyPressEventArgs e)
  234. {
  235. base.OnKeyPress(e);
  236. if (e.KeyChar == (char)Keys.Escape)
  237. CancelButton.InvokeMouseClick(EventArgs.Empty);
  238. else if (e.KeyChar == (char)Keys.Enter)
  239. OKButton.InvokeMouseClick(EventArgs.Empty);
  240. e.Handled = true;
  241. }
  242. #region Disposable
  243. protected override void Dispose(bool disposing)
  244. {
  245. base.Dispose(disposing);
  246. if (!disposing) return;
  247. for (int i = 0; i < Program.Form.Controls.Count; i++)
  248. {
  249. TextBox T = (TextBox)Program.Form.Controls[i];
  250. if (T != null && T.Tag != null && T.Tag != null)
  251. ((MirTextBox)T.Tag).DialogChanged();
  252. }
  253. }
  254. #endregion
  255. }
  256. }