Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <mail@jlucke.com>2018-10-03 11:20:16 +0300
committerJacques Lucke <mail@jlucke.com>2018-10-03 11:20:16 +0300
commit2d21eb79ad48485bc7b3385d6df5c2c25fd88ee0 (patch)
tree57d18e1020c8acc2d46110bd43c0721dc30b7d76 /source/blender/editors
parent1c3411ac899d1ae8dfd790249a53054698bdd1e8 (diff)
Units: Support for fixed units
Users can select the main unit they want to use now. Previously the displayed unit always depended on the magnitude of the value. The old behavior can be restored by switching to the "Adaptive" mode for length, mass and time units. Meters, kilograms and seconds are the default units for new and old scenes. The selected unit is also the default unit for user input. E.g. if cm is selected, whenever the user inputs a unitless number into a field of type length, it will be interpreted as cm. Reviewer: brecht Differential: https://developer.blender.org/D3740
Diffstat (limited to 'source/blender/editors')
-rw-r--r--source/blender/editors/include/ED_numinput.h2
-rw-r--r--source/blender/editors/interface/interface.c26
-rw-r--r--source/blender/editors/interface/interface_eyedropper_depth.c9
-rw-r--r--source/blender/editors/space_view3d/view3d_gizmo_ruler.c14
-rw-r--r--source/blender/editors/space_view3d/view3d_ruler.c13
-rw-r--r--source/blender/editors/transform/transform.c12
-rw-r--r--source/blender/editors/util/numinput.c54
7 files changed, 59 insertions, 71 deletions
diff --git a/source/blender/editors/include/ED_numinput.h b/source/blender/editors/include/ED_numinput.h
index 00558a3a787..f674a0d87f9 100644
--- a/source/blender/editors/include/ED_numinput.h
+++ b/source/blender/editors/include/ED_numinput.h
@@ -88,4 +88,6 @@ bool handleNumInput(struct bContext *C, NumInput *n, const struct wmEvent *event
#define NUM_MODAL_INCREMENT_UP 18
#define NUM_MODAL_INCREMENT_DOWN 19
+bool user_string_to_number(bContext *C, const char *str, const struct UnitSettings *unit, int type, double *r_value);
+
#endif /* __ED_NUMINPUT_H__ */
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c
index 312d41b796d..dcd09f0d4b0 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -80,6 +80,7 @@
#include "BPY_extern.h"
#include "ED_screen.h"
+#include "ED_numinput.h"
#include "IMB_colormanagement.h"
@@ -2177,7 +2178,6 @@ void ui_but_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen)
static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, bool pad, int float_precision)
{
UnitSettings *unit = but->block->unit;
- const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
int unit_type = UI_but_unit_type_get(but);
int precision;
@@ -2194,9 +2194,9 @@ static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double va
precision = float_precision;
}
- bUnit_AsString(
+ bUnit_AsString2(
str, len_max, ui_get_but_scale_unit(but, value), precision,
- unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type), do_split, pad);
+ RNA_SUBTYPE_UNIT_VALUE(unit_type), unit, pad);
}
static float ui_get_but_step_unit(uiBut *but, float step_default)
@@ -2406,27 +2406,13 @@ char *ui_but_string_get_dynamic(uiBut *but, int *r_str_size)
return str;
}
-#ifdef WITH_PYTHON
-
static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *r_value)
{
- char str_unit_convert[256];
- const int unit_type = UI_but_unit_type_get(but);
-
- BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
-
- /* ugly, use the draw string to get the value,
- * this could cause problems if it includes some text which resolves to a unit */
- bUnit_ReplaceString(
- str_unit_convert, sizeof(str_unit_convert), but->drawstr,
- ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
-
- return BPY_execute_string_as_number(C, NULL, str_unit_convert, true, r_value);
+ const UnitSettings *unit = but->block->unit;
+ int type = RNA_SUBTYPE_UNIT_VALUE(UI_but_unit_type_get(but));
+ return user_string_to_number(C, str, unit, type, r_value);
}
-#endif /* WITH_PYTHON */
-
-
bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *r_value)
{
bool ok = false;
diff --git a/source/blender/editors/interface/interface_eyedropper_depth.c b/source/blender/editors/interface/interface_eyedropper_depth.c
index fb125a3845b..1dae076f930 100644
--- a/source/blender/editors/interface/interface_eyedropper_depth.c
+++ b/source/blender/editors/interface/interface_eyedropper_depth.c
@@ -158,9 +158,6 @@ static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx,
ScrArea *sa = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, mx, my);
Scene *scene = CTX_data_scene(C);
- UnitSettings *unit = &scene->unit;
- const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
-
ScrArea *area_prev = CTX_wm_area(C);
ARegion *ar_prev = CTX_wm_region(C);
@@ -199,9 +196,9 @@ static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx,
*r_depth = len_v3v3(view_co, co_align);
- bUnit_AsString(ddr->name, sizeof(ddr->name),
- (double)*r_depth,
- 4, unit->system, B_UNIT_LENGTH, do_split, false);
+ bUnit_AsString2(
+ ddr->name, sizeof(ddr->name), (double)*r_depth,
+ 4, B_UNIT_LENGTH, &scene->unit, false);
}
else {
BLI_strncpy(ddr->name, "Nothing under cursor", sizeof(ddr->name));
diff --git a/source/blender/editors/space_view3d/view3d_gizmo_ruler.c b/source/blender/editors/space_view3d/view3d_gizmo_ruler.c
index 62f66334516..a3bd1b26a01 100644
--- a/source/blender/editors/space_view3d/view3d_gizmo_ruler.c
+++ b/source/blender/editors/space_view3d/view3d_gizmo_ruler.c
@@ -164,8 +164,6 @@ static void ruler_item_remove(bContext *C, wmGizmoGroup *gzgroup, RulerItem *rul
static void ruler_item_as_string(RulerItem *ruler_item, UnitSettings *unit,
char *numstr, size_t numstr_size, int prec)
{
- const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
-
if (ruler_item->flag & RULERITEM_USE_ANGLE) {
const float ruler_angle = angle_v3v3v3(ruler_item->co[0],
ruler_item->co[1],
@@ -175,9 +173,9 @@ static void ruler_item_as_string(RulerItem *ruler_item, UnitSettings *unit,
BLI_snprintf(numstr, numstr_size, "%.*f°", prec, RAD2DEGF(ruler_angle));
}
else {
- bUnit_AsString(numstr, numstr_size,
- (double)ruler_angle,
- prec, unit->system, B_UNIT_ROTATION, do_split, false);
+ bUnit_AsString2(
+ numstr, numstr_size, (double)ruler_angle,
+ prec, B_UNIT_ROTATION, unit, false);
}
}
else {
@@ -188,9 +186,9 @@ static void ruler_item_as_string(RulerItem *ruler_item, UnitSettings *unit,
BLI_snprintf(numstr, numstr_size, "%.*f", prec, ruler_len);
}
else {
- bUnit_AsString(numstr, numstr_size,
- (double)(ruler_len * unit->scale_length),
- prec, unit->system, B_UNIT_LENGTH, do_split, false);
+ bUnit_AsString2(
+ numstr, numstr_size, (double)(ruler_len * unit->scale_length),
+ prec, B_UNIT_LENGTH, unit, false);
}
}
}
diff --git a/source/blender/editors/space_view3d/view3d_ruler.c b/source/blender/editors/space_view3d/view3d_ruler.c
index d6637c2209e..ae57700653a 100644
--- a/source/blender/editors/space_view3d/view3d_ruler.c
+++ b/source/blender/editors/space_view3d/view3d_ruler.c
@@ -159,7 +159,6 @@ static void ruler_item_active_set(RulerInfo *ruler_info, RulerItem *ruler_item)
static void ruler_item_as_string(RulerItem *ruler_item, UnitSettings *unit,
char *numstr, size_t numstr_size, int prec)
{
- const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
if (ruler_item->flag & RULERITEM_USE_ANGLE) {
const float ruler_angle = angle_v3v3v3(ruler_item->co[0],
@@ -170,9 +169,9 @@ static void ruler_item_as_string(RulerItem *ruler_item, UnitSettings *unit,
BLI_snprintf(numstr, numstr_size, "%.*f°", prec, RAD2DEGF(ruler_angle));
}
else {
- bUnit_AsString(numstr, numstr_size,
- (double)ruler_angle,
- prec, unit->system, B_UNIT_ROTATION, do_split, false);
+ bUnit_AsString2(
+ numstr, numstr_size, (double)ruler_angle,
+ prec, B_UNIT_ROTATION, unit, false);
}
}
else {
@@ -183,9 +182,9 @@ static void ruler_item_as_string(RulerItem *ruler_item, UnitSettings *unit,
BLI_snprintf(numstr, numstr_size, "%.*f", prec, ruler_len);
}
else {
- bUnit_AsString(numstr, numstr_size,
- (double)(ruler_len * unit->scale_length),
- prec, unit->system, B_UNIT_LENGTH, do_split, false);
+ bUnit_AsString2(
+ numstr, numstr_size, (double)(ruler_len * unit->scale_length),
+ prec, B_UNIT_LENGTH, unit, false);
}
}
diff --git a/source/blender/editors/transform/transform.c b/source/blender/editors/transform/transform.c
index 55fa1fb78c6..61a80e900e1 100644
--- a/source/blender/editors/transform/transform.c
+++ b/source/blender/editors/transform/transform.c
@@ -4671,12 +4671,12 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_
dist = len_v3(vec);
if (!(t->flag & T_2D_EDIT) && t->scene->unit.system) {
- const bool do_split = (t->scene->unit.flag & USER_UNIT_OPT_SPLIT) != 0;
int i;
for (i = 0; i < 3; i++) {
- bUnit_AsString(&tvec[NUM_STR_REP_LEN * i], NUM_STR_REP_LEN, dvec[i] * t->scene->unit.scale_length,
- 4, t->scene->unit.system, B_UNIT_LENGTH, do_split, true);
+ bUnit_AsString2(
+ &tvec[NUM_STR_REP_LEN * i], NUM_STR_REP_LEN, dvec[i] * t->scene->unit.scale_length,
+ 4, B_UNIT_LENGTH, &t->scene->unit, true);
}
}
else {
@@ -4687,9 +4687,9 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_
}
if (!(t->flag & T_2D_EDIT) && t->scene->unit.system) {
- const bool do_split = (t->scene->unit.flag & USER_UNIT_OPT_SPLIT) != 0;
- bUnit_AsString(distvec, sizeof(distvec), dist * t->scene->unit.scale_length, 4, t->scene->unit.system,
- B_UNIT_LENGTH, do_split, false);
+ bUnit_AsString2(
+ distvec, sizeof(distvec), dist * t->scene->unit.scale_length,
+ 4, B_UNIT_LENGTH, &t->scene->unit, false);
}
else if (dist > 1e10f || dist < -1e10f) {
/* prevent string buffer overflow */
diff --git a/source/blender/editors/util/numinput.c b/source/blender/editors/util/numinput.c
index 98e7e5ba897..887c85300d1 100644
--- a/source/blender/editors/util/numinput.c
+++ b/source/blender/editors/util/numinput.c
@@ -254,6 +254,32 @@ static bool editstr_insert_at_cursor(NumInput *n, const char *buf, const int buf
return true;
}
+bool user_string_to_number(bContext *C, const char *str, const UnitSettings *unit, int type, double *r_value)
+{
+#ifdef WITH_PYTHON
+ double unit_scale = BKE_scene_unit_scale(unit, type, 1.0);
+ if (!bUnit_ContainsUnit(str, unit->system, type)) {
+ int success = BPY_execute_string_as_number(C, NULL, str, true, r_value);
+ *r_value *= bUnit_PreferredUnitScalar(unit, type);
+ *r_value /= unit_scale;
+ return success;
+ }
+ else {
+ char str_unit_convert[256];
+ BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
+ bUnit_ReplaceString(
+ str_unit_convert, sizeof(str_unit_convert), str,
+ unit_scale, unit->system, type);
+
+ return BPY_execute_string_as_number(C, NULL, str_unit_convert, true, r_value);
+ }
+#else
+ *r_value = atof(str);
+ return true;
+#endif
+}
+
+
static bool editstr_is_simple_numinput(const char ascii)
{
if (ascii >= '0' && ascii <= '9') {
@@ -519,38 +545,18 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event)
/* At this point, our value has changed, try to interpret it with python (if str is not empty!). */
if (n->str[0]) {
const float val_prev = n->val[idx];
- double val;
-#ifdef WITH_PYTHON
Scene *sce = CTX_data_scene(C);
- char str_unit_convert[NUM_STR_REP_LEN * 6]; /* Should be more than enough! */
- const char *default_unit = NULL;
- /* Use scale_length if needed! */
- const float fac = (float)BKE_scene_unit_scale(&sce->unit, n->unit_type[idx], 1.0);
-
- /* Make radian default unit when needed. */
- if (n->unit_use_radians && n->unit_type[idx] == B_UNIT_ROTATION) {
- default_unit = "r";
- }
-
- BLI_strncpy(str_unit_convert, n->str, sizeof(str_unit_convert));
-
- bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), default_unit, fac,
- n->unit_sys, n->unit_type[idx]);
+ double val;
+ int success = user_string_to_number(C, n->str, &sce->unit, n->unit_type[idx], &val);
- /* Note: with angles, we always get values as radians here... */
- if (BPY_execute_string_as_number(C, NULL, str_unit_convert, false, &val)) {
+ if (success) {
n->val[idx] = (float)val;
n->val_flag[idx] &= ~NUM_INVALID;
}
else {
n->val_flag[idx] |= NUM_INVALID;
- }
-#else /* Very unlikely, but does not harm... */
- val = atof(n->str);
- n->val[idx] = (float)val;
- UNUSED_VARS(C);
-#endif /* WITH_PYTHON */
+ }
#ifdef USE_FAKE_EDIT