frame-of-mind/src/logic-scenes/board/sticky-note.gd

180 lines
5.8 KiB
GDScript3
Raw Normal View History

2023-04-19 16:53:24 +00:00
@tool
2023-06-28 10:18:00 +00:00
extends Area2D
2023-10-12 16:25:21 +00:00
class_name StickyNote
2025-01-31 02:22:07 +00:00
var sibling: StickyNote
var shift_tween: Tween
var modulate_tween: Tween
var card_stick_tween: Tween
var hovering_cards: Array[Card]
var hover_pos_shift: float
2025-02-24 15:14:08 +00:00
var attached_to: Node = null:
set(new_attatchement):
attached_to = new_attatchement
# cannot be explicitly typed, as this can be bnoth handled by picker and physics-board
var current_handle
2023-04-19 16:53:24 +00:00
2023-09-23 14:19:58 +00:00
signal transform_tween_finished
2023-04-19 16:53:24 +00:00
@export var text: String = "" :
2024-09-15 09:30:31 +00:00
set (value):
if is_inside_tree() or Engine.is_editor_hint():
$Content/Label.text = value
$Content/BackgroundSprite.frame = text.hash() % $Content/BackgroundSprite.sprite_frames.get_frame_count($Content/BackgroundSprite.animation)
text = value
@export var shift_by: Vector2 = Vector2(-32, 0)
@export_color_no_alpha var highlight_color: Color = Color(1.5, 1.5, 1.5)
@export var highlighted: bool = false:
2024-09-15 09:30:31 +00:00
set(highlight):
if highlight != highlighted:
highlighted = highlight
2025-02-24 15:14:08 +00:00
print("hightlight_set")
2024-09-15 09:30:31 +00:00
if is_inside_tree() and is_node_ready():
if modulate_tween: modulate_tween.kill()
if shift_tween: shift_tween.kill()
if highlighted:
2025-02-24 15:14:08 +00:00
var modulate_tween = get_tree().create_tween()
2024-09-15 09:30:31 +00:00
modulate_tween.tween_property(self, "modulate", highlight_color, 0.1)
2025-02-24 15:14:08 +00:00
var shift_tween = get_tree().create_tween()
2024-09-15 09:30:31 +00:00
shift_tween.tween_property($Content, "position", shift_by, 0.2)
else:
modulate_tween = get_tree().create_tween()
modulate_tween.tween_property(self, "modulate", Color(1, 1, 1), 0.3)
shift_tween = get_tree().create_tween()
shift_tween.tween_property($Content, "position", Vector2.ZERO, 0.5)
else:
if highlighted:
modulate = Color(1, 1, 1)
else:
modulate = Color(1, 1, 1)
2023-04-19 16:53:24 +00:00
@export var voice_line: AudioStream = null
@export var is_dragable: bool = false
@onready var base_rotation = rotation
@onready var base_scale = scale
2023-07-14 19:36:20 +00:00
var is_dragged: bool = false:
2024-09-15 09:30:31 +00:00
set(dragged):
is_dragged = dragged
z_index = int(dragged)
2025-01-31 02:22:07 +00:00
if not is_dragged: highlighted = false
2023-07-14 19:36:20 +00:00
2023-11-01 22:19:47 +00:00
var initial_drag_position: Vector2
var mouse_diff: Vector2
2023-04-19 16:53:24 +00:00
@onready var diameter = $CollisionShape2D.shape.height
@export_range(1.0, 10.0) var bounce_speed: float = 8
var on_board: bool = false
2025-02-24 15:14:08 +00:00
2023-04-19 16:53:24 +00:00
func _ready() -> void:
2024-09-15 09:30:31 +00:00
2025-01-31 02:22:07 +00:00
input_event.connect(_on_input_event)
mouse_entered.connect(_on_mouse_entered)
mouse_exited.connect(_on_mouse_exited)
area_entered.connect(_on_area_enter)
area_exited.connect(_on_area_exit)
2024-09-15 09:30:31 +00:00
$Content/Label.text = self.text
$Content/BackgroundSprite.frame = text.hash() % $Content/BackgroundSprite.sprite_frames.get_frame_count($Content/BackgroundSprite.animation)
$Content/Label.theme = State.current_main_theme
State.theme_changed.connect(func change_theme(new_theme): $Content/Label.theme = new_theme)
2023-04-19 16:53:24 +00:00
2023-10-12 16:25:21 +00:00
func replace_with(sticky_note: StickyNote):
2024-09-15 09:30:31 +00:00
self.text = sticky_note.text
self.voice_line = sticky_note.voice_line
self.sibling = sticky_note.sibling
self.name = sticky_note.name
for group in self.get_groups():
self.remove_from_group(group)
for group in sticky_note.get_groups():
self.add_to_group(group)
func _process(delta: float) -> void:
2024-09-15 09:30:31 +00:00
if get_overlapping_areas().size() > 0 and is_dragable and on_board:
for area in get_overlapping_areas():
if area is Card or area is CardCollider:
if area is CardCollider:
position += area.direction * delta
elif not area.highlighted or self.highlighted:
var diff:Vector2 = position - area.position
position -= diff.normalized() * ((diff.length()-diameter)/diameter) * bounce_speed * (delta/(1.0/60))
_move_sticky_note()
func _on_mouse_entered():
2024-09-15 09:30:31 +00:00
if not Input.is_action_pressed("mouse_left"):
highlighted = true
2025-02-24 15:14:08 +00:00
if "handle_hover" in current_handle:
current_handle.handle_hover(self)
func _on_mouse_exited():
2025-01-31 02:22:07 +00:00
if not is_dragged:
highlighted = false
2025-02-24 15:14:08 +00:00
if is_sticky_note_attached() and "check_hover" in attached_to:
attached_to.check_hover()
2025-01-31 02:22:07 +00:00
func _on_area_enter(card: Area2D):
if card is Card:
if hovering_cards == []:
hovering_cards = [card]
card_stick_tween = get_tree().create_tween()
card_stick_tween.set_ease(Tween.EASE_IN_OUT)
2025-02-24 15:14:08 +00:00
card_stick_tween.tween_property(self, "hover_pos_shift", 0.3, 0.1)
2025-01-31 02:22:07 +00:00
else:
hovering_cards.append(card)
2025-02-24 15:14:08 +00:00
elif card is StickyNote and is_sticky_note_in_panel() and not is_dragged:
attached_to.create_gap()
2025-01-31 02:22:07 +00:00
func _on_area_exit(card: Area2D):
if hovering_cards.has(card):
hovering_cards.erase(card)
card.highlighted = false
if hovering_cards == []:
hover_pos_shift = 0
$Content.position = Vector2.ZERO
2025-02-24 15:14:08 +00:00
elif card is StickyNote and is_sticky_note_in_panel():
attached_to.collapse_gap()
func _on_input_event(viewport, event, shape_idx):
2024-09-15 09:30:31 +00:00
if event is InputEventMouseButton:
2025-02-24 15:14:08 +00:00
if (event.button_index == MOUSE_BUTTON_LEFT and event.pressed) or event.button_index == MOUSE_BUTTON_RIGHT:
if "handle_hover" in current_handle:
2024-09-15 09:30:31 +00:00
mouse_diff = get_viewport().get_mouse_position()
initial_drag_position = global_position
2025-02-24 15:14:08 +00:00
current_handle.handle_mouse_button(event, self)
2023-10-12 16:25:21 +00:00
func _move_sticky_note():
2024-09-15 09:30:31 +00:00
if is_dragged:
2025-02-24 15:14:08 +00:00
var old_position = position
2024-09-15 09:30:31 +00:00
position = initial_drag_position + get_viewport().get_mouse_position() - mouse_diff
2025-01-31 02:22:07 +00:00
if hovering_cards != []:
var closest: Card = hovering_cards[0]
for card in hovering_cards:
card.highlighted = false
if (closest.position - position).length() > (closest.position - position).length():
card = closest
closest.highlighted = true
$Content.position = (closest.sticky_note_anchor.global_position - global_position) * hover_pos_shift
2023-07-02 08:42:58 +00:00
2023-10-12 16:25:21 +00:00
func is_sticky_note_attached() -> bool:
2025-02-24 15:14:08 +00:00
return attached_to is Card
func is_sticky_note_in_panel() -> bool:
return attached_to is StickyNotePanel
2023-07-02 13:10:33 +00:00
2023-10-12 16:25:21 +00:00
func tween_transform_to(target: Transform2D):
2024-09-15 09:30:31 +00:00
var transform_tween: Tween = create_tween()
transform_tween.tween_property(self, "transform", target, 0.25)
await transform_tween.finished
emit_signal("transform_tween_finished")
2023-11-01 22:19:47 +00:00
func reset_drag():
2024-09-15 09:30:31 +00:00
if attached_to != null:
attached_to.reclaim_sticky_note()