From 177a6fb778d620dd6ffd4a6bc437dd04a1127328 Mon Sep 17 00:00:00 2001 From: dam Date: Fri, 3 Dec 2021 17:37:45 +0000 Subject: Fixed rounding errors affecting binning of 'big' numbers (~9000). --- date_picker/scroll_picker.gd | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/date_picker/scroll_picker.gd b/date_picker/scroll_picker.gd index 02d5933..afbb02e 100644 --- a/date_picker/scroll_picker.gd +++ b/date_picker/scroll_picker.gd @@ -17,7 +17,7 @@ func set_max_value(value: int): num_of_values = max_value - min_value + 1 var pointer: Dictionary -var value: float = 0.0 +var value: float = 0.0 # @DAM TODO Maybe rename variable? var anchor: float = 0.0 var current_value_base_position: float @@ -27,11 +27,11 @@ var previous_value_base_position: float func _ready(): pointer = { - index = -1, - initial_position = Vector2.ZERO, - current_position = Vector2.ZERO, - velocity = Vector2.ZERO, - is_active = false, + index = -1, + initial_position = Vector2.ZERO, + current_position = Vector2.ZERO, + velocity = Vector2.ZERO, + is_active = false, } current_value_base_position = ($value as Label).rect_position.y next_value_base_position = ($value_next as Label).rect_position.y @@ -40,32 +40,31 @@ func _ready(): func _process(delta: float): - # @DAM TODO It's weird that we use '-value'... check maths. var scroll_unit_height := ($value as Label).rect_size.y - var normalized_value := - value / scroll_unit_height + var normalized_value := value / scroll_unit_height var normalized_int_value := round(normalized_value) as int - # $debug.text = "%s | %s" % [normalized_value, normalized_int_value] - pointer.velocity *= clamp((1.0 - VELOCITY_DECAYING_FACTOR * delta), 0.0, 1.0) + var normalized_displacement := normalized_value - normalized_int_value + var displacement := normalized_displacement * scroll_unit_height + if pointer.is_active: - value = anchor + (pointer.current_position.y - pointer.initial_position.y) - elif is_zero_approx(pointer.velocity.y) == false: - value += pointer.velocity.y * delta - if abs(pointer.velocity.y) < BINNING_THRESHOLD * scroll_unit_height: - var fix := ((normalized_int_value as float) - normalized_value) * scroll_unit_height - value -= fix * BINNING_ADJUST_P * delta + value = anchor - (pointer.current_position.y - pointer.initial_position.y) + else: + pointer.velocity *= clamp((1.0 - VELOCITY_DECAYING_FACTOR * delta), 0.0, 1.0) + value -= pointer.velocity.y * delta + if abs(pointer.velocity.y) < BINNING_THRESHOLD * scroll_unit_height && abs(displacement) > 0.5: + pointer.velocity.y = 0.0 + value -= displacement * BINNING_ADJUST_P * delta var logic_value := min_value + fposmod(normalized_int_value, num_of_values) var logic_next_value := min_value + fposmod(normalized_int_value + 1, num_of_values) var logic_previous_value := min_value + fposmod(normalized_int_value - 1, num_of_values) + # @DAM TODO Change all '$access' with variables set during on '_ready'. $value.text = "%d" % logic_value $value_next.text = "%d" % logic_next_value $value_previous.text = "%d" % logic_previous_value - var normalized_displacement := normalized_value - normalized_int_value - var displacement := normalized_displacement * scroll_unit_height - ($value as Label).rect_position.y = current_value_base_position - displacement ($value_previous as Label).rect_position.y = previous_value_base_position - displacement ($value_next as Label).rect_position.y = next_value_base_position - displacement @@ -73,6 +72,7 @@ func _process(delta: float): ($value_next as Label).modulate.a = normalized_displacement + 0.5 ($value_previous as Label).modulate.a = 0.5 - normalized_displacement + # @DAM TODO Rename these variables. var current_pos := value var turn_over := num_of_values * scroll_unit_height if current_pos >= turn_over || current_pos < 0.0: -- cgit v1.2.3