class_name ListOrder extends RefCounted ## List view's drag and drop, applied to the spread: reorders cards ## (TowerBoard.tables) and groups (.groups), and moves them in or out of ## groups. A card that joins a group also moves on the spread, to just under ## the group's frame, so the frame doesn't stretch across the spread. ## `what` and `onto` are {kind: "card"|"group", id}; `onto` is {} for empty ## space (the end of the list). `section`: -1 above, 0 onto, 1 below. static func move(m: Main, what: Dictionary, onto: Dictionary, section: int) -> void: m.undo_redo._begin_edit() var into := "" # the group it ends up in if onto.is_empty(): into = "" elif section == 0 and onto["kind"] == "group": into = str(onto["id"]) elif onto["kind"] == "group": into = m.data.get_group(onto["id"]).parent else: var g := m.data.group_of(str(onto["id"])) into = g.id if g else "" var ok := _move_card(m, str(what["id"]), into) if what["kind"] == "card" else m.data.set_group_parent(str(what["id"]), into) if not ok: m.toast("A group can't go inside itself") m.undo_redo._end_edit() return if section != 0 and not onto.is_empty() and onto["kind"] == what["kind"]: _reorder(m, what, onto, section) m.grouping._groups_changed() m.mark_dirty() m.undo_redo._end_edit() static func _move_card(m: Main, tid: String, into: String) -> bool: var old := m.data.group_of(tid) if (old.id if old else "") == into: return true if into == "": m.data.remove_from_group(tid) if old and old.parent != "": m.data.add_to_group(tid, old.parent) return true var g := m.data.get_group(into) m.data.add_to_group(tid, into) # Under the frame's last card, so the frame grows a little, not across the spread. var t := m.data.get_table(tid) var bottom := g.position.y + TableGroup.TITLE_H for other in m.data.tables_in(into): if other != tid: var c := m.card_for(other) var ot := m.data.get_table(other) bottom = maxf(bottom, ot.position.y + (c.size.y if c else 160.0)) t.position = Vector2(g.position.x + TableGroup.PAD, bottom + 24.0).round() var card := m.card_for(tid) if card: card.position = t.position return true static func _reorder(m: Main, what: Dictionary, onto: Dictionary, section: int) -> void: var list: Array = m.data.tables if what["kind"] == "card" else m.data.groups var from := -1 var to := -1 for i in list.size(): if list[i].id == what["id"]: from = i if list[i].id == onto["id"]: to = i if from < 0 or to < 0: return var item: Variant = list[from] list.remove_at(from) if from < to: to -= 1 list.insert(to + (1 if section > 0 else 0), item)