Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/util/numinput.c
| Show All 23 Lines | |||||
| #include "BLI_string.h" | #include "BLI_string.h" | ||||
| #include "BLI_string_cursor_utf8.h" | #include "BLI_string_cursor_utf8.h" | ||||
| #include "BLI_string_utf8.h" | #include "BLI_string_utf8.h" | ||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "BLT_translation.h" | #include "BLT_translation.h" | ||||
| #include "BKE_context.h" | #include "BKE_context.h" | ||||
| #include "BKE_report.h" | |||||
| #include "BKE_scene.h" | #include "BKE_scene.h" | ||||
| #include "BKE_unit.h" | #include "BKE_unit.h" | ||||
| #include "DNA_scene_types.h" | #include "DNA_scene_types.h" | ||||
| #include "WM_api.h" | #include "WM_api.h" | ||||
| #include "WM_types.h" | #include "WM_types.h" | ||||
| ▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | #ifdef USE_FAKE_EDIT | ||||
| trailing_exp = ")"; | trailing_exp = ")"; | ||||
| } | } | ||||
| else if (n->val_flag[i] & NUM_INVERSE) { | else if (n->val_flag[i] & NUM_INVERSE) { | ||||
| heading_exp = "1/("; | heading_exp = "1/("; | ||||
| trailing_exp = ")"; | trailing_exp = ")"; | ||||
| } | } | ||||
| #endif | #endif | ||||
| if (n->val_flag[i] & NUM_INVALID) { | if ((n->val_flag[i] & NUM_INVALID) == 0) { | ||||
| BLI_strncpy(val, "Invalid", sizeof(val)); | |||||
| } | |||||
| else { | |||||
| BKE_unit_value_as_string_adaptive(val, | BKE_unit_value_as_string_adaptive(val, | ||||
| sizeof(val), | sizeof(val), | ||||
| (double)(n->val[i] * fac), | (double)(n->val[i] * fac), | ||||
| prec, | prec, | ||||
| n->unit_sys, | n->unit_sys, | ||||
| n->unit_type[i], | n->unit_type[i], | ||||
| true, | true, | ||||
| false); | false); | ||||
| } | } | ||||
| /* +1 because of trailing '\0' */ | /* +1 because of trailing '\0' */ | ||||
| BLI_strncpy(before_cursor, n->str, n->str_cur + 1); | BLI_strncpy(before_cursor, n->str, n->str_cur + 1); | ||||
| BLI_snprintf(&str[j * ln], | BLI_snprintf(&str[j * ln], | ||||
| ln, | ln, | ||||
| "[%s%s|%s%s] = %s", | "[%s%s|%s%s] = %s", | ||||
| heading_exp, | heading_exp, | ||||
| before_cursor, | before_cursor, | ||||
| &n->str[n->str_cur], | &n->str[n->str_cur], | ||||
| trailing_exp, | trailing_exp, | ||||
| val); | (n->val_flag[i] & NUM_INVALID) ? n->val_error[i] : val); | ||||
| } | } | ||||
| else { | else { | ||||
| const char *cur = (i == n->idx) ? "|" : ""; | const char *cur = (i == n->idx) ? "|" : ""; | ||||
| if (n->unit_use_radians && n->unit_type[i] == B_UNIT_ROTATION) { | if (n->unit_use_radians && n->unit_type[i] == B_UNIT_ROTATION) { | ||||
| /* Radian exception... */ | /* Radian exception... */ | ||||
| BLI_snprintf(&str[j * ln], ln, "%s%.6gr%s", cur, n->val[i], cur); | BLI_snprintf(&str[j * ln], ln, "%s%.6gr%s", cur, n->val[i], cur); | ||||
| } | } | ||||
| else { | else { | ||||
| ▲ Show 20 Lines • Show All 414 Lines • ▼ Show 20 Lines | #endif | ||||
| * (if str is not empty!). */ | * (if str is not empty!). */ | ||||
| if (n->str[0]) { | if (n->str[0]) { | ||||
| const float val_prev = n->val[idx]; | const float val_prev = n->val[idx]; | ||||
| Scene *sce = CTX_data_scene(C); | Scene *sce = CTX_data_scene(C); | ||||
| char *error = NULL; | char *error = NULL; | ||||
| double val; | double val; | ||||
| int success = user_string_to_number( | int success = user_string_to_number( | ||||
| C, n->str, &sce->unit, n->unit_type[idx], &val, false, &error); | C, n->str, &sce->unit, n->unit_type[idx], &val, true, &error); | ||||
| if (error) { | |||||
| ReportList *reports = CTX_wm_reports(C); | |||||
| printf("%s\n", error); | |||||
| BKE_report(reports, RPT_ERROR, error); | |||||
| BKE_report(reports, RPT_ERROR, IFACE_("Numeric input evaluation")); | |||||
| MEM_freeN(error); | |||||
| } | |||||
| if (success) { | if (success) { | ||||
| n->val[idx] = (float)val; | n->val[idx] = (float)val; | ||||
| n->val_flag[idx] &= ~NUM_INVALID; | n->val_flag[idx] &= ~NUM_INVALID; | ||||
| n->val_error[idx][0] = '\0'; | |||||
| } | } | ||||
| else { | else { | ||||
| n->val_flag[idx] |= NUM_INVALID; | n->val_flag[idx] |= NUM_INVALID; | ||||
| if (error != NULL) { | |||||
| STRNCPY(n->val_error[idx], error); | |||||
| MEM_freeN(error); | |||||
| } | |||||
| } | } | ||||
| #ifdef USE_FAKE_EDIT | #ifdef USE_FAKE_EDIT | ||||
| if (n->val_flag[idx] & NUM_NEGATE) { | if (n->val_flag[idx] & NUM_NEGATE) { | ||||
| n->val[idx] = -n->val[idx]; | n->val[idx] = -n->val[idx]; | ||||
| } | } | ||||
| if (n->val_flag[idx] & NUM_INVERSE) { | if (n->val_flag[idx] & NUM_INVERSE) { | ||||
| val = n->val[idx]; | val = n->val[idx]; | ||||
| /* If we invert on radians when user is in degrees, | /* If we invert on radians when user is in degrees, | ||||
| * you get unexpected results... See T53463. */ | * you get unexpected results... See T53463. */ | ||||
| if (!n->unit_use_radians && n->unit_type[idx] == B_UNIT_ROTATION) { | if (!n->unit_use_radians && n->unit_type[idx] == B_UNIT_ROTATION) { | ||||
| val = RAD2DEG(val); | val = RAD2DEG(val); | ||||
| } | } | ||||
| val = 1.0 / val; | val = 1.0 / val; | ||||
| if (!n->unit_use_radians && n->unit_type[idx] == B_UNIT_ROTATION) { | if (!n->unit_use_radians && n->unit_type[idx] == B_UNIT_ROTATION) { | ||||
| val = DEG2RAD(val); | val = DEG2RAD(val); | ||||
| } | } | ||||
| n->val[idx] = (float)val; | n->val[idx] = (float)val; | ||||
| } | } | ||||
| #endif | #endif | ||||
| if (UNLIKELY(!isfinite(n->val[idx]))) { | if (UNLIKELY(!isfinite(n->val[idx]))) { | ||||
| n->val[idx] = val_prev; | n->val[idx] = val_prev; | ||||
| n->val_flag[idx] |= NUM_INVALID; | n->val_flag[idx] |= NUM_INVALID; | ||||
| STRNCPY(n->val_error[idx], IFACE_("Non-finite number")); | |||||
| } | |||||
| } | } | ||||
| else { | |||||
| /* Clear the error when empty. */ | |||||
| n->val_flag[idx] &= ~NUM_INVALID; | |||||
| n->val_error[idx][0] = '\0'; | |||||
| } | } | ||||
| /* REDRAW SINCE NUMBERS HAVE CHANGED */ | /* REDRAW SINCE NUMBERS HAVE CHANGED */ | ||||
| return true; | return true; | ||||
| } | } | ||||