ASK.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. namespace OpenVIII.Fields.Scripts.Instructions
  2. {
  3. /// <summary>
  4. /// Opens a field message window and lets player choose a single line.
  5. /// ASK saves the chosen line index into a temp variable which you can retrieve with PSHI_L 0.
  6. /// AASK is an upgrade that also lets you set the window position.
  7. /// </summary>
  8. internal sealed class ASK : JsmInstruction
  9. {
  10. #region Fields
  11. private readonly IJsmExpression _beginLine;
  12. private readonly IJsmExpression _cancelLine;
  13. private readonly IJsmExpression _channel;
  14. private readonly IJsmExpression _firstLine;
  15. private readonly IJsmExpression _lastLine;
  16. private readonly IJsmExpression _messageId;
  17. #endregion Fields
  18. #region Constructors
  19. public ASK(IJsmExpression channel, IJsmExpression messageId, IJsmExpression firstLine, IJsmExpression lastLine, IJsmExpression beginLine, IJsmExpression cancelLine)
  20. {
  21. _channel = channel;
  22. _messageId = messageId;
  23. _firstLine = firstLine;
  24. _lastLine = lastLine;
  25. _beginLine = beginLine;
  26. _cancelLine = cancelLine;
  27. }
  28. public ASK(int parameter, IStack<IJsmExpression> stack)
  29. : this(
  30. cancelLine: stack.Pop(),
  31. beginLine: stack.Pop(),
  32. lastLine: stack.Pop(),
  33. firstLine: stack.Pop(),
  34. messageId: stack.Pop(),
  35. channel: stack.Pop())
  36. {
  37. }
  38. #endregion Constructors
  39. #region Methods
  40. public override void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services)
  41. {
  42. if (_messageId is IConstExpression message)
  43. FormatHelper.FormatAnswers(sw, formatterContext.GetMessage(message.Int32()), _firstLine, _lastLine, _beginLine, _cancelLine);
  44. sw.Format(formatterContext, services)
  45. .Await()
  46. .StaticType(nameof(IMessageService))
  47. .Method(nameof(IMessageService.ShowDialog))
  48. .Argument("channel", _channel)
  49. .Argument("messageId", _messageId)
  50. .Argument("firstLine", _firstLine)
  51. .Argument("lastLine", _lastLine)
  52. .Argument("beginLine", _beginLine)
  53. .Argument("cancelLine", _cancelLine)
  54. .Comment(nameof(AASK));
  55. }
  56. public override IAwaitable TestExecute(IServices services) => ServiceId.Message[services].ShowQuestion(
  57. _channel.Int32(services),
  58. _messageId.Int32(services),
  59. _firstLine.Int32(services),
  60. _lastLine.Int32(services),
  61. _beginLine.Int32(services),
  62. _cancelLine.Int32(services));
  63. public override string ToString() => $"{nameof(ASK)}({nameof(_channel)}: {_channel}, {nameof(_messageId)}: {_messageId}, {nameof(_firstLine)}: {_firstLine}, {nameof(_lastLine)}: {_lastLine}, {nameof(_beginLine)}: {_beginLine}, {nameof(_cancelLine)}: {_cancelLine})";
  64. #endregion Methods
  65. }
  66. }