Add independent spinbox arrow step precision

Backported from the `master` branch, with a property hint added.
This commit is contained in:
Jóhannes Gunnar Þorsteinsson 2019-11-15 15:04:34 +00:00 committed by Hugo Locurcio
parent 7d6db300d2
commit e09d4d255e
No known key found for this signature in database
GPG key ID: 39E8F8BE30B0A49C
3 changed files with 27 additions and 7 deletions

View file

@ -39,6 +39,9 @@
<member name="align" type="int" setter="set_align" getter="get_align" enum="LineEdit.Align" default="0"> <member name="align" type="int" setter="set_align" getter="get_align" enum="LineEdit.Align" default="0">
Sets the text alignment of the [SpinBox]. Sets the text alignment of the [SpinBox].
</member> </member>
<member name="custom_arrow_step" type="float" setter="set_custom_arrow_step" getter="get_custom_arrow_step" default="0.0">
If not [code]0[/code], [code]value[/code] will always be rounded to a multiple of [code]custom_arrow_step[/code] when interacting with the arrow buttons of the [SpinBox].
</member>
<member name="editable" type="bool" setter="set_editable" getter="is_editable" default="true"> <member name="editable" type="bool" setter="set_editable" getter="is_editable" default="true">
If [code]true[/code], the [SpinBox] will be editable. Otherwise, it will be read only. If [code]true[/code], the [SpinBox] will be editable. Otherwise, it will be read only.
</member> </member>

View file

@ -79,7 +79,8 @@ void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
void SpinBox::_range_click_timeout() { void SpinBox::_range_click_timeout() {
if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) { if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
bool up = get_local_mouse_position().y < (get_size().height / 2); bool up = get_local_mouse_position().y < (get_size().height / 2);
set_value(get_value() + (up ? get_step() : -get_step())); double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
set_value(get_value() + (up ? step : -step));
if (range_click_timer->is_one_shot()) { if (range_click_timer->is_one_shot()) {
range_click_timer->set_wait_time(0.075); range_click_timer->set_wait_time(0.075);
@ -109,6 +110,8 @@ void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event; Ref<InputEventMouseButton> mb = p_event;
double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
if (mb.is_valid() && mb->is_pressed()) { if (mb.is_valid() && mb->is_pressed()) {
bool up = mb->get_position().y < (get_size().height / 2); bool up = mb->get_position().y < (get_size().height / 2);
@ -116,7 +119,7 @@ void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
case BUTTON_LEFT: { case BUTTON_LEFT: {
line_edit->grab_focus(); line_edit->grab_focus();
set_value(get_value() + (up ? get_step() : -get_step())); set_value(get_value() + (up ? step : -step));
range_click_timer->set_wait_time(0.6); range_click_timer->set_wait_time(0.6);
range_click_timer->set_one_shot(true); range_click_timer->set_one_shot(true);
@ -131,13 +134,13 @@ void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
} break; } break;
case BUTTON_WHEEL_UP: { case BUTTON_WHEEL_UP: {
if (line_edit->has_focus()) { if (line_edit->has_focus()) {
set_value(get_value() + get_step() * mb->get_factor()); set_value(get_value() + step * mb->get_factor());
accept_event(); accept_event();
} }
} break; } break;
case BUTTON_WHEEL_DOWN: { case BUTTON_WHEEL_DOWN: {
if (line_edit->has_focus()) { if (line_edit->has_focus()) {
set_value(get_value() - get_step() * mb->get_factor()); set_value(get_value() - step * mb->get_factor());
accept_event(); accept_event();
} }
} break; } break;
@ -156,8 +159,8 @@ void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) { if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
if (drag.enabled) { if (drag.enabled) {
drag.diff_y += mm->get_relative().y; drag.diff_y += mm->get_relative().y;
float diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8f) * SGN(drag.diff_y); double diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8f) * SGN(drag.diff_y);
set_value(CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max())); set_value(CLAMP(drag.base_val + step * diff_y, get_min(), get_max()));
} else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) { } else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
drag.enabled = true; drag.enabled = true;
@ -252,6 +255,14 @@ void SpinBox::apply() {
_text_entered(line_edit->get_text()); _text_entered(line_edit->get_text());
} }
void SpinBox::set_custom_arrow_step(double p_custom_arrow_step) {
custom_arrow_step = p_custom_arrow_step;
}
double SpinBox::get_custom_arrow_step() const {
return custom_arrow_step;
}
void SpinBox::_bind_methods() { void SpinBox::_bind_methods() {
//ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed); //ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed);
ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input); ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input);
@ -262,8 +273,10 @@ void SpinBox::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix); ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix); ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix); ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable); ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &SpinBox::set_editable);
ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable); ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
ClassDB::bind_method(D_METHOD("set_custom_arrow_step", "arrow_step"), &SpinBox::set_custom_arrow_step);
ClassDB::bind_method(D_METHOD("get_custom_arrow_step"), &SpinBox::get_custom_arrow_step);
ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply); ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply);
ClassDB::bind_method(D_METHOD("_line_edit_focus_enter"), &SpinBox::_line_edit_focus_enter); ClassDB::bind_method(D_METHOD("_line_edit_focus_enter"), &SpinBox::_line_edit_focus_enter);
ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit); ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit);
@ -275,6 +288,7 @@ void SpinBox::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "custom_arrow_step", PROPERTY_HINT_RANGE, "0,10000,0.0001,or_greater"), "set_custom_arrow_step", "get_custom_arrow_step");
} }
SpinBox::SpinBox() { SpinBox::SpinBox() {

View file

@ -49,6 +49,7 @@ class SpinBox : public Range {
virtual void _value_changed(double); virtual void _value_changed(double);
String prefix; String prefix;
String suffix; String suffix;
double custom_arrow_step = 0.0;
void _line_edit_input(const Ref<InputEvent> &p_event); void _line_edit_input(const Ref<InputEvent> &p_event);
@ -90,6 +91,8 @@ public:
String get_prefix() const; String get_prefix() const;
void apply(); void apply();
void set_custom_arrow_step(const double p_custom_arrow_step);
double get_custom_arrow_step() const;
SpinBox(); SpinBox();
}; };