Fix T73784: Python console: incorrect wrapped line cursor position

Regression in aa919f3e82

Remove character margins, it complicated drawing & picking
to have one margin in pixels and a second margin in characters.

Replace this with an outer pixel-margin for drawing background colors.
This commit is contained in:
Campbell Barton
2020-02-14 13:38:47 +11:00
parent 75a5ea01c1
commit 69be8039e8
4 changed files with 47 additions and 47 deletions

View File

@@ -161,7 +161,7 @@ static void console_textview_draw_cursor(struct TextViewContext *tvc)
console_cursor_wrap_offset(sc->prompt, tvc->columns, &offl, &offc, NULL);
console_cursor_wrap_offset(cl->line, tvc->columns, &offl, &offc, cl->line + cl->cursor);
pen[0] = tvc->cwidth * (offc + tvc->margin_left_chars);
pen[0] = tvc->cwidth * offc;
pen[1] = -2 - tvc->lheight * offl;
console_cursor_wrap_offset(cl->line + cl->cursor, tvc->columns, &offl, &offc, NULL);
@@ -185,12 +185,21 @@ static void console_textview_const_colors(TextViewContext *UNUSED(tvc), unsigned
UI_GetThemeColor4ubv(TH_CONSOLE_SELECT, bg_sel);
}
static void console_textview_draw_rect_calc(const ARegion *ar, rcti *draw_rect)
static void console_textview_draw_rect_calc(const ARegion *ar,
rcti *r_draw_rect,
rcti *r_draw_rect_outer)
{
draw_rect->xmin = 0;
draw_rect->xmax = ar->winx;
draw_rect->ymin = 0;
draw_rect->ymax = ar->winy;
const int margin = 4 * UI_DPI_FAC;
r_draw_rect->xmin = margin;
r_draw_rect->xmax = ar->winx - V2D_SCROLL_WIDTH;
r_draw_rect->ymin = margin;
/* No margin at the top (allow text to scroll off the window). */
r_draw_rect->ymax = ar->winy;
r_draw_rect_outer->xmin = 0;
r_draw_rect_outer->xmax = ar->winx;
r_draw_rect_outer->ymin = 0;
r_draw_rect_outer->ymax = ar->winy;
}
static int console_textview_main__internal(struct SpaceConsole *sc,
@@ -223,12 +232,10 @@ static int console_textview_main__internal(struct SpaceConsole *sc,
tvc.sel_start = sc->sel_start;
tvc.sel_end = sc->sel_end;
tvc.lheight = sc->lheight * 1.2f * UI_DPI_FAC;
tvc.margin_left_chars = 1;
tvc.margin_right_chars = 2;
tvc.scroll_ymin = v2d->cur.ymin;
tvc.scroll_ymax = v2d->cur.ymax;
console_textview_draw_rect_calc(ar, &tvc.draw_rect);
console_textview_draw_rect_calc(ar, &tvc.draw_rect, &tvc.draw_rect_outer);
console_scrollback_prompt_begin(sc, &cl_dummy);
ret = textview_draw(&tvc, do_draw, mval, r_mval_pick_item, r_mval_pick_offset);
@@ -254,9 +261,6 @@ int console_char_pick(struct SpaceConsole *sc, const ARegion *ar, const int mval
int r_mval_pick_offset = 0;
void *mval_pick_item = NULL;
rcti draw_rect;
console_textview_draw_rect_calc(ar, &draw_rect);
console_textview_main__internal(sc, ar, false, mval, &mval_pick_item, &r_mval_pick_offset);
return r_mval_pick_offset;
}

View File

@@ -206,12 +206,21 @@ static int report_textview_line_get(struct TextViewContext *tvc, const char **li
return 1;
}
static void info_textview_draw_rect_calc(const ARegion *ar, rcti *draw_rect)
static void info_textview_draw_rect_calc(const ARegion *ar,
rcti *r_draw_rect,
rcti *r_draw_rect_outer)
{
draw_rect->xmin = 0;
draw_rect->xmax = ar->winx;
draw_rect->ymin = 0;
draw_rect->ymax = ar->winy;
const int margin = 0.45f * U.widget_unit;
r_draw_rect->xmin = margin + UI_UNIT_X;
r_draw_rect->xmax = ar->winx - V2D_SCROLL_WIDTH;
r_draw_rect->ymin = margin;
r_draw_rect->ymax = ar->winy;
/* No margin at the top (allow text to scroll off the window). */
r_draw_rect_outer->xmin = 0;
r_draw_rect_outer->xmax = ar->winx;
r_draw_rect_outer->ymin = 0;
r_draw_rect_outer->ymax = ar->winy;
}
static int info_textview_main__internal(struct SpaceInfo *sinfo,
@@ -243,12 +252,10 @@ static int info_textview_main__internal(struct SpaceInfo *sinfo,
tvc.sel_end = 0;
tvc.lheight = 17 * UI_DPI_FAC;
tvc.row_vpadding = 0.4 * tvc.lheight;
tvc.margin_left_chars = 5;
tvc.margin_right_chars = 2;
tvc.scroll_ymin = v2d->cur.ymin;
tvc.scroll_ymax = v2d->cur.ymax;
info_textview_draw_rect_calc(ar, &tvc.draw_rect);
info_textview_draw_rect_calc(ar, &tvc.draw_rect, &tvc.draw_rect_outer);
ret = textview_draw(&tvc, do_draw, mval, r_mval_pick_item, r_mval_pick_offset);

View File

@@ -53,12 +53,13 @@ typedef struct TextViewDrawState {
int lheight;
/** Text vertical offset per line. */
int lofs;
int margin_left_chars;
int margin_right_chars;
int row_vpadding;
/** Number of characters that fit into the width of the console (fixed width). */
int columns;
/** For drawing text. */
const rcti *draw_rect;
/** For drawing backgrounds colors which may extend beyond text. */
const rcti *draw_rect_outer;
int scroll_ymin, scroll_ymax;
int *xy; // [2]
int *sel; // [2]
@@ -85,9 +86,8 @@ static void console_draw_sel(const char *str,
const int lheight = tds->lheight;
if (sel[0] <= str_len_draw && sel[1] >= 0) {
const int sta = BLI_str_utf8_offset_to_column(str, max_ii(sel[0], 0)) + tds->margin_left_chars;
const int end = BLI_str_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw)) +
tds->margin_left_chars;
const int sta = BLI_str_utf8_offset_to_column(str, max_ii(sel[0], 0));
const int end = BLI_str_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));
GPU_blend(true);
GPU_blend_set_func_separate(
@@ -154,11 +154,7 @@ static bool console_draw_string(TextViewDrawState *tds,
int tot_lines; /* Total number of lines for wrapping. */
int *offsets; /* Offsets of line beginnings for wrapping. */
str_len = console_wrap_offsets(str,
str_len,
tds->columns - (tds->margin_left_chars + tds->margin_right_chars),
&tot_lines,
&offsets);
str_len = console_wrap_offsets(str, str_len, tds->columns, &tot_lines, &offsets);
int line_height = (tot_lines * tds->lheight) + (tds->row_vpadding * 2);
int line_bottom = tds->xy[1];
@@ -223,15 +219,15 @@ static bool console_draw_string(TextViewDrawState *tds,
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
immUniformColor4ubv(bg);
immRecti(pos, 0, line_bottom, tds->draw_rect->xmax, line_top);
immRecti(pos, tds->draw_rect_outer->xmin, line_bottom, tds->draw_rect_outer->xmax, line_top);
immUnbindProgram();
}
if (icon_bg) {
float col[4];
int bg_size = 20 * UI_DPI_FAC;
int bg_size = UI_DPI_ICON_SIZE * 1.2;
float vpadding = (tds->lheight + (tds->row_vpadding * 2) - bg_size) / 2;
float hpadding = ((tds->margin_left_chars * tds->cwidth) - bg_size) / 2;
float hpadding = tds->draw_rect->xmin - (bg_size * 1.2f);
rgba_uchar_to_float(col, icon_bg);
UI_draw_roundbox_corner_set(UI_CNR_ALL);
@@ -246,7 +242,7 @@ static bool console_draw_string(TextViewDrawState *tds,
if (icon) {
int vpadding = (tds->lheight + (tds->row_vpadding * 2) - UI_DPI_ICON_SIZE) / 2;
int hpadding = ((tds->margin_left_chars * tds->cwidth) - UI_DPI_ICON_SIZE) / 2;
int hpadding = tds->draw_rect->xmin - (UI_DPI_ICON_SIZE * 1.3f);
GPU_blend(true);
UI_icon_draw_ex(hpadding,
@@ -266,10 +262,7 @@ static bool console_draw_string(TextViewDrawState *tds,
const int final_offset = offsets[tot_lines - 1];
len = str_len - final_offset;
s = str + final_offset;
BLF_position(tds->font_id,
tds->xy[0] + (tds->margin_left_chars * tds->cwidth),
tds->lofs + line_bottom + tds->row_vpadding,
0);
BLF_position(tds->font_id, tds->xy[0], tds->lofs + line_bottom + tds->row_vpadding, 0);
BLF_color4ubv(tds->font_id, fg);
BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
@@ -287,10 +280,7 @@ static bool console_draw_string(TextViewDrawState *tds,
len = offsets[i] - offsets[i - 1];
s = str + offsets[i - 1];
BLF_position(tds->font_id,
tds->xy[0] + (tds->margin_left_chars * tds->cwidth),
tds->lofs + tds->xy[1],
0);
BLF_position(tds->font_id, tds->xy[0], tds->lofs + tds->xy[1], 0);
BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
if (tds->sel[0] != tds->sel[1]) {
@@ -364,8 +354,6 @@ int textview_draw(TextViewContext *tvc,
tds.cwidth = (int)BLF_fixed_width(font_id);
BLI_assert(tds.cwidth > 0);
tds.lheight = tvc->lheight;
tds.margin_left_chars = tvc->margin_left_chars;
tds.margin_right_chars = tvc->margin_right_chars;
tds.row_vpadding = tvc->row_vpadding;
tds.lofs = -BLF_descender(font_id);
/* Note, scroll bar must be already subtracted. */
@@ -375,6 +363,7 @@ int textview_draw(TextViewContext *tvc,
tds.columns = 1;
}
tds.draw_rect = &tvc->draw_rect;
tds.draw_rect_outer = &tvc->draw_rect_outer;
tds.scroll_ymin = tvc->scroll_ymin;
tds.scroll_ymax = tvc->scroll_ymax;
tds.xy = xy;

View File

@@ -32,11 +32,11 @@ typedef struct TextViewContext {
int columns; /* shouldnt be needed! */
int row_vpadding;
int margin_left_chars;
int margin_right_chars;
/** Area to draw: (0, 0, winx, winy) with a margin applied and scroll-bar subtracted. */
/** Area to draw text: (0, 0, winx, winy) with a margin applied and scroll-bar subtracted. */
rcti draw_rect;
/** Area to draw text background colors (extending beyond text in some cases). */
rcti draw_rect_outer;
/** Scroll offset in pixels. */
int scroll_ymin, scroll_ymax;