diff options
-rw-r--r-- | source/blender/blenkernel/BKE_unit.h | 3 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/unit.c | 16 | ||||
-rw-r--r-- | source/blender/editors/interface/interface.c | 38 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_intern.h | 1 |
4 files changed, 55 insertions, 3 deletions
diff --git a/source/blender/blenkernel/BKE_unit.h b/source/blender/blenkernel/BKE_unit.h index 6b72c01bf0a..91b1702ca82 100644 --- a/source/blender/blenkernel/BKE_unit.h +++ b/source/blender/blenkernel/BKE_unit.h @@ -36,6 +36,9 @@ void bUnit_AsString(char *str, double value, int prec, int system, int type, int /* replace units with values, used before python button evaluation */ int bUnit_ReplaceString(char *str, char *str_orig, char *str_prev, double scale_pref, int system, int type); +/* the size of the unit used for this value (used for calculating the ckickstep) */ +double bUnit_Size(double value, int system, int type); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenkernel/intern/unit.c b/source/blender/blenkernel/intern/unit.c index eaa90a87691..5ecc8ddf5b8 100644 --- a/source/blender/blenkernel/intern/unit.c +++ b/source/blender/blenkernel/intern/unit.c @@ -347,3 +347,19 @@ int bUnit_ReplaceString(char *str, char *str_orig, char *str_prev, double scale_ // printf("replace %s\n", str); return change; } + + +double bUnit_Size(double value, int system, int type) +{ + bUnitCollection *usys = unit_get_system(system, type); + bUnitDef *unit; + + if(usys==NULL) + return -1; + + unit= unit_best_fit(value, usys, NULL); + if(unit==NULL) + return -1; + + return unit->mul; +} diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 878f1bef5cf..a3ae08bbe09 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -1142,6 +1142,20 @@ int ui_is_but_float(uiBut *but) return 0; } +int ui_is_but_unit(uiBut *but) +{ + if(U.unit_system == USER_UNIT_NONE) + return 0; + + if(but->rnaprop==NULL) + return 0; + + if(RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop))==0) + return 0; + + return 1; +} + double ui_get_but_val(uiBut *but) { PropertyRNA *prop; @@ -1337,6 +1351,21 @@ static void ui_get_but_string_unit(uiBut *but, char *str, double value, int pad) bUnit_AsString(str, ui_get_but_scale_unit(but, value), precission, U.unit_system, unit_type, do_split, pad); } +static float ui_get_but_step_unit(uiBut *but, double value, float step_default) +{ + int unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop)); + float step; + + step = bUnit_Size(ui_get_but_scale_unit(but, value), U.unit_system, unit_type); + + if(step > 0.0) { /* -1 is an error value */ + return (step/ui_get_but_scale_unit(but, 1.0))*100; + } + else { + return step_default; + } +} + void ui_get_but_string(uiBut *but, char *str, int maxlen) { @@ -1393,8 +1422,7 @@ void ui_get_but_string(uiBut *but, char *str, int maxlen) value= ui_get_but_val(but); if(ui_is_but_float(but)) { - - if(U.unit_system != USER_UNIT_NONE && but->rnaprop && RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop))) { + if(ui_is_but_unit(but)) { ui_get_but_string_unit(but, str, value, 0); } else if(but->a2) { /* amount of digits defined */ @@ -1835,7 +1863,7 @@ void ui_check_but(uiBut *but) if(value == FLT_MAX) sprintf(but->drawstr, "%sinf", but->str); else if(value == -FLT_MAX) sprintf(but->drawstr, "%s-inf", but->str); /* support length type buttons */ - else if(U.unit_system != USER_UNIT_NONE && but->rnaprop && RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop))) { + else if(ui_is_but_unit(but)) { char new_str[256]; if(U.unit_scale_length<0.0001) U.unit_scale_length= 1.0; // XXX do_versions @@ -2382,6 +2410,10 @@ uiBut *ui_def_but_rna(uiBlock *block, int type, int retval, char *str, short x1, but->lockstr = ""; } + /* If this button uses units, calculate the step from this */ + if(ui_is_but_unit(but)) + but->a1= ui_get_but_step_unit(but, ui_get_but_val(but), but->a1); + if(freestr) MEM_freeN(str); diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h index 04c0c417fdb..fbf95a4cde8 100644 --- a/source/blender/editors/interface/interface_intern.h +++ b/source/blender/editors/interface/interface_intern.h @@ -337,6 +337,7 @@ extern void ui_set_but_soft_range(uiBut *but, double value); extern void ui_check_but(uiBut *but); extern int ui_is_but_float(uiBut *but); +extern int ui_is_but_unit(uiBut *but); extern void ui_update_block_buts_hsv(uiBlock *block, float *hsv); extern void ui_bounds_block(uiBlock *block); |