From 2362849ce1058b96149f00d20de6ce1de6196908 Mon Sep 17 00:00:00 2001 From: kobewi Date: Wed, 8 Mar 2023 02:26:17 +0100 Subject: [PATCH] Preserve item ids to avoid shiftings when deleted --- addons/SimpleTODO/SimpleTODO.gd | 3 ++- addons/SimpleTODO/TODOColumn.gd | 17 +++++++++++++++++ addons/SimpleTODO/TODOItem.gd | 2 ++ addons/SimpleTODO/plugin.cfg | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/addons/SimpleTODO/SimpleTODO.gd b/addons/SimpleTODO/SimpleTODO.gd index 6034986..e658450 100644 --- a/addons/SimpleTODO/SimpleTODO.gd +++ b/addons/SimpleTODO/SimpleTODO.gd @@ -77,7 +77,7 @@ func save_data(): if column.item_container.get_child_count() > 0: for item in column.item_container.get_children(): - data.set_value(section, str("item", item.get_index()), item.text_field.text) + data.set_value(section, str("item", item.id), item.text_field.text) else: data.set_value(section, "__none__", "null") @@ -99,6 +99,7 @@ func load_data(): var column_item = column.add_item() column_item.text_field.text = data.get_value(section, item) + column_item.id = item.to_int() todo_screen.undo_redo.clear_history() is_loading = false diff --git a/addons/SimpleTODO/TODOColumn.gd b/addons/SimpleTODO/TODOColumn.gd index 583cb85..b9fb012 100644 --- a/addons/SimpleTODO/TODOColumn.gd +++ b/addons/SimpleTODO/TODOColumn.gd @@ -61,6 +61,7 @@ func _ready() -> void: update_mirror(0) main.connect_scrollbar(update_mirror) + item_container.child_entered_tree.connect(validate_unique_id) item_container.child_entered_tree.connect(update_counter.unbind(1), CONNECT_DEFERRED) item_container.child_exiting_tree.connect(update_counter.unbind(1), CONNECT_DEFERRED) @@ -197,3 +198,19 @@ func get_column_from_mouse_position() -> PanelContainer: if child.get_rect().has_point(Vector2(mouse_position.x, 0)): return child return null + +func validate_unique_id(for_item: Control): + var is_unique := true + for item in item_container.get_children(): + if item != for_item and item.id == for_item.id: + is_unique = false + + if not is_unique: + var id_list := item_container.get_children().map(func(item: Control): return item.id) + + for i in 1000000: + if not i in id_list: + for_item.id = i + return + + push_error("Simple TODO: Unique ID could not be ensured. Farewell.") diff --git a/addons/SimpleTODO/TODOItem.gd b/addons/SimpleTODO/TODOItem.gd index 1a872a9..e447f13 100644 --- a/addons/SimpleTODO/TODOItem.gd +++ b/addons/SimpleTODO/TODOItem.gd @@ -18,6 +18,8 @@ var current_drag_item_index := 0 var is_dragging := false var item_margin := 20 +var id: int + func _ready() -> void: undo_redo = main.undo_redo item_placement_holder = main.item_placement_holder diff --git a/addons/SimpleTODO/plugin.cfg b/addons/SimpleTODO/plugin.cfg index f79e1dc..224fa41 100644 --- a/addons/SimpleTODO/plugin.cfg +++ b/addons/SimpleTODO/plugin.cfg @@ -3,5 +3,5 @@ name="Simple TODO" description="Organize random notes in tabs." author="KoBeWi" -version="1.3a" +version="1.4" script="SimpleTODO.gd" \ No newline at end of file