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 238 Lines • ▼ Show 20 Lines | static bool editstr_insert_at_cursor(NumInput *n, const char *buf, const int buf_len) | ||||
| n->str_cur = n_cur; | n->str_cur = n_cur; | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool user_string_to_number(bContext *C, | bool user_string_to_number(bContext *C, | ||||
| const char *str, | const char *str, | ||||
| const UnitSettings *unit, | const UnitSettings *unit, | ||||
| int type, | int type, | ||||
| const char *error_prefix, | double *r_value, | ||||
| double *r_value) | const bool use_single_line_error, | ||||
| char **r_error) | |||||
| { | { | ||||
| #ifdef WITH_PYTHON | #ifdef WITH_PYTHON | ||||
| struct BPy_RunErrInfo err_info = { | |||||
| .use_single_line_error = use_single_line_error, | |||||
| .r_string = r_error, | |||||
| }; | |||||
| double unit_scale = BKE_scene_unit_scale(unit, type, 1.0); | double unit_scale = BKE_scene_unit_scale(unit, type, 1.0); | ||||
| if (BKE_unit_string_contains_unit(str, type)) { | if (BKE_unit_string_contains_unit(str, type)) { | ||||
| char str_unit_convert[256]; | char str_unit_convert[256]; | ||||
| BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert)); | BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert)); | ||||
| BKE_unit_replace_string( | BKE_unit_replace_string( | ||||
| str_unit_convert, sizeof(str_unit_convert), str, unit_scale, unit->system, type); | str_unit_convert, sizeof(str_unit_convert), str, unit_scale, unit->system, type); | ||||
| return BPY_run_string_as_number(C, NULL, str_unit_convert, error_prefix, r_value); | return BPY_run_string_as_number(C, NULL, str_unit_convert, &err_info, r_value); | ||||
| } | } | ||||
| int success = BPY_run_string_as_number(C, NULL, str, error_prefix, r_value); | int success = BPY_run_string_as_number(C, NULL, str, &err_info, r_value); | ||||
| *r_value = BKE_unit_apply_preferred_unit(unit, type, *r_value); | *r_value = BKE_unit_apply_preferred_unit(unit, type, *r_value); | ||||
| *r_value /= unit_scale; | *r_value /= unit_scale; | ||||
| return success; | return success; | ||||
| #else | #else | ||||
| UNUSED_VARS(C, unit, type); | UNUSED_VARS(C, unit, type); | ||||
| *r_value = atof(str); | *r_value = atof(str); | ||||
| return true; | return true; | ||||
| ▲ Show 20 Lines • Show All 263 Lines • ▼ Show 20 Lines | else if (!updated) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| /* At this point, our value has changed, try to interpret it with python | /* At this point, our value has changed, try to interpret it with python | ||||
| * (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; | |||||
| 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], IFACE_("Numeric input evaluation"), &val); | C, n->str, &sce->unit, n->unit_type[idx], &val, false, &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; | ||||
| } | } | ||||
| else { | else { | ||||
| n->val_flag[idx] |= NUM_INVALID; | n->val_flag[idx] |= NUM_INVALID; | ||||
| } | } | ||||
| Show All 29 Lines | |||||