extends Control 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 export var max_value: int var pointer: Dictionary var anchor: float var value: int var offset: float var current_value_base_position: float var previous_value_base_position: float var next_value_base_position: float func _ready(): pointer = { 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 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 if pointer.is_active: 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) offset -= pointer.velocity.y * delta / scroll_unit_height if abs(pointer.velocity.y) < BINNING_THRESHOLD * scroll_unit_height: offset -= offset * BINNING_ADJUST_P * delta # 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" % 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) 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 ($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 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 + offset else: pointer.index = -1 if event is InputEventScreenDrag && event.index == pointer.index: var drag := event as InputEventScreenDrag pointer.current_position = drag.position pointer.velocity = drag.speed func set_value(value: int): breakpoint # @DAM TODO func get_value() -> int: breakpoint # @DAM TODO return 0