diff options
Diffstat (limited to 'date_picker/scroll_picker.gd')
| -rw-r--r-- | date_picker/scroll_picker.gd | 85 |
1 files changed, 62 insertions, 23 deletions
diff --git a/date_picker/scroll_picker.gd b/date_picker/scroll_picker.gd index 4ae22c0..7d3004a 100644 --- a/date_picker/scroll_picker.gd +++ b/date_picker/scroll_picker.gd @@ -12,9 +12,15 @@ 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 +var input: LineEdit +var label_previous: Label +var label_current: Label +var label_next: Label + +var scroll_unit_height: float +var label_previous_base_position: float +var label_current_base_position: float +var label_next_base_position: float func _ready(): @@ -23,20 +29,30 @@ func _ready(): initial_position = Vector2.ZERO, current_position = Vector2.ZERO, velocity = Vector2.ZERO, + was_dragged = false, 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 + + input = get_node("current/input") + input.connect("text_entered", self, "input_text_entered") + input.connect("focus_entered", self, "input_focus_entered") + input.connect("focus_exited", self, "input_focus_exited") + + label_previous = get_node("previous") + label_current = get_node("current") + label_next = get_node("next") + + scroll_unit_height = label_current.rect_size.y + label_previous_base_position = label_previous.rect_position.y + label_current_base_position = label_current.rect_position.y + label_next_base_position = label_next.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) @@ -53,46 +69,69 @@ func _process(delta: float): 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) + label_current.text = "%d" % value + label_next.text = "%d" % wrapi(value + 1, min_value, max_value + 1) + label_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 + label_current.rect_position.y = label_current_base_position - offset_position + label_previous.rect_position.y = label_previous_base_position - offset_position + label_next.rect_position.y = label_next_base_position - offset_position - ($value_previous as Label).modulate.a = 0.5 - offset - ($value_next as Label).modulate.a = offset + 0.5 + label_previous.modulate.a = 0.5 - offset + label_next.modulate.a = offset + 0.5 func _gui_input(event: InputEvent): - # @DAM TODO TEST + # @DAM A bug on GODOT-3.X makes events from non-mouse-emulated pointers (index > 0) unreliable. + if event is InputEventScreenTouch || event is InputEventScreenDrag: + if event.index != 0: + return + 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: + input.release_focus() pointer.index = touch.index pointer.initial_position = touch.position anchor = value + offset else: + if pointer.was_dragged == false: # Click detected. + input.grab_focus() pointer.index = -1 + pointer.was_dragged = false if event is InputEventScreenDrag && event.index == pointer.index: var drag := event as InputEventScreenDrag pointer.current_position = drag.position pointer.velocity = drag.speed + pointer.was_dragged = true + + +func input_text_entered(new_text: String): + input.release_focus() + + +func input_focus_entered(): + input.text = "%d" % value + input.visible = true + input.select_all() + + +func input_focus_exited(): + if input.text.is_valid_integer(): + value = int(input.text) + input.visible = false -func set_value(value: int): - breakpoint # @DAM TODO +func set_value(new_value: int): + value = new_value func get_value() -> int: - breakpoint # @DAM TODO - return 0 + return value |
