Fix #113682: Improved Text Caret Position #113707

Merged
Harley Acheson merged 3 commits from Harley/blender:Fix113682 into blender-v4.0-release 2023-10-14 20:58:59 +02:00
1 changed files with 46 additions and 6 deletions

View File

@ -2032,7 +2032,7 @@ static void widget_draw_text(const uiFontStyle *fstyle,
}
}
/* text cursor */
/* Text cursor position. */
but_pos_ofs = but->pos;
#ifdef WITH_INPUT_IME
@ -2042,14 +2042,54 @@ static void widget_draw_text(const uiFontStyle *fstyle,
}
#endif
/* Draw text cursor (caret). */
if (but->pos >= but->ofs) {
int t;
int t = 0;
if (drawstr[0] != 0) {
t = BLF_width(fstyle->uifont_id, drawstr + but->ofs, but_pos_ofs - but->ofs);
}
else {
t = 0;
const int pos = but_pos_ofs - but->ofs;
rcti bounds;
/* Find right edge of previous character if available. */
int prev_right_edge = 0;
bool has_prev = false;
if (pos > 0) {
if (BLF_str_offset_to_glyph_bounds(
fstyle->uifont_id, drawstr + but->ofs, pos - 1, &bounds) &&
!BLI_rcti_is_empty(&bounds))
{
prev_right_edge = bounds.xmax;
has_prev = true;
}
}
/* Find left edge of next character if available. */
int next_left_edge = 0;
bool has_next = false;
if (pos < strlen(drawstr)) {
if (BLF_str_offset_to_glyph_bounds(
fstyle->uifont_id, drawstr + but->ofs, pos, &bounds) &&
!BLI_rcti_is_empty(&bounds))
{
next_left_edge = bounds.xmin;
has_next = true;
}
}
if (has_next && !has_prev) {
/* Left of the first character. */
t = next_left_edge - U.pixelsize;
}
else if (has_prev && !has_next) {
/* Right of the last character. */
t = prev_right_edge + U.pixelsize;
}
else if (has_prev && has_next) {
/* Middle of the string, so in between. */
t = (prev_right_edge + next_left_edge) / 2;
}
}
/* We are drawing on top of widget bases. Flush cache. */
GPU_blend(GPU_BLEND_ALPHA);
UI_widgetbase_draw_cache_flush();