Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce garbage from TextInput #6664

Merged
merged 2 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MonoGame.Framework/GameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ protected GameWindow()
/// This event is only supported on the Windows DirectX, Windows OpenGL and Linux platforms.
/// </remarks>
public event EventHandler<TextInputEventArgs> TextInput;

internal bool IsTextInputHandled { get { return TextInput != null; } }
#endif

#endregion Events
Expand Down
55 changes: 40 additions & 15 deletions MonoGame.Framework/SDL/SDLGamePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,48 @@ private void SdlRunLoop()
}
else if (ev.Type == Sdl.EventType.TextInput)
{
int len = 0;
string text = String.Empty;
unsafe
if (_view.IsTextInputHandled)
{
while (Marshal.ReadByte ((IntPtr)ev.Text.Text, len) != 0) {
len++;
int len = 0;
int utf8character = 0; // using an int to encode multibyte characters longer than 2 bytes
byte currentByte = 0;
int charByteSize = 0; // UTF8 char lenght to decode
unsafe
{
while ((currentByte = Marshal.ReadByte((IntPtr)ev.Text.Text, len)) != 0)
{
// we're reading the first UTF8 byte, we need to check if it's multibyte
if (charByteSize == 0)
{
if (currentByte < 192)
charByteSize = 1;
else if (currentByte < 224)
charByteSize = 2;
else if (currentByte < 240)
charByteSize = 3;
else
charByteSize = 4;

utf8character = 0;
}

// assembling the character
utf8character <<= 8;
utf8character |= currentByte;

charByteSize--;
if (charByteSize == 0) // finished decoding the current character
{
var key = KeyboardUtil.ToXna(utf8character);
// multibyte conversion might fail here, because the char type only handles UTF8 up to 2-byte characters
// we should switch to a String parameter for this event to fully support UTF8
// as-is, it won't support CJK characters and will produce wrong results
_view.CallTextInput((char)utf8character, key);
}

len++;
}
}
var buffer = new byte [len];
Marshal.Copy ((IntPtr)ev.Text.Text, buffer, 0, len);
text = System.Text.Encoding.UTF8.GetString (buffer);
}
if (text.Length == 0)
continue;
foreach (var c in text)
{
var key = KeyboardUtil.ToXna((int)c);
_view.CallTextInput(c, key);
}
}
else if (ev.Type == Sdl.EventType.WindowEvent)
Expand Down