Skip to content

Commit 2e4900a

Browse files
CopperbaneArufonsu
andauthored
fix: text input caret (#2807)
Co-authored-by: Copperbane <71472964+Copperbane@users.noreply.github.com> Co-authored-by: Arufonsu <17498701+Arufonsu@users.noreply.github.com>
1 parent a73e5c7 commit 2e4900a

2 files changed

Lines changed: 39 additions & 16 deletions

File tree

Intersect.Client.Framework/Gwen/Control/Label.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -745,31 +745,31 @@ protected override void OnChildBoundsChanged(Base child, Rectangle oldChildBound
745745
{
746746
base.OnChildBoundsChanged(child, oldChildBounds, newChildBounds);
747747

748-
if (oldChildBounds.Size != newChildBounds.Size)
748+
if (oldChildBounds.Size == newChildBounds.Size)
749749
{
750-
if (_autoSizeToContents)
751-
{
752-
Invalidate();
753-
}
754-
else
750+
return;
751+
}
752+
753+
if (!_autoSizeToContents)
754+
{
755+
if (child is Text)
755756
{
756-
if (child is Text)
757+
var textSize = newChildBounds.Size;
758+
var ownSize = Size;
759+
if (textSize.X > ownSize.X || textSize.Y > ownSize.Y)
757760
{
758-
var textSize = newChildBounds.Size;
759-
var ownSize = Size;
760-
if (textSize.X > ownSize.X || textSize.Y > ownSize.Y)
761-
{
762-
OnTextExceedsSize(ownSize, textSize);
763-
}
761+
OnTextExceedsSize(ownSize, textSize);
764762
}
765-
766-
AlignTextElement(_textElement);
767763
}
768764
}
765+
766+
// Always invalidate when text bounds change
767+
Invalidate();
769768
}
770769

771770
protected virtual void OnTextExceedsSize(Point ownSize, Point textSize)
772771
{
772+
Invalidate();
773773
}
774774

775775
public virtual void SetTextScale(float scale)

Intersect.Client.Framework/Gwen/ControlInternal/Text.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,34 @@ public Point GetCharacterPosition(int index)
365365
}
366366

367367
var substring = displayedText[..index];
368-
var substringSize = Skin.Renderer.MeasureText(font, fontSize: _fontSize, substring) with
368+
var substringSize = Skin.Renderer.MeasureText(font, fontSize: _fontSize, substring, _scale) with
369369
{
370370
Y = 0,
371371
};
372372

373+
if (substringSize.X <= 0 && substring.Length > 0)
374+
{
375+
var referenceCharWidth = Skin.Renderer.MeasureText(font, fontSize: _fontSize, "n", _scale).X;
376+
377+
if (referenceCharWidth <= 0)
378+
{
379+
referenceCharWidth = (int)(_fontSize * 0.6f);
380+
}
381+
382+
var spaceWidth = (int)(referenceCharWidth * 0.4f);
383+
var accumulatedWidth = 0;
384+
385+
for (int i = 0; i < substring.Length; i++)
386+
{
387+
var charStr = substring[i].ToString();
388+
var charSize = Skin.Renderer.MeasureText(font, fontSize: _fontSize, charStr, _scale);
389+
390+
accumulatedWidth += charSize.X > 0 ? charSize.X : spaceWidth;
391+
}
392+
393+
return new Point(accumulatedWidth, 0);
394+
}
395+
373396
return substringSize;
374397
}
375398

0 commit comments

Comments
 (0)