aboutsummaryrefslogtreecommitdiff
path: root/touch_item_list/touch_item_list.gd
diff options
context:
space:
mode:
Diffstat (limited to 'touch_item_list/touch_item_list.gd')
-rw-r--r--touch_item_list/touch_item_list.gd70
1 files changed, 70 insertions, 0 deletions
diff --git a/touch_item_list/touch_item_list.gd b/touch_item_list/touch_item_list.gd
new file mode 100644
index 0000000..d2d0d37
--- /dev/null
+++ b/touch_item_list/touch_item_list.gd
@@ -0,0 +1,70 @@
+extends ItemList
+class_name TouchItemList
+
+const POINTER_VELOCITY_DECAYING_FACTOR: float = 2.5
+
+var is_pointer_dragging := false
+var pointer_drag_velocity := 0.0
+
+onready var drag_sensor := get_node("drag_sensor") as PointerInputSensor
+onready var v_scroll_bar := get_v_scroll() as ScrollBar
+
+
+func _ready():
+ drag_sensor.connect("on_press", self, "pointer_input_handler")
+ drag_sensor.connect("on_drag", self, "pointer_input_handler")
+ drag_sensor.connect("on_end_drag", self, "pointer_input_handler")
+ drag_sensor.connect("on_click", self, "pointer_input_handler")
+ drag_sensor.connect("on_scroll", self, "pointer_input_handler")
+
+
+func _process(delta: float):
+ # Apply drag movement inertia.
+ if is_pointer_dragging == false && abs(pointer_drag_velocity) > 0.5:
+ pointer_drag_velocity *= clamp((1.0 - POINTER_VELOCITY_DECAYING_FACTOR * delta), 0.0, 1.0)
+ v_scroll_bar.value -= pointer_drag_velocity * delta
+
+
+func pointer_input_handler(pointer: PointerInputSensor.PointerInputData):
+ match pointer.action:
+ PointerInputSensor.PointerInputAction.ON_PRESS:
+ is_pointer_dragging = true
+
+ PointerInputSensor.PointerInputAction.ON_DRAG:
+ is_pointer_dragging = true
+ pointer_drag_velocity = pointer.velocity.y
+ v_scroll_bar.value -= pointer.relative_position.y
+
+ PointerInputSensor.PointerInputAction.ON_END_DRAG:
+ is_pointer_dragging = false
+
+ PointerInputSensor.PointerInputAction.ON_SCROLL:
+ var target := self
+ target._gui_input(pointer.event)
+
+ PointerInputSensor.PointerInputAction.ON_CLICK:
+ var target := self
+ var position := target.get_global_mouse_position() - target.rect_global_position
+
+ var event_touch := InputEventScreenTouch.new()
+ event_touch.index = 0
+ event_touch.position = position
+
+ var event_mouse := InputEventMouseButton.new()
+ event_mouse.button_index = BUTTON_LEFT
+ event_mouse.button_mask = BUTTON_MASK_LEFT
+ event_mouse.position = position
+
+ event_mouse.pressed = true
+ event_touch.pressed = true
+ target._gui_input(event_mouse)
+ target._gui_input(event_touch)
+
+ target.grab_focus()
+
+ event_mouse.pressed = false
+ event_touch.pressed = false
+ target._gui_input(event_mouse)
+ target._gui_input(event_touch)
+
+