brackeys-12/components/Cursor/cursor.gd

149 lines
3.5 KiB
GDScript3
Raw Permalink Normal View History

2024-09-08 17:34:41 +00:00
@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
2024-09-15 01:09:17 +00:00
var cursor_tween
2024-09-08 17:34:41 +00:00
func _ready() -> void:
Persister.data_persisted.connect(_on_data_persisted)
2024-09-15 01:09:17 +00:00
2024-09-08 17:34:41 +00:00
func _on_fire(_data):
if cursor_state == "ui":
return
2024-09-14 00:09:38 +00:00
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
2024-09-08 17:34:41 +00:00
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
2024-09-14 01:02:26 +00:00
if "visible" in current_node:
if not current_node.visible:
hidden = true
break
2024-09-08 17:34:41 +00:00
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
2024-09-14 01:02:26 +00:00
if "visible" in current_node:
if not current_node.visible:
hidden = true
break
2024-09-08 17:34:41 +00:00
current_node = current_node.get_parent()
if hidden:
continue
if not area._on_rclicked():
break
func _process(delta):
2024-09-14 00:09:38 +00:00
if Input.is_action_just_released("lclick"):
_on_release()
2024-09-08 17:34:41 +00:00
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()
2024-09-15 01:09:17 +00:00
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)
2024-09-08 17:34:41 +00:00
func _on_mouse_control_area_entered(area):
if area.has_method("_on_hovered"):
2024-09-14 01:02:26 +00:00
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()
2024-09-08 17:34:41 +00:00
func _on_mouse_control_area_exited(area):
if area.has_method("_on_unhovered"):
2024-09-14 01:02:26 +00:00
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()