From b0560592c881dab5c86e24052af1dc927416da94 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Tue, 25 Apr 2023 14:11:04 -0700 Subject: [PATCH] UI: Measure/Ruler Text Position Small improvements to the placement of text with Ruler tool. --- .../space_view3d/view3d_gizmo_ruler.cc | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc b/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc index e08c6989266..84b2089e69e 100644 --- a/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc +++ b/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc @@ -856,9 +856,17 @@ static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz) BLF_width_and_height(blf_mono_font, numstr, sizeof(numstr), &numstr_size[0], &numstr_size[1]); - posit[0] = co_ss[1][0] + (cap_size * 2.0f); + /* Center text. */ + posit[0] = co_ss[1][0] - (numstr_size[0] / 2.0f); posit[1] = co_ss[1][1] - (numstr_size[1] / 2.0f); + /* Adjust text position to help readability. */ + sub_v2_v2v2(dir_ruler, co_ss[0], co_ss[1]); + float rot_90_vec[2] = {-dir_ruler[1], dir_ruler[0]}; + normalize_v2(rot_90_vec); + posit[1] += rot_90_vec[0] * numstr_size[1]; + posit[0] += ((rot_90_vec[1] < 0)) ? numstr_size[0] : -numstr_size[0]; + /* draw text (bg) */ if (proj_ok[1]) { immUniformColor4fv(color_back); @@ -885,10 +893,10 @@ static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz) immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); dir_ruler = co_ss[0] - co_ss[2]; + float2 rot_90_vec = blender::math::normalize(float2{-dir_ruler[1], dir_ruler[0]}); /* capping */ { - float2 rot_90_vec = blender::math::normalize(float2{-dir_ruler[1], dir_ruler[0]}); float2 cap; GPU_blend(GPU_BLEND_ALPHA); @@ -933,6 +941,20 @@ static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz) /* center text */ posit -= numstr_size / 2.0f; + /* Adjust text position if this helps readability. */ + + const float len = len_v2v2(co_ss[0], co_ss[2]); + + if ((len < (numstr_size[1] * 2.5f)) || + ((len < (numstr_size[0] + bg_margin + bg_margin)) && (fabs(rot_90_vec[0]) < 0.5f))) { + /* Super short, or quite short and also shallow angle. Position below line.*/ + posit[1] = MIN2(co_ss[0][1], co_ss[2][1]) - numstr_size[1] - bg_margin - bg_margin; + } + else if (fabs(rot_90_vec[0]) < 0.2f) { + /* Very shallow angle. Shift down by text height.*/ + posit[1] -= numstr_size[1]; + } + /* draw text (bg) */ if (proj_ok[0] && proj_ok[2]) { immUniformColor4fv(color_back); -- 2.30.2