From 3fc854d3dfed831172ee4dcef11416dbc0fe3a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20G=C3=B3mez?= Date: Wed, 6 Aug 2025 12:12:15 +0200 Subject: [PATCH 1/2] Add move_card method. --- addons/card_3d/scripts/card_collection_3d.gd | 20 +++++++++++++++++++- addons/card_3d/scripts/drag_controller.gd | 3 +-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/addons/card_3d/scripts/card_collection_3d.gd b/addons/card_3d/scripts/card_collection_3d.gd index 37f7d8b..e089266 100644 --- a/addons/card_3d/scripts/card_collection_3d.gd +++ b/addons/card_3d/scripts/card_collection_3d.gd @@ -21,6 +21,8 @@ signal mouse_exit_drop_zone() signal card_selected(card) signal card_clicked(card) signal card_added(card) +signal card_moved(card,from,to) + const _DEFAULT_DROP_ZONE_SHAPE_3D: Shape3D = preload("res://addons/card_3d/shapes_3d/default_card_collection_3d_drop_zone_shape_3d.tres") const _DEFAULT_DROP_ZONE_Z_OFFSET: float = 1.6 @@ -98,7 +100,7 @@ func remove_card(index: int) -> Card3D: remove_child(removed_card) apply_card_layout() - + removed_card.card_3d_mouse_down.disconnect(_on_card_pressed.bind(removed_card)) removed_card.card_3d_mouse_up.disconnect(_on_card_clicked.bind(removed_card)) removed_card.card_3d_mouse_over.disconnect(_on_card_hover.bind(removed_card)) @@ -125,6 +127,22 @@ func remove_all() -> Array[Card3D]: return cards_to_return +func move_card(card_to_move: Card3D, new_index: int) -> void: + var current_index: int = card_indicies[card_to_move] + + cards.remove_at(current_index) + + for i in range(current_index, cards.size()): + card_indicies[cards[i]] = i + + cards.insert(new_index, card_to_move) + + for i in range(new_index, cards.size()): + card_indicies[cards[i]] = i + + apply_card_layout() + card_moved.emit(card_to_move,current_index,new_index) + func apply_card_layout(): card_layout_strategy.update_card_positions(cards, card_move_tween_duration) diff --git a/addons/card_3d/scripts/drag_controller.gd b/addons/card_3d/scripts/drag_controller.gd index b4e10a7..79f18ca 100644 --- a/addons/card_3d/scripts/drag_controller.gd +++ b/addons/card_3d/scripts/drag_controller.gd @@ -103,8 +103,7 @@ func _return_card_to_collection(mouse_position: Vector2): new_index = clamp(new_index, 0, _drag_from_collection.cards.size() - 1) if current_index != new_index: - _drag_from_collection.remove_card(current_index) - _drag_from_collection.insert_card(_dragging_card, new_index) + _drag_from_collection.move_card(_dragging_card,new_index) card_moved.emit(_dragging_card, _drag_from_collection, _drag_from_collection, current_index, new_index) _drag_from_collection.apply_card_layout() From 6da86eae9a533b974c2a4f6360c4c1b6a6200720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20G=C3=B3mez?= Date: Thu, 7 Aug 2025 11:32:38 +0200 Subject: [PATCH 2/2] Simplified card indices update. --- addons/card_3d/scripts/card_collection_3d.gd | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/addons/card_3d/scripts/card_collection_3d.gd b/addons/card_3d/scripts/card_collection_3d.gd index e089266..8fea04e 100644 --- a/addons/card_3d/scripts/card_collection_3d.gd +++ b/addons/card_3d/scripts/card_collection_3d.gd @@ -126,20 +126,17 @@ func remove_all() -> Array[Card3D]: return cards_to_return - func move_card(card_to_move: Card3D, new_index: int) -> void: var current_index: int = card_indicies[card_to_move] - - cards.remove_at(current_index) - - for i in range(current_index, cards.size()): - card_indicies[cards[i]] = i + cards.remove_at(current_index) cards.insert(new_index, card_to_move) - for i in range(new_index, cards.size()): + var from = min(current_index, new_index) + var to = max(current_index, new_index) + 1 + for i in range(from, to): card_indicies[cards[i]] = i - + apply_card_layout() card_moved.emit(card_to_move,current_index,new_index)