brackeys-12/components/Cursor/cursor.gd
2024-09-14 21:09:17 -04:00

148 lines
3.5 KiB
GDScript

@icon("res://components/Cursor/mouse-pointer-2.svg")
extends Base
@onready var mouse_control = $"MouseControl"
@onready var sprite_2d: Sprite2D = $MouseControl/Sprite2D
const CURSOR_UI = preload("res://components/Cursor/cursor-ui.png")
const CURSOR = preload("res://components/Cursor/cursor.png")
var cursor_state = "normal"
var rotation_tween
var cursor_tween
func _ready() -> void:
Persister.data_persisted.connect(_on_data_persisted)
func _on_fire(_data):
if cursor_state == "ui":
return
func _on_release():
var overlapping_areas = mouse_control.get_overlapping_areas()
overlapping_areas.sort_custom(func(a, b):
return a.z_index > b.z_index
)
for area in overlapping_areas:
if area.has_method("_on_released"):
var current_node = area
var hidden = false
while current_node:
if "modulate" in current_node:
if current_node.modulate.a < 1:
hidden = true
break
current_node = current_node.get_parent()
if hidden:
continue
if not area._on_released():
break
func _on_click():
var overlapping_areas = mouse_control.get_overlapping_areas()
overlapping_areas.sort_custom(func(a, b):
return a.z_index > b.z_index
)
for area in overlapping_areas:
if area.has_method("_on_clicked"):
var current_node = area
var hidden = false
while current_node:
if "modulate" in current_node:
if current_node.modulate.a < 1:
hidden = true
break
if "visible" in current_node:
if not current_node.visible:
hidden = true
break
current_node = current_node.get_parent()
if hidden:
continue
if not area._on_clicked():
break
func _on_rclick():
var overlapping_areas = mouse_control.get_overlapping_areas()
overlapping_areas.sort_custom(func(a, b):
return a.z_index > b.z_index
)
for area in overlapping_areas:
if area.has_method("_on_rclicked"):
var current_node = area
var hidden = false
while current_node:
if "modulate" in current_node:
if current_node.modulate.a < 1:
hidden = true
break
if "visible" in current_node:
if not current_node.visible:
hidden = true
break
current_node = current_node.get_parent()
if hidden:
continue
if not area._on_rclicked():
break
func _process(delta):
if Input.is_action_just_released("lclick"):
_on_release()
if Input.is_action_just_pressed("lclick"):
_on_click()
if Input.is_action_just_pressed("rclick"):
_on_rclick()
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
mouse_control.position = mouse_control.get_global_mouse_position()
if not Input.is_action_pressed("lclick"):
sprite_2d.rotation = move_toward(sprite_2d.rotation, 0, delta)
sprite_2d.scale = sprite_2d.scale.move_toward(Vector2.ONE, delta)
else:
sprite_2d.scale = sprite_2d.scale.move_toward(Vector2.ONE * 0.9, delta * 2)
sprite_2d.rotation = move_toward(sprite_2d.rotation, -0.1, delta * 2)
func _on_mouse_control_area_entered(area):
if area.has_method("_on_hovered"):
var current_node = area
var hidden = false
while current_node:
if "visible" in current_node:
if not current_node.visible:
hidden = true
break
current_node = current_node.get_parent()
if not hidden:
area._on_hovered()
func _on_mouse_control_area_exited(area):
if area.has_method("_on_unhovered"):
var current_node = area
var hidden = false
while current_node:
if "visible" in current_node:
if not current_node.visible:
hidden = true
break
current_node = current_node.get_parent()
if not hidden:
area._on_unhovered()