Animation: Allow setting the slider unit and mode #109768

Merged
Christoph Lendenfeld merged 3 commits from ChrisLend/blender:slider_factor_string into main 2023-07-07 15:08:46 +02:00
2 changed files with 49 additions and 13 deletions

View File

@ -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 ************* */
/**

View File

@ -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;
@ -79,6 +80,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. */
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
* extrapolation. */
@ -328,8 +335,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 +344,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 +414,10 @@ tSlider *ED_slider_create(bContext *C)
slider->factor_bounds[0] = 0;
slider->factor_bounds[1] = 1;
slider->unit_string[0] = "%";
slider->slider_mode = SLIDER_MODE_PERCENT;
/* Set initial factor. */
slider->raw_factor = 0.5f;
slider->factor = 0.5;
@ -554,6 +576,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)
{
BLI_strncpy(slider->unit_string, unit, SLIDER_UNIT_STRING_SIZE);
}
/** \} */
void ED_region_draw_mouse_line_cb(const bContext *C, ARegion *region, void *arg_info)