Skip to content

Commit 80634ac

Browse files
committed
Revamp text input event with the advanced ime support in SDL3; Remove ImeSharp package depedency; Upgrade UWP runtime package
1 parent 4d8c86b commit 80634ac

3 files changed

Lines changed: 46 additions & 75 deletions

File tree

FNA.NET.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@
426426
</ItemGroup>
427427

428428
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-windows10.0.26100.0'">
429-
<PackageReference Include="FNA.NET.NativeAssets.UWP" Version="2.0.3.2602" />
429+
<PackageReference Include="FNA.NET.NativeAssets.UWP" Version="2.0.5.2602" />
430430
</ItemGroup>
431431

432432
<PropertyGroup Condition="'$(TargetFramework)' == 'net9.0-windows10.0.26100.0'">
@@ -443,7 +443,6 @@
443443
</PropertyGroup>
444444

445445
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-windows'">
446-
<PackageReference Include="ImeSharp" Version="1.4.1" />
447446
</ItemGroup>
448447

449448
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0-android'">

src/FNAPlatform/SDL3_FNAPlatform.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,40 @@ ref bool textInputSuppress
11811181
}
11821182
}
11831183

1184+
else if (evt.type == (uint) SDL.SDL_EventType.SDL_EVENT_TEXT_EDITING_CANDIDATES)
1185+
{
1186+
if (evt.edit_candidates.num_candidates > 0)
1187+
{
1188+
string[] candidates = new string[evt.edit_candidates.num_candidates];
1189+
for (int i = 0; i < evt.edit_candidates.num_candidates; i++)
1190+
{
1191+
byte* candidatePtr = evt.edit_candidates.candidates[i];
1192+
int bytes = MeasureStringLength(candidatePtr);
1193+
if (bytes > 0)
1194+
{
1195+
int chars = Encoding.UTF8.GetChars(
1196+
candidatePtr,
1197+
bytes,
1198+
charsBuffer,
1199+
bytes
1200+
);
1201+
candidates[i] = new string(charsBuffer, 0, chars);
1202+
}
1203+
else
1204+
{
1205+
candidates[i] = string.Empty;
1206+
}
1207+
}
1208+
TextInputEXT.OnTextEditingCandidates(
1209+
candidates,
1210+
evt.edit_candidates.selected_candidate,
1211+
evt.edit_candidates.horizontal
1212+
);
1213+
}
1214+
else
1215+
TextInputEXT.OnTextEditingCandidates(Array.Empty<string>(), 0, false);
1216+
}
1217+
11841218
// Quit
11851219
else if (evt.type == (uint) SDL.SDL_EventType.SDL_EVENT_QUIT)
11861220
{

src/Input/TextInputEXT.cs

Lines changed: 11 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ public static class TextInputEXT
3333
/// </summary>
3434
public static event Action<string, int, int> TextEditing;
3535

36+
public static event Action<string[], int, bool> TextEditingCandidates;
37+
3638
#endregion
3739

3840
#region Public Properties
3941

40-
static IntPtr windowHandle;
4142
public static IntPtr WindowHandle
4243
{
43-
get => windowHandle;
44-
set
45-
{
46-
#if WINDOWS7_0_OR_GREATER && !WINDOWS10_0_17763_0_OR_GREATER
47-
if (value != IntPtr.Zero && windowHandle != value)
48-
AdvancedImeInit(value);
49-
#endif
50-
windowHandle = value;
51-
}
44+
get;
45+
set;
5246
}
5347

5448
#endregion
@@ -65,11 +59,7 @@ public static IntPtr WindowHandle
6559
/// <returns>True if text input state is active</returns>
6660
public static bool IsTextInputActive()
6761
{
68-
#if WINDOWS7_0_OR_GREATER && !WINDOWS10_0_17763_0_OR_GREATER
69-
return ImeSharp.InputMethod.Enabled;
70-
#else
7162
return FNAPlatform.IsTextInputActive(WindowHandle);
72-
#endif
7363
}
7464

7565
public static bool IsScreenKeyboardShown()
@@ -84,22 +74,12 @@ public static bool IsScreenKeyboardShown(IntPtr window)
8474

8575
public static void StartTextInput()
8676
{
87-
#if WINDOWS7_0_OR_GREATER && !WINDOWS10_0_17763_0_OR_GREATER
88-
// Need to ensure SDL2 text input is stopped
89-
FNAPlatform.StopTextInput(WindowHandle);
90-
ImeSharp.InputMethod.Enabled = true;
91-
#else
9277
FNAPlatform.StartTextInput(WindowHandle);
93-
#endif
9478
}
9579

9680
public static void StopTextInput()
9781
{
98-
#if WINDOWS7_0_OR_GREATER && !WINDOWS10_0_17763_0_OR_GREATER
99-
ImeSharp.InputMethod.Enabled = false;
100-
#else
10182
FNAPlatform.StopTextInput(WindowHandle);
102-
#endif
10383
}
10484

10585
/// <summary>
@@ -109,12 +89,7 @@ public static void StopTextInput()
10989
/// <param name="rectangle">Text input location relative to GameWindow.ClientBounds</param>
11090
public static void SetInputRectangle(Rectangle rectangle)
11191
{
112-
#if WINDOWS7_0_OR_GREATER && !WINDOWS10_0_17763_0_OR_GREATER
113-
if (ImeSharp.InputMethod.Enabled)
114-
ImeSharp.InputMethod.SetTextInputRect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
115-
#else
11692
FNAPlatform.SetTextInputRectangle(WindowHandle, rectangle);
117-
#endif
11893
}
11994

12095
#endregion
@@ -137,51 +112,14 @@ internal static void OnTextEditing(string text, int start, int length)
137112
}
138113
}
139114

