Preserve item ids to avoid shiftings when deleted

This commit is contained in:
kobewi 2023-03-08 02:26:17 +01:00
parent 853147b932
commit 2362849ce1
4 changed files with 22 additions and 2 deletions

View File

@ -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

View File

@ -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.")

View File

@ -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

View File

@ -3,5 +3,5 @@
name="Simple TODO"
description="Organize random notes in tabs."
author="KoBeWi"
version="1.3a"
version="1.4"
script="SimpleTODO.gd"