QuickDialogSystem.OpenDialog.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Content.Shared.Administration;
  2. using JetBrains.Annotations;
  3. using Robust.Shared.Player;
  4. namespace Content.Server.Administration;
  5. public sealed partial class QuickDialogSystem
  6. {
  7. /// <summary>
  8. /// Opens a dialog for the given client, allowing them to enter in the desired data.
  9. /// </summary>
  10. /// <param name="session">Client to show a dialog for.</param>
  11. /// <param name="title">Title of the dialog.</param>
  12. /// <param name="prompt">The prompt.</param>
  13. /// <param name="okAction">The action to execute upon Ok being pressed.</param>
  14. /// <param name="cancelAction">The action to execute upon the dialog being cancelled.</param>
  15. /// <typeparam name="T1">Type of the input.</typeparam>
  16. [PublicAPI]
  17. public void OpenDialog<T1>(ICommonSession session, string title, string prompt, Action<T1> okAction,
  18. Action? cancelAction = null)
  19. {
  20. OpenDialogInternal(
  21. session,
  22. title,
  23. new List<QuickDialogEntry>
  24. {
  25. new("1", TypeToEntryType(typeof(T1)), prompt)
  26. },
  27. QuickDialogButtonFlag.OkButton | QuickDialogButtonFlag.CancelButton,
  28. (ev =>
  29. {
  30. if (TryParseQuickDialog<T1>(TypeToEntryType(typeof(T1)), ev.Responses["1"], out var v1))
  31. okAction.Invoke(v1);
  32. else
  33. {
  34. session.Channel.Disconnect("Replied with invalid quick dialog data.");
  35. cancelAction?.Invoke();
  36. }
  37. }),
  38. cancelAction ?? (() => { })
  39. );
  40. }
  41. /// <summary>
  42. /// Opens a dialog for the given client, allowing them to enter in the desired data.
  43. /// </summary>
  44. /// <param name="session">Client to show a dialog for.</param>
  45. /// <param name="title">Title of the dialog.</param>
  46. /// <param name="prompt1">The first prompt.</param>
  47. /// <param name="prompt2">The second prompt.</param>
  48. /// <param name="okAction">The action to execute upon Ok being pressed.</param>
  49. /// <param name="cancelAction">The action to execute upon the dialog being cancelled.</param>
  50. /// <typeparam name="T1">Type of the first input.</typeparam>
  51. /// <typeparam name="T2">Type of the second input.</typeparam>
  52. [PublicAPI]
  53. public void OpenDialog<T1, T2>(ICommonSession session, string title, string prompt1, string prompt2,
  54. Action<T1, T2> okAction, Action? cancelAction = null)
  55. {
  56. OpenDialogInternal(
  57. session,
  58. title,
  59. new List<QuickDialogEntry>
  60. {
  61. new("1", TypeToEntryType(typeof(T1)), prompt1),
  62. new("2", TypeToEntryType(typeof(T2)), prompt2)
  63. },
  64. QuickDialogButtonFlag.OkButton | QuickDialogButtonFlag.CancelButton,
  65. (ev =>
  66. {
  67. if (TryParseQuickDialog<T1>(TypeToEntryType(typeof(T1)), ev.Responses["1"], out var v1) &&
  68. TryParseQuickDialog<T2>(TypeToEntryType(typeof(T2)), ev.Responses["2"], out var v2)
  69. )
  70. okAction.Invoke(v1, v2);
  71. else
  72. {
  73. session.Channel.Disconnect("Replied with invalid quick dialog data.");
  74. cancelAction?.Invoke();
  75. }
  76. }),
  77. cancelAction ?? (() => { })
  78. );
  79. }
  80. /// <summary>
  81. /// Opens a dialog for the given client, allowing them to enter in the desired data.
  82. /// </summary>
  83. /// <param name="session">Client to show a dialog for.</param>
  84. /// <param name="title">Title of the dialog.</param>
  85. /// <param name="prompt1">The first prompt.</param>
  86. /// <param name="prompt2">The second prompt.</param>
  87. /// <param name="prompt3">The third prompt.</param>
  88. /// <param name="okAction">The action to execute upon Ok being pressed.</param>
  89. /// <param name="cancelAction">The action to execute upon the dialog being cancelled.</param>
  90. /// <typeparam name="T1">Type of the first input.</typeparam>
  91. /// <typeparam name="T2">Type of the second input.</typeparam>
  92. /// <typeparam name="T3">Type of the third input.</typeparam>
  93. [PublicAPI]
  94. public void OpenDialog<T1, T2, T3>(ICommonSession session, string title, string prompt1, string prompt2,
  95. string prompt3, Action<T1, T2, T3> okAction, Action? cancelAction = null)
  96. {
  97. OpenDialogInternal(
  98. session,
  99. title,
  100. new List<QuickDialogEntry>
  101. {
  102. new("1", TypeToEntryType(typeof(T1)), prompt1),
  103. new("2", TypeToEntryType(typeof(T2)), prompt2),
  104. new("3", TypeToEntryType(typeof(T3)), prompt3)
  105. },
  106. QuickDialogButtonFlag.OkButton | QuickDialogButtonFlag.CancelButton,
  107. (ev =>
  108. {
  109. if (TryParseQuickDialog<T1>(TypeToEntryType(typeof(T1)), ev.Responses["1"], out var v1) &&
  110. TryParseQuickDialog<T2>(TypeToEntryType(typeof(T2)), ev.Responses["2"], out var v2) &&
  111. TryParseQuickDialog<T3>(TypeToEntryType(typeof(T3)), ev.Responses["3"], out var v3)
  112. )
  113. okAction.Invoke(v1, v2, v3);
  114. else
  115. {
  116. session.Channel.Disconnect("Replied with invalid quick dialog data.");
  117. cancelAction?.Invoke();
  118. }
  119. }),
  120. cancelAction ?? (() => { })
  121. );
  122. }
  123. /// <summary>
  124. /// Opens a dialog for the given client, allowing them to enter in the desired data.
  125. /// </summary>
  126. /// <param name="session">Client to show a dialog for.</param>
  127. /// <param name="title">Title of the dialog.</param>
  128. /// <param name="prompt1">The first prompt.</param>
  129. /// <param name="prompt2">The second prompt.</param>
  130. /// <param name="prompt3">The third prompt.</param>
  131. /// <param name="prompt4">The fourth prompt.</param>
  132. /// <param name="okAction">The action to execute upon Ok being pressed.</param>
  133. /// <param name="cancelAction">The action to execute upon the dialog being cancelled.</param>
  134. /// <typeparam name="T1">Type of the first input.</typeparam>
  135. /// <typeparam name="T2">Type of the second input.</typeparam>
  136. /// <typeparam name="T3">Type of the third input.</typeparam>
  137. /// <typeparam name="T4">Type of the fourth input.</typeparam>
  138. [PublicAPI]
  139. public void OpenDialog<T1, T2, T3, T4>(ICommonSession session, string title, string prompt1, string prompt2,
  140. string prompt3, string prompt4, Action<T1, T2, T3, T4> okAction, Action? cancelAction = null)
  141. {
  142. OpenDialogInternal(
  143. session,
  144. title,
  145. new List<QuickDialogEntry>
  146. {
  147. new("1", TypeToEntryType(typeof(T1)), prompt1),
  148. new("2", TypeToEntryType(typeof(T2)), prompt2),
  149. new("3", TypeToEntryType(typeof(T3)), prompt3),
  150. new("4", TypeToEntryType(typeof(T4)), prompt4),
  151. },
  152. QuickDialogButtonFlag.OkButton | QuickDialogButtonFlag.CancelButton,
  153. (ev =>
  154. {
  155. if (TryParseQuickDialog<T1>(TypeToEntryType(typeof(T1)), ev.Responses["1"], out var v1) &&
  156. TryParseQuickDialog<T2>(TypeToEntryType(typeof(T2)), ev.Responses["2"], out var v2) &&
  157. TryParseQuickDialog<T3>(TypeToEntryType(typeof(T3)), ev.Responses["3"], out var v3) &&
  158. TryParseQuickDialog<T4>(TypeToEntryType(typeof(T4)), ev.Responses["4"], out var v4)
  159. )
  160. okAction.Invoke(v1, v2, v3, v4);
  161. else
  162. {
  163. session.Channel.Disconnect("Replied with invalid quick dialog data.");
  164. cancelAction?.Invoke();
  165. }
  166. }),
  167. cancelAction ?? (() => { })
  168. );
  169. }
  170. }