aboutsummaryrefslogtreecommitdiff
path: root/date_picker
diff options
context:
space:
mode:
authordam <dam@gudinoff>2021-12-04 03:06:12 +0000
committerdam <dam@gudinoff>2021-12-04 03:06:12 +0000
commit33ecde916329f58a6d0600a5a8ba6a60b83b9ac9 (patch)
tree198375b34066ec1b523c3852ec94fa3e5c1f1cee /date_picker
parent177a6fb778d620dd6ffd4a6bc437dd04a1127328 (diff)
downloadsurgery-log-33ecde916329f58a6d0600a5a8ba6a60b83b9ac9.tar.zst
surgery-log-33ecde916329f58a6d0600a5a8ba6a60b83b9ac9.zip
Use int+float to store picker value to fix rounding errors.
Diffstat (limited to 'date_picker')
-rw-r--r--date_picker/scroll_picker.gd82
1 files changed, 34 insertions, 48 deletions
diff --git a/date_picker/scroll_picker.gd b/date_picker/scroll_picker.gd
index afbb02e..4ae22c0 100644
--- a/date_picker/scroll_picker.gd
+++ b/date_picker/scroll_picker.gd
@@ -4,25 +4,17 @@ const VELOCITY_DECAYING_FACTOR: float = 5.5
const BINNING_THRESHOLD: float = 0.75
const BINNING_ADJUST_P: float = 5.0
-export var min_value: int setget set_min_value
-export var max_value: int setget set_max_value
-var num_of_values: int
-
-func set_min_value(value: int):
- min_value = value
- num_of_values = max_value - min_value + 1
-
-func set_max_value(value: int):
- max_value = value
- num_of_values = max_value - min_value + 1
+export var min_value: int
+export var max_value: int
var pointer: Dictionary
-var value: float = 0.0 # @DAM TODO Maybe rename variable?
-var anchor: float = 0.0
+var anchor: float
+var value: int
+var offset: float
var current_value_base_position: float
-var next_value_base_position: float
var previous_value_base_position: float
+var next_value_base_position: float
func _ready():
@@ -32,66 +24,60 @@ func _ready():
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
previous_value_base_position = ($value_previous as Label).rect_position.y
+ next_value_base_position = ($value_next as Label).rect_position.y
+ value = min_value
+ offset = 0.0
func _process(delta: float):
+ # @DAM Review variable name, and maybe move it elsewhere.
var scroll_unit_height := ($value as Label).rect_size.y
- var normalized_value := value / scroll_unit_height
- var normalized_int_value := round(normalized_value) as int
-
- 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)
+ var dragged_distance: float = - (pointer.current_position.y - pointer.initial_position.y)
+ offset = anchor + (dragged_distance / scroll_unit_height)
+ value = int(offset)
+ offset -= value
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
+ offset -= pointer.velocity.y * delta / scroll_unit_height
+ if abs(pointer.velocity.y) < BINNING_THRESHOLD * scroll_unit_height:
+ offset -= offset * 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)
+ # Using 'offset * 2.0' (equivalent to 'offset / 0.5') rounds the value based on 0.5 units.
+ var cummulative_displacement := int(offset * 2.0)
+ value = wrapi(value + cummulative_displacement, min_value, max_value + 1)
+ offset -= cummulative_displacement
# @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
-
- ($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
+ $value.text = "%d" % value
+ $value_next.text = "%d" % wrapi(value + 1, min_value, max_value + 1)
+ $value_previous.text = "%d" % wrapi(value - 1, min_value, max_value + 1)
- ($value_next as Label).modulate.a = normalized_displacement + 0.5
- ($value_previous as Label).modulate.a = 0.5 - normalized_displacement
+ var offset_position := offset * scroll_unit_height
+ ($value as Label).rect_position.y = current_value_base_position - offset_position
+ ($value_previous as Label).rect_position.y = previous_value_base_position - offset_position
+ ($value_next as Label).rect_position.y = next_value_base_position - offset_position
- # @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:
- value = fposmod(current_pos, turn_over)
+ ($value_previous as Label).modulate.a = 0.5 - offset
+ ($value_next as Label).modulate.a = offset + 0.5
func _gui_input(event: InputEvent):
- # @DAM TODO When we release the pointer, we will receive an event that will se the 'current_position' as the last 'touch.position'.
- if event is InputEventScreenTouch:
-# if (event is InputEventScreenTouch && pointer.is_active && pointer.index == event.index
-# || event is InputEventScreenTouch && pointer.is_active == false):
+ # @DAM TODO TEST
+ if event is InputEventScreenTouch && (pointer.is_active == false || pointer.index == event.index):
var touch := event as InputEventScreenTouch
pointer.is_active = event.pressed
pointer.current_position = touch.position
if pointer.is_active:
pointer.index = touch.index
pointer.initial_position = touch.position
- anchor = value
+ anchor = value + offset
else:
pointer.index = -1