mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-20 23:07:43 +01:00
Make CJK imes work better
This commit is contained in:
parent
280a9d6b05
commit
b6d88f798a
9 changed files with 1064 additions and 466 deletions
223
Dalamud/Interface/Internal/Windows/DalamudImeWindow.cs
Normal file
223
Dalamud/Interface/Internal/Windows/DalamudImeWindow.cs
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
using System.Numerics;
|
||||
|
||||
using Dalamud.Interface.Windowing;
|
||||
|
||||
using ImGuiNET;
|
||||
|
||||
namespace Dalamud.Interface.Internal.Windows;
|
||||
|
||||
/// <summary>
|
||||
/// A window for displaying IME details.
|
||||
/// </summary>
|
||||
internal unsafe class DalamudImeWindow : Window
|
||||
{
|
||||
private const int ImePageSize = 9;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DalamudImeWindow"/> class.
|
||||
/// </summary>
|
||||
public DalamudImeWindow()
|
||||
: base(
|
||||
"Dalamud IME",
|
||||
ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoBackground)
|
||||
{
|
||||
this.Size = default(Vector2);
|
||||
|
||||
this.RespectCloseHotkey = false;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Draw()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void PostDraw()
|
||||
{
|
||||
if (Service<DalamudIme>.GetNullable() is not { } ime)
|
||||
return;
|
||||
|
||||
var viewport = ime.AssociatedViewport;
|
||||
if (viewport.NativePtr is null)
|
||||
return;
|
||||
|
||||
var drawCand = ime.ImmCand.Count != 0;
|
||||
var drawConv = drawCand || ime.ShowPartialConversion;
|
||||
var drawIme = ime.InputModeIcon != null;
|
||||
|
||||
var pad = ImGui.GetStyle().WindowPadding;
|
||||
var candTextSize = ImGui.CalcTextSize(ime.ImmComp == string.Empty ? " " : ime.ImmComp);
|
||||
|
||||
var native = ime.ImmCandNative;
|
||||
var totalIndex = native.dwSelection + 1;
|
||||
var totalSize = native.dwCount;
|
||||
|
||||
var pageStart = native.dwPageStart;
|
||||
var pageIndex = (pageStart / ImePageSize) + 1;
|
||||
var pageCount = (totalSize / ImePageSize) + 1;
|
||||
var pageInfo = $"{totalIndex}/{totalSize} ({pageIndex}/{pageCount})";
|
||||
|
||||
// Calc the window size.
|
||||
var maxTextWidth = 0f;
|
||||
for (var i = 0; i < ime.ImmCand.Count; i++)
|
||||
{
|
||||
var textSize = ImGui.CalcTextSize($"{i + 1}. {ime.ImmCand[i]}");
|
||||
maxTextWidth = maxTextWidth > textSize.X ? maxTextWidth : textSize.X;
|
||||
}
|
||||
|
||||
maxTextWidth = maxTextWidth > ImGui.CalcTextSize(pageInfo).X ? maxTextWidth : ImGui.CalcTextSize(pageInfo).X;
|
||||
maxTextWidth = maxTextWidth > ImGui.CalcTextSize(ime.ImmComp).X
|
||||
? maxTextWidth
|
||||
: ImGui.CalcTextSize(ime.ImmComp).X;
|
||||
|
||||
var numEntries = (drawCand ? ime.ImmCand.Count + 1 : 0) + 1 + (drawIme ? 1 : 0);
|
||||
var spaceY = ImGui.GetStyle().ItemSpacing.Y;
|
||||
var imeWindowHeight = (spaceY * (numEntries - 1)) + (candTextSize.Y * numEntries);
|
||||
var windowSize = new Vector2(maxTextWidth, imeWindowHeight) + (pad * 2);
|
||||
|
||||
// 1. Figure out the expanding direction.
|
||||
var expandUpward = ime.CursorPos.Y + windowSize.Y > viewport.WorkPos.Y + viewport.WorkSize.Y;
|
||||
var windowPos = ime.CursorPos - pad;
|
||||
if (expandUpward)
|
||||
{
|
||||
windowPos.Y -= windowSize.Y - candTextSize.Y - (pad.Y * 2);
|
||||
if (drawIme)
|
||||
windowPos.Y += candTextSize.Y + spaceY;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (drawIme)
|
||||
windowPos.Y -= candTextSize.Y + spaceY;
|
||||
}
|
||||
|
||||
// 2. Contain within the viewport. Do not use clamp, as the target window might be too small.
|
||||
if (windowPos.X < viewport.WorkPos.X)
|
||||
windowPos.X = viewport.WorkPos.X;
|
||||
else if (windowPos.X + windowSize.X > viewport.WorkPos.X + viewport.WorkSize.X)
|
||||
windowPos.X = (viewport.WorkPos.X + viewport.WorkSize.X) - windowSize.X;
|
||||
if (windowPos.Y < viewport.WorkPos.Y)
|
||||
windowPos.Y = viewport.WorkPos.Y;
|
||||
else if (windowPos.Y + windowSize.Y > viewport.WorkPos.Y + viewport.WorkSize.Y)
|
||||
windowPos.Y = (viewport.WorkPos.Y + viewport.WorkSize.Y) - windowSize.Y;
|
||||
|
||||
var cursor = windowPos + pad;
|
||||
|
||||
// Draw the ime window.
|
||||
var drawList = ImGui.GetForegroundDrawList(viewport);
|
||||
|
||||
// Draw the background rect for candidates.
|
||||
if (drawCand)
|
||||
{
|
||||
Vector2 candRectLt, candRectRb;
|
||||
if (!expandUpward)
|
||||
{
|
||||
candRectLt = windowPos + candTextSize with { X = 0 } + pad with { X = 0 };
|
||||
candRectRb = windowPos + windowSize;
|
||||
if (drawIme)
|
||||
candRectLt.Y += spaceY + candTextSize.Y;
|
||||
}
|
||||
else
|
||||
{
|
||||
candRectLt = windowPos;
|
||||
candRectRb = windowPos + (windowSize - candTextSize with { X = 0 } - pad with { X = 0 });
|
||||
if (drawIme)
|
||||
candRectRb.Y -= spaceY + candTextSize.Y;
|
||||
}
|
||||
|
||||
drawList.AddRectFilled(
|
||||
candRectLt,
|
||||
candRectRb,
|
||||
ImGui.GetColorU32(ImGuiCol.WindowBg),
|
||||
ImGui.GetStyle().WindowRounding);
|
||||
}
|
||||
|
||||
if (!expandUpward && drawIme)
|
||||
{
|
||||
drawList.AddText(cursor, ImGui.GetColorU32(ImGuiCol.Text), ime.InputModeIcon);
|
||||
cursor.Y += candTextSize.Y + spaceY;
|
||||
}
|
||||
|
||||
if (!expandUpward && drawConv)
|
||||
{
|
||||
DrawTextBeingConverted();
|
||||
cursor.Y += candTextSize.Y + spaceY;
|
||||
|
||||
// Add a separator.
|
||||
drawList.AddLine(cursor, cursor + new Vector2(maxTextWidth, 0), ImGui.GetColorU32(ImGuiCol.Separator));
|
||||
}
|
||||
|
||||
if (drawCand)
|
||||
{
|
||||
// Add the candidate words.
|
||||
for (var i = 0; i < ime.ImmCand.Count; i++)
|
||||
{
|
||||
var selected = i == (native.dwSelection % ImePageSize);
|
||||
var color = ImGui.GetColorU32(ImGuiCol.Text);
|
||||
if (selected)
|
||||
color = ImGui.GetColorU32(ImGuiCol.NavHighlight);
|
||||
|
||||
drawList.AddText(cursor, color, $"{i + 1}. {ime.ImmCand[i]}");
|
||||
cursor.Y += candTextSize.Y + spaceY;
|
||||
}
|
||||
|
||||
// Add a separator
|
||||
drawList.AddLine(cursor, cursor + new Vector2(maxTextWidth, 0), ImGui.GetColorU32(ImGuiCol.Separator));
|
||||
|
||||
// Add the pages infomation.
|
||||
drawList.AddText(cursor, ImGui.GetColorU32(ImGuiCol.Text), pageInfo);
|
||||
cursor.Y += candTextSize.Y + spaceY;
|
||||
}
|
||||
|
||||
if (expandUpward && drawConv)
|
||||
{
|
||||
// Add a separator.
|
||||
drawList.AddLine(cursor, cursor + new Vector2(maxTextWidth, 0), ImGui.GetColorU32(ImGuiCol.Separator));
|
||||
|
||||
DrawTextBeingConverted();
|
||||
cursor.Y += candTextSize.Y + spaceY;
|
||||
}
|
||||
|
||||
if (expandUpward && drawIme)
|
||||
{
|
||||
drawList.AddText(cursor, ImGui.GetColorU32(ImGuiCol.Text), ime.InputModeIcon);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
void DrawTextBeingConverted()
|
||||
{
|
||||
// Draw the text background.
|
||||
drawList.AddRectFilled(
|
||||
cursor - (pad / 2),
|
||||
cursor + candTextSize + (pad / 2),
|
||||
ImGui.GetColorU32(ImGuiCol.WindowBg));
|
||||
|
||||
// If only a part of the full text is marked for conversion, then draw background for the part being edited.
|
||||
if (ime.PartialConversionFrom != 0 || ime.PartialConversionTo != ime.ImmComp.Length)
|
||||
{
|
||||
var part1 = ime.ImmComp[..ime.PartialConversionFrom];
|
||||
var part2 = ime.ImmComp[..ime.PartialConversionTo];
|
||||
var size1 = ImGui.CalcTextSize(part1);
|
||||
var size2 = ImGui.CalcTextSize(part2);
|
||||
drawList.AddRectFilled(
|
||||
cursor + size1 with { Y = 0 },
|
||||
cursor + size2,
|
||||
ImGui.GetColorU32(ImGuiCol.TextSelectedBg));
|
||||
}
|
||||
|
||||
// Add the text being converted.
|
||||
drawList.AddText(cursor, ImGui.GetColorU32(ImGuiCol.Text), ime.ImmComp);
|
||||
|
||||
// Draw the caret inside the composition string.
|
||||
if (DalamudIme.ShowCursorInInputText)
|
||||
{
|
||||
var partBeforeCaret = ime.ImmComp[..ime.CompositionCursorOffset];
|
||||
var sizeBeforeCaret = ImGui.CalcTextSize(partBeforeCaret);
|
||||
drawList.AddLine(
|
||||
cursor + sizeBeforeCaret with { Y = 0 },
|
||||
cursor + sizeBeforeCaret,
|
||||
ImGui.GetColorU32(ImGuiCol.Text));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,120 +0,0 @@
|
|||
using System.Numerics;
|
||||
|
||||
using Dalamud.Game.ClientState.Keys;
|
||||
using Dalamud.Game.Gui.Internal;
|
||||
using Dalamud.Interface.Windowing;
|
||||
using ImGuiNET;
|
||||
|
||||
namespace Dalamud.Interface.Internal.Windows;
|
||||
|
||||
/// <summary>
|
||||
/// A window for displaying IME details.
|
||||
/// </summary>
|
||||
internal unsafe class ImeWindow : Window
|
||||
{
|
||||
private const int ImePageSize = 9;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ImeWindow"/> class.
|
||||
/// </summary>
|
||||
public ImeWindow()
|
||||
: base("Dalamud IME", ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoBackground)
|
||||
{
|
||||
this.Size = new Vector2(100, 200);
|
||||
this.SizeCondition = ImGuiCond.FirstUseEver;
|
||||
|
||||
this.RespectCloseHotkey = false;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Draw()
|
||||
{
|
||||
if (this.IsOpen && Service<KeyState>.Get()[VirtualKey.SHIFT]) Service<DalamudInterface>.Get().CloseImeWindow();
|
||||
var ime = Service<DalamudIME>.GetNullable();
|
||||
|
||||
if (ime == null || !ime.IsEnabled)
|
||||
{
|
||||
ImGui.Text("IME is unavailable.");
|
||||
return;
|
||||
}
|
||||
|
||||
// ImGui.Text($"{ime.GetCursorPos()}");
|
||||
// ImGui.Text($"{ImGui.GetWindowViewport().WorkSize}");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void PostDraw()
|
||||
{
|
||||
if (this.IsOpen && Service<KeyState>.Get()[VirtualKey.SHIFT]) Service<DalamudInterface>.Get().CloseImeWindow();
|
||||
var ime = Service<DalamudIME>.GetNullable();
|
||||
|
||||
if (ime == null || !ime.IsEnabled)
|
||||
return;
|
||||
|
||||
var maxTextWidth = 0f;
|
||||
var textHeight = ImGui.CalcTextSize(ime.ImmComp).Y;
|
||||
|
||||
var native = ime.ImmCandNative;
|
||||
var totalIndex = native.Selection + 1;
|
||||
var totalSize = native.Count;
|
||||
|
||||
var pageStart = native.PageStart;
|
||||
var pageIndex = (pageStart / ImePageSize) + 1;
|
||||
var pageCount = (totalSize / ImePageSize) + 1;
|
||||
var pageInfo = $"{totalIndex}/{totalSize} ({pageIndex}/{pageCount})";
|
||||
|
||||
// Calc the window size
|
||||
for (var i = 0; i < ime.ImmCand.Count; i++)
|
||||
{
|
||||
var textSize = ImGui.CalcTextSize($"{i + 1}. {ime.ImmCand[i]}");
|
||||
maxTextWidth = maxTextWidth > textSize.X ? maxTextWidth : textSize.X;
|
||||
}
|
||||
|
||||
maxTextWidth = maxTextWidth > ImGui.CalcTextSize(pageInfo).X ? maxTextWidth : ImGui.CalcTextSize(pageInfo).X;
|
||||
maxTextWidth = maxTextWidth > ImGui.CalcTextSize(ime.ImmComp).X ? maxTextWidth : ImGui.CalcTextSize(ime.ImmComp).X;
|
||||
|
||||
var imeWindowWidth = maxTextWidth + (2 * ImGui.GetStyle().WindowPadding.X);
|
||||
var imeWindowHeight = (textHeight * (ime.ImmCand.Count + 2)) + (5 * (ime.ImmCand.Count - 1)) + (2 * ImGui.GetStyle().WindowPadding.Y);
|
||||
|
||||
// Calc the window pos
|
||||
var cursorPos = ime.GetCursorPos();
|
||||
var imeWindowMinPos = new Vector2(cursorPos.X, cursorPos.Y);
|
||||
var imeWindowMaxPos = new Vector2(imeWindowMinPos.X + imeWindowWidth, imeWindowMinPos.Y + imeWindowHeight);
|
||||
var gameWindowSize = ImGui.GetWindowViewport().WorkSize;
|
||||
|
||||
var offset = new Vector2(
|
||||
imeWindowMaxPos.X - gameWindowSize.X > 0 ? imeWindowMaxPos.X - gameWindowSize.X : 0,
|
||||
imeWindowMaxPos.Y - gameWindowSize.Y > 0 ? imeWindowMaxPos.Y - gameWindowSize.Y : 0);
|
||||
imeWindowMinPos -= offset;
|
||||
imeWindowMaxPos -= offset;
|
||||
|
||||
var nextDrawPosY = imeWindowMinPos.Y;
|
||||
var drawAreaPosX = imeWindowMinPos.X + ImGui.GetStyle().WindowPadding.X;
|
||||
|
||||
// Draw the ime window
|
||||
var drawList = ImGui.GetForegroundDrawList();
|
||||
// Draw the background rect
|
||||
drawList.AddRectFilled(imeWindowMinPos, imeWindowMaxPos, ImGui.GetColorU32(ImGuiCol.WindowBg), ImGui.GetStyle().WindowRounding);
|
||||
// Add component text
|
||||
drawList.AddText(new Vector2(drawAreaPosX, nextDrawPosY), ImGui.GetColorU32(ImGuiCol.Text), ime.ImmComp);
|
||||
nextDrawPosY += textHeight + ImGui.GetStyle().ItemSpacing.Y;
|
||||
// Add separator
|
||||
drawList.AddLine(new Vector2(drawAreaPosX, nextDrawPosY), new Vector2(drawAreaPosX + maxTextWidth, nextDrawPosY), ImGui.GetColorU32(ImGuiCol.Separator));
|
||||
// Add candidate words
|
||||
for (var i = 0; i < ime.ImmCand.Count; i++)
|
||||
{
|
||||
var selected = i == (native.Selection % ImePageSize);
|
||||
var color = ImGui.GetColorU32(ImGuiCol.Text);
|
||||
if (selected)
|
||||
color = ImGui.GetColorU32(ImGuiCol.NavHighlight);
|
||||
|
||||
drawList.AddText(new Vector2(drawAreaPosX, nextDrawPosY), color, $"{i + 1}. {ime.ImmCand[i]}");
|
||||
nextDrawPosY += textHeight + ImGui.GetStyle().ItemSpacing.Y;
|
||||
}
|
||||
|
||||
// Add separator
|
||||
drawList.AddLine(new Vector2(drawAreaPosX, nextDrawPosY), new Vector2(drawAreaPosX + maxTextWidth, nextDrawPosY), ImGui.GetColorU32(ImGuiCol.Separator));
|
||||
// Add pages infomation
|
||||
drawList.AddText(new Vector2(drawAreaPosX, nextDrawPosY), ImGui.GetColorU32(ImGuiCol.Text), pageInfo);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue