2023-11-29 01:16:25 +02:00
|
|
|
@tool
|
2024-04-23 23:11:18 +03:00
|
|
|
extends Container3D
|
2023-11-29 01:16:25 +02:00
|
|
|
class_name Slider3D
|
|
|
|
|
|
|
|
@export var min: float = 0.0:
|
|
|
|
set(new_value):
|
|
|
|
min = new_value
|
|
|
|
if value < min: value = min
|
2024-03-21 14:29:10 +02:00
|
|
|
if !is_inside_tree(): return
|
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
_update_slider()
|
|
|
|
|
|
|
|
@export var max: float = 1.0:
|
|
|
|
set(new_value):
|
|
|
|
max = new_value
|
|
|
|
if value > max: value = max
|
2024-03-21 14:29:10 +02:00
|
|
|
if !is_inside_tree(): return
|
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
_update_slider()
|
2024-03-21 14:29:10 +02:00
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
@export var value: float = 0.2:
|
|
|
|
set(new_value):
|
|
|
|
value = roundi(clamp(new_value, min, max) / step) * step
|
|
|
|
|
2024-03-21 14:29:10 +02:00
|
|
|
if !is_inside_tree(): return
|
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
label.text = str(value) + " " + label_unit
|
2024-03-21 14:29:10 +02:00
|
|
|
if new_value != value: on_value_changed.emit(value)
|
2023-11-29 01:16:25 +02:00
|
|
|
_update_slider()
|
|
|
|
|
|
|
|
@export var step: float = 0.01
|
|
|
|
|
|
|
|
@export var show_label: bool = false:
|
|
|
|
set(value):
|
|
|
|
show_label = value
|
2024-03-21 14:29:10 +02:00
|
|
|
if !is_inside_tree(): return
|
2023-11-29 01:16:25 +02:00
|
|
|
label.visible = show_label
|
|
|
|
|
|
|
|
@export var label_unit: String = "":
|
|
|
|
set(new_value):
|
|
|
|
label_unit = new_value
|
2024-03-21 14:29:10 +02:00
|
|
|
if !is_inside_tree(): return
|
2023-11-29 01:16:25 +02:00
|
|
|
label.text = str(value) + " " + label_unit
|
|
|
|
|
|
|
|
@onready var label: Label3D = $Label
|
|
|
|
|
2024-04-23 23:11:18 +03:00
|
|
|
@onready var mesh: MeshInstance3D = $Body/MeshInstance3D
|
|
|
|
@onready var body_collision_shape: CollisionShape3D = $Body/CollisionShape3D
|
2024-03-21 17:35:27 +02:00
|
|
|
@onready var area_collision_shape: CollisionShape3D = $Area3D/CollisionShape3D
|
2023-11-29 01:16:25 +02:00
|
|
|
@onready var slider_knob: MeshInstance3D = $Knob
|
|
|
|
|
|
|
|
signal on_value_changed(value: float)
|
|
|
|
|
|
|
|
var move_plane: Plane
|
|
|
|
|
|
|
|
func _ready():
|
2024-03-21 14:29:10 +02:00
|
|
|
Update.props(self, ["value", "show_label", "label_unit"])
|
|
|
|
|
|
|
|
_update_slider()
|
2024-04-23 23:11:18 +03:00
|
|
|
_update()
|
2024-05-10 02:00:31 +03:00
|
|
|
move_plane = Plane(Vector3.BACK, Vector3(0, 0, size.z))
|
2023-11-29 01:16:25 +02:00
|
|
|
|
|
|
|
func _on_press_down(event: EventPointer):
|
|
|
|
_handle_press(event)
|
|
|
|
|
|
|
|
func _on_press_move(event: EventPointer):
|
|
|
|
_handle_press(event)
|
|
|
|
|
2024-03-21 17:35:27 +02:00
|
|
|
func _on_touch_enter(event: EventTouch):
|
|
|
|
_handle_touch(event)
|
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
func _get_slider_min_max():
|
2024-04-23 23:11:18 +03:00
|
|
|
return Vector2( - size.x / 2 + 0.01, size.x / 2 - 0.01)
|
2023-11-29 01:16:25 +02:00
|
|
|
|
|
|
|
func _handle_press(event: EventPointer):
|
|
|
|
var ray_pos = event.ray.global_position
|
|
|
|
var ray_dir = -event.ray.global_transform.basis.z
|
|
|
|
|
|
|
|
var local_pos = to_local(ray_pos)
|
|
|
|
var local_dir = global_transform.basis.inverse() * ray_dir
|
|
|
|
|
|
|
|
var click_pos = move_plane.intersects_ray(local_pos, local_dir)
|
|
|
|
|
|
|
|
if click_pos == null:
|
|
|
|
return
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-03-21 17:35:27 +02:00
|
|
|
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)
|
|
|
|
|
2023-11-29 01:16:25 +02:00
|
|
|
func _update_slider():
|
|
|
|
var min_max = _get_slider_min_max()
|
|
|
|
|
|
|
|
var click_percent = inverse_lerp(min, max, value)
|
|
|
|
|
|
|
|
slider_knob.position.x = lerp(min_max.x, min_max.y, click_percent)
|
|
|
|
|
2024-04-23 23:11:18 +03:00
|
|
|
func _update():
|
|
|
|
_update_slider()
|
|
|
|
body_collision_shape.shape.size = size
|
|
|
|
body_collision_shape.position = Vector3(0, 0, size.z / 2)
|
|
|
|
|
|
|
|
area_collision_shape.shape.size = Vector3(size.x, size.y, 0.01)
|
|
|
|
area_collision_shape.position = Vector3(0, 0, size.z + 0.005)
|
|
|
|
|
|
|
|
mesh.position = Vector3(0, 0, size.z)
|
|
|
|
mesh.mesh.size = Vector2(size.x, size.y)
|
|
|
|
mesh.material_override.set_shader_parameter("size", Vector2(size.x, size.y) * 10.0)
|
|
|
|
|
|
|
|
slider_knob.position.z = size.z + 0.002
|
|
|
|
slider_knob.mesh.size = Vector2(size.y * 0.75, size.y * 0.75)
|
|
|
|
slider_knob.material_override.set_shader_parameter("size", Vector2(size.y * 7.5, size.y * 7.5))
|
|
|
|
|
|
|
|
label.position = Vector3(size.x / 2 + 0.005, 0, size.z)
|