Skip to content

Commit

Permalink
Add CJK IME Support
Browse files Browse the repository at this point in the history
DX  XNA (WinForms base) Only
OGL UGL (SDL base) are not support

Signed-off-by: 舰队的偶像-岛风酱! <[email protected]>
  • Loading branch information
frg2089 committed Jan 23, 2025
1 parent b865f27 commit 6fe7ef1
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
11 changes: 11 additions & 0 deletions ClientCore/ClientCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
<None Remove="Resources\*.png" />
<EmbeddedResource Include="Resources\*.png" />
</ItemGroup>
<ItemGroup Condition="$(Configuration.Contains('GL'))">
<!--Remove WinForm-->
<Compile Remove="IME\WinFormsIMEHandler.cs" />
<None Include="IME\WinFormsIMEHandler.cs" />
</ItemGroup>
<ItemGroup Condition="!$(Configuration.Contains('GL'))">
<!--Remove SDL-->
<Compile Remove="IME\SdlIMEHandler.cs" />
<None Include="IME\SdlIMEHandler.cs" />
<PackageReference Include="ImeSharp" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Rampastring.Tools" />
<PackageReference Include="Rampastring.XNAUI.$(Engine)" Condition="'!$(Configuration.Contains(Debug))'" />
Expand Down
52 changes: 52 additions & 0 deletions ClientCore/IME/IMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;

using Microsoft.Xna.Framework;

using Rampastring.XNAUI.Input;
using Rampastring.XNAUI.XNAControls;

namespace ClientCore.IME;

public abstract class IMEHandler : IIMEHandler
{
private string _composition = string.Empty;

public abstract bool Enabled { get; protected set; }

public XNAControl IMEFocus { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public string Composition
{
get => _composition;
set
{
string old = _composition;
_composition = value;
CompositionChanged?.Invoke(null, new(old, value));
}
}

public virtual int CompositionCursorPosition { get; set; }

public event EventHandler<CharacterEventArgs> CharInput;
public event EventHandler<CompositionChangedEventArgs> CompositionChanged;
public static IMEHandler Create(Game game)
{
#if !GL
return new WinFormsIMEHandler(game);
#else
return new SdlIMEHandler(game);
#endif
}

public virtual void SetTextInputRectangle(Rectangle rectangle)
{
}

public abstract void StartTextComposition();

public abstract void StopTextComposition();

protected virtual void OnTextInput(char character)
=> CharInput?.Invoke(this, new(character));
}
25 changes: 25 additions & 0 deletions ClientCore/IME/SdlIMEHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Xna.Framework;

namespace ClientCore.IME;

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

Check warning on line 14 in ClientCore/IME/SdlIMEHandler.cs

View workflow job for this annotation

GitHub Actions / build-clients (Ares)

Parameter 'game' is unread.

Check warning on line 14 in ClientCore/IME/SdlIMEHandler.cs

View workflow job for this annotation

GitHub Actions / build-clients (TS)

Parameter 'game' is unread.

Check warning on line 14 in ClientCore/IME/SdlIMEHandler.cs

View workflow job for this annotation

GitHub Actions / build-clients (YR)

Parameter 'game' is unread.
{
public override bool Enabled { get => false; protected set => _ = value; }

public override void StartTextComposition()
{
}

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

using Microsoft.Xna.Framework;

namespace ClientCore.IME;

/// <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();
CompositionCursorPosition = cursorPosition;
};
}


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


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


public override void SetTextInputRectangle(Rectangle rect)
=> InputMethod.SetTextInputRect(rect.X, rect.Y, rect.Width, rect.Height);
}
8 changes: 6 additions & 2 deletions DXMainClient/DXGUI/GameClass.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ClientCore;
using ClientCore;
using ClientCore.CnCNet5;
using ClientCore.IME;
using ClientGUI;
using DTAClient.Domain;
using DTAClient.DXGUI.Generic;
Expand Down Expand Up @@ -144,7 +145,10 @@ protected override void Initialize()
#endif
InitializeUISettings();

WindowManager wm = new WindowManager(this, graphics);
WindowManager wm = new(this, graphics)
{
IMEHandler = IMEHandler.Create(this),
};
wm.Initialize(content, ProgramConstants.GetBaseResourcePath());

wm.ControlINIAttributeParsers.Add(new TranslationINIParser());
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</ItemGroup>
<ItemGroup>
<PackageVersion Include="DiscordRichPresence" Version="1.1.3.18" />
<PackageVersion Include="ImeSharp" Version="1.4.0" />
<PackageVersion Include="lzo.net" Version="0.0.6" />
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" />
Expand Down Expand Up @@ -51,7 +52,6 @@
<!-- and -p:Engine=WindowsDX -f net48 -->
<PackageReference Include="NETStandard.Library" />
<PackageVersion Include="NETStandard.Library" Version="2.0.3" />

<PackageReference Include="System.IO.FileSystem" />
<PackageVersion Include="System.IO.FileSystem" Version="4.3.0" />
</ItemGroup>
Expand Down

0 comments on commit 6fe7ef1

Please sign in to comment.