been working on this today
class_name ItemListColumns extends ItemList
var _rows_count: int = 0
var select_row_to_move_to: int
var column__sorted = {}
func _ready() -> void:
add_item_with_id(["name1", 3, 1, 3], {"id": 1})
add_item_with_id(["name2", 3, 1, 2], {"id": 2})
add_item_with_id(["name3", 3, 2, 2], {"id": 3})
add_item_with_id(["name4", 3, 2, 3], {"id": 4})
add_item_with_id(["name5", 4, 2, 4], {"id": 5})
sort_items_by_column(0, 1, false)
func add_item_with_id(array_columns: Array, meta_data) -> void:
if not array_columns.size() == max_columns:
printerr("array size is not equals to max column size")
_rows_count += 1
for column in array_columns:
var index = add_item(str(column))
set_item_metadata(index, meta_data)
func move_to_row(index: int, to_row: int) -> void:
var i = current_column(index)
# first in row
index -= i
# if same row
if to_row == index / max_columns:
return
if to_row < index / max_columns:
# forward
for ie in max_columns:
_move_all_in_column(index, ie, to_row)
else:
# backward
for ie in range(max_columns-1, -1, -1):
_move_all_in_column(index, ie, to_row)
func _move_all_in_column(index: int, i: int, to_row: int) -> void:
move_item(index + i, i + (max_columns * (to_row) ) )
func current_column(index: int) -> int:
return index % max_columns
func current_row(index: int) -> int:
var i = index % max_columns
# first in row
index -= i
return index / max_columns
func move_to_last_selected(index: int, selected: bool) -> void:
if selected:
select_row_to_move_to = current_row(index)
else:
move_to_row(index, select_row_to_move_to)
## which column to sort
## how many rows to not sort
## in which order
func sort_items_by_column(column: int, execlude_rows: int = 1, reverse: bool = false):
var index: int = execlude_rows
var text_item
var text_item1
if reverse:
index = _rows_count
while index > -1 + execlude_rows:
text_item = _get_row_text(column, index)
text_item1 = _get_row_text(column, index +1)
if text_item and text_item1 and text_item < text_item1:
move_to_row(index * max_columns, index + 1)
index += 1
continue
index -= 1
else:
while index < _rows_count:
text_item = _get_row_text(column, index)
text_item1 = _get_row_text(column, index +1)
if text_item and text_item1 and text_item > text_item1:
move_to_row(index * max_columns, index + 1)
index -= 1
if index < execlude_rows:
index = execlude_rows
continue
index += 1
func _get_row_text(column: int, row: int):
if row <= _rows_count:
return get_item_text(column + max_columns * row)
func _on_item_selected(index: int) -> void:
print(current_column(index))
func sort_first_row_column(index: int):
if current_row(index) == 0:
var _current_column = current_column(index)
var reverse = column__sorted.get_or_add(_current_column, true)
reverse = not reverse
column__sorted[_current_column] = reverse
print(reverse)
sort_items_by_column(_current_column, 1, reverse)
func _on_multi_selected(index: int, selected: bool) -> void:
sort_first_row_column(index)
print(current_row(index))
print(current_column(index))