From 09e306c974c43db47576248a454c7cc893708835 Mon Sep 17 00:00:00 2001 From: betalars Date: Fri, 31 Jan 2025 03:22:07 +0100 Subject: [PATCH] removing many mouse movement bugs --- src/logic-scenes/board/card-board.gd | 56 ++++++++++++++--------- src/logic-scenes/board/card.gd | 17 +++++-- src/logic-scenes/board/card.tscn | 4 -- src/logic-scenes/board/sticky-note.gd | 60 +++++++++++++++++++------ src/logic-scenes/board/sticky-note.tscn | 4 -- src/logic-scenes/board/void_card.tscn | 4 -- 6 files changed, 96 insertions(+), 49 deletions(-) diff --git a/src/logic-scenes/board/card-board.gd b/src/logic-scenes/board/card-board.gd index 73950f9..ed50415 100644 --- a/src/logic-scenes/board/card-board.gd +++ b/src/logic-scenes/board/card-board.gd @@ -12,13 +12,15 @@ var focus_stickies:bool = true: set(stickies): if stickies and sticky_note_container.get_child_count() == 0: return - focus_stickies = stickies - - if not current_context == ASSIGN: - if stickies: - current_sticky_note_id = current_sticky_note_id - else: - current_dropzone_id = current_dropzone_id + # this messes things up if called unneeded. + if focus_stickies != stickies: + focus_stickies = stickies + + if not current_context == ASSIGN: + if stickies: + current_sticky_note_id = current_sticky_note_id + else: + current_dropzone_id = current_dropzone_id var has_stage = false: set(focus): @@ -32,6 +34,9 @@ var has_stage = false: process_mode = Node.PROCESS_MODE_INHERIT else: process_mode = Node.PROCESS_MODE_DISABLED + for sticky in dropzone.get_children(): + if sticky is StickyNote: + sticky.is_dragged = false visible = has_stage @onready var dropzone = $HBoxContainer/dropzone @@ -69,11 +74,13 @@ var mementos_collected: int = 0: @onready var currently_active_node: Area2D = null: set(new_node): - if not currently_active_node == null: - currently_active_node.highlighted = false - currently_active_node = new_node - if not currently_active_node == null: - currently_active_node.highlighted = true + # this makes sure no accidental context switches can happen while a card is being dragged. + if not current_context == DRAG: + if not currently_active_node == null: + currently_active_node.highlighted = false + currently_active_node = new_node + if not currently_active_node == null: + currently_active_node.highlighted = true @onready var current_dropzone_id: int = 0: set(new_id): @@ -157,16 +164,16 @@ func add_sticky_note(sticky: StickyNote): func is_in_dropzone(to_check: Node) -> bool: return dropzone.get_rect().has_point(to_check.global_position) -# called if a mouse button is pressed -func handle_mouse_button(to_handle: Area2D, input: InputEvent): - # No two areas can be dragged at the same time. - # Make sure that only the same area is dragged. +# Called by notes when a mouse event needs handling +func handle_mouse_button(input: InputEventMouseButton, to_handle = currently_active_node): + + # Makes sure that only the same area is dragged. # Otherwise overlapping areas are dragged at the same time. if current_context == DRAG and to_handle != currently_active_node: return - currently_active_node = to_handle # update currently selected - if input.is_action_pressed("mouse_left"): + if input.button_index == MOUSE_BUTTON_MASK_LEFT and input.pressed: + currently_active_node = to_handle to_handle.is_dragged = true if to_handle is StickyNote: if not to_handle.on_board: @@ -176,7 +183,7 @@ func handle_mouse_button(to_handle: Area2D, input: InputEvent): current_context = DRAG # when Drag stops ... - if input.is_action_released("mouse_left"): + if input.button_index == MOUSE_BUTTON_MASK_LEFT and not input.pressed: to_handle.is_dragged = false if to_handle is StickyNote: if is_in_dropzone(to_handle): @@ -216,6 +223,7 @@ func handle_mouse_button(to_handle: Area2D, input: InputEvent): if input.is_action_pressed("mouse_right") and current_context == DRAG: to_handle.reset_drag() + func _return_sticky_notes_to_panels(): for panel in sticky_note_container.get_children(): panel.reclaim_sticky_note() @@ -272,7 +280,15 @@ func _input(event): State.leave_stage(self) # Return, if the input is a mouse event (mouse events are handled separately) - if event is InputEventMouse or !has_stage or not is_instance_valid(currently_active_node): return + if not has_stage or not is_instance_valid(currently_active_node): return + + if event is InputEventMouse: + # makes sure to pass release events so notes do not get attached to the mouse while the cursor leaves the area. + if event is InputEventMouseButton and current_context == DRAG: + if event.button_index == MOUSE_BUTTON_LEFT and not event.pressed: + handle_mouse_button(event) + else: + return if current_context != DRAG: if event.is_action_pressed("ui_up"): diff --git a/src/logic-scenes/board/card.gd b/src/logic-scenes/board/card.gd index 8c61d37..f7c41c2 100644 --- a/src/logic-scenes/board/card.gd +++ b/src/logic-scenes/board/card.gd @@ -76,6 +76,10 @@ var mouse_offset: Vector2 func _ready(): + input_event.connect(_on_input_event) + mouse_entered.connect(_on_mouse_entered) + mouse_exited.connect(_on_mouse_exited) + _handle_wiggle(0) if not Engine.is_editor_hint() and is_inside_tree(): for sticky_note in self.get_children(): @@ -130,7 +134,12 @@ func replace_with(card: Card): self.own_sticky_notes = card.own_sticky_notes self.voice_line = card.voice_line self.name = card.name - + +func _input(event: InputEvent) -> void: + if event is InputEventMouseButton: + if event.button_index == MOUSE_BUTTON_LEFT and not event.pressed: + is_dragged = false + func _on_focus_entered(): print(self, "is focused") @@ -142,7 +151,7 @@ func _on_mouse_entered(): if not Input.is_action_pressed("mouse_left"): # Do nothing if mouse hovers over sticky_note if has_sticky_note_attached(): - if get_child(-1).highlighted: + if current_sticky_note.highlighted: return highlighted = true if "handle_hover" in owner: @@ -162,7 +171,7 @@ func _on_input_event(viewport, event, shape_idx): if "handle_mouse_button" in owner: mouse_offset = (get_viewport().get_mouse_position() - position) if highlighted: - owner.handle_mouse_button(self, event) + owner.handle_mouse_button(event, self) func _move_card(): if is_dragged: @@ -205,7 +214,7 @@ func exchange_sticky_note_with(new_note: StickyNote) -> StickyNote: attach_sticky_note(new_note) return tmp -## TODO why does this exist? +# This makes sure this node highlights itself when focus has left the sticky note. func check_hover(): if is_mouse_entered: _on_mouse_entered() diff --git a/src/logic-scenes/board/card.tscn b/src/logic-scenes/board/card.tscn index 46a89aa..428b104 100644 --- a/src/logic-scenes/board/card.tscn +++ b/src/logic-scenes/board/card.tscn @@ -85,7 +85,3 @@ autowrap_mode = 3 [node name="sticky note anchor" type="Node2D" parent="."] position = Vector2(-66, 83) - -[connection signal="input_event" from="." to="." method="_on_input_event"] -[connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"] -[connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"] diff --git a/src/logic-scenes/board/sticky-note.gd b/src/logic-scenes/board/sticky-note.gd index ff7d0b9..7b69e42 100644 --- a/src/logic-scenes/board/sticky-note.gd +++ b/src/logic-scenes/board/sticky-note.gd @@ -2,9 +2,13 @@ extends Area2D class_name StickyNote -var sibling -var shift_tween -var modulate_tween +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 + signal transform_tween_finished @@ -50,6 +54,7 @@ var is_dragged: bool = false: set(dragged): is_dragged = dragged z_index = int(dragged) + if not is_dragged: highlighted = false var initial_drag_position: Vector2 var mouse_diff: Vector2 @@ -61,6 +66,12 @@ var attached_to: Node = null func _ready() -> void: + 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) + $Content/Label.text = self.text $Content/BackgroundSprite.frame = text.hash() % $Content/BackgroundSprite.sprite_frames.get_frame_count($Content/BackgroundSprite.animation) @@ -89,12 +100,6 @@ func _process(delta: float) -> void: _move_sticky_note() -func _on_focus_entered(): - print(self, "is focused") - -func _on_focus_exited(): - print(self, "is not focused") - func _on_mouse_entered(): if not Input.is_action_pressed("mouse_left"): highlighted = true @@ -102,26 +107,55 @@ func _on_mouse_entered(): owner.handle_hover(self) func _on_mouse_exited(): - highlighted = false if is_sticky_note_attached() and "check_hover" in get_parent(): get_parent().check_hover() + + if not is_dragged: + highlighted = false + +func _on_area_enter(card: Area2D): + print(card) + 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) + card_stick_tween.tween_property(self, "hover_pos_shift", 0.3, 0.3) + else: + hovering_cards.append(card) + +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 func _on_input_event(viewport, event, shape_idx): - if event is InputEventMouseButton: if event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_RIGHT: if "handle_mouse_button" in owner: mouse_diff = get_viewport().get_mouse_position() initial_drag_position = global_position - owner.handle_mouse_button(self, event) + owner.handle_mouse_button(event, self) func _move_sticky_note(): if is_dragged: position = initial_drag_position + get_viewport().get_mouse_position() - mouse_diff + + 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 func is_sticky_note_attached() -> bool: # there is probably a nicer way to do this - return self.get_parent().get_parent() is Card + return self.get_parent() is Card func tween_transform_to(target: Transform2D): var transform_tween: Tween = create_tween() diff --git a/src/logic-scenes/board/sticky-note.tscn b/src/logic-scenes/board/sticky-note.tscn index 115468b..0896720 100644 --- a/src/logic-scenes/board/sticky-note.tscn +++ b/src/logic-scenes/board/sticky-note.tscn @@ -134,7 +134,3 @@ theme = ExtResource("3_qmm0h") theme_type_variation = &"card_text" vertical_alignment = 1 autowrap_mode = 3 - -[connection signal="input_event" from="." to="." method="_on_input_event"] -[connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"] -[connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"] diff --git a/src/logic-scenes/board/void_card.tscn b/src/logic-scenes/board/void_card.tscn index 9731dc6..651ba1e 100644 --- a/src/logic-scenes/board/void_card.tscn +++ b/src/logic-scenes/board/void_card.tscn @@ -184,7 +184,3 @@ autowrap_mode = 3 [node name="sticky note anchor" type="Node2D" parent="."] position = Vector2(-65.6478, 60.3852) - -[connection signal="input_event" from="." to="." method="_on_input_event"] -[connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"] -[connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"]