源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 line
1.6 KiB

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6. namespace ConsoleWindows
  7. {
  8. public class ConsoleInput
  9. {
  10. public event System.Action<string> OnInputText;
  11. public string inputString;
  12. public void ClearLine()
  13. {
  14. Console.CursorLeft = 0;
  15. Console.Write( new String( ' ', Console.BufferWidth ) );
  16. Console.CursorTop--;
  17. Console.CursorLeft = 0;
  18. }
  19. public void RedrawInputLine()
  20. {
  21. if ( inputString.Length == 0 ) return;
  22. if ( Console.CursorLeft > 0 )
  23. ClearLine();
  24. //System.Console.ForegroundColor = ConsoleColor.Green;
  25. System.Console.Write( inputString );
  26. }
  27. internal void OnBackspace()
  28. {
  29. if ( inputString.Length < 1 ) return;
  30. inputString = inputString.Substring( 0, inputString.Length - 1 );
  31. RedrawInputLine();
  32. }
  33. internal void OnEscape()
  34. {
  35. ClearLine();
  36. inputString = "";
  37. }
  38. internal void OnEnter()
  39. {
  40. ClearLine();
  41. //System.Console.ForegroundColor = ConsoleColor.Green;
  42. System.Console.WriteLine( "> " + inputString );
  43. var strtext = inputString;
  44. inputString = "";
  45. if ( OnInputText != null )
  46. {
  47. OnInputText( strtext );
  48. }
  49. }
  50. public void Update()
  51. {
  52. if ( !Console.KeyAvailable ) return;
  53. var key = Console.ReadKey();
  54. if ( key.Key == ConsoleKey.Enter )
  55. {
  56. OnEnter();
  57. return;
  58. }
  59. if ( key.Key == ConsoleKey.Backspace )
  60. {
  61. OnBackspace();
  62. return;
  63. }
  64. if ( key.Key == ConsoleKey.Escape )
  65. {
  66. OnEscape();
  67. return;
  68. }
  69. if ( key.KeyChar != '\u0000' )
  70. {
  71. inputString += key.KeyChar;
  72. RedrawInputLine();
  73. return;
  74. }
  75. }
  76. }
  77. }