blob: d2d0d37e373f0ca3db824790d63c61313dde4c2f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)
|