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

Add IME Support for DX and XNA #32

Closed
wants to merge 7 commits into from
Closed
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
12 changes: 12 additions & 0 deletions Input/CompositionChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// MonoGame.IMEHelper.Common
// Copyright (c) 2020 ryancheung

using System;

namespace Rampastring.XNAUI.Input;

internal sealed class CompositionChangedEventArgs(string oldValue, string newValue) : EventArgs
{
public string OldValue => oldValue;
public string NewValue => newValue;
}
84 changes: 84 additions & 0 deletions Input/IMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// MonoGame.IMEHelper.Common
// Copyright (c) 2020 ryancheung

using System;

using Microsoft.Xna.Framework;

using Rampastring.XNAUI.PlatformSpecific;

namespace Rampastring.XNAUI.Input;

internal abstract class IMEHandler
{
private string composition = string.Empty;

/// <summary>
/// Check if text composition is enabled currently.
/// </summary>
public abstract bool Enabled { get; protected set; }

/// <summary>
/// Composition String
/// </summary>
public virtual string Composition
{
get => composition;
protected set
{
string old = composition;
composition = value;
CompositionChanged?.Invoke(null, new(old, value));
}
}
/// <summary>
/// Caret position of the composition
/// </summary>
public virtual int CompositionCursorPos { get; protected set; }

/// <summary>
/// Invoked when the IMM service emit character input event.
/// </summary>
/// <seealso cref="StartTextComposition" />
/// <seealso cref="StopTextComposition" />
public event EventHandler<char> TextInput;
public event EventHandler<CompositionChangedEventArgs> CompositionChanged;

public static IMEHandler Create(Game game)
{
#if !GL
return new WinFormsIMEHandler(game);
#else
return new SdlIMEHandler(game);
#endif
}

/// <summary>
/// Enable the system IMM service to support composited character input.
/// This should be called when you expect text input from a user and you support languages
/// that require an IME (Input Method Editor).
/// </summary>
/// <seealso cref="StopTextComposition" />
public abstract void StartTextComposition();

/// <summary>
/// Stop the system IMM service.
/// </summary>
/// <seealso cref="StartTextComposition" />
public abstract void StopTextComposition();

/// <summary>
/// Use this function to set the rectangle used to type Unicode text inputs if IME supported.
/// In SDL2, this method call gives the OS a hint for where to show the candidate text list,
/// since the OS doesn't know where you want to draw the text you received via SDL_TEXTEDITING event.
/// </summary>
public virtual void SetTextInputRect(in Rectangle rect)
{ }

/// <summary>
/// Trigger a text input event.
/// </summary>
/// <param name="character"></param>
protected virtual void OnTextInput(char character)
=> TextInput?.Invoke(this, character);
}
27 changes: 27 additions & 0 deletions PlatformSpecific/SdlIMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Xna.Framework;

using Rampastring.XNAUI.Input;

namespace Rampastring.XNAUI.PlatformSpecific;

/// <summary>
/// Integrate IME to DesktopGL(SDL2) platform.
/// </summary>
/// <remarks>
/// Note: We were unable to provide reliable input method support for
/// SDL2 due to the lack of a way to be able to stabilize hooks for
/// the SDL2 main loop.<br/>
/// Perhaps this requires some changes in Monogame.
/// </remarks>
internal sealed class SdlIMEHandler(Game game) : IMEHandler
{
public override bool Enabled { get => false; protected set => _ = value; }

public override void StartTextComposition()
{
}

public override void StopTextComposition()
{
}
}
42 changes: 42 additions & 0 deletions PlatformSpecific/WinFormsIMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using ImeSharp;

using Microsoft.Xna.Framework;

using Rampastring.XNAUI.Input;

namespace Rampastring.XNAUI.PlatformSpecific;

/// <summary>
/// Integrate IME to XNA framework.
/// </summary>
internal class WinFormsIMEHandler : IMEHandler
{
public override bool Enabled
{
get => InputMethod.Enabled;
protected set => InputMethod.Enabled = value;
}

public WinFormsIMEHandler(Game game)
{
InputMethod.Initialize(game.Window.Handle);
InputMethod.TextInputCallback = OnTextInput;
InputMethod.TextCompositionCallback = (compositionText, cursorPosition, _, _, _, _) =>
{
Composition = compositionText.ToString();
CompositionCursorPos = cursorPosition;
};
}


public override void StartTextComposition()
=> Enabled = true;


public override void StopTextComposition()
=> Enabled = false;


public override void SetTextInputRect(in Rectangle rect)
=> InputMethod.SetTextInputRect(rect.X, rect.Y, rect.Width, rect.Height);
}
11 changes: 11 additions & 0 deletions Rampastring.XNAUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@
<Compile Remove="Input\KeyboardEventArgs.cs" />
<Compile Remove="Input\KeyboardEventInput.cs" />
</ItemGroup>
<ItemGroup Condition="$(Configuration.Contains('GL'))">
<!--Remove WinForm-->
<Compile Remove="PlatformSpecific\WinFormsIMEHandler.cs" />
<None Include="PlatformSpecific\WinFormsIMEHandler.cs" />
</ItemGroup>
<ItemGroup Condition="!$(Configuration.Contains('GL'))">
<!--Remove SDL-->
<Compile Remove="PlatformSpecific\SdlIMEHandler.cs" />
<None Include="PlatformSpecific\SdlIMEHandler.cs" />
<PackageReference Include="ImeSharp" Version="1.2.5" />
</ItemGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Rampastring.XNAUI;
public class WindowManager : DrawableGameComponent
{
private const int XNA_MAX_TEXTURE_SIZE = 2048;
internal IMEHandler IMEHandler { get; }

/// <summary>
/// Creates a new WindowManager.
Expand All @@ -35,6 +36,7 @@ public class WindowManager : DrawableGameComponent
/// <param name="graphics">The game's GraphicsDeviceManager.</param>
public WindowManager(Game game, GraphicsDeviceManager graphics) : base(game)
{
IMEHandler = IMEHandler.Create(game);
this.graphics = graphics;
}

Expand Down Expand Up @@ -768,8 +770,8 @@ public override void Draw(GameTime gameTime)
Game.Window.ClientBounds.Width - (SceneXPosition * 2), Game.Window.ClientBounds.Height - (SceneYPosition * 2)), Color.White);

#if DEBUG
Renderer.DrawString("Active control " + activeControlName, 0, Vector2.Zero, Color.Red, 1.0f);

Renderer.DrawString("Active control: " + activeControlName, 0, Vector2.Zero, Color.Red, 1.0f);
Renderer.DrawString("IME Status: " + (IMEHandler.Enabled ? "Enabled" : "Disabled"), 0, new Vector2(0, 16), Color.Red, 1.0f);
#endif
if (Cursor.Visible)
Cursor.Draw(gameTime);
Expand Down
Loading