IMECompositionEvent.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine.InputSystem.Utilities;
  6. namespace UnityEngine.InputSystem.LowLevel
  7. {
  8. /// <summary>
  9. /// A specialized event that contains the current IME Composition string, if IME is enabled and active.
  10. /// This event contains the entire current string to date, and once a new composition is submitted will send a blank string event.
  11. /// </summary>
  12. [StructLayout(LayoutKind.Explicit, Size = InputEvent.kBaseEventSize + sizeof(int) + (sizeof(char) * kIMECharBufferSize))]
  13. public struct IMECompositionEvent : IInputEventTypeInfo
  14. {
  15. // These needs to match the native ImeCompositionStringInputEventData settings
  16. internal const int kIMECharBufferSize = 64;
  17. public const int Type = 0x494D4553;
  18. [FieldOffset(0)]
  19. public InputEvent baseEvent;
  20. [FieldOffset(InputEvent.kBaseEventSize)]
  21. public IMECompositionString compositionString;
  22. public FourCC typeStatic => Type;
  23. public static IMECompositionEvent Create(int deviceId, string compositionString, double time)
  24. {
  25. var inputEvent = new IMECompositionEvent();
  26. inputEvent.baseEvent = new InputEvent(Type, InputEvent.kBaseEventSize + sizeof(int) + (sizeof(char) * kIMECharBufferSize), deviceId, time);
  27. inputEvent.compositionString = new IMECompositionString(compositionString);
  28. return inputEvent;
  29. }
  30. }
  31. /// <summary>
  32. /// A struct representing an string of characters generated by an IME for text input.
  33. /// </summary>
  34. /// <remarks>
  35. /// This is the internal representation of character strings in the event stream. It is exposed to user content through the
  36. /// <see cref="ITextInputReceiver.OnIMECompositionChanged"/> method. It can easily be converted to a normal C# string using
  37. /// <see cref="ToString"/>, but is exposed as the raw struct to avoid allocating memory by default.
  38. /// </remarks>
  39. [StructLayout(LayoutKind.Explicit, Size = sizeof(int) + sizeof(char) * LowLevel.IMECompositionEvent.kIMECharBufferSize)]
  40. public unsafe struct IMECompositionString : IEnumerable<char>
  41. {
  42. internal struct Enumerator : IEnumerator<char>
  43. {
  44. IMECompositionString m_CompositionString;
  45. char m_CurrentCharacter;
  46. int m_CurrentIndex;
  47. public Enumerator(IMECompositionString compositionString)
  48. {
  49. m_CompositionString = compositionString;
  50. m_CurrentCharacter = '\0';
  51. m_CurrentIndex = -1;
  52. }
  53. public bool MoveNext()
  54. {
  55. int size = m_CompositionString.Count;
  56. m_CurrentIndex++;
  57. if (m_CurrentIndex == size)
  58. return false;
  59. fixed(char* ptr = m_CompositionString.buffer)
  60. {
  61. m_CurrentCharacter = *(ptr + m_CurrentIndex);
  62. }
  63. return true;
  64. }
  65. public void Reset()
  66. {
  67. m_CurrentIndex = -1;
  68. }
  69. public void Dispose()
  70. {
  71. }
  72. public char Current => m_CurrentCharacter;
  73. object IEnumerator.Current => Current;
  74. }
  75. public int Count => size;
  76. public char this[int index]
  77. {
  78. get
  79. {
  80. if (index >= Count || index < 0)
  81. throw new ArgumentOutOfRangeException(nameof(index));
  82. fixed(char* ptr = buffer)
  83. {
  84. return *(ptr + index);
  85. }
  86. }
  87. }
  88. [FieldOffset(0)]
  89. int size;
  90. [FieldOffset(sizeof(int))]
  91. fixed char buffer[IMECompositionEvent.kIMECharBufferSize];
  92. public IMECompositionString(string characters)
  93. {
  94. if (string.IsNullOrEmpty(characters))
  95. {
  96. size = 0;
  97. return;
  98. }
  99. Debug.Assert(characters.Length < IMECompositionEvent.kIMECharBufferSize);
  100. size = characters.Length;
  101. for (var i = 0; i < size; i++)
  102. buffer[i] = characters[i];
  103. }
  104. public override string ToString()
  105. {
  106. fixed(char* ptr = buffer)
  107. {
  108. return new string(ptr, 0, size);
  109. }
  110. }
  111. public IEnumerator<char> GetEnumerator()
  112. {
  113. return new Enumerator(this);
  114. }
  115. IEnumerator IEnumerable.GetEnumerator()
  116. {
  117. return GetEnumerator();
  118. }
  119. }
  120. }