2026-11-10 18:50:32 -06:00
class_name ListTree
extends Tree
## List view's tree: groups as folders (nested), cards as rows with their
## threads and a Roll cell. Drag a row to reorder it or drop it on a folder to
## move it in; the order is the spread's own (TowerBoard.tables and .groups).
const COL_NAME : = 0
const COL_THREADS : = 1
const COL_ROLL : = 2
var m : Main
var filter : = " "
func _init ( p_main : Main ) - > void :
m = p_main
columns = 3
hide_root = true
column_titles_visible = false
select_mode = Tree . SELECT_ROW
allow_reselect = true
set_column_expand ( COL_NAME , true )
set_column_expand ( COL_THREADS , false )
set_column_custom_minimum_width ( COL_THREADS , 64 )
set_column_expand ( COL_ROLL , false )
set_column_custom_minimum_width ( COL_ROLL , 54 )
add_theme_font_override ( " font " , Style . body ( ) )
add_theme_font_size_override ( " font_size " , 15 )
add_theme_color_override ( " font_color " , Style . INK )
add_theme_color_override ( " font_selected_color " , Style . INK )
add_theme_constant_override ( " v_separation " , 6 )
add_theme_stylebox_override ( " panel " , Style . box ( Style . CARD , 0 , [ 4 , 4 , 4 , 4 ] ) )
add_theme_stylebox_override ( " selected " , Style . box ( Style . LIT , 4 ) )
add_theme_stylebox_override ( " selected_focus " , Style . box ( Style . LIT , 4 ) )
add_theme_stylebox_override ( " focus " , StyleBoxEmpty . new ( ) )
2026-11-14 13:59:47 -06:00
# Hover from the theme too: the engine's default hover (dark, with light
# text) made rows unreadable on light cards (Tarot, playtest 2026-11-14).
var hover : = Style . box ( Color ( Style . LIT , 0.55 ) , 4 )
for box_name in [ " hovered " , " hovered_dimmed " , " hovered_selected " , " hovered_selected_focus " ] :
add_theme_stylebox_override ( box_name , hover if not box_name . begins_with ( " hovered_selected " ) else Style . box ( Style . LIT , 4 ) )
for colour_name in [ " font_hovered_color " , " font_hovered_dimmed_color " , " font_hovered_selected_color " ] :
add_theme_color_override ( colour_name , Style . INK )
2026-11-10 18:50:32 -06:00
item_mouse_selected . connect ( _on_click )
item_activated . connect ( _on_activated )
item_collapsed . connect ( _on_folder_toggled )
## Rebuilds every row from the spread, keeping the open folders and selection.
func rebuild ( ) - > void :
var sel : = _meta ( get_selected ( ) )
clear ( )
var root : = create_item ( )
_fill ( root , " " )
if not sel . is_empty ( ) :
_select_meta ( root , sel )
func _fill ( parent : TreeItem , gid : String ) - > void :
for g in m . data . groups :
if g . parent == gid :
var it : = create_item ( parent )
it . set_text ( COL_NAME , Formatter . strip_tags ( g . name ) )
it . set_custom_font ( COL_NAME , Style . body_bold ( ) )
it . set_custom_color ( COL_NAME , Style . TRIM_DARK )
it . set_metadata ( COL_NAME , { " kind " : " group " , " id " : g . id } )
it . set_tooltip_text ( COL_NAME , " Folder (a group on the spread): %s " % MainSelection . describe_counts ( m . data . tables_in ( g . id ) . size ( ) , m . data . groups_in ( g . id ) . size ( ) - 1 ) )
_fill ( it , g . id )
it . collapsed = g . collapsed and filter == " "
if filter != " " and it . get_first_child ( ) == null :
it . free ( )
var members : = { }
var g0 : = m . data . get_group ( gid )
if g0 :
for tid in g0 . members :
members [ tid ] = true
for t in m . data . tables :
if ( gid == " " and m . data . group_of ( t . id ) == null ) or members . has ( t . id ) :
if filter != " " and not _matches ( t ) :
continue
_card_row ( parent , t )
func _card_row ( parent : TreeItem , t : RollTable ) - > void :
var it : = create_item ( parent )
it . set_text ( COL_NAME , Formatter . strip_tags ( t . name ) )
it . set_tooltip_text ( COL_NAME , Describe . kind_label ( t ) )
if t . color != " " :
it . set_custom_color ( COL_NAME , Color ( t . color ) . darkened ( 0.2 ) )
it . set_metadata ( COL_NAME , { " kind " : " card " , " id " : t . id } )
var ins : = _threads_in ( t . id )
var outs : = _threads_out ( t )
var counts : = PackedStringArray ( )
if not ins . is_empty ( ) :
counts . append ( " ← %d " % ins . size ( ) )
if not outs . is_empty ( ) :
counts . append ( " → %d " % outs . size ( ) )
it . set_text ( COL_THREADS , " " . join ( counts ) )
it . set_custom_color ( COL_THREADS , Style . MUTED )
var tip : = PackedStringArray ( )
if not ins . is_empty ( ) :
tip . append ( " Threads in: \n " + " \n " . join ( ins ) )
if not outs . is_empty ( ) :
tip . append ( " Threads out: \n " + " \n " . join ( outs ) )
it . set_tooltip_text ( COL_THREADS , " \n " . join ( tip ) if not tip . is_empty ( ) else " No threads " )
if t . kind != RollTable . KIND_VARIABLE :
it . set_text ( COL_ROLL , " Roll " )
it . set_custom_color ( COL_ROLL , Style . TRIM_DARK )
it . set_custom_font ( COL_ROLL , Style . body_bold ( ) )
it . set_tooltip_text ( COL_ROLL , " Roll %s " % Formatter . strip_tags ( t . name ) )
func _threads_in ( tid : String ) - > PackedStringArray :
var out : = PackedStringArray ( )
for src in m . data . tables :
for e in src . entries :
if e . pipes . has ( tid ) :
out . append ( " from %s · %s " % [ Formatter . strip_tags ( src . name ) , Formatter . strip_tags ( e . text ) . left ( 40 ) ] )
return out
func _threads_out ( t : RollTable ) - > PackedStringArray :
var out : = PackedStringArray ( )
for e in t . entries :
for to in e . pipes :
var target : = m . data . get_table ( to )
out . append ( " %s → %s " % [ Formatter . strip_tags ( e . text ) . left ( 40 ) , Formatter . strip_tags ( target . name ) if target else to ] )
return out
func _matches ( t : RollTable ) - > bool :
if Formatter . strip_tags ( t . name ) . to_lower ( ) . contains ( filter ) :
return true
for e in t . entries :
if Formatter . strip_tags ( e . text ) . to_lower ( ) . contains ( filter ) :
return true
return false
# ---------- Clicks ----------
func _on_click ( at : Vector2 , button : int ) - > void :
var meta : = _meta ( get_selected ( ) )
if meta . is_empty ( ) :
return
if button == MOUSE_BUTTON_RIGHT :
var list : = Actions . card ( m , m . data . get_table ( meta [ " id " ] ) ) if meta [ " kind " ] == " card " else Actions . group ( m , m . data . get_group ( meta [ " id " ] ) )
Actions . popup_at ( m , list , get_screen_transform ( ) * at )
return
if meta [ " kind " ] == " card " :
if get_column_at_position ( at ) == COL_ROLL :
m . rolling . start_roll ( str ( meta [ " id " ] ) )
elif m . mode == Main . Mode . BUILD :
m . select_card ( str ( meta [ " id " ] ) )
## Double-click or Enter: show the card on the spread.
func _on_activated ( ) - > void :
var meta : = _meta ( get_selected ( ) )
if meta . get ( " kind " ) == " card " :
m . focus_table ( str ( meta [ " id " ] ) )
## Opening or closing a folder opens or closes its group on the spread too.
func _on_folder_toggled ( it : TreeItem ) - > void :
var meta : = _meta ( it )
if meta . get ( " kind " ) == " group " and filter == " " :
var g : = m . data . get_group ( meta [ " id " ] )
if g and g . collapsed != it . collapsed :
m . grouping . set_group_collapsed ( g . id , it . collapsed )
static func _meta ( it : TreeItem ) - > Dictionary :
if it == null :
return { }
var v : Variant = it . get_metadata ( COL_NAME )
return v if v is Dictionary else { }
func _select_meta ( it : TreeItem , want : Dictionary ) - > bool :
var c : = it . get_first_child ( )
while c :
if _meta ( c ) == want :
c . select ( COL_NAME )
return true
if _select_meta ( c , want ) :
return true
c = c . get_next ( )
return false
# ---------- Drag and drop ----------
func _get_drag_data ( at : Vector2 ) - > Variant :
var it : = get_item_at_position ( at )
var meta : = _meta ( it )
if meta . is_empty ( ) or filter != " " :
return null
var l : = Label . new ( )
l . text = it . get_text ( COL_NAME )
set_drag_preview ( l )
drop_mode_flags = DROP_MODE_ON_ITEM | DROP_MODE_INBETWEEN
return { " list_item " : meta }
func _can_drop_data ( at : Vector2 , data : Variant ) - > bool :
if not ( data is Dictionary and ( data as Dictionary ) . has ( " list_item " ) ) :
return false
var target : = _meta ( get_item_at_position ( at ) )
return target . is_empty ( ) or target != data [ " list_item " ]
func _drop_data ( at : Vector2 , data : Variant ) - > void :
drop_mode_flags = DROP_MODE_DISABLED
var it : = get_item_at_position ( at )
ListOrder . move ( m , data [ " list_item " ] , _meta ( it ) , get_drop_section_at_position ( at ) )