add touch to slider

This commit is contained in:
Nitwel 2024-03-21 16:35:27 +01:00
parent 60f6beb68f
commit c39012c17c
2 changed files with 34 additions and 1 deletions

View File

@ -65,6 +65,9 @@ class_name Slider3D
@onready var cutout_end_right: CSGCylinder3D = $Rod/Cutout/EndRight @onready var cutout_end_right: CSGCylinder3D = $Rod/Cutout/EndRight
@onready var label: Label3D = $Label @onready var label: Label3D = $Label
@onready var body_collision_shape: CollisionShape3D = $CollisionBody/CollisionShape3D
@onready var area_collision_shape: CollisionShape3D = $Area3D/CollisionShape3D
@onready var slider_knob: MeshInstance3D = $Knob @onready var slider_knob: MeshInstance3D = $Knob
signal on_value_changed(value: float) signal on_value_changed(value: float)
@ -84,6 +87,9 @@ func _on_press_down(event: EventPointer):
func _on_press_move(event: EventPointer): func _on_press_move(event: EventPointer):
_handle_press(event) _handle_press(event)
func _on_touch_enter(event: EventTouch):
_handle_touch(event)
func _get_slider_min_max(): func _get_slider_min_max():
var cutout_radius = (size.z - cutout_border * 2) / 2 var cutout_radius = (size.z - cutout_border * 2) / 2
@ -109,6 +115,18 @@ func _handle_press(event: EventPointer):
value = lerp(min, max, click_percent) value = lerp(min, max, click_percent)
func _handle_touch(event: EventTouch):
var click_pos = to_local(event.fingers[0].area.global_position)
var min_max = _get_slider_min_max()
var pos_x = clamp(click_pos.x, min_max.x, min_max.y)
var click_percent = inverse_lerp(min_max.x, min_max.y, pos_x)
value = lerp(min, max, click_percent)
func _update_slider(): func _update_slider():
var min_max = _get_slider_min_max() var min_max = _get_slider_min_max()
@ -119,6 +137,10 @@ func _update_slider():
func _update_shape(): func _update_shape():
outside_rod.size = size outside_rod.size = size
body_collision_shape.shape.size = size * 0.01
area_collision_shape.shape.size = Vector3(size.x, size.y * 2, size.z) * 0.01
area_collision_shape.position = Vector3(0, size.y, 0) * 0.01
var cutout_width = size.z - cutout_border * 2 var cutout_width = size.z - cutout_border * 2
cutout_box.size = Vector3( cutout_box.size = Vector3(

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=6 format=3 uid="uid://pk5k1q8bx0rj"] [gd_scene load_steps=7 format=3 uid="uid://pk5k1q8bx0rj"]
[ext_resource type="Script" path="res://content/ui/components/slider/slider.gd" id="1_ylune"] [ext_resource type="Script" path="res://content/ui/components/slider/slider.gd" id="1_ylune"]
@ -19,6 +19,9 @@ height = 0.005
[sub_resource type="BoxShape3D" id="BoxShape3D_h1mn1"] [sub_resource type="BoxShape3D" id="BoxShape3D_h1mn1"]
size = Vector3(0.2, 0.004, 0.01) size = Vector3(0.2, 0.004, 0.01)
[sub_resource type="BoxShape3D" id="BoxShape3D_s64dk"]
size = Vector3(0.2, 0.008, 0.01)
[node name="Slider" type="Node3D"] [node name="Slider" type="Node3D"]
script = ExtResource("1_ylune") script = ExtResource("1_ylune")
size = Vector3(20, 0.4, 1) size = Vector3(20, 0.4, 1)
@ -79,3 +82,11 @@ text = "0.2 "
font_size = 10 font_size = 10
outline_size = 4 outline_size = 4
horizontal_alignment = 0 horizontal_alignment = 0
[node name="Area3D" type="Area3D" parent="."]
collision_layer = 4
collision_mask = 4
[node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.004, 0)
shape = SubResource("BoxShape3D_s64dk")