EditorProperty: Fix range hint parsing with optional step

This could lead to have a step of 0 when parsing e.g. "1,10,is_greater".
This commit is contained in:
Rémi Verschelde 2022-02-03 11:57:32 +01:00
parent 36880714e4
commit ebe9495b7d
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 25 additions and 21 deletions

View file

@ -1263,7 +1263,7 @@ void EditorPropertyInteger::_bind_methods() {
void EditorPropertyInteger::setup(int64_t p_min, int64_t p_max, int64_t p_step, bool p_allow_greater, bool p_allow_lesser) {
spin->set_min(p_min);
spin->set_max(p_max);
spin->set_step((p_step == 0) ? 1 : p_step);
spin->set_step(p_step);
spin->set_allow_greater(p_allow_greater);
spin->set_allow_lesser(p_allow_lesser);
}
@ -1353,7 +1353,7 @@ void EditorPropertyFloat::setup(double p_min, double p_max, double p_step, bool
angle_in_radians = p_angle_in_radians;
spin->set_min(p_min);
spin->set_max(p_max);
spin->set_step((p_step == 0) ? 0.1 : p_step);
spin->set_step(p_step);
spin->set_hide_slider(p_no_slider);
spin->set_exp_ratio(p_exp_range);
spin->set_allow_greater(p_greater);
@ -3318,9 +3318,9 @@ struct EditorPropertyRangeHint {
bool angle_in_degrees = false;
bool greater = true;
bool lesser = true;
double min = -99999;
double max = 99999;
double step = 0;
double min = -99999.0;
double max = 99999.0;
double step = 1.0;
String suffix;
bool exp_range = false;
bool hide_slider = true;
@ -3331,18 +3331,25 @@ static EditorPropertyRangeHint _parse_range_hint(PropertyHint p_hint, const Stri
EditorPropertyRangeHint hint;
hint.step = p_default_step;
bool degrees = false;
if (p_hint == PROPERTY_HINT_RANGE && p_hint_text.get_slice_count(",") >= 2) {
hint.greater = false; //if using ranged, assume false by default
if (p_hint == PROPERTY_HINT_RANGE) {
Vector<String> slices = p_hint_text.split(",");
ERR_FAIL_COND_V_MSG(slices.size() < 2, hint,
vformat("Invalid PROPERTY_HINT_RANGE with hint \"%s\": Missing required min and/or max values.", p_hint_text));
hint.greater = false; // If using ranged, assume false by default.
hint.lesser = false;
hint.min = p_hint_text.get_slice(",", 0).to_float();
hint.max = p_hint_text.get_slice(",", 1).to_float();
if (p_hint_text.get_slice_count(",") >= 3) {
hint.step = p_hint_text.get_slice(",", 2).to_float();
hint.min = slices[0].to_float();
hint.max = slices[1].to_float();
if (slices.size() >= 3 && slices[2].is_valid_float()) {
// Step is optional, could be something else if not a number.
hint.step = slices[2].to_float();
}
hint.hide_slider = false;
for (int i = 2; i < p_hint_text.get_slice_count(","); i++) {
String slice = p_hint_text.get_slice(",", i).strip_edges();
for (int i = 2; i < slices.size(); i++) {
String slice = slices[i].strip_edges();
if (slice == "radians") {
hint.radians = true;
} else if (slice == "degrees") {
@ -3365,6 +3372,9 @@ static EditorPropertyRangeHint _parse_range_hint(PropertyHint p_hint, const Stri
hint.suffix = U"\u00B0";
}
ERR_FAIL_COND_V_MSG(hint.step == 0, hint,
vformat("Invalid PROPERTY_HINT_RANGE with hint \"%s\": Step cannot be 0.", p_hint_text));
return hint;
}
@ -3435,9 +3445,6 @@ EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_property(Object *p_
EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
EditorPropertyRangeHint hint = _parse_range_hint(p_hint, p_hint_text, 1);
if (hint.step == 0) {
WARN_PRINT(p_path + ": Range step size is 0.");
}
editor->setup(hint.min, hint.max, hint.step, hint.greater, hint.lesser);
return editor;
@ -3466,9 +3473,6 @@ EditorProperty *EditorInspectorDefaultPlugin::get_editor_for_property(Object *p_
EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
EditorPropertyRangeHint hint = _parse_range_hint(p_hint, p_hint_text, default_float_step);
if (hint.step == 0) {
WARN_PRINT(p_path + ": Range step size is 0.");
}
editor->setup(hint.min, hint.max, hint.step, hint.hide_slider, hint.exp_range, hint.greater, hint.lesser, hint.suffix, hint.radians);
return editor;

View file

@ -1310,7 +1310,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/viewport_width",
PropertyInfo(Variant::INT, "display/window/size/viewport_width",
PROPERTY_HINT_RANGE,
"0,7680,or_greater")); // 8K resolution
"0,7680,1,or_greater")); // 8K resolution
GLOBAL_DEF_BASIC("display/window/size/viewport_height", 600);
ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/viewport_height",
@ -1333,7 +1333,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
PropertyInfo(Variant::INT,
"display/window/size/window_height_override",
PROPERTY_HINT_RANGE,
"0,4320,or_greater")); // 8K resolution
"0,4320,1,or_greater")); // 8K resolution
if (use_custom_res) {
if (!force_res) {