diff options
Diffstat (limited to 'touch_scroll.gd')
| -rw-r--r-- | touch_scroll.gd | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/touch_scroll.gd b/touch_scroll.gd index f2aeca7..7b6aefd 100644 --- a/touch_scroll.gd +++ b/touch_scroll.gd @@ -1,22 +1,45 @@ extends Control +class_name TouchScroll -const VELOCITY_DECAYING_FACTOR: float = 5.5 const DRAG_THRESHOLD_CM: float = 0.250 +export var click_target_path: String +export var scroll_target_path: String +export var scroll_bar_get_method: String +export var scroll_velocity_decaying_factor: float = 2.5 + var pointer: Dictionary var anchor: float var offset: float var screen_dpcm: float -onready var target: Control = get_parent() -onready var target_scroll: VScrollBar = get_parent().get_v_scroll() +var click_target: Control +var scroll_target: Control +var target_scroll_bar: VScrollBar + + + +# @DAM +# Change this into an event-driven approach by changing the touch_scroll into a signal emitter and +# the target scrollable control a signal consumer. This way, the signal consumer will behave as the +# central node and will have the capability to detect when different touch_scroll sensors are +# activated and stop previous inputs (if required). +# Signal emitters should signal: +# - when an input drag starts +# - when an input drag stops +# - when an input drags +# - when an input clicks func _ready(): + click_target = get_node(click_target_path) + scroll_target = get_node(scroll_target_path) + target_scroll_bar = scroll_target.call(scroll_bar_get_method) pointer = { index = -1, initial_position = Vector2.ZERO, current_position = Vector2.ZERO, + relative_position = Vector2.ZERO, velocity = Vector2.ZERO, was_dragged = false, is_active = false, @@ -25,14 +48,23 @@ func _ready(): func _process(delta: float): + # @DAM This only works for the stage... for the database this hides the touch_scroll after the first click. + if click_target.has_focus(): + mouse_filter = Control.MOUSE_FILTER_IGNORE + else: + mouse_filter = Control.MOUSE_FILTER_STOP + if pointer.is_active: var dragged_distance: float = - (pointer.current_position.y - pointer.initial_position.y) offset = anchor + dragged_distance - self.target_scroll.value = offset +# self.target_scroll_bar.value = offset + self.target_scroll_bar.value -= pointer.relative_position.y + pointer.relative_position = Vector2.ZERO elif pointer.velocity.length() > 0.5: - pointer.velocity *= clamp((1.0 - VELOCITY_DECAYING_FACTOR * delta), 0.0, 1.0) + pointer.velocity *= clamp((1.0 - scroll_velocity_decaying_factor * delta), 0.0, 1.0) offset -= pointer.velocity.y * delta - self.target_scroll.value = offset +# self.target_scroll_bar.value = offset + self.target_scroll_bar.value -= pointer.velocity.y * delta func _gui_input(event: InputEvent): @@ -45,11 +77,11 @@ func _gui_input(event: InputEvent): 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 + pointer.current_position = get_global_mouse_position() # touch.position if pointer.is_active: pointer.index = touch.index - pointer.initial_position = touch.position - anchor = self.target_scroll.value + pointer.initial_position = get_global_mouse_position() # touch.position + anchor = self.target_scroll_bar.value else: if pointer.was_dragged == false: # Click detected. simulate_gui_click() @@ -58,15 +90,16 @@ func _gui_input(event: InputEvent): if event is InputEventScreenDrag && event.index == pointer.index: var drag := event as InputEventScreenDrag - pointer.current_position = drag.position + pointer.current_position = get_global_mouse_position() # drag.position pointer.velocity = drag.speed + pointer.relative_position = drag.relative if pointer.current_position.distance_to(pointer.initial_position) / screen_dpcm > DRAG_THRESHOLD_CM: pointer.was_dragged = true func simulate_gui_click(): - var position := self.get_global_mouse_position() - target.rect_global_position + var position := self.get_global_mouse_position() - click_target.rect_global_position var event_touch := InputEventScreenTouch.new() event_touch.index = 0 @@ -79,12 +112,14 @@ func simulate_gui_click(): event_mouse.pressed = true event_touch.pressed = true - target._gui_input(event_mouse) - target._gui_input(event_touch) + click_target._gui_input(event_mouse) + click_target._gui_input(event_touch) + click_target.grab_focus() + event_mouse.pressed = false event_touch.pressed = false - target._gui_input(event_mouse) - target._gui_input(event_touch) + click_target._gui_input(event_mouse) + click_target._gui_input(event_touch) |
