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 4 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
75 changes: 75 additions & 0 deletions Input/IME/IMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// MonoGame.IMEHelper.Common
// Copyright (c) 2020 ryancheung

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Rampastring.XNAUI.Input.IME
{
internal abstract class IMEHandler
{
public static IMEHandler Create(Game game)
{
#if !GL
return new WinForms.WinFormsIMEHandler(game);
#else
return new Sdl.SdlIMEHandler(game);
#endif
}

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

/// <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>
/// Composition String
/// </summary>
public virtual string Composition { get; protected set; } = string.Empty;

/// <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;

/// <summary>
/// Trigger a text input event.
/// </summary>
/// <param name="character"></param>
protected virtual void OnTextInput(char character)
{
TextInput?.Invoke(this, character);
}
frg2089 marked this conversation as resolved.
Show resolved Hide resolved
}
}
20 changes: 20 additions & 0 deletions Input/IME/Sdl/SdlIMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Xna.Framework;

namespace Rampastring.XNAUI.Input.IME.Sdl
{
/// <summary>
/// Integrate IME to DesktopGL(SDL2) platform.
/// </summary>
internal sealed class SdlIMEHandler(Game game) : IMEHandler
{
public override bool Enabled { get => false; protected set => _ = value; }

public override void StartTextComposition()
{
}

public override void StopTextComposition()
{
}
frg2089 marked this conversation as resolved.
Show resolved Hide resolved
}
}
44 changes: 44 additions & 0 deletions Input/IME/WinForms/WinFormsIMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using ImeSharp;

using Microsoft.Xna.Framework;

namespace Rampastring.XNAUI.Input.IME.WinForms
{
/// <summary>
/// Integrate IME to XNA framework.
/// </summary>
internal class WinFormsIMEHandler : IMEHandler
{
public WinFormsIMEHandler(Game game)
{
InputMethod.Initialize(game.Window.Handle);
InputMethod.TextInputCallback = OnTextInput;
InputMethod.TextCompositionCallback = (compositionText, cursorPosition, _, _, _, _) =>
{
Composition = compositionText.ToString();
CompositionCursorPos = cursorPosition;
};
}

public override bool Enabled
{
get => InputMethod.Enabled;
protected set => InputMethod.Enabled = value;
}
frg2089 marked this conversation as resolved.
Show resolved Hide resolved

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);
}
frg2089 marked this conversation as resolved.
Show resolved Hide resolved
}
}
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="Input\IME\WinForms\*.cs" />
<None Include="Input\IME\WinForms\*.cs" />
</ItemGroup>
<ItemGroup Condition="!$(Configuration.Contains('GL'))">
<!--Remove SDL-->
<Compile Remove="Input\IME\Sdl\*.cs" />
<None Include="Input\IME\Sdl\*.cs" />
<PackageReference Include="ImeSharp" Version="1.2.5" />
</ItemGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Diagnostics;
using System.Linq;
using Rampastring.XNAUI.PlatformSpecific;
using Rampastring.XNAUI.Input.IME;
#if NETFRAMEWORK
using System.Reflection;
#endif
Expand All @@ -27,6 +28,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 +37,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 +771,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