diff options
| author | dam <dam@gudinoff> | 2022-01-09 01:25:55 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-01-09 01:25:55 +0000 |
| commit | 5ba40857cdc770841d216a27e2a9e8bb3ebf3186 (patch) | |
| tree | 2212f9685067270285265c81208fd5122a760d5d /touch_item_list | |
| parent | 431f042390ad36297a5ec986772c77da23b7fb67 (diff) | |
| download | surgery-log-5ba40857cdc770841d216a27e2a9e8bb3ebf3186.tar.zst surgery-log-5ba40857cdc770841d216a27e2a9e8bb3ebf3186.zip | |
Split touch logic from database. Prototype of popup with list of options.
Diffstat (limited to 'touch_item_list')
| -rw-r--r-- | touch_item_list/touch_item_list.gd | 70 | ||||
| -rw-r--r-- | touch_item_list/touch_item_list.tscn | 23 |
2 files changed, 93 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) + + diff --git a/touch_item_list/touch_item_list.tscn b/touch_item_list/touch_item_list.tscn new file mode 100644 index 0000000..6022ed4 --- /dev/null +++ b/touch_item_list/touch_item_list.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://pointer_input_sensor.gd" type="Script" id=1] +[ext_resource path="res://touch_item_list/touch_item_list.gd" type="Script" id=2] + +[node name="item_list" type="ItemList"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_top = 60.0 +mouse_filter = 2 +script = ExtResource( 2 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="drag_sensor" type="Control" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -8.0 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} |
