From 41a213574a6aed80ffb6453ae4cff772174eb4b4 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 6 Jul 2023 10:29:58 +0200 Subject: [PATCH 1/3] allow setting the slider unit --- source/blender/editors/include/ED_util.h | 4 ++ source/blender/editors/util/ed_draw.c | 57 ++++++++++++++++++------ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/source/blender/editors/include/ED_util.h b/source/blender/editors/include/ED_util.h index bbeb102c5b2..f06dd6384cc 100644 --- a/source/blender/editors/include/ED_util.h +++ b/source/blender/editors/include/ED_util.h @@ -73,6 +73,7 @@ void ED_region_image_metadata_draw( /* Slider */ struct tSlider; +typedef enum SliderMode { SLIDER_MODE_PERCENT = 0, SLIDER_MODE_FLOAT = 1 } SliderMode; struct tSlider *ED_slider_create(struct bContext *C); /** @@ -106,6 +107,9 @@ void ED_slider_factor_bounds_set(struct tSlider *slider, float lower_bound, floa bool ED_slider_allow_increments_get(struct tSlider *slider); void ED_slider_allow_increments_set(struct tSlider *slider, bool value); +void ED_slider_mode_set(struct tSlider *slider, SliderMode unit); +void ED_slider_unit_set(struct tSlider *slider, const char *unit); + /* ************** XXX OLD CRUFT WARNING ************* */ /** diff --git a/source/blender/editors/util/ed_draw.c b/source/blender/editors/util/ed_draw.c index 985cc4d310d..3a0c7fb38d5 100644 --- a/source/blender/editors/util/ed_draw.c +++ b/source/blender/editors/util/ed_draw.c @@ -79,6 +79,12 @@ typedef struct tSlider { /** Range of the slider without overshoot. */ float factor_bounds[2]; + /* How the factor number is drawn. When drawing percent it is factor*100. */ + SliderMode slider_mode; + + /* What unit to add to the slider. */ + const char *unit_string; + /** Enable range beyond factor_bounds. * This is set by the code that uses the slider, as not all operations support * extrapolation. */ @@ -328,8 +334,6 @@ static void slider_draw(const bContext *UNUSED(C), ARegion *region, void *arg) } } - char percentage_string[256]; - /* Draw handle indicating current factor. */ const rctf handle_rect = { .xmin = handle_pos_x - (line_width), @@ -339,26 +343,39 @@ static void slider_draw(const bContext *UNUSED(C), ARegion *region, void *arg) }; UI_draw_roundbox_3ub_alpha(&handle_rect, true, 1, color_handle, 255); - SNPRINTF(percentage_string, "%.0f%%", slider->factor * 100); - /* Draw percentage string. */ - float percentage_string_pixel_size[2]; + char factor_string[256]; + switch (slider->slider_mode) { + case SLIDER_MODE_PERCENT: + SNPRINTF(factor_string, "%.0f %s", slider->factor * 100, slider->unit_string); + break; + case SLIDER_MODE_FLOAT: + SNPRINTF(factor_string, "%.1f %s", slider->factor, slider->unit_string); + break; + } + + /* Draw factor string. */ + float factor_string_pixel_size[2]; BLF_width_and_height(fontid, - percentage_string, - sizeof(percentage_string), - &percentage_string_pixel_size[0], - &percentage_string_pixel_size[1]); + factor_string, + sizeof(factor_string), + &factor_string_pixel_size[0], + &factor_string_pixel_size[1]); BLF_position(fontid, - main_line_rect.xmin - 24.0 * U.pixelsize - percentage_string_pixel_size[0] / 2, - (region->winy / 2) - percentage_string_pixel_size[1] / 2, + main_line_rect.xmin - 24.0 * U.pixelsize - factor_string_pixel_size[0] / 2, + (region->winy / 2) - factor_string_pixel_size[1] / 2, 0.0f); - BLF_draw(fontid, percentage_string, sizeof(percentage_string)); + BLF_draw(fontid, factor_string, sizeof(factor_string)); } static void slider_update_factor(tSlider *slider, const wmEvent *event) { - const float factor_delta = (event->xy[0] - slider->last_cursor[0]) / SLIDE_PIXEL_DISTANCE; + /* Normalize so no matter the factor bounds, the mouse distance travelled from min to max is + * constant. */ + const float slider_range = slider->factor_bounds[1] - slider->factor_bounds[0]; + const float factor_delta = (event->xy[0] - slider->last_cursor[0]) / + (SLIDE_PIXEL_DISTANCE / slider_range); /* Reduced factor delta in precision mode (shift held). */ slider->raw_factor += slider->precision ? (factor_delta / 8) : factor_delta; slider->factor = slider->raw_factor; @@ -396,6 +413,10 @@ tSlider *ED_slider_create(bContext *C) slider->factor_bounds[0] = 0; slider->factor_bounds[1] = 1; + slider->unit_string = "%"; + + slider->slider_mode = SLIDER_MODE_PERCENT; + /* Set initial factor. */ slider->raw_factor = 0.5f; slider->factor = 0.5; @@ -554,6 +575,16 @@ void ED_slider_factor_bounds_set(tSlider *slider, slider->factor_bounds[1] = factor_bound_upper; } +void ED_slider_mode_set(tSlider *slider, SliderMode mode) +{ + slider->slider_mode = mode; +} + +void ED_slider_unit_set(tSlider *slider, const char *unit) +{ + slider->unit_string = unit; +} + /** \} */ void ED_region_draw_mouse_line_cb(const bContext *C, ARegion *region, void *arg_info) -- 2.30.2 From d6ad2f98d4048a2a2160b6542463dcbec90663d9 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 6 Jul 2023 15:41:37 +0200 Subject: [PATCH 2/3] use strncopy for unit string --- source/blender/editors/util/ed_draw.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/blender/editors/util/ed_draw.c b/source/blender/editors/util/ed_draw.c index 3a0c7fb38d5..584d1971d58 100644 --- a/source/blender/editors/util/ed_draw.c +++ b/source/blender/editors/util/ed_draw.c @@ -83,7 +83,7 @@ typedef struct tSlider { SliderMode slider_mode; /* What unit to add to the slider. */ - const char *unit_string; + char unit_string[64]; /** Enable range beyond factor_bounds. * This is set by the code that uses the slider, as not all operations support @@ -413,7 +413,7 @@ tSlider *ED_slider_create(bContext *C) slider->factor_bounds[0] = 0; slider->factor_bounds[1] = 1; - slider->unit_string = "%"; + slider->unit_string[0] = "%"; slider->slider_mode = SLIDER_MODE_PERCENT; @@ -582,7 +582,7 @@ void ED_slider_mode_set(tSlider *slider, SliderMode mode) void ED_slider_unit_set(tSlider *slider, const char *unit) { - slider->unit_string = unit; + strcpy(slider->unit_string, unit); } /** \} */ -- 2.30.2 From 0dc427ca596704b976763b2a4c743791ed42ffc9 Mon Sep 17 00:00:00 2001 From: Christoph Lendenfeld Date: Thu, 6 Jul 2023 17:50:34 +0200 Subject: [PATCH 3/3] change to BLI_strncpy --- source/blender/editors/util/ed_draw.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/blender/editors/util/ed_draw.c b/source/blender/editors/util/ed_draw.c index 584d1971d58..7b453294a98 100644 --- a/source/blender/editors/util/ed_draw.c +++ b/source/blender/editors/util/ed_draw.c @@ -56,6 +56,7 @@ #define SLIDE_PIXEL_DISTANCE (300.0f * UI_SCALE_FAC) #define OVERSHOOT_RANGE_DELTA 0.2f +#define SLIDER_UNIT_STRING_SIZE 64 typedef struct tSlider { Scene *scene; @@ -83,7 +84,7 @@ typedef struct tSlider { SliderMode slider_mode; /* What unit to add to the slider. */ - char unit_string[64]; + char unit_string[SLIDER_UNIT_STRING_SIZE]; /** Enable range beyond factor_bounds. * This is set by the code that uses the slider, as not all operations support @@ -582,7 +583,7 @@ void ED_slider_mode_set(tSlider *slider, SliderMode mode) void ED_slider_unit_set(tSlider *slider, const char *unit) { - strcpy(slider->unit_string, unit); + BLI_strncpy(slider->unit_string, unit, SLIDER_UNIT_STRING_SIZE); } /** \} */ -- 2.30.2