140-
#endregion
141-
142-
#if WINDOWS7_0_OR_GREATER && !WINDOWS10_0_17763_0_OR_GREATER
143-
internal static void AdvancedImeInit(IntPtr sdlWindowHandle)
115+
internal static void OnTextEditingCandidates(string[] candidates, int selectedCandidate, bool horizontal)
144116
{
145-
var windowProps = SDL3.SDL.SDL_GetWindowProperties(sdlWindowHandle);
146-
nint hwnd = SDL3.SDL.SDL_GetPointerProperty(windowProps, SDL3.SDL.SDL_PROP_WINDOW_WIN32_HWND_POINTER, IntPtr.Zero);
147-
148-
// Only initialize InputMethod once
149-
if (ImeSharp.InputMethod.WindowHandle == IntPtr.Zero)
150-
ImeSharp.InputMethod.Initialize(hwnd, ShowOSImeWindow);
151-
152-
ImeSharp.InputMethod.TextInputCallback = OnTextInput;
153-
ImeSharp.InputMethod.TextCompositionCallback = (compositionText, cursorPosition) => {
154-
OnTextEditing(compositionText, cursorPosition, 0);
155-
};
117+
if (TextEditingCandidates != null)
118+
{
119+
TextEditingCandidates(candidates, selectedCandidate, horizontal);
120+
}
156121
}
157122

158-
/// <summary>
159-
/// Show the IME Candidate window rendered by the OS. Defaults to true.<br/>
160-
/// Set to <c>false</c> if you want to render the IME candidate list yourself.<br/>
161-
/// Note there's no way to toggle this option while game running! Please set this value main function or static initializer.<br/>
162-
/// **This is a Windows only API.**
163-
/// </summary>
164-
public static bool ShowOSImeWindow;
165-
166-
/// <summary>
167-
/// The candidate text list for the current composition.<br/>
168-
/// If the composition string does not generate candidates, the candidate page size is zero.
169-
/// This array is fixed length of 16.<br/>
170-
/// **This property is only supported on Windows.**
171-
/// </summary>
172-
public static ImeSharp.IMEString[] CandidateList => ImeSharp.InputMethod.CandidateList;
173-
174-
/// <summary>
175-
/// IME Candidate page size.<br/>
176-
/// **This property is only supported on Windows.**
177-
/// </summary>
178-
public static int CandidatePageSize => ImeSharp.InputMethod.CandidatePageSize;
179-
180-
/// <summary>
181-
/// The selected IME candidate index.<br/>
182-
/// **This property is only supported on Windows.**
183-
/// </summary>
184-
public static int CandidateSelection => ImeSharp.InputMethod.CandidateSelection;
185-
#endif
123+
#endregion
186124
}
187-
}
125+
}

0 commit comments

Comments
 (0)