diff --git a/source/blender/blenkernel/BKE_unit.hh b/source/blender/blenkernel/BKE_unit.hh index ea54f44a523..06b04ef4edd 100644 --- a/source/blender/blenkernel/BKE_unit.hh +++ b/source/blender/blenkernel/BKE_unit.hh @@ -37,6 +37,17 @@ size_t BKE_unit_value_as_string(char *str, const UnitSettings *settings, bool pad); +/** + * A version of #BKE_unit_value_as_string with the `value` scaled by #BKE_unit_value_scale. + */ +size_t BKE_unit_value_as_string_scaled(char *str, + int str_maxncpy, + double value, + int prec, + int type, + const UnitSettings *settings, + bool pad); + /** * Replace units with values, used before python button evaluation. * diff --git a/source/blender/blenkernel/intern/unit.cc b/source/blender/blenkernel/intern/unit.cc index 3adc826b9af..29efe9e54a4 100644 --- a/source/blender/blenkernel/intern/unit.cc +++ b/source/blender/blenkernel/intern/unit.cc @@ -1886,6 +1886,18 @@ size_t BKE_unit_value_as_string(char *str, return unit_as_string_main(str, str_maxncpy, value, prec, type, do_split, pad, units); } +size_t BKE_unit_value_as_string_scaled(char *str, + int str_maxncpy, + double value, + int prec, + int type, + const UnitSettings *settings, + bool pad) +{ + return BKE_unit_value_as_string( + str, str_maxncpy, BKE_unit_value_scale(settings, type, value), prec, type, settings, pad); +} + double BKE_unit_value_scale(const UnitSettings *unit, const int unit_type, double value) { if (unit->system == USER_UNIT_NONE) { diff --git a/source/blender/draw/intern/draw_manager_text.cc b/source/blender/draw/intern/draw_manager_text.cc index e211fb9e783..589c7c319d6 100644 --- a/source/blender/draw/intern/draw_manager_text.cc +++ b/source/blender/draw/intern/draw_manager_text.cc @@ -362,16 +362,11 @@ void DRW_text_edit_mesh_measure_stats(const ARegion *region, v2 = ob->object_to_world().view<3, 3>() * v2; } - const size_t numstr_len = unit->system ? - BKE_unit_value_as_string(numstr, - sizeof(numstr), - len_v3v3(v1, v2) * - unit->scale_length, - 3, - B_UNIT_LENGTH, - unit, - false) : - SNPRINTF_RLEN(numstr, conv_float, len_v3v3(v1, v2)); + const size_t numstr_len = + unit->system ? + BKE_unit_value_as_string_scaled( + numstr, sizeof(numstr), len_v3v3(v1, v2), 3, B_UNIT_LENGTH, unit, false) : + SNPRINTF_RLEN(numstr, conv_float, len_v3v3(v1, v2)); DRW_text_cache_add(dt, co, numstr, numstr_len, 0, edge_tex_sep, txt_flag, col); } @@ -502,16 +497,10 @@ void DRW_text_edit_mesh_measure_stats(const ARegion *region, vmid *= 1.0f / float(n); vmid = blender::math::transform_point(ob->object_to_world(), vmid); - const size_t numstr_len = unit->system ? - BKE_unit_value_as_string( - numstr, - sizeof(numstr), - double(area * unit->scale_length * unit->scale_length), - 3, - B_UNIT_AREA, - unit, - false) : - SNPRINTF_RLEN(numstr, conv_float, area); + const size_t numstr_len = + unit->system ? BKE_unit_value_as_string_scaled( + numstr, sizeof(numstr), area, 3, B_UNIT_AREA, unit, false) : + SNPRINTF_RLEN(numstr, conv_float, area); DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0, 0, txt_flag, col); } diff --git a/source/blender/editors/mesh/editmesh_bevel.cc b/source/blender/editors/mesh/editmesh_bevel.cc index ae0f6dc270d..8b6b09c6f78 100644 --- a/source/blender/editors/mesh/editmesh_bevel.cc +++ b/source/blender/editors/mesh/editmesh_bevel.cc @@ -133,13 +133,8 @@ static void edbm_bevel_update_status_text(bContext *C, wmOperator *op) } else { double offset_val = double(RNA_float_get(op->ptr, "offset")); - BKE_unit_value_as_string(offset_str, - NUM_STR_REP_LEN, - offset_val * sce->unit.scale_length, - 3, - B_UNIT_LENGTH, - &sce->unit, - true); + BKE_unit_value_as_string_scaled( + offset_str, NUM_STR_REP_LEN, offset_val, 3, B_UNIT_LENGTH, &sce->unit, true); } PropertyRNA *prop; diff --git a/source/blender/editors/mesh/editmesh_knife.cc b/source/blender/editors/mesh/editmesh_knife.cc index c567109a608..df90e649434 100644 --- a/source/blender/editors/mesh/editmesh_knife.cc +++ b/source/blender/editors/mesh/editmesh_knife.cc @@ -514,13 +514,8 @@ static void knifetool_draw_visible_distances(const KnifeTool_OpData *kcd) SNPRINTF(numstr, "%.*f", distance_precision, cut_len); } else { - BKE_unit_value_as_string(numstr, - sizeof(numstr), - double(cut_len * unit->scale_length), - distance_precision, - B_UNIT_LENGTH, - unit, - false); + BKE_unit_value_as_string_scaled( + numstr, sizeof(numstr), cut_len, distance_precision, B_UNIT_LENGTH, unit, false); } BLF_enable(blf_mono_font, BLF_ROTATION); diff --git a/source/blender/editors/object/object_remesh.cc b/source/blender/editors/object/object_remesh.cc index f83cf5f59f5..33a8da88448 100644 --- a/source/blender/editors/object/object_remesh.cc +++ b/source/blender/editors/object/object_remesh.cc @@ -320,13 +320,8 @@ static void voxel_size_edit_draw(const bContext *C, ARegion * /*region*/, void * short strdrawlen = 0; Scene *scene = CTX_data_scene(C); const UnitSettings *unit = &scene->unit; - BKE_unit_value_as_string(str, - VOXEL_SIZE_EDIT_MAX_STR_LEN, - double(cd->voxel_size * unit->scale_length), - -3, - B_UNIT_LENGTH, - unit, - true); + + BKE_unit_value_as_string_scaled(str, sizeof(str), cd->voxel_size, -3, B_UNIT_LENGTH, unit, true); strdrawlen = BLI_strlen_utf8(str); immUnbindProgram(); diff --git a/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc b/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc index b40f6df6f0f..463fb2a1885 100644 --- a/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc +++ b/source/blender/editors/space_view3d/view3d_gizmo_ruler.cc @@ -215,13 +215,7 @@ static void ruler_item_as_string( BLI_snprintf(numstr, numstr_size, "%.*f", prec, ruler_len); } else { - BKE_unit_value_as_string(numstr, - numstr_size, - double(ruler_len * unit->scale_length), - prec, - B_UNIT_LENGTH, - unit, - false); + BKE_unit_value_as_string(numstr, numstr_size, ruler_len, prec, B_UNIT_LENGTH, unit, false); } } } diff --git a/source/blender/editors/transform/transform_mode_shrink_fatten.cc b/source/blender/editors/transform/transform_mode_shrink_fatten.cc index bfc5d326fc4..1afd4e12520 100644 --- a/source/blender/editors/transform/transform_mode_shrink_fatten.cc +++ b/source/blender/editors/transform/transform_mode_shrink_fatten.cc @@ -114,10 +114,10 @@ static void applyShrinkFatten(TransInfo *t) } else { /* Default header print. */ - if (unit != nullptr) { + if (unit->system != USER_UNIT_NONE) { char unit_str[64]; - BKE_unit_value_as_string( - unit_str, sizeof(unit_str), distance * unit->scale_length, 4, B_UNIT_LENGTH, unit, true); + BKE_unit_value_as_string_scaled( + unit_str, sizeof(unit_str), distance, 4, B_UNIT_LENGTH, unit, true); fmt::format_to(fmt::appender(str), "{}", unit_str); } else { diff --git a/source/blender/editors/transform/transform_mode_translate.cc b/source/blender/editors/transform/transform_mode_translate.cc index d7829a8b20f..8218f9fc801 100644 --- a/source/blender/editors/transform/transform_mode_translate.cc +++ b/source/blender/editors/transform/transform_mode_translate.cc @@ -187,9 +187,8 @@ static void translate_dist_to_str(char *r_str, const float val, const UnitSettings *unit) { - if (unit) { - BKE_unit_value_as_string( - r_str, r_str_maxncpy, val * unit->scale_length, 4, B_UNIT_LENGTH, unit, false); + if (unit && (unit->system != USER_UNIT_NONE)) { + BKE_unit_value_as_string_scaled(r_str, r_str_maxncpy, val, 4, B_UNIT_LENGTH, unit, false); } else { /* Check range to prevent string buffer overflow. */ diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h index e34e13bf1ba..edc2ef506c3 100644 --- a/source/blender/makesdna/DNA_scene_types.h +++ b/source/blender/makesdna/DNA_scene_types.h @@ -1801,7 +1801,13 @@ typedef struct ToolSettings { /** Display/Editing unit options for each scene. */ typedef struct UnitSettings { - /** Maybe have other unit conversions? */ + /* Maybe have other unit conversions? */ + /** + * Spatial scale. + * - This must not be used when `system == USER_UNIT_NONE`. + * - Typically the scale should be applied using #BKE_unit_value_scale + * which supports different kinds of users and checks a none unit system. + */ float scale_length; /** Imperial, metric etc. */ char system;