185 lines
5.2 KiB
GDScript
185 lines
5.2 KiB
GDScript
extends Draggable
|
|
class_name StickyNote
|
|
|
|
var sticky_id
|
|
var parent_id
|
|
|
|
var sibling: StickyNote
|
|
var shift_tween: Tween
|
|
var modulate_tween: Tween
|
|
|
|
# cannot be explicitly typed, as this can be both handled by picker and physics-board
|
|
var current_handle: Node
|
|
|
|
var position_locked: bool = false
|
|
|
|
## Computed property: Is this currently attached to a card
|
|
var is_attached : bool:
|
|
get: return get_parent() is Card
|
|
|
|
## Replaces the need for tracking attached_to as state
|
|
var attached_to: Card:
|
|
get: return get_parent() as Card if is_attached else null
|
|
|
|
|
|
signal transform_tween_finished
|
|
|
|
@onready var background_sprite: AnimatedSprite2D = %BackgroundSprite
|
|
|
|
@export var text: String = "" :
|
|
set (value):
|
|
if is_node_ready():
|
|
_on_text_updated.call_deferred()
|
|
text = value
|
|
var content: Node2D
|
|
var label: Label
|
|
|
|
@export var picked_random: bool = false
|
|
|
|
@export var shift_by: Vector2 = Vector2(-32, 0)
|
|
@export_color_no_alpha var highlight_color: Color = Color(1.5, 1.5, 1.5)
|
|
|
|
|
|
## Override set_highlight to add visual feedback for sticky notes
|
|
func set_highlight(value: bool) -> void:
|
|
if value != _highlighted:
|
|
_highlighted = value
|
|
|
|
if modulate_tween: modulate_tween.kill()
|
|
if shift_tween: shift_tween.kill()
|
|
|
|
if _highlighted:
|
|
modulate_tween = create_tween()
|
|
modulate_tween.tween_property(self, "modulate", highlight_color, 0.1)
|
|
shift_tween = create_tween()
|
|
shift_tween.tween_property(content, "position", shift_by, 0.2)
|
|
else:
|
|
modulate_tween = create_tween()
|
|
modulate_tween.tween_property(self, "modulate", Color(1, 1, 1), 0.3)
|
|
shift_tween = create_tween()
|
|
shift_tween.tween_property(content, "position", Vector2.ZERO, 0.5)
|
|
|
|
@export var voice_line: AudioStream = null
|
|
@export var is_dragable: bool = false
|
|
|
|
var mouse_offset: Vector2
|
|
|
|
@onready var diameter := 312.0
|
|
@export_range(1.0, 10.0) var bounce_speed: float = 8
|
|
|
|
## Computed property: Check if on the board (dropzone)
|
|
## Replaces on_board state tracking
|
|
var on_board: bool:
|
|
get:
|
|
var parent := get_parent()
|
|
return parent != null and parent.name == "dropzone"
|
|
|
|
func init(sticky_name: String = "sticky_note", card_id: StringName = "-1") -> void:
|
|
name = sticky_name
|
|
text = sticky_name
|
|
parent_id = StringName(card_id.rsplit(".", false, 1)[0])
|
|
sticky_id = card_id
|
|
|
|
func _ready() -> void:
|
|
super._ready()
|
|
label = $Content/Label
|
|
background_sprite = $Content/BackgroundSprite
|
|
content = $Content
|
|
|
|
_on_text_updated.call_deferred()
|
|
|
|
|
|
func _on_text_updated():
|
|
label.text = text
|
|
background_sprite.frame = text.hash() % background_sprite.sprite_frames.get_frame_count(background_sprite.animation)
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
_move_sticky_note(delta)
|
|
|
|
|
|
## frame rate independent FIR smoothing filter
|
|
func _smooth(current: Vector2, goal: Vector2, delta: float) -> Vector2:
|
|
var k := pow(0.1, 60.0 * delta)
|
|
return (1.0-k) * current + k * goal
|
|
|
|
|
|
func _move_sticky_note(delta: float) -> void:
|
|
if is_dragged:
|
|
update_drag_position(get_viewport().get_mouse_position())
|
|
return
|
|
|
|
if is_attached:
|
|
var card := attached_to
|
|
position = _smooth(position, card.sticky_note_position, delta)
|
|
|
|
|
|
|
|
|
|
var transform_tween: Tween
|
|
|
|
func tween_transform_to(target: Transform2D, duration: float = 0.25) ->void:
|
|
# Validate position to prevent teleporting
|
|
if not is_finite(target.origin.x) or not is_finite(target.origin.y):
|
|
push_warning("StickyNote.tween_transform_to: Invalid position, skipping tween")
|
|
transform_tween_finished.emit()
|
|
return
|
|
|
|
if transform_tween and transform_tween.is_running():
|
|
transform_tween.stop()
|
|
|
|
transform_tween = create_tween()
|
|
transform_tween.tween_property(self, "transform", target, duration)
|
|
|
|
await transform_tween.finished
|
|
transform_tween_finished.emit()
|
|
|
|
|
|
# === DRAG LIFECYCLE OVERRIDES ===
|
|
|
|
func end_drag() -> Node:
|
|
super.end_drag()
|
|
return _find_drop_target()
|
|
|
|
|
|
## Find best drop target: Card > Panel > Board (in priority order)
|
|
func _find_drop_target() -> Node:
|
|
# Priority 1: Check for overlapping cards in dropzone
|
|
var closest : Card = null
|
|
for area in get_overlapping_areas():
|
|
if area is StickyNote and not area.is_attached: continue # Can only drop on attached stickies
|
|
|
|
if area is Card:
|
|
if (not closest) or ((closest.position-position).length() < (area.position - position).length()):
|
|
closest = area
|
|
return area
|
|
|
|
# Priority 3: Default to board (stay loose in dropzone)
|
|
return null
|
|
|
|
## Find the nearest panel that can accept this sticky
|
|
func _find_nearest_panel() -> StickyNotePanel:
|
|
if not current_handle or not current_handle.has_node("HBoxContainer/ScrollContainer/VBoxContainer"):
|
|
return null
|
|
|
|
var panel_container := current_handle.get_node("HBoxContainer/ScrollContainer/VBoxContainer")
|
|
var sticky_rect := Rect2(global_position - Vector2(diameter/2, 10), Vector2(diameter/2, 10))
|
|
|
|
# First pass: look for empty panels we're hovering over
|
|
for panel in panel_container.get_children():
|
|
if panel is StickyNotePanel:
|
|
if panel.is_empty() and panel.get_global_rect().intersects(sticky_rect):
|
|
return panel
|
|
|
|
# Second pass: if no empty panel found, find first empty panel
|
|
for panel in panel_container.get_children():
|
|
if panel is StickyNotePanel and panel.is_empty():
|
|
return panel
|
|
|
|
# No empty panels found - will need to create one (handled by board)
|
|
return null
|
|
|
|
|
|
func confine_to_screen() -> void:
|
|
if attached_to is not Card: super.confine_to_screen()
|